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