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_host_unique(cudaStream_t stream) {
97  return cms::cuda::make_host_unique<T>(stream);
98  }
99 
100  template <typename T>
101  static auto make_device_unique(cudaStream_t stream) {
102  return cms::cuda::make_device_unique<T>(stream);
103  }
104 
105  template <typename T>
106  static auto make_device_unique(size_t size, cudaStream_t stream) {
107  return cms::cuda::make_device_unique<T>(size, stream);
108  }
109  };
110 
111  struct CPUTraits {
112  template <typename T>
113  using unique_ptr = std::unique_ptr<T>;
114 
115  template <typename T>
116  static auto make_unique(cudaStream_t) {
117  return std::make_unique<T>();
118  }
119 
120  template <typename T>
121  static auto make_unique(size_t size, cudaStream_t) {
122  return std::make_unique<T>(size);
123  }
124 
125  template <typename T>
126  static auto make_host_unique(cudaStream_t) {
127  return std::make_unique<T>();
128  }
129 
130  template <typename T>
131  static auto make_device_unique(cudaStream_t) {
132  return std::make_unique<T>();
133  }
134 
135  template <typename T>
136  static auto make_device_unique(size_t size, cudaStream_t) {
137  return std::make_unique<T>(size);
138  }
139  };
140 
141  } // namespace cudacompat
142 } // namespace cms
143 
144 // a heterogeneous unique pointer (of a different sort) ...
145 template <typename T, typename Traits>
147 public:
148  template <typename V>
150 
151  HeterogeneousSoAImpl() = default; // make root happy
152  ~HeterogeneousSoAImpl() = default;
155 
157  explicit HeterogeneousSoAImpl(cudaStream_t stream);
158 
159  T const *get() const { return m_ptr.get(); }
160 
161  T *get() { return m_ptr.get(); }
162 
164 
165 private:
167 };
168 
169 template <typename T, typename Traits>
171  m_ptr = Traits::template make_unique<T>(stream);
172 }
173 
174 // in reality valid only for GPU version...
175 template <typename T, typename Traits>
177  auto ret = cms::cuda::make_host_unique<T>(stream);
178  cudaCheck(cudaMemcpyAsync(ret.get(), get(), sizeof(T), cudaMemcpyDefault, stream));
179  return ret;
180 }
181 
182 template <typename T>
184 template <typename T>
186 template <typename T>
188 
189 #endif
size
Write out results.
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