ATLAS Offline Software
Loading...
Searching...
No Matches
AthCUDA/AthCUDAServices/src/DeviceMemoryResourceTool.cxx
Go to the documentation of this file.
1//
2// Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3//
4
5// Local include(s).
7
8// CUDA include(s).
9#include <cuda_runtime_api.h>
10
11// System include(s).
12#include <cassert>
13
14namespace AthCUDA {
15
17
18 // Construct the appropriate memory resource.
19 m_mr = std::make_unique<vecmem::cuda::device_memory_resource>(m_deviceID);
20
21 // Figure out which device the memory resource was created for. Unfortunately
22 // vecmem doesn't advertise this in a convenient way at the moment. :( So we
23 // have to re-create its logic here.
24 int deviceID = m_deviceID;
25 if (deviceID == vecmem::cuda::device_memory_resource::INVALID_DEVICE) {
26 // The default device was requested. We have to query for the current
27 // device.
28 cudaError_t err = cudaGetDevice(&deviceID);
29 if (err != cudaSuccess) {
30 ATH_MSG_ERROR("Failed to query the current CUDA device: "
31 << cudaGetErrorString(err));
32 return StatusCode::FAILURE;
33 }
34 }
35
36 // Get the device's properties.
37 cudaDeviceProp props;
38 cudaError_t err = cudaGetDeviceProperties(&props, deviceID);
39 if (err != cudaSuccess) {
40 ATH_MSG_ERROR("Failed to get the CUDA device properties: "
41 << cudaGetErrorString(err));
42 return StatusCode::FAILURE;
43 }
44
45 // Construct a unique name out of those properties.
46 std::ostringstream deviceName;
47 deviceName << props.name << " [id: " << deviceID
48 << ", bus: " << props.pciBusID << ", device: " << props.pciDeviceID
49 << "]";
51
52 // Return gracefully.
53 return StatusCode::SUCCESS;
54}
55
56std::pmr::memory_resource& DeviceMemoryResourceTool::mr() const {
57
58 assert(m_mr);
59 return *m_mr;
60}
61
62std::string_view DeviceMemoryResourceTool::deviceName() const {
63
64 return m_deviceName;
65}
66
67} // namespace AthCUDA
#define ATH_MSG_ERROR(x,...)
std::unique_ptr< std::pmr::memory_resource > m_mr
The memory resource that this tool uses.
std::string m_deviceName
Name of the device for which the memory resource is provided.
virtual std::pmr::memory_resource & mr() const override
Get the provided std::pmr::memory_resource object.
Gaudi::Property< int > m_deviceID
Device to allocate memory on.
virtual std::string_view deviceName() const override
The name of the device for which the memory resource is provided.
virtual StatusCode initialize() override
Initialize the tool.
Definition Info.h:14