2021-09-15 11:02:39 +02:00
|
|
|
/*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "FN_multi_function_parallel.hh"
|
|
|
|
|
|
|
|
|
|
#include "BLI_task.hh"
|
|
|
|
|
|
|
|
|
|
namespace blender::fn {
|
|
|
|
|
|
|
|
|
|
ParallelMultiFunction::ParallelMultiFunction(const MultiFunction &fn, const int64_t grain_size)
|
|
|
|
|
: fn_(fn), grain_size_(grain_size)
|
|
|
|
|
{
|
|
|
|
|
this->set_signature(&fn.signature());
|
|
|
|
|
|
|
|
|
|
threading_supported_ = true;
|
|
|
|
|
for (const int param_index : fn.param_indices()) {
|
|
|
|
|
const MFParamType param_type = fn.param_type(param_index);
|
|
|
|
|
if (param_type.data_type().category() == MFDataType::Vector) {
|
|
|
|
|
/* Vector parameters do not support threading yet. */
|
|
|
|
|
threading_supported_ = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ParallelMultiFunction::call(IndexMask full_mask, MFParams params, MFContext context) const
|
|
|
|
|
{
|
|
|
|
|
if (full_mask.size() <= grain_size_ || !threading_supported_) {
|
|
|
|
|
fn_.call(full_mask, params, context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
threading::parallel_for(full_mask.index_range(), grain_size_, [&](const IndexRange mask_slice) {
|
|
|
|
|
Vector<int64_t> sub_mask_indices;
|
|
|
|
|
const IndexMask sub_mask = full_mask.slice_and_offset(mask_slice, sub_mask_indices);
|
|
|
|
|
if (sub_mask.is_empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const int64_t input_slice_start = full_mask[mask_slice.first()];
|
|
|
|
|
const int64_t input_slice_size = full_mask[mask_slice.last()] - input_slice_start + 1;
|
|
|
|
|
const IndexRange input_slice_range{input_slice_start, input_slice_size};
|
|
|
|
|
|
|
|
|
|
MFParamsBuilder sub_params{fn_, sub_mask.min_array_size()};
|
|
|
|
|
|
|
|
|
|
/* All parameters are sliced so that the wrapped multi-function does not have to take care of
|
|
|
|
|
* the index offset. */
|
|
|
|
|
for (const int param_index : fn_.param_indices()) {
|
|
|
|
|
const MFParamType param_type = fn_.param_type(param_index);
|
|
|
|
|
switch (param_type.category()) {
|
|
|
|
|
case MFParamType::SingleInput: {
|
|
|
|
|
const GVArray &varray = params.readonly_single_input(param_index);
|
Geometry Nodes: refactor virtual array system
Goals of this refactor:
* Simplify creating virtual arrays.
* Simplify passing virtual arrays around.
* Simplify converting between typed and generic virtual arrays.
* Reduce memory allocations.
As a quick reminder, a virtual arrays is a data structure that behaves like an
array (i.e. it can be accessed using an index). However, it may not actually
be stored as array internally. The two most important implementations
of virtual arrays are those that correspond to an actual plain array and those
that have the same value for every index. However, many more
implementations exist for various reasons (interfacing with legacy attributes,
unified iterator over all points in multiple splines, ...).
With this refactor the core types (`VArray`, `GVArray`, `VMutableArray` and
`GVMutableArray`) can be used like "normal values". They typically live
on the stack. Before, they were usually inside a `std::unique_ptr`. This makes
passing them around much easier. Creation of new virtual arrays is also
much simpler now due to some constructors. Memory allocations are
reduced by making use of small object optimization inside the core types.
Previously, `VArray` was a class with virtual methods that had to be overridden
to change the behavior of a the virtual array. Now,`VArray` has a fixed size
and has no virtual methods. Instead it contains a `VArrayImpl` that is
similar to the old `VArray`. `VArrayImpl` should rarely ever be used directly,
unless a new virtual array implementation is added.
To support the small object optimization for many `VArrayImpl` classes,
a new `blender::Any` type is added. It is similar to `std::any` with two
additional features. It has an adjustable inline buffer size and alignment.
The inline buffer size of `std::any` can't be relied on and is usually too
small for our use case here. Furthermore, `blender::Any` can store
additional user-defined type information without increasing the
stack size.
Differential Revision: https://developer.blender.org/D12986
2021-11-16 10:15:51 +01:00
|
|
|
sub_params.add_readonly_single_input(varray.slice(input_slice_range));
|
2021-09-15 11:02:39 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MFParamType::SingleMutable: {
|
|
|
|
|
const GMutableSpan span = params.single_mutable(param_index);
|
|
|
|
|
const GMutableSpan sliced_span = span.slice(input_slice_start, input_slice_size);
|
|
|
|
|
sub_params.add_single_mutable(sliced_span);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MFParamType::SingleOutput: {
|
|
|
|
|
const GMutableSpan span = params.uninitialized_single_output(param_index);
|
|
|
|
|
const GMutableSpan sliced_span = span.slice(input_slice_start, input_slice_size);
|
|
|
|
|
sub_params.add_uninitialized_single_output(sliced_span);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MFParamType::VectorInput:
|
|
|
|
|
case MFParamType::VectorMutable:
|
|
|
|
|
case MFParamType::VectorOutput: {
|
|
|
|
|
BLI_assert_unreachable();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn_.call(sub_mask, sub_params, context);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace blender::fn
|