Blender V5.3
oneapi/queue.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2025 Intel Corporation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifdef WITH_ONEAPI
6
7# include "device/oneapi/queue.h"
10# include "util/log.h"
11
13
15
16struct KernelExecutionInfo {
17 double elapsed_summary = 0.0;
18 int enqueue_count = 0;
19};
20
21/* OneapiDeviceQueue */
22
23OneapiDeviceQueue::OneapiDeviceQueue(OneapiDevice *device)
24 : DeviceQueue(device), oneapi_device_(device)
25{
26}
27
28ConcurrentStatesParams OneapiDeviceQueue::concurrent_states_params() const
29{
30# if 1
31 /* Keep the same number of concurrent states as Blender 5.2 and earlier.
32 * For other GPU backends there have been reports about TDR timeouts,
33 * excessive memory usage with multiple Blender instances, etc. Assume
34 * oneAPI is likely affected too and don't use more memory. */
35 const int max_num_threads = oneapi_device_->get_num_multiprocessors() *
36 oneapi_device_->get_max_num_threads_per_multiprocessor();
37
39 params.baseline = 16 * max(8 * max_num_threads, 65536);
40# else
41 /* NOTE(@xavierh-intel): At the time of writing this, 32768 * the number of
42 * Execution Units is the best baseline across at least Intel Arc B390 and
43 * Arc Pro B70. */
45 params.baseline = 32768 * oneapi_device_->get_num_multiprocessors();
46# ifdef __linux__
47 /* Intel Arc A770 and older on Linux use i915 drivers that don't accurately
48 * report free memory in default configuration, we need to be more careful
49 * with the number of states on these platforms, so we use the original lower
50 * baseline. */
51 if (oneapi_device_->get_max_num_threads_per_multiprocessor() < 128) {
52 const int max_num_threads = oneapi_device_->get_num_multiprocessors() *
53 oneapi_device_->get_max_num_threads_per_multiprocessor();
54 params.baseline = 16 * max(8 * max_num_threads, 65536);
55 }
56# endif
57# endif
58
59 /* Only shrink to fit within available memory, growing did not show clear
60 * benefit compared to the baseline. */
61 params.min = params.baseline / 8;
62 params.max = params.baseline;
63 params.reserve_percent = 5;
64
65 return params;
66}
67
68void OneapiDeviceQueue::get_memory_info(size_t &total, size_t &free) const
69{
70 oneapi_device_->get_device_memory_info(total, free);
71}
72
73int OneapiDeviceQueue::num_sort_partitions(int max_num_paths, uint /*max_scene_shaders*/) const
74{
75 int sort_partition_elements = (oneapi_device_->get_max_num_threads_per_multiprocessor() >= 128) ?
76 32768 :
77 8192;
78 /* Sort partitioning with local sorting on Intel GPUs is currently the most effective solution no
79 * matter the number of shaders. */
80 return max(max_num_paths / sort_partition_elements, 1);
81}
82
83void OneapiDeviceQueue::init_execution()
84{
85 oneapi_device_->load_image_info(nullptr);
86
87 SyclQueue *device_queue = oneapi_device_->sycl_queue();
88 void *kg_dptr = oneapi_device_->kernel_globals_device_pointer();
89 assert(device_queue);
90 assert(kg_dptr);
91 kernel_context_ = make_unique<KernelContext>();
92 kernel_context_->queue = device_queue;
93 kernel_context_->kernel_globals = kg_dptr;
94
95 debug_init_execution();
96}
97
98void OneapiDeviceQueue::load_image_info()
99{
100 oneapi_device_->load_image_info(this);
101}
102
103bool OneapiDeviceQueue::enqueue(DeviceKernel kernel,
104 const int signed_kernel_work_size,
105 const DeviceKernelArguments &_args)
106{
107 if (oneapi_device_->have_error()) {
108 return false;
109 }
110
111 /* Update image info in case memory moved to host. */
112 if (oneapi_device_->load_image_info(nullptr)) {
113 if (!synchronize()) {
114 return false;
115 }
116 }
117
118 void **args = const_cast<void **>(_args.values);
119
120 debug_enqueue_begin(kernel, signed_kernel_work_size);
121 assert(signed_kernel_work_size >= 0);
122 size_t kernel_global_size = (size_t)signed_kernel_work_size;
123 size_t kernel_local_size;
124
125 assert(kernel_context_);
126 kernel_context_->scene_max_shaders = oneapi_device_->scene_max_shaders();
127
128 oneapi_device_->get_adjusted_global_and_local_sizes(
129 kernel_context_->queue, kernel, kernel_global_size, kernel_local_size);
130
131 /* Call the oneAPI kernel DLL to launch the requested kernel. */
132 bool is_finished_ok = oneapi_device_->enqueue_kernel(
133 kernel_context_.get(), kernel, kernel_global_size, kernel_local_size, args);
134
135 if (is_finished_ok == false) {
136 oneapi_device_->set_error("oneAPI kernel \"" + std::string(device_kernel_as_string(kernel)) +
137 "\" execution error: got runtime exception \"" +
138 oneapi_device_->oneapi_error_message() + "\"");
139 }
140
141 debug_enqueue_end();
142
143 return is_finished_ok;
144}
145
146bool OneapiDeviceQueue::synchronize()
147{
148 if (oneapi_device_->have_error()) {
149 return false;
150 }
151
152 bool is_finished_ok = oneapi_device_->queue_synchronize(oneapi_device_->sycl_queue());
153 if (is_finished_ok == false) {
154 oneapi_device_->set_error("oneAPI unknown kernel execution error: got runtime exception \"" +
155 oneapi_device_->oneapi_error_message() + "\"");
156 }
157
158 debug_synchronize();
159
160 return !(oneapi_device_->have_error());
161}
162
163void OneapiDeviceQueue::zero_to_device(device_memory &mem)
164{
165 oneapi_device_->mem_zero(mem);
166}
167
168void OneapiDeviceQueue::copy_to_device(device_memory &mem)
169{
170 oneapi_device_->mem_copy_to(mem);
171}
172
173void OneapiDeviceQueue::copy_from_device(device_memory &mem)
174{
175 oneapi_device_->mem_copy_from(mem);
176}
177
178void *OneapiDeviceQueue::copy_from_device_synchronized(device_memory &mem,
180{
181 if (mem.memory_size() == 0) {
182 return nullptr;
183 }
184
185 storage.resize(mem.memory_size());
186 oneapi_device_->mem_copy_from(mem, 0, 0, 0, 0, storage.data());
187 synchronize();
188 return storage.data();
189}
190
191# ifdef SYCL_LINEAR_MEMORY_INTEROP_AVAILABLE
192unique_ptr<DeviceGraphicsInterop> OneapiDeviceQueue::graphics_interop_create()
193{
194 return make_unique<OneapiDeviceGraphicsInterop>(this);
195}
196# endif
197
199
200#endif /* WITH_ONEAPI */
btScalar max
#define CCL_NAMESPACE_END
const char * device_kernel_as_string(DeviceKernel kernel)
#define assert(assertion)
#define storage(slot, qualifiers)
DeviceKernel
void * values[MAX_ARGS]