1#include <polyfem/utils/MaybeParallelFor.hpp>
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>
11// Not using parallel for
18 inline void maybe_parallel_for(int size, const std::function<void(int, int, int)> &partial_for)
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());
27 partial_for(0, size, /*thread_id=*/0); // actually the full for loop
31 inline void maybe_parallel_for(int size, const std::function<void(int)> &body)
33#if defined(POLYFEM_WITH_CPP_THREADS)
34 for (int i = 0; i < size; ++i)
36#elif defined(POLYFEM_WITH_TBB)
37 tbb::parallel_for(0, size, body);
39 for (int i = 0; i < size; ++i)
44 template <typename LocalStorage>
45 inline auto create_thread_storage(const LocalStorage &initial_local_storage)
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);
52 return std::array<LocalStorage, 1>{{initial_local_storage}};
56 template <typename Storages>
57 inline auto &get_local_thread_storage(Storages &storage, int thread_id)
59#if defined(POLYFEM_WITH_CPP_THREADS)
60 return storage[thread_id];
61#elif defined(POLYFEM_WITH_TBB)
62 return storage.local();
64 assert(thread_id == 0);
65 assert(storage.size() == 1);