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 if (dtype == 0) {
121 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)(static_cast<float>(sector) / o2::tpc::constants::MAXSECTOR);
122 clustererNN.mInputData_16[write_idx + 1] = (OrtDataType::Float16_t)(static_cast<float>(row) / o2::tpc::constants::MAXGLOBALPADROW);
123 clustererNN.mInputData_16[write_idx + 2] = (OrtDataType::Float16_t)(static_cast<float>(pad) / GPUTPCGeometry::NPads(row));
124 } else {
125 clustererNN.mInputData_32[write_idx] = static_cast<float>(sector) / o2::tpc::constants::MAXSECTOR;
126 clustererNN.mInputData_32[write_idx + 1] = static_cast<float>(row) / o2::tpc::constants::MAXGLOBALPADROW;
127 clustererNN.mInputData_32[write_idx + 2] = static_cast<float>(pad) / GPUTPCGeometry::NPads(row);
128 }
129 }
130
131 if (!clustererNN.mNnClusterizerSetDeconvolutionFlags) {
132 clustererNN.mClusterFlags[2 * glo_idx] = 0;
133 clustererNN.mClusterFlags[2 * glo_idx + 1] = 0;
134
135 for (uint16_t i = 0; i < 8; ++i) {
136 Delta2 d = cfconsts::InnerNeighbors[i];
137 CfChargePos tmp_pos = peak.delta(d);
138 clustererNN.mClusterFlags[2 * glo_idx] += CfUtils::isPeak(isPeakMap[tmp_pos]);
139 }
140 clustererNN.mClusterFlags[2 * glo_idx + 1] = clustererNN.mClusterFlags[2 * glo_idx];
141 }
142}
143
144template <>
145GPUdii() 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)
146{
147 uint32_t glo_idx = get_global_id(0);
148
149 auto& clusterer = processors.tpcClusterer[sector];
150 auto& clustererNN = processors.tpcNNClusterer[sector];
151
152 // Optimized division using bit operations
153 uint32_t base_idx = glo_idx / clustererNN.mNnClusterizerRowTimeSizeFull;
154 uint32_t transient_index = glo_idx - (base_idx * clustererNN.mNnClusterizerRowTimeSizeFull);
155
156 // Early exit for out-of-bounds threads
157 if (base_idx + batchStart >= clusterer.mPmemory->counters.nClusters) {
158 return;
159 }
160 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
161 CfArray2D<uint8_t> isPeakMap(clusterer.mPpeakMap);
162
163 // Use dedicated neural network shared memory arrays for warp-level caching
164 // First thread in each warp loads shared data
165 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(base_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
166 float central_charge = static_cast<float>(chargeMap[peak].unpack());
167 int32_t row = static_cast<int>(peak.row());
168 int32_t pad = static_cast<int>(peak.pad());
169 int32_t time = static_cast<int>(peak.time());
170
171 // Handle index data with fewer branches
172 if (clustererNN.mNnClusterizerAddIndexData && transient_index >= clustererNN.mNnClusterizerRowTimeSize) {
173 int32_t data_idx = transient_index - clustererNN.mNnClusterizerRowTimeSize;
174 uint32_t write_idx = base_idx * clustererNN.mNnClusterizerElementSize + clustererNN.mNnClusterizerChargeArraySize + data_idx;
175
176 float index_values[3] = {
177 static_cast<float>(sector) / o2::tpc::constants::MAXSECTOR,
178 static_cast<float>(row) / o2::tpc::constants::MAXGLOBALPADROW,
179 static_cast<float>(pad) / GPUTPCGeometry::NPads(row)};
180
181 if (dtype == 0) {
182 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)index_values[data_idx];
183 } else {
184 clustererNN.mInputData_32[write_idx] = index_values[data_idx];
185 }
186
187 // Handle deconvolution flags only once per cluster (last thread in element)
188 if (data_idx == 2 && !clustererNN.mNnClusterizerSetDeconvolutionFlags) {
189 uint8_t cluster_flags = 0;
190 for (uint16_t i = 0; i < 8; i++) {
191 Delta2 d = cfconsts::InnerNeighbors[i];
192 CfChargePos tmp_pos = peak.delta(d);
193 cluster_flags += CfUtils::isPeak(isPeakMap[tmp_pos]);
194 }
195 clustererNN.mClusterFlags[2 * base_idx] = cluster_flags;
196 clustererNN.mClusterFlags[2 * base_idx + 1] = cluster_flags;
197 }
198 return;
199 }
200
201 // Main data processing - optimize index calculations
202 if (transient_index < clustererNN.mNnClusterizerRowTimeSize) {
203 // Optimize 3D index calculation
204 int32_t row_idx = transient_index / clustererNN.mNnClusterizerFullTimeSize;
205 int32_t r_local = row_idx - clustererNN.mNnClusterizerSizeInputRow;
206 int32_t time_idx = transient_index - row_idx * clustererNN.mNnClusterizerFullTimeSize;
207 int32_t t_local = time_idx - clustererNN.mNnClusterizerSizeInputTime;
208 int32_t write_idx = base_idx * clustererNN.mNnClusterizerElementSize + row_idx * clustererNN.mNnClusterizerPadTimeSize + time_idx;
209
210 // Early boundary check for row
211 int32_t target_row = row + r_local;
212 int8_t is_row_boundary = (target_row < 0) || (target_row > (o2::tpc::constants::MAXGLOBALPADROW - 1));
213
214 // Calculate offsets
215 int32_t row_offset = GPUTPCNNClusterizerKernels::rowOffset(row, clustererNN.mNnClusterizerSizeInputRow);
216 int32_t pad_offset = GPUTPCNNClusterizerKernels::padOffset(row, target_row);
217 for (int32_t p_local = -clustererNN.mNnClusterizerSizeInputPad + pad_offset; p_local <= clustererNN.mNnClusterizerSizeInputPad + pad_offset; p_local++) {
218 if (is_row_boundary) {
219 // Use boundary fill value
220 float boundary_val = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
221 if (dtype == 0) {
222 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)boundary_val;
223 } else {
224 clustererNN.mInputData_32[write_idx] = boundary_val;
225 }
226 write_idx += clustererNN.mNnClusterizerFullTimeSize; // Move to next pad position
227 continue;
228 }
229
230 // Calculate target pad and time
231 int32_t target_pad = pad + p_local;
232 int32_t target_time = time + t_local;
233
234 // Optimized boundary check
235 int8_t is_boundary = GPUTPCNNClusterizerKernels::isBoundary(target_row + row_offset, target_pad, clustererNN.mNnClusterizerSizeInputRow) || (target_time < 0) || (target_time >= TPC_MAX_FRAGMENT_LEN_GPU);
236
237 float output_value;
238 if (is_boundary) {
239 output_value = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
240 } else {
241 // Coalesced memory access - create position and read charge
242 CfChargePos tmp_pos(target_row, target_pad, target_time);
243 output_value = static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge; // Normalize by central charge
244 }
245
246 // Write output with reduced branching
247 if (dtype == 0) {
248 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)output_value;
249 } else {
250 clustererNN.mInputData_32[write_idx] = output_value;
251 }
252 write_idx += clustererNN.mNnClusterizerFullTimeSize; // Move to next pad position
253 }
254 }
255}
256
257template <>
258GPUdii() 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)
259{
260 uint32_t glo_idx = get_global_id(0);
261 if (dtype == 0) {
262 processors.tpcNNClusterer[sector].mOutputDataClass[glo_idx + batchStart] = (int)((processors.tpcNNClusterer[sector].mModelProbabilities_16[glo_idx]).ToFloat() > processors.tpcNNClusterer[sector].mNnClassThreshold);
263 } else if (dtype == 1) {
264 processors.tpcNNClusterer[sector].mOutputDataClass[glo_idx + batchStart] = (int)(processors.tpcNNClusterer[sector].mModelProbabilities_32[glo_idx] > processors.tpcNNClusterer[sector].mNnClassThreshold);
265 }
266}
267
268template <>
269GPUdii() 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)
270{
271 auto& clustererNN = processors.tpcNNClusterer[sector];
272 uint32_t glo_idx = get_global_id(0);
273 uint32_t elem_iterator = glo_idx * clustererNN.mNnClusterizerModelClassNumOutputNodes;
274 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]
275 uint32_t class_label = 0;
276 for (uint32_t pIdx = elem_iterator; pIdx < elem_iterator + clustererNN.mNnClusterizerModelClassNumOutputNodes; pIdx++) {
277 if (pIdx == elem_iterator) {
278 if (dtype == 0) {
279 current_max_prob = static_cast<float>(clustererNN.mModelProbabilities_16[pIdx]);
280 } else if (dtype == 1) {
281 current_max_prob = clustererNN.mModelProbabilities_32[pIdx];
282 }
283 } else {
284 if (dtype == 0) {
285 current_max_prob = CAMath::Max(current_max_prob, clustererNN.mModelProbabilities_16[pIdx].ToFloat());
286 } else if (dtype == 1) {
287 current_max_prob = CAMath::Max(current_max_prob, clustererNN.mModelProbabilities_32[pIdx]);
288 }
289 }
290 }
291 // 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"
292 clustererNN.mOutputDataClass[glo_idx + batchStart] = class_label;
293 if (class_label > 1) {
294 clustererNN.mClusterFlags[2 * glo_idx] = 1;
295 clustererNN.mClusterFlags[2 * glo_idx + 1] = 1;
296 }
297}
298
299template <>
300GPUdii() 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)
301{
302 uint32_t glo_idx = get_global_id(0);
303 auto& clusterer = processors.tpcClusterer[sector];
304 auto& clustererNN = processors.tpcNNClusterer[sector];
305
306 uint32_t maxClusterNum = clusterer.mPmemory->counters.nClusters;
307 uint32_t full_glo_idx = glo_idx + batchStart;
308 int32_t model_output_index = glo_idx * clustererNN.mNnClusterizerModelReg1NumOutputNodes;
309
310 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
311 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(full_glo_idx, maxClusterNum - 1)];
312 float central_charge = static_cast<float>(chargeMap[peak].unpack());
313
314 CPU_ONLY(MCLabelAccumulator labelAccElem(clusterer));
315 MCLabelAccumulator* labelAcc = CPU_PTR(&labelAccElem);
316
317 if (full_glo_idx >= maxClusterNum) {
318 if (withMC) {
319 ClusterAccumulator dummy_pc;
320 CPU_ONLY(labelAcc->collect(peak, central_charge));
321 GPUTPCCFClusterizer::buildCluster(
322 clusterer.Param().rec,
323 chargeMap,
324 peak,
325 smem.posBcast,
326 smem.buf,
327 smem.innerAboveThreshold,
328 &dummy_pc,
329 labelAcc);
330 }
331 return;
332 }
333
334 tpc::ClusterNative* clusterOut = clusterer.mPclusterByRow;
335
336 // LOG(info) << glo_idx << " -- " << model_output_index << " / " << clustererNN.outputDataReg1.size() << " / " << clustererNN.mNnClusterizerModelReg1NumOutputNodes << " -- " << clusterer.peakPositions.size() << " -- " << clusterer.centralCharges.size();
337
338 if (clustererNN.mOutputDataClass[full_glo_idx] == 1 || (clustererNN.mNnClusterizerUseClassification <= 0)) {
339
341
342 // Publishing logic is taken from default clusterizer
343 if (withMC) {
344 ClusterAccumulator dummy_pc;
345 CPU_ONLY(labelAcc->collect(peak, central_charge));
346 GPUTPCCFClusterizer::buildCluster(
347 clusterer.Param().rec,
348 chargeMap,
349 peak,
350 smem.posBcast,
351 smem.buf,
352 smem.innerAboveThreshold,
353 &dummy_pc,
354 labelAcc);
355 }
356 if ((clusterer.mPmemory->fragment).isOverlap(peak.time())) {
357 if (clusterer.mPclusterPosInRow) {
358 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
359 }
360 return;
361 }
362
363 if (dtype == 0) {
364 pc.setFull(central_charge * clustererNN.mOutputDataReg1_16[model_output_index + 4].ToFloat(),
365 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg1_16[model_output_index].ToFloat(),
366 clustererNN.mOutputDataReg1_16[model_output_index + 2].ToFloat(),
367 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg1_16[model_output_index + 1].ToFloat(),
368 clustererNN.mOutputDataReg1_16[model_output_index + 3].ToFloat(),
369 clustererNN.mClusterFlags[2 * glo_idx],
370 clustererNN.mClusterFlags[2 * glo_idx + 1]);
371 } else if (dtype == 1) {
372 pc.setFull(central_charge * clustererNN.mOutputDataReg1_32[model_output_index + 4],
373 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg1_32[model_output_index],
374 clustererNN.mOutputDataReg1_32[model_output_index + 2],
375 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg1_32[model_output_index + 1],
376 clustererNN.mOutputDataReg1_32[model_output_index + 3],
377 clustererNN.mClusterFlags[2 * glo_idx],
378 clustererNN.mClusterFlags[2 * glo_idx + 1]);
379 }
380
381 tpc::ClusterNative myCluster;
382 bool rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
383 if (rejectCluster) {
384 if (clusterer.mPclusterPosInRow) {
385 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
386 }
387 return;
388 }
389
390 uint32_t rowIndex = 0;
391 if (clusterOut != nullptr) {
392 rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
393 clusterer,
394 myCluster,
395 peak.row(),
396 clusterer.mNMaxClusterPerRow,
397 clusterer.mPclusterInRow,
398 clusterOut);
399 if (clusterer.mPclusterPosInRow != nullptr) {
400 clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
401 }
402 } else if (clusterer.mPclusterPosInRow) {
403 rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
404 }
405 CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow));
406 } else {
407 if (clusterer.mPclusterPosInRow) {
408 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
409 }
410 return;
411 }
412}
413
414template <>
415GPUdii() 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)
416{
417 uint32_t glo_idx = get_global_id(0);
418 auto& clusterer = processors.tpcClusterer[sector];
419 auto& clustererNN = processors.tpcNNClusterer[sector];
420
421 uint32_t maxClusterNum = clusterer.mPmemory->counters.nClusters;
422 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
423 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(glo_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
424 float central_charge = static_cast<float>(chargeMap[peak].unpack());
425
426 CPU_ONLY(MCLabelAccumulator labelAccElem(clusterer));
427 MCLabelAccumulator* labelAcc = CPU_PTR(&labelAccElem);
428 tpc::ClusterNative* clusterOut = (withMC) ? nullptr : clusterer.mPclusterByRow;
429 uint32_t full_glo_idx = glo_idx + batchStart;
430
431 if (full_glo_idx >= maxClusterNum) {
432 if (withMC) {
433 ClusterAccumulator dummy_pc;
434 CPU_ONLY(labelAcc->collect(peak, central_charge));
435 GPUTPCCFClusterizer::buildCluster(
436 clusterer.Param().rec,
437 chargeMap,
438 peak,
439 smem.posBcast,
440 smem.buf,
441 smem.innerAboveThreshold,
442 &dummy_pc,
443 labelAcc);
444 }
445 return;
446 }
447
448 uint32_t model_output_index = glo_idx * clustererNN.mNnClusterizerModelReg2NumOutputNodes;
449
450 if ((clustererNN.mOutputDataClass[full_glo_idx] > 0) || (clustererNN.mNnClusterizerUseClassification <= 0)) {
451
453
454 if (withMC) {
455 ClusterAccumulator dummy_pc;
456 CPU_ONLY(labelAcc->collect(peak, central_charge));
457 GPUTPCCFClusterizer::buildCluster(
458 clusterer.Param().rec,
459 chargeMap,
460 peak,
461 smem.posBcast,
462 smem.buf,
463 smem.innerAboveThreshold,
464 &dummy_pc,
465 labelAcc);
466 }
467 if ((clusterer.mPmemory->fragment).isOverlap(peak.time())) {
468 if (clusterer.mPclusterPosInRow) {
469 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
470 }
471 return;
472 }
473
474 // Cluster 1
475 if (dtype == 0) {
476 pc.setFull(central_charge * clustererNN.mOutputDataReg2_16[model_output_index + 8].ToFloat(),
477 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_16[model_output_index].ToFloat(),
478 clustererNN.mOutputDataReg2_16[model_output_index + 4].ToFloat(),
479 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_16[model_output_index + 2].ToFloat(),
480 clustererNN.mOutputDataReg2_16[model_output_index + 6].ToFloat(),
481 clustererNN.mClusterFlags[2 * glo_idx],
482 clustererNN.mClusterFlags[2 * glo_idx + 1]);
483 } else if (dtype == 1) {
484 pc.setFull(central_charge * clustererNN.mOutputDataReg2_32[model_output_index + 8],
485 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_32[model_output_index],
486 clustererNN.mOutputDataReg2_32[model_output_index + 4],
487 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_32[model_output_index + 2],
488 clustererNN.mOutputDataReg2_32[model_output_index + 6],
489 clustererNN.mClusterFlags[2 * glo_idx],
490 clustererNN.mClusterFlags[2 * glo_idx + 1]);
491 }
492
493 tpc::ClusterNative myCluster;
494 bool rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
495 if (rejectCluster) {
496 if (clusterer.mPclusterPosInRow) {
497 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
498 }
499 return;
500 }
501
502 uint32_t rowIndex = 0;
503 if (clusterOut != nullptr) {
504 rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
505 clusterer,
506 myCluster,
507 peak.row(),
508 clusterer.mNMaxClusterPerRow,
509 clusterer.mPclusterInRow,
510 clusterOut);
511 if (clusterer.mPclusterPosInRow != nullptr) {
512 clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
513 }
514 } else if (clusterer.mPclusterPosInRow) {
515 rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
516 }
517 CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow));
518
519 // Cluster 2
520 if (dtype == 0) {
521 pc.setFull(central_charge * clustererNN.mOutputDataReg2_16[model_output_index + 9].ToFloat(),
522 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_16[model_output_index + 1].ToFloat(),
523 clustererNN.mOutputDataReg2_16[model_output_index + 5].ToFloat(),
524 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_16[model_output_index + 3].ToFloat(),
525 clustererNN.mOutputDataReg2_16[model_output_index + 7].ToFloat(),
526 clustererNN.mClusterFlags[2 * glo_idx],
527 clustererNN.mClusterFlags[2 * glo_idx + 1]);
528 } else if (dtype == 1) {
529 pc.setFull(central_charge * clustererNN.mOutputDataReg2_32[model_output_index + 9],
530 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_32[model_output_index + 1],
531 clustererNN.mOutputDataReg2_32[model_output_index + 5],
532 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_32[model_output_index + 3],
533 clustererNN.mOutputDataReg2_32[model_output_index + 7],
534 clustererNN.mClusterFlags[2 * glo_idx],
535 clustererNN.mClusterFlags[2 * glo_idx + 1]);
536 }
537
538 rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
539 if (rejectCluster) {
540 if (clusterer.mPclusterPosInRow) {
541 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
542 }
543 return;
544 }
545
546 if (clusterOut != nullptr) {
547 rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
548 clusterer,
549 myCluster,
550 peak.row(),
551 clusterer.mNMaxClusterPerRow,
552 clusterer.mPclusterInRow,
553 clusterOut);
554 if (clusterer.mPclusterPosInRow != nullptr) {
555 clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
556 }
557 } else if (clusterer.mPclusterPosInRow) {
558 rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
559 }
560 // CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow)); // -> Is this needed? How to handle MC labels for split clusters?
561 } else {
562 if (clusterer.mPclusterPosInRow) {
563 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
564 }
565 return;
566 }
567}
568
569// ---------------------------------
570template <>
571GPUdii() 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)
572{
573 // Implements identical publishing logic as the heuristic clusterizer and deconvolution kernel
574 uint32_t idx = get_global_id(0);
575 auto& clusterer = processors.tpcClusterer[sector];
576 auto& clustererNN = processors.tpcNNClusterer[sector];
577 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
578 CfChargePos peak = clusterer.mPfilteredPeakPositions[idx + batchStart];
579
580 clustererNN.mClusterFlags[2 * idx] = 0;
581 clustererNN.mClusterFlags[2 * idx + 1] = 0;
582 for (int i = 0; i < 8; i++) {
583 Delta2 d = cfconsts::InnerNeighbors[i];
584 CfChargePos tmp_pos = peak.delta(d);
585 PackedCharge charge = chargeMap[tmp_pos];
586 clustererNN.mClusterFlags[2 * idx] += (d.y != 0 && charge.isSplit());
587 clustererNN.mClusterFlags[2 * idx + 1] += (d.x != 0 && charge.isSplit());
588 }
589 for (int i = 0; i < 16; i++) {
590 Delta2 d = cfconsts::OuterNeighbors[i];
591 CfChargePos tmp_pos = peak.delta(d);
592 PackedCharge charge = chargeMap[tmp_pos];
593 clustererNN.mClusterFlags[2 * idx] += (d.y != 0 && charge.isSplit() && !charge.has3x3Peak());
594 clustererNN.mClusterFlags[2 * idx + 1] += (d.x != 0 && charge.isSplit() && !charge.has3x3Peak());
595 }
596}
597
598// THe following arithmetic is done because the network is trained with a split between IROC and OROC boundary
599GPUd() int32_t GPUTPCNNClusterizerKernels::padOffset(int32_t row_ref, int32_t row_current)
600{
601 if (row_current < 0 || row_current >= o2::tpc::constants::MAXGLOBALPADROW) {
602 return 0; // Short-circuit for negative rows
603 } else {
604 return (int)((GPUTPCGeometry::NPads(row_current) - GPUTPCGeometry::NPads(row_ref)) / 2);
605 }
606}
607
608GPUd() int32_t GPUTPCNNClusterizerKernels::rowOffset(int32_t row, int32_t offset)
609{
610 return (row > 62 ? offset : 0);
611}
612
613GPUd() bool GPUTPCNNClusterizerKernels::isBoundary(int32_t row, int32_t pad, int32_t offset)
614{
615 if (pad < 0 || row < 0) { // Faster short-circuit
616 return true;
617 } else if (row < 63) {
618 return ((pad < 0) || (pad >= static_cast<int>(GPUTPCGeometry::NPads(row))));
619 } 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
620 return true;
622 return ((pad < 0) || (pad >= static_cast<int>(GPUTPCGeometry::NPads(row - offset))));
623 } else {
624 return true;
625 }
626}
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 MAXSECTOR
Definition Constants.h:28
constexpr int MAXGLOBALPADROW
Definition Constants.h:34
int16_t y
int16_t x
std::vector< int > row