CMS 3D CMS Logo

HeterogeneousHGCalHEBConditions.cc
Go to the documentation of this file.
2 
4  const HGCalParameters* cpuHGCalParameters) {
5  calculate_memory_bytes(cpuHGCalParameters);
6 
7  chunk_ = std::accumulate(this->sizes_.begin(), this->sizes_.end(), 0); //total memory required in bytes
8  cudaCheck(cudaMallocHost(&this->params_.testD_, chunk_));
9 
10  //store cumulative sum in bytes and convert it to sizes in units of C++ typesHEB, i.e., number if items to be transferred to GPU
11  std::vector<size_t> cumsum_sizes(this->sizes_.size() + 1, 0); //starting with zero
12  std::partial_sum(this->sizes_.begin(), this->sizes_.end(), cumsum_sizes.begin() + 1);
13  for (unsigned int i = 1; i < cumsum_sizes.size(); ++i) //start at second element (the first is zero)
14  {
15  unsigned int typesHEBize = 0;
17  typesHEBize = sizeof(double);
18  else if (cp::typesHEB[i - 1] == cp::HeterogeneousHGCalHEBParametersType::Int32_t)
19  typesHEBize = sizeof(int32_t);
20  else
21  throw cms::Exception("HeterogeneousHGCalHEBConditionsWrapper") << "Wrong HeterogeneousHGCalParameters type";
22  cumsum_sizes[i] /= typesHEBize;
23  }
24 
25  for (unsigned int j = 0; j < this->sizes_.size(); ++j) {
26  //setting the pointers
27  if (j != 0) {
28  const unsigned int jm1 = j - 1;
31  select_pointer_d(&this->params_, j) = select_pointer_d(&this->params_, jm1) + this->sizes_[jm1];
33  cp::typesHEB[j] == cp::HeterogeneousHGCalHEBParametersType::Int32_t)
34  select_pointer_i(&this->params_, j) =
35  reinterpret_cast<int32_t*>(select_pointer_d(&this->params_, jm1) + this->sizes_[jm1]);
36  }
37 
38  //copying the pointers' content
39  for (unsigned int i = cumsum_sizes[j]; i < cumsum_sizes[j + 1]; ++i) {
40  unsigned int index = i - cumsum_sizes[j];
42  select_pointer_d(&this->params_, j)[index] = select_pointer_d(cpuHGCalParameters, j)[index];
43  } else if (cp::typesHEB[j] == cp::HeterogeneousHGCalHEBParametersType::Int32_t)
44  select_pointer_i(&this->params_, j)[index] = select_pointer_i(cpuHGCalParameters, j)[index];
45  else
46  throw cms::Exception("HeterogeneousHGCalHEBConditionsWrapper") << "Wrong HeterogeneousHGCalParameters type";
47  }
48  }
49 }
50 
52  size_t npointers = hgcal_conditions::parameters::typesHEB.size();
53  std::vector<size_t> sizes(npointers);
54  for (unsigned int i = 0; i < npointers; ++i) {
56  sizes[i] = select_pointer_d(cpuHGCalParameters, i).size();
57  else
58  sizes[i] = select_pointer_i(cpuHGCalParameters, i).size();
59  }
60 
61  std::vector<size_t> sizes_units(npointers);
62  for (unsigned int i = 0; i < npointers; ++i) {
64  sizes_units[i] = sizeof(double);
65  else if (cp::typesHEB[i] == cp::HeterogeneousHGCalHEBParametersType::Int32_t)
66  sizes_units[i] = sizeof(int32_t);
67  }
68 
69  //element by element multiplication
70  this->sizes_.resize(npointers);
71  std::transform(sizes.begin(), sizes.end(), sizes_units.begin(), this->sizes_.begin(), std::multiplies<size_t>());
72 }
73 
75  cudaCheck(cudaFreeHost(this->params_.testD_));
76 }
77 
78 //I could use template specializations
79 //try to use std::variant in the future to avoid similar functions with different return values
81  const unsigned int& item) const {
82  switch (item) {
83  case 0:
84  return cpuObject->testD_;
85  default:
86  throw cms::Exception("HeterogeneousHGCalHEBConditionsWrapper") << "select_pointer_d(heterogeneous): no item.";
87  return cpuObject->testD_;
88  }
89 }
90 
92  const unsigned int& item) const {
93  switch (item) {
94  case 0:
95  return cpuObject->cellFineX_;
96  default:
97  throw cms::Exception("HeterogeneousHGCalHEBConditionsWrapper") << "select_pointer_d(non-heterogeneous): no item.";
98  return cpuObject->cellFineX_;
99  }
100 }
101 
103  const unsigned int& item) const {
104  switch (item) {
105  case 1:
106  return cpuObject->testI_;
107  default:
108  throw cms::Exception("HeterogeneousHGCalHEBConditionsWrapper") << "select_pointer_i(heterogeneous): no item.";
109  return cpuObject->testI_;
110  }
111 }
112 
114  const unsigned int& item) const {
115  switch (item) {
116  case 4:
117  return cpuObject->waferTypeL_;
118  default:
119  throw cms::Exception("HeterogeneousHGCalHEBConditionsWrapper") << "select_pointer_i(non-heterogeneous): no item.";
120  return cpuObject->waferTypeL_;
121  }
122 }
123 
126  // cms::cuda::ESProduct<T> essentially holds an array of GPUData objects,
127  // one per device. If the data have already been transferred to the
128  // current device (or the transfer has been queued), the helper just
129  // returns a reference to that GPUData object. Otherwise, i.e. data are
130  // not yet on the current device, the helper calls the lambda to do the
131  // necessary memory allocations and to queue the transfers.
132  auto const& data = gpuData_.dataForCurrentDeviceAsync(stream, [this](GPUData& data, cudaStream_t stream) {
133  // Allocate the payload object on pinned host memory.
135  // Allocate the payload array(s) on device memory.
136  cudaCheck(cudaMalloc(&(data.host->params.testD_), chunk_));
137 
138  // Allocate the payload object on the device memory.
140  // Transfer the payload, first the array(s) ...
141  cudaCheck(cudaMemcpyAsync(data.host->params.testD_, this->params_.testD_, chunk_, cudaMemcpyHostToDevice, stream));
142 
143  for (unsigned int j = 0; j < this->sizes_.size() - 1; ++j) {
146  select_pointer_d(&(data.host->params), j + 1) = select_pointer_d(&(data.host->params), j) + this->sizes_[j];
148  cp::typesHEB[j + 1] == cp::HeterogeneousHGCalHEBParametersType::Int32_t)
149  select_pointer_i(&(data.host->params), j + 1) =
150  reinterpret_cast<int32_t*>(select_pointer_d(&(data.host->params), j) + this->sizes_[j]);
151  else
152  throw cms::Exception("HeterogeneousHGCalHEBConditionsWrapper")
153  << "compare this functions' logic with hgcal_conditions::parameters::typesHEB";
154  }
155 
156  // ... and then the payload object
157  cudaCheck(cudaMemcpyAsync(data.device,
158  data.host,
160  cudaMemcpyHostToDevice,
161  stream));
162  }); //gpuData_.dataForCurrentDeviceAsync
163 
164  // Returns the payload object on the memory of the current device
165  return data.device;
166 }
167 
168 // Destructor frees all member pointers
170  if (host != nullptr) {
171  cudaCheck(cudaFree(host->params.testD_));
172  cudaCheck(cudaFreeHost(host));
173  }
174  cudaCheck(cudaFree(device));
175 }
176 
177 //template double*& HeterogeneousHGCalHEBConditionsWrapper::select_pointer_d<cp::HeterogeneousHGCalParameters*>(cp::HeterogeneousHGCalParameters*, const unsigned int&) const;
hgcal_conditions::HeterogeneousHEBConditionsESProduct * host
parameters::HeterogeneousHGCalHEBParameters params
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
double *& select_pointer_d(cp::HeterogeneousHGCalHEBParameters *, const unsigned int &) const
hgcal_conditions::HeterogeneousHEBConditionsESProduct * device
const std::array< HeterogeneousHGCalHEBParametersType, 2 > typesHEB
std::vector< double > cellFineX_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
int32_t *& select_pointer_i(cp::HeterogeneousHGCalHEBParameters *, const unsigned int &) const
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:69
std::vector< int > waferTypeL_
hgcal_conditions::HeterogeneousHEBConditionsESProduct const * getHeterogeneousConditionsESProductAsync(cudaStream_t stream) const
unsigned transform(const HcalDetId &id, unsigned transformCode)