Skip to content

Commit eec1eda

Browse files
committed
[coro_io] add pci_bus_id() to cuda_device and cache result at init
1 parent aaab428 commit eec1eda

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

include/ylt/coro_io/cuda/cuda_device.hpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#pragma once
1717

1818
#include <atomic>
19+
#include <cstdio>
1920
#include <memory>
2021
#include <mutex>
2122
#include <span>
@@ -132,13 +133,33 @@ class cuda_device_t : public std::enable_shared_from_this<cuda_device_t> {
132133
else {
133134
name_.clear();
134135
}
135-
ELOG_INFO << "Get cuda device(" << gpu_id_ << "): " << name_;
136+
pci_bus_id_ = query_pci_bus_id();
137+
ELOG_INFO << "Get cuda device(" << gpu_id_ << "): " << name_
138+
<< ", PCI: " << pci_bus_id_;
136139
}
137140
int get_gpu_id() const noexcept { return gpu_id_; }
138141
std::string_view name() const noexcept { return name_; }
139142

143+
// Returns PCI bus ID string in "DDDD:BB:DD.0" format.
144+
std::string pci_bus_id() const { return pci_bus_id_; }
145+
140146
private:
147+
std::string query_pci_bus_id() const {
148+
int domain_id = 0, bus_id = 0, dev_id = 0;
149+
YLT_CHECK_CUDA_ERR(cuDeviceGetAttribute(
150+
&domain_id, CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID, device_));
151+
YLT_CHECK_CUDA_ERR(
152+
cuDeviceGetAttribute(&bus_id, CU_DEVICE_ATTRIBUTE_PCI_BUS_ID, device_));
153+
YLT_CHECK_CUDA_ERR(cuDeviceGetAttribute(
154+
&dev_id, CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID, device_));
155+
char buf[64];
156+
std::snprintf(buf, sizeof(buf), "%04x:%02x:%02x.0", domain_id, bus_id,
157+
dev_id);
158+
return std::string(buf);
159+
}
160+
141161
std::string name_;
162+
std::string pci_bus_id_;
142163
int gpu_id_;
143164
CUcontext context_;
144165
CUdevice device_;

0 commit comments

Comments
 (0)