Blender V5.3
device/hip/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
5#include "device/hip/device.h"
6#include "device/device.h"
7
8#include "util/log.h"
9
10#ifdef WITH_HIP
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_HIP */
20
21#ifdef WITH_HIPRT
23#endif
24
26
27bool device_hip_init(bool *r_meets_driver_requirement)
28{
29 if (r_meets_driver_requirement) {
30 *r_meets_driver_requirement = true;
31 }
32#if !defined(WITH_HIP)
33 return false;
34#elif defined(WITH_HIP_DYNLOAD)
35 static bool initialized = false;
36 static bool result = false;
37
38 if (initialized) {
39 return result;
40 }
41
42 initialized = true;
43 int hipew_result = hipewInit(HIPEW_INIT_HIP);
44
45 if (hipew_result == HIPEW_SUCCESS) {
46 LOG_INFO << "HIPEW initialization succeeded";
47 if (!hipSupportsDriver()) {
48 LOG_WARNING << "HIP driver version is too old";
49 if (r_meets_driver_requirement) {
50 *r_meets_driver_requirement = false;
51 }
52 /* The device will not be used because it does not meet the driver requirement.
53 * Returning true ensures the real AMD device information is still reported
54 * in the UI. */
55 result = true;
56 }
57 else if (HIPDevice::have_precompiled_kernels()) {
58 LOG_INFO << "HIP: Found precompiled kernels";
59 result = true;
60 }
61 else if (hipewCompilerPath() != nullptr) {
62 LOG_INFO << "Found HIPCC " << hipewCompilerPath();
63 result = true;
64 }
65 else {
66 LOG_INFO << "Neither precompiled kernels nor HIPCC was found,"
67 << " unable to use HIP";
68 }
69 }
70 else {
71 if (hipew_result == HIPEW_ERROR_ATEXIT_FAILED) {
72 LOG_WARNING << "HIPEW initialization failed: Error setting up atexit() handler";
73 }
74 else if (hipew_result == HIPEW_ERROR_OLD_DRIVER) {
75 LOG_WARNING << "HIPEW initialization failed: Driver version too old, requires AMD Adrenalin "
76 "driver 24.9.1 or newer, or AMD Radeon Pro driver 24.Q4 or newer";
77 if (r_meets_driver_requirement) {
78 *r_meets_driver_requirement = false;
79 }
80 }
81 else {
82 LOG_WARNING << "HIPEW initialization failed: Error opening HIP dynamic library";
83 }
84 }
85
86 return result;
87#else /* WITH_HIP_DYNLOAD */
88 return true;
89#endif /* WITH_HIP_DYNLOAD */
90}
91
93 Stats &stats,
94 Profiler &profiler,
95 const bool headless)
96{
97#ifdef WITH_HIPRT
98 if (info.use_hardware_raytracing) {
99 return make_unique<HIPRTDevice>(info, stats, profiler, headless);
100 }
101 return make_unique<HIPDevice>(info, stats, profiler, headless);
102#elif defined(WITH_HIP)
103 return make_unique<HIPDevice>(info, stats, profiler, headless);
104#else
105 (void)info;
106 (void)stats;
107 (void)profiler;
108 (void)headless;
109
110 LOG_FATAL << "Request to create HIP device without compiled-in support. Should never happen.";
111
112 return nullptr;
113#endif
114}
115
116#ifdef WITH_HIP
117static hipError_t device_hip_safe_init()
118{
119# ifdef _WIN32
120 __try {
121 return hipInit(0);
122 }
123 __except (EXCEPTION_EXECUTE_HANDLER) {
124 /* Ignore crashes inside the HIP driver and hope we can
125 * survive even with corrupted HIP installs. */
126 fprintf(stderr, "Cycles HIP: driver crashed, continuing without HIP.\n");
127 }
128
129 return hipErrorNoDevice;
130# else
131 return hipInit(0);
132# endif
133}
134#endif /* WITH_HIP */
135
137{
138#ifdef WITH_HIP
139 hipError_t result = device_hip_safe_init();
140 if (result != hipSuccess) {
141 if (result != hipErrorNoDevice) {
142 LOG_ERROR << "HIP hipInit: " << hipewErrorString(result);
143 }
144 return;
145 }
146
147 int count = 0;
148 result = hipGetDeviceCount(&count);
149 if (result != hipSuccess) {
150 LOG_ERROR << "HIP hipGetDeviceCount: " << hipewErrorString(result);
151 return;
152 }
153
154# ifdef WITH_HIPRT
155 const bool has_hardware_raytracing = HIPRTDevice::is_supported();
156# else
157 const bool has_hardware_raytracing = false;
158# endif
159
160 vector<DeviceInfo> display_devices;
161
162 for (int num = 0; num < count; num++) {
163 char name[256];
164
165 result = hipDeviceGetName(name, 256, num);
166 if (result != hipSuccess) {
167 LOG_ERROR << "HIP hipDeviceGetName: " << hipewErrorString(result);
168 continue;
169 }
170
171 if (!hipSupportsDevice(num)) {
172 continue;
173 }
174
175 DeviceInfo info;
176
177 info.type = DEVICE_HIP;
178 info.description = string(name);
179 info.num = num;
180
181 info.has_nanovdb = true;
182
183 info.has_gpu_queue = true;
184 /* Check if the device has P2P access to any other device in the system. */
185 for (int peer_num = 0; peer_num < count && !info.has_peer_memory; peer_num++) {
186 if (num != peer_num) {
187 if (hipSupportsDevice(peer_num)) {
188 int can_access = 0;
189 hipDeviceCanAccessPeer(&can_access, num, peer_num);
190 info.has_peer_memory = (can_access != 0);
191 }
192 }
193 }
194
195 /* Disable on RDNA1 due to bug rendering curves in HIP-RT 2.5 or HIP SDK 6.3. */
196 info.use_hardware_raytracing = has_hardware_raytracing && hipIsRDNA2OrNewer(num);
197
198 int pci_location[3] = {0, 0, 0};
199 hipDeviceGetAttribute(&pci_location[0], hipDeviceAttributePciDomainID, num);
200 hipDeviceGetAttribute(&pci_location[1], hipDeviceAttributePciBusId, num);
201 hipDeviceGetAttribute(&pci_location[2], hipDeviceAttributePciDeviceId, num);
202 info.id = string_printf("HIP_%s_%04x:%02x:%02x",
203 name,
204 (unsigned int)pci_location[0],
205 (unsigned int)pci_location[1],
206 (unsigned int)pci_location[2]);
207
208 info.denoisers = 0;
209# if defined(WITH_OPENIMAGEDENOISE)
210 /* Check first if OIDN supports it, not doing so can crash the HIP driver with
211 * "hipErrorNoBinaryForGpu: Unable to find code object for all current devices". */
212# if OIDN_VERSION >= 20300
213 if (hipSupportsDeviceOIDN(num) && oidnIsHIPDeviceSupported(num)) {
214# else
215 if (hipSupportsDeviceOIDN(num) && OIDNDenoiserGPU::is_device_supported(info)) {
216# endif
218 }
219# endif
220
221 /* If device has a kernel timeout and no compute preemption, we assume
222 * it is connected to a display and will freeze the display while doing
223 * computations. */
224 int timeout_attr = 0;
225 hipDeviceGetAttribute(&timeout_attr, hipDeviceAttributeKernelExecTimeout, num);
226
227 if (timeout_attr) {
228 LOG_INFO << "Device is recognized as display.";
229 info.description += " (Display)";
230 info.display_device = true;
231 display_devices.push_back(info);
232 }
233 else {
234 LOG_INFO << "Device has compute preemption or is not used for display.";
235 devices.push_back(info);
236 }
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_HIP */
250 (void)devices;
251#endif /* WITH_HIP */
252}
253
255{
256#ifdef WITH_HIP
257 hipError_t result = device_hip_safe_init();
258 if (result != hipSuccess) {
259 if (result != hipErrorNoDevice) {
260 return string("Error initializing HIP: ") + hipewErrorString(result);
261 }
262 return "No HIP device found\n";
263 }
264
265 int count;
266 result = hipGetDeviceCount(&count);
267 if (result != hipSuccess) {
268 return string("Error getting devices: ") + hipewErrorString(result);
269 }
270
271 string capabilities;
272 for (int num = 0; num < count; num++) {
273 char name[256];
274 if (hipDeviceGetName(name, 256, num) != hipSuccess) {
275 continue;
276 }
277 capabilities += string("\t") + name + "\n";
278 int value;
279# define GET_ATTR(attr) \
280 { \
281 if (hipDeviceGetAttribute(&value, hipDeviceAttribute##attr, num) == hipSuccess) { \
282 capabilities += string_printf("\t\thipDeviceAttribute" #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(MaxThreadsPerBlock);
290 GET_ATTR(MaxBlockDimX);
291 GET_ATTR(MaxBlockDimY);
292 GET_ATTR(MaxBlockDimZ);
293 GET_ATTR(MaxGridDimX);
294 GET_ATTR(MaxGridDimY);
295 GET_ATTR(MaxGridDimZ);
296 GET_ATTR(MaxSharedMemoryPerBlock);
297 GET_ATTR(TotalConstantMemory);
298 GET_ATTR(WarpSize);
299 GET_ATTR(MaxPitch);
300 GET_ATTR(MaxRegistersPerBlock);
301 GET_ATTR(ClockRate);
302 GET_ATTR(TextureAlignment);
303 GET_ATTR(MultiprocessorCount);
304 GET_ATTR(KernelExecTimeout);
305 GET_ATTR(Integrated);
306 GET_ATTR(CanMapHostMemory);
307 GET_ATTR(ComputeMode);
308 GET_ATTR(MaxTexture1DWidth);
309 GET_ATTR(MaxTexture2DWidth);
310 GET_ATTR(MaxTexture2DHeight);
311 GET_ATTR(MaxTexture3DWidth);
312 GET_ATTR(MaxTexture3DHeight);
313 GET_ATTR(MaxTexture3DDepth);
314 GET_ATTR(ConcurrentKernels);
315 GET_ATTR(EccEnabled);
316 GET_ATTR(MemoryClockRate);
317 GET_ATTR(MemoryBusWidth);
318 GET_ATTR(L2CacheSize);
319 GET_ATTR(MaxThreadsPerMultiProcessor);
320 GET_ATTR(ComputeCapabilityMajor);
321 GET_ATTR(ComputeCapabilityMinor);
322 GET_ATTR(MaxSharedMemoryPerMultiprocessor);
323 GET_ATTR(ManagedMemory);
324 GET_ATTR(IsMultiGpuBoard);
325# undef GET_ATTR
326 capabilities += "\n";
327 }
328
329 return capabilities;
330
331#else /* WITH_HIP */
332 return "";
333#endif /* WITH_HIP */
334}
335
DenoiserTypeMask denoisers
bool display_device
bool has_peer_memory
bool has_gpu_queue
DeviceType type
string description
bool use_hardware_raytracing
CCL_NAMESPACE_BEGIN const char * denoiserTypeToHumanReadable(DenoiserType type)
Definition denoise.cpp:9
@ DENOISER_OPENIMAGEDENOISE
Definition denoise.h:13
#define CCL_NAMESPACE_END
@ DEVICE_HIP
CCL_NAMESPACE_BEGIN bool device_hip_init(bool *r_meets_driver_requirement)
void device_hip_info(vector< DeviceInfo > &devices)
string device_hip_capabilities()
unique_ptr< Device > device_hip_create(const DeviceInfo &info, Stats &stats, Profiler &profiler, const bool headless)
#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