forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUTPCNNClusterizerKernels.cxx
More file actions
513 lines (468 loc) · 26.5 KB
/
GPUTPCNNClusterizerKernels.cxx
File metadata and controls
513 lines (468 loc) · 26.5 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file GPUTPCNNClusterizerKernels.cxx
/// \author Christian Sonnabend
#include "GPUTPCNNClusterizerKernels.h"
#include "GPUTPCCFClusterizer.h"
#include "GPUTPCGeometry.h"
using namespace o2::gpu;
using namespace o2::gpu::tpccf;
#include "CfConsts.h"
#include "CfUtils.h"
#include "ClusterAccumulator.h"
#include "ML/3rdparty/GPUORTFloat16.h"
#if !defined(GPUCA_GPUCODE)
#include "GPUHostDataTypes.h"
#include "MCLabelAccumulator.h"
#endif
#ifdef GPUCA_GPUCODE
#include "GPUTPCCFClusterizer.inc"
#endif
// Defining individual thread functions for data filling, determining the class label and running the CF clusterizer
template <>
GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::runCfClusterizer>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& processors, uint8_t sector, int8_t dtype, int8_t withMC, uint32_t batchStart)
{
uint32_t glo_idx = get_global_id(0);
auto& clusterer = processors.tpcClusterer[sector];
auto& clustererNN = processors.tpcNNClusterer[sector];
if (clustererNN.mOutputDataClass[glo_idx] == 0) { // default clusterizer should not be called in batched mode due to mess-up with thread indices
return;
}
CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
CPU_ONLY(MCLabelAccumulator labelAcc(clusterer));
tpc::ClusterNative* clusterOut = (withMC) ? nullptr : clusterer.mPclusterByRow;
o2::gpu::GPUTPCCFClusterizer::GPUSharedMemory smem_new;
GPUTPCCFClusterizer::computeClustersImpl(get_num_groups(0), get_local_size(0), get_group_id(0), get_local_id(0), clusterer, clusterer.mPmemory->fragment, smem_new, chargeMap, clusterer.mPfilteredPeakPositions, clusterer.Param().rec, CPU_PTR(&labelAcc), clusterer.mPmemory->counters.nClusters, clusterer.mNMaxClusterPerRow, clusterer.mPclusterInRow, clusterOut, clusterer.mPclusterPosInRow);
}
template <>
GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::fillInputNN>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& processors, uint8_t sector, int8_t dtype, int8_t withMC, uint32_t batchStart)
{
uint32_t glo_idx = get_global_id(0);
auto& clusterer = processors.tpcClusterer[sector];
auto& clustererNN = processors.tpcNNClusterer[sector];
uint32_t write_idx = glo_idx * clustererNN.mNnClusterizerElementSize; // Potential optimization: Either choose mNnClusterizerBatchedMode as a power of 2 or calculate from threadId and blockId
CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
CfArray2D<uint8_t> isPeakMap(clusterer.mPpeakMap);
CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(glo_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
int32_t row = static_cast<int>(peak.row()), pad = static_cast<int>(peak.pad()), time = static_cast<int>(peak.time()); // Explicit casting to avoid conversion errors
float central_charge = static_cast<float>(chargeMap[peak].unpack());
int32_t row_offset = GPUTPCNNClusterizerKernels::rowOffset(row, clustererNN.mNnClusterizerSizeInputRow);
#ifndef GPUCA_GPUCODE
GPUCA_UNROLL(U(), U());
#endif
for (int32_t r = -clustererNN.mNnClusterizerSizeInputRow; r <= clustererNN.mNnClusterizerSizeInputRow; r++) {
bool is_row_boundary = ((row + r) > (o2::tpc::constants::MAXGLOBALPADROW - 1)) || ((row + r) < 0);
int32_t pad_offset = is_row_boundary ? 0 : GPUTPCNNClusterizerKernels::padOffset(row, row + r);
for (int32_t p = -clustererNN.mNnClusterizerSizeInputPad + pad_offset; p <= clustererNN.mNnClusterizerSizeInputPad + pad_offset; p++) {
bool is_boundary = is_row_boundary || GPUTPCNNClusterizerKernels::isBoundary(row + r + row_offset, pad + p, clustererNN.mNnClusterizerSizeInputRow);
for (int32_t t = -clustererNN.mNnClusterizerSizeInputTime; t <= clustererNN.mNnClusterizerSizeInputTime; t++) {
if (!is_boundary) {
CfChargePos tmp_pos(row + r, pad + p, time + t);
if (r == 0 && !clustererNN.mClusterFlags[2 * glo_idx] && CAMath::Abs(p) < 3 && CAMath::Abs(t) < 3 && p != 0 && t != 0) { // ordering is done for short circuit optimization
clustererNN.mClusterFlags[2 * glo_idx] += CfUtils::isPeak(isPeakMap[tmp_pos]);
clustererNN.mClusterFlags[2 * glo_idx + 1] = clustererNN.mClusterFlags[2 * glo_idx];
}
if (dtype == 0) {
clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)(static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge);
} else if (dtype == 1) {
clustererNN.mInputData_32[write_idx] = static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge;
}
} else {
// Filling boundary just to make sure that no values are left unintentionally
if (dtype == 0) {
clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)(static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue));
} else {
clustererNN.mInputData_32[write_idx] = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
}
}
write_idx++;
}
}
}
if (clustererNN.mNnClusterizerAddIndexData) {
if (dtype == 0) {
clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)(sector / 36.f);
clustererNN.mInputData_16[write_idx + 1] = (OrtDataType::Float16_t)(row / 152.f);
clustererNN.mInputData_16[write_idx + 2] = (OrtDataType::Float16_t)(static_cast<float>(pad) / GPUTPCGeometry::NPads(row));
} else {
clustererNN.mInputData_32[write_idx] = sector / 36.f;
clustererNN.mInputData_32[write_idx + 1] = row / 152.f;
clustererNN.mInputData_32[write_idx + 2] = static_cast<float>(pad) / GPUTPCGeometry::NPads(row);
}
}
}
template <>
GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::fillInputNNSingleElement>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& processors, uint8_t sector, int8_t dtype, int8_t withMC, uint32_t batchStart)
{
uint32_t glo_idx = get_global_id(0);
auto& clusterer = processors.tpcClusterer[sector];
auto& clustererNN = processors.tpcNNClusterer[sector];
uint32_t base_idx = CAMath::Floor(glo_idx / clustererNN.mNnClusterizerElementSize);
uint32_t transient_index = glo_idx - (base_idx * clustererNN.mNnClusterizerElementSize);
CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
CfArray2D<uint8_t> isPeakMap(clusterer.mPpeakMap);
CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(base_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
int32_t row = static_cast<int>(peak.row()), pad = static_cast<int>(peak.pad());
if (clustererNN.mNnClusterizerAddIndexData && (int32_t)transient_index == (clustererNN.mNnClusterizerElementSize - 1)) {
uint32_t top_idx = (base_idx + 1) * clustererNN.mNnClusterizerElementSize;
if (!clustererNN.mNnClusterizerSetDeconvolutionFlags) { // Only if deconvolution flags are not set
for (uint16_t i = 0; i < 8; i++) { // This solution needs testing. It is not the same as the deconvolution flags
Delta2 d = cfconsts::InnerNeighbors[i];
CfChargePos tmp_pos = peak.delta(d);
clustererNN.mClusterFlags[2 * base_idx] += CfUtils::isPeak(isPeakMap[tmp_pos]);
}
clustererNN.mClusterFlags[2 * base_idx + 1] = clustererNN.mClusterFlags[2 * base_idx];
}
if (dtype == 0) {
clustererNN.mInputData_16[top_idx - 3] = (OrtDataType::Float16_t)(sector / 36.f);
clustererNN.mInputData_16[top_idx - 2] = (OrtDataType::Float16_t)(row / 152.f);
clustererNN.mInputData_16[top_idx - 1] = (OrtDataType::Float16_t)(static_cast<float>(pad) / GPUTPCGeometry::NPads(row));
} else {
clustererNN.mInputData_32[top_idx - 3] = sector / 36.f;
clustererNN.mInputData_32[top_idx - 2] = row / 152.f;
clustererNN.mInputData_32[top_idx - 1] = static_cast<float>(pad) / GPUTPCGeometry::NPads(row);
}
} else if ((int32_t)transient_index < (clustererNN.mNnClusterizerElementSize - 3)) {
int32_t time = static_cast<int>(peak.time());
int32_t r = CAMath::Floor(transient_index / ((2 * clustererNN.mNnClusterizerSizeInputPad + 1) * (2 * clustererNN.mNnClusterizerSizeInputTime + 1))) - clustererNN.mNnClusterizerSizeInputRow;
bool is_row_boundary = ((row + r) > (o2::tpc::constants::MAXGLOBALPADROW - 1)) || ((row + r) < 0);
if (is_row_boundary) {
if (dtype == 0) {
clustererNN.mInputData_16[glo_idx] = (OrtDataType::Float16_t)(static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue));
} else {
clustererNN.mInputData_32[glo_idx] = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
}
} else {
int32_t row_offset = GPUTPCNNClusterizerKernels::rowOffset(row, clustererNN.mNnClusterizerSizeInputRow);
int32_t pad_offset = GPUTPCNNClusterizerKernels::padOffset(row, row + r);
int32_t rest_1 = transient_index % ((2 * clustererNN.mNnClusterizerSizeInputPad + 1) * (2 * clustererNN.mNnClusterizerSizeInputTime + 1));
int32_t p = CAMath::Floor(rest_1 / (2 * clustererNN.mNnClusterizerSizeInputTime + 1)) - clustererNN.mNnClusterizerSizeInputPad + pad_offset;
int32_t time_pos = (rest_1 % (2 * clustererNN.mNnClusterizerSizeInputTime + 1)) - clustererNN.mNnClusterizerSizeInputTime + time;
bool is_boundary = GPUTPCNNClusterizerKernels::isBoundary(row + r + row_offset, pad + p, clustererNN.mNnClusterizerSizeInputRow) && (time_pos < 0 || time_pos >= TPC_MAX_FRAGMENT_LEN_GPU);
if (!is_boundary) {
float central_charge = static_cast<float>(chargeMap[peak].unpack());
CfChargePos tmp_pos(row + r, pad + p, time_pos);
if (dtype == 0) {
clustererNN.mInputData_16[glo_idx] = (OrtDataType::Float16_t)(static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge);
} else if (dtype == 1) {
clustererNN.mInputData_32[glo_idx] = static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge;
}
} else {
if (dtype == 0) {
clustererNN.mInputData_16[glo_idx] = (OrtDataType::Float16_t)(static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue));
} else {
clustererNN.mInputData_32[glo_idx] = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
}
}
}
}
}
template <>
GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::determineClass1Labels>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& processors, uint8_t sector, int8_t dtype, int8_t withMC, uint32_t batchStart)
{
uint32_t glo_idx = get_global_id(0);
if (dtype == 0) {
processors.tpcNNClusterer[sector].mOutputDataClass[glo_idx + batchStart] = (int)((processors.tpcNNClusterer[sector].mModelProbabilities_16[glo_idx]).ToFloat() > processors.tpcNNClusterer[sector].mNnClassThreshold);
} else if (dtype == 1) {
processors.tpcNNClusterer[sector].mOutputDataClass[glo_idx + batchStart] = (int)(processors.tpcNNClusterer[sector].mModelProbabilities_32[glo_idx] > processors.tpcNNClusterer[sector].mNnClassThreshold);
}
}
template <>
GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::determineClass2Labels>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& processors, uint8_t sector, int8_t dtype, int8_t withMC, uint32_t batchStart)
{
auto& clustererNN = processors.tpcNNClusterer[sector];
uint32_t glo_idx = get_global_id(0);
uint32_t elem_iterator = glo_idx * clustererNN.mNnClusterizerModelClassNumOutputNodes;
float current_max_prob = 0.f; // If the neural network doesn't contain the softmax as a last layer, the outputs can range in [-infty, infty]
uint32_t class_label = 0;
for (uint32_t pIdx = elem_iterator; pIdx < elem_iterator + clustererNN.mNnClusterizerModelClassNumOutputNodes; pIdx++) {
if (pIdx == elem_iterator) {
if (dtype == 0) {
current_max_prob = static_cast<float>(clustererNN.mModelProbabilities_16[pIdx]);
} else if (dtype == 1) {
current_max_prob = clustererNN.mModelProbabilities_32[pIdx];
}
} else {
if (dtype == 0) {
current_max_prob = CAMath::Max(current_max_prob, clustererNN.mModelProbabilities_16[pIdx].ToFloat());
} else if (dtype == 1) {
current_max_prob = CAMath::Max(current_max_prob, clustererNN.mModelProbabilities_32[pIdx]);
}
}
}
// uint32_t class_label = std::distance(elem_iterator, std::max_element(elem_iterator, elem_iterator + clustererNN.mNnClusterizerModelClassNumOutputNodes)); // Multiple outputs of the class network are the probabilities for each class. The highest one "wins"
clustererNN.mOutputDataClass[glo_idx + batchStart] = class_label;
if (class_label > 1) {
clustererNN.mClusterFlags[2 * glo_idx] = 1;
clustererNN.mClusterFlags[2 * glo_idx + 1] = 1;
}
}
template <>
GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::publishClass1Regression>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& processors, uint8_t sector, int8_t dtype, int8_t withMC, uint32_t batchStart)
{
uint32_t glo_idx = get_global_id(0);
auto& clusterer = processors.tpcClusterer[sector];
auto& clustererNN = processors.tpcNNClusterer[sector];
uint32_t maxClusterNum = clusterer.mPmemory->counters.nClusters;
uint32_t full_glo_idx = glo_idx + batchStart;
if (full_glo_idx >= maxClusterNum) {
return;
}
int32_t model_output_index = glo_idx * clustererNN.mNnClusterizerModelReg1NumOutputNodes;
CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(full_glo_idx, maxClusterNum - 1)];
float central_charge = static_cast<float>(chargeMap[peak].unpack());
CPU_ONLY(MCLabelAccumulator labelAccElem(clusterer));
MCLabelAccumulator* labelAcc = CPU_PTR(&labelAccElem);
tpc::ClusterNative* clusterOut = (withMC) ? nullptr : clusterer.mPclusterByRow;
// LOG(info) << glo_idx << " -- " << model_output_index << " / " << clustererNN.outputDataReg1.size() << " / " << clustererNN.mNnClusterizerModelReg1NumOutputNodes << " -- " << clusterer.peakPositions.size() << " -- " << clusterer.centralCharges.size();
if (clustererNN.mOutputDataClass[full_glo_idx] == 1 || (clustererNN.mNnClusterizerModelReg2NumOutputNodes != -1 && clustererNN.mOutputDataClass[full_glo_idx] >= 1)) {
ClusterAccumulator pc;
// Publishing logic is taken from default clusterizer
if (withMC) {
ClusterAccumulator dummy_pc;
CPU_ONLY(labelAcc->collect(peak, central_charge));
GPUTPCCFClusterizer::buildCluster(
clusterer.Param().rec,
chargeMap,
peak,
smem.posBcast,
smem.buf,
smem.innerAboveThreshold,
&dummy_pc,
labelAcc);
}
if ((clusterer.mPmemory->fragment).isOverlap(peak.time())) {
if (clusterer.mPclusterPosInRow) {
clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
}
return;
}
if (dtype == 0) {
pc.setFull(central_charge * clustererNN.mOutputDataReg1_16[model_output_index + 4].ToFloat(),
static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg1_16[model_output_index].ToFloat(),
clustererNN.mOutputDataReg1_16[model_output_index + 2].ToFloat(),
(clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg1_16[model_output_index + 1].ToFloat(),
clustererNN.mOutputDataReg1_16[model_output_index + 3].ToFloat(),
clustererNN.mClusterFlags[2 * glo_idx],
clustererNN.mClusterFlags[2 * glo_idx + 1]);
} else if (dtype == 1) {
pc.setFull(central_charge * clustererNN.mOutputDataReg1_32[model_output_index + 4],
static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg1_32[model_output_index],
clustererNN.mOutputDataReg1_32[model_output_index + 2],
(clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg1_32[model_output_index + 1],
clustererNN.mOutputDataReg1_32[model_output_index + 3],
clustererNN.mClusterFlags[2 * glo_idx],
clustererNN.mClusterFlags[2 * glo_idx + 1]);
}
tpc::ClusterNative myCluster;
bool rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
if (rejectCluster) {
if (clusterer.mPclusterPosInRow) {
clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
}
return;
}
uint32_t rowIndex = 0;
if (clusterOut != nullptr) {
rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
clusterer,
myCluster,
peak.row(),
clusterer.mNMaxClusterPerRow,
clusterer.mPclusterInRow,
clusterOut);
if (clusterer.mPclusterPosInRow != nullptr) {
clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
}
} else if (clusterer.mPclusterPosInRow) {
rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
}
CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow));
} else {
if (clusterer.mPclusterPosInRow) {
clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
}
return;
}
}
template <>
GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::publishClass2Regression>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& processors, uint8_t sector, int8_t dtype, int8_t withMC, uint32_t batchStart)
{
uint32_t glo_idx = get_global_id(0);
auto& clusterer = processors.tpcClusterer[sector];
auto& clustererNN = processors.tpcNNClusterer[sector];
CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(glo_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
float central_charge = static_cast<float>(chargeMap[peak].unpack());
CPU_ONLY(MCLabelAccumulator labelAccElem(clusterer));
MCLabelAccumulator* labelAcc = CPU_PTR(&labelAccElem);
tpc::ClusterNative* clusterOut = (withMC) ? nullptr : clusterer.mPclusterByRow;
uint32_t full_glo_idx = glo_idx + batchStart;
uint32_t model_output_index = glo_idx * clustererNN.mNnClusterizerModelReg2NumOutputNodes;
if (clustererNN.mOutputDataClass[full_glo_idx] > 0) {
ClusterAccumulator pc;
if (withMC) {
ClusterAccumulator dummy_pc;
CPU_ONLY(labelAcc->collect(peak, central_charge));
GPUTPCCFClusterizer::buildCluster(
clusterer.Param().rec,
chargeMap,
peak,
smem.posBcast,
smem.buf,
smem.innerAboveThreshold,
&dummy_pc,
labelAcc);
}
if ((clusterer.mPmemory->fragment).isOverlap(peak.time())) {
if (clusterer.mPclusterPosInRow) {
clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
}
return;
}
// Cluster 1
if (dtype == 0) {
pc.setFull(central_charge * clustererNN.mOutputDataReg2_16[model_output_index + 8].ToFloat(),
static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_16[model_output_index].ToFloat(),
clustererNN.mOutputDataReg2_16[model_output_index + 4].ToFloat(),
(clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_16[model_output_index + 2].ToFloat(),
clustererNN.mOutputDataReg2_16[model_output_index + 6].ToFloat(),
clustererNN.mClusterFlags[2 * glo_idx],
clustererNN.mClusterFlags[2 * glo_idx + 1]);
} else if (dtype == 1) {
pc.setFull(central_charge * clustererNN.mOutputDataReg2_32[model_output_index + 8],
static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_32[model_output_index],
clustererNN.mOutputDataReg2_32[model_output_index + 4],
(clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_32[model_output_index + 2],
clustererNN.mOutputDataReg2_32[model_output_index + 6],
clustererNN.mClusterFlags[2 * glo_idx],
clustererNN.mClusterFlags[2 * glo_idx + 1]);
}
tpc::ClusterNative myCluster;
bool rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
if (rejectCluster) {
if (clusterer.mPclusterPosInRow) {
clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
}
return;
}
uint32_t rowIndex = 0;
if (clusterOut != nullptr) {
rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
clusterer,
myCluster,
peak.row(),
clusterer.mNMaxClusterPerRow,
clusterer.mPclusterInRow,
clusterOut);
if (clusterer.mPclusterPosInRow != nullptr) {
clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
}
} else if (clusterer.mPclusterPosInRow) {
rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
}
CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow));
// Cluster 2
if (dtype == 0) {
pc.setFull(central_charge * clustererNN.mOutputDataReg2_16[model_output_index + 9].ToFloat(),
static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_16[model_output_index + 1].ToFloat(),
clustererNN.mOutputDataReg2_16[model_output_index + 5].ToFloat(),
(clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_16[model_output_index + 3].ToFloat(),
clustererNN.mOutputDataReg2_16[model_output_index + 7].ToFloat(),
clustererNN.mClusterFlags[2 * glo_idx],
clustererNN.mClusterFlags[2 * glo_idx + 1]);
} else if (dtype == 1) {
pc.setFull(central_charge * clustererNN.mOutputDataReg2_32[model_output_index + 9],
static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_32[model_output_index + 1],
clustererNN.mOutputDataReg2_32[model_output_index + 5],
(clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_32[model_output_index + 3],
clustererNN.mOutputDataReg2_32[model_output_index + 7],
clustererNN.mClusterFlags[2 * glo_idx],
clustererNN.mClusterFlags[2 * glo_idx + 1]);
}
rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
if (rejectCluster) {
if (clusterer.mPclusterPosInRow) {
clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
}
return;
}
if (clusterOut != nullptr) {
rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
clusterer,
myCluster,
peak.row(),
clusterer.mNMaxClusterPerRow,
clusterer.mPclusterInRow,
clusterOut);
if (clusterer.mPclusterPosInRow != nullptr) {
clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
}
} else if (clusterer.mPclusterPosInRow) {
rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
}
// CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow)); // -> Is this needed? How to handle MC labels for split clusters?
} else {
if (clusterer.mPclusterPosInRow) {
clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
}
return;
}
}
// ---------------------------------
template <>
GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::publishDeconvolutionFlags>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& processors, uint8_t sector, int8_t dtype, int8_t withMC, uint batchStart)
{
// Implements identical publishing logic as the heuristic clusterizer and deconvolution kernel
uint32_t idx = get_global_id(0);
auto& clusterer = processors.tpcClusterer[sector];
auto& clustererNN = processors.tpcNNClusterer[sector];
CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
CfChargePos peak = clusterer.mPfilteredPeakPositions[idx + batchStart];
for (int i = 0; i < 8; i++) {
Delta2 d = cfconsts::InnerNeighbors[i];
CfChargePos tmp_pos = peak.delta(d);
PackedCharge charge = chargeMap[tmp_pos];
clustererNN.mClusterFlags[2 * idx] += (d.y != 0 && charge.isSplit());
clustererNN.mClusterFlags[2 * idx + 1] += (d.x != 0 && charge.isSplit());
}
for (int i = 0; i < 16; i++) {
Delta2 d = cfconsts::OuterNeighbors[i];
CfChargePos tmp_pos = peak.delta(d);
PackedCharge charge = chargeMap[tmp_pos];
clustererNN.mClusterFlags[2 * idx] += (d.y != 0 && charge.isSplit() && !charge.has3x3Peak());
clustererNN.mClusterFlags[2 * idx + 1] += (d.x != 0 && charge.isSplit() && !charge.has3x3Peak());
}
}
// THe following arithmetic is done because the network is trained with a split between IROC and OROC boundary
GPUd() int32_t GPUTPCNNClusterizerKernels::padOffset(int32_t row_ref, int32_t row_current)
{
return (int)((GPUTPCGeometry::NPads(row_current) - GPUTPCGeometry::NPads(row_ref)) / 2);
}
GPUd() int32_t GPUTPCNNClusterizerKernels::rowOffset(int32_t row, int32_t global_shift)
{
return (row > 62 ? global_shift : 0);
}
GPUd() bool GPUTPCNNClusterizerKernels::isBoundary(int32_t row, int32_t pad, int32_t global_shift)
{
if (pad < 0 || row < 0) { // Faster short-circuit
return true;
} else if (row < 63) {
return (pad >= static_cast<int>(GPUTPCGeometry::NPads(row)));
} else if (row < (63 + global_shift)) { // to account for the gap between IROC and OROC. Charge will be set to -1 in order to signal boundary to the neural network
return true;
} else if (row < (o2::tpc::constants::MAXGLOBALPADROW + global_shift)) {
return (pad >= static_cast<int>(GPUTPCGeometry::NPads(row - global_shift)));
} else {
return true;
}
}