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
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:373
cms::cudacompat::CPUTraits::make_unique
static auto make_unique(cudaStream_t)
Definition: HeterogeneousSoA.h:116
HeterogeneousSoA::operator*
const auto & operator*() const
Definition: HeterogeneousSoA.h:28
HeterogeneousSoA::dm_ptr
cms::cuda::device::unique_ptr< T > dm_ptr
Definition: HeterogeneousSoA.h:48
cms::cudacompat::GPUTraits::unique_ptr
cms::cuda::device::unique_ptr< T > unique_ptr
Definition: HeterogeneousSoA.h:58
cms::cudacompat::CPUTraits::unique_ptr
std::unique_ptr< T > unique_ptr
Definition: HeterogeneousSoA.h:113
cms::cudacompat::GPUTraits::make_unique
static auto make_unique(size_t size, cudaStream_t stream)
Definition: HeterogeneousSoA.h:66
cms::cudacompat::GPUTraits::make_unique
static auto make_unique(cudaStream_t stream)
Definition: HeterogeneousSoA.h:61
HeterogeneousSoAImpl
Definition: HeterogeneousSoA.h:146
HeterogeneousSoAImpl::HeterogeneousSoAImpl
HeterogeneousSoAImpl(unique_ptr< T > &&p)
Definition: HeterogeneousSoA.h:156
cms::cudacompat::HostTraits::make_device_unique
static auto make_device_unique(size_t size, cudaStream_t stream)
Definition: HeterogeneousSoA.h:106
HeterogeneousSoAImpl::get
T * get()
Definition: HeterogeneousSoA.h:161
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
cms::cuda::stream
cudaStream_t stream
Definition: HistoContainer.h:57
cms::cudacompat::GPUTraits::make_device_unique
static auto make_device_unique(size_t size, cudaStream_t stream)
Definition: HeterogeneousSoA.h:81
HeterogeneousSoA::operator->
auto * operator->()
Definition: HeterogeneousSoA.h:36
cms::cuda::assert
assert(be >=bs)
HeterogeneousSoA
Definition: HeterogeneousSoA.h:13
HeterogeneousSoAImpl::HeterogeneousSoAImpl
HeterogeneousSoAImpl()=default
device_unique_ptr.h
cms::cudacompat::GPUTraits::make_host_unique
static auto make_host_unique(cudaStream_t stream)
Definition: HeterogeneousSoA.h:71
cms::cudacompat::HostTraits::make_device_unique
static auto make_device_unique(cudaStream_t stream)
Definition: HeterogeneousSoA.h:101
cms::cudacompat::CPUTraits::make_device_unique
static auto make_device_unique(size_t size, cudaStream_t)
Definition: HeterogeneousSoA.h:136
HeterogeneousSoA::hm_ptr
cms::cuda::host::unique_ptr< T > hm_ptr
Definition: HeterogeneousSoA.h:49
HeterogeneousSoAImpl::unique_ptr
typename Traits::template unique_ptr< V > unique_ptr
Definition: HeterogeneousSoA.h:149
cms::cudacompat::CPUTraits
Definition: HeterogeneousSoA.h:111
cms::cudacompat::GPUTraits
Definition: HeterogeneousSoA.h:56
host_unique_ptr.h
HeterogeneousSoA::HeterogeneousSoA
HeterogeneousSoA(cms::cuda::device::unique_ptr< T > &&p)
Definition: HeterogeneousSoA.h:22
HeterogeneousSoA::get
auto * get()
Definition: HeterogeneousSoA.h:32
HeterogeneousSoA::toHostAsync
cms::cuda::host::unique_ptr< T > toHostAsync(cudaStream_t stream) const
Definition: HeterogeneousSoA.h:39
HeterogeneousSoA::~HeterogeneousSoA
~HeterogeneousSoA()=default
cms::cudacompat::HostTraits::make_unique
static auto make_unique(cudaStream_t stream)
Definition: HeterogeneousSoA.h:91
HeterogeneousSoA::operator=
HeterogeneousSoA & operator=(HeterogeneousSoA &&)=default
HeterogeneousSoAImpl::toHostAsync
cms::cuda::host::unique_ptr< T > toHostAsync(cudaStream_t stream) const
Definition: HeterogeneousSoA.h:176
cudaCheck.h
HeterogeneousSoA::Product
T Product
Definition: HeterogeneousSoA.h:15
HeterogeneousSoAImpl::m_ptr
unique_ptr< T > m_ptr
Definition: HeterogeneousSoA.h:166
copyAsync.h
cms::cudacompat::HostTraits::unique_ptr
cms::cuda::host::unique_ptr< T > unique_ptr
Definition: HeterogeneousSoA.h:88
svgfig.template
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
HeterogeneousSoA::HeterogeneousSoA
HeterogeneousSoA(cms::cuda::host::unique_ptr< T > &&p)
Definition: HeterogeneousSoA.h:23
HeterogeneousSoA::get
const auto * get() const
Definition: HeterogeneousSoA.h:26
get
#define get
cms::cudacompat::GPUTraits::make_device_unique
static auto make_device_unique(cudaStream_t stream)
Definition: HeterogeneousSoA.h:76
HeterogeneousSoA::std_ptr
std::unique_ptr< T > std_ptr
Definition: HeterogeneousSoA.h:50
cms::cuda::device::unique_ptr
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
Definition: device_unique_ptr.h:33
cms::cudacompat::CPUTraits::make_host_unique
static auto make_host_unique(cudaStream_t)
Definition: HeterogeneousSoA.h:126
HeterogeneousSoA::HeterogeneousSoA
HeterogeneousSoA(std::unique_ptr< T > &&p)
Definition: HeterogeneousSoA.h:24
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
cudaCheck
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:62
cms::cudacompat::HostTraits
Definition: HeterogeneousSoA.h:86
T
long double T
Definition: Basic3DVectorLD.h:48
cms::cuda::host::unique_ptr
std::unique_ptr< T, impl::HostDeleter > unique_ptr
Definition: host_unique_ptr.h:21
cms::cudacompat::HostTraits::make_host_unique
static auto make_host_unique(cudaStream_t stream)
Definition: HeterogeneousSoA.h:96
HeterogeneousSoAImpl::operator=
HeterogeneousSoAImpl & operator=(HeterogeneousSoAImpl &&)=default
HeterogeneousSoA::HeterogeneousSoA
HeterogeneousSoA()=default
HeterogeneousSoAImpl::~HeterogeneousSoAImpl
~HeterogeneousSoAImpl()=default
cms::cudacompat::CPUTraits::make_unique
static auto make_unique(size_t size, cudaStream_t)
Definition: HeterogeneousSoA.h:121
HeterogeneousSoAImpl::get
const T * get() const
Definition: HeterogeneousSoA.h:159
HeterogeneousSoA::operator*
auto & operator*()
Definition: HeterogeneousSoA.h:34
cms::cudacompat::CPUTraits::make_device_unique
static auto make_device_unique(cudaStream_t)
Definition: HeterogeneousSoA.h:131
HeterogeneousSoA::operator->
const auto * operator->() const
Definition: HeterogeneousSoA.h:30
cms
Namespace of DDCMS conversion namespace.
Definition: ProducerAnalyzer.cc:21
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443