MaCh3  2.8.0
Reference Guide
Public Member Functions | Private Attributes | List of all members
SplineMonolithGPU Class Reference

Class responsible for calculating spline weight on GPU. More...

Collaboration diagram for SplineMonolithGPU:
[legend]

Public Member Functions

 SplineMonolithGPU ()
 constructor More...
 
virtual ~SplineMonolithGPU ()
 This function deallocates the resources allocated for the separate {x} and {ybcd} arrays in the and TF1 stuff at GPU. More...
 
__host__ void InitGPU_SplineMonolith (unsigned int total_nknots, unsigned int n_splines, unsigned int n_tf1, int Eve_size)
 Allocate memory on gpu for spline monolith. More...
 
__host__ void InitGPU_Unbinned_SplineMonolith (M3::float_t **cpu_total_weights, int n_events)
 Allocate memory on gpu for spline monolith. More...
 
__host__ void CopyToGPU_SplineMonolith_Unbinned (const SplineMonoStruct *cpu_spline_handler, const std::vector< float > &cpu_many_array_TF1, const std::vector< short int > &cpu_paramNo_arr_TF1, const int n_events, const std::vector< unsigned int > &cpu_nParamPerEvent, const std::vector< unsigned int > &cpu_nParamPerEvent_TF1, const int n_params, const unsigned int n_splines, const short int spline_size, const unsigned int total_nknots, const unsigned int n_tf1)
 Copies data from CPU to GPU for the spline monolith. More...
 
__host__ void CopyToGPU_SplineMonolith_Binned (M3::float_t *manycoeff_arr, M3::float_t *xcoeff_arr, const std::vector< short int > &uniquesplinevec_Monolith, const std::vector< unsigned int > &coeffindexvec, const int n_params, const unsigned int n_splines, const short int spline_size, const unsigned int total_nknots)
 Copies data from CPU to GPU for the spline monolith. More...
 
__host__ void InitGPU_Segments (short int **segment)
 Allocate memory for spline segments. More...
 
__host__ void InitGPU_Vals (float **vals)
 Allocate memory for spline segments. More...
 
__host__ void RunGPU_SplineMonolith_Unbinned (M3::float_t *cpu_total_weights, float *vals, short int *segment)
 Run the GPU code for the separate many arrays. As in separate {x}, {y,b,c,d} arrays Pass the segment and the parameter values (binary search already performed in SplineBase::FindSplineSegment() More...
 
__host__ void RunGPU_SplineMonolith_Binned (M3::float_t *cpu_spline_weights, float *vals, short int *segment)
 
__host__ void CleanupPinnedMemory (M3::float_t *cpu_total_weights, short int *segment, float *vals)
 Clean up pinned variables at CPU. More...
 

Private Attributes

unsigned int * gpu_nParamPerEvent
 KS: GPU map keeping track how many parameters applies to each event, we keep two numbers here {number of splines per event, index where splines start for a given event}. More...
 
unsigned int * gpu_nParamPerEvent_TF1
 KS: GPU map keeping track how many parameters applies to each event, we keep two numbers here {number of TF1 per event, index where TF1 start for a given event}. More...
 
float * gpu_coeff_x
 KS: GPU arrays to hold X coefficient. More...
 
float * gpu_coeff_many
 GPU arrays to hold other coefficients. More...
 
unsigned int * gpu_nKnots_arr
 KS: GPU Number of knots per spline. More...
 
short int * gpu_paramNo_arr
 CW: GPU array with the number of points per spline (not per spline point!) More...
 
float * gpu_coeff_TF1_many
 GPU arrays to hold TF1 coefficients. More...
 
short int * gpu_nPoints_arr
 GPU arrays to hold number of points. More...
 
short int * gpu_paramNo_TF1_arr
 CW: GPU array with the number of points per TF1 object. More...
 
float * gpu_par_val
 CW: parameter value on GPU. More...
 
short int * gpu_spline_segment
 CW: Spline segment on GPU. More...
 
M3::float_t * gpu_total_weights
 GPU arrays to hold weight for event. More...
 
float * gpu_weights
 GPU arrays to hold weight for each spline. More...
 
float * gpu_weights_tf1
 GPU arrays to hold weight for each TF1. More...
 
short int cpu_spline_size
 Size of splines living on CPU. More...
 
unsigned int cpu_n_splines
 Number of splines living on CPU. More...
 
unsigned int cpu_n_TF1
 Number of tf1 living on CPU. More...
 
int cpu_n_params
 Number of params living on CPU. More...
 
int cpu_n_events
 Number of events living on CPU. More...
 
std::vector< float > cpu_tmp_weights
 Temporary weight needed for conversion from float to double. More...
 
cudaTextureObject_t text_coeff_x = 0
 KS: Textures are L1 cache variables which are well optimised for fetching. Make texture only for variables you often access but rarely overwrite. There are limits on texture memory so don't use huge arrays. More...
 
cudaTextureObject_t text_nParamPerEvent = 0
 KS: Map keeping track how many parameters applies to each event, we keep two numbers here {number of splines per event, index where splines start for a given event}. More...
 
cudaTextureObject_t text_nParamPerEvent_TF1 = 0
 KS: Map keeping track how many parameters applies to each event, we keep two numbers here {number of TF1 per event, index where TF1 start for a given event}. More...
 

Detailed Description

Class responsible for calculating spline weight on GPU.

Author
Richard Calland
Asher Kaboth
Clarence Wret
Kamil Skwarczynski

Definition at line 74 of file gpuSplineUtils.cuh.

Constructor & Destructor Documentation

◆ SplineMonolithGPU()

SplineMonolithGPU::SplineMonolithGPU ( )

constructor

Number of events living on CPU

Definition at line 58 of file gpuSplineUtils.cu.

58  {
60  cpu_n_params = -1;
61  cpu_n_events = -1;
62  cpu_n_TF1 = 0;
63  cpu_n_splines = 0;
64  cpu_spline_size = 0;
65 
66  gpu_weights = nullptr;
67  gpu_total_weights = nullptr;
68  gpu_nParamPerEvent = nullptr;
69  gpu_nPoints_arr = nullptr;
70  gpu_paramNo_arr = nullptr;
71  gpu_nKnots_arr = nullptr;
72  gpu_coeff_x = nullptr;
73  gpu_coeff_many = nullptr;
74  gpu_coeff_TF1_many = nullptr;
75  gpu_paramNo_TF1_arr = nullptr;
76  gpu_nParamPerEvent_TF1 = nullptr;
77  gpu_weights_tf1 = nullptr;
78 }
float * gpu_weights
GPU arrays to hold weight for each spline.
unsigned int * gpu_nParamPerEvent
KS: GPU map keeping track how many parameters applies to each event, we keep two numbers here {number...
unsigned int cpu_n_TF1
Number of tf1 living on CPU.
unsigned int * gpu_nParamPerEvent_TF1
KS: GPU map keeping track how many parameters applies to each event, we keep two numbers here {number...
float * gpu_weights_tf1
GPU arrays to hold weight for each TF1.
unsigned int * gpu_nKnots_arr
KS: GPU Number of knots per spline.
float * gpu_coeff_TF1_many
GPU arrays to hold TF1 coefficients.
float * gpu_coeff_many
GPU arrays to hold other coefficients.
short int * gpu_paramNo_TF1_arr
CW: GPU array with the number of points per TF1 object.
int cpu_n_params
Number of params living on CPU.
int cpu_n_events
Number of events living on CPU.
short int * gpu_nPoints_arr
GPU arrays to hold number of points.
unsigned int cpu_n_splines
Number of splines living on CPU.
short int * gpu_paramNo_arr
CW: GPU array with the number of points per spline (not per spline point!)
float * gpu_coeff_x
KS: GPU arrays to hold X coefficient.
short int cpu_spline_size
Size of splines living on CPU.
M3::float_t * gpu_total_weights
GPU arrays to hold weight for event.

◆ ~SplineMonolithGPU()

SplineMonolithGPU::~SplineMonolithGPU ( )
virtual

This function deallocates the resources allocated for the separate {x} and {ybcd} arrays in the and TF1 stuff at GPU.

Definition at line 81 of file gpuSplineUtils.cu.

81  {
82 // *******************************************
83  cudaFree(gpu_paramNo_arr);
84  cudaFree(gpu_nKnots_arr);
85 
86  // free the coefficient arrays
87  cudaDestroyTextureObject(text_coeff_x);
88  cudaFree(gpu_coeff_x);
89  cudaFree(gpu_coeff_many);
90 
91  cudaFree(gpu_par_val);
92  cudaFree(gpu_spline_segment);
93 
94  cudaFree(gpu_coeff_TF1_many);
95  cudaFree(gpu_paramNo_TF1_arr);
96  // free weights on the gpu
97  cudaFree(gpu_weights);
98  cudaFree(gpu_weights_tf1);
99  cudaFree(gpu_total_weights);
100  //KS: Before removing variable let's destroy texture
101  cudaDestroyTextureObject(text_nParamPerEvent);
102  cudaDestroyTextureObject(text_nParamPerEvent_TF1);
103 
104  cudaFree(gpu_nParamPerEvent);
105  cudaFree(gpu_nParamPerEvent_TF1);
106 }
float * gpu_par_val
CW: parameter value on GPU.
cudaTextureObject_t text_nParamPerEvent_TF1
KS: Map keeping track how many parameters applies to each event, we keep two numbers here {number of ...
cudaTextureObject_t text_coeff_x
KS: Textures are L1 cache variables which are well optimised for fetching. Make texture only for vari...
cudaTextureObject_t text_nParamPerEvent
KS: Map keeping track how many parameters applies to each event, we keep two numbers here {number of ...
short int * gpu_spline_segment
CW: Spline segment on GPU.

Member Function Documentation

◆ CleanupPinnedMemory()

__host__ void SplineMonolithGPU::CleanupPinnedMemory ( M3::float_t *  cpu_total_weights,
short int *  segment,
float *  vals 
)

Clean up pinned variables at CPU.

Parameters
cpu_total_weightsPointer to the total weights array on the CPU (used if Weight_On_SplineBySpline_Basis is not defined).
segmentFound spline segment for each parameter
valsValue to which we want reweight for each parameter

Definition at line 657 of file gpuSplineUtils.cu.

658  {
659 // *******************************************
660  if(cpu_total_weights != nullptr) cudaFreeHost(cpu_total_weights);
661  cudaFreeHost(segment);
662  cudaFreeHost(vals);
663 
664  cpu_total_weights = nullptr;
665  segment = nullptr;
666  vals = nullptr;
667 }

◆ CopyToGPU_SplineMonolith_Binned()

__host__ void SplineMonolithGPU::CopyToGPU_SplineMonolith_Binned ( M3::float_t *  manycoeff_arr,
M3::float_t *  xcoeff_arr,
const std::vector< short int > &  uniquesplinevec_Monolith,
const std::vector< unsigned int > &  coeffindexvec,
const int  n_params,
const unsigned int  n_splines,
const short int  spline_size,
const unsigned int  total_nknots 
)

Copies data from CPU to GPU for the spline monolith.

Size of splines living

Definition at line 325 of file gpuSplineUtils.cu.

334  {
335 // ******************************************************
336  // Write to the global statics (h_* denotes host stored variable)
337  cpu_n_params = n_params;
338  // Total number of valid splines for all loaded events
339  cpu_n_splines = n_splines;
341  cpu_spline_size = spline_size;
342 
343 
344  //CW: Allocate memory for the frequently copied objects
345  cudaMalloc(&gpu_par_val, n_params * sizeof(float));
346  cudaMalloc(&gpu_spline_segment, n_params * sizeof(short int));
347  #ifndef _LOW_MEMORY_STRUCTS_
349  // Convert M3::float_t -> float for GPU
350  std::vector<float> coeff_many_float(manycoeff_arr, manycoeff_arr + total_nknots * _nCoeff_);
351  std::vector<float> coeff_x_float(xcoeff_arr, xcoeff_arr + spline_size * n_params);
352 
353  // Copy the coefficient arrays to the GPU; this only happens once per entire Markov Chain so is OK to do multiple extensive memory copies
354  cudaMemcpy(gpu_coeff_many, coeff_many_float.data(), sizeof(float)*total_nknots*_nCoeff_, cudaMemcpyHostToDevice);
355  CudaCheckError();
356 
357  cudaMemcpy(gpu_coeff_x, coeff_x_float.data(), sizeof(float)*spline_size*n_params, cudaMemcpyHostToDevice);
358  CudaCheckError();
359  #else
360  // Copy the coefficient arrays to the GPU; this only happens once per entire Markov Chain so is OK to do multiple extensive memory copies
361  cudaMemcpy(gpu_coeff_many, manycoeff_arr, sizeof(float)*total_nknots*_nCoeff_, cudaMemcpyHostToDevice);
362  CudaCheckError();
363 
364  cudaMemcpy(gpu_coeff_x, xcoeff_arr, sizeof(float)*spline_size*n_params, cudaMemcpyHostToDevice);
365  CudaCheckError();
366  #endif
367  //KS: Bind our texture with the GPU variable
368  //KS: Tried also moving gpu_many_array to texture memory it only worked with restricted number of MC runs, most likely hit texture memory limit :(
369  struct cudaResourceDesc resDesc_coeff_x;
370  memset(&resDesc_coeff_x, 0, sizeof(resDesc_coeff_x));
371  resDesc_coeff_x.resType = cudaResourceTypeLinear;
372  resDesc_coeff_x.res.linear.devPtr = gpu_coeff_x;
373  resDesc_coeff_x.res.linear.desc = cudaCreateChannelDesc<float>();
374  resDesc_coeff_x.res.linear.sizeInBytes = sizeof(float)*spline_size*n_params;
375 
376  // Specify texture object parameters
377  struct cudaTextureDesc texDesc_coeff_x;
378  memset(&texDesc_coeff_x, 0, sizeof(texDesc_coeff_x));
379  texDesc_coeff_x.readMode = cudaReadModeElementType;
380 
381  // Create texture object
382  cudaCreateTextureObject(&text_coeff_x, &resDesc_coeff_x, &texDesc_coeff_x, nullptr);
383  CudaCheckError();
384 
385  // Also copy the parameter number for each spline onto the GPU; i.e. what spline parameter are we calculating right now
386  cudaMemcpy(gpu_paramNo_arr, uniquesplinevec_Monolith.data(), n_splines*sizeof(short int), cudaMemcpyHostToDevice);
387  CudaCheckError();
388 
389  // Also copy the knot map for each spline onto the GPU;
390  cudaMemcpy(gpu_nKnots_arr, coeffindexvec.data(), n_splines*sizeof(unsigned int), cudaMemcpyHostToDevice);
391  CudaCheckError();
392 }
constexpr int _nCoeff_
KS: We store coefficients {y,b,c,d} in one array one by one, this is only to define it once rather th...
Definition: SplineCommon.h:18
std::vector< float > cpu_tmp_weights
Temporary weight needed for conversion from float to double.
#define CudaCheckError()
Definition: gpuUtils.cuh:21

◆ CopyToGPU_SplineMonolith_Unbinned()

__host__ void SplineMonolithGPU::CopyToGPU_SplineMonolith_Unbinned ( const SplineMonoStruct *  cpu_spline_handler,
const std::vector< float > &  cpu_many_array_TF1,
const std::vector< short int > &  cpu_paramNo_arr_TF1,
const int  n_events,
const std::vector< unsigned int > &  cpu_nParamPerEvent,
const std::vector< unsigned int > &  cpu_nParamPerEvent_TF1,
const int  n_params,
const unsigned int  n_splines,
const short int  spline_size,
const unsigned int  total_nknots,
const unsigned int  n_tf1 
)

Copies data from CPU to GPU for the spline monolith.

This function transfers the necessary spline data and parameters from the CPU to the GPU, including TF1-related arrays and parameters. This setup is crucial for spline evaluations on the GPU.

Parameters
cpu_spline_handlerPointer to the structure managing spline data on the CPU.
cpu_many_array_TF1Array of TF1 parameters on the CPU.
cpu_paramNo_arr_TF1Array containing parameter numbers for TF1 objects.
n_eventsNumber of events, necessary for correct data handling.
cpu_nParamPerEventArray indicating the number of parameters per event.
cpu_nParamPerEvent_TF1Array indicating the number of parameters per TF1 object.
n_paramsTotal number of parameters across all splines.
n_splinesTotal number of spline objects.
spline_sizeSize of each spline object.
total_nknotsTotal number of knots across all splines.
n_tf1Total number of TF1 objects.

Size of splines living

Definition at line 204 of file gpuSplineUtils.cu.

219  {
220 // ******************************************************
221  // Write to the global statics (h_* denotes host stored variable)
222  cpu_n_params = n_params;
223  // Number of events
224  cpu_n_events = n_events;
225  // Total number of valid TF1 for all loaded events
226  cpu_n_TF1 = n_tf1;
227  // Total number of valid splines for all loaded events
228  cpu_n_splines = n_splines;
230  cpu_spline_size = spline_size;
231 
232  //CW: Allocate memory for the frequently copied objects
233  cudaMalloc(&gpu_par_val, n_params * sizeof(float));
234  cudaMalloc(&gpu_spline_segment, n_params * sizeof(short int));
235 
236  // Copy the coefficient arrays to the GPU; this only happens once per entire Markov Chain so is OK to do multiple extensive memory copies
237  cudaMemcpy(gpu_coeff_many, cpu_spline_handler->coeff_many.data(), sizeof(float)*total_nknots*_nCoeff_, cudaMemcpyHostToDevice);
238  CudaCheckError();
239 
240  cudaMemcpy(gpu_coeff_x, cpu_spline_handler->coeff_x.data(), sizeof(float)*spline_size*n_params, cudaMemcpyHostToDevice);
241  CudaCheckError();
242 
243  //KS: Bind our texture with the GPU variable
244  //KS: Tried also moving gpu_many_array to texture memory it only worked with restricted number of MC runs, most likely hit texture memory limit :(
245  struct cudaResourceDesc resDesc_coeff_x;
246  memset(&resDesc_coeff_x, 0, sizeof(resDesc_coeff_x));
247  resDesc_coeff_x.resType = cudaResourceTypeLinear;
248  resDesc_coeff_x.res.linear.devPtr = gpu_coeff_x;
249  resDesc_coeff_x.res.linear.desc = cudaCreateChannelDesc<float>();
250  resDesc_coeff_x.res.linear.sizeInBytes = sizeof(float)*spline_size*n_params;
251 
252  // Specify texture object parameters
253  struct cudaTextureDesc texDesc_coeff_x;
254  memset(&texDesc_coeff_x, 0, sizeof(texDesc_coeff_x));
255  texDesc_coeff_x.readMode = cudaReadModeElementType;
256 
257  // Create texture object
258  cudaCreateTextureObject(&text_coeff_x, &resDesc_coeff_x, &texDesc_coeff_x, nullptr);
259  CudaCheckError();
260 
261  // Also copy the parameter number for each spline onto the GPU; i.e. what spline parameter are we calculating right now
262  cudaMemcpy(gpu_paramNo_arr, cpu_spline_handler->paramNo_arr.data(), n_splines*sizeof(short int), cudaMemcpyHostToDevice);
263  CudaCheckError();
264 
265  // Also copy the knot map for each spline onto the GPU;
266  cudaMemcpy(gpu_nKnots_arr, cpu_spline_handler->nKnots_arr.data(), n_splines*sizeof(unsigned int), cudaMemcpyHostToDevice);
267  CudaCheckError();
268 
269  //Now TF1
270  // Copy the coefficient arrays to the GPU; this only happens once per entire Markov Chain so is OK to do multiple extensive memory copies
271  cudaMemcpy(gpu_coeff_TF1_many, cpu_many_array_TF1.data(), sizeof(float)*n_tf1*_nTF1Coeff_, cudaMemcpyHostToDevice);
272  CudaCheckError();
273 
274  // Also copy the parameter number for each TF1 onto the GPU; i.e. what TF1 parameter are we calculating right now
275  cudaMemcpy(gpu_paramNo_TF1_arr, cpu_paramNo_arr_TF1.data(), n_tf1*sizeof(short int), cudaMemcpyHostToDevice);
276  CudaCheckError();
277 
278  //KS: Keep track how much splines each event has
279  cudaMemcpy(gpu_nParamPerEvent, cpu_nParamPerEvent.data(), 2*n_events*sizeof(unsigned int), cudaMemcpyHostToDevice);
280  CudaCheckError();
281 
282  //KS: Bind our texture with the GPU variable
283  // create a resource descriptor based on device pointers
284  struct cudaResourceDesc resDesc_nParamPerEvent;
285  memset(&resDesc_nParamPerEvent, 0, sizeof(resDesc_nParamPerEvent));
286  resDesc_nParamPerEvent.resType = cudaResourceTypeLinear;
287  resDesc_nParamPerEvent.res.linear.devPtr = gpu_nParamPerEvent;
288  resDesc_nParamPerEvent.res.linear.desc = cudaCreateChannelDesc<unsigned int>();
289  resDesc_nParamPerEvent.res.linear.sizeInBytes = 2*n_events*sizeof(unsigned int);
290 
291  // Specify texture object parameters
292  struct cudaTextureDesc texDesc_nParamPerEvent;
293  memset(&texDesc_nParamPerEvent, 0, sizeof(texDesc_nParamPerEvent));
294  texDesc_nParamPerEvent.readMode = cudaReadModeElementType;
295 
296  //Finally create texture object
297  cudaCreateTextureObject(&text_nParamPerEvent, &resDesc_nParamPerEvent, &texDesc_nParamPerEvent, nullptr);
298  CudaCheckError();
299 
300  // Now TF1
301  cudaMemcpy(gpu_nParamPerEvent_TF1, cpu_nParamPerEvent_TF1.data(), 2*n_events*sizeof(unsigned int), cudaMemcpyHostToDevice);
302  CudaCheckError();
303 
304  //KS: Bind our texture with the GPU variable
305  // create a resource descriptor based on device pointers
306  struct cudaResourceDesc resDesc_nParamPerEvent_tf1;
307  memset(&resDesc_nParamPerEvent_tf1, 0, sizeof(resDesc_nParamPerEvent_tf1));
308  resDesc_nParamPerEvent_tf1.resType = cudaResourceTypeLinear;
309  resDesc_nParamPerEvent_tf1.res.linear.devPtr = gpu_nParamPerEvent_TF1;
310  resDesc_nParamPerEvent_tf1.res.linear.desc = cudaCreateChannelDesc<unsigned int>();
311  resDesc_nParamPerEvent_tf1.res.linear.sizeInBytes = 2*n_events*sizeof(unsigned int);
312 
313  // Specify texture object parameters
314  struct cudaTextureDesc texDesc_nParamPerEvent_tf1;
315  memset(&texDesc_nParamPerEvent_tf1, 0, sizeof(texDesc_nParamPerEvent_tf1));
316  texDesc_nParamPerEvent_tf1.readMode = cudaReadModeElementType;
317 
318  //Finally create texture object
319  cudaCreateTextureObject(&text_nParamPerEvent_TF1, &resDesc_nParamPerEvent_tf1, &texDesc_nParamPerEvent_tf1, nullptr);
320  CudaCheckError();
321 }
constexpr int _nTF1Coeff_
KS: For TF1 we store at most 5 coefficients, we could make it more flexible but for now define it her...
Definition: SplineCommon.h:20
std::vector< unsigned int > nKnots_arr
KS: CPU Number of knots per spline.
Definition: SplineCommon.h:73
std::vector< float > coeff_x
KS: CPU arrays to hold X coefficient.
Definition: SplineCommon.h:67
std::vector< float > coeff_many
CPU arrays to hold other coefficients.
Definition: SplineCommon.h:70
std::vector< short int > paramNo_arr
CW: CPU array with the number of points per spline (not per spline point!)
Definition: SplineCommon.h:76

◆ InitGPU_Segments()

__host__ void SplineMonolithGPU::InitGPU_Segments ( short int **  segment)

Allocate memory for spline segments.

Parameters
segmentFound spline segment for each parameter

Definition at line 182 of file gpuSplineUtils.cu.

182  {
183 // *******************************************
184  //KS: Rather than allocate memory in standard way this fancy cuda tool allows to pin host memory which make memory transfer faster
185  cudaMallocHost((void **) segment, cpu_n_params*sizeof(short int));
186  CudaCheckError();
187 }

◆ InitGPU_SplineMonolith()

__host__ void SplineMonolithGPU::InitGPU_SplineMonolith ( unsigned int  total_nknots,
unsigned int  n_splines,
unsigned int  n_tf1,
int  Eve_size 
)

Allocate memory on gpu for spline monolith.

Parameters
cpu_total_weightsKS: Rather than allocate memory in standard way this fancy cuda tool allows to pin host memory which make memory transfer faster
n_eventsNumber of events, this is necessary to allocate correctly memory
total_nknotsTotal number of knots in all splines summed
n_splinesTotal number of spline objects, not knots
n_tf1Total number of TF1 objects, not coefficients

Definition at line 110 of file gpuSplineUtils.cu.

114  {
115 // *******************************************
116  // Allocate chunks of memory to GPU
117  cudaMalloc((void **) &gpu_paramNo_arr, n_splines*sizeof(short int));
118  CudaCheckError();
119 
120  cudaMalloc((void **) &gpu_nKnots_arr, n_splines*sizeof(unsigned int));
121  CudaCheckError();
122 
123  cudaMalloc((void **) &gpu_coeff_x, Eve_size*sizeof(float));
124  CudaCheckError();
125 
126  cudaMalloc((void **) &gpu_coeff_many, _nCoeff_*total_nknots*sizeof(float));
127  CudaCheckError();
128 
129  // Allocate memory for the array of weights to be returned to CPU
130  cudaMalloc((void **) &gpu_weights, n_splines*sizeof(float));
131  CudaCheckError();
132 
133  // Now TF1 specific
134  cudaMalloc((void **) &gpu_coeff_TF1_many, _nTF1Coeff_*n_tf1*sizeof(float));
135  CudaCheckError();
136 
137  cudaMalloc((void **) &gpu_weights_tf1, n_tf1*sizeof(float));
138  CudaCheckError();
139 
140  cudaMalloc((void **) &gpu_paramNo_TF1_arr, n_tf1*sizeof(short int));
141  CudaCheckError();
142 
143  // Print allocation info to user
144  printf("Allocated %i entries for paramNo and nKnots arrays, size = %f MB\n",
145  n_splines, static_cast<double>(sizeof(short int) * n_splines + sizeof(unsigned int) * n_splines) / 1.0e6);
146  printf("Allocated %i entries for x coeff arrays, size = %f MB\n",
147  Eve_size, static_cast<double>(sizeof(float) * Eve_size) / 1.0e6);
148  printf("Allocated %i entries for {ybcd} coeff arrays, size = %f MB\n",
149  _nCoeff_ * total_nknots, static_cast<double>(sizeof(float) * _nCoeff_ * total_nknots) / 1.0e6);
150  printf("Allocated %i entries for TF1 coefficient arrays, size = %f MB\n",
151  _nTF1Coeff_ * n_tf1, static_cast<double>(sizeof(float) * _nTF1Coeff_ * n_tf1) / 1.0e6);
152 
153  //KS: Ask CUDA about memory usage
154  checkGpuMem();
155  PrintNdevices();
156 }
void checkGpuMem()
KS: Get some fancy info about VRAM usage.
Definition: gpuUtils.cu:43
void PrintNdevices()
KS: Get some fancy info about GPU.
Definition: gpuUtils.cu:58

◆ InitGPU_Unbinned_SplineMonolith()

__host__ void SplineMonolithGPU::InitGPU_Unbinned_SplineMonolith ( M3::float_t **  cpu_total_weights,
int  n_events 
)

Allocate memory on gpu for spline monolith.

Parameters
cpu_total_weightsKS: Rather than allocate memory in standard way this fancy cuda tool allows to pin host memory which make memory transfer faster
n_eventsNumber of events, this is necessary to allocate correctly memory

Definition at line 159 of file gpuSplineUtils.cu.

161  {
162 // *******************************************
163  //KS: Rather than allocate memory in standard way this fancy cuda tool allows to pin host memory which make memory transfer faster
164  cudaMallocHost((void **) cpu_total_weights, n_events*sizeof(M3::float_t));
165  CudaCheckError();
166 
167  //KS: Allocate memory for the array of total weights to be returned to CPU
168  cudaMalloc((void **) &gpu_total_weights, n_events*sizeof(M3::float_t));
169  CudaCheckError();
170 
171  //KS: Allocate memory for the map keeping track how many splines each parameter has
172  cudaMalloc((void **) &gpu_nParamPerEvent, 2*n_events*sizeof(unsigned int));
173  CudaCheckError();
174 
175  //KS: Allocate memory for the map keeping track how many TF1 each parameter has
176  cudaMalloc((void **) &gpu_nParamPerEvent_TF1, 2*n_events*sizeof(unsigned int));
177  CudaCheckError();
178 }
double float_t
Definition: Core.h:37

◆ InitGPU_Vals()

__host__ void SplineMonolithGPU::InitGPU_Vals ( float **  vals)

Allocate memory for spline segments.

Parameters
valsValue to which we want reweight for each parameter

Definition at line 191 of file gpuSplineUtils.cu.

191  {
192 // *******************************************
193  //KS: Rather than allocate memory in standard way this fancy cuda tool allows to pin host memory which make memory transfer faster
194  cudaMallocHost((void **) vals, cpu_n_params*sizeof(float));
195  CudaCheckError();
196 }

◆ RunGPU_SplineMonolith_Binned()

__host__ void SplineMonolithGPU::RunGPU_SplineMonolith_Binned ( M3::float_t *  cpu_spline_weights,
float *  vals,
short int *  segment 
)
Todo:
add maybe some ifdef to avoid doing this when we actually have float

Definition at line 595 of file gpuSplineUtils.cu.

600  {
601 // *****************************************
602  dim3 block_size;
603  dim3 grid_size;
604 
605  block_size.x = _BlockSize_;
606  grid_size.x = (cpu_n_splines / block_size.x) + 1;
607 
608  // Copy the segment values to the GPU (segment_gpu), which is cpu_n_params long
609  cudaMemcpy(gpu_spline_segment, segment, cpu_n_params * sizeof(short int), cudaMemcpyHostToDevice);
610  CudaCheckError();
611 
612  // Copy the parameter values values to the GPU (vals_gpu), which is cpu_n_params long
613  cudaMemcpy(gpu_par_val, vals, cpu_n_params * sizeof(float), cudaMemcpyHostToDevice);
614  CudaCheckError();
615 
616  // KS: Consider asynchronous kernel call, this might help EvalOnGPU_Splines and EvalOnGPU_TF1 are independent
617  // Set the cache config to prefer L1 for the kernel
618  //cudaFuncSetCacheConfig(EvalOnGPU_Splines, cudaFuncCachePreferL1);
619  EvalOnGPU_Splines<<<grid_size, block_size>>>(
625  gpu_par_val,
627 
628  gpu_weights,
630  );
631  CudaCheckError();
632 
633  #ifndef _LOW_MEMORY_STRUCTS_
634  // Here we have to make a somewhat large GPU->CPU transfer because it's all the splines' response
635  cudaMemcpy(cpu_tmp_weights.data(), gpu_weights, cpu_n_splines * sizeof(float), cudaMemcpyDeviceToHost);
636  CudaCheckError();
637  // KS: we need to perform conversion from float (this is what GPU uses) to M3::float_t
639  for (unsigned int i = 0; i < cpu_n_splines; ++i) {
640  cpu_spline_weights[i] = static_cast<M3::float_t>(cpu_tmp_weights[i]);
641  }
642  #else
643  // Here we have to make a somewhat large GPU->CPU transfer because it's all the splines' response
645  cudaMemcpy(cpu_spline_weights, gpu_weights, cpu_n_splines * sizeof(float), cudaMemcpyDeviceToHost);
646  CudaCheckError();
647  #endif
648 
649  #ifdef MACH3_DEBUG
650  printf("Copied GPU total weights to CPU with SUCCESS (drink more tea)\n");
651  printf("Released calculated response from GPU with SUCCESS (drink most tea)\n");
652  #endif
653 }
#define _BlockSize_
KS: Need it for shared memory, there is way to use dynamic shared memory but I am lazy right now.
Definition: gpuUtils.cuh:24

◆ RunGPU_SplineMonolith_Unbinned()

__host__ void SplineMonolithGPU::RunGPU_SplineMonolith_Unbinned ( M3::float_t *  cpu_total_weights,
float *  vals,
short int *  segment 
)

Run the GPU code for the separate many arrays. As in separate {x}, {y,b,c,d} arrays Pass the segment and the parameter values (binary search already performed in SplineBase::FindSplineSegment()

Executes the GPU code for calculating spline weights.

This function runs the GPU computation for the spline monolith. It assumes that the appropriate segment has already been identified through binary search in the SplineBase::FindSplineSegment() function.

Parameters
cpu_weightsPointer to the array of weights on the CPU (used if Weight_On_SplineBySpline_Basis is defined).
cpu_weights_tf1Pointer to the array of TF1 weights (used if Weight_On_SplineBySpline_Basis is defined).
cpu_total_weightsPointer to the total weights array (used if Weight_On_SplineBySpline_Basis is not defined).
valsPointer to an array holding the parameter values to be processed.
segmentPointer to an array containing segment indices for parameters.

Definition at line 518 of file gpuSplineUtils.cu.

523  {
524 // *****************************************
525  dim3 block_size;
526  dim3 grid_size;
527 
528  block_size.x = _BlockSize_;
529  grid_size.x = (cpu_n_splines / block_size.x) + 1;
530 
531  // Copy the segment values to the GPU (segment_gpu), which is cpu_n_params long
532  cudaMemcpy(gpu_spline_segment, segment, cpu_n_params * sizeof(short int), cudaMemcpyHostToDevice);
533  CudaCheckError();
534 
535  // Copy the parameter values values to the GPU (vals_gpu), which is cpu_n_params long
536  cudaMemcpy(gpu_par_val, vals, cpu_n_params * sizeof(float), cudaMemcpyHostToDevice);
537  CudaCheckError();
538 
539  // KS: Consider asynchronous kernel call, this might help EvalOnGPU_Splines and EvalOnGPU_TF1 are independent
540  // Set the cache config to prefer L1 for the kernel
541  //cudaFuncSetCacheConfig(EvalOnGPU_Splines, cudaFuncCachePreferL1);
542  EvalOnGPU_Splines<<<grid_size, block_size>>>(
548  gpu_par_val,
550 
551  gpu_weights,
553  );
554  CudaCheckError();
555 
556  grid_size.x = (cpu_n_TF1 / block_size.x) + 1;
557  EvalOnGPU_TF1<<<grid_size, block_size>>>(
558  cpu_n_TF1,
561  gpu_par_val,
563  );
564  CudaCheckError();
565 
566  grid_size.x = (cpu_n_events / block_size.x) + 1;
567 
568  EvalOnGPU_TotWeight<<<grid_size, block_size>>>(
569  cpu_n_events,
570  gpu_weights,
572 
574 
577  );
578  CudaCheckError();
579 
580  //KS: Here we have to make a somewhat large GPU->CPU transfer because it is proportional to number of events
581  //KS: Normally code wait for memory transfer to finish before moving further cudaMemcpyAsync means we will continue to execute code and in a meantime keep copying stuff.
582  cudaMemcpyAsync(cpu_total_weights, gpu_total_weights, cpu_n_events * sizeof(M3::float_t), cudaMemcpyDeviceToHost, 0);
583  CudaCheckError();
584 
585  #ifdef MACH3_DEBUG
586  printf("Copied GPU total weights to CPU with SUCCESS (drink more tea)\n");
587  printf("Released calculated response from GPU with SUCCESS (drink most tea)\n");
588  #endif
589 }

Member Data Documentation

◆ cpu_n_events

int SplineMonolithGPU::cpu_n_events
private

Number of events living on CPU.

Definition at line 243 of file gpuSplineUtils.cuh.

◆ cpu_n_params

int SplineMonolithGPU::cpu_n_params
private

Number of params living on CPU.

Definition at line 241 of file gpuSplineUtils.cuh.

◆ cpu_n_splines

unsigned int SplineMonolithGPU::cpu_n_splines
private

Number of splines living on CPU.

Definition at line 237 of file gpuSplineUtils.cuh.

◆ cpu_n_TF1

unsigned int SplineMonolithGPU::cpu_n_TF1
private

Number of tf1 living on CPU.

Definition at line 239 of file gpuSplineUtils.cuh.

◆ cpu_spline_size

short int SplineMonolithGPU::cpu_spline_size
private

Size of splines living on CPU.

Definition at line 235 of file gpuSplineUtils.cuh.

◆ cpu_tmp_weights

std::vector<float> SplineMonolithGPU::cpu_tmp_weights
private

Temporary weight needed for conversion from float to double.

Definition at line 246 of file gpuSplineUtils.cuh.

◆ gpu_coeff_many

float* SplineMonolithGPU::gpu_coeff_many
private

GPU arrays to hold other coefficients.

Definition at line 207 of file gpuSplineUtils.cuh.

◆ gpu_coeff_TF1_many

float* SplineMonolithGPU::gpu_coeff_TF1_many
private

GPU arrays to hold TF1 coefficients.

Definition at line 216 of file gpuSplineUtils.cuh.

◆ gpu_coeff_x

float* SplineMonolithGPU::gpu_coeff_x
private

KS: GPU arrays to hold X coefficient.

Definition at line 204 of file gpuSplineUtils.cuh.

◆ gpu_nKnots_arr

unsigned int* SplineMonolithGPU::gpu_nKnots_arr
private

KS: GPU Number of knots per spline.

Definition at line 210 of file gpuSplineUtils.cuh.

◆ gpu_nParamPerEvent

unsigned int* SplineMonolithGPU::gpu_nParamPerEvent
private

KS: GPU map keeping track how many parameters applies to each event, we keep two numbers here {number of splines per event, index where splines start for a given event}.

Definition at line 199 of file gpuSplineUtils.cuh.

◆ gpu_nParamPerEvent_TF1

unsigned int* SplineMonolithGPU::gpu_nParamPerEvent_TF1
private

KS: GPU map keeping track how many parameters applies to each event, we keep two numbers here {number of TF1 per event, index where TF1 start for a given event}.

Definition at line 201 of file gpuSplineUtils.cuh.

◆ gpu_nPoints_arr

short int* SplineMonolithGPU::gpu_nPoints_arr
private

GPU arrays to hold number of points.

Definition at line 218 of file gpuSplineUtils.cuh.

◆ gpu_par_val

float* SplineMonolithGPU::gpu_par_val
private

CW: parameter value on GPU.

Definition at line 223 of file gpuSplineUtils.cuh.

◆ gpu_paramNo_arr

short int* SplineMonolithGPU::gpu_paramNo_arr
private

CW: GPU array with the number of points per spline (not per spline point!)

Definition at line 213 of file gpuSplineUtils.cuh.

◆ gpu_paramNo_TF1_arr

short int* SplineMonolithGPU::gpu_paramNo_TF1_arr
private

CW: GPU array with the number of points per TF1 object.

Definition at line 220 of file gpuSplineUtils.cuh.

◆ gpu_spline_segment

short int* SplineMonolithGPU::gpu_spline_segment
private

CW: Spline segment on GPU.

Definition at line 225 of file gpuSplineUtils.cuh.

◆ gpu_total_weights

M3::float_t* SplineMonolithGPU::gpu_total_weights
private

GPU arrays to hold weight for event.

Definition at line 228 of file gpuSplineUtils.cuh.

◆ gpu_weights

float* SplineMonolithGPU::gpu_weights
private

GPU arrays to hold weight for each spline.

Definition at line 230 of file gpuSplineUtils.cuh.

◆ gpu_weights_tf1

float* SplineMonolithGPU::gpu_weights_tf1
private

GPU arrays to hold weight for each TF1.

Definition at line 232 of file gpuSplineUtils.cuh.

◆ text_coeff_x

cudaTextureObject_t SplineMonolithGPU::text_coeff_x = 0
private

KS: Textures are L1 cache variables which are well optimised for fetching. Make texture only for variables you often access but rarely overwrite. There are limits on texture memory so don't use huge arrays.

Definition at line 253 of file gpuSplineUtils.cuh.

◆ text_nParamPerEvent

cudaTextureObject_t SplineMonolithGPU::text_nParamPerEvent = 0
private

KS: Map keeping track how many parameters applies to each event, we keep two numbers here {number of splines per event, index where splines start for a given event}.

Definition at line 255 of file gpuSplineUtils.cuh.

◆ text_nParamPerEvent_TF1

cudaTextureObject_t SplineMonolithGPU::text_nParamPerEvent_TF1 = 0
private

KS: Map keeping track how many parameters applies to each event, we keep two numbers here {number of TF1 per event, index where TF1 start for a given event}.

Definition at line 257 of file gpuSplineUtils.cuh.


The documentation for this class was generated from the following files: