PolyFEM
Loading...
Searching...
No Matches
par_for.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <thread>
5
6#include <Eigen/Core>
7
8#ifdef POLYFEM_WITH_TBB
9#include <tbb/global_control.h>
10#endif
11
12namespace polyfem
13{
14 namespace utils
15 {
16 class NThread
17 {
18 public:
19 static NThread &get()
20 {
21 static NThread instance;
22 return instance;
23 }
24
25 inline size_t num_threads() const { return num_threads_; }
26
27 void set_num_threads(const int max_threads)
28 {
29 const unsigned int tmp = max_threads <= 0 ? std::numeric_limits<int>::max() : max_threads;
30 const unsigned int num_threads = std::min(tmp, std::thread::hardware_concurrency());
31
33#ifdef POLYFEM_WITH_TBB
34 thread_limiter = std::make_shared<tbb::global_control>(tbb::global_control::max_allowed_parallelism, num_threads);
35#endif
36 Eigen::setNbThreads(num_threads);
37 }
38
39 private:
41
43
44#ifdef POLYFEM_WITH_TBB
46 std::shared_ptr<tbb::global_control> thread_limiter;
47#endif
48 };
49
50 void par_for(const int size, const std::function<void(int, int, int)> &func);
51 inline size_t get_n_threads() { return NThread::get().num_threads(); }
52 } // namespace utils
53} // namespace polyfem
static NThread & get()
Definition par_for.hpp:19
size_t num_threads() const
Definition par_for.hpp:25
void set_num_threads(const int max_threads)
Definition par_for.hpp:27
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