4040#include " TPCBase/CRU.h"
4141#include " DetectorsRaw/RDHUtils.h"
4242
43+ #include < oneapi/tbb.h>
44+
4345using namespace o2 ::gpu;
4446using namespace o2 ::tpc;
4547using namespace o2 ::tpc::constants;
@@ -1306,6 +1308,17 @@ size_t zsEncoderRun<T>::compare(std::vector<zsPage>* buffer, std::vector<o2::tpc
13061308} // anonymous namespace
13071309#endif // GPUCA_TPC_GEOMETRY_O2
13081310
1311+ namespace o2 ::gpu::internal
1312+ {
1313+ struct tmpReductionResult {
1314+ uint32_t totalPages = 0 ;
1315+ size_t totalSize = 0 ;
1316+ size_t nErrors = 0 ;
1317+ size_t digitsInput = 0 ;
1318+ size_t digitsEncoded = 0 ;
1319+ };
1320+ } // namespace o2::gpu::internal
1321+
13091322template <class S >
13101323void GPUReconstructionConvert::RunZSEncoder (const S& in, std::unique_ptr<uint64_t []>* outBuffer, uint32_t * outSizes, o2::raw::RawFileWriter* raw, const o2::InteractionRecord* ir, const GPUParam& param, int32_t version, bool verify, float threshold, bool padding, std::function<void (std::vector<o2::tpc::Digit>&)> digitsFilter)
13111324{
@@ -1316,67 +1329,68 @@ void GPUReconstructionConvert::RunZSEncoder(const S& in, std::unique_ptr<uint64_
13161329 }
13171330#ifdef GPUCA_TPC_GEOMETRY_O2
13181331 std::vector<zsPage> buffer[NSLICES][GPUTrackingInOutZS::NENDPOINTS];
1319- uint32_t totalPages = 0 ;
1320- size_t totalSize = 0 ;
1321- size_t nErrors = 0 ;
1322- size_t digitsInput = 0 ;
1323- size_t digitsEncoded = 0 ;
1324- // clang-format off
1325- GPUCA_OPENMP (parallel for reduction (+ : totalPages, nErrors, totalSize, digitsInput, digitsEncoded))
1326- // clang-format on
1327- for (uint32_t i = 0 ; i < NSLICES; i++) {
1328- std::vector<o2::tpc::Digit> tmpBuffer;
1329- digitsInput += ZSEncoderGetNDigits (in, i);
1330- tmpBuffer.resize (ZSEncoderGetNDigits (in, i));
1331- if (threshold > 0 .f && !digitsFilter) {
1332- auto it = std::copy_if (ZSEncoderGetDigits (in, i), ZSEncoderGetDigits (in, i) + ZSEncoderGetNDigits (in, i), tmpBuffer.begin (), [threshold](auto & v) { return v.getChargeFloat () >= threshold; });
1333- tmpBuffer.resize (std::distance (tmpBuffer.begin (), it));
1334- } else {
1335- std::copy (ZSEncoderGetDigits (in, i), ZSEncoderGetDigits (in, i) + ZSEncoderGetNDigits (in, i), tmpBuffer.begin ());
1336- }
1337-
1338- if (digitsFilter) {
1339- digitsFilter (tmpBuffer);
1340- if (threshold > 0 .f ) {
1341- std::vector<o2::tpc::Digit> tmpBuffer2 = std::move (tmpBuffer);
1342- tmpBuffer = std::vector<o2::tpc::Digit>(tmpBuffer2.size ());
1343- auto it = std::copy_if (tmpBuffer2.begin (), tmpBuffer2.end (), tmpBuffer.begin (), [threshold](auto & v) { return v.getChargeFloat () >= threshold; });
1332+ auto reduced = tbb::parallel_reduce (tbb::blocked_range<uint32_t >(0 , NSLICES), o2::gpu::internal::tmpReductionResult (), [&](const auto range, auto red) {
1333+ for (uint32_t i = range.begin (); i < range.end (); i++) {
1334+ std::vector<o2::tpc::Digit> tmpBuffer;
1335+ red.digitsInput += ZSEncoderGetNDigits (in, i);
1336+ tmpBuffer.resize (ZSEncoderGetNDigits (in, i));
1337+ if (threshold > 0 .f && !digitsFilter) {
1338+ auto it = std::copy_if (ZSEncoderGetDigits (in, i), ZSEncoderGetDigits (in, i) + ZSEncoderGetNDigits (in, i), tmpBuffer.begin (), [threshold](auto & v) { return v.getChargeFloat () >= threshold; });
13441339 tmpBuffer.resize (std::distance (tmpBuffer.begin (), it));
1340+ } else {
1341+ std::copy (ZSEncoderGetDigits (in, i), ZSEncoderGetDigits (in, i) + ZSEncoderGetNDigits (in, i), tmpBuffer.begin ());
13451342 }
1346- }
1347- digitsEncoded += tmpBuffer. size ();
1348-
1349- auto runZS = [&]( auto & encoder ) {
1350- encoder. zsVersion = version ;
1351- encoder. init ( );
1352- totalPages += encoder. run (buffer[i], tmpBuffer, &totalSize );
1353- if (verify) {
1354- nErrors += encoder. compare (buffer[i], tmpBuffer); // Verification
1343+
1344+ if (digitsFilter) {
1345+ digitsFilter (tmpBuffer);
1346+ if (threshold > 0 . f ) {
1347+ std::vector<o2::tpc::Digit> tmpBuffer2 = std::move (tmpBuffer) ;
1348+ tmpBuffer = std::vector<o2::tpc::Digit>(tmpBuffer2. size () );
1349+ auto it = std::copy_if (tmpBuffer2. begin (), tmpBuffer2. end (), tmpBuffer. begin (), [threshold]( auto & v) { return v. getChargeFloat () >= threshold; } );
1350+ tmpBuffer. resize ( std::distance (tmpBuffer. begin (), it));
1351+ }
13551352 }
1356- };
1353+ red.digitsEncoded += tmpBuffer.size ();
1354+
1355+ auto runZS = [&](auto & encoder) {
1356+ encoder.zsVersion = version;
1357+ encoder.init ();
1358+ red.totalPages += encoder.run (buffer[i], tmpBuffer, &red.totalSize );
1359+ if (verify) {
1360+ red.nErrors += encoder.compare (buffer[i], tmpBuffer); // Verification
1361+ }
1362+ };
13571363
1358- if (version >= ZSVersion::ZSVersionRowBased10BitADC && version <= ZSVersion::ZSVersionRowBased12BitADC) {
1359- zsEncoderRun<zsEncoderRow> enc{{{.iSector = i, .raw = raw, .ir = ir, .param = ¶m, .padding = padding}}};
1360- runZS (enc);
1361- } else if (version >= ZSVersion::ZSVersionLinkBasedWithMeta && version <= ZSVersion::ZSVersionDenseLinkBasedV2) {
1362- #ifdef GPUCA_O2_LIB
1363- if (version == ZSVersion::ZSVersionLinkBasedWithMeta) {
1364- zsEncoderRun<zsEncoderImprovedLinkBased> enc{{{{.iSector = i, .raw = raw, .ir = ir, .param = ¶m, .padding = padding}}}};
1364+ if (version >= ZSVersion::ZSVersionRowBased10BitADC && version <= ZSVersion::ZSVersionRowBased12BitADC) {
1365+ zsEncoderRun<zsEncoderRow> enc{{{.iSector = i, .raw = raw, .ir = ir, .param = ¶m, .padding = padding}}};
13651366 runZS (enc);
1366- } else if (version >= ZSVersion::ZSVersionDenseLinkBased && version <= ZSVersion::ZSVersionDenseLinkBasedV2) {
1367- zsEncoderRun<zsEncoderDenseLinkBased> enc{{{{.iSector = i, .raw = raw, .ir = ir, .param = ¶m, .padding = padding}}}};
1368- runZS (enc);
1369- }
1367+ } else if (version >= ZSVersion::ZSVersionLinkBasedWithMeta && version <= ZSVersion::ZSVersionDenseLinkBasedV2) {
1368+ #ifdef GPUCA_O2_LIB
1369+ if (version == ZSVersion::ZSVersionLinkBasedWithMeta) {
1370+ zsEncoderRun<zsEncoderImprovedLinkBased> enc{{{{.iSector = i, .raw = raw, .ir = ir, .param = ¶m, .padding = padding}}}};
1371+ runZS (enc);
1372+ } else if (version >= ZSVersion::ZSVersionDenseLinkBased && version <= ZSVersion::ZSVersionDenseLinkBasedV2) {
1373+ zsEncoderRun<zsEncoderDenseLinkBased> enc{{{{.iSector = i, .raw = raw, .ir = ir, .param = ¶m, .padding = padding}}}};
1374+ runZS (enc);
1375+ }
13701376#else
1371- throw std::runtime_error (" Link based ZS encoding not supported in standalone build" );
1377+ throw std::runtime_error (" Link based ZS encoding not supported in standalone build" );
13721378#endif
1373- } else {
1374- throw std::runtime_error (" Invalid ZS version " s + std::to_string (version) + " , cannot decode" s);
1379+ } else {
1380+ throw std::runtime_error (" Invalid ZS version " s + std::to_string (version) + " , cannot decode" s);
1381+ }
13751382 }
1376- }
1383+ return red; }, [&](const auto & red1, const auto & red2) {
1384+ auto red = red1;
1385+ red.totalPages += red2.totalPages ;
1386+ red.totalSize += red2.totalSize ;
1387+ red.nErrors += red2.nErrors ;
1388+ red.digitsInput += red2.digitsInput ;
1389+ red.digitsEncoded += red2.digitsEncoded ;
1390+ return red; });
13771391
13781392 if (outBuffer) {
1379- outBuffer->reset (new uint64_t [totalPages * TPCZSHDR::TPC_ZS_PAGE_SIZE / sizeof (uint64_t )]);
1393+ outBuffer->reset (new uint64_t [reduced. totalPages * TPCZSHDR::TPC_ZS_PAGE_SIZE / sizeof (uint64_t )]);
13801394 uint64_t offset = 0 ;
13811395 for (uint32_t i = 0 ; i < NSLICES; i++) {
13821396 for (uint32_t j = 0 ; j < GPUTrackingInOutZS::NENDPOINTS; j++) {
@@ -1386,12 +1400,12 @@ void GPUReconstructionConvert::RunZSEncoder(const S& in, std::unique_ptr<uint64_
13861400 }
13871401 }
13881402 }
1389- if (nErrors) {
1390- GPUError (" ERROR: %ld INCORRECT SAMPLES DURING ZS ENCODING VERIFICATION!!!" , ( int64_t ) nErrors);
1403+ if (reduced. nErrors ) {
1404+ GPUError (" ERROR: %lu INCORRECT SAMPLES DURING ZS ENCODING VERIFICATION!!!" , reduced. nErrors );
13911405 } else if (verify) {
13921406 GPUInfo (" ENCODING VERIFICATION PASSED" );
13931407 }
1394- GPUInfo (" TOTAL ENCODED SIZE: %lu (%lu of %lu digits encoded)" , totalSize, digitsEncoded, digitsInput);
1408+ GPUInfo (" TOTAL ENCODED SIZE: %lu (%lu of %lu digits encoded)" , reduced. totalSize , reduced. digitsEncoded , reduced. digitsInput );
13951409#endif
13961410}
13971411
0 commit comments