Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-11-24 00:02:22

0001 #ifndef HeterogeneousCore_AlpakaCore_interface_alpaka_ESDeviceProduct_h
0002 #define HeterogeneousCore_AlpakaCore_interface_alpaka_ESDeviceProduct_h
0003 
0004 #include <memory>
0005 #include <optional>
0006 #include <vector>
0007 
0008 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0009 
0010 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0011   /**
0012    * The sole purpose of this wrapper class is to segregate the
0013    * EventSetup products in the device memory from the host memory
0014    *
0015    * In contrast to ED side, no synchronization are needed here as we
0016    * mark the ES product done only after all the (in the future
0017    * asynchronous) work has finished.
0018    */
0019   template <typename T>
0020   class ESDeviceProduct {
0021   public:
0022     virtual ~ESDeviceProduct() {}
0023 
0024     T const& get(Device const& dev) const { return *cache_[alpaka::getNativeHandle(dev)]; }
0025 
0026   protected:
0027     explicit ESDeviceProduct(size_t ndevices) : cache_(ndevices, nullptr) {}
0028 
0029     void setCache(size_t idev, T const* data) { cache_[idev] = data; }
0030 
0031   private:
0032     // trading memory to avoid virtual function
0033     std::vector<T const*> cache_;
0034   };
0035 
0036   namespace detail {
0037     /**
0038      * This class holds the actual storage (since EventSetup proxies
0039      * are able to hold std::optional<T>, std::unique_ptr<T>, and
0040      * std::shared_ptr<T>()). The object of this class holds the
0041      * storage, while the consumers of the ESProducts see only the
0042      * base class.
0043      */
0044     template <typename TProduct, typename TStorage>
0045     class ESDeviceProductWithStorage : public ESDeviceProduct<TProduct> {
0046       using Base = ESDeviceProduct<TProduct>;
0047 
0048     public:
0049       explicit ESDeviceProductWithStorage(size_t ndevices) : Base(ndevices), data_(ndevices) {}
0050 
0051       void insert(Device const& dev, TStorage data) {
0052         auto const idev = alpaka::getNativeHandle(dev);
0053         data_[idev] = std::move(data);
0054         this->setCache(idev, &*data_[idev]);
0055       }
0056 
0057     private:
0058       std::vector<TStorage> data_;
0059     };
0060   }  // namespace detail
0061 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0062 
0063 #endif