CMS 3D CMS Logo

HeterogeneousSoA.h
Go to the documentation of this file.
1 #ifndef CUDADataFormatsCommonHeterogeneousSoA_H
2 #define CUDADataFormatsCommonHeterogeneousSoA_H
3 
4 #include <cassert>
5 
10 
11 // a heterogeneous unique pointer...
12 template <typename T>
14 public:
15  using Product = T;
16 
17  HeterogeneousSoA() = default; // make root happy
18  ~HeterogeneousSoA() = default;
19  HeterogeneousSoA(HeterogeneousSoA &&) = default;
21 
24  explicit HeterogeneousSoA(std::unique_ptr<T> &&p) : std_ptr(std::move(p)) {}
25 
26  auto const *get() const { return dm_ptr ? dm_ptr.get() : (hm_ptr ? hm_ptr.get() : std_ptr.get()); }
27 
28  auto const &operator*() const { return *get(); }
29 
30  auto const *operator->() const { return get(); }
31 
32  auto *get() { return dm_ptr ? dm_ptr.get() : (hm_ptr ? hm_ptr.get() : std_ptr.get()); }
33 
34  auto &operator*() { return *get(); }
35 
36  auto *operator->() { return get(); }
37 
38  // in reality valid only for GPU version...
40  assert(dm_ptr);
41  auto ret = cms::cuda::make_host_unique<T>(stream);
42  cudaCheck(cudaMemcpyAsync(ret.get(), dm_ptr.get(), sizeof(T), cudaMemcpyDefault, stream));
43  return ret;
44  }
45 
46 private:
47  // a union wan't do it, a variant will not be more efficienct
50  std::unique_ptr<T> std_ptr;
51 };
52 
53 namespace cms {
54  namespace cudacompat {
55 
56  struct GPUTraits {
57  template <typename T>
59 
60  template <typename T>
61  static auto make_unique(cudaStream_t stream) {
62  return cms::cuda::make_device_unique<T>(stream);
63  }
64 
65  template <typename T>
66  static auto make_unique(size_t size, cudaStream_t stream) {
67  return cms::cuda::make_device_unique<T>(size, stream);
68  }
69 
70  template <typename T>
71  static auto make_host_unique(cudaStream_t stream) {
72  return cms::cuda::make_host_unique<T>(stream);
73  }
74 
75  template <typename T>
76  static auto make_device_unique(cudaStream_t stream) {
77  return cms::cuda::make_device_unique<T>(stream);
78  }
79 
80  template <typename T>
81  static auto make_device_unique(size_t size, cudaStream_t stream) {
82  return cms::cuda::make_device_unique<T>(size, stream);
83  }
84  };
85 
86  struct HostTraits {
87  template <typename T>
89 
90  template <typename T>
91  static auto make_unique(cudaStream_t stream) {
92  return cms::cuda::make_host_unique<T>(stream);
93  }
94 
95  template <typename T>
96  static auto make_unique(size_t size, cudaStream_t stream) {
97  return cms::cuda::make_host_unique<T>(size, stream);
98  }
99 
100  template <typename T>
101  static auto make_host_unique(cudaStream_t stream) {
102  return cms::cuda::make_host_unique<T>(stream);
103  }
104 
105  template <typename T>
106  static auto make_device_unique(cudaStream_t stream) {
107  return cms::cuda::make_device_unique<T>(stream);
108  }
109 
110  template <typename T>
111  static auto make_device_unique(size_t size, cudaStream_t stream) {
112  return cms::cuda::make_device_unique<T>(size, stream);
113  }
114  };
115 
116  struct CPUTraits {
117  template <typename T>
118  using unique_ptr = std::unique_ptr<T>;
119 
120  template <typename T>
121  static auto make_unique(cudaStream_t) {
122  return std::make_unique<T>();
123  }
124 
125  template <typename T>
126  static auto make_unique(size_t size, cudaStream_t) {
127  return std::make_unique<T>(size);
128  }
129 
130  template <typename T>
131  static auto make_host_unique(cudaStream_t) {
132  return std::make_unique<T>();
133  }
134 
135  template <typename T>
136  static auto make_device_unique(cudaStream_t) {
137  return std::make_unique<T>();
138  }
139 
140  template <typename T>
141  static auto make_device_unique(size_t size, cudaStream_t) {
142  return std::make_unique<T>(size);
143  }
144  };
145 
146  } // namespace cudacompat
147 } // namespace cms
148 
149 // a heterogeneous unique pointer (of a different sort) ...
150 template <typename T, typename Traits>
152 public:
153  template <typename V>
155 
156  HeterogeneousSoAImpl() = default; // make root happy
157  ~HeterogeneousSoAImpl() = default;
160 
162  explicit HeterogeneousSoAImpl(cudaStream_t stream);
163 
164  T const *get() const { return m_ptr.get(); }
165 
166  T *get() { return m_ptr.get(); }
167 
169 
170 private:
172 };
173 
174 template <typename T, typename Traits>
176  m_ptr = Traits::template make_unique<T>(stream);
177 }
178 
179 // in reality valid only for GPU version...
180 template <typename T, typename Traits>
182  auto ret = cms::cuda::make_host_unique<T>(stream);
183  cudaCheck(cudaMemcpyAsync(ret.get(), get(), sizeof(T), cudaMemcpyDefault, stream));
184  return ret;
185 }
186 
187 template <typename T>
189 template <typename T>
191 template <typename T>
193 
194 #endif
HeterogeneousSoA(cms::cuda::host::unique_ptr< T > &&p)
~HeterogeneousSoA()=default
auto const * operator->() const
HeterogeneousSoAImpl()=default
ret
prodAgent to be discontinued
auto const & operator*() const
HeterogeneousSoA & operator=(HeterogeneousSoA &&)=default
static auto make_device_unique(cudaStream_t stream)
HeterogeneousSoAImpl(unique_ptr< T > &&p)
static auto make_device_unique(cudaStream_t stream)
cms::cuda::host::unique_ptr< T > unique_ptr
static auto make_host_unique(cudaStream_t)
std::unique_ptr< T > std_ptr
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
unique_ptr< T > m_ptr
assert(be >=bs)
cms::cuda::device::unique_ptr< T > dm_ptr
cms::cuda::device::unique_ptr< T > unique_ptr
HeterogeneousSoA(cms::cuda::device::unique_ptr< T > &&p)
cms::cuda::host::unique_ptr< T > toHostAsync(cudaStream_t stream) const
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
std::unique_ptr< T > unique_ptr
static auto make_host_unique(cudaStream_t stream)
cms::cuda::host::unique_ptr< T > hm_ptr
static auto make_host_unique(cudaStream_t stream)
HeterogeneousSoA()=default
static auto make_unique(size_t size, cudaStream_t stream)
Namespace of DDCMS conversion namespace.
static auto make_device_unique(cudaStream_t)
static auto make_device_unique(size_t size, cudaStream_t stream)
cms::cuda::host::unique_ptr< T > toHostAsync(cudaStream_t stream) const
typename Traits::template unique_ptr< V > unique_ptr
static auto make_unique(cudaStream_t)
static auto make_unique(cudaStream_t stream)
static auto make_device_unique(size_t size, cudaStream_t)
static auto make_unique(cudaStream_t stream)
HeterogeneousSoAImpl & operator=(HeterogeneousSoAImpl &&)=default
HeterogeneousSoA(std::unique_ptr< T > &&p)
std::unique_ptr< T, impl::HostDeleter > unique_ptr
~HeterogeneousSoAImpl()=default
static auto make_device_unique(size_t size, cudaStream_t stream)
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:69
long double T
static auto make_unique(size_t size, cudaStream_t)
def move(src, dest)
Definition: eostools.py:511
static auto make_unique(size_t size, cudaStream_t stream)