-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathgpuClustering.h
306 lines (275 loc) · 9.7 KB
/
gpuClustering.h
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
#ifndef RecoLocalTracker_SiPixelClusterizer_plugins_gpuClustering_h
#define RecoLocalTracker_SiPixelClusterizer_plugins_gpuClustering_h
#include <cstdint>
#include <cstdio>
#include "Geometry/phase1PixelTopology.h"
#include "CUDACore/HistoContainer.h"
#include "CUDACore/cuda_assert.h"
#include "gpuClusteringConstants.h"
namespace gpuClustering {
#ifdef GPU_DEBUG
__device__ uint32_t gMaxHit = 0;
#endif
__global__ void countModules(uint16_t const* __restrict__ id,
uint32_t* __restrict__ moduleStart,
int32_t* __restrict__ clusterId,
int numElements) {
int first = blockDim.x * blockIdx.x + threadIdx.x;
for (int i = first; i < numElements; i += gridDim.x * blockDim.x) {
clusterId[i] = i;
if (InvId == id[i])
continue;
auto j = i - 1;
while (j >= 0 and id[j] == InvId)
--j;
if (j < 0 or id[j] != id[i]) {
// boundary...
auto loc = atomicInc(moduleStart, MaxNumModules);
moduleStart[loc + 1] = i;
}
}
}
__global__
// __launch_bounds__(256,4)
void
findClus(uint16_t const* __restrict__ id, // module id of each pixel
uint16_t const* __restrict__ x, // local coordinates of each pixel
uint16_t const* __restrict__ y, //
uint32_t const* __restrict__ moduleStart, // index of the first pixel of each module
uint32_t* __restrict__ nClustersInModule, // output: number of clusters found in each module
uint32_t* __restrict__ moduleId, // output: module id of each module
int32_t* __restrict__ clusterId, // output: cluster id of each pixel
int numElements) {
if (blockIdx.x >= moduleStart[0])
return;
auto firstPixel = moduleStart[1 + blockIdx.x];
auto thisModuleId = id[firstPixel];
assert(thisModuleId < MaxNumModules);
#ifdef GPU_DEBUG
if (thisModuleId % 100 == 1)
if (threadIdx.x == 0)
printf("start clusterizer for module %d in block %d\n", thisModuleId, blockIdx.x);
#endif
auto first = firstPixel + threadIdx.x;
// find the index of the first pixel not belonging to this module (or invalid)
__shared__ int msize;
msize = numElements;
__syncthreads();
// skip threads not associated to an existing pixel
for (int i = first; i < numElements; i += blockDim.x) {
if (id[i] == InvId) // skip invalid pixels
continue;
if (id[i] != thisModuleId) { // find the first pixel in a different module
atomicMin(&msize, i);
break;
}
}
//init hist (ymax=416 < 512 : 9bits)
constexpr uint32_t maxPixInModule = 4000;
constexpr auto nbins = phase1PixelTopology::numColsInModule + 2; //2+2;
using Hist = cms::cuda::HistoContainer<uint16_t, nbins, maxPixInModule, 9, uint16_t>;
__shared__ Hist hist;
__shared__ typename Hist::Counter ws[32];
for (auto j = threadIdx.x; j < Hist::totbins(); j += blockDim.x) {
hist.off[j] = 0;
}
__syncthreads();
assert((msize == numElements) or ((msize < numElements) and (id[msize] != thisModuleId)));
// limit to maxPixInModule (FIXME if recurrent (and not limited to simulation with low threshold) one will need to implement something cleverer)
if (0 == threadIdx.x) {
if (msize - firstPixel > maxPixInModule) {
printf("too many pixels in module %d: %d > %d\n", thisModuleId, msize - firstPixel, maxPixInModule);
msize = maxPixInModule + firstPixel;
}
}
__syncthreads();
assert(msize - firstPixel <= maxPixInModule);
#ifdef GPU_DEBUG
__shared__ uint32_t totGood;
totGood = 0;
__syncthreads();
#endif
// fill histo
for (int i = first; i < msize; i += blockDim.x) {
if (id[i] == InvId) // skip invalid pixels
continue;
hist.count(y[i]);
#ifdef GPU_DEBUG
atomicAdd(&totGood, 1);
#endif
}
__syncthreads();
if (threadIdx.x < 32)
ws[threadIdx.x] = 0; // used by prefix scan...
__syncthreads();
hist.finalize(ws);
__syncthreads();
#ifdef GPU_DEBUG
assert(hist.size() == totGood);
if (thisModuleId % 100 == 1)
if (threadIdx.x == 0)
printf("histo size %d\n", hist.size());
#endif
for (int i = first; i < msize; i += blockDim.x) {
if (id[i] == InvId) // skip invalid pixels
continue;
hist.fill(y[i], i - firstPixel);
}
#ifdef __CUDA_ARCH__
// assume that we can cover the whole module with up to 16 blockDim.x-wide iterations
constexpr int maxiter = 16;
#else
auto maxiter = hist.size();
#endif
// allocate space for duplicate pixels: a pixel can appear more than once with different charge in the same event
constexpr int maxNeighbours = 10;
assert((hist.size() / blockDim.x) <= maxiter);
// nearest neighbour
uint16_t nn[maxiter][maxNeighbours];
uint8_t nnn[maxiter]; // number of nn
for (uint32_t k = 0; k < maxiter; ++k)
nnn[k] = 0;
__syncthreads(); // for hit filling!
#ifdef GPU_DEBUG
// look for anomalous high occupancy
__shared__ uint32_t n40, n60;
n40 = n60 = 0;
__syncthreads();
for (auto j = threadIdx.x; j < Hist::nbins(); j += blockDim.x) {
if (hist.size(j) > 60)
atomicAdd(&n60, 1);
if (hist.size(j) > 40)
atomicAdd(&n40, 1);
}
__syncthreads();
if (0 == threadIdx.x) {
if (n60 > 0)
printf("columns with more than 60 px %d in %d\n", n60, thisModuleId);
else if (n40 > 0)
printf("columns with more than 40 px %d in %d\n", n40, thisModuleId);
}
__syncthreads();
#endif
// fill NN
for (auto j = threadIdx.x, k = 0U; j < hist.size(); j += blockDim.x, ++k) {
assert(k < maxiter);
auto p = hist.begin() + j;
auto i = *p + firstPixel;
assert(id[i] != InvId);
assert(id[i] == thisModuleId); // same module
int be = Hist::bin(y[i] + 1);
auto e = hist.end(be);
++p;
assert(0 == nnn[k]);
for (; p < e; ++p) {
auto m = (*p) + firstPixel;
assert(m != i);
assert(int(y[m]) - int(y[i]) >= 0);
assert(int(y[m]) - int(y[i]) <= 1);
if (std::abs(int(x[m]) - int(x[i])) > 1)
continue;
auto l = nnn[k]++;
assert(l < maxNeighbours);
nn[k][l] = *p;
}
}
// for each pixel, look at all the pixels until the end of the module;
// when two valid pixels within +/- 1 in x or y are found, set their id to the minimum;
// after the loop, all the pixel in each cluster should have the id equeal to the lowest
// pixel in the cluster ( clus[i] == i ).
bool more = true;
int nloops = 0;
while (__syncthreads_or(more)) {
if (1 == nloops % 2) {
for (auto j = threadIdx.x, k = 0U; j < hist.size(); j += blockDim.x, ++k) {
auto p = hist.begin() + j;
auto i = *p + firstPixel;
auto m = clusterId[i];
while (m != clusterId[m])
m = clusterId[m];
clusterId[i] = m;
}
} else {
more = false;
for (auto j = threadIdx.x, k = 0U; j < hist.size(); j += blockDim.x, ++k) {
auto p = hist.begin() + j;
auto i = *p + firstPixel;
for (int kk = 0; kk < nnn[k]; ++kk) {
auto l = nn[k][kk];
auto m = l + firstPixel;
assert(m != i);
auto old = atomicMin(&clusterId[m], clusterId[i]);
if (old != clusterId[i]) {
// end the loop only if no changes were applied
more = true;
}
atomicMin(&clusterId[i], old);
} // nnloop
} // pixel loop
}
++nloops;
} // end while
#ifdef GPU_DEBUG
{
__shared__ int n0;
if (threadIdx.x == 0)
n0 = nloops;
__syncthreads();
auto ok = n0 == nloops;
assert(__syncthreads_and(ok));
if (thisModuleId % 100 == 1)
if (threadIdx.x == 0)
printf("# loops %d\n", nloops);
}
#endif
__shared__ unsigned int foundClusters;
foundClusters = 0;
__syncthreads();
// find the number of different clusters, identified by a pixels with clus[i] == i;
// mark these pixels with a negative id.
for (int i = first; i < msize; i += blockDim.x) {
if (id[i] == InvId) // skip invalid pixels
continue;
if (clusterId[i] == i) {
auto old = atomicInc(&foundClusters, 0xffffffff);
clusterId[i] = -(old + 1);
}
}
__syncthreads();
// propagate the negative id to all the pixels in the cluster.
for (int i = first; i < msize; i += blockDim.x) {
if (id[i] == InvId) // skip invalid pixels
continue;
if (clusterId[i] >= 0) {
// mark each pixel in a cluster with the same id as the first one
clusterId[i] = clusterId[clusterId[i]];
}
}
__syncthreads();
// adjust the cluster id to be a positive value starting from 0
for (int i = first; i < msize; i += blockDim.x) {
if (id[i] == InvId) { // skip invalid pixels
clusterId[i] = -9999;
continue;
}
clusterId[i] = -clusterId[i] - 1;
}
__syncthreads();
if (threadIdx.x == 0) {
nClustersInModule[thisModuleId] = foundClusters;
moduleId[blockIdx.x] = thisModuleId;
#ifdef GPU_DEBUG
if (foundClusters > gMaxHit) {
gMaxHit = foundClusters;
if (foundClusters > 8)
printf("max hit %d in %d\n", foundClusters, thisModuleId);
}
#endif
#ifdef GPU_DEBUG
if (thisModuleId % 100 == 1)
printf("%d clusters in module %d\n", foundClusters, thisModuleId);
#endif
}
}
} // namespace gpuClustering
#endif // RecoLocalTracker_SiPixelClusterizer_plugins_gpuClustering_h