CMS 3D CMS Logo

HeterogeneousHGCalHEFConditions.cc
Go to the documentation of this file.
2 
4  const HGCalParameters* cpuHGCalParameters) {
5  //HGCalParameters as defined in CMSSW
6  this->sizes_params_ = calculate_memory_bytes_params_(cpuHGCalParameters);
9 }
10 
12  size_t chunk_ = std::accumulate(sz.begin(), sz.end(), 0); //total memory required in bytes
13  cudaCheck(cudaMallocHost(&this->params_.cellFineX_, chunk_));
14  return chunk_;
15 }
16 
18  const std::vector<size_t>& sz, const HGCalParameters* cpuParams) {
19  //store cumulative sum in bytes and convert it to sizes in units of C++ typesHEF, i.e., number if items to be transferred to GPU
20  std::vector<size_t> cumsum_sizes(sz.size() + 1, 0); //starting with zero
21  std::partial_sum(sz.begin(), sz.end(), cumsum_sizes.begin() + 1);
22  for (unsigned int i = 1; i < cumsum_sizes.size(); ++i) //start at second element (the first is zero)
23  {
24  size_t typesHEFsize = 0;
26  typesHEFsize = sizeof(double);
27  else if (cpar::typesHEF[i - 1] == cpar::HeterogeneousHGCalHEFParametersType::Int32_t)
28  typesHEFsize = sizeof(int32_t);
29  else
30  throw cms::Exception("HeterogeneousHGCalHEFConditionsWrapper") << "Wrong HeterogeneousHGCalParameters type";
31  cumsum_sizes[i] /= typesHEFsize;
32  }
33 
34  for (unsigned int j = 0; j < sz.size(); ++j) {
35  //setting the pointers
36  if (j != 0) {
37  const unsigned int jm1 = j - 1;
38  const size_t shift = cumsum_sizes[j] - cumsum_sizes[jm1];
41  select_pointer_d_(&this->params_, j) = select_pointer_d_(&this->params_, jm1) + shift;
43  cpar::typesHEF[j] == cpar::HeterogeneousHGCalHEFParametersType::Int32_t)
44  select_pointer_i_(&this->params_, j) =
45  reinterpret_cast<int32_t*>(select_pointer_d_(&this->params_, jm1) + shift);
46  }
47 
48  //copying the pointers' content
49  for (unsigned int i = cumsum_sizes[j]; i < cumsum_sizes[j + 1]; ++i) {
50  unsigned int index = i - cumsum_sizes[j];
52  select_pointer_d_(&this->params_, j)[index] = select_pointer_d_(cpuParams, j)[index];
53  } else if (cpar::typesHEF[j] == cpar::HeterogeneousHGCalHEFParametersType::Int32_t) {
54  select_pointer_i_(&this->params_, j)[index] = select_pointer_i_(cpuParams, j)[index];
55  } else
56  throw cms::Exception("HeterogeneousHGCalHEFConditionsWrapper") << "Wrong HeterogeneousHGCalParameters type";
57  }
58  }
59 }
60 
62  const HGCalParameters* cpuParams) {
63  size_t npointers = hgcal_conditions::parameters::typesHEF.size();
64  std::vector<size_t> sizes(npointers);
65  for (unsigned int i = 0; i < npointers; ++i) {
67  sizes[i] = select_pointer_d_(cpuParams, i).size();
68  else
69  sizes[i] = select_pointer_i_(cpuParams, i).size();
70  }
71 
72  std::vector<size_t> sizes_units(npointers);
73  for (unsigned int i = 0; i < npointers; ++i) {
75  sizes_units[i] = sizeof(double);
76  else if (cpar::typesHEF[i] == cpar::HeterogeneousHGCalHEFParametersType::Int32_t)
77  sizes_units[i] = sizeof(int32_t);
78  }
79 
80  //element by element multiplication
81  this->sizes_params_.resize(npointers);
83  sizes.begin(), sizes.end(), sizes_units.begin(), this->sizes_params_.begin(), std::multiplies<size_t>());
84  return this->sizes_params_;
85 }
86 
88  cudaCheck(cudaFreeHost(this->params_.cellFineX_));
89 }
90 
91 //I could use template specializations
92 //try to use std::variant in the future to avoid similar functions with different return values
94  const unsigned int& item) const {
95  switch (item) {
96  case 0:
97  return cpuObject->cellFineX_;
98  case 1:
99  return cpuObject->cellFineY_;
100  case 2:
101  return cpuObject->cellCoarseX_;
102  case 3:
103  return cpuObject->cellCoarseY_;
104  default:
105  edm::LogError("HeterogeneousHGCalHEFConditionsWrapper") << "select_pointer_d(heterogeneous): no item.";
106  return cpuObject->cellCoarseY_;
107  }
108 }
109 
111  const unsigned int& item) const {
112  switch (item) {
113  case 0:
114  return cpuObject->cellFineX_;
115  case 1:
116  return cpuObject->cellFineY_;
117  case 2:
118  return cpuObject->cellCoarseX_;
119  case 3:
120  return cpuObject->cellCoarseY_;
121  default:
122  edm::LogError("HeterogeneousHGCalHEFConditionsWrapper") << "select_pointer_d(non-heterogeneous): no item.";
123  return cpuObject->cellCoarseY_;
124  }
125 }
126 
128  const unsigned int& item) const {
129  switch (item) {
130  case 4:
131  return cpuObject->waferTypeL_;
132  default:
133  edm::LogError("HeterogeneousHGCalHEFConditionsWrapper") << "select_pointer_i(heterogeneous): no item.";
134  return cpuObject->waferTypeL_;
135  }
136 }
137 
139  const unsigned int& item) const {
140  switch (item) {
141  case 4:
142  return cpuObject->waferTypeL_;
143  default:
144  edm::LogError("HeterogeneousHGCalHEFConditionsWrapper") << "select_pointer_i(non-heterogeneous): no item.";
145  return cpuObject->waferTypeL_;
146  }
147 }
148 
151  // cms::cuda::ESProduct<T> essentially holds an array of GPUData objects,
152  // one per device. If the data have already been transferred to the
153  // current device (or the transfer has been queued), the helper just
154  // returns a reference to that GPUData object. Otherwise, i.e. data are
155  // not yet on the current device, the helper calls the lambda to do the
156  // necessary memory allocations and to queue the transfers.
157  auto const& data = gpuData_.dataForCurrentDeviceAsync(stream, [this](GPUData& data, cudaStream_t stream) {
158  // Allocate the payload object on pinned host memory.
160  // Allocate the payload array(s) on device memory.
161  cudaCheck(cudaMalloc(&(data.host->params.cellFineX_), chunk_params_));
162 
163  // Complete the host-side information on the payload
164 
165  //(set the pointers of the parameters)
166  size_t sdouble = sizeof(double);
167  for (unsigned int j = 0; j < this->sizes_params_.size() - 1; ++j) {
170  select_pointer_d_(&(data.host->params), j + 1) =
171  select_pointer_d_(&(data.host->params), j) + (this->sizes_params_[j] / sdouble);
173  cpar::typesHEF[j + 1] == cpar::HeterogeneousHGCalHEFParametersType::Int32_t)
174  select_pointer_i_(&(data.host->params), j + 1) =
175  reinterpret_cast<int32_t*>(select_pointer_d_(&(data.host->params), j) + (this->sizes_params_[j] / sdouble));
176  else
177  throw cms::Exception("HeterogeneousHGCalHEFConditionsWrapper")
178  << "compare this functions' logic with hgcal_conditions::parameters::typesHEF";
179  }
180 
181  // Allocate the payload object on the device memory.
183  // Transfer the payload, first the array(s) ...
184  cudaCheck(cudaMemcpyAsync(
185  data.host->params.cellFineX_, this->params_.cellFineX_, chunk_params_, cudaMemcpyHostToDevice, stream));
186 
187  // ... and then the payload object
188  cudaCheck(cudaMemcpyAsync(data.device,
189  data.host,
191  cudaMemcpyHostToDevice,
192  stream));
193  }); //gpuData_.dataForCurrentDeviceAsync
194 
195  // Returns the payload object on the memory of the current device
196  return data.device;
197 }
198 
199 // Destructor frees all member pointers
201  if (host != nullptr) {
202  cudaCheck(cudaFree(host->params.cellFineX_));
203  cudaCheck(cudaFreeHost(host));
204  }
205  cudaCheck(cudaFree(device));
206 }
hgcal_conditions::HeterogeneousHEFConditionsESProduct * host
size_t allocate_memory_params_(const std::vector< size_t > &)
const std::array< HeterogeneousHGCalHEFParametersType, 5 > typesHEF
Definition: HGCConditions.h:97
std::vector< double > cellFineY_
Log< level::Error, false > LogError
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
int32_t *& select_pointer_i_(cpar::HeterogeneousHGCalHEFParameters *, const unsigned int &) const
hgcal_conditions::HeterogeneousHEFConditionsESProduct * device
std::vector< double > cellCoarseX_
parameters::HeterogeneousHGCalHEFParameters params
void transfer_data_to_heterogeneous_pointers_params_(const std::vector< size_t > &, const HGCalParameters *)
std::vector< size_t > calculate_memory_bytes_params_(const HGCalParameters *)
hgcal_conditions::HeterogeneousHEFConditionsESProduct const * getHeterogeneousConditionsESProductAsync(cudaStream_t stream) const
std::vector< double > cellFineX_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
static unsigned int const shift
std::vector< double > cellCoarseY_
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:69
std::vector< int > waferTypeL_
cpar::HeterogeneousHGCalHEFParameters params_
double *& select_pointer_d_(cpar::HeterogeneousHGCalHEFParameters *, const unsigned int &) const
unsigned transform(const HcalDetId &id, unsigned transformCode)