Skip to content

Instantly share code, notes, and snippets.

@wangjiezhe
Last active December 31, 2023 11:20
Show Gist options
  • Save wangjiezhe/8e11f8957b058cb4e37de24fdbd7f5f1 to your computer and use it in GitHub Desktop.
Save wangjiezhe/8e11f8957b058cb4e37de24fdbd7f5f1 to your computer and use it in GitHub Desktop.
Get PCI Bus ID for Nvidia GPU. Compiled and run by `nvcc -arch=sm_89 nvidia_pcibusid.cc -o nvidia_pcibusid -lcuda -run`
#include <iostream>
#include <cuda_runtime.h>
#include <cuda.h>
int main() {
CUresult cuResult;
cuInit(0);
int deviceCount;
cuResult = cuDeviceGetCount(&deviceCount);
if (cuResult != CUDA_SUCCESS || deviceCount == 0) {
std::cerr << "No CUDA-capable devices found" << std::endl;
return 1;
}
for (int deviceId = 0; deviceId < deviceCount; ++deviceId) {
CUdevice cuDevice;
cuResult = cuDeviceGet(&cuDevice, deviceId);
if (cuResult != CUDA_SUCCESS) {
std::cerr << "Error getting CUDA device" << std::endl;
return 1;
}
char deviceName[256];
cuDeviceGetName(deviceName, sizeof(deviceName), cuDevice);
char pciBusId[16];
cuResult = cuDeviceGetPCIBusId(pciBusId, sizeof(pciBusId), cuDevice);
if (cuResult != CUDA_SUCCESS) {
std::cerr << "Error getting PCI Bus ID" << std::endl;
return 1;
}
std::cout << "GPU Device ID: " << deviceId << std::endl;
std::cout << " Device Name: " << deviceName << std::endl;
std::cout << " PCI Bus ID: " << pciBusId << std::endl;
}
cuResult = cuInit(0); // Cleanup CUDA Driver API
if (cuResult != CUDA_SUCCESS) {
std::cerr << "Error cleaning up CUDA Driver API" << std::endl;
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment