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