Blender V5.3
multi_function.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BLI_task.hh"
12#include "BLI_threads.hh"
13
15
17
19{
20 hash.add(this);
21}
22
23bool MultiFunction::equals(const MultiFunction &other) const
24{
25 return this == &other;
26}
27
32
37
39{
40 for (const int i : fn.param_indices()) {
41 const ParamType param_type = fn.param_type(i);
42 if (ELEM(param_type.interface_type(),
45 {
46 if (param_type.data_type().is_vector()) {
47 return false;
48 }
49 }
50 }
51 return true;
52}
53
55{
56 int64_t grain_size = hints.min_grain_size;
57 if (hints.uniform_execution_time) {
58 const int thread_count = BLI_system_thread_count();
59 /* Avoid using a small grain size even if it is not necessary. */
60 const int64_t thread_based_grain_size = mask.size() / thread_count / 4;
61 grain_size = std::max(grain_size, thread_based_grain_size);
62 }
63 if (hints.allocates_array) {
64 const int64_t max_grain_size = 10000;
65 /* Avoid allocating many large intermediate arrays. Better process data in smaller chunks to
66 * keep peak memory usage lower. */
67 grain_size = std::min(grain_size, max_grain_size);
68 }
69 return grain_size;
70}
71
72static int64_t compute_alignment(const int64_t grain_size)
73{
74 if (grain_size <= 512) {
75 /* Don't use a number that's too large, or otherwise the work will be split quite unevenly. */
76 return 8;
77 }
78 /* It's not common that more elements are processed in a loop at once. */
79 return 32;
80}
81
82static void add_sliced_parameters(const Signature &signature,
83 Params &full_params,
84 const IndexRange slice_range,
85 ParamsBuilder &r_sliced_params)
86{
87 for (const int param_index : signature.params.index_range()) {
88 const ParamType &param_type = signature.params[param_index].type;
89 switch (param_type.category()) {
91 const GVArray &varray = full_params.readonly_single_input(param_index);
92 r_sliced_params.add_readonly_single_input(varray.slice(slice_range));
93 break;
94 }
96 const GMutableSpan span = full_params.single_mutable(param_index);
97 const GMutableSpan sliced_span = span.slice(slice_range);
98 r_sliced_params.add_single_mutable(sliced_span);
99 break;
100 }
102 if (flag_is_set(signature.params[param_index].flag, ParamFlag::SupportsUnusedOutput)) {
104 param_index);
105 if (span.is_empty()) {
106 r_sliced_params.add_ignored_single_output();
107 }
108 else {
109 const GMutableSpan sliced_span = span.slice(slice_range);
110 r_sliced_params.add_uninitialized_single_output(sliced_span);
111 }
112 }
113 else {
114 const GMutableSpan span = full_params.uninitialized_single_output(param_index);
115 const GMutableSpan sliced_span = span.slice(slice_range);
116 r_sliced_params.add_uninitialized_single_output(sliced_span);
117 }
118 break;
119 }
124 break;
125 }
126 }
127 }
128}
129
130void MultiFunction::call_auto(const IndexMask &mask, Params params, Context context) const
131{
132 if (mask.is_empty()) {
133 return;
134 }
135 const ExecutionHints hints = this->execution_hints();
136 const int64_t grain_size = compute_grain_size(hints, mask);
137
138 if (mask.size() <= grain_size) {
139 this->call(mask, params, context);
140 return;
141 }
142
143 const bool supports_threading = supports_threading_by_slicing_params(*this);
144 if (!supports_threading) {
145 this->call(mask, params, context);
146 return;
147 }
148
149 const int64_t alignment = compute_alignment(grain_size);
151 mask.index_range(), grain_size, alignment, [&](const IndexRange sub_range) {
152 const IndexMask sliced_mask = mask.slice(sub_range);
153 if (!hints.allocates_array) {
154 /* There is no benefit to changing indices in this case. */
155 this->call(sliced_mask, params, context);
156 return;
157 }
158 if (sliced_mask[0] < grain_size) {
159 /* The indices are low, no need to offset them. */
160 this->call(sliced_mask, params, context);
161 return;
162 }
163 const int64_t input_slice_start = sliced_mask[0];
164 const int64_t input_slice_size = sliced_mask.last() - input_slice_start + 1;
165 const IndexRange input_slice_range{input_slice_start, input_slice_size};
166
167 IndexMaskMemory memory;
168 const int64_t offset = -input_slice_start;
169 const IndexMask shifted_mask = mask.slice_and_shift(sub_range, offset, memory);
170
171 ParamsBuilder sliced_params{*this, &shifted_mask};
172 add_sliced_parameters(*signature_ref_, params, input_slice_range, sliced_params);
173 this->call(shifted_mask, sliced_params, context);
174 });
175}
176
177std::string MultiFunction::debug_name() const
178{
179 return signature_ref_->function_name;
180}
181
182} // namespace blender::fn::multi_function
#define BLI_assert_unreachable()
#define ELEM(...)
GMutableSpan slice(const int64_t start, int64_t size) const
GVArray slice(IndexRange slice) const
virtual bool equals(const MultiFunction &other) const
virtual void call(const IndexMask &mask, Params params, Context context) const =0
virtual void hash_unique(UniqueHashBytes &hash) const
virtual ExecutionHints get_execution_hints() const
void call_auto(const IndexMask &mask, Params params, Context context) const
void add_single_mutable(GMutableSpan ref, StringRef expected_name="")
void add_ignored_single_output(StringRef expected_name="")
void add_uninitialized_single_output(T *value, StringRef expected_name="")
void add_readonly_single_input(const T *value, StringRef expected_name="")
MutableSpan< T > uninitialized_single_output_if_required(int param_index, StringRef name="")
MutableSpan< T > uninitialized_single_output(int param_index, StringRef name="")
MutableSpan< T > single_mutable(int param_index, StringRef name="")
VArray< T > readonly_single_input(int param_index, StringRef name="")
static bool supports_threading_by_slicing_params(const MultiFunction &fn)
MultiFunction::ExecutionHints ExecutionHints
static void add_sliced_parameters(const Signature &signature, Params &full_params, const IndexRange slice_range, ParamsBuilder &r_sliced_params)
static int64_t compute_alignment(const int64_t grain_size)
static int64_t compute_grain_size(const ExecutionHints &hints, const IndexMask &mask)
void parallel_for_aligned(const IndexRange range, const int64_t grain_size, const int64_t alignment, const Function &function)
Definition BLI_task.hh:115
__int64 int64_t
Definition BKE_text.h:90
int BLI_system_thread_count()
Definition threads.cc:258
#define hash
Definition noise_c.cc:156