1 //===- NVPTXArch.cpp - list installed NVPTX devies ------*- C++ -*---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements a tool for detecting name of CUDA gpus installed in the
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/Version.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/DynamicLibrary.h"
17 #include "llvm/Support/Error.h"
24 static cl::opt
<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden
);
26 static void PrintVersion(raw_ostream
&OS
) {
27 OS
<< clang::getClangToolFullVersion("nvptx-arch") << '\n';
29 // Mark all our options with this category, everything else (except for -version
30 // and -help) will be hidden.
31 static cl::OptionCategory
NVPTXArchCategory("nvptx-arch options");
33 typedef enum cudaError_enum
{
35 CUDA_ERROR_NO_DEVICE
= 100,
38 typedef enum CUdevice_attribute_enum
{
39 CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR
= 75,
40 CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR
= 76,
43 typedef uint32_t CUdevice
;
45 CUresult (*cuInit
)(unsigned int);
46 CUresult (*cuDeviceGetCount
)(int *);
47 CUresult (*cuGetErrorString
)(CUresult
, const char **);
48 CUresult (*cuDeviceGet
)(CUdevice
*, int);
49 CUresult (*cuDeviceGetAttribute
)(int *, CUdevice_attribute
, CUdevice
);
51 constexpr const char *DynamicCudaPath
= "libcuda.so.1";
53 llvm::Error
loadCUDA() {
55 auto DynlibHandle
= std::make_unique
<llvm::sys::DynamicLibrary
>(
56 llvm::sys::DynamicLibrary::getPermanentLibrary(DynamicCudaPath
, &ErrMsg
));
57 if (!DynlibHandle
->isValid()) {
58 return llvm::createStringError(llvm::inconvertibleErrorCode(),
59 "Failed to 'dlopen' %s", DynamicCudaPath
);
61 #define DYNAMIC_INIT(SYMBOL) \
63 void *SymbolPtr = DynlibHandle->getAddressOfSymbol(#SYMBOL); \
65 return llvm::createStringError(llvm::inconvertibleErrorCode(), \
66 "Failed to 'dlsym' " #SYMBOL); \
67 SYMBOL = reinterpret_cast<decltype(SYMBOL)>(SymbolPtr); \
70 DYNAMIC_INIT(cuDeviceGetCount
);
71 DYNAMIC_INIT(cuGetErrorString
);
72 DYNAMIC_INIT(cuDeviceGet
);
73 DYNAMIC_INIT(cuDeviceGetAttribute
);
75 return llvm::Error::success();
78 static int handleError(CUresult Err
) {
79 const char *ErrStr
= nullptr;
80 CUresult Result
= cuGetErrorString(Err
, &ErrStr
);
81 if (Result
!= CUDA_SUCCESS
)
83 fprintf(stderr
, "CUDA error: %s\n", ErrStr
);
87 int main(int argc
, char *argv
[]) {
88 cl::HideUnrelatedOptions(NVPTXArchCategory
);
90 cl::SetVersionPrinter(PrintVersion
);
91 cl::ParseCommandLineOptions(
93 "A tool to detect the presence of NVIDIA devices on the system. \n\n"
94 "The tool will output each detected GPU architecture separated by a\n"
95 "newline character. If multiple GPUs of the same architecture are found\n"
96 "a string will be printed for each\n");
99 cl::PrintHelpMessage();
103 // Attempt to load the NVPTX driver runtime.
104 if (llvm::Error Err
= loadCUDA()) {
105 logAllUnhandledErrors(std::move(Err
), llvm::errs());
109 if (CUresult Err
= cuInit(0)) {
110 if (Err
== CUDA_ERROR_NO_DEVICE
)
113 return handleError(Err
);
117 if (CUresult Err
= cuDeviceGetCount(&Count
))
118 return handleError(Err
);
121 for (int DeviceId
= 0; DeviceId
< Count
; ++DeviceId
) {
123 if (CUresult Err
= cuDeviceGet(&Device
, DeviceId
))
124 return handleError(Err
);
126 int32_t Major
, Minor
;
127 if (CUresult Err
= cuDeviceGetAttribute(
128 &Major
, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR
, Device
))
129 return handleError(Err
);
130 if (CUresult Err
= cuDeviceGetAttribute(
131 &Minor
, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR
, Device
))
132 return handleError(Err
);
134 printf("sm_%d%d\n", Major
, Minor
);