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