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::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)
55{
56 uint32_t glo_idx = get_global_id(0);
57 auto& clusterer = processors.tpcClusterer[sector];
58 auto& clustererNN = processors.tpcNNClusterer[sector];
59 uint32_t write_idx = glo_idx * clustererNN.mNnClusterizerElementSize; // Potential optimization: Either choose mNnClusterizerBatchedMode as a power of 2 or calculate from threadId and blockId
60
61 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
62 CfArray2D<uint8_t> isPeakMap(clusterer.mPpeakMap);
63 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(glo_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
64 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
65 float central_charge = static_cast<float>(chargeMap[peak].unpack());
66 int32_t row_offset = GPUTPCNNClusterizerKernels::rowOffset(row, clustererNN.mNnClusterizerSizeInputRow);
67
68#ifndef GPUCA_GPUCODE
69 GPUCA_UNROLL(U(), U());
70#endif
71 for (int32_t r = -clustererNN.mNnClusterizerSizeInputRow; r <= clustererNN.mNnClusterizerSizeInputRow; r++) {
72 bool is_row_boundary = ((row + r) > (o2::tpc::constants::MAXGLOBALPADROW - 1)) || ((row + r) < 0);
73 int32_t pad_offset = is_row_boundary ? 0 : GPUTPCNNClusterizerKernels::padOffset(row, row + r);
74 for (int32_t p = -clustererNN.mNnClusterizerSizeInputPad + pad_offset; p <= clustererNN.mNnClusterizerSizeInputPad + pad_offset; p++) {
75 bool is_boundary = is_row_boundary || GPUTPCNNClusterizerKernels::isBoundary(row + r + row_offset, pad + p, clustererNN.mNnClusterizerSizeInputRow);
76 for (int32_t t = -clustererNN.mNnClusterizerSizeInputTime; t <= clustererNN.mNnClusterizerSizeInputTime; t++) {
77 if (!is_boundary) {
78 CfChargePos tmp_pos(row + r, pad + p, time + t);
79 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
80 clustererNN.mClusterFlags[2 * glo_idx] += CfUtils::isPeak(isPeakMap[tmp_pos]);
81 clustererNN.mClusterFlags[2 * glo_idx + 1] = clustererNN.mClusterFlags[2 * glo_idx];
82 }
83 if (dtype == 0) {
84 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)(static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge);
85 } else if (dtype == 1) {
86 clustererNN.mInputData_32[write_idx] = static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge;
87 }
88 } else {
89 // Filling boundary just to make sure that no values are left unintentionally
90 if (dtype == 0) {
91 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)(static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue));
92 } else {
93 clustererNN.mInputData_32[write_idx] = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
94 }
95 }
96 write_idx++;
97 }
98 }
99 }
100 if (clustererNN.mNnClusterizerAddIndexData) {
101 if (dtype == 0) {
102 clustererNN.mInputData_16[write_idx] = (OrtDataType::Float16_t)(sector / 36.f);
103 clustererNN.mInputData_16[write_idx + 1] = (OrtDataType::Float16_t)(row / 152.f);
104 clustererNN.mInputData_16[write_idx + 2] = (OrtDataType::Float16_t)(static_cast<float>(pad) / GPUTPCGeometry::NPads(row));
105 } else {
106 clustererNN.mInputData_32[write_idx] = sector / 36.f;
107 clustererNN.mInputData_32[write_idx + 1] = row / 152.f;
108 clustererNN.mInputData_32[write_idx + 2] = static_cast<float>(pad) / GPUTPCGeometry::NPads(row);
109 }
110 }
111}
112
113template <>
114GPUdii() 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)
115{
116 uint32_t glo_idx = get_global_id(0);
117 auto& clusterer = processors.tpcClusterer[sector];
118 auto& clustererNN = processors.tpcNNClusterer[sector];
119 uint32_t base_idx = CAMath::Floor(glo_idx / clustererNN.mNnClusterizerElementSize);
120 uint32_t transient_index = glo_idx - (base_idx * clustererNN.mNnClusterizerElementSize);
121
122 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
123 CfArray2D<uint8_t> isPeakMap(clusterer.mPpeakMap);
124 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(base_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
125 int32_t row = static_cast<int>(peak.row()), pad = static_cast<int>(peak.pad());
126
127 if (clustererNN.mNnClusterizerAddIndexData && (int32_t)transient_index == (clustererNN.mNnClusterizerElementSize - 1)) {
128 uint32_t top_idx = (base_idx + 1) * clustererNN.mNnClusterizerElementSize;
129 if (!clustererNN.mNnClusterizerSetDeconvolutionFlags) { // Only if deconvolution flags are not set
130 clustererNN.mClusterFlags[2 * base_idx] = 0;
131 clustererNN.mClusterFlags[2 * base_idx + 1] = 0;
132 for (uint16_t i = 0; i < 8; i++) { // This solution needs testing. It is not the same as the deconvolution flags
133 Delta2 d = cfconsts::InnerNeighbors[i];
134 CfChargePos tmp_pos = peak.delta(d);
135 clustererNN.mClusterFlags[2 * base_idx] += CfUtils::isPeak(isPeakMap[tmp_pos]);
136 }
137 clustererNN.mClusterFlags[2 * base_idx + 1] = clustererNN.mClusterFlags[2 * base_idx];
138 }
139 if (dtype == 0) {
140 clustererNN.mInputData_16[top_idx - 3] = (OrtDataType::Float16_t)(sector / 36.f);
141 clustererNN.mInputData_16[top_idx - 2] = (OrtDataType::Float16_t)(row / 152.f);
142 clustererNN.mInputData_16[top_idx - 1] = (OrtDataType::Float16_t)(static_cast<float>(pad) / GPUTPCGeometry::NPads(row));
143 } else {
144 clustererNN.mInputData_32[top_idx - 3] = sector / 36.f;
145 clustererNN.mInputData_32[top_idx - 2] = row / 152.f;
146 clustererNN.mInputData_32[top_idx - 1] = static_cast<float>(pad) / GPUTPCGeometry::NPads(row);
147 }
148 } else if ((int32_t)transient_index < (clustererNN.mNnClusterizerElementSize - 3)) {
149 int32_t time = static_cast<int>(peak.time());
150 int32_t r = CAMath::Floor(transient_index / ((2 * clustererNN.mNnClusterizerSizeInputPad + 1) * (2 * clustererNN.mNnClusterizerSizeInputTime + 1))) - clustererNN.mNnClusterizerSizeInputRow;
151 bool is_row_boundary = ((row + r) > (o2::tpc::constants::MAXGLOBALPADROW - 1)) || ((row + r) < 0);
152 if (is_row_boundary) {
153 if (dtype == 0) {
154 clustererNN.mInputData_16[glo_idx] = (OrtDataType::Float16_t)(static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue));
155 } else {
156 clustererNN.mInputData_32[glo_idx] = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
157 }
158 } else {
159 int32_t row_offset = GPUTPCNNClusterizerKernels::rowOffset(row, clustererNN.mNnClusterizerSizeInputRow);
160 int32_t pad_offset = GPUTPCNNClusterizerKernels::padOffset(row, row + r);
161 int32_t rest_1 = transient_index % ((2 * clustererNN.mNnClusterizerSizeInputPad + 1) * (2 * clustererNN.mNnClusterizerSizeInputTime + 1));
162 int32_t p = CAMath::Floor(rest_1 / (2 * clustererNN.mNnClusterizerSizeInputTime + 1)) - clustererNN.mNnClusterizerSizeInputPad + pad_offset;
163 int32_t time_pos = (rest_1 % (2 * clustererNN.mNnClusterizerSizeInputTime + 1)) - clustererNN.mNnClusterizerSizeInputTime + time;
164
165 bool is_boundary = GPUTPCNNClusterizerKernels::isBoundary(row + r + row_offset, pad + p, clustererNN.mNnClusterizerSizeInputRow) && (time_pos < 0 || time_pos >= TPC_MAX_FRAGMENT_LEN_GPU);
166
167 if (!is_boundary) {
168 float central_charge = static_cast<float>(chargeMap[peak].unpack());
169 CfChargePos tmp_pos(row + r, pad + p, time_pos);
170 if (dtype == 0) {
171 clustererNN.mInputData_16[glo_idx] = (OrtDataType::Float16_t)(static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge);
172 } else if (dtype == 1) {
173 clustererNN.mInputData_32[glo_idx] = static_cast<float>(chargeMap[tmp_pos].unpack()) / central_charge;
174 }
175 } else {
176 if (dtype == 0) {
177 clustererNN.mInputData_16[glo_idx] = (OrtDataType::Float16_t)(static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue));
178 } else {
179 clustererNN.mInputData_32[glo_idx] = static_cast<float>(clustererNN.mNnClusterizerBoundaryFillValue);
180 }
181 }
182 }
183 }
184}
185
186template <>
187GPUdii() 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)
188{
189 uint32_t glo_idx = get_global_id(0);
190 if (dtype == 0) {
191 processors.tpcNNClusterer[sector].mOutputDataClass[glo_idx + batchStart] = (int)((processors.tpcNNClusterer[sector].mModelProbabilities_16[glo_idx]).ToFloat() > processors.tpcNNClusterer[sector].mNnClassThreshold);
192 } else if (dtype == 1) {
193 processors.tpcNNClusterer[sector].mOutputDataClass[glo_idx + batchStart] = (int)(processors.tpcNNClusterer[sector].mModelProbabilities_32[glo_idx] > processors.tpcNNClusterer[sector].mNnClassThreshold);
194 }
195}
196
197template <>
198GPUdii() 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)
199{
200 auto& clustererNN = processors.tpcNNClusterer[sector];
201 uint32_t glo_idx = get_global_id(0);
202 uint32_t elem_iterator = glo_idx * clustererNN.mNnClusterizerModelClassNumOutputNodes;
203 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]
204 uint32_t class_label = 0;
205 for (uint32_t pIdx = elem_iterator; pIdx < elem_iterator + clustererNN.mNnClusterizerModelClassNumOutputNodes; pIdx++) {
206 if (pIdx == elem_iterator) {
207 if (dtype == 0) {
208 current_max_prob = static_cast<float>(clustererNN.mModelProbabilities_16[pIdx]);
209 } else if (dtype == 1) {
210 current_max_prob = clustererNN.mModelProbabilities_32[pIdx];
211 }
212 } else {
213 if (dtype == 0) {
214 current_max_prob = CAMath::Max(current_max_prob, clustererNN.mModelProbabilities_16[pIdx].ToFloat());
215 } else if (dtype == 1) {
216 current_max_prob = CAMath::Max(current_max_prob, clustererNN.mModelProbabilities_32[pIdx]);
217 }
218 }
219 }
220 // 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"
221 clustererNN.mOutputDataClass[glo_idx + batchStart] = class_label;
222 if (class_label > 1) {
223 clustererNN.mClusterFlags[2 * glo_idx] = 1;
224 clustererNN.mClusterFlags[2 * glo_idx + 1] = 1;
225 }
226}
227
228template <>
229GPUdii() 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)
230{
231 uint32_t glo_idx = get_global_id(0);
232 auto& clusterer = processors.tpcClusterer[sector];
233 auto& clustererNN = processors.tpcNNClusterer[sector];
234
235 uint32_t maxClusterNum = clusterer.mPmemory->counters.nClusters;
236 uint32_t full_glo_idx = glo_idx + batchStart;
237 if (full_glo_idx >= maxClusterNum) {
238 return;
239 }
240 int32_t model_output_index = glo_idx * clustererNN.mNnClusterizerModelReg1NumOutputNodes;
241
242 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
243 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(full_glo_idx, maxClusterNum - 1)];
244 float central_charge = static_cast<float>(chargeMap[peak].unpack());
245
246 CPU_ONLY(MCLabelAccumulator labelAccElem(clusterer));
247 MCLabelAccumulator* labelAcc = CPU_PTR(&labelAccElem);
248 tpc::ClusterNative* clusterOut = (withMC) ? nullptr : clusterer.mPclusterByRow;
249
250 // LOG(info) << glo_idx << " -- " << model_output_index << " / " << clustererNN.outputDataReg1.size() << " / " << clustererNN.mNnClusterizerModelReg1NumOutputNodes << " -- " << clusterer.peakPositions.size() << " -- " << clusterer.centralCharges.size();
251
252 if (clustererNN.mOutputDataClass[full_glo_idx] == 1 || (clustererNN.mNnClusterizerModelReg2NumOutputNodes != -1 && clustererNN.mOutputDataClass[full_glo_idx] >= 1)) {
253
255
256 // Publishing logic is taken from default clusterizer
257 if (withMC) {
258 ClusterAccumulator dummy_pc;
259 CPU_ONLY(labelAcc->collect(peak, central_charge));
260 GPUTPCCFClusterizer::buildCluster(
261 clusterer.Param().rec,
262 chargeMap,
263 peak,
264 smem.posBcast,
265 smem.buf,
266 smem.innerAboveThreshold,
267 &dummy_pc,
268 labelAcc);
269 }
270 if ((clusterer.mPmemory->fragment).isOverlap(peak.time())) {
271 if (clusterer.mPclusterPosInRow) {
272 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
273 }
274 return;
275 }
276
277 if (dtype == 0) {
278 pc.setFull(central_charge * clustererNN.mOutputDataReg1_16[model_output_index + 4].ToFloat(),
279 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg1_16[model_output_index].ToFloat(),
280 clustererNN.mOutputDataReg1_16[model_output_index + 2].ToFloat(),
281 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg1_16[model_output_index + 1].ToFloat(),
282 clustererNN.mOutputDataReg1_16[model_output_index + 3].ToFloat(),
283 clustererNN.mClusterFlags[2 * glo_idx],
284 clustererNN.mClusterFlags[2 * glo_idx + 1]);
285 } else if (dtype == 1) {
286 pc.setFull(central_charge * clustererNN.mOutputDataReg1_32[model_output_index + 4],
287 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg1_32[model_output_index],
288 clustererNN.mOutputDataReg1_32[model_output_index + 2],
289 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg1_32[model_output_index + 1],
290 clustererNN.mOutputDataReg1_32[model_output_index + 3],
291 clustererNN.mClusterFlags[2 * glo_idx],
292 clustererNN.mClusterFlags[2 * glo_idx + 1]);
293 }
294
295 tpc::ClusterNative myCluster;
296 bool rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
297 if (rejectCluster) {
298 if (clusterer.mPclusterPosInRow) {
299 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
300 }
301 return;
302 }
303
304 uint32_t rowIndex = 0;
305 if (clusterOut != nullptr) {
306 rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
307 clusterer,
308 myCluster,
309 peak.row(),
310 clusterer.mNMaxClusterPerRow,
311 clusterer.mPclusterInRow,
312 clusterOut);
313 if (clusterer.mPclusterPosInRow != nullptr) {
314 clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
315 }
316 } else if (clusterer.mPclusterPosInRow) {
317 rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
318 }
319 CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow));
320 } else {
321 if (clusterer.mPclusterPosInRow) {
322 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
323 }
324 return;
325 }
326}
327
328template <>
329GPUdii() 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)
330{
331 uint32_t glo_idx = get_global_id(0);
332 auto& clusterer = processors.tpcClusterer[sector];
333 auto& clustererNN = processors.tpcNNClusterer[sector];
334
335 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
336 CfChargePos peak = clusterer.mPfilteredPeakPositions[CAMath::Min(glo_idx + batchStart, (uint32_t)(clusterer.mPmemory->counters.nClusters - 1))];
337 float central_charge = static_cast<float>(chargeMap[peak].unpack());
338
339 CPU_ONLY(MCLabelAccumulator labelAccElem(clusterer));
340 MCLabelAccumulator* labelAcc = CPU_PTR(&labelAccElem);
341 tpc::ClusterNative* clusterOut = (withMC) ? nullptr : clusterer.mPclusterByRow;
342 uint32_t full_glo_idx = glo_idx + batchStart;
343 uint32_t model_output_index = glo_idx * clustererNN.mNnClusterizerModelReg2NumOutputNodes;
344
345 if (clustererNN.mOutputDataClass[full_glo_idx] > 0) {
346
348
349 if (withMC) {
350 ClusterAccumulator dummy_pc;
351 CPU_ONLY(labelAcc->collect(peak, central_charge));
352 GPUTPCCFClusterizer::buildCluster(
353 clusterer.Param().rec,
354 chargeMap,
355 peak,
356 smem.posBcast,
357 smem.buf,
358 smem.innerAboveThreshold,
359 &dummy_pc,
360 labelAcc);
361 }
362 if ((clusterer.mPmemory->fragment).isOverlap(peak.time())) {
363 if (clusterer.mPclusterPosInRow) {
364 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
365 }
366 return;
367 }
368
369 // Cluster 1
370 if (dtype == 0) {
371 pc.setFull(central_charge * clustererNN.mOutputDataReg2_16[model_output_index + 8].ToFloat(),
372 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_16[model_output_index].ToFloat(),
373 clustererNN.mOutputDataReg2_16[model_output_index + 4].ToFloat(),
374 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_16[model_output_index + 2].ToFloat(),
375 clustererNN.mOutputDataReg2_16[model_output_index + 6].ToFloat(),
376 clustererNN.mClusterFlags[2 * glo_idx],
377 clustererNN.mClusterFlags[2 * glo_idx + 1]);
378 } else if (dtype == 1) {
379 pc.setFull(central_charge * clustererNN.mOutputDataReg2_32[model_output_index + 8],
380 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_32[model_output_index],
381 clustererNN.mOutputDataReg2_32[model_output_index + 4],
382 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_32[model_output_index + 2],
383 clustererNN.mOutputDataReg2_32[model_output_index + 6],
384 clustererNN.mClusterFlags[2 * glo_idx],
385 clustererNN.mClusterFlags[2 * glo_idx + 1]);
386 }
387
388 tpc::ClusterNative myCluster;
389 bool rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
390 if (rejectCluster) {
391 if (clusterer.mPclusterPosInRow) {
392 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
393 }
394 return;
395 }
396
397 uint32_t rowIndex = 0;
398 if (clusterOut != nullptr) {
399 rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
400 clusterer,
401 myCluster,
402 peak.row(),
403 clusterer.mNMaxClusterPerRow,
404 clusterer.mPclusterInRow,
405 clusterOut);
406 if (clusterer.mPclusterPosInRow != nullptr) {
407 clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
408 }
409 } else if (clusterer.mPclusterPosInRow) {
410 rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
411 }
412 CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow));
413
414 // Cluster 2
415 if (dtype == 0) {
416 pc.setFull(central_charge * clustererNN.mOutputDataReg2_16[model_output_index + 9].ToFloat(),
417 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_16[model_output_index + 1].ToFloat(),
418 clustererNN.mOutputDataReg2_16[model_output_index + 5].ToFloat(),
419 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_16[model_output_index + 3].ToFloat(),
420 clustererNN.mOutputDataReg2_16[model_output_index + 7].ToFloat(),
421 clustererNN.mClusterFlags[2 * glo_idx],
422 clustererNN.mClusterFlags[2 * glo_idx + 1]);
423 } else if (dtype == 1) {
424 pc.setFull(central_charge * clustererNN.mOutputDataReg2_32[model_output_index + 9],
425 static_cast<float>(peak.pad()) + clustererNN.mOutputDataReg2_32[model_output_index + 1],
426 clustererNN.mOutputDataReg2_32[model_output_index + 5],
427 (clusterer.mPmemory->fragment).start + static_cast<float>(peak.time()) + clustererNN.mOutputDataReg2_32[model_output_index + 3],
428 clustererNN.mOutputDataReg2_32[model_output_index + 7],
429 clustererNN.mClusterFlags[2 * glo_idx],
430 clustererNN.mClusterFlags[2 * glo_idx + 1]);
431 }
432
433 rejectCluster = !pc.toNative(peak, central_charge, myCluster, clusterer.Param(), chargeMap);
434 if (rejectCluster) {
435 if (clusterer.mPclusterPosInRow) {
436 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
437 }
438 return;
439 }
440
441 if (clusterOut != nullptr) {
442 rowIndex = GPUTPCCFClusterizer::sortIntoBuckets(
443 clusterer,
444 myCluster,
445 peak.row(),
446 clusterer.mNMaxClusterPerRow,
447 clusterer.mPclusterInRow,
448 clusterOut);
449 if (clusterer.mPclusterPosInRow != nullptr) {
450 clusterer.mPclusterPosInRow[full_glo_idx] = rowIndex;
451 }
452 } else if (clusterer.mPclusterPosInRow) {
453 rowIndex = clusterer.mPclusterPosInRow[full_glo_idx];
454 }
455 // CPU_ONLY(labelAcc->commit(peak.row(), rowIndex, clusterer.mNMaxClusterPerRow)); // -> Is this needed? How to handle MC labels for split clusters?
456 } else {
457 if (clusterer.mPclusterPosInRow) {
458 clusterer.mPclusterPosInRow[full_glo_idx] = clusterer.mNMaxClusterPerRow;
459 }
460 return;
461 }
462}
463
464// ---------------------------------
465template <>
466GPUdii() 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)
467{
468 // Implements identical publishing logic as the heuristic clusterizer and deconvolution kernel
469 uint32_t idx = get_global_id(0);
470 auto& clusterer = processors.tpcClusterer[sector];
471 auto& clustererNN = processors.tpcNNClusterer[sector];
472 CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
473 CfChargePos peak = clusterer.mPfilteredPeakPositions[idx + batchStart];
474
475 clustererNN.mClusterFlags[2 * idx] = 0;
476 clustererNN.mClusterFlags[2 * idx + 1] = 0;
477 for (int i = 0; i < 8; i++) {
478 Delta2 d = cfconsts::InnerNeighbors[i];
479 CfChargePos tmp_pos = peak.delta(d);
480 PackedCharge charge = chargeMap[tmp_pos];
481 clustererNN.mClusterFlags[2 * idx] += (d.y != 0 && charge.isSplit());
482 clustererNN.mClusterFlags[2 * idx + 1] += (d.x != 0 && charge.isSplit());
483 }
484 for (int i = 0; i < 16; i++) {
485 Delta2 d = cfconsts::OuterNeighbors[i];
486 CfChargePos tmp_pos = peak.delta(d);
487 PackedCharge charge = chargeMap[tmp_pos];
488 clustererNN.mClusterFlags[2 * idx] += (d.y != 0 && charge.isSplit() && !charge.has3x3Peak());
489 clustererNN.mClusterFlags[2 * idx + 1] += (d.x != 0 && charge.isSplit() && !charge.has3x3Peak());
490 }
491}
492
493// THe following arithmetic is done because the network is trained with a split between IROC and OROC boundary
494GPUd() int32_t GPUTPCNNClusterizerKernels::padOffset(int32_t row_ref, int32_t row_current)
495{
496 return (int)((GPUTPCGeometry::NPads(row_current) - GPUTPCGeometry::NPads(row_ref)) / 2);
497}
498
499GPUd() int32_t GPUTPCNNClusterizerKernels::rowOffset(int32_t row, int32_t global_shift)
500{
501 return (row > 62 ? global_shift : 0);
502}
503
504GPUd() bool GPUTPCNNClusterizerKernels::isBoundary(int32_t row, int32_t pad, int32_t global_shift)
505{
506 if (pad < 0 || row < 0) { // Faster short-circuit
507 return true;
508 } else if (row < 63) {
509 return (pad >= static_cast<int>(GPUTPCGeometry::NPads(row)));
510 } 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
511 return true;
512 } else if (row < (o2::tpc::constants::MAXGLOBALPADROW + global_shift)) {
513 return (pad >= static_cast<int>(GPUTPCGeometry::NPads(row - global_shift)));
514 } else {
515 return true;
516 }
517}
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)
#define GPUCA_UNROLL(optCu, optHi)
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)
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