Project
Loading...
Searching...
No Matches
GPUReconstruction.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
15#include <cstring>
16#include <cstdio>
17#include <iostream>
18#include <mutex>
19#include <string>
20#include <map>
21#include <queue>
22#include <mutex>
23#include <condition_variable>
24#include <array>
25
26#include "GPUReconstruction.h"
29#include "GPUReconstructionIO.h"
30#include "GPUROOTDumpCore.h"
31#include "GPUConfigDump.h"
32#include "GPUChainTracking.h"
33#include "GPUConstantMem.h"
34#include "GPUCommonHelpers.h"
35#include "GPUSettings.h"
36
37#include "GPUMemoryResource.h"
38#include "GPUChain.h"
40
41#include "GPULogging.h"
42#include "utils/strtag.h"
43#include "utils/stdspinlock.h"
44
45#ifndef GPUCA_STANDALONE
47#endif
48
50
51namespace o2::gpu
52{
53namespace // anonymous
54{
55struct GPUReconstructionPipelineQueue {
56 uint32_t op = 0; // For now, 0 = process, 1 = terminate
57 GPUChain* chain = nullptr;
58 std::mutex m;
59 std::condition_variable c;
60 bool done = false;
61 int32_t retVal = 0;
62};
63} // namespace
64
66 std::queue<GPUReconstructionPipelineQueue*> pipelineQueue;
67 std::mutex mutex;
68 std::condition_variable cond;
69 bool terminate = false;
70};
71} // namespace o2::gpu
72
73using namespace o2::gpu;
74
75constexpr const char* const GPUReconstruction::GEOMETRY_TYPE_NAMES[];
76constexpr const char* const GPUReconstruction::IOTYPENAMES[];
78
79static ptrdiff_t ptrDiff(void* a, void* b) { return (char*)a - (char*)b; }
80
81GPUReconstruction::GPUReconstruction(const GPUSettingsDeviceBackend& cfg) : mHostConstantMem(new GPUConstantMem), mGRPSettings(new GPUSettingsGRP), mDeviceBackendSettings(new GPUSettingsDeviceBackend(cfg)), mProcessingSettings(new GPUSettingsProcessing)
82{
83 if (cfg.master) {
85 throw std::invalid_argument("device type of master and slave GPUReconstruction does not match");
86 }
87 if (cfg.master->mMaster) {
88 throw std::invalid_argument("Cannot be slave to a slave");
89 }
90 mMaster = cfg.master;
91 mSlaveId = cfg.master->mSlaves.size();
92 cfg.master->mSlaves.emplace_back(this);
93 }
96 for (uint32_t i = 0; i < NSECTORS; i++) {
97 processors()->tpcTrackers[i].SetSector(i); // TODO: Move to a better place
99#ifdef GPUCA_HAS_ONNX
100 processors()->tpcNNClusterer[i].mISector = i;
101#endif
102 }
103}
104
106{
107 if (mInitialized) {
108 GPUError("GPU Reconstruction not properly deinitialized!");
109 }
110}
111
112void GPUReconstruction::GetITSTraits(std::unique_ptr<o2::its::TrackerTraits<7>>* trackerTraits, std::unique_ptr<o2::its::VertexerTraits<7>>* vertexerTraits, std::unique_ptr<o2::its::TimeFrame<7>>* timeFrame)
113{
114 if (trackerTraits) {
115 trackerTraits->reset(new o2::its::TrackerTraits<7>);
116 }
117 if (vertexerTraits) {
118 vertexerTraits->reset(new o2::its::VertexerTraits<7>);
119 }
120 if (timeFrame) {
121 timeFrame->reset(new o2::its::TimeFrame<7>);
122 }
123}
124
126{
127 return std::max<int32_t>(0, tbb::this_task_arena::current_thread_index());
128}
129
131{
132 if (mMaster) {
133 throw std::runtime_error("Must not call init on slave!");
134 }
135#ifndef GPUCA_NO_ROOT
136 if (!mROOTDump) {
137 mROOTDump = GPUROOTDumpCore::getAndCreate(GetProcessingSettings().ROOTDumpFile.c_str());
138 }
139#endif
140 int32_t retVal = InitPhaseBeforeDevice();
141 if (retVal) {
142 return retVal;
143 }
144 for (uint32_t i = 0; i < mSlaves.size(); i++) {
145 retVal = mSlaves[i]->InitPhaseBeforeDevice();
146 if (retVal) {
147 GPUError("Error initialization slave (before deviceinit)");
148 return retVal;
149 }
150 mNStreams = std::max(mNStreams, mSlaves[i]->mNStreams);
153 }
154 if (InitDevice()) {
155 return 1;
156 }
157 if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_GLOBAL) {
160 } else {
162 }
164 return 1;
165 }
166 for (uint32_t i = 0; i < mSlaves.size(); i++) {
167 mSlaves[i]->mDeviceMemoryBase = mDeviceMemoryPermanent;
168 mSlaves[i]->mHostMemoryBase = mHostMemoryPermanent;
169 mSlaves[i]->mDeviceMemorySize = mDeviceMemorySize - ptrDiff(mSlaves[i]->mDeviceMemoryBase, mDeviceMemoryBase);
170 mSlaves[i]->mHostMemorySize = mHostMemorySize - ptrDiff(mSlaves[i]->mHostMemoryBase, mHostMemoryBase);
171 mSlaves[i]->mHostMemoryPoolEnd = mHostMemoryPoolEnd;
172 mSlaves[i]->mDeviceMemoryPoolEnd = mDeviceMemoryPoolEnd;
173 if (mSlaves[i]->InitDevice()) {
174 GPUError("Error initialization slave (deviceinit)");
175 return 1;
176 }
178 GPUError("Error initialization slave (permanent memory)");
179 return 1;
180 }
181 mDeviceMemoryPermanent = mSlaves[i]->mDeviceMemoryPermanent;
182 mHostMemoryPermanent = mSlaves[i]->mHostMemoryPermanent;
183 }
185 if (retVal) {
186 return retVal;
187 }
189 for (uint32_t i = 0; i < mSlaves.size(); i++) {
190 mSlaves[i]->mDeviceMemoryPermanent = mDeviceMemoryPermanent;
191 mSlaves[i]->mHostMemoryPermanent = mHostMemoryPermanent;
192 retVal = mSlaves[i]->InitPhaseAfterDevice();
193 if (retVal) {
194 GPUError("Error initialization slave (after device init)");
195 return retVal;
196 }
197 mSlaves[i]->ClearAllocatedMemory();
198 }
199 debugInit();
200 return 0;
201}
202
203namespace o2::gpu::internal
204{
205static uint32_t getDefaultNThreads()
206{
207 const char* tbbEnv = getenv("TBB_NUM_THREADS");
208 uint32_t tbbNum = tbbEnv ? atoi(tbbEnv) : 0;
209 if (tbbNum) {
210 return tbbNum;
211 }
212 const char* ompEnv = getenv("OMP_NUM_THREADS");
213 uint32_t ompNum = ompEnv ? atoi(ompEnv) : 0;
214 if (ompNum) {
215 return ompNum;
216 }
217 return tbb::info::default_concurrency();
218}
219} // namespace o2::gpu::internal
220
222{
223 if (GetProcessingSettings().printSettings) {
224 if (mSlaves.size() || mMaster) {
225 printf("\nConfig Dump %s\n", mMaster ? "Slave" : "Master");
226 }
227 const GPUChainTracking* chTrk;
228 for (uint32_t i = 0; i < mChains.size(); i++) {
229 if ((chTrk = dynamic_cast<GPUChainTracking*>(mChains[i].get()))) {
230 break;
231 }
232 }
233 GPUConfigDump::dumpConfig(&param().rec, mProcessingSettings.get(), chTrk ? chTrk->GetQAConfig() : nullptr, chTrk ? chTrk->GetEventDisplayConfig() : nullptr, mDeviceBackendSettings.get(), &mRecoSteps);
234 }
237 if (!IsGPU()) {
238 mRecoSteps.stepsGPUMask.set((uint8_t)0);
239 }
240
241 if (GetProcessingSettings().forceMemoryPoolSize >= 1024 || GetProcessingSettings().forceHostMemoryPoolSize >= 1024) {
243 }
244 if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_AUTO) {
246 }
247 if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_INDIVIDUAL) {
248 mProcessingSettings->forceMemoryPoolSize = mProcessingSettings->forceHostMemoryPoolSize = 0;
249 }
250 if (GetProcessingSettings().debugLevel >= 4) {
251 mProcessingSettings->keepAllMemory = true;
252 }
253 if (GetProcessingSettings().debugLevel >= 5 && GetProcessingSettings().allocDebugLevel < 2) {
254 mProcessingSettings->allocDebugLevel = 2;
255 }
257 mProcessingSettings->keepDisplayMemory = true;
258 }
259 if (GetProcessingSettings().debugLevel < 6) {
260 mProcessingSettings->debugMask = 0;
261 }
262 if (GetProcessingSettings().debugLevel < 1) {
263 mProcessingSettings->deviceTimers = false;
264 }
265 if (GetProcessingSettings().debugLevel > 0) {
266 mProcessingSettings->recoTaskTiming = true;
267 }
268 bool detMode = false;
269#ifdef GPUCA_DETERMINISTIC_MODE
270 detMode = true;
271#endif
272 if (GetProcessingSettings().deterministicGPUReconstruction == -1) {
273 mProcessingSettings->deterministicGPUReconstruction = detMode ? 1 : (GetProcessingSettings().debugLevel >= 6);
274 }
275 if (GetProcessingSettings().deterministicGPUReconstruction) {
276 if (!detMode) {
277 GPUError("WARNING, deterministicGPUReconstruction needs GPUCA_DETERMINISTIC_MODE for being fully deterministic, without only most indeterminism by concurrency is removed, but floating point effects remain!");
278 } else {
279 GPUInfo("GPU Deterministic Reconstruction is enabled");
280 }
281 if (mProcessingSettings->debugLevel >= 6 && ((mProcessingSettings->debugMask + 1) & mProcessingSettings->debugMask)) {
282 GPUError("WARNING: debugMask %d - debug output might not be deterministic with intermediate steps missing", mProcessingSettings->debugMask);
283 }
284 mProcessingSettings->overrideClusterizerFragmentLen = TPC_MAX_FRAGMENT_LEN_GPU;
285 if (GetProcessingSettings().createO2Output > 1) {
286 mProcessingSettings->createO2Output = 1;
287 }
288 mProcessingSettings->rtc.deterministic = 1;
289 } else {
290 if (detMode) {
291 GPUError("WARNING, compiled with GPUCA_DETERMINISTIC_MODE but deterministicGPUReconstruction not set, only compile-time determinism and deterministic math enforced, not fully deterministic!");
292 }
293 }
294 if (GetProcessingSettings().deterministicGPUReconstruction && GetProcessingSettings().debugLevel >= 6) {
295 mProcessingSettings->nTPCClustererLanes = 1;
296 }
297 if (GetProcessingSettings().createO2Output > 1 && GetProcessingSettings().runQA && GetProcessingSettings().qcRunFraction == 100.f) {
298 mProcessingSettings->createO2Output = 1;
299 }
300 if (!GetProcessingSettings().createO2Output || !IsGPU()) {
301 mProcessingSettings->clearO2OutputFromGPU = false;
302 }
304 mProcessingSettings->mergerSortTracks = false;
305 }
306 if (GetProcessingSettings().debugLevel > 3 || !IsGPU() || GetProcessingSettings().deterministicGPUReconstruction) {
307 mProcessingSettings->delayedOutput = false;
308 }
309 if (!GetProcessingSettings().rtc.enable) {
310 mProcessingSettings->rtc.optConstexpr = false;
311 }
312 if (GetProcessingSettings().allSanityChecks) {
313 mProcessingSettings->clusterizerZSSanityCheck = mProcessingSettings->mergerSanityCheck = mProcessingSettings->outputSanityCheck = true;
314 }
315
316 static_cast<GPUSettingsProcessingScaling&>(*mMemoryScalers) = GetProcessingSettings().scaling;
317 mMemoryScalers->scalingFactor = GetProcessingSettings().memoryScalingFactor;
318 mMemoryScalers->returnMaxVal = GetProcessingSettings().forceMaxMemScalers != 0;
319 if (GetProcessingSettings().forceMaxMemScalers > 1) {
320 mMemoryScalers->rescaleMaxMem(GetProcessingSettings().forceMaxMemScalers);
321 }
322
323 if (GetProcessingSettings().nHostThreads != -1 && GetProcessingSettings().ompThreads != -1) {
324 GPUFatal("Must not use both nHostThreads and ompThreads at the same time!");
325 } else if (GetProcessingSettings().ompThreads != -1) {
326 mProcessingSettings->nHostThreads = GetProcessingSettings().ompThreads;
327 GPUWarning("You are using the deprecated ompThreads option, please switch to nHostThreads!");
328 }
329
330 if (GetProcessingSettings().nHostThreads <= 0) {
331 mProcessingSettings->nHostThreads = internal::getDefaultNThreads();
332 } else {
333 mProcessingSettings->autoAdjustHostThreads = false;
334 }
335 mMaxHostThreads = GetProcessingSettings().nHostThreads;
336 if (mMaster == nullptr) {
337 mThreading = std::make_shared<GPUReconstructionThreading>();
338 mThreading->control = std::make_unique<tbb::global_control>(tbb::global_control::max_allowed_parallelism, mMaxHostThreads);
339 mThreading->allThreads = std::make_unique<tbb::task_arena>(mMaxHostThreads);
340 mThreading->activeThreads = std::make_unique<tbb::task_arena>(mMaxHostThreads);
341 } else {
343 }
345 if (IsGPU()) {
346 mNStreams = std::max<int32_t>(GetProcessingSettings().nStreams, 3);
347 }
348
349 if (GetProcessingSettings().nTPCClustererLanes == -1) {
350 mProcessingSettings->nTPCClustererLanes = (GetRecoStepsGPU() & RecoStep::TPCClusterFinding) ? 3 : std::max<int32_t>(1, std::min<int32_t>(GPUTPCGeometry::NSECTORS, GetProcessingSettings().inKernelParallel ? (mMaxHostThreads >= 4 ? std::min<int32_t>(mMaxHostThreads / 2, mMaxHostThreads >= 32 ? GPUTPCGeometry::NSECTORS : 4) : 1) : mMaxHostThreads));
351 }
352 if (GetProcessingSettings().overrideClusterizerFragmentLen == -1) {
353 mProcessingSettings->overrideClusterizerFragmentLen = ((GetRecoStepsGPU() & RecoStep::TPCClusterFinding) || (mMaxHostThreads / GetProcessingSettings().nTPCClustererLanes >= 3)) ? TPC_MAX_FRAGMENT_LEN_GPU : TPC_MAX_FRAGMENT_LEN_HOST;
354 }
355 if ((uint32_t)GetProcessingSettings().nTPCClustererLanes > GPUTPCGeometry::NSECTORS) {
356 GPUError("Invalid value for nTPCClustererLanes: %d", GetProcessingSettings().nTPCClustererLanes);
357 mProcessingSettings->nTPCClustererLanes = GPUTPCGeometry::NSECTORS;
358 }
359
360 if (GetProcessingSettings().doublePipeline) {
361 mProcessingSettings->rtctech.allowOptimizedSlaveReconstruction = true;
362 }
363 if (GetProcessingSettings().doublePipeline && (mChains.size() != 1 || mChains[0]->SupportsDoublePipeline() == false || !IsGPU() || GetProcessingSettings().memoryAllocationStrategy != GPUMemoryResource::ALLOCATION_GLOBAL)) {
364 GPUError("Must use double pipeline mode only with exactly one chain that must support it");
365 return 1;
366 }
367 if (mMaster == nullptr && GetProcessingSettings().doublePipeline) {
369 }
370
371 if (mMaster && GetProcessingSettings().rtc.enable && (GetProcessingSettings().rtc.optConstexpr || GetProcessingSettings().rtc.optSpecialCode) && !GetProcessingSettings().rtctech.allowOptimizedSlaveReconstruction) {
372 GPUError("Not allowed to create optimized RTC code with more than one GPUReconstruction instances");
373 return 1;
374 }
375
377 for (uint32_t i = 0; i < mChains.size(); i++) {
378 if (mChains[i]->EarlyConfigure()) {
379 return 1;
380 }
381 mChains[i]->RegisterPermanentMemoryAndProcessors();
382 size_t memPrimary, memPageLocked;
383 mChains[i]->MemorySize(memPrimary, memPageLocked);
384 if (!IsGPU() || mOutputControl.useInternal()) {
385 memPageLocked = memPrimary;
386 }
387 mDeviceMemorySize += memPrimary;
388 mHostMemorySize += memPageLocked;
389 }
390 if (GetProcessingSettings().forceMemoryPoolSize && GetProcessingSettings().forceMemoryPoolSize <= 2 && CanQueryMaxMemory()) {
391 mDeviceMemorySize = GetProcessingSettings().forceMemoryPoolSize;
392 } else if (GetProcessingSettings().forceMemoryPoolSize > 2) {
393 mDeviceMemorySize = GetProcessingSettings().forceMemoryPoolSize;
394 if (!IsGPU() || mOutputControl.useInternal()) {
396 }
397 }
398 if (GetProcessingSettings().forceHostMemoryPoolSize) {
399 mHostMemorySize = GetProcessingSettings().forceHostMemoryPoolSize;
400 }
401
402 for (uint32_t i = 0; i < mProcessors.size(); i++) {
403 (mProcessors[i].proc->*(mProcessors[i].RegisterMemoryAllocation))();
404 }
405
406 return 0;
407}
408
410{
411 if (IsGPU()) {
412 for (uint32_t i = 0; i < mChains.size(); i++) {
413 mChains[i]->RegisterGPUProcessors();
414 }
415 }
417 return 0;
418}
419
421{
422 if (GetProcessingSettings().forceMaxMemScalers <= 1 && GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_GLOBAL) {
424 }
425 for (uint32_t i = 0; i < mChains.size(); i++) {
426 if (mChains[i]->Init()) {
427 return 1;
428 }
429 }
430 for (uint32_t i = 0; i < mProcessors.size(); i++) {
431 (mProcessors[i].proc->*(mProcessors[i].InitializeProcessor))();
432 }
433
434 WriteConstantParams(); // Initialize with initial values, can optionally be updated later
435
436 mInitialized = true;
437 return 0;
438}
439
441{
442 if (IsGPU()) {
443 const auto threadContext = GetThreadContext();
444 WriteToConstantMemory(ptrDiff(&processors()->param, processors()), &param(), sizeof(param()), stream);
445 }
446}
447
449{
450 for (uint32_t i = 0; i < mChains.size(); i++) {
451 mChains[i]->Finalize();
452 }
453 return 0;
454}
455
457{
458 if (!mInitialized) {
459 return 1;
460 }
461 for (uint32_t i = 0; i < mSlaves.size(); i++) {
462 if (mSlaves[i]->Exit()) {
463 GPUError("Error exiting slave");
464 }
465 }
466
467 mChains.clear(); // Make sure we destroy a possible ITS GPU tracker before we call the destructors
468 mHostConstantMem.reset(); // Reset these explicitly before the destruction of other members unloads the library
469 if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_INDIVIDUAL) {
470 for (uint32_t i = 0; i < mMemoryResources.size(); i++) {
471 if (mMemoryResources[i].mReuse >= 0) {
472 continue;
473 }
475 mMemoryResources[i].mPtr = mMemoryResources[i].mPtrDevice = nullptr;
476 }
477 }
478 mMemoryResources.clear();
479 if (mInitialized) {
480 ExitDevice();
481 }
482 debugExit();
483 mInitialized = false;
484 return 0;
485}
486
489
491{
492 for (auto it = mMemoryReuse1to1.begin(); it != mMemoryReuse1to1.end(); it++) {
493 auto& re = it->second;
494 if (proc == nullptr || re.proc == proc) {
495 GPUMemoryResource& resMain = mMemoryResources[re.res[0]];
496 resMain.mOverrideSize = 0;
497 for (uint32_t i = 0; i < re.res.size(); i++) {
499 resMain.mOverrideSize = std::max<size_t>(resMain.mOverrideSize, ptrDiff(res.SetPointers((void*)1), (char*)1));
500 }
501 }
502 }
503}
504
506{
508 if ((type & GPUMemoryResource::MEMORY_SCRATCH) && !GetProcessingSettings().keepDisplayMemory) { // keepAllMemory --> keepDisplayMemory
510 } else {
512 }
513 }
515 type &= ~GPUMemoryResource::MEMORY_GPU;
516 }
517 mMemoryResources.emplace_back(proc, setPtr, (GPUMemoryResource::MemoryType)type, name);
518 if (mMemoryResources.size() >= 32768) {
519 throw std::bad_alloc();
520 }
521 uint16_t retVal = mMemoryResources.size() - 1;
522 if (re.type != GPUMemoryReuse::NONE && !GetProcessingSettings().disableMemoryReuse) {
523 const auto& it = mMemoryReuse1to1.find(re.id);
524 if (it == mMemoryReuse1to1.end()) {
525 mMemoryReuse1to1[re.id] = {proc, retVal};
526 } else {
527 mMemoryResources[retVal].mReuse = it->second.res[0];
528 it->second.res.emplace_back(retVal);
529 }
530 }
531 return retVal;
532}
533
535{
536 if (GetProcessingSettings().debugLevel >= 5) {
537 GPUInfo("Allocating memory %p", (void*)proc);
538 }
539 size_t total = 0;
540 for (uint32_t i = 0; i < mMemoryResources.size(); i++) {
541 if (proc == nullptr ? !mMemoryResources[i].mProcessor->mAllocateAndInitializeLate : mMemoryResources[i].mProcessor == proc) {
543 total += AllocateRegisteredMemory(i);
544 } else if (resetCustom && (mMemoryResources[i].mPtr || mMemoryResources[i].mPtrDevice)) {
546 }
547 }
548 }
549 if (GetProcessingSettings().debugLevel >= 5) {
550 GPUInfo("Allocating memory done");
551 }
552 return total;
553}
554
556{
557 if (GetProcessingSettings().debugLevel >= 5) {
558 GPUInfo("Allocating Permanent Memory");
559 }
561 GPUError("Must not allocate permanent memory while volatile chunks are allocated");
562 throw std::bad_alloc();
563 }
564 int32_t total = 0;
565 for (uint32_t i = 0; i < mMemoryResources.size(); i++) {
566 if ((mMemoryResources[i].mType & GPUMemoryResource::MEMORY_PERMANENT) && mMemoryResources[i].mPtr == nullptr) {
567 total += AllocateRegisteredMemory(i);
568 }
569 }
572 if (GetProcessingSettings().debugLevel >= 5) {
573 GPUInfo("Permanent Memory Done");
574 }
575 return total;
576}
577
578size_t GPUReconstruction::AllocateRegisteredMemoryHelper(GPUMemoryResource* res, void*& ptr, void*& memorypool, void* memorybase, size_t memorysize, void* (GPUMemoryResource::*setPtr)(void*) const, void*& memorypoolend, const char* device)
579{
580 if (res->mReuse >= 0) {
581 ptr = (&ptr == &res->mPtrDevice) ? mMemoryResources[res->mReuse].mPtrDevice : mMemoryResources[res->mReuse].mPtr;
582 if (ptr == nullptr) {
583 GPUError("Invalid reuse ptr (%s)", res->mName);
584 throw std::bad_alloc();
585 }
586 size_t retVal = ptrDiff((res->*setPtr)(ptr), ptr);
587 if (retVal > mMemoryResources[res->mReuse].mSize) {
588 GPUError("Insufficient reuse memory %lu < %lu (%s) (%s)", mMemoryResources[res->mReuse].mSize, retVal, res->mName, device);
589 throw std::bad_alloc();
590 }
591 if (GetProcessingSettings().allocDebugLevel >= 2) {
592 std::cout << "Reused (" << device << ") " << res->mName << ": " << retVal << "\n";
593 }
594 return retVal;
595 }
596 if (memorypool == nullptr) {
597 GPUError("Cannot allocate memory from uninitialized pool");
598 throw std::bad_alloc();
599 }
600 size_t retVal;
601 stdspinlock spinlock(mMemoryMutex);
602 if ((res->mType & GPUMemoryResource::MEMORY_STACK) && memorypoolend) {
603 retVal = ptrDiff((res->*setPtr)((char*)1), (char*)(1));
604 memorypoolend = (void*)((char*)memorypoolend - GPUProcessor::getAlignmentMod<constants::GPU_MEMALIGN>(memorypoolend));
605 if (retVal < res->mOverrideSize) {
606 retVal = res->mOverrideSize;
607 }
608 retVal += GPUProcessor::getAlignment<constants::GPU_MEMALIGN>(retVal);
609 memorypoolend = (char*)memorypoolend - retVal;
610 ptr = memorypoolend;
611 retVal = std::max<size_t>(ptrDiff((res->*setPtr)(ptr), ptr), res->mOverrideSize);
612 } else {
613 ptr = memorypool;
614 memorypool = (char*)((res->*setPtr)(ptr));
615 retVal = ptrDiff(memorypool, ptr);
616 if (retVal < res->mOverrideSize) {
617 retVal = res->mOverrideSize;
618 memorypool = (char*)ptr + res->mOverrideSize;
619 }
620 memorypool = (void*)((char*)memorypool + GPUProcessor::getAlignment<constants::GPU_MEMALIGN>(memorypool));
621 }
622 if (memorypoolend ? (memorypool > memorypoolend) : ((size_t)ptrDiff(memorypool, memorybase) > memorysize)) {
623 std::cerr << "Memory pool size exceeded (" << device << ") (" << res->mName << ": " << (memorypoolend ? (memorysize + ptrDiff(memorypool, memorypoolend)) : ptrDiff(memorypool, memorybase)) << " > " << memorysize << "\n";
624 throw std::bad_alloc();
625 }
626 if (GetProcessingSettings().allocDebugLevel >= 2) {
627 std::cout << "Allocated (" << device << ") " << res->mName << ": " << retVal << " - available: " << (memorypoolend ? ptrDiff(memorypoolend, memorypool) : (memorysize - ptrDiff(memorypool, memorybase))) << "\n";
628 }
629 return retVal;
630}
631
633{
634 if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_INDIVIDUAL && (control == nullptr || control->useInternal())) {
635 if (!(res->mType & GPUMemoryResource::MEMORY_EXTERNAL)) {
636 if (res->mPtrDevice && res->mReuse < 0) {
637 alignedDefaultBufferDeleter()(res->mPtrDevice);
638 }
639 res->mSize = std::max((size_t)res->SetPointers((void*)1) - 1, res->mOverrideSize);
640 if (res->mReuse >= 0) {
641 if (res->mSize > mMemoryResources[res->mReuse].mSize) {
642 GPUError("Invalid reuse, insufficient size: %ld < %ld", (int64_t)mMemoryResources[res->mReuse].mSize, (int64_t)res->mSize);
643 throw std::bad_alloc();
644 }
645 res->mPtrDevice = mMemoryResources[res->mReuse].mPtrDevice;
646 } else {
647 res->mPtrDevice = alignedDefaultBufferAllocator<char>(res->mSize + constants::GPU_BUFFER_ALIGNMENT);
648 }
649 res->mPtr = GPUProcessor::alignPointer<constants::GPU_BUFFER_ALIGNMENT>(res->mPtrDevice);
650 res->SetPointers(res->mPtr);
651 if (GetProcessingSettings().allocDebugLevel >= 2) {
652 std::cout << (res->mReuse >= 0 ? "Reused " : "Allocated ") << res->mName << ": " << res->mSize << " (individual" << ((res->mType & GPUMemoryResource::MEMORY_STACK) ? " stack" : "") << ")\n";
653 }
655 stdspinlock spinlock(mMemoryMutex);
657 }
658 if ((size_t)res->mPtr % constants::GPU_BUFFER_ALIGNMENT) {
659 GPUError("Got buffer with insufficient alignment");
660 throw std::bad_alloc();
661 }
662 }
663 } else {
664 if (res->mPtr != nullptr) {
665 GPUError("Double allocation! (%s)", res->mName);
666 throw std::bad_alloc();
667 }
668 if (IsGPU() && res->mOverrideSize < constants::GPU_BUFFER_ALIGNMENT) {
669 res->mOverrideSize = constants::GPU_BUFFER_ALIGNMENT;
670 }
671 if ((!IsGPU() || (res->mType & GPUMemoryResource::MEMORY_HOST) || GetProcessingSettings().keepDisplayMemory) && !(res->mType & GPUMemoryResource::MEMORY_EXTERNAL)) { // keepAllMemory --> keepDisplayMemory
672 if (control && control->useExternal()) {
673 if (control->allocator) {
674 res->mSize = std::max((size_t)res->SetPointers((void*)1) - 1, res->mOverrideSize);
675 res->mPtr = control->allocator(CAMath::nextMultipleOf<constants::GPU_BUFFER_ALIGNMENT>(res->mSize));
676 res->mSize = std::max<size_t>(ptrDiff(res->SetPointers(res->mPtr), res->mPtr), res->mOverrideSize);
677 if (GetProcessingSettings().allocDebugLevel >= 2) {
678 std::cout << "Allocated (from callback) " << res->mName << ": " << res->mSize << "\n";
679 }
680 } else {
681 void* dummy = nullptr;
682 res->mSize = AllocateRegisteredMemoryHelper(res, res->mPtr, control->ptrCurrent, control->ptrBase, control->size, &GPUMemoryResource::SetPointers, dummy, "host");
683 }
684 } else {
686 }
687 if ((size_t)res->mPtr % constants::GPU_BUFFER_ALIGNMENT) {
688 GPUError("Got buffer with insufficient alignment");
689 throw std::bad_alloc();
690 }
691 }
692 if (IsGPU() && (res->mType & GPUMemoryResource::MEMORY_GPU)) {
693 if (res->mProcessor->mLinkedProcessor == nullptr) {
694 GPUError("Device Processor not set (%s)", res->mName);
695 throw std::bad_alloc();
696 }
698 GPUError("Must not allocate non-stacked device memory while volatile chunks are allocated");
699 throw std::bad_alloc();
700 }
702
704 res->mSize = size;
705 } else if (size != res->mSize) {
706 GPUError("Inconsistent device memory allocation (%s: device %lu vs %lu)", res->mName, size, res->mSize);
707 throw std::bad_alloc();
708 }
709 if ((size_t)res->mPtrDevice % constants::GPU_BUFFER_ALIGNMENT) {
710 GPUError("Got buffer with insufficient alignment");
711 throw std::bad_alloc();
712 }
713 }
715 }
716}
717
722
724{
726 if ((res->mType & GPUMemoryResource::MEMORY_PERMANENT) && res->mPtr != nullptr) {
728 } else {
730 }
731 return res->mReuse >= 0 ? 0 : res->mSize;
732}
733
735{
736 stdspinlock spinlock(mMemoryMutex);
737 if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_INDIVIDUAL) {
738 char* retVal = alignedDefaultBufferAllocator<char>(size);
741 } else {
743 }
744 return retVal;
745 }
746
748 throw std::runtime_error("Requested invalid memory typo for direct allocation");
749 }
751 GPUError("Must not allocate direct memory while volatile chunks are allocated");
752 throw std::bad_alloc();
753 }
754
757 char* retVal;
759 poolend = (char*)poolend - size; // TODO: Implement overflow check
760 poolend = (char*)poolend - GPUProcessor::getAlignmentMod<constants::GPU_MEMALIGN>(poolend);
761 retVal = (char*)poolend;
762 } else {
764 }
765 if (pool > poolend) {
766 GPUError("Insufficient unmanaged memory: missing %ld bytes", ptrDiff(pool, poolend));
767 throw std::bad_alloc();
768 }
770 if (GetProcessingSettings().allocDebugLevel >= 2) {
771 std::cout << "Allocated (unmanaged " << ((type & GPUMemoryResource::MEMORY_GPU) ? "gpu" : "host") << "): " << size << " - available: " << ptrDiff(poolend, pool) << "\n";
772 }
773 return retVal;
774}
775
777{
778 stdspinlock spinlock(mMemoryMutex);
779 if (mVolatileMemoryStart == nullptr) {
781 }
782 if (size == 0) {
783 return nullptr; // Future GPU memory allocation is volatile
784 }
785 char* retVal;
788 GPUError("Insufficient volatile device memory: missing %ld", ptrDiff(mDeviceMemoryPool, mDeviceMemoryPoolEnd));
789 throw std::bad_alloc();
790 }
792 if (GetProcessingSettings().allocDebugLevel >= 2) {
793 std::cout << "Allocated (volatile GPU): " << size << " - available: " << ptrDiff(mDeviceMemoryPoolEnd, mDeviceMemoryPool) << "\n";
794 }
795 return retVal;
796}
797
799{
800 if (device) {
802 }
803 char* retVal = alignedDefaultBufferAllocator<char>(size);
804 stdspinlock spinlock(mMemoryMutex);
806 return retVal;
807}
808
814
816{
820 mVolatileMemoryStart = nullptr;
821 }
822 if (GetProcessingSettings().allocDebugLevel >= 2) {
823 std::cout << "Freed (volatile GPU) - available: " << ptrDiff(mDeviceMemoryPoolEnd, mDeviceMemoryPool) << "\n";
824 }
825}
826
832
834{
835 for (uint32_t i = 0; i < mMemoryResources.size(); i++) {
836 if (proc == nullptr || mMemoryResources[i].mProcessor == proc) {
838 }
839 }
840}
841
843{
846 void* basePtr = res->mReuse >= 0 ? mMemoryResources[res->mReuse].mPtr : res->mPtr;
847 size_t size = ptrDiff(res->SetPointers(basePtr), basePtr);
848 if (basePtr && size > std::max(res->mSize, res->mOverrideSize)) {
849 std::cerr << "Updated pointers exceed available memory size: " << size << " > " << std::max(res->mSize, res->mOverrideSize) << " - host - " << res->mName << "\n";
850 throw std::bad_alloc();
851 }
852 }
853 if (IsGPU() && (res->mType & GPUMemoryResource::MEMORY_GPU)) {
854 void* basePtr = res->mReuse >= 0 ? mMemoryResources[res->mReuse].mPtrDevice : res->mPtrDevice;
855 size_t size = ptrDiff(res->SetDevicePointers(basePtr), basePtr);
856 if (basePtr && size > std::max(res->mSize, res->mOverrideSize)) {
857 std::cerr << "Updated pointers exceed available memory size: " << size << " > " << std::max(res->mSize, res->mOverrideSize) << " - GPU - " << res->mName << "\n";
858 throw std::bad_alloc();
859 }
860 }
861}
862
863void GPUReconstruction::FreeRegisteredMemory(GPUProcessor* proc, bool freeCustom, bool freePermanent)
864{
865 for (uint32_t i = 0; i < mMemoryResources.size(); i++) {
866 if ((proc == nullptr || mMemoryResources[i].mProcessor == proc) && (freeCustom || !(mMemoryResources[i].mType & GPUMemoryResource::MEMORY_CUSTOM)) && (freePermanent || !(mMemoryResources[i].mType & GPUMemoryResource::MEMORY_PERMANENT))) {
868 }
869 }
870}
871
876
878{
879 if (GetProcessingSettings().allocDebugLevel >= 2 && (res->mPtr || res->mPtrDevice)) {
880 std::cout << "Freeing " << res->mName << ": size " << res->mSize << " (reused " << res->mReuse << ")\n";
881 }
882 if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_INDIVIDUAL && res->mReuse < 0) {
883 alignedDefaultBufferDeleter()(res->mPtrDevice);
884 }
885 res->mPtr = nullptr;
886 res->mPtrDevice = nullptr;
887}
888
893
895{
896 if (proc && GetProcessingSettings().memoryAllocationStrategy != GPUMemoryResource::ALLOCATION_INDIVIDUAL) {
897 GPUFatal("Processor-depending memory-free works only with allocation strategy ALLOCATION_INDIVIDUAL");
898 }
899 if (GetProcessingSettings().keepDisplayMemory || GetProcessingSettings().disableMemoryReuse) {
900 return;
901 }
902 if (mNonPersistentMemoryStack.size() == 0) {
903 GPUFatal("Trying to pop memory state from empty stack");
904 }
905 if (tag != 0 && std::get<4>(mNonPersistentMemoryStack.back()) != tag) {
906 GPUFatal("Tag mismatch when popping non persistent memory from stack : pop %s vs on stack %s", qTag2Str(tag).c_str(), qTag2Str(std::get<4>(mNonPersistentMemoryStack.back())).c_str());
907 }
908 if (!proc && (GetProcessingSettings().debugLevel >= 3 || GetProcessingSettings().allocDebugLevel) && (IsGPU() || GetProcessingSettings().forceHostMemoryPoolSize)) {
909 printf("Allocated memory after %30s (%8s) (Stack %zu): ", gpudatatypes::RECO_STEP_NAMES[getRecoStepNum(step, true)], qTag2Str(std::get<4>(mNonPersistentMemoryStack.back())).c_str(), mNonPersistentMemoryStack.size());
911 printf("%76s", "");
913 }
914 for (uint32_t i = std::get<2>(mNonPersistentMemoryStack.back()); i < mNonPersistentIndividualAllocations.size(); i++) {
916 if (proc && res->mProcessor != proc) {
917 continue;
918 }
919 if (GetProcessingSettings().allocDebugLevel >= 2 && (res->mPtr || res->mPtrDevice)) {
920 std::cout << "Freeing NonPersistent " << res->mName << ": size " << res->mSize << " (reused " << res->mReuse << ")\n";
921 }
922 if (res->mReuse < 0) {
923 alignedDefaultBufferDeleter()(res->mPtrDevice);
924 }
925 res->mPtr = nullptr;
926 res->mPtrDevice = nullptr;
927 }
928 if (!proc) {
929 stdspinlock spinlock(mMemoryMutex);
930 mHostMemoryPoolEnd = std::get<0>(mNonPersistentMemoryStack.back());
934 mNonPersistentMemoryStack.pop_back();
935 }
936}
937
939{
941 throw std::runtime_error("temporary memory stack already blocked");
942 }
945}
946
948{
949 if (mNonPersistentMemoryStack.size()) {
950 throw std::runtime_error("cannot unblock while there is stacked memory");
951 }
954 mHostMemoryPoolBlocked = nullptr;
955 mDeviceMemoryPoolBlocked = nullptr;
956}
957
959{
960 mMemoryResources[res].mPtr = ptr;
961}
962
964{
965 for (uint32_t i = 0; i < mMemoryResources.size(); i++) {
968 }
969 }
972 mDirectMemoryChunks.clear();
974 mVolatileChunks.clear();
975 mVolatileMemoryStart = nullptr;
976 if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_GLOBAL) {
977 mHostMemoryPool = GPUProcessor::alignPointer<constants::GPU_MEMALIGN>(mHostMemoryPermanent);
978 mDeviceMemoryPool = GPUProcessor::alignPointer<constants::GPU_MEMALIGN>(mDeviceMemoryPermanent);
981 } else {
983 }
984}
985
991
993{
994 printf("Maximum Memory Allocation: Host %'zu / Device %'zu\n", mHostMemoryUsedMax, mDeviceMemoryUsedMax);
995}
996
998{
999 if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_GLOBAL) {
1000 printf("Memory Allocation: Host %'13zd / %'13zu (Permanent %'13zd, Data %'13zd, Scratch %'13zd), Device %'13zd / %'13zu, (Permanent %'13zd, Data %'13zd, Scratch %'13zd) %zu chunks\n",
1003 mMemoryResources.size());
1004 }
1005}
1006
1008{
1009 std::map<std::string, std::array<size_t, 3>> sizes;
1010 for (uint32_t i = 0; i < mMemoryResources.size(); i++) {
1011 auto& res = mMemoryResources[i];
1012 if (res.mReuse >= 0) {
1013 continue;
1014 }
1015 auto& x = sizes[res.mName];
1016 if (res.mPtr) {
1017 x[0] += res.mSize;
1018 }
1019 if (res.mPtrDevice) {
1020 x[1] += res.mSize;
1021 }
1023 x[2] = 1;
1024 }
1025 }
1026 printf("%59s CPU / %9s GPU\n", "", "");
1027 for (auto it = sizes.begin(); it != sizes.end(); it++) {
1028 printf("Allocation %50s %s: Size %'14zu / %'14zu\n", it->first.c_str(), it->second[2] ? "P" : " ", it->second[0], it->second[1]);
1029 }
1031 for (uint32_t i = 0; i < mChains.size(); i++) {
1032 mChains[i]->PrintMemoryStatistics();
1033 }
1034}
1035
1037{
1038 if (GetProcessingSettings().noGPUMemoryRegistration) {
1039 return 0;
1040 }
1042 if (retVal == 0) {
1043 mRegisteredMemoryPtrs.emplace(ptr);
1044 }
1045 return retVal;
1046}
1047
1049{
1050 if (GetProcessingSettings().noGPUMemoryRegistration) {
1051 return 0;
1052 }
1053 const auto& pos = mRegisteredMemoryPtrs.find(ptr);
1054 if (pos != mRegisteredMemoryPtrs.end()) {
1057 }
1058 return 1;
1059}
1060
1061namespace o2::gpu::internal
1062{
1063namespace // anonymous
1064{
1065template <class T>
1066constexpr static inline int32_t getStepNum(T step, bool validCheck, int32_t N, const char* err = "Invalid step num")
1067{
1068 static_assert(sizeof(step) == sizeof(uint32_t), "Invalid step enum size");
1069 int32_t retVal = 8 * sizeof(uint32_t) - 1 - CAMath::Clz((uint32_t)step);
1070 if ((uint32_t)step == 0 || retVal >= N) {
1071 if (!validCheck) {
1072 return -1;
1073 }
1074 throw std::runtime_error("Invalid General Step");
1075 }
1076 return retVal;
1077}
1078} // anonymous namespace
1079} // namespace o2::gpu::internal
1080
1081int32_t GPUReconstruction::getRecoStepNum(RecoStep step, bool validCheck) { return internal::getStepNum(step, validCheck, gpudatatypes::N_RECO_STEPS, "Invalid Reco Step"); }
1082int32_t GPUReconstruction::getGeneralStepNum(GeneralStep step, bool validCheck) { return internal::getStepNum(step, validCheck, gpudatatypes::N_GENERAL_STEPS, "Invalid General Step"); }
1083
1085{
1086 if (!mInitialized || !GetProcessingSettings().doublePipeline || mMaster != nullptr || !mSlaves.size()) {
1087 throw std::invalid_argument("Cannot start double pipeline mode");
1088 }
1089 if (GetProcessingSettings().debugLevel >= 3) {
1090 GPUInfo("Pipeline worker started");
1091 }
1092 bool terminate = false;
1093 while (!terminate) {
1094 {
1095 std::unique_lock<std::mutex> lk(mPipelineContext->mutex);
1096 mPipelineContext->cond.wait(lk, [this] { return this->mPipelineContext->pipelineQueue.size() > 0; });
1097 }
1098 GPUReconstructionPipelineQueue* q;
1099 {
1100 std::lock_guard<std::mutex> lk(mPipelineContext->mutex);
1101 q = mPipelineContext->pipelineQueue.front();
1102 mPipelineContext->pipelineQueue.pop();
1103 }
1104 if (q->op == 1) {
1105 terminate = 1;
1106 } else {
1107 q->retVal = q->chain->RunChain();
1108 }
1109 {
1110 std::lock_guard<std::mutex> lk(q->m);
1111 q->done = true;
1112 }
1113 q->c.notify_one();
1114 }
1115 if (GetProcessingSettings().debugLevel >= 3) {
1116 GPUInfo("Pipeline worker ended");
1117 }
1118}
1119
1124
1126{
1129 std::unique_ptr<GPUReconstructionPipelineQueue> qu(new GPUReconstructionPipelineQueue);
1130 GPUReconstructionPipelineQueue* q = qu.get();
1131 q->chain = terminate ? nullptr : mChains[0].get();
1132 q->op = terminate ? 1 : 0;
1133 std::unique_lock<std::mutex> lkdone(q->m);
1134 {
1135 std::lock_guard<std::mutex> lkpipe(rec->mPipelineContext->mutex);
1136 if (rec->mPipelineContext->terminate) {
1137 throw std::runtime_error("Must not enqueue work after termination request");
1138 }
1139 rec->mPipelineContext->pipelineQueue.push(q);
1140 rec->mPipelineContext->terminate = terminate;
1141 rec->mPipelineContext->cond.notify_one();
1142 }
1143 q->c.wait(lkdone, [&q]() { return q->done; });
1144 if (terminate || (q->retVal && (q->retVal != 3 || !GetProcessingSettings().ignoreNonFatalGPUErrors))) {
1145 return q->retVal;
1146 }
1147 int32_t retVal2 = mChains[0]->FinalizePipelinedProcessing();
1148 return retVal2 ? retVal2 : q->retVal;
1149}
1150
1152{
1154 std::lock_guard<std::mutex> lk(rec->mPipelineContext->mutex);
1155 return rec->mPipelineContext->pipelineQueue.size() && rec->mPipelineContext->pipelineQueue.front()->op == 0 ? rec->mPipelineContext->pipelineQueue.front()->chain : nullptr;
1156}
1157
1158void GPUReconstruction::PrepareEvent() // TODO: Clean this up, this should not be called from chainTracking but before
1159{
1161 for (uint32_t i = 0; i < mChains.size(); i++) {
1162 mChains[i]->PrepareEvent();
1163 }
1164 for (uint32_t i = 0; i < mProcessors.size(); i++) {
1165 if (mProcessors[i].proc->mAllocateAndInitializeLate) {
1166 continue;
1167 }
1168 (mProcessors[i].proc->*(mProcessors[i].SetMaxData))(mHostConstantMem->ioPtrs);
1169 if (mProcessors[i].proc->mGPUProcessorType != GPUProcessor::PROCESSOR_TYPE_DEVICE && mProcessors[i].proc->mLinkedProcessor) {
1170 (mProcessors[i].proc->mLinkedProcessor->*(mProcessors[i].SetMaxData))(mHostConstantMem->ioPtrs);
1171 }
1172 }
1173 ComputeReuseMax(nullptr);
1174 AllocateRegisteredMemory(nullptr);
1175}
1176
1177int32_t GPUReconstruction::CheckErrorCodes(bool cpuOnly, bool forceShowErrors, std::vector<std::array<uint32_t, 4>>* fillErrors)
1178{
1179 int32_t retVal = 0;
1180 for (uint32_t i = 0; i < mChains.size(); i++) {
1181 if (mChains[i]->CheckErrorCodes(cpuOnly, forceShowErrors, fillErrors)) {
1182 retVal++;
1183 }
1184 }
1185 return retVal;
1186}
1187
1188int32_t GPUReconstruction::GPUChkErrA(const int64_t error, const char* file, int32_t line, bool failOnError)
1189{
1190 if (error == 0 || !GPUChkErrInternal(error, file, line)) {
1191 return 0;
1192 }
1193 if (failOnError) {
1194 if (mInitialized && mInErrorHandling == false) {
1195 mInErrorHandling = true;
1196 CheckErrorCodes(false, true);
1197 }
1198 throw std::runtime_error("GPU Backend Failure");
1199 }
1200 return 1;
1201}
1202
1204{
1205 std::string f;
1206 f = dir;
1207 f += "settings.dump";
1208 DumpStructToFile(mGRPSettings.get(), f.c_str());
1209 for (uint32_t i = 0; i < mChains.size(); i++) {
1210 mChains[i]->DumpSettings(dir);
1211 }
1212}
1213
1214void GPUReconstruction::UpdateDynamicSettings(const GPUSettingsRecDynamic* d)
1215{
1216 UpdateSettings(nullptr, nullptr, d);
1217}
1218
1219void GPUReconstruction::UpdateSettings(const GPUSettingsGRP* g, const GPUSettingsProcessing* p, const GPUSettingsRecDynamic* d)
1220{
1221 if (g) {
1222 *mGRPSettings = *g;
1223 }
1224 if (p) {
1225 mProcessingSettings->debugLevel = p->debugLevel;
1226 mProcessingSettings->resetTimers = p->resetTimers;
1227 }
1228 GPURecoStepConfiguration* w = nullptr;
1230 w = &mRecoSteps;
1231 }
1232 param().UpdateSettings(g, p, w, d);
1233 if (mInitialized) {
1235 }
1236}
1237
1238int32_t GPUReconstruction::ReadSettings(const char* dir)
1239{
1240 std::string f;
1241 f = dir;
1242 f += "settings.dump";
1243 new (mGRPSettings.get()) GPUSettingsGRP;
1244 bool error;
1245 ReadStructFromFile(f.c_str(), mGRPSettings.get(), &error, true);
1246 if (error) {
1247 return 1;
1248 }
1250 for (uint32_t i = 0; i < mChains.size(); i++) {
1251 mChains[i]->ReadSettings(dir);
1252 }
1253 return 0;
1254}
1255
1256void GPUReconstruction::SetSettings(float solenoidBzNominalGPU, const GPURecoStepConfiguration* workflow)
1257{
1258#ifdef GPUCA_STANDALONE
1259 GPUSettingsGRP grp;
1260 grp.solenoidBzNominalGPU = solenoidBzNominalGPU;
1261 SetSettings(&grp, nullptr, nullptr, workflow);
1262#else
1264 config.ReadConfigurableParam(config);
1265 config.configGRP.solenoidBzNominalGPU = solenoidBzNominalGPU;
1266 SetSettings(&config.configGRP, &config.configReconstruction, &config.configProcessing, workflow);
1267#endif
1268}
1269
1270void GPUReconstruction::SetSettings(const GPUSettingsGRP* grp, const GPUSettingsRec* rec, const GPUSettingsProcessing* proc, const GPURecoStepConfiguration* workflow)
1271{
1272 if (mInitialized) {
1273 GPUError("Cannot update settings while initialized");
1274 throw std::runtime_error("Settings updated while initialized");
1275 }
1276 *mGRPSettings = *grp;
1277 if (proc) {
1278 *mProcessingSettings = *proc;
1279 }
1280 if (workflow) {
1281 mRecoSteps.steps = workflow->steps;
1283 mRecoSteps.inputs = workflow->inputs;
1284 mRecoSteps.outputs = workflow->outputs;
1285 }
1286 param().SetDefaults(mGRPSettings.get(), rec, proc, workflow);
1287}
1288
1290{
1291 GPUOutputControl outputControl;
1292 outputControl.set(ptr, size);
1293 SetOutputControl(outputControl);
1294}
1295
1299void GPUReconstruction::SetResetTimers(bool reset) { mProcessingSettings->resetTimers = reset; }
1304
1305ThrustVolatileAllocator::ThrustVolatileAllocator(GPUReconstruction* r)
1306{
1307 mAlloc = [&r](size_t n) { return (char*)r->AllocateVolatileDeviceMemory(n); };
1308}
int32_t i
uint32_t op
bool done
int32_t retVal
GPUChain * chain
uint16_t pos
Definition RawData.h:3
uint32_t res
Definition RawData.h:0
TBranch * ptr
void set(S v)
Definition bitfield.h:55
bool isSet(const bitfield &v) const
Definition bitfield.h:66
const GPUSettingsDisplay * GetEventDisplayConfig() const
const GPUSettingsQA * GetQAConfig() const
static void dumpConfig(const GPUSettingsRec *rec, const GPUSettingsProcessing *proc, const GPUSettingsQA *qa, const GPUSettingsDisplay *display, const GPUSettingsDeviceBackend *device, const GPURecoStepConfiguration *workflow)
void * SetDevicePointers(void *ptr) const
void * SetPointers(void *ptr) const
static void computePointerWithAlignment(T *&basePtr, S *&objPtr, size_t nEntries=1)
void InitGPUProcessor(GPUReconstruction *rec, ProcessorType type=PROCESSOR_TYPE_CPU, GPUProcessor *slaveProcessor=nullptr)
ProcessorType mGPUProcessorType
GPURecoStepConfiguration mRecoSteps
std::unordered_set< const void * > mRegisteredMemoryPtrs
int16_t RegisterMemoryAllocationHelper(GPUProcessor *proc, void *(GPUProcessor::*setPtr)(void *), int32_t type, const char *name, const GPUMemoryReuse &re)
std::vector< std::unique_ptr< GPUChain > > mChains
void * AllocateVolatileMemory(size_t size, bool device)
ThrustVolatileAllocator getThrustVolatileDeviceAllocator()
std::unique_ptr< GPUMemorySizeScalers > mMemoryScalers
void AllocateRegisteredForeignMemory(int16_t res, GPUReconstruction *rec, GPUOutputControl *control=nullptr)
std::unique_ptr< T > ReadStructFromFile(const char *file, T *obj=nullptr, bool *errorOnMissing=nullptr, bool allowSmaller=false)
void SetInputControl(void *ptr, size_t size)
GPUConstantMem * mDeviceConstantMem
void ConstructGPUProcessor(GPUProcessor *proc)
std::shared_ptr< GPUROOTDumpCore > mROOTDump
const GPUSettingsDeviceBackend & GetDeviceBackendSettings() const
void ComputeReuseMax(GPUProcessor *proc)
void SetMemoryExternalInput(int16_t res, void *ptr)
int32_t getGeneralStepNum(GeneralStep step, bool validCheck=true)
static constexpr uint32_t NSECTORS
RecoStepField GetRecoStepsGPU() const
void RegisterGPUDeviceProcessor(GPUProcessor *proc, GPUProcessor *slaveProcessor)
std::vector< GPUReconstruction * > mSlaves
std::vector< std::tuple< void *, void *, size_t, size_t, uint64_t > > mNonPersistentMemoryStack
alignedDeleter< char, constants::GPU_BUFFER_ALIGNMENT > alignedDefaultBufferDeleter
void UpdateDynamicSettings(const GPUSettingsRecDynamic *d)
std::unique_ptr< GPUSettingsDeviceBackend > mDeviceBackendSettings
std::vector< GPUMemoryResource > mMemoryResources
std::unique_ptr< GPUReconstructionPipelineContext > mPipelineContext
std::unique_ptr< GPUConstantMem > mHostConstantMem
void ResetRegisteredMemoryPointers(GPUProcessor *proc)
void DumpStructToFile(const T *obj, const char *file)
void AllocateRegisteredMemoryInternal(GPUMemoryResource *res, GPUOutputControl *control, GPUReconstruction *recPool)
std::vector< std::unique_ptr< char[], alignedDefaultBufferDeleter > > mVolatileChunks
virtual int32_t registerMemoryForGPU_internal(const void *ptr, size_t size)=0
virtual size_t WriteToConstantMemory(size_t offset, const void *src, size_t size, int32_t stream=-1, gpu_reconstruction_kernels::deviceEvent *ev=nullptr)=0
std::unordered_map< GPUMemoryReuse::ID, MemoryReuseMeta > mMemoryReuse1to1
std::vector< ProcessorData > mProcessors
void * AllocateVolatileDeviceMemory(size_t size)
virtual int32_t InitDevice()=0
void SetSettings(float solenoidBzNominalGPU, const GPURecoStepConfiguration *workflow=nullptr)
const GPUCalibObjectsConst & GetCalib() const
const GPUTrackingInOutPointers GetIOPtrs() const
virtual std::unique_ptr< gpu_reconstruction_kernels::threadContext > GetThreadContext()=0
GPUReconstruction(const GPUReconstruction &)=delete
static constexpr GeometryType geometryType
void WriteConstantParams(int32_t stream=-1)
void FreeRegisteredMemory(GPUProcessor *proc, bool freeCustom=false, bool freePermanent=false)
virtual RecoStepField AvailableGPURecoSteps()
static constexpr const char *const IOTYPENAMES[]
void PopNonPersistentMemory(RecoStep step, uint64_t tag, const GPUProcessor *proc=nullptr)
void UpdateSettings(const GPUSettingsGRP *g, const GPUSettingsProcessing *p=nullptr, const GPUSettingsRecDynamic *d=nullptr)
int32_t CheckErrorCodes(bool cpuOnly=false, bool forceShowErrors=false, std::vector< std::array< uint32_t, 4 > > *fillErrors=nullptr)
const GPUParam & GetParam() const
void ClearAllocatedMemory(bool clearOutputs=true)
static constexpr const char *const GEOMETRY_TYPE_NAMES[]
virtual int32_t ExitDevice()=0
std::unique_ptr< GPUSettingsGRP > mGRPSettings
std::unique_ptr< GPUSettingsProcessing > mProcessingSettings
void PushNonPersistentMemory(uint64_t tag)
int32_t getRecoStepNum(RecoStep step, bool validCheck=true)
virtual int32_t unregisterMemoryForGPU_internal(const void *ptr)=0
void BlockStackedMemory(GPUReconstruction *rec)
const GPUSettingsProcessing & GetProcessingSettings() const
void DumpSettings(const char *dir="")
std::vector< std::unique_ptr< char[], alignedDefaultBufferDeleter > > mNonPersistentIndividualDirectAllocations
void * AllocateDirectMemory(size_t size, int32_t type)
virtual void GetITSTraits(std::unique_ptr< o2::its::TrackerTraits< 7 > > *trackerTraits, std::unique_ptr< o2::its::VertexerTraits< 7 > > *vertexerTraits, std::unique_ptr< o2::its::TimeFrame< 7 > > *timeFrame)
int32_t unregisterMemoryForGPU(const void *ptr)
int32_t registerMemoryForGPU(const void *ptr, size_t size)
void SetDebugLevelTmp(int32_t level)
size_t AllocateRegisteredMemoryHelper(GPUMemoryResource *res, void *&ptr, void *&memorypool, void *memorybase, size_t memorysize, void *(GPUMemoryResource::*SetPointers)(void *) const, void *&memorypoolend, const char *device)
std::vector< std::unique_ptr< char[], alignedDefaultBufferDeleter > > mDirectMemoryChunks
int32_t EnqueuePipeline(bool terminate=false)
std::shared_ptr< GPUReconstructionThreading > mThreading
std::vector< GPUMemoryResource * > mNonPersistentIndividualAllocations
virtual int32_t GPUChkErrInternal(const int64_t error, const char *file, int32_t line) const
int32_t GPUChkErrA(const int64_t error, const char *file, int32_t line, bool failOnError)
size_t AllocateRegisteredMemory(GPUProcessor *proc, bool resetCustom=false)
int32_t ReadSettings(const char *dir="")
void SetOutputControl(const GPUOutputControl &v)
static constexpr uint32_t NSECTORS
void SetSector(int32_t iSector)
#define TPC_MAX_FRAGMENT_LEN_GPU
#define TPC_MAX_FRAGMENT_LEN_HOST
GLdouble n
Definition glcorearb.h:1982
GLint GLenum GLint x
Definition glcorearb.h:403
const GLfloat * m
Definition glcorearb.h:4066
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
Definition glcorearb.h:2595
GLuint const GLchar * name
Definition glcorearb.h:781
GLdouble f
Definition glcorearb.h:310
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLboolean GLboolean g
Definition glcorearb.h:1233
GLint level
Definition glcorearb.h:275
GLboolean r
Definition glcorearb.h:1233
GLenum GLfloat param
Definition glcorearb.h:271
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLuint GLuint stream
Definition glcorearb.h:1806
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
std::unique_ptr< GPUDisplayFrontendInterface > eventDisplay
GPUReconstruction * rec
std::string qTag2Str(const T tag)
Definition strtag.h:36
GPUTPCTracker tpcTrackers[GPUTPCGeometry::NSECTORS]
GPUCalibObjectsConst calibObjects
GPUTPCClusterFinder tpcClusterer[GPUTPCGeometry::NSECTORS]
void set(void *p, size_t s)
std::function< void *(size_t)> allocator
void SetDefaults(float solenoidBz, bool assumeConstantBz=false)
Definition GPUParam.cxx:33
void UpdateSettings(const GPUSettingsGRP *g, const GPUSettingsProcessing *p=nullptr, const GPURecoStepConfiguration *w=nullptr, const GPUSettingsRecDynamic *d=nullptr)
Definition GPUParam.cxx:95
gpudatatypes::RecoStepField steps
gpudatatypes::InOutTypeField inputs
gpudatatypes::RecoStepField stepsGPUMask
gpudatatypes::InOutTypeField outputs
std::queue< GPUReconstructionPipelineQueue * > pipelineQueue