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