Blender V4.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
15/* CUDADeviceQueue */
16
17CUDADeviceQueue::CUDADeviceQueue(CUDADevice *device)
18 : DeviceQueue(device), cuda_device_(device), cuda_stream_(nullptr)
19{
20 const CUDAContextScope scope(cuda_device_);
21 cuda_device_assert(cuda_device_, cuStreamCreate(&cuda_stream_, CU_STREAM_NON_BLOCKING));
22}
23
24CUDADeviceQueue::~CUDADeviceQueue()
25{
26 const CUDAContextScope scope(cuda_device_);
27 cuStreamDestroy(cuda_stream_);
28}
29
30int CUDADeviceQueue::num_concurrent_states(const size_t state_size) const
31{
32 const int max_num_threads = cuda_device_->get_num_multiprocessors() *
33 cuda_device_->get_max_num_threads_per_multiprocessor();
34 int num_states = max(max_num_threads, 65536) * 16;
35
36 const char *factor_str = getenv("CYCLES_CONCURRENT_STATES_FACTOR");
37 if (factor_str) {
38 const float factor = (float)atof(factor_str);
39 if (factor != 0.0f) {
40 num_states = max((int)(num_states * factor), 1024);
41 }
42 else {
43 VLOG_DEVICE_STATS << "CYCLES_CONCURRENT_STATES_FACTOR evaluated to 0";
44 }
45 }
46
47 VLOG_DEVICE_STATS << "GPU queue concurrent states: " << num_states << ", using up to "
49
50 return num_states;
51}
52
53int CUDADeviceQueue::num_concurrent_busy_states(const size_t /*state_size*/) const
54{
55 const int max_num_threads = cuda_device_->get_num_multiprocessors() *
56 cuda_device_->get_max_num_threads_per_multiprocessor();
57
58 if (max_num_threads == 0) {
59 return 65536;
60 }
61
62 return 4 * max_num_threads;
63}
64
65void CUDADeviceQueue::init_execution()
66{
67 /* Synchronize all textures and memory copies before executing task. */
68 CUDAContextScope scope(cuda_device_);
69 cuda_device_->load_texture_info();
70 cuda_device_assert(cuda_device_, cuCtxSynchronize());
71
72 debug_init_execution();
73}
74
75bool CUDADeviceQueue::enqueue(DeviceKernel kernel,
76 const int work_size,
77 DeviceKernelArguments const &args)
78{
79 if (cuda_device_->have_error()) {
80 return false;
81 }
82
83 debug_enqueue_begin(kernel, work_size);
84
85 const CUDAContextScope scope(cuda_device_);
86 const CUDADeviceKernel &cuda_kernel = cuda_device_->kernels.get(kernel);
87
88 /* Compute kernel launch parameters. */
89 const int num_threads_per_block = cuda_kernel.num_threads_per_block;
90 const int num_blocks = divide_up(work_size, num_threads_per_block);
91
92 int shared_mem_bytes = 0;
93
94 switch (kernel) {
103 /* See parall_active_index.h for why this amount of shared memory is needed. */
104 shared_mem_bytes = (num_threads_per_block + 1) * sizeof(int);
105 break;
106
107 default:
108 break;
109 }
110
111 /* Launch kernel. */
112 assert_success(cuLaunchKernel(cuda_kernel.function,
113 num_blocks,
114 1,
115 1,
116 num_threads_per_block,
117 1,
118 1,
119 shared_mem_bytes,
120 cuda_stream_,
121 const_cast<void **>(args.values),
122 0),
123 "enqueue");
124
125 debug_enqueue_end();
126
127 return !(cuda_device_->have_error());
128}
129
130bool CUDADeviceQueue::synchronize()
131{
132 if (cuda_device_->have_error()) {
133 return false;
134 }
135
136 const CUDAContextScope scope(cuda_device_);
137 assert_success(cuStreamSynchronize(cuda_stream_), "synchronize");
138
139 debug_synchronize();
140
141 return !(cuda_device_->have_error());
142}
143
144void CUDADeviceQueue::zero_to_device(device_memory &mem)
145{
146 assert(mem.type != MEM_GLOBAL && mem.type != MEM_TEXTURE);
147
148 if (mem.memory_size() == 0) {
149 return;
150 }
151
152 /* Allocate on demand. */
153 if (mem.device_pointer == 0) {
154 cuda_device_->mem_alloc(mem);
155 }
156
157 /* Zero memory on device. */
158 assert(mem.device_pointer != 0);
159
160 const CUDAContextScope scope(cuda_device_);
161 assert_success(
162 cuMemsetD8Async((CUdeviceptr)mem.device_pointer, 0, mem.memory_size(), cuda_stream_),
163 "zero_to_device");
164}
165
166void CUDADeviceQueue::copy_to_device(device_memory &mem)
167{
168 assert(mem.type != MEM_GLOBAL && mem.type != MEM_TEXTURE);
169
170 if (mem.memory_size() == 0) {
171 return;
172 }
173
174 /* Allocate on demand. */
175 if (mem.device_pointer == 0) {
176 cuda_device_->mem_alloc(mem);
177 }
178
179 assert(mem.device_pointer != 0);
180 assert(mem.host_pointer != nullptr);
181
182 /* Copy memory to device. */
183 const CUDAContextScope scope(cuda_device_);
184 assert_success(
185 cuMemcpyHtoDAsync(
186 (CUdeviceptr)mem.device_pointer, mem.host_pointer, mem.memory_size(), cuda_stream_),
187 "copy_to_device");
188}
189
190void CUDADeviceQueue::copy_from_device(device_memory &mem)
191{
192 assert(mem.type != MEM_GLOBAL && mem.type != MEM_TEXTURE);
193
194 if (mem.memory_size() == 0) {
195 return;
196 }
197
198 assert(mem.device_pointer != 0);
199 assert(mem.host_pointer != nullptr);
200
201 /* Copy memory from device. */
202 const CUDAContextScope scope(cuda_device_);
203 assert_success(
204 cuMemcpyDtoHAsync(
205 mem.host_pointer, (CUdeviceptr)mem.device_pointer, mem.memory_size(), cuda_stream_),
206 "copy_from_device");
207}
208
209void CUDADeviceQueue::assert_success(CUresult result, const char *operation)
210{
211 if (result != CUDA_SUCCESS) {
212 const char *name = cuewErrorString(result);
213 cuda_device_->set_error(string_printf(
214 "%s in CUDA queue %s (%s)", name, operation, debug_active_kernels().c_str()));
215 }
216}
217
218unique_ptr<DeviceGraphicsInterop> CUDADeviceQueue::graphics_interop_create()
219{
220 return make_unique<CUDADeviceGraphicsInterop>(this);
221}
222
224
225#endif /* WITH_CUDA */
@ MEM_TEXTURE
#define CCL_NAMESPACE_END
draw_view in_light_buf[] float
ccl_gpu_kernel_postfix ccl_global const 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
#define VLOG_DEVICE_STATS
Definition log.h:78
string string_human_readable_size(size_t size)
Definition string.cpp:234
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:23
void * values[MAX_ARGS]
float max
ccl_device_inline size_t divide_up(size_t x, size_t y)
Definition util/types.h:53