2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2022-09-14 08:54:04 -05:00
|
|
|
|
2025-03-29 15:18:50 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bli
|
|
|
|
|
*/
|
|
|
|
|
|
2024-01-12 14:30:34 +01:00
|
|
|
#include <functional>
|
|
|
|
|
|
2022-09-13 11:36:14 -05:00
|
|
|
#include "BLI_array_utils.hh"
|
2023-07-05 16:05:16 +02:00
|
|
|
#include "BLI_threads.h"
|
2022-09-13 11:36:14 -05:00
|
|
|
|
2023-07-03 18:47:03 -04:00
|
|
|
#include "atomic_ops.h"
|
|
|
|
|
|
2022-09-13 11:36:14 -05:00
|
|
|
namespace blender::array_utils {
|
|
|
|
|
|
2023-03-19 16:18:19 +01:00
|
|
|
void copy(const GVArray &src, GMutableSpan dst, const int64_t grain_size)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(src.type() == dst.type());
|
|
|
|
|
BLI_assert(src.size() == dst.size());
|
|
|
|
|
threading::parallel_for(src.index_range(), grain_size, [&](const IndexRange range) {
|
|
|
|
|
src.materialize_to_uninitialized(range, dst.data());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-13 11:36:14 -05:00
|
|
|
void copy(const GVArray &src,
|
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
|
|
|
const IndexMask &selection,
|
2022-09-13 11:36:14 -05:00
|
|
|
GMutableSpan dst,
|
|
|
|
|
const int64_t grain_size)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(src.type() == dst.type());
|
2023-01-17 13:11:56 +01:00
|
|
|
BLI_assert(src.size() >= selection.min_array_size());
|
|
|
|
|
BLI_assert(dst.size() >= selection.min_array_size());
|
2022-09-13 11:36:14 -05:00
|
|
|
threading::parallel_for(selection.index_range(), grain_size, [&](const IndexRange range) {
|
|
|
|
|
src.materialize_to_uninitialized(selection.slice(range), dst.data());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-17 22:12:02 -05:00
|
|
|
void gather(const GVArray &src,
|
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
|
|
|
const IndexMask &indices,
|
2022-09-17 22:12:02 -05:00
|
|
|
GMutableSpan dst,
|
|
|
|
|
const int64_t grain_size)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(src.type() == dst.type());
|
|
|
|
|
BLI_assert(indices.size() == dst.size());
|
|
|
|
|
threading::parallel_for(indices.index_range(), grain_size, [&](const IndexRange range) {
|
|
|
|
|
src.materialize_compressed_to_uninitialized(indices.slice(range), dst.slice(range).data());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
void gather(const GSpan src, const IndexMask &indices, GMutableSpan dst, const int64_t grain_size)
|
2022-10-19 12:38:48 -05:00
|
|
|
{
|
2025-07-17 09:09:16 +02:00
|
|
|
gather(GVArray::from_span(src), indices, dst, grain_size);
|
2022-10-19 12:38:48 -05:00
|
|
|
}
|
|
|
|
|
|
2023-10-09 15:23:35 +02:00
|
|
|
void copy_group_to_group(const OffsetIndices<int> src_offsets,
|
|
|
|
|
const OffsetIndices<int> dst_offsets,
|
|
|
|
|
const IndexMask &selection,
|
|
|
|
|
const GSpan src,
|
|
|
|
|
GMutableSpan dst)
|
|
|
|
|
{
|
|
|
|
|
/* Each group might be large, so a threaded copy might make sense here too. */
|
|
|
|
|
selection.foreach_index(GrainSize(512), [&](const int i) {
|
|
|
|
|
dst.slice(dst_offsets[i]).copy_from(src.slice(src_offsets[i]));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 18:47:03 -04:00
|
|
|
void count_indices(const Span<int> indices, MutableSpan<int> counts)
|
|
|
|
|
{
|
2023-07-05 16:05:16 +02:00
|
|
|
if (indices.size() < 8192 || BLI_system_thread_count() < 4) {
|
|
|
|
|
for (const int i : indices) {
|
|
|
|
|
counts[i]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
threading::parallel_for(indices.index_range(), 4096, [&](const IndexRange range) {
|
|
|
|
|
for (const int i : indices.slice(range)) {
|
|
|
|
|
atomic_add_and_fetch_int32(&counts[i], 1);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-03 18:47:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 22:59:25 -05:00
|
|
|
void invert_booleans(MutableSpan<bool> span)
|
|
|
|
|
{
|
|
|
|
|
threading::parallel_for(span.index_range(), 4096, [&](IndexRange range) {
|
|
|
|
|
for (const int i : range) {
|
|
|
|
|
span[i] = !span[i];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-20 10:12:24 +02:00
|
|
|
void invert_booleans(MutableSpan<bool> span, const IndexMask &mask)
|
|
|
|
|
{
|
|
|
|
|
mask.foreach_index_optimized<int64_t>([&](const int64_t i) { span[i] = !span[i]; });
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 18:44:03 -04:00
|
|
|
static bool all_equal(const Span<bool> span, const bool test)
|
|
|
|
|
{
|
|
|
|
|
return std::all_of(span.begin(), span.end(), [&](const bool value) { return value == test; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool all_equal(const VArray<bool> &varray, const IndexRange range, const bool test)
|
|
|
|
|
{
|
|
|
|
|
return std::all_of(
|
|
|
|
|
range.begin(), range.end(), [&](const int64_t i) { return varray[i] == test; });
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 09:54:25 -04:00
|
|
|
BooleanMix booleans_mix_calc(const VArray<bool> &varray, const IndexRange range_to_check)
|
|
|
|
|
{
|
|
|
|
|
if (varray.is_empty()) {
|
|
|
|
|
return BooleanMix::None;
|
|
|
|
|
}
|
|
|
|
|
const CommonVArrayInfo info = varray.common_info();
|
|
|
|
|
if (info.type == CommonVArrayInfo::Type::Single) {
|
|
|
|
|
return *static_cast<const bool *>(info.data) ? BooleanMix::AllTrue : BooleanMix::AllFalse;
|
|
|
|
|
}
|
|
|
|
|
if (info.type == CommonVArrayInfo::Type::Span) {
|
|
|
|
|
const Span<bool> span(static_cast<const bool *>(info.data), varray.size());
|
|
|
|
|
return threading::parallel_reduce(
|
|
|
|
|
range_to_check,
|
|
|
|
|
4096,
|
|
|
|
|
BooleanMix::None,
|
|
|
|
|
[&](const IndexRange range, const BooleanMix init) {
|
|
|
|
|
if (init == BooleanMix::Mixed) {
|
|
|
|
|
return init;
|
|
|
|
|
}
|
|
|
|
|
const Span<bool> slice = span.slice(range);
|
2024-06-14 18:44:03 -04:00
|
|
|
const bool compare = (init == BooleanMix::None) ? slice.first() :
|
|
|
|
|
(init == BooleanMix::AllTrue);
|
|
|
|
|
if (all_equal(slice, compare)) {
|
|
|
|
|
return compare ? BooleanMix::AllTrue : BooleanMix::AllFalse;
|
2023-05-24 09:54:25 -04:00
|
|
|
}
|
2024-06-14 18:44:03 -04:00
|
|
|
return BooleanMix::Mixed;
|
2023-05-24 09:54:25 -04:00
|
|
|
},
|
|
|
|
|
[&](BooleanMix a, BooleanMix b) { return (a == b) ? a : BooleanMix::Mixed; });
|
|
|
|
|
}
|
|
|
|
|
return threading::parallel_reduce(
|
|
|
|
|
range_to_check,
|
|
|
|
|
2048,
|
|
|
|
|
BooleanMix::None,
|
|
|
|
|
[&](const IndexRange range, const BooleanMix init) {
|
|
|
|
|
if (init == BooleanMix::Mixed) {
|
|
|
|
|
return init;
|
|
|
|
|
}
|
|
|
|
|
/* Alternatively, this could use #materialize to retrieve many values at once. */
|
2024-06-14 18:44:03 -04:00
|
|
|
const bool compare = (init == BooleanMix::None) ? varray[range.first()] :
|
|
|
|
|
(init == BooleanMix::AllTrue);
|
|
|
|
|
if (all_equal(varray, range, compare)) {
|
|
|
|
|
return compare ? BooleanMix::AllTrue : BooleanMix::AllFalse;
|
2023-05-24 09:54:25 -04:00
|
|
|
}
|
2024-06-14 18:44:03 -04:00
|
|
|
return BooleanMix::Mixed;
|
2023-05-24 09:54:25 -04:00
|
|
|
},
|
|
|
|
|
[&](BooleanMix a, BooleanMix b) { return (a == b) ? a : BooleanMix::Mixed; });
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 10:38:12 +01:00
|
|
|
int64_t count_booleans(const VArray<bool> &varray, const IndexMask &mask)
|
2023-10-17 18:29:17 +02:00
|
|
|
{
|
2023-12-08 10:38:12 +01:00
|
|
|
if (varray.is_empty() || mask.is_empty()) {
|
2023-10-17 18:29:17 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2023-12-08 10:38:12 +01:00
|
|
|
/* Check if mask is full. */
|
|
|
|
|
if (varray.size() == mask.size()) {
|
|
|
|
|
const CommonVArrayInfo info = varray.common_info();
|
|
|
|
|
if (info.type == CommonVArrayInfo::Type::Single) {
|
|
|
|
|
return *static_cast<const bool *>(info.data) ? varray.size() : 0;
|
|
|
|
|
}
|
|
|
|
|
if (info.type == CommonVArrayInfo::Type::Span) {
|
|
|
|
|
const Span<bool> span(static_cast<const bool *>(info.data), varray.size());
|
|
|
|
|
return threading::parallel_reduce(
|
|
|
|
|
varray.index_range(),
|
|
|
|
|
4096,
|
|
|
|
|
0,
|
|
|
|
|
[&](const IndexRange range, const int64_t init) {
|
|
|
|
|
const Span<bool> slice = span.slice(range);
|
|
|
|
|
return init + std::count(slice.begin(), slice.end(), true);
|
|
|
|
|
},
|
2025-02-09 19:05:01 +01:00
|
|
|
std::plus<>());
|
2023-12-08 10:38:12 +01:00
|
|
|
}
|
2023-10-17 18:29:17 +02:00
|
|
|
return threading::parallel_reduce(
|
|
|
|
|
varray.index_range(),
|
2023-12-08 10:38:12 +01:00
|
|
|
2048,
|
2023-10-17 18:29:17 +02:00
|
|
|
0,
|
|
|
|
|
[&](const IndexRange range, const int64_t init) {
|
2023-12-08 10:38:12 +01:00
|
|
|
int64_t value = init;
|
|
|
|
|
/* Alternatively, this could use #materialize to retrieve many values at once. */
|
|
|
|
|
for (const int64_t i : range) {
|
|
|
|
|
value += int64_t(varray[i]);
|
|
|
|
|
}
|
|
|
|
|
return value;
|
2023-10-17 18:29:17 +02:00
|
|
|
},
|
2025-02-09 19:05:01 +01:00
|
|
|
std::plus<>());
|
2023-10-17 18:29:17 +02:00
|
|
|
}
|
2023-12-08 10:38:12 +01:00
|
|
|
const CommonVArrayInfo info = varray.common_info();
|
|
|
|
|
if (info.type == CommonVArrayInfo::Type::Single) {
|
|
|
|
|
return *static_cast<const bool *>(info.data) ? mask.size() : 0;
|
|
|
|
|
}
|
|
|
|
|
int64_t value = 0;
|
|
|
|
|
mask.foreach_segment([&](const IndexMaskSegment segment) {
|
|
|
|
|
for (const int64_t i : segment) {
|
|
|
|
|
value += int64_t(varray[i]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 18:22:36 +02:00
|
|
|
bool contains(const VArray<bool> &varray, const IndexMask &indices_to_check, const bool value)
|
|
|
|
|
{
|
|
|
|
|
const CommonVArrayInfo info = varray.common_info();
|
|
|
|
|
if (info.type == CommonVArrayInfo::Type::Single) {
|
|
|
|
|
return *static_cast<const bool *>(info.data) == value;
|
|
|
|
|
}
|
|
|
|
|
if (info.type == CommonVArrayInfo::Type::Span) {
|
|
|
|
|
const Span<bool> span(static_cast<const bool *>(info.data), varray.size());
|
|
|
|
|
return threading::parallel_reduce(
|
|
|
|
|
indices_to_check.index_range(),
|
|
|
|
|
4096,
|
|
|
|
|
false,
|
|
|
|
|
[&](const IndexRange range, const bool init) {
|
|
|
|
|
if (init) {
|
|
|
|
|
return init;
|
|
|
|
|
}
|
|
|
|
|
const IndexMask sliced_mask = indices_to_check.slice(range);
|
|
|
|
|
if (std::optional<IndexRange> range = sliced_mask.to_range()) {
|
|
|
|
|
return span.slice(*range).contains(value);
|
|
|
|
|
}
|
|
|
|
|
for (const int64_t segment_i : IndexRange(sliced_mask.segments_num())) {
|
|
|
|
|
const IndexMaskSegment segment = sliced_mask.segment(segment_i);
|
|
|
|
|
for (const int i : segment) {
|
|
|
|
|
if (span[i] == value) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
std::logical_or());
|
|
|
|
|
}
|
|
|
|
|
return threading::parallel_reduce(
|
|
|
|
|
indices_to_check.index_range(),
|
|
|
|
|
2048,
|
|
|
|
|
false,
|
|
|
|
|
[&](const IndexRange range, const bool init) {
|
|
|
|
|
if (init) {
|
|
|
|
|
return init;
|
|
|
|
|
}
|
|
|
|
|
constexpr int64_t MaxChunkSize = 512;
|
|
|
|
|
const int64_t slice_end = range.one_after_last();
|
|
|
|
|
for (int64_t start = range.start(); start < slice_end; start += MaxChunkSize) {
|
|
|
|
|
const int64_t end = std::min<int64_t>(start + MaxChunkSize, slice_end);
|
|
|
|
|
const int64_t size = end - start;
|
|
|
|
|
const IndexMask sliced_mask = indices_to_check.slice(start, size);
|
|
|
|
|
std::array<bool, MaxChunkSize> values;
|
|
|
|
|
auto values_end = values.begin() + size;
|
|
|
|
|
varray.materialize_compressed(sliced_mask, values);
|
|
|
|
|
if (std::find(values.begin(), values_end, value) != values_end) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
std::logical_or());
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 10:38:12 +01:00
|
|
|
int64_t count_booleans(const VArray<bool> &varray)
|
|
|
|
|
{
|
|
|
|
|
return count_booleans(varray, IndexMask(varray.size()));
|
2023-10-17 18:29:17 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-12 14:30:34 +01:00
|
|
|
bool indices_are_range(Span<int> indices, IndexRange range)
|
|
|
|
|
{
|
|
|
|
|
if (indices.size() != range.size()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return threading::parallel_reduce(
|
|
|
|
|
range.index_range(),
|
|
|
|
|
4096,
|
|
|
|
|
true,
|
|
|
|
|
[&](const IndexRange part, const bool is_range) {
|
|
|
|
|
const Span<int> local_indices = indices.slice(part);
|
|
|
|
|
const IndexRange local_range = range.slice(part);
|
|
|
|
|
return is_range &&
|
|
|
|
|
std::equal(local_indices.begin(), local_indices.end(), local_range.begin());
|
|
|
|
|
},
|
2025-02-09 19:05:01 +01:00
|
|
|
std::logical_and<>());
|
2024-01-12 14:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-13 11:36:14 -05:00
|
|
|
} // namespace blender::array_utils
|