forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUTPCCFDecodeZS.cxx
More file actions
767 lines (641 loc) · 31.5 KB
/
GPUTPCCFDecodeZS.cxx
File metadata and controls
767 lines (641 loc) · 31.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// 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 GPUTPCCFDecodeZS.cxx
/// \author David Rohr, Felix Weiglhofer
#include "GPUTPCCFDecodeZS.h"
#include "GPUCommonMath.h"
#include "GPUTPCClusterFinder.h"
#include "CfArray2D.h"
#include "PackedCharge.h"
#include "CfUtils.h"
#include "CommonConstants/LHCConstants.h"
#include "GPUCommonAlgorithm.h"
#include "TPCPadGainCalib.h"
#include "TPCZSLinkMapping.h"
#include "GPUTPCGeometry.h"
#include "DetectorsRaw/RDHUtils.h"
using namespace o2::gpu;
using namespace o2::gpu::tpccf;
using namespace o2::tpc;
using namespace o2::tpc::constants;
// ===========================================================================
// ===========================================================================
// Decode ZS Row
// ===========================================================================
// ===========================================================================
template <>
GPUdii() void GPUTPCCFDecodeZS::Thread<GPUTPCCFDecodeZS::decodeZS>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& clusterer, int32_t firstHBF, int32_t tpcTimeBinCut)
{
GPUTPCCFDecodeZS::decode(clusterer, smem, nBlocks, nThreads, iBlock, iThread, firstHBF, tpcTimeBinCut);
}
GPUdii() void GPUTPCCFDecodeZS::decode(GPUTPCClusterFinder& clusterer, GPUSharedMemory& s, int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t firstHBF, int32_t tpcTimeBinCut)
{
const uint32_t sector = clusterer.mISector;
#ifdef GPUCA_GPUCODE
const uint32_t endpoint = clusterer.mPzsOffsets[iBlock].endpoint;
#else
const uint32_t endpoint = iBlock;
#endif
const GPUTrackingInOutZS::GPUTrackingInOutZSSector& zs = clusterer.GetConstantMem()->ioPtrs.tpcZS->sector[sector];
if (zs.count[endpoint] == 0) {
return;
}
CfChargePos* positions = clusterer.mPpositions;
CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
const size_t nDigits = clusterer.mPzsOffsets[iBlock].offset;
if (iThread == 0) {
const int32_t region = endpoint / 2;
s.nRowsRegion = GPUTPCGeometry::GetRegionRows(region);
s.regionStartRow = GPUTPCGeometry::GetRegionStart(region);
s.nThreadsPerRow = CAMath::Max(1u, nThreads / ((s.nRowsRegion + (endpoint & 1)) / 2));
s.rowStride = nThreads / s.nThreadsPerRow;
s.rowOffsetCounter = 0;
}
GPUbarrier();
const uint32_t myRow = iThread / s.nThreadsPerRow;
const uint32_t mySequence = iThread % s.nThreadsPerRow;
#ifdef GPUCA_GPUCODE
const uint32_t i = 0;
const uint32_t j = clusterer.mPzsOffsets[iBlock].num;
{
{
#else
for (uint32_t i = clusterer.mMinMaxCN[endpoint].zsPtrFirst; i < clusterer.mMinMaxCN[endpoint].zsPtrLast; i++) {
const uint32_t minJ = (i == clusterer.mMinMaxCN[endpoint].zsPtrFirst) ? clusterer.mMinMaxCN[endpoint].zsPageFirst : 0;
const uint32_t maxJ = (i + 1 == clusterer.mMinMaxCN[endpoint].zsPtrLast) ? clusterer.mMinMaxCN[endpoint].zsPageLast : zs.nZSPtr[endpoint][i];
for (uint32_t j = minJ; j < maxJ; j++) {
#endif
const uint32_t* pageSrc = (const uint32_t*)(((const uint8_t*)zs.zsPtr[endpoint][i]) + j * TPCZSHDR::TPC_ZS_PAGE_SIZE);
CA_SHARED_CACHE_REF(&s.ZSPage[0], pageSrc, TPCZSHDR::TPC_ZS_PAGE_SIZE, uint32_t, pageCache);
GPUbarrier();
const uint8_t* page = (const uint8_t*)pageCache;
const o2::header::RAWDataHeader* rdh = (const o2::header::RAWDataHeader*)page;
if (o2::raw::RDHUtils::getMemorySize(*rdh) == sizeof(o2::header::RAWDataHeader)) {
#ifdef GPUCA_GPUCODE
return;
#else
continue;
#endif
}
const uint8_t* pagePtr = page + sizeof(o2::header::RAWDataHeader);
const TPCZSHDR* hdr = reinterpret_cast<const TPCZSHDR*>(pagePtr);
pagePtr += sizeof(*hdr);
const bool decode12bit = hdr->version == 2;
const uint32_t decodeBits = decode12bit ? TPCZSHDR::TPC_ZS_NBITS_V2 : TPCZSHDR::TPC_ZS_NBITS_V1;
const float decodeBitsFactor = 1.f / (1 << (decodeBits - 10));
uint32_t mask = (1 << decodeBits) - 1;
int32_t timeBin = (hdr->timeOffset + (o2::raw::RDHUtils::getHeartBeatOrbit(*rdh) - firstHBF) * o2::constants::lhc::LHCMaxBunches) / LHCBCPERTIMEBIN;
const int32_t rowOffset = s.regionStartRow + ((endpoint & 1) ? (s.nRowsRegion / 2) : 0);
const int32_t nRows = (endpoint & 1) ? (s.nRowsRegion - s.nRowsRegion / 2) : (s.nRowsRegion / 2);
for (int32_t l = 0; l < hdr->nTimeBinSpan; l++) { // TODO: Parallelize over time bins
pagePtr += (pagePtr - page) & 1; // Ensure 16 bit alignment
const TPCZSTBHDR* tbHdr = reinterpret_cast<const TPCZSTBHDR*>(pagePtr);
if ((tbHdr->rowMask & 0x7FFF) == 0) {
pagePtr += 2;
continue;
}
const int32_t nRowsUsed = CAMath::Popcount((uint32_t)(tbHdr->rowMask & 0x7FFF));
pagePtr += 2 * nRowsUsed;
GPUbarrier();
for (int32_t n = iThread; n < nRowsUsed; n += nThreads) {
const uint8_t* rowData = n == 0 ? pagePtr : (page + tbHdr->rowAddr1()[n - 1]);
s.RowClusterOffset[n] = CAMath::AtomicAddShared<uint32_t>(&s.rowOffsetCounter, rowData[2 * *rowData]);
}
/*if (iThread < GPUCA_WARP_SIZE) { // TODO: Seems to miscompile with HIP, CUDA performance doesn't really change, for now sticking to the AtomicAdd
GPUSharedMemory& smem = s;
int32_t o;
if (iThread < nRowsUsed) {
const uint8_t* rowData = iThread == 0 ? pagePtr : (page + tbHdr->rowAddr1()[iThread - 1]);
o = rowData[2 * *rowData];
} else {
o = 0;
}
int32_t x = warp_scan_inclusive_add(o);
if (iThread < nRowsUsed) {
s.RowClusterOffset[iThread] = s.rowOffsetCounter + x - o;
} else if (iThread == GPUCA_WARP_SIZE - 1) {
s.rowOffsetCounter += x;
}
}*/
GPUbarrier();
if (myRow < s.rowStride) {
for (int32_t m = myRow; m < nRows; m += s.rowStride) {
if ((tbHdr->rowMask & (1 << m)) == 0) {
continue;
}
const int32_t rowPos = CAMath::Popcount((uint32_t)(tbHdr->rowMask & ((1 << m) - 1)));
size_t nDigitsTmp = nDigits + s.RowClusterOffset[rowPos];
const uint8_t* rowData = rowPos == 0 ? pagePtr : (page + tbHdr->rowAddr1()[rowPos - 1]);
const int32_t nSeqRead = *rowData;
const int32_t nSeqPerThread = (nSeqRead + s.nThreadsPerRow - 1) / s.nThreadsPerRow;
const int32_t mySequenceStart = mySequence * nSeqPerThread;
const int32_t mySequenceEnd = CAMath::Min(mySequenceStart + nSeqPerThread, nSeqRead);
if (mySequenceEnd > mySequenceStart) {
const uint8_t* adcData = rowData + 2 * nSeqRead + 1;
const uint32_t nSamplesStart = mySequenceStart ? rowData[2 * mySequenceStart] : 0;
nDigitsTmp += nSamplesStart;
uint32_t nADCStartBits = nSamplesStart * decodeBits;
const uint32_t nADCStart = (nADCStartBits + 7) / 8;
const int32_t nADC = (rowData[2 * mySequenceEnd] * decodeBits + 7) / 8;
adcData += nADCStart;
nADCStartBits &= 0x7;
uint32_t byte = 0, bits = 0;
if (nADCStartBits) { // % 8 != 0
bits = 8 - nADCStartBits;
byte = ((*(adcData - 1) & (0xFF ^ ((1 << nADCStartBits) - 1)))) >> nADCStartBits;
}
int32_t nSeq = mySequenceStart;
int32_t seqLen = nSeq ? (rowData[(nSeq + 1) * 2] - rowData[nSeq * 2]) : rowData[2];
Pad pad = rowData[nSeq++ * 2 + 1];
for (int32_t n = nADCStart; n < nADC; n++) {
byte |= *(adcData++) << bits;
bits += 8;
while (bits >= decodeBits) {
if (seqLen == 0) {
seqLen = rowData[(nSeq + 1) * 2] - rowData[nSeq * 2];
pad = rowData[nSeq++ * 2 + 1];
}
const CfFragment& fragment = clusterer.mPmemory->fragment;
TPCTime globalTime = timeBin + l;
bool discardTimeBin = not fragment.contains(globalTime);
discardTimeBin |= (tpcTimeBinCut > 0 && globalTime > tpcTimeBinCut);
Row row = rowOffset + m;
CfChargePos pos(row, Pad(pad), discardTimeBin ? INVALID_TIME_BIN : fragment.toLocal(globalTime));
positions[nDigitsTmp++] = pos;
if (!discardTimeBin) {
float q = float(byte & mask) * decodeBitsFactor;
q *= clusterer.GetConstantMem()->calibObjects.tpcPadGain->getGainCorrection(sector, row, pad);
chargeMap[pos] = PackedCharge(q);
}
pad++;
byte = byte >> decodeBits;
bits -= decodeBits;
seqLen--;
}
}
}
}
}
if (nRowsUsed > 1) {
pagePtr = page + tbHdr->rowAddr1()[nRowsUsed - 2];
}
pagePtr += 2 * *pagePtr; // Go to entry for last sequence length
pagePtr += 1 + (*pagePtr * decodeBits + 7) / 8; // Go to beginning of next time bin
}
}
}
}
// ===========================================================================
// ===========================================================================
// Decode ZS Link
// ===========================================================================
// ===========================================================================
template <>
GPUdii() void GPUTPCCFDecodeZSLink::Thread<0>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& clusterer, int32_t firstHBF, int32_t tpcTimeBinCut)
{
Decode<GPUTPCCFDecodeZSLink>(nBlocks, nThreads, iBlock, iThread, smem, clusterer, firstHBF, tpcTimeBinCut);
}
GPUd() size_t GPUTPCCFDecodeZSLink::DecodePage(GPUSharedMemory& smem, DecodeCtx& ctx)
{
const CfFragment& fragment = ctx.clusterer.mPmemory->fragment;
const auto* rdHdr = ConsumeHeader<header::RAWDataHeader>(ctx.page);
if (o2::raw::RDHUtils::getMemorySize(*rdHdr) == sizeof(o2::header::RAWDataHeader)) {
return ctx.pageDigitOffset;
}
[[maybe_unused]] int32_t nDecoded = 0;
const auto* decHdr = ConsumeHeader<TPCZSHDRV2>(ctx.page);
ConsumeBytes(ctx.page, decHdr->firstZSDataOffset * 16);
assert(decHdr->version == ZSVersionLinkBasedWithMeta);
assert(decHdr->magicWord == o2::tpc::zerosupp_link_based::CommonHeader::MagicWordLinkZSMetaHeader);
for (uint32_t t = 0; t < decHdr->nTimebinHeaders; t++) {
const auto* tbHdr = ConsumeHeader<zerosupp_link_based::CommonHeader>(ctx.page);
const auto* adcData = ConsumeBytes(ctx.page, tbHdr->numWordsPayload * 16); // Page now points to next timebin or past the page
int32_t timeBin = (decHdr->timeOffset + tbHdr->bunchCrossing + (uint64_t)(o2::raw::RDHUtils::getHeartBeatOrbit(*rdHdr) - ctx.firstHBF) * o2::constants::lhc::LHCMaxBunches) / LHCBCPERTIMEBIN;
uint32_t channelMask[3];
GetChannelBitmask(*tbHdr, channelMask);
uint32_t nAdc = CAMath::Popcount(channelMask[0]) + CAMath::Popcount(channelMask[1]) + CAMath::Popcount(channelMask[2]);
nDecoded += nAdc;
bool discardTimeBin = not fragment.contains(timeBin);
discardTimeBin |= (ctx.tpcTimeBinCut > 0 && timeBin > ctx.tpcTimeBinCut);
if (discardTimeBin) {
FillWithInvalid(ctx.clusterer, ctx.iThread, ctx.nThreads, ctx.pageDigitOffset, nAdc);
} else {
DecodeTB(
smem,
ctx,
adcData,
nAdc,
channelMask,
timeBin,
decHdr->cruID,
tbHdr->fecInPartition);
}
ctx.pageDigitOffset += nAdc;
} // for (uint32_t t = 0; t < decHdr->nTimebinHeaders; t++)
#ifdef GPUCA_CHECK_TPCZS_CORRUPTION
if (iThread == 0 && nDecoded != decHdr->nADCsamples) {
clusterer.raiseError(GPUErrors::ERROR_TPCZS_INVALID_NADC, clusterer.mISector * 1000 + decHdr->cruID, decHdr->nADCsamples, nDecoded);
/*#ifndef GPUCA_GPUCODE
FILE* foo = fopen("dump.bin", "w+b");
fwrite(pageSrc, 1, o2::raw::RDHUtils::getMemorySize(*rdHdr), foo);
fclose(foo);
#endif*/
}
#endif
return ctx.pageDigitOffset;
}
GPUd() void GPUTPCCFDecodeZSLink::DecodeTB(
GPUSharedMemory& smem,
DecodeCtx& ctx,
const uint8_t* adcData,
uint32_t nAdc,
const uint32_t* channelMask,
int32_t timeBin,
int32_t cru,
int32_t fecInPartition)
{
constexpr int32_t NTHREADS = GPUCA_GET_THREAD_COUNT(GPUCA_LB_GPUTPCCFDecodeZSLink);
static_assert(NTHREADS == GPUCA_WARP_SIZE, "Decoding TB Headers in parallel assumes block size is a single warp.");
uint8_t blockOffset = 0;
for (uint8_t i = ctx.iThread; blockOffset < nAdc; i += NTHREADS) {
uint8_t rawFECChannel = i;
uint8_t myChannelActive = ChannelIsActive(channelMask, rawFECChannel);
uint8_t myOffset = warp_scan_inclusive_add(myChannelActive) - 1 + blockOffset;
blockOffset = warp_broadcast(myOffset, NTHREADS - 1) + 1;
if (not myChannelActive) {
continue;
}
assert(myOffset < nAdc);
uint32_t adc = 0;
if constexpr (TPCZSHDRV2::TIGHTLY_PACKED_V3) {
uint32_t adcBitOffset = myOffset * DECODE_BITS;
uint32_t adcByteOffset = adcBitOffset / CHAR_BIT;
uint32_t adcOffsetInByte = adcBitOffset - adcByteOffset * CHAR_BIT;
uint32_t byte = 0, bits = 0;
while (bits < DECODE_BITS) {
byte |= ((uint32_t)adcData[adcByteOffset]) << bits;
adcByteOffset++;
bits += CHAR_BIT;
}
adc = byte >> adcOffsetInByte;
} else { // ! TPCZSHDRV2::TIGHTLY_PACKED_V3
const uint64_t* adcData64 = (const uint64_t*)adcData;
adc = (adcData64[myOffset / TPCZSHDRV2::SAMPLESPER64BIT] >> ((myOffset % TPCZSHDRV2::SAMPLESPER64BIT) * DECODE_BITS)) & DECODE_MASK;
}
o2::tpc::PadPos padAndRow = GetPadAndRowFromFEC(ctx.clusterer, cru, rawFECChannel, fecInPartition);
const CfFragment& fragment = ctx.clusterer.mPmemory->fragment;
float charge = ADCToFloat(adc, DECODE_MASK, DECODE_BITS_FACTOR);
WriteCharge(ctx.clusterer, charge, padAndRow, fragment.toLocal(timeBin), ctx.pageDigitOffset + myOffset);
} // for (uint8_t i = iThread; blockOffset < nAdc; i += NThreads)
}
GPUd() void GPUTPCCFDecodeZSLink::GetChannelBitmask(const zerosupp_link_based::CommonHeader& tbHdr, uint32_t* chan)
{
chan[0] = tbHdr.bitMaskLow & 0xfffffffful;
chan[1] = tbHdr.bitMaskLow >> (sizeof(uint32_t) * CHAR_BIT);
chan[2] = tbHdr.bitMaskHigh;
}
GPUd() bool GPUTPCCFDecodeZSLink::ChannelIsActive(const uint32_t* chan, uint8_t chanIndex)
{
if (chanIndex >= zerosupp_link_based::ChannelPerTBHeader) {
return false;
}
constexpr uint8_t N_BITS_PER_ENTRY = sizeof(*chan) * CHAR_BIT;
const uint8_t entryIndex = chanIndex / N_BITS_PER_ENTRY;
const uint8_t bitInEntry = chanIndex % N_BITS_PER_ENTRY;
return chan[entryIndex] & (1 << bitInEntry);
}
// ===========================================================================
// ===========================================================================
// Decode ZS Link Base
// ===========================================================================
// ===========================================================================
template <class Decoder>
GPUd() void GPUTPCCFDecodeZSLinkBase::Decode(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, typename Decoder::GPUSharedMemory& smem, processorType& clusterer, int32_t firstHBF, int32_t tpcTimeBinCut)
{
const uint32_t sector = clusterer.mISector;
#ifdef GPUCA_GPUCODE
const uint32_t endpoint = clusterer.mPzsOffsets[iBlock].endpoint;
#else // CPU
const uint32_t endpoint = iBlock;
#endif
const GPUTrackingInOutZS::GPUTrackingInOutZSSector& zs = clusterer.GetConstantMem()->ioPtrs.tpcZS->sector[sector];
if (zs.count[endpoint] == 0) {
return;
}
uint32_t pageDigitOffset = clusterer.mPzsOffsets[iBlock].offset;
#ifdef GPUCA_GPUCODE
const uint32_t i = 0;
const uint32_t j = clusterer.mPzsOffsets[iBlock].num;
{
{
#else // CPU
for (uint32_t i = clusterer.mMinMaxCN[endpoint].zsPtrFirst; i < clusterer.mMinMaxCN[endpoint].zsPtrLast; i++) {
const uint32_t minJ = (i == clusterer.mMinMaxCN[endpoint].zsPtrFirst) ? clusterer.mMinMaxCN[endpoint].zsPageFirst : 0;
const uint32_t maxJ = (i + 1 == clusterer.mMinMaxCN[endpoint].zsPtrLast) ? clusterer.mMinMaxCN[endpoint].zsPageLast : zs.nZSPtr[endpoint][i];
for (uint32_t j = minJ; j < maxJ; j++) {
#endif
const uint32_t* pageSrc = (const uint32_t*)(((const uint8_t*)zs.zsPtr[endpoint][i]) + j * TPCZSHDR::TPC_ZS_PAGE_SIZE);
// Cache zs page in shared memory. Curiously this actually degrades performance...
// CA_SHARED_CACHE_REF(&smem.ZSPage[0], pageSrc, TPCZSHDR::TPC_ZS_PAGE_SIZE, uint32_t, pageCache);
// GPUbarrier();
// const uint8_t* page = (const uint8_t*)pageCache;
const uint8_t* page = (const uint8_t*)pageSrc;
const auto* rdHdr = Peek<header::RAWDataHeader>(page);
if (o2::raw::RDHUtils::getMemorySize(*rdHdr) == sizeof(o2::header::RAWDataHeader)) {
#ifdef GPUCA_GPUCODE
return;
#else
continue;
#endif
}
DecodeCtx ctx{
.clusterer = clusterer,
.page = page,
.iBlock = iBlock,
.nThreads = nThreads,
.iThread = iThread,
.pageDigitOffset = pageDigitOffset,
.firstHBF = firstHBF,
.tpcTimeBinCut = tpcTimeBinCut,
};
pageDigitOffset = Decoder::DecodePage(smem, ctx);
} // [CPU] for (uint32_t j = minJ; j < maxJ; j++)
} // [CPU] for (uint32_t i = clusterer.mMinMaxCN[endpoint].zsPtrFirst; i < clusterer.mMinMaxCN[endpoint].zsPtrLast; i++)
#ifdef GPUCA_CHECK_TPCZS_CORRUPTION
if (iThread == 0 && iBlock < nBlocks - 1) {
uint32_t maxOffset = clusterer.mPzsOffsets[iBlock + 1].offset;
if (pageDigitOffset != maxOffset) {
clusterer.raiseError(GPUErrors::ERROR_TPCZS_INVALID_OFFSET, clusterer.mISector * 1000 + endpoint, pageDigitOffset, maxOffset);
}
}
#endif
}
GPUd() o2::tpc::PadPos GPUTPCCFDecodeZSLinkBase::GetPadAndRowFromFEC(processorType& clusterer, int32_t cru, int32_t rawFECChannel, int32_t fecInPartition)
{
#ifdef GPUCA_TPC_GEOMETRY_O2
// Ported from tpc::Mapper (Not available on GPU...)
constexpr GPUTPCGeometry geo;
const int32_t regionIter = cru % 2;
const int32_t istreamm = ((rawFECChannel % 10) / 2);
const int32_t partitionStream = istreamm + regionIter * 5;
const int32_t sampaOnFEC = geo.GetSampaMapping(partitionStream);
const int32_t channel = (rawFECChannel % 2) + 2 * (rawFECChannel / 10);
const int32_t channelOnSAMPA = channel + geo.GetChannelOffset(partitionStream);
const int32_t partition = (cru % 10) / 2;
const int32_t fecInSector = geo.GetSectorFECOffset(partition) + fecInPartition;
const TPCZSLinkMapping* gpuMapping = clusterer.GetConstantMem()->calibObjects.tpcZSLinkMapping;
assert(gpuMapping != nullptr);
uint16_t globalSAMPAId = (static_cast<uint16_t>(fecInSector) << 8) + (static_cast<uint16_t>(sampaOnFEC) << 5) + static_cast<uint16_t>(channelOnSAMPA);
const o2::tpc::PadPos pos = gpuMapping->FECIDToPadPos[globalSAMPAId];
return pos;
#else
return o2::tpc::PadPos{};
#endif
}
GPUd() void GPUTPCCFDecodeZSLinkBase::WriteCharge(processorType& clusterer, float charge, PadPos padAndRow, TPCFragmentTime localTime, size_t positionOffset)
{
const uint32_t sector = clusterer.mISector;
CfChargePos* positions = clusterer.mPpositions;
#ifdef GPUCA_CHECK_TPCZS_CORRUPTION
if (padAndRow.getRow() >= GPUCA_ROW_COUNT) {
positions[positionOffset] = INVALID_CHARGE_POS;
clusterer.raiseError(GPUErrors::ERROR_TPCZS_INVALID_ROW, clusterer.mISector * 1000 + padAndRow.getRow());
return;
}
#endif
CfArray2D<PackedCharge> chargeMap(reinterpret_cast<PackedCharge*>(clusterer.mPchargeMap));
CfChargePos pos(padAndRow.getRow(), padAndRow.getPad(), localTime);
positions[positionOffset] = pos;
charge *= clusterer.GetConstantMem()->calibObjects.tpcPadGain->getGainCorrection(sector, padAndRow.getRow(), padAndRow.getPad());
chargeMap[pos] = PackedCharge(charge);
}
GPUd() uint16_t GPUTPCCFDecodeZSLinkBase::FillWithInvalid(processorType& clusterer, int32_t iThread, int32_t nThreads, uint32_t pageDigitOffset, uint16_t nSamples)
{
for (uint16_t i = iThread; i < nSamples; i += nThreads) {
clusterer.mPpositions[pageDigitOffset + i] = INVALID_CHARGE_POS;
}
return nSamples;
}
// ===========================================================================
// ===========================================================================
// Decode ZS Dense Link
// ===========================================================================
// ===========================================================================
template <>
GPUd() void GPUTPCCFDecodeZSDenseLink::Thread<0>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& clusterer, int32_t firstHBF, int32_t tpcTimeBinCut)
{
Decode<GPUTPCCFDecodeZSDenseLink>(nBlocks, nThreads, iBlock, iThread, smem, clusterer, firstHBF, tpcTimeBinCut);
}
GPUd() uint32_t GPUTPCCFDecodeZSDenseLink::DecodePage(GPUSharedMemory& smem, DecodeCtx& ctx)
{
const uint8_t* const pageStart = ctx.page;
const auto* rawDataHeader = Peek<header::RAWDataHeader>(ctx.page);
const auto* decHeader = Peek<TPCZSHDRV2>(ctx.page, raw::RDHUtils::getMemorySize(*rawDataHeader) - sizeof(TPCZSHDRV2));
ConsumeHeader<header::RAWDataHeader>(ctx.page);
uint16_t nSamplesWritten = 0;
const uint16_t nSamplesInPage = decHeader->nADCsamples;
const auto* payloadEnd = Peek(pageStart, raw::RDHUtils::getMemorySize(*rawDataHeader) - sizeof(TPCZSHDRV2) - ((decHeader->flags & TPCZSHDRV2::ZSFlags::TriggerWordPresent) ? TPCZSHDRV2::TRIGGER_WORD_SIZE : 0));
const auto* nextPage = Peek(pageStart, TPCZSHDR::TPC_ZS_PAGE_SIZE);
const bool extendsToNextPage = decHeader->flags & TPCZSHDRV2::ZSFlags::payloadExtendsToNextPage;
ConsumeBytes(ctx.page, decHeader->firstZSDataOffset - sizeof(o2::header::RAWDataHeader));
int err = GPUErrors::ERROR_NONE;
if (decHeader->version < ZSVersionDenseLinkBased) {
err = GPUErrors::ERROR_TPCZS_VERSION_MISMATCH;
}
if (decHeader->magicWord != zerosupp_link_based::CommonHeader::MagicWordLinkZSMetaHeader) {
err = GPUErrors::ERROR_TPCZS_INVALID_MAGIC_WORD;
}
for (uint16_t i = 0; i < decHeader->nTimebinHeaders && !err; i++) {
ptrdiff_t sizeLeftInPage = payloadEnd - ctx.page;
if (sizeLeftInPage <= 0) {
err = GPUErrors::ERROR_TPCZS_PAGE_OVERFLOW;
break;
}
int16_t nSamplesWrittenTB = 0;
uint16_t nSamplesLeftInPage = nSamplesInPage - nSamplesWritten;
if (i == decHeader->nTimebinHeaders - 1 && extendsToNextPage) {
if (raw::RDHUtils::getMemorySize(*rawDataHeader) != TPCZSHDR::TPC_ZS_PAGE_SIZE) {
err = GPUErrors::ERROR_TPCZS_PAGE_OVERFLOW;
break;
}
if ((uint16_t)(raw::RDHUtils::getPageCounter(rawDataHeader) + 1) == raw::RDHUtils::getPageCounter(nextPage)) {
nSamplesWrittenTB = DecodeTB<true>(smem, ctx, rawDataHeader, decHeader->cruID, nSamplesLeftInPage, payloadEnd, nextPage);
} else {
err = GPUErrors::ERROR_TPCZS_INCOMPLETE_HBF;
break;
}
} else {
nSamplesWrittenTB = DecodeTB<false>(smem, ctx, rawDataHeader, decHeader->cruID, nSamplesLeftInPage, payloadEnd, nextPage);
}
// Abort decoding the page if an error was detected.
if (nSamplesWrittenTB < 0) {
err = -nSamplesWrittenTB;
break;
}
nSamplesWritten += nSamplesWrittenTB;
ctx.pageDigitOffset += nSamplesWrittenTB;
} // for (uint16_t i = 0; i < decHeader->nTimebinHeaders; i++)
if (nSamplesWritten != nSamplesInPage) {
if (nSamplesWritten < nSamplesInPage) {
ctx.pageDigitOffset += FillWithInvalid(ctx.clusterer, ctx.iThread, ctx.nThreads, ctx.pageDigitOffset, nSamplesInPage - nSamplesWritten);
}
err = !err ? GPUErrors::ERROR_TPCZS_INVALID_NADC : err; // Ensure we don't overwrite any previous error
}
if (ctx.iThread == 0 && err) {
[[maybe_unused]] bool dumpPage = false;
if (err == GPUErrors::ERROR_TPCZS_VERSION_MISMATCH) {
ctx.clusterer.raiseError(err, decHeader->version, ZSVersionDenseLinkBased);
} else if (err == GPUErrors::ERROR_TPCZS_INVALID_MAGIC_WORD) {
ctx.clusterer.raiseError(err, decHeader->magicWord);
} else if (err == GPUErrors::ERROR_TPCZS_INCOMPLETE_HBF) {
ctx.clusterer.raiseError(err, ctx.clusterer.mISector * 1000 + decHeader->cruID, raw::RDHUtils::getPageCounter(rawDataHeader), raw::RDHUtils::getPageCounter(nextPage));
} else if (err == GPUErrors::ERROR_TPCZS_PAGE_OVERFLOW) {
ctx.clusterer.raiseError(err, extendsToNextPage);
dumpPage = true;
} else if (err == GPUErrors::ERROR_TPCZS_INVALID_NADC) {
ctx.clusterer.raiseError(err, nSamplesInPage, nSamplesWritten, extendsToNextPage);
dumpPage = true;
} else {
ctx.clusterer.raiseError(GPUErrors::ERROR_TPCZS_UNKNOWN, err);
}
#ifdef GPUCA_CHECK_TPCZS_CORRUPTION
#ifndef GPUCA_GPUCODE
if (dumpPage) {
// allocate more space on the stack for fname, so it can be overwritten by hand in a debugger.
const char fname[64] = "dump00.bin";
FILE* foo = fopen(fname, "w+b");
fwrite(pageStart, 1, TPCZSHDR::TPC_ZS_PAGE_SIZE, foo);
fclose(foo);
}
#endif
#endif
}
return ctx.pageDigitOffset;
}
template <bool PayloadExtendsToNextPage>
GPUd() int16_t GPUTPCCFDecodeZSDenseLink::DecodeTB(
GPUSharedMemory& smem,
DecodeCtx& ctx,
const header::RAWDataHeader* rawDataHeader,
int32_t cru,
uint16_t nSamplesLeftInPage,
const uint8_t* payloadEnd,
const uint8_t* nextPage)
{
#define MAYBE_PAGE_OVERFLOW(pagePtr) \
if constexpr (PayloadExtendsToNextPage) { \
if (pagePtr >= payloadEnd && pagePtr < nextPage) { \
ptrdiff_t diff = pagePtr - payloadEnd; \
pagePtr = nextPage; \
ConsumeBytes(pagePtr, sizeof(header::RAWDataHeader) + diff); \
} \
} else { \
if (pagePtr > payloadEnd) { \
return -GPUErrors::ERROR_TPCZS_PAGE_OVERFLOW; \
} \
}
#define PEEK_OVERFLOW(pagePtr, offset) \
(*(PayloadExtendsToNextPage && (pagePtr) < nextPage && (pagePtr) + (offset) >= payloadEnd \
? nextPage + sizeof(header::RAWDataHeader) + ((pagePtr) + (offset) - payloadEnd) \
: (pagePtr) + (offset)))
#define TEST_BIT(x, bit) static_cast<bool>((x) & (1 << (bit)))
constexpr int32_t NTHREADS = GPUCA_GET_THREAD_COUNT(GPUCA_LB_GPUTPCCFDecodeZSDenseLink);
static_assert(NTHREADS == GPUCA_WARP_SIZE, "Decoding TB Headers in parallel assumes block size is a single warp.");
const CfFragment& fragment = ctx.clusterer.mPmemory->fragment;
// Read timebin block header
uint16_t tbbHdr = ConsumeByte(ctx.page);
MAYBE_PAGE_OVERFLOW(ctx.page);
tbbHdr |= static_cast<uint16_t>(ConsumeByte(ctx.page)) << CHAR_BIT;
MAYBE_PAGE_OVERFLOW(ctx.page);
uint8_t nLinksInTimebin = tbbHdr & 0x000F;
uint16_t linkBC = (tbbHdr & 0xFFF0) >> 4;
int32_t timeBin = (linkBC + (uint64_t)(raw::RDHUtils::getHeartBeatOrbit(*rawDataHeader) - ctx.firstHBF) * constants::lhc::LHCMaxBunches) / LHCBCPERTIMEBIN;
int16_t nSamplesInTB = 0;
// Read timebin link headers
for (uint8_t iLink = 0; iLink < nLinksInTimebin; iLink++) {
uint8_t timebinLinkHeaderStart = ConsumeByte(ctx.page);
MAYBE_PAGE_OVERFLOW(ctx.page);
if (ctx.iThread == 0) {
smem.linkIds[iLink] = timebinLinkHeaderStart & 0b00011111;
}
bool bitmaskIsFlat = timebinLinkHeaderStart & 0b00100000;
uint16_t bitmaskL2 = 0x03FF;
if (not bitmaskIsFlat) {
bitmaskL2 = static_cast<uint16_t>(timebinLinkHeaderStart & 0b11000000) << 2 | static_cast<uint16_t>(ConsumeByte(ctx.page));
MAYBE_PAGE_OVERFLOW(ctx.page);
}
int32_t nBytesBitmask = CAMath::Popcount(bitmaskL2);
for (int32_t chan = ctx.iThread; chan < CAMath::nextMultipleOf<NTHREADS>(80); chan += NTHREADS) {
int32_t chanL2Idx = chan / 8;
bool l2 = TEST_BIT(bitmaskL2, chanL2Idx);
int32_t chanByteOffset = nBytesBitmask - 1 - CAMath::Popcount(bitmaskL2 >> (chanL2Idx + 1));
uint8_t myChannelHasData = (chan < 80 && l2 ? TEST_BIT(PEEK_OVERFLOW(ctx.page, chanByteOffset), chan % 8) : 0);
int32_t nSamplesStep;
int32_t threadSampleOffset = CfUtils::warpPredicateScan(myChannelHasData, &nSamplesStep);
if (myChannelHasData) {
smem.rawFECChannels[nSamplesInTB + threadSampleOffset] = chan;
}
nSamplesInTB += nSamplesStep;
}
ConsumeBytes(ctx.page, nBytesBitmask);
MAYBE_PAGE_OVERFLOW(ctx.page);
if (ctx.iThread == 0) {
smem.samplesPerLinkEnd[iLink] = nSamplesInTB;
}
} // for (uint8_t iLink = 0; iLink < nLinksInTimebin; iLink++)
GPUbarrierWarp(); // Ensure all writes to shared memory are finished, before reading it
if (nSamplesInTB > nSamplesLeftInPage) {
return -GPUErrors::ERROR_TPCZS_INVALID_NADC;
}
// This needs to happen BEFORE checking if the timebin is in fragment
// to ensure ADC bytes are always consumed, even if data isn't decoded
const uint8_t* adcData = ConsumeBytes(ctx.page, (nSamplesInTB * DECODE_BITS + 7) / 8);
MAYBE_PAGE_OVERFLOW(ctx.page);
bool discardTimeBin = not fragment.contains(timeBin);
discardTimeBin |= (ctx.tpcTimeBinCut > 0 && timeBin > ctx.tpcTimeBinCut);
if (discardTimeBin) {
return FillWithInvalid(ctx.clusterer, ctx.iThread, NTHREADS, ctx.pageDigitOffset, nSamplesInTB);
}
// Unpack ADC
int32_t iLink = 0;
for (uint16_t sample = ctx.iThread; sample < nSamplesInTB; sample += NTHREADS) {
const uint16_t adcBitOffset = sample * DECODE_BITS;
uint16_t adcByteOffset = adcBitOffset / CHAR_BIT;
const uint8_t adcOffsetInByte = adcBitOffset - adcByteOffset * CHAR_BIT;
static_assert(DECODE_BITS <= sizeof(uint16_t) * CHAR_BIT);
uint16_t adc = 0;
for (uint8_t bits = 0; bits < DECODE_BITS; bits += CHAR_BIT) {
adc |= static_cast<uint16_t>(PEEK_OVERFLOW(adcData, adcByteOffset)) << bits;
adcByteOffset++;
}
adc >>= adcOffsetInByte;
while (smem.samplesPerLinkEnd[iLink] <= sample) {
iLink++;
}
int32_t rawFECChannelLink = smem.rawFECChannels[sample];
// Unpack data for cluster finder
o2::tpc::PadPos padAndRow = GetPadAndRowFromFEC(ctx.clusterer, cru, rawFECChannelLink, smem.linkIds[iLink]);
float charge = ADCToFloat(adc, DECODE_MASK, DECODE_BITS_FACTOR);
WriteCharge(ctx.clusterer, charge, padAndRow, fragment.toLocal(timeBin), ctx.pageDigitOffset + sample);
} // for (uint16_t sample = iThread; sample < nSamplesInTB; sample += NTHREADS)
GPUbarrierWarp(); // Ensure all reads to shared memory are finished, before decoding next header into shmem
return nSamplesInTB;
#undef TEST_BIT
#undef PEEK_OVERFLOW
#undef MAYBE_PAGE_OVERFLOW
}
GPUd() bool GPUTPCCFDecodeZSDenseLink::ChannelIsActive(const uint8_t* chan, uint16_t chanIndex)
{
constexpr uint8_t N_BITS_PER_ENTRY = sizeof(*chan) * CHAR_BIT;
const uint8_t entryIndex = chanIndex / N_BITS_PER_ENTRY;
const uint8_t bitInEntry = chanIndex % N_BITS_PER_ENTRY;
return chan[entryIndex] & (1 << bitInEntry);
}