Files
test/source/blender/functions/tests/FN_field_test.cc
Jacques Lucke 2cfcb8b0b8 BLI: refactor IndexMask for better performance and memory usage
Goals of this refactor:
* Reduce memory consumption of `IndexMask`. The old `IndexMask` uses an
  `int64_t` for each index which is more than necessary in pretty much all
  practical cases currently. Using `int32_t` might still become limiting
  in the future in case we use this to index e.g. byte buffers larger than
  a few gigabytes. We also don't want to template `IndexMask`, because
  that would cause a split in the "ecosystem", or everything would have to
  be implemented twice or templated.
* Allow for more multi-threading. The old `IndexMask` contains a single
  array. This is generally good but has the problem that it is hard to fill
  from multiple-threads when the final size is not known from the beginning.
  This is commonly the case when e.g. converting an array of bool to an
  index mask. Currently, this kind of code only runs on a single thread.
* Allow for efficient set operations like join, intersect and difference.
  It should be possible to multi-thread those operations.
* It should be possible to iterate over an `IndexMask` very efficiently.
  The most important part of that is to avoid all memory access when iterating
  over continuous ranges. For some core nodes (e.g. math nodes), we generate
  optimized code for the cases of irregular index masks and simple index ranges.

To achieve these goals, a few compromises had to made:
* Slicing of the mask (at specific indices) and random element access is
  `O(log #indices)` now, but with a low constant factor. It should be possible
  to split a mask into n approximately equally sized parts in `O(n)` though,
  making the time per split `O(1)`.
* Using range-based for loops does not work well when iterating over a nested
  data structure like the new `IndexMask`. Therefor, `foreach_*` functions with
  callbacks have to be used. To avoid extra code complexity at the call site,
  the `foreach_*` methods support multi-threading out of the box.

The new data structure splits an `IndexMask` into an arbitrary number of ordered
`IndexMaskSegment`. Each segment can contain at most `2^14 = 16384` indices. The
indices within a segment are stored as `int16_t`. Each segment has an additional
`int64_t` offset which allows storing arbitrary `int64_t` indices. This approach
has the main benefits that segments can be processed/constructed individually on
multiple threads without a serial bottleneck. Also it reduces the memory
requirements significantly.

For more details see comments in `BLI_index_mask.hh`.

I did a few tests to verify that the data structure generally improves
performance and does not cause regressions:
* Our field evaluation benchmarks take about as much as before. This is to be
  expected because we already made sure that e.g. add node evaluation is
  vectorized. The important thing here is to check that changes to the way we
  iterate over the indices still allows for auto-vectorization.
* Memory usage by a mask is about 1/4 of what it was before in the average case.
  That's mainly caused by the switch from `int64_t` to `int16_t` for indices.
  In the worst case, the memory requirements can be larger when there are many
  indices that are very far away. However, when they are far away from each other,
  that indicates that there aren't many indices in total. In common cases, memory
  usage can be way lower than 1/4 of before, because sub-ranges use static memory.
* For some more specific numbers I benchmarked `IndexMask::from_bools` in
  `index_mask_from_selection` on 10.000.000 elements at various probabilities for
  `true` at every index:
  ```
  Probability      Old        New
  0              4.6 ms     0.8 ms
  0.001          5.1 ms     1.3 ms
  0.2            8.4 ms     1.8 ms
  0.5           15.3 ms     3.0 ms
  0.8           20.1 ms     3.0 ms
  0.999         25.1 ms     1.7 ms
  1             13.5 ms     1.1 ms
  ```

Pull Request: https://projects.blender.org/blender/blender/pulls/104629
2023-05-24 18:11:41 +02:00

283 lines
8.5 KiB
C++

/* SPDX-License-Identifier: Apache-2.0 */
#include "testing/testing.h"
#include "BLI_cpp_type.hh"
#include "FN_field.hh"
#include "FN_multi_function_builder.hh"
#include "FN_multi_function_test_common.hh"
namespace blender::fn::tests {
TEST(field, ConstantFunction)
{
GField constant_field{
FieldOperation::Create(std::make_unique<mf::CustomMF_Constant<int>>(10), {}), 0};
Array<int> result(4);
FieldContext context;
FieldEvaluator evaluator{context, 4};
evaluator.add_with_destination(constant_field, result.as_mutable_span());
evaluator.evaluate();
EXPECT_EQ(result[0], 10);
EXPECT_EQ(result[1], 10);
EXPECT_EQ(result[2], 10);
EXPECT_EQ(result[3], 10);
}
class IndexFieldInput final : public FieldInput {
public:
IndexFieldInput() : FieldInput(CPPType::get<int>(), "Index") {}
GVArray get_varray_for_context(const FieldContext & /*context*/,
const IndexMask &mask,
ResourceScope & /*scope*/) const final
{
auto index_func = [](int i) { return i; };
return VArray<int>::ForFunc(mask.min_array_size(), index_func);
}
};
TEST(field, VArrayInput)
{
GField index_field{std::make_shared<IndexFieldInput>()};
Array<int> result_1(4);
FieldContext context;
FieldEvaluator evaluator{context, 4};
evaluator.add_with_destination(index_field, result_1.as_mutable_span());
evaluator.evaluate();
EXPECT_EQ(result_1[0], 0);
EXPECT_EQ(result_1[1], 1);
EXPECT_EQ(result_1[2], 2);
EXPECT_EQ(result_1[3], 3);
/* Evaluate a second time, just to test that the first didn't break anything. */
Array<int> result_2(10);
const Array<int64_t> indices = {2, 4, 6, 8};
IndexMaskMemory memory;
const IndexMask mask = IndexMask::from_indices<int64_t>(indices, memory);
FieldEvaluator evaluator_2{context, &mask};
evaluator_2.add_with_destination(index_field, result_2.as_mutable_span());
evaluator_2.evaluate();
EXPECT_EQ(result_2[2], 2);
EXPECT_EQ(result_2[4], 4);
EXPECT_EQ(result_2[6], 6);
EXPECT_EQ(result_2[8], 8);
}
TEST(field, VArrayInputMultipleOutputs)
{
std::shared_ptr<FieldInput> index_input = std::make_shared<IndexFieldInput>();
GField field_1{index_input};
GField field_2{index_input};
Array<int> result_1(10);
Array<int> result_2(10);
const Array<int64_t> indices = {2, 4, 6, 8};
IndexMaskMemory memory;
const IndexMask mask = IndexMask::from_indices<int64_t>(indices, memory);
FieldContext context;
FieldEvaluator evaluator{context, &mask};
evaluator.add_with_destination(field_1, result_1.as_mutable_span());
evaluator.add_with_destination(field_2, result_2.as_mutable_span());
evaluator.evaluate();
EXPECT_EQ(result_1[2], 2);
EXPECT_EQ(result_1[4], 4);
EXPECT_EQ(result_1[6], 6);
EXPECT_EQ(result_1[8], 8);
EXPECT_EQ(result_2[2], 2);
EXPECT_EQ(result_2[4], 4);
EXPECT_EQ(result_2[6], 6);
EXPECT_EQ(result_2[8], 8);
}
TEST(field, InputAndFunction)
{
GField index_field{std::make_shared<IndexFieldInput>()};
auto add_fn = mf::build::SI2_SO<int, int, int>("add", [](int a, int b) { return a + b; });
GField output_field{FieldOperation::Create(add_fn, {index_field, index_field}), 0};
Array<int> result(10);
const Array<int64_t> indices = {2, 4, 6, 8};
IndexMaskMemory memory;
const IndexMask mask = IndexMask::from_indices<int64_t>(indices, memory);
FieldContext context;
FieldEvaluator evaluator{context, &mask};
evaluator.add_with_destination(output_field, result.as_mutable_span());
evaluator.evaluate();
EXPECT_EQ(result[2], 4);
EXPECT_EQ(result[4], 8);
EXPECT_EQ(result[6], 12);
EXPECT_EQ(result[8], 16);
}
TEST(field, TwoFunctions)
{
GField index_field{std::make_shared<IndexFieldInput>()};
auto add_fn = mf::build::SI2_SO<int, int, int>("add", [](int a, int b) { return a + b; });
GField add_field{FieldOperation::Create(add_fn, {index_field, index_field}), 0};
auto add_10_fn = mf::build::SI1_SO<int, int>("add_10", [](int a) { return a + 10; });
GField result_field{FieldOperation::Create(add_10_fn, {add_field}), 0};
Array<int> result(10);
const Array<int64_t> indices = {2, 4, 6, 8};
IndexMaskMemory memory;
const IndexMask mask = IndexMask::from_indices<int64_t>(indices, memory);
FieldContext context;
FieldEvaluator evaluator{context, &mask};
evaluator.add_with_destination(result_field, result.as_mutable_span());
evaluator.evaluate();
EXPECT_EQ(result[2], 14);
EXPECT_EQ(result[4], 18);
EXPECT_EQ(result[6], 22);
EXPECT_EQ(result[8], 26);
}
class TwoOutputFunction : public mf::MultiFunction {
private:
mf::Signature signature_;
public:
TwoOutputFunction()
{
mf::SignatureBuilder builder{"Two Outputs", signature_};
builder.single_input<int>("In1");
builder.single_input<int>("In2");
builder.single_output<int>("Add");
builder.single_output<int>("Add10");
this->set_signature(&signature_);
}
void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
{
const VArray<int> &in1 = params.readonly_single_input<int>(0, "In1");
const VArray<int> &in2 = params.readonly_single_input<int>(1, "In2");
MutableSpan<int> add = params.uninitialized_single_output<int>(2, "Add");
MutableSpan<int> add_10 = params.uninitialized_single_output<int>(3, "Add10");
mask.foreach_index([&](const int64_t i) {
add[i] = in1[i] + in2[i];
add_10[i] = add[i] + 10;
});
}
};
TEST(field, FunctionTwoOutputs)
{
/* Also use two separate input fields, why not. */
GField index_field_1{std::make_shared<IndexFieldInput>()};
GField index_field_2{std::make_shared<IndexFieldInput>()};
std::shared_ptr<FieldOperation> fn = FieldOperation::Create(
std::make_unique<TwoOutputFunction>(), {index_field_1, index_field_2});
GField result_field_1{fn, 0};
GField result_field_2{fn, 1};
Array<int> result_1(10);
Array<int> result_2(10);
const Array<int64_t> indices = {2, 4, 6, 8};
IndexMaskMemory memory;
const IndexMask mask = IndexMask::from_indices<int64_t>(indices, memory);
FieldContext context;
FieldEvaluator evaluator{context, &mask};
evaluator.add_with_destination(result_field_1, result_1.as_mutable_span());
evaluator.add_with_destination(result_field_2, result_2.as_mutable_span());
evaluator.evaluate();
EXPECT_EQ(result_1[2], 4);
EXPECT_EQ(result_1[4], 8);
EXPECT_EQ(result_1[6], 12);
EXPECT_EQ(result_1[8], 16);
EXPECT_EQ(result_2[2], 14);
EXPECT_EQ(result_2[4], 18);
EXPECT_EQ(result_2[6], 22);
EXPECT_EQ(result_2[8], 26);
}
TEST(field, TwoFunctionsTwoOutputs)
{
GField index_field{std::make_shared<IndexFieldInput>()};
std::shared_ptr<FieldOperation> fn = FieldOperation::Create(
std::make_unique<TwoOutputFunction>(), {index_field, index_field});
Array<int64_t> mask_indices = {2, 4, 6, 8};
IndexMaskMemory memory;
IndexMask mask = IndexMask::from_indices<int64_t>(mask_indices, memory);
Field<int> result_field_1{fn, 0};
Field<int> intermediate_field{fn, 1};
auto add_10_fn = mf::build::SI1_SO<int, int>("add_10", [](int a) { return a + 10; });
Field<int> result_field_2{FieldOperation::Create(add_10_fn, {intermediate_field}), 0};
FieldContext field_context;
FieldEvaluator field_evaluator{field_context, &mask};
VArray<int> result_1;
VArray<int> result_2;
field_evaluator.add(result_field_1, &result_1);
field_evaluator.add(result_field_2, &result_2);
field_evaluator.evaluate();
EXPECT_EQ(result_1.get(2), 4);
EXPECT_EQ(result_1.get(4), 8);
EXPECT_EQ(result_1.get(6), 12);
EXPECT_EQ(result_1.get(8), 16);
EXPECT_EQ(result_2.get(2), 24);
EXPECT_EQ(result_2.get(4), 28);
EXPECT_EQ(result_2.get(6), 32);
EXPECT_EQ(result_2.get(8), 36);
}
TEST(field, SameFieldTwice)
{
GField constant_field{FieldOperation::Create(std::make_unique<mf::CustomMF_Constant<int>>(10)),
0};
FieldContext field_context;
IndexMask mask{IndexRange(2)};
ResourceScope scope;
Vector<GVArray> results = evaluate_fields(
scope, {constant_field, constant_field}, mask, field_context);
VArray<int> varray1 = results[0].typed<int>();
VArray<int> varray2 = results[1].typed<int>();
EXPECT_EQ(varray1.get(0), 10);
EXPECT_EQ(varray1.get(1), 10);
EXPECT_EQ(varray2.get(0), 10);
EXPECT_EQ(varray2.get(1), 10);
}
TEST(field, IgnoredOutput)
{
static mf::tests::OptionalOutputsFunction fn;
Field<int> field{FieldOperation::Create(fn), 0};
FieldContext field_context;
FieldEvaluator field_evaluator{field_context, 10};
VArray<int> results;
field_evaluator.add(field, &results);
field_evaluator.evaluate();
EXPECT_EQ(results.get(0), 5);
EXPECT_EQ(results.get(3), 5);
}
} // namespace blender::fn::tests