Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-05-30 02:10:01

0001 #ifndef HeterogeneousCore_CUDACore_ContextState_h
0002 #define HeterogeneousCore_CUDACore_ContextState_h
0003 
0004 #include "HeterogeneousCore/CUDAUtilities/interface/SharedStreamPtr.h"
0005 
0006 #include <memory>
0007 
0008 namespace cms {
0009   namespace cuda {
0010     /**
0011      * The purpose of this class is to deliver the device and CUDA stream
0012      * information from ExternalWork's acquire() to producer() via a
0013      * member/StreamCache variable.
0014      */
0015     class ContextState {
0016     public:
0017       ContextState() = default;
0018       ~ContextState() = default;
0019 
0020       ContextState(const ContextState&) = delete;
0021       ContextState& operator=(const ContextState&) = delete;
0022       ContextState(ContextState&&) = delete;
0023       ContextState& operator=(ContextState&& other) = delete;
0024 
0025     private:
0026       friend class ScopedContextAcquire;
0027       friend class ScopedContextProduce;
0028 
0029       void set(int device, SharedStreamPtr stream) {
0030         throwIfStream();
0031         device_ = device;
0032         stream_ = std::move(stream);
0033       }
0034 
0035       int device() const { return device_; }
0036 
0037       const SharedStreamPtr& streamPtr() const {
0038         throwIfNoStream();
0039         return stream_;
0040       }
0041 
0042       SharedStreamPtr releaseStreamPtr() {
0043         throwIfNoStream();
0044         // This function needs to effectively reset stream_ (i.e. stream_
0045         // must be empty after this function). This behavior ensures that
0046         // the SharedStreamPtr is not hold for inadvertedly long (i.e. to
0047         // the next event), and is checked at run time.
0048         return std::move(stream_);
0049       }
0050 
0051       void throwIfStream() const;
0052       void throwIfNoStream() const;
0053 
0054       SharedStreamPtr stream_;
0055       int device_;
0056     };
0057   }  // namespace cuda
0058 }  // namespace cms
0059 
0060 #endif