MeshLib C++ Docs
Loading...
Searching...
No Matches
MRParallelFor.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRFunctional.h"
4#include "MRParallel.h"
6#include "MRTbbThreadMutex.h"
7#include "MRVector.h"
8
9#include <atomic>
10
11namespace MR
12{
13
16
17MRMESH_API void parallelFor( size_t begin, size_t end, FunctionRef<void ( size_t )> f );
18
19MRMESH_API void parallelFor( size_t begin, size_t end, FunctionRef<void ( size_t, void* )> f, FunctionRef<void* ()> ctx );
20
21MRMESH_API bool parallelFor( size_t begin, size_t end, FunctionRef<void ( size_t, void* )> f, FunctionRef<void* ()> ctx,
22 ProgressCallback cb, size_t reportProgressEvery = 1024 );
23
27template <typename I, typename F, typename ...Cb>
28inline auto ParallelFor( I begin, I end, F && f, Cb && ... cb )
29{
30 if constexpr ( sizeof...( cb ) == 0 )
31 {
32 return parallelFor( begin, end, [&] ( size_t i )
33 {
34 std::forward<F>( f )( I( i ) );
35 } );
36 }
37 else
38 {
39 return parallelFor( begin, end, [&] ( size_t i, void* )
40 {
41 std::forward<F>( f )( I( i ) );
42 }, [&]
43 {
44 return nullptr;
45 }, std::forward<Cb>( cb )... );
46 }
47}
48
53template <typename I, typename L, typename F, typename ...Cb>
54inline auto ParallelFor( I begin, I end, tbb::enumerable_thread_specific<L> & e, F && f, Cb && ... cb )
55{
56 return parallelFor( begin, end, [&] ( size_t i, void* ctx )
57 {
58 std::forward<F>( f )( I( i ), *(L*)ctx );
59 }, [&]
60 {
61 return &e.local();
62 }, std::forward<Cb>( cb )... );
63}
64
68template <typename T, typename ...F>
69inline auto ParallelFor( const std::vector<T> & v, F &&... f )
70{
71 return ParallelFor( size_t(0), v.size(), std::forward<F>( f )... );
72}
73
77template <typename T, typename I, typename ...F>
78inline auto ParallelFor( const Vector<T, I> & v, F &&... f )
79{
80 return ParallelFor( v.beginId(), v.endId(), std::forward<F>( f )... );
81}
82
86template <typename T, typename I, typename ...F>
87inline auto ParallelFor( const Buffer<T, I> & buf, F &&... f )
88{
89 return ParallelFor( buf.beginId(), buf.endId(), std::forward<F>( f )... );
90}
91
93
94}
std::vector<V>-like container that is 1) resized without initialization of its elements,...
Definition MRBuffer.h:58
Definition MRFunctional.h:15
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:23
MRMESH_API void parallelFor(size_t begin, size_t end, FunctionRef< void(size_t)> f)
MR_BIND_IGNORE_PY auto end(const BitSet &)
Definition MRBitSet.h:397
MR_BIND_IGNORE_PY auto begin(const BitSet &a)
Definition MRBitSet.h:395
auto ParallelFor(I begin, I end, F &&f, Cb &&... cb)
Definition MRParallelFor.h:28
I beginId() const
returns the identifier of the first element
Definition MRBuffer.h:105
I beginId() const
returns the identifier of the first element
Definition MRVector.h:145
I endId() const
returns backId() + 1
Definition MRBuffer.h:111
I endId() const
returns backId() + 1
Definition MRVector.h:151
only for bindings generation
Definition MRCameraOrientationPlugin.h:8