forked from dbarrow257/CUDAProb3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeamcudapropagator.cuh
358 lines (269 loc) · 12.5 KB
/
beamcudapropagator.cuh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
This file is part of CUDAProb3++.
CUDAProb3++ is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CUDAProb3++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with CUDAProb3++. If not, see <http://www.gnu.org/licenses/>.
*/
#include "constants.hpp"
#include "beamcpupropagator.hpp"
#include "physics.hpp"
//#ifdef __NVCC__ //change this to ifndef __NVCC__ before running doxygen. otherwise both classes are not included in the documentation
#ifdef GPU_ON
#ifndef CUDAPROB3_BEAMCUDAPROPAGATOR_CUH
#define CUDAPROB3_BEAMCUDAPROPAGATOR_CUH
#include "cuda_unique.cuh"
#include <algorithm>
#include <string>
#include <vector>
namespace cudaprob3{
/// \class BeamCudaPropagatorSingle
/// \brief Single-GPU beam neutrino propagation. Derived from Propagator
/// @param FLOAT_T The floating point type to use for calculations, i.e float, double
//template<class FLOAT_T>
class BeamCudaPropagatorSingle : public BeamCpuPropagator<double> {
template<typename>
friend class CudaPropagator;
public:
/// \brief Constructor
///
/// @param id device id of the GPU to use
/// @param n_cosines_ Number cosine bins
/// @param n_energies_ Number of energy bins
BeamCudaPropagatorSingle(int id, int n_energies_);
/// \brief Constructor which uses device id 0
///
/// @param n_energies Number of energy bins
BeamCudaPropagatorSingle(int n_energies); //: BeamCudaPropagatorSingle(0, n_energies);
/// \brief Destructor
~BeamCudaPropagatorSingle();
BeamCudaPropagatorSingle(const BeamCudaPropagatorSingle& other) = delete;
/// \brief Move constructor
/// @param other
BeamCudaPropagatorSingle(BeamCudaPropagatorSingle&& other); //: BeamCpuPropagator<double>(other);
BeamCudaPropagatorSingle& operator=(const BeamCudaPropagatorSingle& other) = delete;
/// \brief Move assignment operator
/// @param other
BeamCudaPropagatorSingle& operator=(BeamCudaPropagatorSingle&& other);
public:
void setDensity( double beam_density_) override;
void setPathLength( double beam_path_length_) override;
void setEnergyList(const std::vector<double>& list) override;
// calculate the probability of each cell
void calculateProbabilities(NeutrinoType type) override;
// get oscillation weight for specific cosine and energy
double getProbability(int index_energy, ProbType t) override;
// get oscillation weight for specific energy
void getProbabilityArr(double* probArr, ProbType t) override;
protected:
// launch the calculation kernel without waiting for its completion
void calculateBeamProbabilitiesAsync(NeutrinoType type);
// wait for calculateProbabilitiesAsync to finish
void waitForCompletion();
// copy results from device to host
void getResultFromDevice();
private:
unique_pinned_ptr<double> resultList;
// density
unique_dev_ptr<double> d_rhos;
// path length
unique_dev_ptr<double> d_path_lengths;
unique_dev_ptr<double> d_energy_list;
unique_dev_ptr<double> d_cosine_list;
shared_dev_ptr<double> d_result_list;
cudaStream_t stream;
int deviceId;
bool resultsResideOnHost = false;
};
// Mutli-GPU class commented out for now
/*
/// \class CudaPropagator
/// \brief Multi-GPU neutrino propagation. Derived from Propagator.
/// \details This is essentially a wrapper around multiple CudaPropagatorSingle instances, one per used GPU
/// Most of the setters and calculation functions simply call the appropriate function for each GPU
/// @param FLOAT_T The floating point type to use for calculations, i.e float, double
template<class FLOAT_T>
class CudaPropagator : public Propagator<FLOAT_T>{
public:
/// \brief Single GPU constructor for device id 0
///
/// @param nc Number cosine bins
/// @param ne Number of energy bins
CudaPropagator(int nc, int ne) : CudaPropagator(std::vector<int>{0}, nc, ne, true){}
/// \brief Constructor
///
/// @param ids List of device ids of the GPUs to use
/// @param nc Number cosine bins
/// @param ne Number of energy bins
/// @param failOnInvalidId If true, throw exception if ids contains an invalid device id
CudaPropagator(const std::vector<int>& ids, int nc, int ne, bool failOnInvalidId = true) : Propagator<FLOAT_T>(nc, ne) {
int nDevices;
cudaGetDeviceCount(&nDevices);
if(nDevices == 0) throw std::runtime_error("No GPU found");
for(const auto& id: ids){
if(id >= nDevices){
if(failOnInvalidId){
std::cout << "Available GPUs:" << std::endl;
for(int j = 0; j < nDevices; j++){
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, j);
std::cout << "Id " << j << " : " << prop.name << std::endl;
}
throw std::runtime_error("The requested GPU Id " + std::to_string(id) + " is not available.");
}else{
std::cout << "invalid device id found : " << id << std::endl;
}
}else{
deviceIds.push_back(id);
}
}
if(deviceIds.size() == 0){
throw std::runtime_error("No valid device id found.");
}
cosineIndices.resize(deviceIds.size());
localCosineIndices.resize(this->n_cosines);
for(int icos = 0; icos < this->n_cosines; icos++){
int deviceIndex = getCosineDeviceIndex(icos);
cosineIndices[deviceIndex].push_back(icos);
// the icos-th path is processed by GPU deviceIndex.
// In the subproblem processed by GPU deviceIndex, the icos-th path is the localCosineIndices[icos]-th path
localCosineIndices[icos] = cosineIndices[deviceIndex].size() - 1;
}
for(size_t i = 0; i < deviceIds.size() && i < size_t(this->n_cosines); i++){
propagatorVector.push_back(
std::unique_ptr<CudaPropagatorSingle<FLOAT_T>>(
new CudaPropagatorSingle<FLOAT_T>(deviceIds[i], cosineIndices[i].size(), this->n_energies)
)
);
}
}
CudaPropagator(const CudaPropagator& other) = delete;
/// \brief Move constructor
/// @param other
CudaPropagator(CudaPropagator&& other) : Propagator<FLOAT_T>(other){
*this = std::move(other);
}
CudaPropagator& operator=(const CudaPropagator& other) = delete;
/// \brief Move assignment operator
/// @param other
CudaPropagator& operator=(CudaPropagator&& other){
Propagator<FLOAT_T>::operator=(std::move(other));
deviceIds = std::move(other.deviceIds);
cosineIndices = std::move(other.cosineIndices);
localCosineIndices = std::move(other.localCosineIndices);
cosineBatches = std::move(other.cosineBatches);
propagatorVector = std::move(other.propagatorVector);
return *this;
}
public:
void setDensityFromFile(const std::string& filename) override{
Propagator<FLOAT_T>::setDensityFromFile(filename);
for(auto& propagator : propagatorVector)
propagator->setDensityFromFile(filename);
}
void setDensity(
const std::vector<FLOAT_T>& radii,
const std::vector<FLOAT_T>& rhos,
const std::vector<FLOAT_T>& yps) override{
Propagator<FLOAT_T>::setDensity(radii, rhos, yps);
for(auto& propagator : propagatorVector)
propagator->setDensity(radii, rhos, yps);
}
void setDensity(
const std::vector<FLOAT_T>& radii,
const std::vector<FLOAT_T>& a,
const std::vector<FLOAT_T>& b,
const std::vector<FLOAT_T>& c,
const std::vector<FLOAT_T>& yps) override{
Propagator<FLOAT_T>::setDensity(radii, a, b, c, yps);
for(auto& propagator : propagatorVector)
propagator->setDensity(radii, a, b, c, yps);
}
void setNeutrinoMasses(FLOAT_T dm12sq, FLOAT_T dm23sq) override{
Propagator<FLOAT_T>::setNeutrinoMasses(dm12sq, dm23sq);
for(auto& propagator : propagatorVector)
propagator->setNeutrinoMasses(dm12sq, dm23sq);
}
void setMNSMatrix(FLOAT_T theta12, FLOAT_T theta13, FLOAT_T theta23, FLOAT_T dCP) override{
Propagator<FLOAT_T>::setMNSMatrix(theta12, theta13, theta23, dCP);
for(auto& propagator : propagatorVector)
propagator->setMNSMatrix(theta12, theta13, theta23, dCP);
}
void setEnergyList(const std::vector<FLOAT_T>& list) override{
Propagator<FLOAT_T>::setEnergyList(list);
for(auto& propagator : propagatorVector)
propagator->setEnergyList(list);
}
void setCosineList(const std::vector<FLOAT_T>& list) override{
Propagator<FLOAT_T>::setCosineList(list);
for(size_t i = 0; i < propagatorVector.size(); i++){
// make list of cosines for GPU i and pass it to propagator i
std::vector<FLOAT_T> myCos(cosineIndices[i].size());
std::transform(cosineIndices[i].begin(),
cosineIndices[i].end(),
myCos.begin(),
[&](int icos){ return this->cosineList[icos]; }
);
propagatorVector[i]->setCosineList(myCos);
}
}
void setProductionHeight(FLOAT_T heightKM) override{
Propagator<FLOAT_T>::setProductionHeight(heightKM);
for(auto& propagator : propagatorVector)
propagator->setProductionHeight(heightKM);
}
public:
void calculateProbabilities(NeutrinoType type) override{
for(auto& propagator : propagatorVector)
propagator->calculateProbabilitiesAsync(type);
for(auto& propagator : propagatorVector)
propagator->waitForCompletion();
}
FLOAT_T getProbability(int index_cosine, int index_energy, ProbType t) override{
const int deviceIndex = getCosineDeviceIndex(index_cosine);
const int localCosineIndex = localCosineIndices[index_cosine];
return propagatorVector[deviceIndex]->getProbability(localCosineIndex, index_energy, t);
}
void getProbabilityArr(FLOAT_T* probArr, ProbType t) {
throw std::runtime_error("CudaPropagatorSingle::getProbabilityArr. Will not work!");
}
private:
void setMaxlayers() override{
Propagator<FLOAT_T>::setMaxlayers();
for(auto& propagator : propagatorVector)
propagator->setMaxlayers();
}
// get index in device id for the GPU which processes the index_cosine-th path
int getCosineDeviceIndex(int index_cosine){
#if 0
// block distribution
int id = 0;
for(int i = deviceIds.size(); i-- > 0;){
if(index_cosine < (i+1) * n_cosines / deviceIds.size())
id = i;
}
#else
// cyclic distribution.
const int id = index_cosine % deviceIds.size();
#endif
return id;
}
private:
std::vector<int> deviceIds;
std::vector<std::vector<int>> cosineIndices;
std::vector<int> localCosineIndices;
std::vector<int> cosineBatches;
// one CudaPropagatorSingle per GPU
std::vector<std::unique_ptr<CudaPropagatorSingle<FLOAT_T>>> propagatorVector;
};
*/
}
#endif
//#endif // #ifdef __NVCC__
#endif // #ifdef GPU_ON