PolyFEM
Loading...
Searching...
No Matches
MaybeParallelFor.tpp
Go to the documentation of this file.
1#include <polyfem/utils/MaybeParallelFor.hpp>
2
3#if defined(POLYFEM_WITH_TBB)
4#include <tbb/parallel_for.h>
5#include <tbb/parallel_reduce.h>
6#include <tbb/enumerable_thread_specific.h>
7#elif defined(POLYFEM_WITH_CPP_THREADS)
8#include <polyfem/utils/par_for.hpp>
9#include <execution>
10#else
11// Not using parallel for
12#endif
13
14namespace polyfem
15{
16 namespace utils
17 {
18 inline void maybe_parallel_for(int size, const std::function<void(int, int, int)> &partial_for)
19 {
20#if defined(POLYFEM_WITH_CPP_THREADS)
21 par_for(size, partial_for);
22#elif defined(POLYFEM_WITH_TBB)
23 tbb::parallel_for(tbb::blocked_range<int>(0, size), [&](const tbb::blocked_range<int> &r) {
24 partial_for(r.begin(), r.end(), tbb::this_task_arena::current_thread_index());
25 });
26#else
27 partial_for(0, size, /*thread_id=*/0); // actually the full for loop
28#endif
29 }
30
31 inline void maybe_parallel_for(int size, const std::function<void(int)> &body)
32 {
33#if defined(POLYFEM_WITH_CPP_THREADS)
34 for (int i = 0; i < size; ++i)
35 body(i);
36#elif defined(POLYFEM_WITH_TBB)
37 tbb::parallel_for(0, size, body);
38#else
39 for (int i = 0; i < size; ++i)
40 body(i);
41#endif
42 }
43
44 template <typename LocalStorage>
45 inline auto create_thread_storage(const LocalStorage &initial_local_storage)
46 {
47#if defined(POLYFEM_WITH_CPP_THREADS)
48 return std::vector<LocalStorage>(get_n_threads(), initial_local_storage);
49#elif defined(POLYFEM_WITH_TBB)
50 return tbb::enumerable_thread_specific<LocalStorage>(initial_local_storage);
51#else
52 return std::array<LocalStorage, 1>{{initial_local_storage}};
53#endif
54 }
55
56 template <typename Storages>
57 inline auto &get_local_thread_storage(Storages &storage, int thread_id)
58 {
59#if defined(POLYFEM_WITH_CPP_THREADS)
60 return storage[thread_id];
61#elif defined(POLYFEM_WITH_TBB)
62 return storage.local();
63#else
64 assert(thread_id == 0);
65 assert(storage.size() == 1);
66 return storage[0];
67#endif
68 }
69 } // namespace utils
70} // namespace polyfem