Blender V5.3
hip/queue.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#ifdef WITH_HIP
6
7# include "device/hip/queue.h"
8
11# include "device/hip/kernel.h"
12
14
15/* HIPDeviceQueue */
16
17HIPDeviceQueue::HIPDeviceQueue(HIPDevice *device)
18 : DeviceQueue(device), hip_device_(device), hip_stream_(nullptr)
19{
20 const HIPContextScope scope(hip_device_);
21 hip_device_assert(hip_device_, hipStreamCreateWithFlags(&hip_stream_, hipStreamNonBlocking));
22}
23
24HIPDeviceQueue::~HIPDeviceQueue()
25{
26 const HIPContextScope scope(hip_device_);
27 hipStreamDestroy(hip_stream_);
28}
29
30ConcurrentStatesParams HIPDeviceQueue::concurrent_states_params() const
31{
32 const int max_num_threads = hip_device_->get_num_multiprocessors() *
33 hip_device_->get_max_num_threads_per_multiprocessor();
34
35 /* Only shrink to fit within available memory. Growing up to 10x does show
36 * performance improvements, however it causes TDR timeouts, too much memory
37 * usage when there are multiple Blender instances, etc. */
39 params.baseline = ((max_num_threads == 0) ? 65536 : max_num_threads) * 16;
40 params.min = params.baseline / 2;
41 params.max = params.baseline;
42 return params;
43}
44
45void HIPDeviceQueue::get_memory_info(size_t &total, size_t &free) const
46{
47 hip_device_->get_device_memory_info(total, free);
48}
49
50void HIPDeviceQueue::init_execution()
51{
52 /* Synchronize all textures and memory copies before executing task. */
53 HIPContextScope scope(hip_device_);
54 hip_device_->load_image_info(nullptr);
55 hip_device_assert(hip_device_, hipDeviceSynchronize());
56
57 debug_init_execution();
58}
59
60void HIPDeviceQueue::load_image_info()
61{
62 HIPContextScope scope(hip_device_);
63 hip_device_->load_image_info(this);
64}
65
66bool HIPDeviceQueue::enqueue(DeviceKernel kernel,
67 const int work_size,
68 const DeviceKernelArguments &args)
69{
70 if (hip_device_->have_error()) {
71 return false;
72 }
73
74 debug_enqueue_begin(kernel, work_size);
75
76 const HIPContextScope scope(hip_device_);
77
78 /* Update image info in case memory moved to host. */
79 if (hip_device_->load_image_info(nullptr)) {
80 hip_device_assert(hip_device_, hipDeviceSynchronize());
81 if (hip_device_->have_error()) {
82 return false;
83 }
84 }
85
86 /* Compute kernel launch parameters. */
87 const HIPDeviceKernel &hip_kernel = hip_device_->kernels.get(kernel);
88 const int num_threads_per_block = hip_kernel.num_threads_per_block;
89 const int num_blocks = divide_up(work_size, num_threads_per_block);
90
91 int shared_mem_bytes = 0;
92
93 switch (kernel) {
102 /* See parall_active_index.h for why this amount of shared memory is needed. */
103 shared_mem_bytes = (num_threads_per_block + 1) * sizeof(int);
104 break;
105 default:
106 break;
107 }
108
109 /* Launch kernel. */
110 assert_success(hipModuleLaunchKernel(hip_kernel.function,
111 num_blocks,
112 1,
113 1,
114 num_threads_per_block,
115 1,
116 1,
117 shared_mem_bytes,
118 hip_stream_,
119 const_cast<void **>(args.values),
120 nullptr),
121 "enqueue");
122
123 debug_enqueue_end();
124
125 return !(hip_device_->have_error());
126}
127
128bool HIPDeviceQueue::synchronize()
129{
130 if (hip_device_->have_error()) {
131 return false;
132 }
133
134 const HIPContextScope scope(hip_device_);
135 assert_success(hipStreamSynchronize(hip_stream_), "synchronize");
136 debug_synchronize();
137
138 return !(hip_device_->have_error());
139}
140
141void HIPDeviceQueue::zero_to_device(device_memory &mem)
142{
144
145 if (mem.memory_size() == 0) {
146 return;
147 }
148
149 /* Allocate on demand. */
150 if (mem.device_pointer == 0) {
151 if (mem.type == MEM_GLOBAL) {
152 hip_device_->global_alloc(mem);
153 }
154 else {
155 hip_device_->mem_alloc(mem);
156 }
157 }
158
159 /* Zero memory on device. */
160 device_ptr d_ptr = mem.device->mem_device_ptr(mem, hip_device_);
161 assert(d_ptr != 0);
162
163 const HIPContextScope scope(hip_device_);
164 assert_success(hipMemsetD8Async((hipDeviceptr_t)d_ptr, 0, mem.memory_size(), hip_stream_),
165 "zero_to_device");
166}
167
168void HIPDeviceQueue::copy_to_device(device_memory &mem)
169{
171
172 if (mem.memory_size() == 0) {
173 return;
174 }
175
176 /* Allocate on demand. */
177 if (mem.device_pointer == 0) {
178 if (mem.type == MEM_GLOBAL) {
179 hip_device_->global_alloc(mem);
180 }
181 else {
182 hip_device_->mem_alloc(mem);
183 }
184 }
185
186 device_ptr d_ptr = mem.device->mem_device_ptr(mem, hip_device_);
187 assert(d_ptr != 0);
188 assert(mem.host_pointer != nullptr);
189
190 /* Copy memory to device. */
191 const HIPContextScope scope(hip_device_);
192 assert_success(
193 hipMemcpyHtoDAsync((hipDeviceptr_t)d_ptr, mem.host_pointer, mem.memory_size(), hip_stream_),
194 "copy_to_device");
195}
196
197void HIPDeviceQueue::copy_from_device(device_memory &mem)
198{
199 assert(mem.type != MEM_GLOBAL && mem.type != MEM_IMAGE_TEXTURE);
200
201 if (mem.memory_size() == 0) {
202 return;
203 }
204
205 assert(mem.device_pointer != 0);
206 assert(mem.host_pointer != nullptr);
207
208 /* Copy memory from device. */
209 const HIPContextScope scope(hip_device_);
210 assert_success(
211 hipMemcpyDtoHAsync(
212 mem.host_pointer, (hipDeviceptr_t)mem.device_pointer, mem.memory_size(), hip_stream_),
213 "copy_from_device");
214}
215
216void *HIPDeviceQueue::copy_from_device_synchronized(device_memory &mem, vector<uint8_t> &storage)
217{
218 if (mem.memory_size() == 0) {
219 return nullptr;
220 }
221
222 storage.resize(mem.memory_size());
223
224 device_ptr d_ptr = mem.device->mem_device_ptr(mem, hip_device_);
225 assert(d_ptr != 0);
226
227 const HIPContextScope scope(hip_device_);
228 assert_success(
229 hipMemcpyDtoHAsync(storage.data(), (hipDeviceptr_t)d_ptr, mem.memory_size(), hip_stream_),
230 "copy_from_device_synchronized");
231
232 synchronize();
233 return storage.data();
234}
235
236void HIPDeviceQueue::assert_success(hipError_t result, const char *operation)
237{
238 if (result != hipSuccess) {
239 const char *name = hipewErrorString(result);
240 hip_device_->set_error(
241 string_printf("%s in HIP queue %s (%s)", name, operation, debug_active_kernels().c_str()));
242 }
243}
244
245unique_ptr<DeviceGraphicsInterop> HIPDeviceQueue::graphics_interop_create()
246{
247 return make_unique<HIPDeviceGraphicsInterop>(this);
248}
249
251
252#endif /* WITH_HIP */
virtual device_ptr mem_device_ptr(const device_memory &mem, Device *sub_device)
@ MEM_IMAGE_TEXTURE
#define CCL_NAMESPACE_END
#define assert(assertion)
#define storage(slot, qualifiers)
ccl_gpu_kernel_postfix const ccl_global int ccl_global float const int work_size
DeviceKernel
@ DEVICE_KERNEL_INTEGRATOR_QUEUED_PATHS_ARRAY
@ DEVICE_KERNEL_INTEGRATOR_QUEUED_SHADOW_PATHS_ARRAY
@ DEVICE_KERNEL_INTEGRATOR_TERMINATED_PATHS_ARRAY
@ DEVICE_KERNEL_INTEGRATOR_SORTED_PATHS_ARRAY
@ DEVICE_KERNEL_INTEGRATOR_COMPACT_SHADOW_PATHS_ARRAY
@ DEVICE_KERNEL_INTEGRATOR_TERMINATED_SHADOW_PATHS_ARRAY
@ DEVICE_KERNEL_INTEGRATOR_ACTIVE_PATHS_ARRAY
@ DEVICE_KERNEL_INTEGRATOR_COMPACT_PATHS_ARRAY
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
void * values[MAX_ARGS]
uint64_t device_ptr
Definition types_base.h:44
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
Definition types_base.h:54