-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathnvcc_arch_sm.c
194 lines (184 loc) · 6.49 KB
/
nvcc_arch_sm.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* nvcc -DNDEBUG nvcc_arch_sm.c -o nvcc_arch_sm -lcuda */
/* If nvcc is not functioning properly, use the native compiler; e.g.,
* on macOS Sierra with CUDA 8 and the latest Clang releases, call:
* clang -DNDEBUG -I$CUDADIR/include nvcc_arch_sm.c -o nvcc_arch_sm -L$CUDADIR/lib -lcuda */
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
int main(int argc, char *argv[])
{
CUresult result = CUDA_SUCCESS;
int count = -1;
int *major = (int*)NULL;
int *minor = (int*)NULL;
CUdevice dev = (CUdevice)-1;
int major_min = 0;
int major_max = 0;
int minor_min = 0;
int minor_max = 0;
int chosen = 0;
switch (result = cuInit(0u)) {
case CUDA_ERROR_INVALID_VALUE:
(void)fprintf(stderr, "cuInit: CUDA_ERROR_INVALID_VALUE\n");
return EXIT_FAILURE;
case CUDA_ERROR_INVALID_DEVICE:
(void)fprintf(stderr, "cuInit: CUDA_ERROR_INVALID_DEVICE\n");
return EXIT_FAILURE;
case CUDA_SUCCESS:
break;
default:
(void)fprintf(stderr, "cuInit: %d\n", result);
return EXIT_FAILURE;
}
switch (result = cuDeviceGetCount(&count)) {
case CUDA_ERROR_DEINITIALIZED:
(void)fprintf(stderr, "cuDeviceGetCount: CUDA_ERROR_DEINITIALIZED\n");
return EXIT_FAILURE;
case CUDA_ERROR_NOT_INITIALIZED:
(void)fprintf(stderr, "cuDeviceGetCount: CUDA_ERROR_NOT_INITIALIZED\n");
return EXIT_FAILURE;
case CUDA_ERROR_INVALID_CONTEXT:
(void)fprintf(stderr, "cuDeviceGetCount: CUDA_ERROR_INVALID_CONTEXT\n");
return EXIT_FAILURE;
case CUDA_ERROR_INVALID_VALUE:
(void)fprintf(stderr, "cuDeviceGetCount: CUDA_ERROR_INVALID_VALUE\n");
return EXIT_FAILURE;
case CUDA_SUCCESS:
if (count < 0) {
(void)fprintf(stderr, "cuDeviceGetCount = %d\n", count);
return EXIT_FAILURE;
}
break;
default:
(void)fprintf(stderr, "cuDeviceGetCount: %d\n", result);
return EXIT_FAILURE;
}
#ifndef NDEBUG
(void)fprintf(stdout, "cuDeviceGetCount = %d\n", count);
(void)fflush(stdout);
#endif /* !NDEBUG */
if (!count) /* signal no GPUs by returning the failure code */
return EXIT_FAILURE;
if (!(major = (int*)calloc((size_t)(2 * count), sizeof(int))))
return EXIT_FAILURE;
minor = major + count;
for (dev = 0; dev < count; ++dev) {
switch (result = cuDeviceGetAttribute((major + dev), CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, dev)) {
case CUDA_ERROR_DEINITIALIZED:
(void)fprintf(stderr, "cuDeviceGetAttribute(major): CUDA_ERROR_DEINITIALIZED\n");
return EXIT_FAILURE;
case CUDA_ERROR_NOT_INITIALIZED:
(void)fprintf(stderr, "cuDeviceGetAttribute(major): CUDA_ERROR_NOT_INITIALIZED\n");
return EXIT_FAILURE;
case CUDA_ERROR_INVALID_CONTEXT:
(void)fprintf(stderr, "cuDeviceGetAttribute(major): CUDA_ERROR_INVALID_CONTEXT\n");
return EXIT_FAILURE;
case CUDA_ERROR_INVALID_VALUE:
(void)fprintf(stderr, "cuDeviceGetAttribute(major): CUDA_ERROR_INVALID_VALUE\n");
return EXIT_FAILURE;
case CUDA_ERROR_INVALID_DEVICE:
(void)fprintf(stderr, "cuDeviceGetAttribute(major): CUDA_ERROR_INVALID_DEVICE\n");
return EXIT_FAILURE;
case CUDA_SUCCESS:
if (major[dev] <= 0) {
(void)fprintf(stderr, "cuDeviceGetAttribute(major) = %d\n", major[dev]);
return EXIT_FAILURE;
}
break;
default:
(void)fprintf(stderr, "cuDeviceGetAttribute(major): %d\n", result);
return EXIT_FAILURE;
}
#ifndef NDEBUG
(void)fprintf(stdout, " %d=%d.", dev, major[dev]);
(void)fflush(stdout);
#endif /* !NDEBUG */
switch (result = cuDeviceGetAttribute((minor + dev), CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, dev)) {
case CUDA_ERROR_DEINITIALIZED:
(void)fprintf(stderr, "cuDeviceGetAttribute(minor): CUDA_ERROR_DEINITIALIZED\n");
return EXIT_FAILURE;
case CUDA_ERROR_NOT_INITIALIZED:
(void)fprintf(stderr, "cuDeviceGetAttribute(minor): CUDA_ERROR_NOT_INITIALIZED\n");
return EXIT_FAILURE;
case CUDA_ERROR_INVALID_CONTEXT:
(void)fprintf(stderr, "cuDeviceGetAttribute(minor): CUDA_ERROR_INVALID_CONTEXT\n");
return EXIT_FAILURE;
case CUDA_ERROR_INVALID_VALUE:
(void)fprintf(stderr, "cuDeviceGetAttribute(minor): CUDA_ERROR_INVALID_VALUE\n");
return EXIT_FAILURE;
case CUDA_ERROR_INVALID_DEVICE:
(void)fprintf(stderr, "cuDeviceGetAttribute(minor): CUDA_ERROR_INVALID_DEVICE\n");
return EXIT_FAILURE;
case CUDA_SUCCESS:
if (minor[dev] < 0) {
(void)fprintf(stderr, "cuDeviceGetAttribute(minor) = %d\n", minor[dev]);
return EXIT_FAILURE;
}
break;
default:
(void)fprintf(stderr, "cuDeviceGetAttribute(minor): %d\n", result);
return EXIT_FAILURE;
}
#ifndef NDEBUG
(void)fprintf(stdout, "%d", minor[dev]);
(void)fflush(stdout);
#endif /* !NDEBUG */
}
#ifndef NDEBUG
(void)fprintf(stdout, "\n");
(void)fflush(stdout);
#endif /* !NDEBUG */
major_max = major_min = major[0];
minor_max = minor_min = minor[0];
for (dev = 1; dev < count; ++dev) {
if (major[dev] < major_min)
major_min = major[dev];
if (major[dev] > major_max)
major_max = major[dev];
if (minor[dev] < minor_min)
minor_min = minor[dev];
if (minor[dev] > minor_max)
minor_max = minor[dev];
}
#if defined(__CUDA_API_VERSION) && __CUDA_API_VERSION >= 8000
/* CC <= 1.x not supported in CUDA >= 8. */
if (major_max <= 1) /* signal no GPUs by returning the failure code */
return EXIT_FAILURE;
#endif /* __CUDA_API_VERSION */
if (major_max == major_min) {
/* uniform GPUs */
if (minor_max == minor_min) {
(void)fprintf(stdout, "-arch=sm_%d%d\n", major_max, minor_max);
return EXIT_SUCCESS;
}
else if (major_max == 2) {
(void)fprintf(stdout, "-arch=compute_20 -code=sm_20,sm_21\n");
return EXIT_SUCCESS;
}
}
for (dev = 0; dev < count; ++dev) {
int seen = 0;
CUdevice prev = (CUdevice)-1;
#if defined(__CUDA_API_VERSION) && __CUDA_API_VERSION >= 8000
/* CC <= 1.x not supported in CUDA >= 8. */
if (major[dev] <= 1)
continue;
#endif /* __CUDA_API_VERSION */
for (prev = dev - 1; prev >= 0; --prev)
if (seen = ((major[prev] == major[dev]) && (minor[prev] == minor[dev])))
break;
if (seen)
continue;
if (chosen++)
(void)fprintf(stdout, " ");
(void)fprintf(stdout, "-gencode arch=compute_%d%d,code=sm_%d%d", major[dev], minor[dev], major[dev], minor[dev]);
(void)fflush(stdout);
}
free(major);
if (chosen) {
(void)fprintf(stdout, "\n");
return EXIT_SUCCESS;
}
/* signal no GPUs by returning the failure code */
return EXIT_FAILURE;
}