CMS 3D CMS Logo

getDeviceCachingAllocator.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_AlpakaInterface_interface_getDeviceCachingAllocator_h
2 #define HeterogeneousCore_AlpakaInterface_interface_getDeviceCachingAllocator_h
3 
4 #include <cassert>
5 #include <memory>
6 
12 
13 namespace cms::alpakatools {
14 
15  namespace detail {
16 
17  template <typename TDev,
18  typename TQueue,
19  typename = std::enable_if_t<cms::alpakatools::is_device_v<TDev> and cms::alpakatools::is_queue_v<TQueue>>>
21  using Allocator = CachingAllocator<TDev, TQueue>;
22  auto const& devices = cms::alpakatools::devices<alpaka::Pltf<TDev>>();
23  ssize_t const size = devices.size();
24 
25  // allocate the storage for the objects
26  auto ptr = std::allocator<Allocator>().allocate(size);
27 
28  // construct the objects in the storage
29  ptrdiff_t index = 0;
30  try {
31  for (; index < size; ++index) {
32 #if __cplusplus >= 202002L
33  std::construct_at(
34 #else
35  std::allocator<Allocator>().construct(
36 #endif
37  ptr + index,
38  devices[index],
44  true, // reuseSameQueueAllocations
45  false); // debug
46  }
47  } catch (...) {
48  --index;
49  // destroy any object that had been succesfully constructed
50  while (index >= 0) {
51  std::destroy_at(ptr + index);
52  --index;
53  }
54  // deallocate the storage
55  std::allocator<Allocator>().deallocate(ptr, size);
56  // rethrow the exception
57  throw;
58  }
59 
60  // use a custom deleter to destroy all objects and deallocate the memory
61  auto deleter = [size](Allocator* ptr) {
62  for (size_t i = size; i > 0; --i) {
63  std::destroy_at(ptr + i - 1);
64  }
65  std::allocator<Allocator>().deallocate(ptr, size);
66  };
67 
68  return std::unique_ptr<Allocator[], decltype(deleter)>(ptr, deleter);
69  }
70 
71  } // namespace detail
72 
73  template <typename TDev,
74  typename TQueue,
75  typename = std::enable_if_t<cms::alpakatools::is_device_v<TDev> and cms::alpakatools::is_queue_v<TQueue>>>
77  // initialise all allocators, one per device
78  CMS_THREAD_SAFE static auto allocators = detail::allocate_device_allocators<TDev, TQueue>();
79 
80  size_t const index = alpaka::getNativeHandle(device);
81  assert(index < cms::alpakatools::devices<alpaka::Pltf<TDev>>().size());
82 
83  // the public interface is thread safe
84  return allocators[index];
85  }
86 
87 } // namespace cms::alpakatools
88 
89 #endif // HeterogeneousCore_AlpakaInterface_interface_getDeviceCachingAllocator_h
size
Write out results.
CachingAllocator< TDev, TQueue > & getDeviceCachingAllocator(TDev const &device)
constexpr unsigned int minBin
constexpr unsigned int maxBin
assert(be >=bs)
constexpr double maxCachedFraction
#define CMS_THREAD_SAFE
constexpr size_t maxCachedBytes
constexpr unsigned int binGrowth
std::vector< alpaka::Dev< TPlatform > > const & devices()
Definition: devices.h:36
std::unique_ptr< GeometricDet > construct(DDCompactView const &cpv, std::vector< int > const &detidShifts)