Project
Loading...
Searching...
No Matches
GPUTPCNNClusterizerKernels.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
16#include "GPUTPCCFClusterizer.h"
17#include "GPUTPCGeometry.h"
18
19using namespace o2::gpu;
20using namespace o2::gpu::tpccf;
21
22#include "CfConsts.h"
23#include "CfUtils.h"
24#include "ClusterAccumulator.h"
26
27#if !defined(GPUCA_GPUCODE)
28#include "GPUHostDataTypes.h"
29#include "MCLabelAccumulator.h"
30#endif
31
32#ifdef GPUCA_GPUCODE
33#include "GPUTPCCFClusterizer.inc"
34#endif
35
36// Defining individual thread functions for data filling, determining the class label and running the CF clusterizer
37template <>
38GPUdii() 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)
39{
40 uint32_t glo_idx = get_global_id(0);
41 auto& clusterer = processors.tpcClusterer[sector];
42 auto& clustererNN = processors.tpcNNClusterer[sector];
43 if (clustererNN.mOutputDataClass[glo_idx] == 0) { // default clusterizer should not be called in batched mode due to mess-up with thread indices
44 return;
45 }
46 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
47 CPU_ONLY(MCLabelAccumulator labelAcc(clusterer));
48 tpc::ClusterNative* clusterOut = (withMC) ? nullptr : clusterer.mPclusterByRow;
50 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);
51}
52
53template <>
54GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::fillInputNNCPU>(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)
55{
56 auto& clusterer = processors.tpcClusterer[sector];
57 auto& clustererNN = processors.tpcNNClusterer[sector];
58
59 uint32_t glo_idx = get_global_id(0);
60 if (glo_idx + batchStart >= clusterer.mPmemory->counters.nClusters) {
61 return;
62 }
63
64 uint32_t write_idx = glo_idx * clustererNN.mNnClusterizerElementSize;
65
66 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
67 CfArray2D<uint8_t> isPeakMap(clusterer.mPpeakMap);
68 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(glo_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
69 int32_t row = static_cast<int>(peak.row());
70 int32_t pad = static_cast<int>(peak.pad());
71 int32_t time = static_cast<int>(peak.time());
72 float central_charge = static_cast<float>(chargeMap[peak].unpack());
73 int32_t row_offset = GPUTPCNNClusterizerKernels::rowOffset(row, clustererNN.mNnClusterizerSizeInputRow);
74
75 for (int32_t r = -clustererNN.mNnClusterizerSizeInputRow; r <= clustererNN.mNnClusterizerSizeInputRow; ++r) {
76 int32_t target_row = row + r;
77 bool is_row_boundary = (target_row < 0) || (target_row >= o2::tpc::constants::MAXGLOBALPADROW);
78 int32_t pad_offset = is_row_boundary ? 0 : GPUTPCNNClusterizerKernels::padOffset(row, target_row);
79
80 for (int32_t p = -clustererNN.mNnClusterizerSizeInputPad + pad_offset; p <= clustererNN.mNnClusterizerSizeInputPad + pad_offset; ++p) {
81 int32_t target_pad = pad + p;
82 bool is_boundary = is_row_boundary || GPUTPCNNClusterizerKernels::isBoundary(target_row + row_offset, target_pad, clustererNN.mNnClusterizerSizeInputRow);
83
84 for (int32_t t = -clustererNN.mNnClusterizerSizeInputTime; t <= clustererNN.mNnClusterizerSizeInputTime; ++t) {
85 int32_t target_time = time + t;
86
87 if (is_boundary || target_time < 0 || target_time >= TPC_MAX_FRAGMENT_LEN_GPU) {
88 // Fill boundary value
89 float boundary_value = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
90 if (dtype == 0) {
91 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)boundary_value;
92 } else {
93 clustererNN.mInputData_32[write_idx] = boundary_value;
94 }
95 } else {
96 CfChargePos tmp_pos(target_row, target_pad, target_time);
97 float normalized_charge = static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge;
98
99 if (!clustererNN.mNnClusterizerSetDeconvolutionFlags && r == 0 && CAMath::Abs(p) < 3 && CAMath::Abs(t) < 3 && p != 0 && t != 0) {
100 clustererNN.mClusterFlags[2 * glo_idx] += CfUtils::isPeak(isPeakMap[tmp_pos]);
101 clustererNN.mClusterFlags[2 * glo_idx + 1] = clustererNN.mClusterFlags[2 * glo_idx];
102 }
103
104 if (dtype == 0) {
105 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)normalized_charge;
106 } else {
107 clustererNN.mInputData_32[write_idx] = normalized_charge;
108 }
109 }
110 // if((CAMath::Abs(static_cast<float>(clustererNN.mInputData_16_Test[write_idx]) - static_cast<float>(clustererNN.mInputData_16[write_idx])) > 1e-4) && ((glo_idx + batchStart) < clusterer.mPmemory->counters.nClusters)) {
111 // printf("Warning: Input data mismatch at index %d, %d - row, pad, time: %d, %d, %d : %f -> %f\n", glo_idx, glo_idx + batchStart, r, p, t,
112 // static_cast<float>(clustererNN.mInputData_16_Test[write_idx]), static_cast<float>(clustererNN.mInputData_16[write_idx]));
113 // }
114 write_idx++;
115 }
116 }
117 }
118
119 if (clustererNN.mNnClusterizerAddIndexData) {
120 float sector_norm = sector / 36.f;
121 float row_norm = row / 152.f;
122 float pad_norm = static_cast<float>(pad) / GPUTPCGeometry::NPads(row);
123
124 if (dtype == 0) {
125 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)sector_norm;
126 clustererNN.mInputData_16[write_idx + 1] = (OrtDataType::Float16_t)row_norm;
127 clustererNN.mInputData_16[write_idx + 2] = (OrtDataType::Float16_t)pad_norm;
128 } else {
129 clustererNN.mInputData_32[write_idx] = sector_norm;
130 clustererNN.mInputData_32[write_idx + 1] = row_norm;
131 clustererNN.mInputData_32[write_idx + 2] = pad_norm;
132 }
133 }
134
135 if (!clustererNN.mNnClusterizerSetDeconvolutionFlags) {
136 clustererNN.mClusterFlags[2 * glo_idx] = 0;
137 clustererNN.mClusterFlags[2 * glo_idx + 1] = 0;
138
139 for (uint16_t i = 0; i < 8; ++i) {
140 Delta2 d = cfconsts::InnerNeighbors[i];
141 CfChargePos tmp_pos = peak.delta(d);
142 clustererNN.mClusterFlags[2 * glo_idx] += CfUtils::isPeak(isPeakMap[tmp_pos]);
143 }
144 clustererNN.mClusterFlags[2 * glo_idx + 1] = clustererNN.mClusterFlags[2 * glo_idx];
145 }
146}
147
148template <>
149GPUdii() void GPUTPCNNClusterizerKernels::Thread<GPUTPCNNClusterizerKernels::fillInputNNGPU>(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)
150{
151 uint32_t glo_idx = get_global_id(0);
152
153 auto& clusterer = processors.tpcClusterer[sector];
154 auto& clustererNN = processors.tpcNNClusterer[sector];
155
156 // Optimized division using bit operations
157 uint32_t base_idx = glo_idx / clustererNN.mNnClusterizerRowTimeSizeFull;
158 uint32_t transient_index = glo_idx - (base_idx * clustererNN.mNnClusterizerRowTimeSizeFull);
159
160 // Early exit for out-of-bounds threads
161 if (base_idx + batchStart >= clusterer.mPmemory->counters.nClusters) {
162 return;
163 }
164 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
165 CfArray2D<uint8_t> isPeakMap(clusterer.mPpeakMap);
166
167 // Use dedicated neural network shared memory arrays for warp-level caching
168 // First thread in each warp loads shared data
169 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(base_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
170 float central_charge = static_cast<float>(chargeMap[peak].unpack());
171 int32_t row = static_cast<int>(peak.row());
172 int32_t pad = static_cast<int>(peak.pad());
173 int32_t time = static_cast<int>(peak.time());
174
175 // Handle index data with fewer branches
176 if (clustererNN.mNnClusterizerAddIndexData && transient_index >= clustererNN.mNnClusterizerRowTimeSize) {
177 int32_t data_idx = transient_index - clustererNN.mNnClusterizerRowTimeSize;
178 uint32_t write_idx = base_idx * clustererNN.mNnClusterizerElementSize + clustererNN.mNnClusterizerChargeArraySize + data_idx;
179
180 float index_values[3] = {
181 sector / 36.f,
182 row / 152.f,
183 static_cast<float>(pad) / GPUTPCGeometry::NPads(row)};
184
185 if (dtype == 0) {
186 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)index_values[data_idx];
187 } else {
188 clustererNN.mInputData_32[write_idx] = index_values[data_idx];
189 }
190
191 // Handle deconvolution flags only once per cluster (last thread in element)
192 if (data_idx == 2 && !clustererNN.mNnClusterizerSetDeconvolutionFlags) {
193 uint8_t cluster_flags = 0;
194 for (uint16_t i = 0; i < 8; i++) {
195 Delta2 d = cfconsts::InnerNeighbors[i];
196 CfChargePos tmp_pos = peak.delta(d);
197 cluster_flags += CfUtils::isPeak(isPeakMap[tmp_pos]);
198 }
199 clustererNN.mClusterFlags[2 * base_idx] = cluster_flags;
200 clustererNN.mClusterFlags[2 * base_idx + 1] = cluster_flags;
201 }
202 return;
203 }
204
205 // Main data processing - optimize index calculations
206 if (transient_index < clustererNN.mNnClusterizerRowTimeSize) {
207 // Optimize 3D index calculation
208 int32_t row_idx = transient_index / clustererNN.mNnClusterizerFullTimeSize;
209 int32_t r_local = row_idx - clustererNN.mNnClusterizerSizeInputRow;
210 int32_t time_idx = transient_index - row_idx * clustererNN.mNnClusterizerFullTimeSize;
211 int32_t t_local = time_idx - clustererNN.mNnClusterizerSizeInputTime;
212 int32_t write_idx = base_idx * clustererNN.mNnClusterizerElementSize + row_idx * clustererNN.mNnClusterizerPadTimeSize + time_idx;
213
214 // Early boundary check for row
215 int32_t target_row = row + r_local;
216 int8_t is_row_boundary = (target_row < 0) || (target_row > (o2::tpc::constants::MAXGLOBALPADROW - 1));
217
218 // Calculate offsets
219 int32_t row_offset = GPUTPCNNClusterizerKernels::rowOffset(row, clustererNN.mNnClusterizerSizeInputRow);
220 int32_t pad_offset = GPUTPCNNClusterizerKernels::padOffset(row, target_row);
221 for (int32_t p_local = -clustererNN.mNnClusterizerSizeInputPad + pad_offset; p_local <= clustererNN.mNnClusterizerSizeInputPad + pad_offset; p_local++) {
222 if (is_row_boundary) {
223 // Use boundary fill value
224 float boundary_val = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
225 if (dtype == 0) {
226 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)boundary_val;
227 } else {
228 clustererNN.mInputData_32[write_idx] = boundary_val;
229 }
230 write_idx += clustererNN.mNnClusterizerFullTimeSize; // Move to next pad position
231 continue;
232 }
233
234 // Calculate target pad and time
235 int32_t target_pad = pad + p_local;
236 int32_t target_time = time + t_local;
237
238 // Optimized boundary check
239 int8_t is_boundary = GPUTPCNNClusterizerKernels::isBoundary(target_row + row_offset, target_pad, clustererNN.mNnClusterizerSizeInputRow) || (target_time < 0) || (target_time >= TPC_MAX_FRAGMENT_LEN_GPU);
240
241 float output_value;
242 if (is_boundary) {
243 output_value = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
244 } else {
245 // Coalesced memory access - create position and read charge
246 CfChargePos tmp_pos(target_row, target_pad, target_time);
247 output_value = static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge; // Normalize by central charge
248 }
249
250 // Write output with reduced branching
251 if (dtype == 0) {
252 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)output_value;
253 } else {
254 clustererNN.mInputData_32[write_idx] = output_value;
255 }
256 write_idx += clustererNN.mNnClusterizerFullTimeSize; // Move to next pad position
257 }
258 }
259}
260
261template <>
262GPUdii() 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)
263{
264 uint32_t glo_idx = get_global_id(0);
265 if (dtype == 0) {
266 processors.tpcNNClusterer[sector].mOutputDataClass[glo_idx + batchStart] = (int)((processors.tpcNNClusterer[sector].mModelProbabilities_16[glo_idx]).ToFloat() > processors.tpcNNClusterer[sector].mNnClassThreshold);
267 } else if (dtype == 1) {
268 processors.tpcNNClusterer[sector].mOutputDataClass[glo_idx + batchStart] = (int)(processors.tpcNNClusterer[sector].mModelProbabilities_32[glo_idx] > processors.tpcNNClusterer[sector].mNnClassThreshold);
269 }
270}
271
272template <>
273GPUdii() 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)
274{
275 auto& clustererNN = processors.tpcNNClusterer[sector];
276 uint32_t glo_idx = get_global_id(0);
277 uint32_t elem_iterator = glo_idx * clustererNN.mNnClusterizerModelClassNumOutputNodes;
278 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]
279 uint32_t class_label = 0;
280 for (uint32_t pIdx = elem_iterator; pIdx < elem_iterator + clustererNN.mNnClusterizerModelClassNumOutputNodes; pIdx++) {
281 if (pIdx == elem_iterator) {
282 if (dtype == 0) {
283 current_max_prob = static_cast<float>(clustererNN.mModelProbabilities_16[pIdx]);
284 } else if (dtype == 1) {
285 current_max_prob = clustererNN.mModelProbabilities_32[pIdx];
286 }
287 } else {
288 if (dtype == 0) {
289 current_max_prob = CAMath::Max(current_max_prob, clustererNN.mModelProbabilities_16[pIdx].ToFloat());
290 } else if (dtype == 1) {
291 current_max_prob = CAMath::Max(current_max_prob, clustererNN.mModelProbabilities_32[pIdx]);
292 }
293 }
294 }
295 // 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"
296 clustererNN.mOutputDataClass[glo_idx + batchStart] = class_label;
297 if (class_label > 1) {
298 clustererNN.mClusterFlags[2 * glo_idx] = 1;
299 clustererNN.mClusterFlags[2 * glo_idx + 1] = 1;
300 }
301}
302
303template <>
304GPUdii() 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)
305{
306 uint32_t glo_idx = get_global_id(0);
307 auto& clusterer = processors.tpcClusterer[sector];
308 auto& clustererNN = processors.tpcNNClusterer[sector];
309
310 uint32_t maxClusterNum = clusterer.mPmemory->counters.nClusters;
311 uint32_t full_glo_idx = glo_idx + batchStart;
312 int32_t model_output_index = glo_idx * clustererNN.mNnClusterizerModelReg1NumOutputNodes;
313
314 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
315 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(full_glo_idx, maxClusterNum - 1)];
316 float central_charge = static_cast<float>(chargeMap[peak].unpack());
317
318 CPU_ONLY(MCLabelAccumulator labelAccElem(clusterer));
319 MCLabelAccumulator* labelAcc = CPU_PTR(&labelAccElem);
320
321 if (full_glo_idx >= maxClusterNum) {
322 if (withMC) {
323 ClusterAccumulator dummy_pc;
324 CPU_ONLY(labelAcc->collect(peak, central_charge));
325 GPUTPCCFClusterizer::buildCluster(
326 clusterer.Param().rec,
327 chargeMap,
328 peak,
329 smem.posBcast,
330 smem.buf,
331 smem.innerAboveThreshold,
332 &dummy_pc,
333 labelAcc);
334 }
335 return;
336 }
337
338 tpc::ClusterNative* clusterOut = (withMC) ? nullptr : clusterer.mPclusterByRow;
339
340 // LOG(info) << glo_idx << " -- " << model_output_index << " / " << clustererNN.outputDataReg1.size() << " / " << clustererNN.mNnClusterizerModelReg1NumOutputNodes << " -- " << clusterer.peakPositions.size() << " -- " << clusterer.centralCharges.size();
341
342 if (clustererNN.mOutputDataClass[full_glo_idx] == 1 || (clustererNN.mNnClusterizerModelReg2NumOutputNodes != -1 && clustererNN.mOutputDataClass[full_glo_idx] >= 1)) {
343
345
346 // Publishing logic is taken from default clusterizer
347 if (withMC) {
348 ClusterAccumulator dummy_pc;
349 CPU_ONLY(labelAcc->collect(peak, central_charge));
350 GPUTPCCFClusterizer::buildCluster(
351 clusterer.Param().rec,
352 chargeMap,
353 peak,
354 smem.posBcast,
355 smem.buf,
356 smem.innerAboveThreshold,
357 &dummy_pc,
358 labelAcc);
359 }
360 if ((clusterer.mPmemory->fragment).isOverlap(peak.time())) {
361 if (clusterer.mPclusterPosInRow) {
362 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
363 }
364 return;
365 }
366
367 if (dtype == 0) {
368 pc.setFull(central_charge * clustererNN.mOutputDataReg1_16[model_output_index + 4].ToFloat(),
369 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg1_16[model_output_index].ToFloat(),
370 clustererNN.mOutputDataReg1_16[model_output_index + 2].ToFloat(),
371 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg1_16[model_output_index + 1].ToFloat(),
372 clustererNN.mOutputDataReg1_16[model_output_index + 3].ToFloat(),
373 clustererNN.mClusterFlags[2 * glo_idx],
374 clustererNN.mClusterFlags[2 * glo_idx + 1]);
375 } else if (dtype == 1) {
376 pc.setFull(central_charge * clustererNN.mOutputDataReg1_32[model_output_index + 4],
377 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg1_32[model_output_index],
378 clustererNN.mOutputDataReg1_32[model_output_index + 2],
379 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg1_32[model_output_index + 1],
380 clustererNN.mOutputDataReg1_32[model_output_index + 3],
381 clustererNN.mClusterFlags[2 * glo_idx],
382 clustererNN.mClusterFlags[2 * glo_idx + 1]);
383 }
384
385 tpc::ClusterNative myCluster;
386 bool rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
387 if (rejectCluster) {
388 if (clusterer.mPclusterPosInRow) {
389 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
390 }
391 return;
392 }
393
394 uint32_t rowIndex = 0;
395 if (clusterOut != nullptr) {
396 rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
397 clusterer,
398 myCluster,
399 peak.row(),
400 clusterer.mNMaxClusterPerRow,
401 clusterer.mPclusterInRow,
402 clusterOut);
403 if (clusterer.mPclusterPosInRow != nullptr) {
404 clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
405 }
406 } else if (clusterer.mPclusterPosInRow) {
407 rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
408 }
409 CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow));
410 } else {
411 if (clusterer.mPclusterPosInRow) {
412 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
413 }
414 return;
415 }
416}
417
418template <>
419GPUdii() 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)
420{
421 uint32_t glo_idx = get_global_id(0);
422 auto& clusterer = processors.tpcClusterer[sector];
423 auto& clustererNN = processors.tpcNNClusterer[sector];
424
425 uint32_t maxClusterNum = clusterer.mPmemory->counters.nClusters;
426 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
427 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(glo_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
428 float central_charge = static_cast<float>(chargeMap[peak].unpack());
429
430 CPU_ONLY(MCLabelAccumulator labelAccElem(clusterer));
431 MCLabelAccumulator* labelAcc = CPU_PTR(&labelAccElem);
432 tpc::ClusterNative* clusterOut = (withMC) ? nullptr : clusterer.mPclusterByRow;
433 uint32_t full_glo_idx = glo_idx + batchStart;
434
435 if (full_glo_idx >= maxClusterNum) {
436 if (withMC) {
437 ClusterAccumulator dummy_pc;
438 CPU_ONLY(labelAcc->collect(peak, central_charge));
439 GPUTPCCFClusterizer::buildCluster(
440 clusterer.Param().rec,
441 chargeMap,
442 peak,
443 smem.posBcast,
444 smem.buf,
445 smem.innerAboveThreshold,
446 &dummy_pc,
447 labelAcc);
448 }
449 return;
450 }
451
452 uint32_t model_output_index = glo_idx * clustererNN.mNnClusterizerModelReg2NumOutputNodes;
453
454 if (clustererNN.mOutputDataClass[full_glo_idx] > 0) {
455
457
458 if (withMC) {
459 ClusterAccumulator dummy_pc;
460 CPU_ONLY(labelAcc->collect(peak, central_charge));
461 GPUTPCCFClusterizer::buildCluster(
462 clusterer.Param().rec,
463 chargeMap,
464 peak,
465 smem.posBcast,
466 smem.buf,
467 smem.innerAboveThreshold,
468 &dummy_pc,
469 labelAcc);
470 }
471 if ((clusterer.mPmemory->fragment).isOverlap(peak.time())) {
472 if (clusterer.mPclusterPosInRow) {
473 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
474 }
475 return;
476 }
477
478 // Cluster 1
479 if (dtype == 0) {
480 pc.setFull(central_charge * clustererNN.mOutputDataReg2_16[model_output_index + 8].ToFloat(),
481 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_16[model_output_index].ToFloat(),
482 clustererNN.mOutputDataReg2_16[model_output_index + 4].ToFloat(),
483 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_16[model_output_index + 2].ToFloat(),
484 clustererNN.mOutputDataReg2_16[model_output_index + 6].ToFloat(),
485 clustererNN.mClusterFlags[2 * glo_idx],
486 clustererNN.mClusterFlags[2 * glo_idx + 1]);
487 } else if (dtype == 1) {
488 pc.setFull(central_charge * clustererNN.mOutputDataReg2_32[model_output_index + 8],
489 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_32[model_output_index],
490 clustererNN.mOutputDataReg2_32[model_output_index + 4],
491 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_32[model_output_index + 2],
492 clustererNN.mOutputDataReg2_32[model_output_index + 6],
493 clustererNN.mClusterFlags[2 * glo_idx],
494 clustererNN.mClusterFlags[2 * glo_idx + 1]);
495 }
496
497 tpc::ClusterNative myCluster;
498 bool rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
499 if (rejectCluster) {
500 if (clusterer.mPclusterPosInRow) {
501 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
502 }
503 return;
504 }
505
506 uint32_t rowIndex = 0;
507 if (clusterOut != nullptr) {
508 rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
509 clusterer,
510 myCluster,
511 peak.row(),
512 clusterer.mNMaxClusterPerRow,
513 clusterer.mPclusterInRow,
514 clusterOut);
515 if (clusterer.mPclusterPosInRow != nullptr) {
516 clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
517 }
518 } else if (clusterer.mPclusterPosInRow) {
519 rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
520 }
521 CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow));
522
523 // Cluster 2
524 if (dtype == 0) {
525 pc.setFull(central_charge * clustererNN.mOutputDataReg2_16[model_output_index + 9].ToFloat(),
526 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_16[model_output_index + 1].ToFloat(),
527 clustererNN.mOutputDataReg2_16[model_output_index + 5].ToFloat(),
528 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_16[model_output_index + 3].ToFloat(),
529 clustererNN.mOutputDataReg2_16[model_output_index + 7].ToFloat(),
530 clustererNN.mClusterFlags[2 * glo_idx],
531 clustererNN.mClusterFlags[2 * glo_idx + 1]);
532 } else if (dtype == 1) {
533 pc.setFull(central_charge * clustererNN.mOutputDataReg2_32[model_output_index + 9],
534 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_32[model_output_index + 1],
535 clustererNN.mOutputDataReg2_32[model_output_index + 5],
536 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_32[model_output_index + 3],
537 clustererNN.mOutputDataReg2_32[model_output_index + 7],
538 clustererNN.mClusterFlags[2 * glo_idx],
539 clustererNN.mClusterFlags[2 * glo_idx + 1]);
540 }
541
542 rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
543 if (rejectCluster) {
544 if (clusterer.mPclusterPosInRow) {
545 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
546 }
547 return;
548 }
549
550 if (clusterOut != nullptr) {
551 rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
552 clusterer,
553 myCluster,
554 peak.row(),
555 clusterer.mNMaxClusterPerRow,
556 clusterer.mPclusterInRow,
557 clusterOut);
558 if (clusterer.mPclusterPosInRow != nullptr) {
559 clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
560 }
561 } else if (clusterer.mPclusterPosInRow) {
562 rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
563 }
564 // CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow)); // -> Is this needed? How to handle MC labels for split clusters?
565 } else {
566 if (clusterer.mPclusterPosInRow) {
567 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
568 }
569 return;
570 }
571}
572
573// ---------------------------------
574template <>
575GPUdii() 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)
576{
577 // Implements identical publishing logic as the heuristic clusterizer and deconvolution kernel
578 uint32_t idx = get_global_id(0);
579 auto& clusterer = processors.tpcClusterer[sector];
580 auto& clustererNN = processors.tpcNNClusterer[sector];
581 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
582 CfChargePos peak = clusterer.mPfilteredPeakPositions[idx + batchStart];
583
584 clustererNN.mClusterFlags[2 * idx] = 0;
585 clustererNN.mClusterFlags[2 * idx + 1] = 0;
586 for (int i = 0; i < 8; i++) {
587 Delta2 d = cfconsts::InnerNeighbors[i];
588 CfChargePos tmp_pos = peak.delta(d);
589 PackedCharge charge = chargeMap[tmp_pos];
590 clustererNN.mClusterFlags[2 * idx] += (d.y != 0 && charge.isSplit());
591 clustererNN.mClusterFlags[2 * idx + 1] += (d.x != 0 && charge.isSplit());
592 }
593 for (int i = 0; i < 16; i++) {
594 Delta2 d = cfconsts::OuterNeighbors[i];
595 CfChargePos tmp_pos = peak.delta(d);
596 PackedCharge charge = chargeMap[tmp_pos];
597 clustererNN.mClusterFlags[2 * idx] += (d.y != 0 && charge.isSplit() && !charge.has3x3Peak());
598 clustererNN.mClusterFlags[2 * idx + 1] += (d.x != 0 && charge.isSplit() && !charge.has3x3Peak());
599 }
600}
601
602// THe following arithmetic is done because the network is trained with a split between IROC and OROC boundary
603GPUd() int32_t GPUTPCNNClusterizerKernels::padOffset(int32_t row_ref, int32_t row_current)
604{
605 if (row_current < 0 || row_current >= o2::tpc::constants::MAXGLOBALPADROW) {
606 return 0; // Short-circuit for negative rows
607 } else {
608 return (int)((GPUTPCGeometry::NPads(row_current) - GPUTPCGeometry::NPads(row_ref)) / 2);
609 }
610}
611
612GPUd() int32_t GPUTPCNNClusterizerKernels::rowOffset(int32_t row, int32_t offset)
613{
614 return (row > 62 ? offset : 0);
615}
616
617GPUd() bool GPUTPCNNClusterizerKernels::isBoundary(int32_t row, int32_t pad, int32_t offset)
618{
619 if (pad < 0 || row < 0) { // Faster short-circuit
620 return true;
621 } else if (row < 63) {
622 return ((pad < 0) || (pad >= static_cast<int>(GPUTPCGeometry::NPads(row))));
623 } else if (row < (63 + offset)) { // to account for the gap between IROC and OROC. Charge will be set to the boundary fill value in order to signal boundaries to the neural network
624 return true;
626 return ((pad < 0) || (pad >= static_cast<int>(GPUTPCGeometry::NPads(row - offset))));
627 } else {
628 return true;
629 }
630}
int16_t charge
Definition RawEventData.h:5
int16_t time
Definition RawEventData.h:4
int32_t i
#define get_local_size(dim)
#define get_local_id(dim)
#define get_num_groups(dim)
#define get_global_id(dim)
#define get_group_id(dim)
GPUdii() void GPUTPCNNClusterizerKernels
GPUd() int32_t GPUTPCNNClusterizerKernels
void collect(const CfChargePos &, tpccf::Charge)
void commit(tpccf::Row, uint32_t, uint32_t)
#define TPC_MAX_FRAGMENT_LEN_GPU
#define CPU_ONLY(x)
#define CPU_PTR(x)
GLintptr offset
Definition glcorearb.h:660
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLboolean r
Definition glcorearb.h:1233
constexpr int MAXGLOBALPADROW
Definition Constants.h:34
int16_t y
int16_t x
std::vector< int > row