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 )> f, tbb::task_group_context& tgc );
20
21MRMESH_API void parallelFor( size_t begin, size_t end, FunctionRef<void ( size_t, void* )> f, FunctionRef<void* ()> ctx );
22
23MRMESH_API bool parallelFor( size_t begin, size_t end, FunctionRef<void ( size_t, void* )> f, FunctionRef<void* ()> ctx,
24 ProgressCallback cb, size_t reportProgressEvery = 1024 );
25
29template <typename I, typename F, typename ...Cb>
30inline auto ParallelFor( I begin, I end, F && f, Cb && ... cb )
31{
32 if constexpr ( sizeof...( cb ) == 0 )
33 {
34 return parallelFor( begin, end, [&] ( size_t i )
35 {
36 std::forward<F>( f )( I( i ) );
37 } );
38 }
39 else
40 {
41 return parallelFor( begin, end, [&] ( size_t i, void* )
42 {
43 std::forward<F>( f )( I( i ) );
44 }, [&]
45 {
46 return nullptr;
47 }, std::forward<Cb>( cb )... );
48 }
49}
50
53template <typename I, typename F>
54inline void ParallelFor( I begin, I end, F && f, tbb::task_group_context& tgc )
55{
56 parallelFor( begin, end, [&] ( size_t i )
57 {
58 std::forward<F>( f )( I( i ) );
59 }, tgc );
60}
61
66template <typename I, typename L, typename F, typename ...Cb>
67inline auto ParallelFor( I begin, I end, tbb::enumerable_thread_specific<L> & e, F && f, Cb && ... cb )
68{
69 return parallelFor( begin, end, [&] ( size_t i, void* ctx )
70 {
71 std::forward<F>( f )( I( i ), *(L*)ctx );
72 }, [&]
73 {
74 return &e.local();
75 }, std::forward<Cb>( cb )... );
76}
77
81template <typename T, typename ...F>
82inline auto ParallelFor( const std::vector<T> & v, F &&... f )
83{
84 return ParallelFor( size_t(0), v.size(), std::forward<F>( f )... );
85}
86
90template <typename T, typename I, typename ...F>
91inline auto ParallelFor( const Vector<T, I> & v, F &&... f )
92{
93 return ParallelFor( v.beginId(), v.endId(), std::forward<F>( f )... );
94}
95
99template <typename T, typename I, typename ...F>
100inline auto ParallelFor( const Buffer<T, I> & buf, F &&... f )
101{
102 return ParallelFor( buf.beginId(), buf.endId(), std::forward<F>( f )... );
103}
104
106
107}
#define MRMESH_API
Definition MRMeshFwd.h:80
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
void parallelFor(size_t begin, size_t end, FunctionRef< void(size_t)> f)
auto ParallelFor(I begin, I end, F &&f, Cb &&... cb)
Definition MRParallelFor.h:30
std::function< bool(float)> ProgressCallback
Definition MRMeshFwd.h:758
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
class MRMESH_CLASS I
Definition MRMeshFwd.h:139
I endId() const
returns backId() + 1
Definition MRBuffer.h:111
I endId() const
returns backId() + 1
Definition MRVector.h:151
auto begin(ViewportMask mask)
Definition MRViewportId.h:122
auto end(ViewportMask)
Definition MRViewportId.h:124
only for bindings generation
Definition MRCameraOrientationPlugin.h:8