Blender V5.3
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#include <climits>
6#include <iomanip>
7
8#include "device/device.h"
9#include "device/kernel.h"
10#include "device/queue.h"
11
12#include "util/algorithm.h"
13#include "util/log.h"
14#include "util/string.h"
15#include "util/time.h"
16
18
20{
21 DCHECK_NE(device, nullptr);
22 is_per_kernel_performance_ = getenv("CYCLES_DEBUG_PER_KERNEL_PERFORMANCE");
23}
24
25static void env_override(const char *name, int &value)
26{
27 if (const char *str = getenv(name)) {
28 value = max(atoi(str), 0);
29 }
30}
31
32int DeviceQueue::num_concurrent_states(const size_t state_size) const
33{
35 env_override("CYCLES_CONCURRENT_STATES_BASELINE", params.baseline);
36 env_override("CYCLES_CONCURRENT_STATES_MIN", params.min);
37 env_override("CYCLES_CONCURRENT_STATES_MAX", params.max);
38 env_override("CYCLES_CONCURRENT_STATES_GROW_PERCENT", params.grow_percent);
39 env_override("CYCLES_CONCURRENT_STATES_RESERVE_PERCENT", params.reserve_percent);
40
41 const int64_t baseline = params.baseline;
42 const int64_t min_num_states = params.min;
43 const int64_t max_num_states = max(params.min, params.max);
44 int64_t num_states = baseline;
45
46 /* Adjust to available memory, unless the number of states is fixed. */
47 if (min_num_states < max_num_states) {
48 size_t total_memory = 0;
49 size_t free_memory = 0;
50 get_memory_info(total_memory, free_memory);
51
52 const size_t reserve_memory = total_memory * size_t(params.reserve_percent) / 100;
53 const size_t budget_memory = free_memory - std::min(reserve_memory, free_memory);
54
55 /* Round down to a multiple of 65536 so sort partitions divide evenly. */
56 size_t fit_num_states = budget_memory / state_size;
57 if (fit_num_states > baseline) {
58 fit_num_states = round_down(fit_num_states * size_t(params.grow_percent) / 100, 65536);
59 num_states = std::max(int64_t(fit_num_states), baseline);
60 }
61 else {
62 num_states = round_down(fit_num_states, 65536);
63 }
64
65 num_states = std::clamp(num_states, min_num_states, max_num_states);
66
67 if (num_states != baseline) {
68 LOG_INFO << "Adjusted concurrent states to available memory: " << baseline << " -> "
69 << num_states << " (" << string_human_readable_size(num_states * state_size)
70 << " of " << string_human_readable_size(free_memory) << " free)";
71 }
72 }
73
74 if (const char *factor_str = getenv("CYCLES_CONCURRENT_STATES_FACTOR")) {
75 const float factor = float(atof(factor_str));
76 if (factor != 0.0f) {
77 num_states = std::max(int64_t(double(num_states) * factor), int64_t(1024));
78 }
79 else {
80 LOG_TRACE << "CYCLES_CONCURRENT_STATES_FACTOR evaluated to 0";
81 }
82 }
83
85
86 LOG_TRACE << "GPU queue concurrent states: " << num_states << ", using up to "
88
89 return num_states;
90}
91
93{
95 /* Print kernel execution times sorted by time. */
97 for (const auto &stat : stats_kernel_time_) {
98 stats_sorted.push_back(stat);
99 }
100
101 sort(stats_sorted.begin(),
102 stats_sorted.end(),
103 [](const pair<DeviceKernelMask, double> &a, const pair<DeviceKernelMask, double> &b) {
104 return a.second > b.second;
105 });
106
107 LOG_TRACE << "GPU queue stats:";
108 double total_time = 0.0;
109 for (const auto &[mask, time] : stats_sorted) {
110 total_time += time;
111 LOG_TRACE << " " << std::setfill(' ') << std::setw(10) << std::fixed << std::setprecision(5)
112 << std::right << time << "s: " << device_kernel_mask_as_string(mask);
113 }
114
116 LOG_TRACE << "GPU queue total time: " << std::fixed << std::setprecision(5) << total_time;
117 }
118 }
119}
120
129
131{
133 LOG_TRACE << "GPU queue launch " << device_kernel_as_string(kernel) << ", work_size "
134 << work_size;
135 }
136
137 last_kernels_enqueued_.set(kernel, true);
138}
139
146
148{
150 const double new_time = time_dt();
151 const double elapsed_time = new_time - last_sync_time_;
152 LOG_TRACE << "GPU queue synchronize, elapsed " << std::setw(10) << elapsed_time << "s";
153
154 /* There is no sense to have an entries in the performance data
155 * container without related kernel information. */
156 if (last_kernels_enqueued_.any()) {
158 }
159
160 last_sync_time_ = new_time;
161 }
162
164}
165
170
long long int int64_t
btScalar max
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
double last_sync_time_
DeviceKernelMask last_kernels_enqueued_
void debug_enqueue_end()
Definition queue.cpp:140
void debug_synchronize()
Definition queue.cpp:147
bool is_per_kernel_performance_
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 ConcurrentStatesParams concurrent_states_params() const =0
map< DeviceKernelMask, double > stats_kernel_time_
string debug_active_kernels()
Definition queue.cpp:166
virtual ~DeviceQueue()
Definition queue.cpp:92
virtual bool synchronize()=0
void debug_init_execution()
Definition queue.cpp:121
DeviceQueue(Device *device)
Definition queue.cpp:19
void debug_enqueue_begin(DeviceKernel kernel, const int work_size)
Definition queue.cpp:130
#define CCL_NAMESPACE_END
string device_kernel_mask_as_string(DeviceKernelMask mask)
const char * device_kernel_as_string(DeviceKernel kernel)
#define str(s)
#define INT_MAX
const int num_states
ccl_gpu_kernel_postfix const ccl_global int ccl_global float const int work_size
DeviceKernel
#define LOG_IS_ON(level)
Definition log.h:113
@ LOG_LEVEL_TRACE
Definition log.h:27
#define DCHECK_NE(a, b)
Definition log.h:143
#define LOG_INFO
Definition log.h:106
#define LOG_TRACE
Definition log.h:108
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
static void env_override(const char *name, int &value)
Definition queue.cpp:25
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
string string_human_readable_size(size_t size)
Definition string.cpp:333
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:31
ccl_device_inline size_t round_down(const size_t x, const size_t multiple)
Definition types_base.h:72