PolyFEM
Loading...
Searching...
No Matches
par_for.cpp
Go to the documentation of this file.
1#include "par_for.hpp"
2
3#include <vector>
4#include <algorithm>
5
6namespace polyfem
7{
8 namespace utils
9 {
10 void par_for(const int size, const std::function<void(int, int, int)> &func)
11 {
12#ifdef POLYFEM_WITH_CPP_THREADS
13 const size_t n_threads = get_n_threads();
14 if (n_threads == 1)
15 func(0, size, /*thread_id=*/0); // actually the full for loop
16 else
17 {
18 std::vector<std::thread> threads(n_threads);
19
20 for (int t = 0; t < n_threads; t++)
21 {
22 threads[t] = std::thread(std::bind(
23 func,
24 t * size / n_threads,
25 (t + 1) == n_threads ? size : (t + 1) * size / n_threads,
26 t));
27 }
28 std::for_each(threads.begin(), threads.end(), [](std::thread &x) { x.join(); });
29 }
30#endif
31 }
32 } // namespace utils
33} // namespace polyfem
int x
void par_for(const int size, const std::function< void(int, int, int)> &func)
Definition par_for.cpp:10
size_t get_n_threads()
Definition par_for.hpp:51