Blender  V3.3
multi_function.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "FN_multi_function.hh"
4 
5 #include "BLI_task.hh"
6 #include "BLI_threads.h"
7 
8 namespace blender::fn {
9 
11 
13 {
14  return this->get_execution_hints();
15 }
16 
18 {
19  return ExecutionHints{};
20 }
21 
23 {
24  for (const int i : fn.param_indices()) {
25  const MFParamType param_type = fn.param_type(i);
26  if (ELEM(param_type.interface_type(),
27  MFParamType::InterfaceType::Mutable,
29  if (param_type.data_type().is_vector()) {
30  return false;
31  }
32  }
33  }
34  return true;
35 }
36 
38 {
39  int64_t grain_size = hints.min_grain_size;
40  if (hints.uniform_execution_time) {
41  const int thread_count = BLI_system_thread_count();
42  /* Avoid using a small grain size even if it is not necessary. */
43  const int64_t thread_based_grain_size = mask.size() / thread_count / 4;
44  grain_size = std::max(grain_size, thread_based_grain_size);
45  }
46  if (hints.allocates_array) {
47  const int64_t max_grain_size = 10000;
48  /* Avoid allocating many large intermediate arrays. Better process data in smaller chunks to
49  * keep peak memory usage lower. */
50  grain_size = std::min(grain_size, max_grain_size);
51  }
52  return grain_size;
53 }
54 
56 {
57  if (mask.is_empty()) {
58  return;
59  }
60  const ExecutionHints hints = this->execution_hints();
61  const int64_t grain_size = compute_grain_size(hints, mask);
62 
63  if (mask.size() <= grain_size) {
64  this->call(mask, params, context);
65  return;
66  }
67 
68  const bool supports_threading = supports_threading_by_slicing_params(*this);
69  if (!supports_threading) {
70  this->call(mask, params, context);
71  return;
72  }
73 
74  threading::parallel_for(mask.index_range(), grain_size, [&](const IndexRange sub_range) {
75  const IndexMask sliced_mask = mask.slice(sub_range);
76  if (!hints.allocates_array) {
77  /* There is no benefit to changing indices in this case. */
78  this->call(sliced_mask, params, context);
79  return;
80  }
81  if (sliced_mask[0] < grain_size) {
82  /* The indices are low, no need to offset them. */
83  this->call(sliced_mask, params, context);
84  return;
85  }
86  const int64_t input_slice_start = sliced_mask[0];
87  const int64_t input_slice_size = sliced_mask.last() - input_slice_start + 1;
88  const IndexRange input_slice_range{input_slice_start, input_slice_size};
89 
90  Vector<int64_t> offset_mask_indices;
91  const IndexMask offset_mask = mask.slice_and_offset(sub_range, offset_mask_indices);
92 
93  MFParamsBuilder offset_params{*this, offset_mask.min_array_size()};
94 
95  /* Slice all parameters so that for the actual function call. */
96  for (const int param_index : this->param_indices()) {
97  const MFParamType param_type = this->param_type(param_index);
98  switch (param_type.category()) {
100  const GVArray &varray = params.readonly_single_input(param_index);
101  offset_params.add_readonly_single_input(varray.slice(input_slice_range));
102  break;
103  }
105  const GMutableSpan span = params.single_mutable(param_index);
106  const GMutableSpan sliced_span = span.slice(input_slice_range);
107  offset_params.add_single_mutable(sliced_span);
108  break;
109  }
111  const GMutableSpan span = params.uninitialized_single_output_if_required(param_index);
112  if (span.is_empty()) {
113  offset_params.add_ignored_single_output();
114  }
115  else {
116  const GMutableSpan sliced_span = span.slice(input_slice_range);
117  offset_params.add_uninitialized_single_output(sliced_span);
118  }
119  break;
120  }
125  break;
126  }
127  }
128  }
129 
130  this->call(offset_mask, offset_params, context);
131  });
132 }
133 
134 std::string MultiFunction::debug_name() const
135 {
136  return signature_ref_->function_name;
137 }
138 
139 } // namespace blender::fn
#define BLI_assert_unreachable()
Definition: BLI_assert.h:93
int BLI_system_thread_count(void)
Definition: threads.cc:281
#define ELEM(...)
Group Output
GMutableSpan slice(const int64_t start, int64_t size) const
GVArray slice(IndexRange slice) const
int64_t min_array_size() const
virtual ExecutionHints get_execution_hints() const
MFParamType param_type(int param_index) const
IndexRange param_indices() const
void call_auto(IndexMask mask, MFParams params, MFContext context) const
virtual void call(IndexMask mask, MFParams params, MFContext context) const =0
ExecutionHints execution_hints() const
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
static bool supports_threading_by_slicing_params(const MultiFunction &fn)
static int64_t compute_grain_size(const ExecutionHints &hints, const IndexMask mask)
MultiFunction::ExecutionHints ExecutionHints
void parallel_for(IndexRange range, int64_t grain_size, const Function &function)
Definition: BLI_task.hh:51
#define min(a, b)
Definition: sort.c:35
__int64 int64_t
Definition: stdint.h:89
float max