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 }
HeterogeneousHGCalHEFConditionsWrapper::transfer_data_to_heterogeneous_pointers_params_
void transfer_data_to_heterogeneous_pointers_params_(const std::vector< size_t > &, const HGCalParameters *)
Definition: HeterogeneousHGCalHEFConditions.cc:17
HeterogeneousHGCalHEFConditionsWrapper::allocate_memory_params_
size_t allocate_memory_params_(const std::vector< size_t > &)
Definition: HeterogeneousHGCalHEFConditions.cc:11
mps_fire.i
i
Definition: mps_fire.py:428
hgcal_conditions::parameters::HeterogeneousHGCalHEFParameters::waferTypeL_
std::int32_t * waferTypeL_
Definition: HGCConditions.h:125
HeterogeneousHGCalHEFConditionsWrapper::sizes_params_
std::vector< size_t > sizes_params_
Definition: HeterogeneousHGCalHEFConditions.h:37
HGCalParameters::cellFineY_
std::vector< double > cellFineY_
Definition: HGCalParameters.h:121
HeterogeneousHGCalHEFConditionsWrapper::select_pointer_d_
double *& select_pointer_d_(cpar::HeterogeneousHGCalHEFParameters *, const unsigned int &) const
Definition: HeterogeneousHGCalHEFConditions.cc:93
HeterogeneousHGCalHEFConditions.h
HeterogeneousHGCalHEFConditionsWrapper::gpuData_
cms::cuda::ESProduct< GPUData > gpuData_
Definition: HeterogeneousHGCalHEFConditions.h:65
cms::cuda::stream
uint32_t const T *__restrict__ const uint32_t *__restrict__ int32_t int Histo::index_type cudaStream_t stream
Definition: HistoContainer.h:51
hgcal_conditions::parameters::HeterogeneousHGCalHEFParameters::cellCoarseX_
double * cellCoarseX_
Definition: HGCConditions.h:122
hgcal_conditions::parameters::HeterogeneousHGCalHEFParametersType::Double
HeterogeneousHGCalHEFConditionsWrapper::GPUData
Definition: HeterogeneousHGCalHEFConditions.h:54
hgcal_conditions::parameters::HeterogeneousHGCalHEFParameters::cellFineY_
double * cellFineY_
Definition: HGCConditions.h:121
HeterogeneousHGCalHEFConditionsWrapper::chunk_params_
size_t chunk_params_
Definition: HeterogeneousHGCalHEFConditions.h:38
hgcal_conditions::parameters::HeterogeneousHGCalHEFParameters
Definition: HGCConditions.h:117
hgcal_conditions::HeterogeneousHEFConditionsESProduct::params
parameters::HeterogeneousHGCalHEFParameters params
Definition: HGCConditions.h:188
hgcal_conditions::parameters::typesHEF
const std::array< HeterogeneousHGCalHEFParametersType, 5 > typesHEF
Definition: HGCConditions.h:97
HeterogeneousHGCalHEFConditionsWrapper::GPUData::~GPUData
~GPUData()
Definition: HeterogeneousHGCalHEFConditions.cc:200
HeterogeneousHGCalHEFConditionsWrapper::GPUData::host
hgcal_conditions::HeterogeneousHEFConditionsESProduct * host
Definition: HeterogeneousHGCalHEFConditions.h:58
HcalDetIdTransform::transform
unsigned transform(const HcalDetId &id, unsigned transformCode)
Definition: HcalDetIdTransform.cc:7
HeterogeneousHGCalHEFConditionsWrapper::~HeterogeneousHGCalHEFConditionsWrapper
~HeterogeneousHGCalHEFConditionsWrapper()
Definition: HeterogeneousHGCalHEFConditions.cc:87
HGCalParameters::cellCoarseY_
std::vector< double > cellCoarseY_
Definition: HGCalParameters.h:125
hgcal_conditions::parameters::HeterogeneousHGCalHEFParameters::cellCoarseY_
double * cellCoarseY_
Definition: HGCConditions.h:123
hgcal_conditions::parameters::HeterogeneousHGCalHEFParameters::cellFineX_
double * cellFineX_
Definition: HGCConditions.h:120
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
HeterogeneousHGCalHEFConditionsWrapper::GPUData::device
hgcal_conditions::HeterogeneousHEFConditionsESProduct * device
Definition: HeterogeneousHGCalHEFConditions.h:60
HGCalParameters
Definition: HGCalParameters.h:14
hgcal_conditions::HeterogeneousHEFConditionsESProduct
Definition: HGCConditions.h:187
cudaCheck
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:69
HeterogeneousHGCalHEFConditionsWrapper::HeterogeneousHGCalHEFConditionsWrapper
HeterogeneousHGCalHEFConditionsWrapper(const HGCalParameters *)
Definition: HeterogeneousHGCalHEFConditions.cc:3
HeterogeneousHGCalHEFConditionsWrapper::calculate_memory_bytes_params_
std::vector< size_t > calculate_memory_bytes_params_(const HGCalParameters *)
Definition: HeterogeneousHGCalHEFConditions.cc:61
edm::shift
static unsigned const int shift
Definition: LuminosityBlockID.cc:7
Exception
Definition: hltDiff.cc:245
HGCalParameters::cellFineX_
std::vector< double > cellFineX_
Definition: HGCalParameters.h:120
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
HGCalParameters::waferTypeL_
std::vector< int > waferTypeL_
Definition: HGCalParameters.h:116
HeterogeneousHGCalHEFConditionsWrapper::params_
cpar::HeterogeneousHGCalHEFParameters params_
Definition: HeterogeneousHGCalHEFConditions.h:35
HGCalParameters::cellCoarseX_
std::vector< double > cellCoarseX_
Definition: HGCalParameters.h:124
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
HeterogeneousHGCalHEFConditionsWrapper::getHeterogeneousConditionsESProductAsync
const hgcal_conditions::HeterogeneousHEFConditionsESProduct * getHeterogeneousConditionsESProductAsync(cudaStream_t stream) const
Definition: HeterogeneousHGCalHEFConditions.cc:150
HeterogeneousHGCalHEFConditionsWrapper::select_pointer_i_
int32_t *& select_pointer_i_(cpar::HeterogeneousHGCalHEFParameters *, const unsigned int &) const
Definition: HeterogeneousHGCalHEFConditions.cc:127