1 #ifndef teca_cpu_thread_pool_h
2 #define teca_cpu_thread_pool_h
4 #include "teca_config.h"
6 #include "teca_algorithm.h"
8 #include "teca_threadsafe_queue.h"
18 #if defined(_GNU_SOURCE)
24 template <
typename task_t,
typename data_t>
27 template <
typename task_t,
typename data_t>
28 using p_teca_cpu_thread_pool = std::shared_ptr<teca_cpu_thread_pool<task_t, data_t>>;
31 template <
typename task_t,
typename data_t>
60 void push_task(task_t &task);
65 template <template <typename ... > class container_t, typename ... args>
66 void wait_all(container_t<data_t, args ...> &data);
77 template <template <typename ... > class container_t, typename ... args>
78 int wait_some(
long n_to_wait,
long long poll_interval,
79 container_t<data_t, args ...> &data);
82 unsigned int size() const noexcept
83 {
return m_threads.
size(); }
87 void create_threads(MPI_Comm comm,
int n_threads,
bool bind,
bool verbose);
90 std::atomic<bool> m_live;
92 std::vector<std::future<data_t>> m_futures;
93 std::vector<std::thread> m_threads;
97 template <
typename task_t,
typename data_t>
99 bool bind,
bool verbose) : m_live(true)
101 this->create_threads(comm, n, bind, verbose);
105 template <
typename task_t,
typename data_t>
107 int n_requested,
bool bind,
bool verbose)
109 #if defined(TECA_HAS_MPI)
111 if (comm == MPI_COMM_NULL)
115 int n_threads = n_requested;
117 std::deque<int> core_ids;
118 std::vector<int> device_ids;
121 n_requested, bind, -1, verbose, n_threads, core_ids,
125 " Falling back to 1 thread, affinity disabled.")
132 for (
int i = 0; i < n_threads; ++i)
134 m_threads.push_back(std::thread([
this]()
137 while (m_live.load())
140 if (m_queue.try_pop(task))
143 std::this_thread::yield();
146 #if defined(_GNU_SOURCE)
150 int core_id = core_ids.front();
151 core_ids.pop_front();
154 CPU_ZERO(&core_mask);
155 CPU_SET(core_id, &core_mask);
157 if (pthread_setaffinity_np(m_threads[i].native_handle(),
158 sizeof(cpu_set_t), &core_mask))
168 template <
typename task_t,
typename data_t>
172 std::for_each(m_threads.begin(), m_threads.end(),
173 [](std::thread &t) { t.join(); });
177 template <
typename task_t,
typename data_t>
180 m_futures.push_back(task.get_future());
181 m_queue.push(std::move(task));
185 template <
typename task_t,
typename data_t>
186 template <
template <
typename ... >
class container_t, typename ... args>
188 long long poll_interval, container_t<data_t, args ...> &data)
190 long n_tasks = m_futures.
size();
195 this->wait_all(data);
199 else if (n_to_wait > n_tasks)
207 auto it = m_futures.begin();
208 while (it != m_futures.end())
210 std::future_status stat = it->wait_for(std::chrono::seconds::zero());
211 if (stat == std::future_status::ready)
213 data.push_back(it->get());
214 it = m_futures.erase(it);
224 if (data.size() <
static_cast<unsigned int>(n_to_wait))
225 std::this_thread::sleep_for(std::chrono::nanoseconds(poll_interval));
231 return m_futures.size();
235 template <
typename task_t,
typename data_t>
236 template <
template <
typename ... >
class container_t, typename ... args>
241 std::for_each(m_futures.begin(), m_futures.end(),
242 [&data] (std::future<data_t> &f)
244 data.push_back(f.get());