CMS 3D CMS Logo

QueueCache.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_AlpakaCore_interface_QueueCache_h
2 #define HeterogeneousCore_AlpakaCore_interface_QueueCache_h
3 
4 #include <memory>
5 #include <vector>
6 
7 #include <alpaka/alpaka.hpp>
8 
14 
15 namespace cms::alpakatools {
16 
17  template <typename Queue>
18  class QueueCache {
19 #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
20  friend class alpaka_cuda_async::AlpakaService;
21 #endif
22 #ifdef ALPAKA_ACC_GPU_HIP_ENABLED
23  friend class alpaka_rocm_async::AlpakaService;
24 #endif
25 #ifdef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
26  friend class alpaka_serial_sync::AlpakaService;
27 #endif
28 #ifdef ALPAKA_ACC_CPU_B_TBB_T_SEQ_ENABLED
29  friend class alpaka_tbb_async::AlpakaService;
30 #endif
31 
32  using Device = alpaka::Dev<Queue>;
33  using Platform = alpaka::Platform<Device>;
34 
35  public:
36  // QueueCache should be constructed by the first call to
37  // getQueueCache() only if we have any devices present
39 
40  // Gets a (cached) queue for the current device. The queue
41  // will be returned to the cache by the shared_ptr destructor.
42  // This function is thread safe
43  std::shared_ptr<Queue> get(Device const& dev) {
44  return cache_[alpaka::getNativeHandle(dev)].makeOrGet([dev]() { return std::make_unique<Queue>(dev); });
45  }
46 
47  private:
48  // not thread safe, intended to be called only from AlpakaService
49  void clear() {
50  // Reset the contents of the caches, but leave an
51  // edm::ReusableObjectHolder alive for each device. This is needed
52  // mostly for the unit tests, where the function-static
53  // QueueCache lives through multiple tests (and go through
54  // multiple shutdowns of the framework).
55  cache_.clear();
56  cache_.resize(devices<Platform>().size());
57  }
58 
59  std::vector<edm::ReusableObjectHolder<Queue>> cache_;
60  };
61 
62  // Gets the global instance of a QueueCache
63  // This function is thread safe
64  template <typename Queue>
66  // the public interface is thread safe
68  return cache;
69  }
70 
71 } // namespace cms::alpakatools
72 
73 #endif // HeterogeneousCore_AlpakaCore_interface_QueueCache_h
alpaka::Dev< Queue > Device
Definition: QueueCache.h:32
std::vector< edm::ReusableObjectHolder< Queue > > cache_
Definition: QueueCache.h:59
#define CMS_THREAD_SAFE
def cache(function)
Definition: utilities.py:3
std::vector< alpaka::Dev< TPlatform > > const & devices()
Definition: devices.h:22
alpaka::Platform< Device > Platform
Definition: QueueCache.h:33
QueueCache< Queue > & getQueueCache()
Definition: QueueCache.h:65