Skip to content

Commit f017263

Browse files
authored
ALICE3: factor GPU tracking into dynamically loaded CUDA/HIP backend (#15420)
1 parent aa96c1a commit f017263

7 files changed

Lines changed: 342 additions & 276 deletions

File tree

Detectors/Upgrades/ALICE3/GlobalReconstruction/reconstruction/CMakeLists.txt

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,9 @@ if(Acts_FOUND)
1313
set(actsTarget Acts::Core)
1414
endif()
1515

16-
set(alice3GlobalRecoGpuSources "")
17-
set(alice3GlobalRecoGpuTargets "")
18-
set(alice3GlobalRecoGpuPrivateTargets "")
19-
if(CUDA_ENABLED)
20-
find_package(CUDAToolkit REQUIRED)
21-
list(APPEND alice3GlobalRecoGpuSources src/TimeFrameGPU.cxx src/GPUExternalAllocator.cxx)
22-
list(APPEND alice3GlobalRecoGpuTargets O2::ITStrackingCUDA)
23-
list(APPEND alice3GlobalRecoGpuPrivateTargets CUDA::cudart)
24-
elseif(HIP_ENABLED)
25-
list(APPEND alice3GlobalRecoGpuSources src/TimeFrameGPU.cxx src/GPUExternalAllocator.cxx)
26-
list(APPEND alice3GlobalRecoGpuTargets O2::ITStrackingHIP)
27-
list(APPEND alice3GlobalRecoGpuPrivateTargets hip::host)
28-
endif()
29-
3016
o2_add_library(ALICE3GlobalReconstruction
3117
TARGETVARNAME targetName
3218
SOURCES src/TimeFrame.cxx
33-
${alice3GlobalRecoGpuSources}
3419
$<$<BOOL:${Acts_FOUND}>:src/TrackerACTS.cxx>
3520
PUBLIC_LINK_LIBRARIES
3621
O2::ITStracking
@@ -48,26 +33,10 @@ o2_add_library(ALICE3GlobalReconstruction
4833
O2::TRKReconstruction
4934
O2::TRKSimulation
5035
nlohmann_json::nlohmann_json
51-
${alice3GlobalRecoGpuTargets}
5236
${actsTarget}
5337
PRIVATE_LINK_LIBRARIES
5438
O2::Steer
55-
TBB::tbb
56-
${alice3GlobalRecoGpuPrivateTargets})
57-
58-
if(alice3GlobalRecoGpuTargets)
59-
target_compile_definitions(${targetName} PUBLIC TRK_HAS_GPU_TRACKING)
60-
endif()
61-
62-
if(CUDA_ENABLED)
63-
target_include_directories(${targetName} PRIVATE ${CUDAToolkit_INCLUDE_DIRS})
64-
endif()
65-
66-
if(CUDA_ENABLED)
67-
target_compile_definitions(${targetName} PUBLIC TRK_HAS_CUDA_TRACKING)
68-
elseif(HIP_ENABLED)
69-
target_compile_definitions(${targetName} PUBLIC TRK_HAS_HIP_TRACKING)
70-
endif()
39+
TBB::tbb)
7140

7241
if(Acts_FOUND)
7342
target_compile_definitions(${targetName} PUBLIC O2_WITH_ACTS)

Detectors/Upgrades/ALICE3/GlobalReconstruction/reconstruction/src/GPUExternalAllocator.cxx renamed to Detectors/Upgrades/ALICE3/GlobalReconstruction/reconstruction/src/GPUExternalAllocator.cu

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
// granted to it by virtue of its status as an Intergovernmental Organization
1010
// or submit itself to any jurisdiction.
1111

12-
#if defined(TRK_HAS_CUDA_TRACKING)
12+
#define GPUCA_GPUCODE_HOSTONLY
13+
1314
#include <cuda_runtime.h>
14-
#elif defined(TRK_HAS_HIP_TRACKING)
15-
#include <hip/hip_runtime.h>
16-
#endif
1715

1816
#include "ALICE3GlobalReconstruction/GPUExternalAllocator.h"
1917

@@ -23,21 +21,12 @@
2321

2422
namespace
2523
{
26-
#if defined(TRK_HAS_CUDA_TRACKING)
2724
void checkGpuError(cudaError_t error, const char* call)
2825
{
2926
if (error != cudaSuccess) {
3027
throw std::runtime_error(std::string(call) + ": " + cudaGetErrorString(error));
3128
}
3229
}
33-
#elif defined(TRK_HAS_HIP_TRACKING)
34-
void checkGpuError(hipError_t error, const char* call)
35-
{
36-
if (error != hipSuccess) {
37-
throw std::runtime_error(std::string(call) + ": " + hipGetErrorString(error));
38-
}
39-
}
40-
#endif
4130
} // namespace
4231

4332
namespace o2::trk
@@ -147,26 +136,14 @@ void GPUExternalAllocator::releaseAll()
147136
void* GPUExternalAllocator::allocateHost(size_t size)
148137
{
149138
void* ptr = nullptr;
150-
#if defined(TRK_HAS_CUDA_TRACKING)
151139
checkGpuError(cudaHostAlloc(&ptr, size, cudaHostAllocPortable), "cudaHostAlloc");
152-
#elif defined(TRK_HAS_HIP_TRACKING)
153-
checkGpuError(hipHostMalloc(&ptr, size, hipHostMallocPortable), "hipHostMalloc");
154-
#else
155-
throw std::runtime_error("GPUExternalAllocator built without a GPU backend");
156-
#endif
157140
return ptr;
158141
}
159142

160143
void* GPUExternalAllocator::allocateDevice(size_t size)
161144
{
162145
void* ptr = nullptr;
163-
#if defined(TRK_HAS_CUDA_TRACKING)
164146
checkGpuError(cudaMalloc(&ptr, size), "cudaMalloc");
165-
#elif defined(TRK_HAS_HIP_TRACKING)
166-
checkGpuError(hipMalloc(&ptr, size), "hipMalloc");
167-
#else
168-
throw std::runtime_error("GPUExternalAllocator built without a GPU backend");
169-
#endif
170147
return ptr;
171148
}
172149

@@ -176,21 +153,11 @@ void GPUExternalAllocator::freeAllocation(void* ptr, AllocationSpace space)
176153
return;
177154
}
178155

179-
#if defined(TRK_HAS_CUDA_TRACKING)
180156
if (space == AllocationSpace::Host) {
181157
checkGpuError(cudaFreeHost(ptr), "cudaFreeHost");
182158
} else {
183159
checkGpuError(cudaFree(ptr), "cudaFree");
184160
}
185-
#elif defined(TRK_HAS_HIP_TRACKING)
186-
if (space == AllocationSpace::Host) {
187-
checkGpuError(hipHostFree(ptr), "hipHostFree");
188-
} else {
189-
checkGpuError(hipFree(ptr), "hipFree");
190-
}
191-
#else
192-
(void)space;
193-
#endif
194161
}
195162

196163
void GPUExternalAllocator::removeFromTagLocked(uint64_t tag, void* ptr)

Detectors/Upgrades/ALICE3/GlobalReconstruction/workflow/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,42 @@ o2_add_library(ALICE3GlobalReconstructionWorkflow
2424
O2::TRKBase
2525
O2::TRKSimulation
2626
O2::ALICE3GlobalReconstruction
27+
O2::CommonUtils
2728
nlohmann_json::nlohmann_json)
2829

30+
if(CUDA_ENABLED OR HIP_ENABLED)
31+
target_compile_definitions(${targetName} PUBLIC TRK_HAS_GPU_TRACKING)
32+
endif()
33+
34+
if(CUDA_ENABLED)
35+
find_package(CUDAToolkit REQUIRED)
36+
target_compile_definitions(${targetName} PUBLIC TRK_HAS_CUDA_TRACKING)
37+
o2_add_library(ALICE3GlobalReconstructionWorkflowCUDA
38+
TARGETVARNAME cudaTargetName
39+
SOURCES src/TrackerSpecGPU.cxx
40+
../reconstruction/src/TimeFrameGPU.cxx
41+
../reconstruction/src/GPUExternalAllocator.cu
42+
PUBLIC_LINK_LIBRARIES
43+
O2::ALICE3GlobalReconstructionWorkflow
44+
O2::ITStrackingCUDA
45+
PRIVATE_LINK_LIBRARIES
46+
CUDA::cudart)
47+
target_include_directories(${cudaTargetName} PRIVATE ${CUDAToolkit_INCLUDE_DIRS})
48+
endif()
49+
50+
if(HIP_ENABLED)
51+
target_compile_definitions(${targetName} PUBLIC TRK_HAS_HIP_TRACKING)
52+
o2_add_hipified_library(ALICE3GlobalReconstructionWorkflowHIP
53+
SOURCES src/TrackerSpecGPU.cxx
54+
../reconstruction/src/TimeFrameGPU.cxx
55+
../reconstruction/src/GPUExternalAllocator.cu
56+
PUBLIC_LINK_LIBRARIES
57+
O2::ALICE3GlobalReconstructionWorkflow
58+
O2::ITStrackingHIP
59+
PRIVATE_LINK_LIBRARIES
60+
hip::host)
61+
endif()
62+
2963
o2_add_executable(reco-workflow
3064
SOURCES src/alice3-global-reconstruction-workflow.cxx
3165
COMPONENT_NAME alice3-global-reconstruction

Detectors/Upgrades/ALICE3/GlobalReconstruction/workflow/include/ALICE3GlobalReconstructionWorkflow/TrackerSpec.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
#include <nlohmann/json.hpp>
3434

35+
#include <memory>
36+
#include <utility>
37+
#include <vector>
38+
3539
namespace o2::trk
3640
{
3741
class TrackerDPL : public framework::Task
@@ -48,10 +52,15 @@ class TrackerDPL : public framework::Task
4852
void endOfStream(framework::EndOfStreamContext& ec) final;
4953
// void finaliseCCDB(framework::ConcreteDataMatcher& matcher, void* obj) final;
5054
void stop() final;
55+
template <typename TimeFrameT, typename TrackerTraitsT>
56+
void runTracking(framework::ProcessingContext& pc, TimeFrameT& timeFrame, TrackerTraitsT& trackerTraits);
57+
const std::shared_ptr<its::ExternalAllocator>& getGPUAllocator() const noexcept { return mGPUAllocator; }
58+
void setGPUAllocator(std::shared_ptr<its::ExternalAllocator> allocator) { mGPUAllocator = std::move(allocator); }
5159

5260
private:
5361
void updateTimeDependentParams(framework::ProcessingContext& pc);
5462
std::vector<o2::its::TrackingParameters> createTrackingParamsFromConfig();
63+
void runGPUTracking(framework::ProcessingContext& pc);
5564
// std::unique_ptr<o2::gpu::GPUReconstruction> mRecChain = nullptr;
5665
// std::unique_ptr<o2::gpu::GPUChainITS> mChainITS = nullptr;
5766
// std::shared_ptr<o2::base::GRPGeomRequest> mGGCCDBRequest;
@@ -61,6 +70,7 @@ class TrackerDPL : public framework::Task
6170
std::shared_ptr<its::BoundedMemoryResource> mMemoryPool;
6271
std::shared_ptr<its::ExternalAllocator> mGPUAllocator;
6372
std::shared_ptr<tbb::task_arena> mTaskArena;
73+
std::vector<o2::its::TrackingParameters> mTrackingParams;
6474
nlohmann::json mHitRecoConfig;
6575
nlohmann::json mClusterRecoConfig;
6676
TStopwatch mTimer;

0 commit comments

Comments
 (0)