Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-21 22:40:09

0001 #ifndef HeterogeneousCore_AlpakaCore_interface_ContextState_h
0002 #define HeterogeneousCore_AlpakaCore_interface_ContextState_h
0003 
0004 #include <memory>
0005 #include <stdexcept>
0006 #include <utility>
0007 
0008 #include <alpaka/alpaka.hpp>
0009 
0010 #include "FWCore/Utilities/interface/Exception.h"
0011 #include "HeterogeneousCore/AlpakaInterface/interface/ScopedContextFwd.h"
0012 #include "HeterogeneousCore/AlpakaInterface/interface/traits.h"
0013 
0014 namespace cms::alpakatools {
0015 
0016   /**
0017    * The purpose of this class is to deliver the device and queue
0018    * information from ExternalWork's acquire() to producer() via a
0019    * member/QueueCache variable.
0020    */
0021   template <typename TQueue, typename = std::enable_if_t<cms::alpakatools::is_queue_v<TQueue>>>
0022   class ContextState {
0023   public:
0024     using Queue = TQueue;
0025     using Device = alpaka::Dev<Queue>;
0026 
0027     ContextState() = default;
0028     ~ContextState() = default;
0029 
0030     ContextState(const ContextState&) = delete;
0031     ContextState& operator=(const ContextState&) = delete;
0032     ContextState(ContextState&&) = delete;
0033     ContextState& operator=(ContextState&& other) = delete;
0034 
0035   private:
0036     friend class ScopedContextAcquire<TQueue>;
0037     friend class ScopedContextProduce<TQueue>;
0038     friend class ScopedContextTask<TQueue>;
0039 
0040     void set(std::shared_ptr<Queue> queue) {
0041       throwIfQueue();
0042       queue_ = std::move(queue);
0043     }
0044 
0045     Device device() const {
0046       throwIfNoQueue();
0047       return alpaka::getDev(*queue_);
0048     }
0049 
0050     Queue queue() const {
0051       throwIfNoQueue();
0052       return *queue_;
0053     }
0054 
0055     std::shared_ptr<Queue> const& queuePtr() const {
0056       throwIfNoQueue();
0057       return queue_;
0058     }
0059 
0060     std::shared_ptr<Queue> releaseQueuePtr() {
0061       throwIfNoQueue();
0062       // This function needs to effectively reset queue_ (i.e. queue_
0063       // must be empty after this function). This behavior ensures that
0064       // the std::shared_ptr<Queue> is not hold for inadvertedly long (i.e. to
0065       // the next event), and is checked at run time.
0066       Queue queue = std::move(queue_);
0067       return queue;
0068     }
0069 
0070     void throwIfQueue() const {
0071       if (queue_) {
0072         throw cms::Exception("LogicError") << "Trying to set ContextState, but it already had a valid state";
0073       }
0074     }
0075 
0076     void throwIfNoQueue() const {
0077       if (not queue_) {
0078         throw cms::Exception("LogicError") << "Trying to get ContextState, but it did not have a valid state";
0079       }
0080     }
0081 
0082     std::shared_ptr<Queue> queue_;
0083   };
0084 
0085 }  // namespace cms::alpakatools
0086 
0087 #endif  // HeterogeneousCore_AlpakaCore_interface_ContextState_h