Blender V4.5
device/cuda/device.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6#include "device/device.h"
7
8#include "util/log.h"
9
10#ifdef WITH_CUDA
12
13# include "integrator/denoiser_oidn_gpu.h" // IWYU pragma: keep
14
15# include "util/string.h"
16# ifdef _WIN32
17# include "util/windows.h"
18# endif
19#endif /* WITH_CUDA */
20
22
24{
25#if !defined(WITH_CUDA)
26 return false;
27#elif defined(WITH_CUDA_DYNLOAD)
28 static bool initialized = false;
29 static bool result = false;
30
31 if (initialized) {
32 return result;
33 }
34
35 initialized = true;
36 int cuew_result = cuewInit(CUEW_INIT_CUDA);
37 if (cuew_result == CUEW_SUCCESS) {
38 VLOG_INFO << "CUEW initialization succeeded";
39 if (CUDADevice::have_precompiled_kernels()) {
40 VLOG_INFO << "Found precompiled kernels";
41 result = true;
42 }
43 else if (cuewCompilerPath() != nullptr) {
44 VLOG_INFO << "Found CUDA compiler " << cuewCompilerPath();
45 result = true;
46 }
47 else {
48 VLOG_INFO << "Neither precompiled kernels nor CUDA compiler was found,"
49 << " unable to use CUDA";
50 }
51 }
52 else {
53 VLOG_WARNING << "CUEW initialization failed: "
54 << ((cuew_result == CUEW_ERROR_ATEXIT_FAILED) ?
55 "Error setting up atexit() handler" :
56 "Error opening the library");
57 }
58
59 return result;
60#else /* WITH_CUDA_DYNLOAD */
61 return true;
62#endif /* WITH_CUDA_DYNLOAD */
63}
64
66 Stats &stats,
67 Profiler &profiler,
68 bool headless)
69{
70#ifdef WITH_CUDA
71 return make_unique<CUDADevice>(info, stats, profiler, headless);
72#else
73 (void)info;
74 (void)stats;
75 (void)profiler;
76 (void)headless;
77
78 LOG(FATAL) << "Request to create CUDA device without compiled-in support. Should never happen.";
79
80 return nullptr;
81#endif
82}
83
84#ifdef WITH_CUDA
85static CUresult device_cuda_safe_init()
86{
87# ifdef _WIN32
88 __try
89 {
90 return cuInit(0);
91 }
92 __except (EXCEPTION_EXECUTE_HANDLER)
93 {
94 /* Ignore crashes inside the CUDA driver and hope we can
95 * survive even with corrupted CUDA installs. */
96 fprintf(stderr, "Cycles CUDA: driver crashed, continuing without CUDA.\n");
97 }
98
99 return CUDA_ERROR_NO_DEVICE;
100# else
101 return cuInit(0);
102# endif
103}
104#endif /* WITH_CUDA */
105
107{
108#ifdef WITH_CUDA
109 CUresult result = device_cuda_safe_init();
110 if (result != CUDA_SUCCESS) {
111 if (result != CUDA_ERROR_NO_DEVICE) {
112 fprintf(stderr, "CUDA cuInit: %s\n", cuewErrorString(result));
113 }
114 return;
115 }
116
117 int count = 0;
118 result = cuDeviceGetCount(&count);
119 if (result != CUDA_SUCCESS) {
120 fprintf(stderr, "CUDA cuDeviceGetCount: %s\n", cuewErrorString(result));
121 return;
122 }
123
124 vector<DeviceInfo> display_devices;
125
126 for (int num = 0; num < count; num++) {
127 char name[256];
128
129 result = cuDeviceGetName(name, 256, num);
130 if (result != CUDA_SUCCESS) {
131 fprintf(stderr, "CUDA cuDeviceGetName: %s\n", cuewErrorString(result));
132 continue;
133 }
134
135 if (!cudaSupportsDevice(num)) {
136 VLOG_INFO << "Ignoring device \"" << name
137 << "\", this graphics card is no longer supported.";
138 continue;
139 }
140
141 DeviceInfo info;
142
143 info.type = DEVICE_CUDA;
144 info.description = string(name);
145 info.num = num;
146
147 info.has_nanovdb = true;
148 info.denoisers = 0;
149
150 info.has_gpu_queue = true;
151
152 /* Check if the device has P2P access to any other device in the system. */
153 for (int peer_num = 0; peer_num < count && !info.has_peer_memory; peer_num++) {
154 if (num != peer_num) {
155 if (cudaSupportsDevice(peer_num)) {
156 int can_access = 0;
157 cuDeviceCanAccessPeer(&can_access, num, peer_num);
158 info.has_peer_memory = (can_access != 0);
159 }
160 }
161 }
162
163 int pci_location[3] = {0, 0, 0};
164 cuDeviceGetAttribute(&pci_location[0], CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID, num);
165 cuDeviceGetAttribute(&pci_location[1], CU_DEVICE_ATTRIBUTE_PCI_BUS_ID, num);
166 cuDeviceGetAttribute(&pci_location[2], CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID, num);
167 info.id = string_printf("CUDA_%s_%04x:%02x:%02x",
168 name,
169 (unsigned int)pci_location[0],
170 (unsigned int)pci_location[1],
171 (unsigned int)pci_location[2]);
172
173# if defined(WITH_OPENIMAGEDENOISE)
174# if OIDN_VERSION >= 20300
175 if (oidnIsCUDADeviceSupported(num)) {
176# else
177 if (OIDNDenoiserGPU::is_device_supported(info)) {
178# endif
180 }
181# endif
182
183 /* If device has a kernel timeout and no compute preemption, we assume
184 * it is connected to a display and will freeze the display while doing
185 * computations. */
186 int timeout_attr = 0, preempt_attr = 0;
187 cuDeviceGetAttribute(&timeout_attr, CU_DEVICE_ATTRIBUTE_KERNEL_EXEC_TIMEOUT, num);
188 cuDeviceGetAttribute(&preempt_attr, CU_DEVICE_ATTRIBUTE_COMPUTE_PREEMPTION_SUPPORTED, num);
189
190# ifdef _WIN32
191 int major;
192 cuDeviceGetAttribute(&major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, num);
193 /* The CUDA driver reports compute preemption as not being available on
194 * Windows 10 even when it is, due to an issue in application profiles.
195 * Detect case where we expect it to be available and override. */
196 if (preempt_attr == 0 && (major >= 6) && system_windows_version_at_least(10, 17134)) {
197 VLOG_INFO << "Assuming device has compute preemption on Windows 10.";
198 preempt_attr = 1;
199 }
200# endif
201
202 if (timeout_attr && !preempt_attr) {
203 VLOG_INFO << "Device is recognized as display.";
204 info.description += " (Display)";
205 info.display_device = true;
206 display_devices.push_back(info);
207 }
208 else {
209 VLOG_INFO << "Device has compute preemption or is not used for display.";
210 devices.push_back(info);
211 }
212 VLOG_INFO << "Added device \"" << info.description << "\" with id \"" << info.id << "\".";
213
215 VLOG_INFO << "Device with id \"" << info.id << "\" supports "
217 }
218 }
219
220 if (!display_devices.empty()) {
221 devices.insert(devices.end(), display_devices.begin(), display_devices.end());
222 }
223#else /* WITH_CUDA */
224 (void)devices;
225#endif /* WITH_CUDA */
226}
227
229{
230#ifdef WITH_CUDA
231 CUresult result = device_cuda_safe_init();
232 if (result != CUDA_SUCCESS) {
233 if (result != CUDA_ERROR_NO_DEVICE) {
234 return string("Error initializing CUDA: ") + cuewErrorString(result);
235 }
236 return "No CUDA device found\n";
237 }
238
239 int count;
240 result = cuDeviceGetCount(&count);
241 if (result != CUDA_SUCCESS) {
242 return string("Error getting devices: ") + cuewErrorString(result);
243 }
244
245 string capabilities;
246 for (int num = 0; num < count; num++) {
247 char name[256];
248 if (cuDeviceGetName(name, 256, num) != CUDA_SUCCESS) {
249 continue;
250 }
251 capabilities += string("\t") + name + "\n";
252 int value;
253# define GET_ATTR(attr) \
254 { \
255 if (cuDeviceGetAttribute(&value, CU_DEVICE_ATTRIBUTE_##attr, num) == CUDA_SUCCESS) { \
256 capabilities += string_printf("\t\tCU_DEVICE_ATTRIBUTE_" #attr "\t\t\t%d\n", value); \
257 } \
258 } \
259 (void)0
260 /* TODO(sergey): Strip all attributes which are not useful for us
261 * or does not depend on the driver.
262 */
263 GET_ATTR(MAX_THREADS_PER_BLOCK);
264 GET_ATTR(MAX_BLOCK_DIM_X);
265 GET_ATTR(MAX_BLOCK_DIM_Y);
266 GET_ATTR(MAX_BLOCK_DIM_Z);
267 GET_ATTR(MAX_GRID_DIM_X);
268 GET_ATTR(MAX_GRID_DIM_Y);
269 GET_ATTR(MAX_GRID_DIM_Z);
270 GET_ATTR(MAX_SHARED_MEMORY_PER_BLOCK);
271 GET_ATTR(SHARED_MEMORY_PER_BLOCK);
272 GET_ATTR(TOTAL_CONSTANT_MEMORY);
273 GET_ATTR(WARP_SIZE);
274 GET_ATTR(MAX_PITCH);
275 GET_ATTR(MAX_REGISTERS_PER_BLOCK);
276 GET_ATTR(REGISTERS_PER_BLOCK);
277 GET_ATTR(CLOCK_RATE);
278 GET_ATTR(TEXTURE_ALIGNMENT);
279 GET_ATTR(GPU_OVERLAP);
280 GET_ATTR(MULTIPROCESSOR_COUNT);
281 GET_ATTR(KERNEL_EXEC_TIMEOUT);
282 GET_ATTR(INTEGRATED);
283 GET_ATTR(CAN_MAP_HOST_MEMORY);
284 GET_ATTR(COMPUTE_MODE);
285 GET_ATTR(MAXIMUM_TEXTURE1D_WIDTH);
286 GET_ATTR(MAXIMUM_TEXTURE2D_WIDTH);
287 GET_ATTR(MAXIMUM_TEXTURE2D_HEIGHT);
288 GET_ATTR(MAXIMUM_TEXTURE3D_WIDTH);
289 GET_ATTR(MAXIMUM_TEXTURE3D_HEIGHT);
290 GET_ATTR(MAXIMUM_TEXTURE3D_DEPTH);
291 GET_ATTR(MAXIMUM_TEXTURE2D_LAYERED_WIDTH);
292 GET_ATTR(MAXIMUM_TEXTURE2D_LAYERED_HEIGHT);
293 GET_ATTR(MAXIMUM_TEXTURE2D_LAYERED_LAYERS);
294 GET_ATTR(MAXIMUM_TEXTURE2D_ARRAY_WIDTH);
295 GET_ATTR(MAXIMUM_TEXTURE2D_ARRAY_HEIGHT);
296 GET_ATTR(MAXIMUM_TEXTURE2D_ARRAY_NUMSLICES);
297 GET_ATTR(SURFACE_ALIGNMENT);
298 GET_ATTR(CONCURRENT_KERNELS);
299 GET_ATTR(ECC_ENABLED);
300 GET_ATTR(TCC_DRIVER);
301 GET_ATTR(MEMORY_CLOCK_RATE);
302 GET_ATTR(GLOBAL_MEMORY_BUS_WIDTH);
303 GET_ATTR(L2_CACHE_SIZE);
304 GET_ATTR(MAX_THREADS_PER_MULTIPROCESSOR);
305 GET_ATTR(ASYNC_ENGINE_COUNT);
306 GET_ATTR(UNIFIED_ADDRESSING);
307 GET_ATTR(MAXIMUM_TEXTURE1D_LAYERED_WIDTH);
308 GET_ATTR(MAXIMUM_TEXTURE1D_LAYERED_LAYERS);
309 GET_ATTR(CAN_TEX2D_GATHER);
310 GET_ATTR(MAXIMUM_TEXTURE2D_GATHER_WIDTH);
311 GET_ATTR(MAXIMUM_TEXTURE2D_GATHER_HEIGHT);
312 GET_ATTR(MAXIMUM_TEXTURE3D_WIDTH_ALTERNATE);
313 GET_ATTR(MAXIMUM_TEXTURE3D_HEIGHT_ALTERNATE);
314 GET_ATTR(MAXIMUM_TEXTURE3D_DEPTH_ALTERNATE);
315 GET_ATTR(TEXTURE_PITCH_ALIGNMENT);
316 GET_ATTR(MAXIMUM_TEXTURECUBEMAP_WIDTH);
317 GET_ATTR(MAXIMUM_TEXTURECUBEMAP_LAYERED_WIDTH);
318 GET_ATTR(MAXIMUM_TEXTURECUBEMAP_LAYERED_LAYERS);
319 GET_ATTR(MAXIMUM_SURFACE1D_WIDTH);
320 GET_ATTR(MAXIMUM_SURFACE2D_WIDTH);
321 GET_ATTR(MAXIMUM_SURFACE2D_HEIGHT);
322 GET_ATTR(MAXIMUM_SURFACE3D_WIDTH);
323 GET_ATTR(MAXIMUM_SURFACE3D_HEIGHT);
324 GET_ATTR(MAXIMUM_SURFACE3D_DEPTH);
325 GET_ATTR(MAXIMUM_SURFACE1D_LAYERED_WIDTH);
326 GET_ATTR(MAXIMUM_SURFACE1D_LAYERED_LAYERS);
327 GET_ATTR(MAXIMUM_SURFACE2D_LAYERED_WIDTH);
328 GET_ATTR(MAXIMUM_SURFACE2D_LAYERED_HEIGHT);
329 GET_ATTR(MAXIMUM_SURFACE2D_LAYERED_LAYERS);
330 GET_ATTR(MAXIMUM_SURFACECUBEMAP_WIDTH);
331 GET_ATTR(MAXIMUM_SURFACECUBEMAP_LAYERED_WIDTH);
332 GET_ATTR(MAXIMUM_SURFACECUBEMAP_LAYERED_LAYERS);
333 GET_ATTR(MAXIMUM_TEXTURE1D_LINEAR_WIDTH);
334 GET_ATTR(MAXIMUM_TEXTURE2D_LINEAR_WIDTH);
335 GET_ATTR(MAXIMUM_TEXTURE2D_LINEAR_HEIGHT);
336 GET_ATTR(MAXIMUM_TEXTURE2D_LINEAR_PITCH);
337 GET_ATTR(MAXIMUM_TEXTURE2D_MIPMAPPED_WIDTH);
338 GET_ATTR(MAXIMUM_TEXTURE2D_MIPMAPPED_HEIGHT);
339 GET_ATTR(COMPUTE_CAPABILITY_MAJOR);
340 GET_ATTR(COMPUTE_CAPABILITY_MINOR);
341 GET_ATTR(MAXIMUM_TEXTURE1D_MIPMAPPED_WIDTH);
342 GET_ATTR(STREAM_PRIORITIES_SUPPORTED);
343 GET_ATTR(GLOBAL_L1_CACHE_SUPPORTED);
344 GET_ATTR(LOCAL_L1_CACHE_SUPPORTED);
345 GET_ATTR(MAX_SHARED_MEMORY_PER_MULTIPROCESSOR);
346 GET_ATTR(MAX_REGISTERS_PER_MULTIPROCESSOR);
347 GET_ATTR(MANAGED_MEMORY);
348 GET_ATTR(MULTI_GPU_BOARD);
349 GET_ATTR(MULTI_GPU_BOARD_GROUP_ID);
350# undef GET_ATTR
351 capabilities += "\n";
352 }
353
354 return capabilities;
355
356#else /* WITH_CUDA */
357 return "";
358#endif /* WITH_CUDA */
359}
360
ATTR_WARN_UNUSED_RESULT const size_t num
DenoiserTypeMask denoisers
bool display_device
bool has_peer_memory
bool has_gpu_queue
DeviceType type
string description
CCL_NAMESPACE_BEGIN const char * denoiserTypeToHumanReadable(DenoiserType type)
Definition denoise.cpp:9
@ DENOISER_OPENIMAGEDENOISE
Definition denoise.h:13
#define CCL_NAMESPACE_END
void device_cuda_info(vector< DeviceInfo > &devices)
string device_cuda_capabilities()
CCL_NAMESPACE_BEGIN bool device_cuda_init()
unique_ptr< Device > device_cuda_create(const DeviceInfo &info, Stats &stats, Profiler &profiler, bool headless)
@ DEVICE_CUDA
static bool initialized
int count
#define VLOG_INFO
Definition log.h:71
#define VLOG_WARNING
Definition log.h:69
#define LOG(severity)
Definition log.h:32
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:23
CCL_NAMESPACE_BEGIN bool system_windows_version_at_least(const int major, const int build)
Definition windows.cpp:13