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;
HeterogeneousHGCalHEBConditionsWrapper::sizes_
std::vector< size_t > sizes_
Definition: HeterogeneousHGCalHEBConditions.h:37
HeterogeneousHGCalHEBConditionsWrapper::select_pointer_i
int32_t *& select_pointer_i(cp::HeterogeneousHGCalHEBParameters *, const unsigned int &) const
Definition: HeterogeneousHGCalHEBConditions.cc:102
mps_fire.i
i
Definition: mps_fire.py:428
hgcal_conditions::parameters::HeterogeneousHGCalHEBParameters::testI_
std::int32_t * testI_
Definition: HGCConditions.h:130
HeterogeneousHGCalHEBConditionsWrapper::~HeterogeneousHGCalHEBConditionsWrapper
~HeterogeneousHGCalHEBConditionsWrapper()
Definition: HeterogeneousHGCalHEBConditions.cc:74
hgcal_conditions::parameters::HeterogeneousHGCalHEBParametersType::Double
HeterogeneousHGCalHEBConditionsWrapper::gpuData_
cms::cuda::ESProduct< GPUData > gpuData_
Definition: HeterogeneousHGCalHEBConditions.h:59
HeterogeneousHGCalHEBConditionsWrapper::getHeterogeneousConditionsESProductAsync
const hgcal_conditions::HeterogeneousHEBConditionsESProduct * getHeterogeneousConditionsESProductAsync(cudaStream_t stream) const
Definition: HeterogeneousHGCalHEBConditions.cc:125
HeterogeneousHGCalHEBConditionsWrapper::select_pointer_d
double *& select_pointer_d(cp::HeterogeneousHGCalHEBParameters *, const unsigned int &) const
Definition: HeterogeneousHGCalHEBConditions.cc:80
HeterogeneousHGCalHEBConditionsWrapper::calculate_memory_bytes
void calculate_memory_bytes(const HGCalParameters *)
Definition: HeterogeneousHGCalHEBConditions.cc:51
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
HeterogeneousHGCalHEBConditionsWrapper::GPUData
Definition: HeterogeneousHGCalHEBConditions.h:48
HeterogeneousHGCalHEBConditionsWrapper::params_
cp::HeterogeneousHGCalHEBParameters params_
Definition: HeterogeneousHGCalHEBConditions.h:35
hgcal_conditions::parameters::HeterogeneousHGCalHEBParameters
Definition: HGCConditions.h:127
HcalDetIdTransform::transform
unsigned transform(const HcalDetId &id, unsigned transformCode)
Definition: HcalDetIdTransform.cc:7
HeterogeneousHGCalHEBConditionsWrapper::HeterogeneousHGCalHEBConditionsWrapper
HeterogeneousHGCalHEBConditionsWrapper(const HGCalParameters *)
Definition: HeterogeneousHGCalHEBConditions.cc:3
hgcal_conditions::HeterogeneousHEBConditionsESProduct
Definition: HGCConditions.h:192
hgcal_conditions::parameters::HeterogeneousHGCalHEBParameters::testD_
double * testD_
Definition: HGCConditions.h:129
hgcal_conditions::parameters::typesHEB
const std::array< HeterogeneousHGCalHEBParametersType, 2 > typesHEB
Definition: HGCConditions.h:104
HeterogeneousHGCalHEBConditionsWrapper::GPUData::device
hgcal_conditions::HeterogeneousHEBConditionsESProduct * device
Definition: HeterogeneousHGCalHEBConditions.h:54
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
HGCalParameters
Definition: HGCalParameters.h:14
HeterogeneousHGCalHEBConditionsWrapper::chunk_
size_t chunk_
Definition: HeterogeneousHGCalHEBConditions.h:38
cudaCheck
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:69
HeterogeneousHGCalHEBConditionsWrapper::GPUData::~GPUData
~GPUData()
Definition: HeterogeneousHGCalHEBConditions.cc:169
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
HeterogeneousHGCalHEBConditionsWrapper::GPUData::host
hgcal_conditions::HeterogeneousHEBConditionsESProduct * host
Definition: HeterogeneousHGCalHEBConditions.h:52
HGCalParameters::waferTypeL_
std::vector< int > waferTypeL_
Definition: HGCalParameters.h:116
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
HeterogeneousHGCalHEBConditions.h
hgcal_conditions::HeterogeneousHEBConditionsESProduct::params
parameters::HeterogeneousHGCalHEBParameters params
Definition: HGCConditions.h:193