ATLAS Offline Software
Loading...
Searching...
No Matches
GPUSystemInfoSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "GaudiKernel/IIncidentSvc.h"
7#include "GaudiKernel/ConcurrencyFlags.h"
8#include "GPUSystemInfoSvc.h"
9#include <cuda_runtime.h>
10
11namespace AthCUDA {
12
14 const bool isMultiprocess = (Gaudi::Concurrency::ConcurrencyFlags::numProcs() > 0);
15 if (isMultiprocess) {
16 SmartIF<IIncidentSvc> incsvc{service("IncidentSvc")};
17 incsvc->addListener( this, AthenaInterprocess::UpdateAfterFork::type(), 1000); // high priority- before other GPU functions
18
19 ATH_MSG_DEBUG("In multiprocess mode the CUDA context will be only checked after fork");
20 return StatusCode::SUCCESS;
21 }
22 else {
24
25 ATH_MSG_DEBUG("GPU availability check in initialize " << std::boolalpha << (m_deviceInfo.size() > 0));
26 return StatusCode::SUCCESS;
27 }
28 }
29
30 void GPUSystemInfoSvc::handle(const Incident&) {
32 ATH_MSG_DEBUG("GPU availability check in UpdateAfterFork " << std::boolalpha << (m_deviceInfo.size() > 0));
33 }
34
35 const std::vector<DeviceInfo>& GPUSystemInfoSvc::getAvailableDevices() const {
36 if (m_wasChecked == false) {
37 ATH_MSG_WARNING("Function was called before service initialization!");
38 }
39 return m_deviceInfo;
40 }
41
43 std::call_once(m_readDevicesOnceFlag, [this]() {
44 std::string cudaErrorStr;
45
46 int deviceCount = 0;
47 cudaError_t error = cudaGetDeviceCount(&deviceCount);
48 if (error != cudaSuccess) {
49 cudaErrorStr = cudaGetErrorString(error);
50 ATH_MSG_DEBUG("Error in cudaGetDeviceCount " << cudaErrorStr);
51 }
52
53 for (int i = 0; i < deviceCount; i++) {
54 cudaDeviceProp prop;
55 error = cudaGetDeviceProperties(&prop, i);
56
57 if (error != cudaSuccess) {
58 cudaErrorStr = cudaGetErrorString(error);
59 ATH_MSG_DEBUG("Error in cudaGetDeviceProperties for device " << i << " " << cudaErrorStr);
60 continue;
61 }
62 m_deviceInfo.emplace_back(i, prop.name, prop.major, prop.minor);
63 }
64
65
66 if (m_deviceInfo.empty()) {
67 ATH_MSG_DEBUG("No CUDA Devices not available");
68 return;
69 }
70
71 m_wasChecked = true;
72 });
73 }
74}
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
virtual StatusCode initialize() override
std::vector< DeviceInfo > m_deviceInfo
virtual const std::vector< DeviceInfo > & getAvailableDevices() const override
virtual void handle(const Incident &) override
std::once_flag m_readDevicesOnceFlag
static const std::string & type()
Incident type.
Definition Incidents.h:49
Definition Info.h:14