Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:43

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       friend class ScopedContextTask;
0029 
0030       void set(int device, SharedStreamPtr stream) {
0031         throwIfStream();
0032         device_ = device;
0033         stream_ = std::move(stream);
0034       }
0035 
0036       int device() const { return device_; }
0037 
0038       const SharedStreamPtr& streamPtr() const {
0039         throwIfNoStream();
0040         return stream_;
0041       }
0042 
0043       SharedStreamPtr releaseStreamPtr() {
0044         throwIfNoStream();
0045         // This function needs to effectively reset stream_ (i.e. stream_
0046         // must be empty after this function). This behavior ensures that
0047         // the SharedStreamPtr is not hold for inadvertedly long (i.e. to
0048         // the next event), and is checked at run time.
0049         return std::move(stream_);
0050       }
0051 
0052       void throwIfStream() const;
0053       void throwIfNoStream() const;
0054 
0055       SharedStreamPtr stream_;
0056       int device_;
0057     };
0058   }  // namespace cuda
0059 }  // namespace cms
0060 
0061 #endif