Blender V5.3
device/queue.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include <climits>
8
9#include "device/kernel.h"
10
12#include "util/log.h"
13#include "util/map.h"
14#include "util/string.h"
15#include "util/unique_ptr.h"
16
18
19class Device;
20class device_memory;
21
22struct KernelWorkTile;
23
24/* Container for device kernel arguments with type correctness ensured by API. */
26
34
35 static const int MAX_ARGS = 23;
38 size_t sizes[MAX_ARGS];
39 size_t count = 0;
40
42
43 template<class T> DeviceKernelArguments(const T *arg)
44 {
45 add(arg);
46 }
47
48 template<class T, class... Args> DeviceKernelArguments(const T *first, Args... args)
49 {
50 add(first);
51 add(args...);
52 }
53
54 void add(const KernelFilmConvert *value)
55 {
57 }
58 void add(const device_ptr *value)
59 {
60 add(POINTER, value, sizeof(device_ptr));
61 }
62 void add(const int32_t *value)
63 {
64 add(INT32, value, sizeof(int32_t));
65 }
66 void add(const float *value)
67 {
68 add(FLOAT32, value, sizeof(float));
69 }
70 void add(const Type type, const void *value, const size_t size)
71 {
73
74 types[count] = type;
75 values[count] = (void *)value;
76 sizes[count] = size;
77 count++;
78 }
79 template<typename T, typename... Args> void add(const T *first, Args... args)
80 {
81 add(first);
82 add(args...);
83 }
84};
85
86/* Parameters for choosing the number of concurrent integrator states,
87 * to balance performance and fitting in available memory. */
89 /* Default number of states estimated from number of processors. */
90 int baseline = 0;
91 /* Minimum and maximum number of states. */
92 int min = 65536;
93 int max = INT_MAX;
94
95 /* Percentage of free memory (minus reserve) to use when growing beyond the baseline. */
96 int grow_percent = 50;
97 /* Percentage of total memory to always keep free for later allocations. */
99};
100
101/* Abstraction of a command queue for a device.
102 * Provides API to schedule kernel execution in a specific queue with minimal possible overhead
103 * from driver side.
104 *
105 * This class encapsulates all properties needed for commands execution. */
107 public:
108 virtual ~DeviceQueue();
109
110 /* Number of concurrent states to process for integrator,
111 * based on number of cores and/or available memory. */
112 virtual int num_concurrent_states(const size_t state_size) const;
113
114 /* Number of partitions of sorted shaders, that improves memory locality of
115 * integrator state fetch at the cost of decreased coherence for shader kernel execution. */
116 virtual int num_sort_partitions(int max_num_paths, uint max_scene_shaders) const
117 {
118 /* Sort partitioning becomes less effective when more shaders are in the wavefront. In lieu of
119 * a more sophisticated heuristic we simply disable sort partitioning if the shader count is
120 * high.
121 */
122 if (max_scene_shaders < 300) {
123 return max(max_num_paths / 65536, 1);
124 }
125 else {
126 return 1;
127 }
128 }
129
130 /* Does device support local atomic sorting kernels (INTEGRATOR_SORT_BUCKET_PASS and
131 * INTEGRATOR_SORT_WRITE_PASS)? */
132 virtual bool supports_local_atomic_sort() const
133 {
134 return false;
135 }
136
137 /* Initialize execution of kernels on this queue.
138 *
139 * Will, for example, load all data required by the kernels from Device to global or path state.
140 *
141 * Use this method after device synchronization has finished before enqueueing any kernels. */
142 virtual void init_execution() = 0;
143
144 /* Update device-specific image state after allocating device_image. */
145 virtual void load_image_info() = 0;
146
147 /* Enqueue kernel execution.
148 *
149 * Execute the kernel work_size times on the device.
150 * Supported arguments types:
151 * - int: pass pointer to the int
152 * - device memory: pass pointer to device_memory.device_pointer
153 * Return false if there was an error executing this or a previous kernel. */
154 virtual bool enqueue(DeviceKernel kernel,
155 const int work_size,
156 const DeviceKernelArguments &args) = 0;
157
158 /* Wait unit all enqueued kernels have finished execution.
159 * Return false if there was an error executing any of the enqueued kernels. */
160 virtual bool synchronize() = 0;
161
162 /* Copy memory to/from device as part of the command queue, to ensure
163 * operations are done in order without having to synchronize. */
164 virtual void zero_to_device(device_memory &mem) = 0;
165 virtual void copy_to_device(device_memory &mem) = 0;
166 virtual void copy_from_device(device_memory &mem) = 0;
168
169 /* Graphics resources interoperability.
170 *
171 * The interoperability comes here by the meaning that the device is capable of computing result
172 * directly into an OpenGL (or other graphics library) buffer. */
173
174 /* Create graphics interoperability context which will be taking care of mapping graphics
175 * resource as a buffer writable by kernels of this device. */
177 {
178 LOG_FATAL << "Request of GPU interop of a device which does not support it.";
179 return nullptr;
180 }
181
182 /* Device this queue has been created for. */
183 Device *device = nullptr;
184
185 virtual void *native_queue()
186 {
187 return nullptr;
188 }
189
190 protected:
191 /* Hide construction so that allocation via `Device` API is enforced. */
192 explicit DeviceQueue(Device *device);
193
195 virtual void get_memory_info(size_t &total, size_t &free) const = 0;
196
197 /* Implementations call these from the corresponding methods to generate debugging logs. */
199 void debug_enqueue_begin(DeviceKernel kernel, const int work_size);
200 void debug_enqueue_end();
201 void debug_synchronize();
202 string debug_active_kernels();
203
204 /* Combination of kernels enqueued together sync last synchronize. */
206 /* Time of synchronize call. */
207 double last_sync_time_ = 0.0;
208 /* Accumulated execution time for combinations of kernels launched together. */
209 map<DeviceKernelMask, double> stats_kernel_time_;
210 /* If it is true, then a performance statistics in the debugging logs will have focus on kernels
211 * and an explicit queue synchronization will be added after each kernel execution. */
213};
214
btScalar max
double last_sync_time_
DeviceKernelMask last_kernels_enqueued_
void debug_enqueue_end()
Definition queue.cpp:140
virtual void copy_from_device(device_memory &mem)=0
void debug_synchronize()
Definition queue.cpp:147
bool is_per_kernel_performance_
virtual void load_image_info()=0
virtual bool supports_local_atomic_sort() const
virtual bool enqueue(DeviceKernel kernel, const int work_size, const DeviceKernelArguments &args)=0
Device * device
virtual void get_memory_info(size_t &total, size_t &free) const =0
virtual int num_concurrent_states(const size_t state_size) const
Definition queue.cpp:32
virtual void init_execution()=0
virtual void copy_to_device(device_memory &mem)=0
virtual ConcurrentStatesParams concurrent_states_params() const =0
map< DeviceKernelMask, double > stats_kernel_time_
virtual unique_ptr< DeviceGraphicsInterop > graphics_interop_create()
virtual int num_sort_partitions(int max_num_paths, uint max_scene_shaders) const
string debug_active_kernels()
Definition queue.cpp:166
virtual bool synchronize()=0
virtual void * native_queue()
void debug_init_execution()
Definition queue.cpp:121
DeviceQueue(Device *device)
Definition queue.cpp:19
virtual void * copy_from_device_synchronized(device_memory &mem, vector< uint8_t > &storage)=0
void debug_enqueue_begin(DeviceKernel kernel, const int work_size)
Definition queue.cpp:130
virtual void zero_to_device(device_memory &mem)=0
#define CCL_NAMESPACE_END
#define assert(assertion)
#define INT_MAX
#define storage(slot, qualifiers)
ccl_gpu_kernel_postfix const ccl_global int ccl_global float const int work_size
DeviceKernel
#define LOG_FATAL
Definition log.h:99
#define T
DeviceKernelArguments()=default
static const int MAX_ARGS
void add(const Type type, const void *value, const size_t size)
void add(const device_ptr *value)
DeviceKernelArguments(const T *arg)
void add(const T *first, Args... args)
void add(const float *value)
void * values[MAX_ARGS]
void add(const int32_t *value)
DeviceKernelArguments(const T *first, Args... args)
size_t sizes[MAX_ARGS]
void add(const KernelFilmConvert *value)
Type types[MAX_ARGS]
uint64_t device_ptr
Definition types_base.h:44