PolyFEM
Loading...
Searching...
No Matches
MaybeParallelFor.hpp
Go to the documentation of this file.
1#pragma once
2
3#if defined(POLYFEM_WITH_TBB)
5#include <tbb/parallel_for.h>
6#include <tbb/parallel_reduce.h>
7#include <tbb/enumerable_thread_specific.h>
8#elif defined(POLYFEM_WITH_CPP_THREADS)
10#else
11// Not using parallel for
12#endif
13
14namespace polyfem
15{
16 namespace utils
17 {
18 // Perform a parallel (maybe) for loop.
19 // The parallel for used depends on the compile definitions.
20 // The overall for loop is from 0 up to `size` with an increment of 1.
21 inline void maybe_parallel_for(int size, const std::function<void(int, int, int)> &partial_for);
22 inline void maybe_parallel_for(int size, const std::function<void(int)> &body);
23
24 // Returns thread specific storage for further use in `maybe_parallel_for()`.
25 // The return type depends on the threading library used.
26 // TBB ⟹ `std::vector<LocalStorage>`
27 // C++ Threads ⟹ `tbb::enumerable_thread_specific<LocalStorage>`
28 // none ⟹ `std::array<LocalStorage, 1>`
29 template <typename LocalStorage>
30 inline auto create_thread_storage(const LocalStorage &initial_local_storage);
31
32 template <typename Storages>
33 inline auto &get_local_thread_storage(Storages &storage, int thread_id);
34 } // namespace utils
35} // namespace polyfem
36
37#include "MaybeParallelFor.tpp"
auto & get_local_thread_storage(Storages &storage, int thread_id)
auto create_thread_storage(const LocalStorage &initial_local_storage)
void maybe_parallel_for(int size, const std::function< void(int, int, int)> &partial_for)