Blender V5.3
cuda/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_CUDA
6
7# include "device/cuda/queue.h"
8
11# include "device/cuda/kernel.h"
12
14# include "util/debug.h"
15
17
18/* CUDADeviceQueue */
19
20CUDADeviceQueue::CUDADeviceQueue(CUDADevice *device)
21 : DeviceQueue(device), cuda_device_(device), cuda_stream_(nullptr)
22{
23 const CUDAContextScope scope(cuda_device_);
24 cuda_device_assert(cuda_device_, cuStreamCreate(&cuda_stream_, CU_STREAM_NON_BLOCKING));
25}
26
27CUDADeviceQueue::~CUDADeviceQueue()
28{
29 const CUDAContextScope scope(cuda_device_);
30 cuStreamDestroy(cuda_stream_);
31}
32
33ConcurrentStatesParams CUDADeviceQueue::concurrent_states_params() const
34{
35 const int max_num_threads = cuda_device_->get_num_multiprocessors() *
36 cuda_device_->get_max_num_threads_per_multiprocessor();
37
38 /* Only shrink to fit within available memory. Growing up to 10x does show
39 * performance improvements, however it causes TDR timeouts, too much memory
40 * usage when there are multiple Blender instances, etc. */
42 params.baseline = max(max_num_threads, 65536) * 16;
43 params.min = params.baseline / 2;
44 params.max = params.baseline;
45 return params;
46}
47
48void CUDADeviceQueue::get_memory_info(size_t &total, size_t &free) const
49{
50 cuda_device_->get_device_memory_info(total, free);
51}
52
53int CUDADeviceQueue::num_sort_partitions(int max_num_paths, uint max_scene_shaders) const
54{
55 /* Sort partitioning becomes less effective when more shaders are in the wavefront. Also need to
56 * ensure the amount of shared memory required is within the limit. */
57 if (max_scene_shaders < min(300, cuda_device_->max_shared_mem_bytes / (int)sizeof(int))) {
58 return max(max_num_paths / 65536, 1);
59 }
60 else {
61 return 1;
62 }
63}
64
65bool CUDADeviceQueue::supports_local_atomic_sort() const
66{
68}
69
70void CUDADeviceQueue::init_execution()
71{
72 /* Synchronize all textures and memory copies before executing task.
73 * Use default stream (nullptr) since that's what we will synchronize
74 * here to ensure all scene data is copied. */
75 CUDAContextScope scope(cuda_device_);
76 cuda_device_->load_image_info(nullptr);
77 cuda_device_assert(cuda_device_, cuCtxSynchronize());
78
79 debug_init_execution();
80}
81
82void CUDADeviceQueue::load_image_info()
83{
84 CUDAContextScope scope(cuda_device_);
85 cuda_device_->load_image_info(this);
86}
87
88bool CUDADeviceQueue::enqueue(DeviceKernel kernel,
89 const int work_size,
90 const DeviceKernelArguments &args)
91{
92 if (cuda_device_->have_error()) {
93 return false;
94 }
95
96 debug_enqueue_begin(kernel, work_size);
97
98 const CUDAContextScope scope(cuda_device_);
99
100 /* Update image info in case integrator memory alloc caused texture to move to host. */
101 if (cuda_device_->load_image_info(nullptr)) {
102 cuda_device_assert(cuda_device_, cuCtxSynchronize());
103 if (cuda_device_->have_error()) {
104 return false;
105 }
106 }
107
108 /* Compute kernel launch parameters. */
109 const CUDADeviceKernel &cuda_kernel = cuda_device_->kernels.get(kernel);
110
111 int num_threads_per_block = cuda_kernel.num_threads_per_block;
112 int shared_mem_bytes = 0;
113
114 switch (kernel) {
123 /* See parall_active_index.h for why this amount of shared memory is needed. */
124 shared_mem_bytes = (num_threads_per_block + 1) * sizeof(int);
125 break;
126
129 num_threads_per_block = GPU_PARALLEL_SORT_BLOCK_SIZE;
130 shared_mem_bytes = cuda_device_->scene_max_shaders_ * sizeof(int);
131 break;
132
133 default:
134 break;
135 }
136
137 const int num_blocks = divide_up(work_size, num_threads_per_block);
138
139 /* Launch kernel. */
140 assert_success(cuLaunchKernel(cuda_kernel.function,
141 num_blocks,
142 1,
143 1,
144 num_threads_per_block,
145 1,
146 1,
147 shared_mem_bytes,
148 cuda_stream_,
149 const_cast<void **>(args.values),
150 nullptr),
151 "enqueue");
152
153 debug_enqueue_end();
154
155 return !(cuda_device_->have_error());
156}
157
158bool CUDADeviceQueue::synchronize()
159{
160 if (cuda_device_->have_error()) {
161 return false;
162 }
163
164 const CUDAContextScope scope(cuda_device_);
165 assert_success(cuStreamSynchronize(cuda_stream_), "synchronize");
166
167 debug_synchronize();
168
169 return !(cuda_device_->have_error());
170}
171
172void CUDADeviceQueue::zero_to_device(device_memory &mem)
173{
175
176 if (mem.memory_size() == 0) {
177 return;
178 }
179
180 /* Allocate on demand. */
181 if (mem.device_pointer == 0) {
182 if (mem.type == MEM_GLOBAL) {
183 cuda_device_->global_alloc(mem);
184 }
185 else {
186 cuda_device_->mem_alloc(mem);
187 }
188 }
189
190 /* Zero memory on device. */
191 device_ptr d_ptr = mem.device->mem_device_ptr(mem, cuda_device_);
192 assert(d_ptr != 0);
193
194 const CUDAContextScope scope(cuda_device_);
195 assert_success(cuMemsetD8Async((CUdeviceptr)d_ptr, 0, mem.memory_size(), cuda_stream_),
196 "zero_to_device");
197}
198
199void CUDADeviceQueue::copy_to_device(device_memory &mem)
200{
202
203 if (mem.memory_size() == 0) {
204 return;
205 }
206
207 /* Allocate on demand. */
208 if (mem.device_pointer == 0) {
209 if (mem.type == MEM_GLOBAL) {
210 cuda_device_->global_alloc(mem);
211 }
212 else {
213 cuda_device_->mem_alloc(mem);
214 }
215 }
216
217 device_ptr d_ptr = mem.device->mem_device_ptr(mem, cuda_device_);
218 assert(d_ptr != 0);
219 assert(mem.host_pointer != nullptr);
220
221 if (mem.is_shared(cuda_device_) && mem.host_pointer == mem.shared_pointer) {
222 return;
223 }
224
225 /* Copy memory to device. */
226 const CUDAContextScope scope(cuda_device_);
227 assert_success(
228 cuMemcpyHtoDAsync((CUdeviceptr)d_ptr, mem.host_pointer, mem.memory_size(), cuda_stream_),
229 "copy_to_device");
230}
231
232void CUDADeviceQueue::copy_from_device(device_memory &mem)
233{
234 assert(mem.type != MEM_GLOBAL && mem.type != MEM_IMAGE_TEXTURE);
235
236 if (mem.memory_size() == 0) {
237 return;
238 }
239
240 assert(mem.device_pointer != 0);
241 assert(mem.host_pointer != nullptr);
242
243 if (mem.is_shared(cuda_device_) && mem.host_pointer == mem.shared_pointer) {
244 return;
245 }
246
247 /* Copy memory from device. */
248 const CUDAContextScope scope(cuda_device_);
249 assert_success(
250 cuMemcpyDtoHAsync(
251 mem.host_pointer, (CUdeviceptr)mem.device_pointer, mem.memory_size(), cuda_stream_),
252 "copy_from_device");
253}
254
255void *CUDADeviceQueue::copy_from_device_synchronized(device_memory &mem, vector<uint8_t> &storage)
256{
257 if (mem.memory_size() == 0) {
258 return nullptr;
259 }
260
261 storage.resize(mem.memory_size());
262
263 device_ptr d_ptr = mem.device->mem_device_ptr(mem, cuda_device_);
264 assert(d_ptr != 0);
265
266 const CUDAContextScope scope(cuda_device_);
267 assert_success(
268 cuMemcpyDtoHAsync(storage.data(), (CUdeviceptr)d_ptr, mem.memory_size(), cuda_stream_),
269 "copy_from_device_synchronized");
270
271 synchronize();
272 return storage.data();
273}
274
275void CUDADeviceQueue::assert_success(CUresult result, const char *operation)
276{
277 if (result != CUDA_SUCCESS) {
278 const char *name = cuewErrorString(result);
279 cuda_device_->set_error(string_printf(
280 "%s in CUDA queue %s (%s)", name, operation, debug_active_kernels().c_str()));
281 }
282}
283
284unique_ptr<DeviceGraphicsInterop> CUDADeviceQueue::graphics_interop_create()
285{
286 return make_unique<CUDADeviceGraphicsInterop>(this);
287}
288
290
291#endif /* WITH_CUDA */
#define GPU_PARALLEL_SORT_BLOCK_SIZE
Definition block_sizes.h:18
btScalar max
CUDA cuda
Definition debug.h:146
virtual device_ptr mem_device_ptr(const device_memory &mem, Device *sub_device)
bool is_shared(Device *sub_device) const
Definition memory.cpp:198
@ 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_SORT_WRITE_PASS
@ 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_SORT_BUCKET_PASS
@ DEVICE_KERNEL_INTEGRATOR_TERMINATED_SHADOW_PATHS_ARRAY
@ DEVICE_KERNEL_INTEGRATOR_ACTIVE_PATHS_ARRAY
@ DEVICE_KERNEL_INTEGRATOR_COMPACT_PATHS_ARRAY
#define min(a, b)
Definition sort.cc:38
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
bool use_local_atomic_sort
Definition debug.h:62
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