24namespace BitSetParallel
27template <
typename IndexType>
30 const size_t beginBlock =
bitRange.beg / BitSet::bits_per_block;
31 const size_t endBlock = ( size_t(
bitRange.end ) + BitSet::bits_per_block - 1 ) / BitSet::bits_per_block;
32 return tbb::blocked_range<size_t>( beginBlock, endBlock );
38 const size_t endBlock = ( bs.size() + BS::bits_per_block - 1 ) / BS::bits_per_block;
39 return tbb::blocked_range<size_t>( 0, endBlock );
48template <
typename IndexType>
53 .
beg = subRange.begin() > range.begin() ? IndexType( subRange.begin() * BitSet::bits_per_block ) :
bitRange.beg,
54 .end = subRange.end() < range.end() ? IndexType( subRange.end() * BitSet::bits_per_block ) :
bitRange.end
58template <
typename IndexType,
typename CM,
typename F>
62 tbb::parallel_for( range, [&](
const tbb::blocked_range<size_t> & subRange )
71template <
typename BS,
typename CM,
typename F>
72inline void ForAllRanged(
const BS & bs,
const CM & callMaker, F && f )
77template <
typename IndexType,
typename CM,
typename F>
86 auto callingThreadId = std::this_thread::get_id();
87 std::atomic<bool> keepGoing{
true };
91 constexpr int hardware_destructive_interference_size = 64;
92 struct alignas( hardware_destructive_interference_size ) S
94 std::atomic<size_t> processedBits{ 0 };
96 static_assert(
alignof( S ) == hardware_destructive_interference_size );
97 static_assert(
sizeof( S ) == hardware_destructive_interference_size );
100 tbb::parallel_for( range, [&] (
const tbb::blocked_range<size_t>& subRange )
103 size_t myProcessedBits = 0;
104 const bool report = std::this_thread::get_id() == callingThreadId;
105 auto c = callMaker();
108 if ( !keepGoing.load( std::memory_order_relaxed ) )
111 if ( ( ++myProcessedBits % reportProgressEveryBit ) == 0 )
115 if ( !progressCb(
float( myProcessedBits + s.processedBits.load( std::memory_order_relaxed ) ) /
bitRange.size() ) )
116 keepGoing.store(
false, std::memory_order_relaxed );
120 s.processedBits.fetch_add( myProcessedBits, std::memory_order_relaxed );
125 const auto total = s.processedBits.fetch_add( myProcessedBits, std::memory_order_relaxed );
126 if ( report && !progressCb(
float( total ) /
bitRange.size() ) )
127 keepGoing.store(
false, std::memory_order_relaxed );
129 return keepGoing.load( std::memory_order_relaxed );
132template <
typename BS,
typename CM,
typename F>
135 return ForAllRanged(
bitRange( bs ), callMaker, std::forward<F>( f ), progressCb, reportProgressEveryBit );
145template <
typename BS,
typename ...F>
157template <
typename BS,
typename L,
typename ...F>
167template <
typename BS,
typename F,
typename ...Cb>
178template <
typename BS,
typename L,
typename F,
typename ...Cb>
188template <
typename BS,
typename F,
typename ...Cb>
191 return BitSetParallelForAll( bs, [&](
auto bit ) {
if ( bs.test( bit ) ) f( bit ); }, std::forward<Cb>( cb )... );
199template <
typename BS,
typename L,
typename F,
typename ...Cb>
200inline auto BitSetParallelFor(
const BS& bs, tbb::enumerable_thread_specific<L> & e, F && f, Cb&&... cb )
202 return BitSetParallelForAll( bs, e, [&](
auto bit,
auto & tls ) {
if ( bs.test( bit ) ) f( bit, tls ); }, std::forward<Cb>( cb )... );
Definition MRMesh/MRId.h:13
auto BitSetParallelForAll(const BS &bs, F &&f, Cb &&... cb)
Definition MRBitSetParallelFor.h:168
auto BitSetParallelForAllRanged(const BS &bs, F &&... f)
Definition MRBitSetParallelFor.h:146
auto BitSetParallelFor(const BS &bs, F &&f, Cb &&... cb)
Definition MRBitSetParallelFor.h:189
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:576
void ForAllRanged(const IdRange< IndexType > &bitRange, const CM &callMaker, F &&f)
Definition MRBitSetParallelFor.h:59
auto bitRange(const BS &bs)
Definition MRBitSetParallelFor.h:43
auto blockRange(const IdRange< IndexType > &bitRange)
Definition MRBitSetParallelFor.h:28
auto bitSubRange(const IdRange< IndexType > &bitRange, const tbb::blocked_range< size_t > &range, const tbb::blocked_range< size_t > &subRange)
Definition MRBitSetParallelFor.h:49
Definition MRCameraOrientationPlugin.h:8
range of indices [beg, end)
Definition MRBitSetParallelFor.h:19
auto size() const
Definition MRBitSetParallelFor.h:21
Id beg
Definition MRBitSetParallelFor.h:20
Id end
Definition MRBitSetParallelFor.h:20
Definition MRParallel.h:18
Definition MRParallel.h:32