-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathCAHitNtupletGeneratorKernels.cu
312 lines (270 loc) · 13.4 KB
/
CAHitNtupletGeneratorKernels.cu
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
#include "RecoPixelVertexing/PixelTriplets/plugins/CAHitNtupletGeneratorKernelsImpl.h"
template <>
void CAHitNtupletGeneratorKernelsGPU::fillHitDetIndices(HitsView const *hv, TkSoA *tracks_d, cudaStream_t cudaStream) {
auto blockSize = 128;
auto numberOfBlocks = (HitContainer::capacity() + blockSize - 1) / blockSize;
kernel_fillHitDetIndices<<<numberOfBlocks, blockSize, 0, cudaStream>>>(
&tracks_d->hitIndices, hv, &tracks_d->detIndices);
cudaCheck(cudaGetLastError());
#ifdef GPU_DEBUG
cudaDeviceSynchronize();
cudaCheck(cudaGetLastError());
#endif
}
template <>
void CAHitNtupletGeneratorKernelsGPU::launchKernels(HitsOnCPU const &hh, TkSoA *tracks_d, cudaStream_t cudaStream) {
// these are pointer on GPU!
auto *tuples_d = &tracks_d->hitIndices;
auto *quality_d = tracks_d->qualityData();
// zero tuples
cms::cuda::launchZero(tuples_d, cudaStream);
auto nhits = hh.nHits();
assert(nhits <= pixelGPUConstants::maxNumberOfHits);
// std::cout << "N hits " << nhits << std::endl;
// if (nhits<2) std::cout << "too few hits " << nhits << std::endl;
//
// applying conbinatoric cleaning such as fishbone at this stage is too expensive
//
auto nthTot = 64;
auto stride = 4;
auto blockSize = nthTot / stride;
auto numberOfBlocks = nDoubletBlocks(blockSize);
auto rescale = numberOfBlocks / 65536;
blockSize *= (rescale + 1);
numberOfBlocks = nDoubletBlocks(blockSize);
assert(numberOfBlocks < 65536);
assert(blockSize > 0 && 0 == blockSize % 16);
dim3 blks(1, numberOfBlocks, 1);
dim3 thrs(stride, blockSize, 1);
kernel_connect<<<blks, thrs, 0, cudaStream>>>(
device_hitTuple_apc_,
device_hitToTuple_apc_, // needed only to be reset, ready for next kernel
hh.view(),
device_theCells_.get(),
device_nCells_,
device_theCellNeighbors_.get(),
device_isOuterHitOfCell_.get(),
params_.hardCurvCut_,
params_.ptmin_,
params_.CAThetaCutBarrel_,
params_.CAThetaCutForward_,
params_.dcaCutInnerTriplet_,
params_.dcaCutOuterTriplet_);
cudaCheck(cudaGetLastError());
if (nhits > 1 && params_.earlyFishbone_) {
auto nthTot = 128;
auto stride = 16;
auto blockSize = nthTot / stride;
auto numberOfBlocks = (nhits + blockSize - 1) / blockSize;
dim3 blks(1, numberOfBlocks, 1);
dim3 thrs(stride, blockSize, 1);
gpuPixelDoublets::fishbone<<<blks, thrs, 0, cudaStream>>>(
hh.view(), device_theCells_.get(), device_nCells_, device_isOuterHitOfCell_.get(), nhits, false);
cudaCheck(cudaGetLastError());
}
blockSize = 64;
numberOfBlocks = (3 * params_.maxNumberOfDoublets_ / 4 + blockSize - 1) / blockSize;
kernel_find_ntuplets<<<numberOfBlocks, blockSize, 0, cudaStream>>>(hh.view(),
device_theCells_.get(),
device_nCells_,
device_theCellTracks_.get(),
tuples_d,
device_hitTuple_apc_,
quality_d,
params_.minHitsPerNtuplet_);
cudaCheck(cudaGetLastError());
if (params_.doStats_)
kernel_mark_used<<<numberOfBlocks, blockSize, 0, cudaStream>>>(hh.view(), device_theCells_.get(), device_nCells_);
cudaCheck(cudaGetLastError());
#ifdef GPU_DEBUG
cudaDeviceSynchronize();
cudaCheck(cudaGetLastError());
#endif
blockSize = 128;
numberOfBlocks = (HitContainer::totbins() + blockSize - 1) / blockSize;
cms::cuda::finalizeBulk<<<numberOfBlocks, blockSize, 0, cudaStream>>>(device_hitTuple_apc_, tuples_d);
// remove duplicates (tracks that share a doublet)
numberOfBlocks = nDoubletBlocks(blockSize);
kernel_earlyDuplicateRemover<<<numberOfBlocks, blockSize, 0, cudaStream>>>(
device_theCells_.get(), device_nCells_, tuples_d, quality_d);
cudaCheck(cudaGetLastError());
blockSize = 128;
numberOfBlocks = (3 * caConstants::maxTuples / 4 + blockSize - 1) / blockSize;
kernel_countMultiplicity<<<numberOfBlocks, blockSize, 0, cudaStream>>>(
tuples_d, quality_d, device_tupleMultiplicity_.get());
cms::cuda::launchFinalize(device_tupleMultiplicity_.get(), cudaStream);
kernel_fillMultiplicity<<<numberOfBlocks, blockSize, 0, cudaStream>>>(
tuples_d, quality_d, device_tupleMultiplicity_.get());
cudaCheck(cudaGetLastError());
if (nhits > 1 && params_.lateFishbone_) {
auto nthTot = 128;
auto stride = 16;
auto blockSize = nthTot / stride;
auto numberOfBlocks = (nhits + blockSize - 1) / blockSize;
dim3 blks(1, numberOfBlocks, 1);
dim3 thrs(stride, blockSize, 1);
gpuPixelDoublets::fishbone<<<blks, thrs, 0, cudaStream>>>(
hh.view(), device_theCells_.get(), device_nCells_, device_isOuterHitOfCell_.get(), nhits, true);
cudaCheck(cudaGetLastError());
}
#ifdef GPU_DEBUG
cudaDeviceSynchronize();
cudaCheck(cudaGetLastError());
#endif
// free space asap
// device_isOuterHitOfCell_.reset();
}
template <>
void CAHitNtupletGeneratorKernelsGPU::buildDoublets(HitsOnCPU const &hh, cudaStream_t stream) {
auto nhits = hh.nHits();
#ifdef NTUPLE_DEBUG
std::cout << "building Doublets out of " << nhits << " Hits" << std::endl;
#endif
#ifdef GPU_DEBUG
cudaDeviceSynchronize();
cudaCheck(cudaGetLastError());
#endif
// in principle we can use "nhits" to heuristically dimension the workspace...
device_isOuterHitOfCell_ = cms::cuda::make_device_unique<GPUCACell::OuterHitOfCell[]>(std::max(1U, nhits), stream);
assert(device_isOuterHitOfCell_.get());
cellStorage_ = cms::cuda::make_device_unique<unsigned char[]>(
caConstants::maxNumOfActiveDoublets * sizeof(GPUCACell::CellNeighbors) +
caConstants::maxNumOfActiveDoublets * sizeof(GPUCACell::CellTracks),
stream);
device_theCellNeighborsContainer_ = (GPUCACell::CellNeighbors *)cellStorage_.get();
device_theCellTracksContainer_ = (GPUCACell::CellTracks *)(cellStorage_.get() + caConstants::maxNumOfActiveDoublets *
sizeof(GPUCACell::CellNeighbors));
{
int threadsPerBlock = 128;
// at least one block!
int blocks = (std::max(1U, nhits) + threadsPerBlock - 1) / threadsPerBlock;
gpuPixelDoublets::initDoublets<<<blocks, threadsPerBlock, 0, stream>>>(device_isOuterHitOfCell_.get(),
nhits,
device_theCellNeighbors_.get(),
device_theCellNeighborsContainer_,
device_theCellTracks_.get(),
device_theCellTracksContainer_);
cudaCheck(cudaGetLastError());
}
device_theCells_ = cms::cuda::make_device_unique<GPUCACell[]>(params_.maxNumberOfDoublets_, stream);
#ifdef GPU_DEBUG
cudaDeviceSynchronize();
cudaCheck(cudaGetLastError());
#endif
if (0 == nhits)
return; // protect against empty events
// take all layer pairs into account
auto nActualPairs = gpuPixelDoublets::nPairs;
if (not params_.includeJumpingForwardDoublets_) {
// exclude forward "jumping" layer pairs
nActualPairs = gpuPixelDoublets::nPairsForTriplets;
}
if (params_.minHitsPerNtuplet_ > 3) {
// for quadruplets, exclude all "jumping" layer pairs
nActualPairs = gpuPixelDoublets::nPairsForQuadruplets;
}
assert(nActualPairs <= gpuPixelDoublets::nPairs);
int stride = 4;
int threadsPerBlock = gpuPixelDoublets::getDoubletsFromHistoMaxBlockSize / stride;
int blocks = (4 * nhits + threadsPerBlock - 1) / threadsPerBlock;
dim3 blks(1, blocks, 1);
dim3 thrs(stride, threadsPerBlock, 1);
gpuPixelDoublets::getDoubletsFromHisto<<<blks, thrs, 0, stream>>>(device_theCells_.get(),
device_nCells_,
device_theCellNeighbors_.get(),
device_theCellTracks_.get(),
hh.view(),
device_isOuterHitOfCell_.get(),
nActualPairs,
params_.idealConditions_,
params_.doClusterCut_,
params_.doZ0Cut_,
params_.doPtCut_,
params_.maxNumberOfDoublets_);
cudaCheck(cudaGetLastError());
#ifdef GPU_DEBUG
cudaDeviceSynchronize();
cudaCheck(cudaGetLastError());
#endif
}
template <>
void CAHitNtupletGeneratorKernelsGPU::classifyTuples(HitsOnCPU const &hh, TkSoA *tracks_d, cudaStream_t cudaStream) {
// these are pointer on GPU!
auto const *tuples_d = &tracks_d->hitIndices;
auto *quality_d = tracks_d->qualityData();
auto blockSize = 64;
// classify tracks based on kinematics
auto numberOfBlocks = nQuadrupletBlocks(blockSize);
kernel_classifyTracks<<<numberOfBlocks, blockSize, 0, cudaStream>>>(tuples_d, tracks_d, params_.cuts_, quality_d);
cudaCheck(cudaGetLastError());
if (params_.lateFishbone_) {
// apply fishbone cleaning to good tracks
numberOfBlocks = nDoubletBlocks(blockSize);
kernel_fishboneCleaner<<<numberOfBlocks, blockSize, 0, cudaStream>>>(
device_theCells_.get(), device_nCells_, quality_d);
cudaCheck(cudaGetLastError());
}
// remove duplicates (tracks that share a doublet)
numberOfBlocks = nDoubletBlocks(blockSize);
kernel_fastDuplicateRemover<<<numberOfBlocks, blockSize, 0, cudaStream>>>(
device_theCells_.get(), device_nCells_, tuples_d, tracks_d);
cudaCheck(cudaGetLastError());
if (params_.minHitsPerNtuplet_ < 4 || params_.doStats_) {
// fill hit->track "map"
numberOfBlocks = nQuadrupletBlocks(blockSize);
kernel_countHitInTracks<<<numberOfBlocks, blockSize, 0, cudaStream>>>(
tuples_d, quality_d, device_hitToTuple_.get());
cudaCheck(cudaGetLastError());
cms::cuda::launchFinalize(device_hitToTuple_.get(), cudaStream);
cudaCheck(cudaGetLastError());
kernel_fillHitInTracks<<<numberOfBlocks, blockSize, 0, cudaStream>>>(tuples_d, quality_d, device_hitToTuple_.get());
cudaCheck(cudaGetLastError());
}
if (params_.doSharedHitCut_) {
// remove duplicates (tracks that share a hit)
numberOfBlocks = (HitToTuple::capacity() + blockSize - 1) / blockSize;
kernel_sharedHitCleaner<<<numberOfBlocks, blockSize, 0, cudaStream>>>(
hh.view(), tuples_d, tracks_d, quality_d, params_.minHitsForSharingCut_, device_hitToTuple_.get());
cudaCheck(cudaGetLastError());
}
if (params_.doStats_) {
auto nhits = hh.nHits();
numberOfBlocks = (std::max(nhits, params_.maxNumberOfDoublets_) + blockSize - 1) / blockSize;
kernel_checkOverflows<<<numberOfBlocks, blockSize, 0, cudaStream>>>(tuples_d,
device_tupleMultiplicity_.get(),
device_hitToTuple_.get(),
device_hitTuple_apc_,
device_theCells_.get(),
device_nCells_,
device_theCellNeighbors_.get(),
device_theCellTracks_.get(),
device_isOuterHitOfCell_.get(),
nhits,
params_.maxNumberOfDoublets_,
counters_);
cudaCheck(cudaGetLastError());
}
if (params_.doStats_) {
// counters (add flag???)
numberOfBlocks = (HitToTuple::capacity() + blockSize - 1) / blockSize;
kernel_doStatsForHitInTracks<<<numberOfBlocks, blockSize, 0, cudaStream>>>(device_hitToTuple_.get(), counters_);
cudaCheck(cudaGetLastError());
numberOfBlocks = (3 * caConstants::maxNumberOfQuadruplets / 4 + blockSize - 1) / blockSize;
kernel_doStatsForTracks<<<numberOfBlocks, blockSize, 0, cudaStream>>>(tuples_d, quality_d, counters_);
cudaCheck(cudaGetLastError());
}
#ifdef GPU_DEBUG
cudaDeviceSynchronize();
cudaCheck(cudaGetLastError());
#endif
#ifdef DUMP_GPU_TK_TUPLES
static std::atomic<int> iev(0);
++iev;
kernel_print_found_ntuplets<<<1, 32, 0, cudaStream>>>(
hh.view(), tuples_d, tracks_d, quality_d, device_hitToTuple_.get(), 100, iev);
#endif
}
template <>
void CAHitNtupletGeneratorKernelsGPU::printCounters(Counters const *counters) {
kernel_printCounters<<<1, 1>>>(counters);
}