1 #ifndef teca_thread_pool_h
2 #define teca_thread_pool_h
5 #include "teca_algorithm.h"
7 #include "teca_threadsafe_queue.h"
17 #if defined(_GNU_SOURCE)
23 template <
typename task_t,
typename data_t>
26 template <
typename task_t,
typename data_t>
27 using p_teca_thread_pool = std::shared_ptr<teca_thread_pool<task_t, data_t>>;
30 template <
typename task_t,
typename data_t>
57 void push_task(task_t &task);
62 template <template <typename ... > class container_t, typename ... args>
63 void wait_all(container_t<data_t, args ...> &data);
73 template <template <typename ... > class container_t, typename ... args>
74 int wait_some(
long n_to_wait,
long long poll_interval,
75 container_t<data_t, args ...> &data);
78 unsigned int size() const noexcept
79 {
return m_threads.size(); }
83 void create_threads(MPI_Comm comm,
int n_threads,
bool bind,
bool verbose);
86 std::atomic<bool> m_live;
89 std::vector<std::future<data_t>>
92 std::vector<std::thread> m_threads;
96 template <
typename task_t,
typename data_t>
98 bool bind,
bool verbose) : m_live(true)
100 this->create_threads(comm, n, bind, verbose);
104 template <
typename task_t,
typename data_t>
106 int n_requested,
bool bind,
bool verbose)
109 if (comm == MPI_COMM_NULL)
112 int n_threads = n_requested;
114 std::deque<int> core_ids;
117 n_requested, bind, verbose, n_threads, core_ids))
120 " Falling back to 1 thread, affinity disabled.")
127 for (
int i = 0; i < n_threads; ++i)
129 m_threads.push_back(std::thread([
this]()
132 while (m_live.load())
135 if (m_queue.try_pop(task))
138 std::this_thread::yield();
141 #if defined(_GNU_SOURCE)
145 int core_id = core_ids.front();
146 core_ids.pop_front();
149 CPU_ZERO(&core_mask);
150 CPU_SET(core_id, &core_mask);
152 if (pthread_setaffinity_np(m_threads[i].native_handle(),
153 sizeof(cpu_set_t), &core_mask))
163 template <
typename task_t,
typename data_t>
167 std::for_each(m_threads.begin(), m_threads.end(),
168 [](std::thread &t) { t.join(); });
172 template <
typename task_t,
typename data_t>
175 m_futures.push_back(task.get_future());
176 m_queue.push(std::move(task));
180 template <
typename task_t,
typename data_t>
181 template <
template <
typename ... >
class container_t, typename ... args>
183 long long poll_interval, container_t<data_t, args ...> &data)
185 long n_tasks = m_futures.size();
190 this->wait_all(data);
194 else if (n_to_wait > n_tasks)
202 auto it = m_futures.begin();
203 while (it != m_futures.end())
205 std::future_status stat = it->wait_for(std::chrono::seconds::zero());
206 if (stat == std::future_status::ready)
208 data.push_back(it->get());
209 it = m_futures.erase(it);
219 if (data.size() <
static_cast<unsigned int>(n_to_wait))
220 std::this_thread::sleep_for(std::chrono::nanoseconds(poll_interval));
226 return m_futures.size();
230 template <
typename task_t,
typename data_t>
231 template <
template <
typename ... >
class container_t, typename ... args>
232 void
teca_thread_pool<task_t, data_t>::wait_all(container_t<data_t, args ...> &data)
236 std::for_each(m_futures.begin(), m_futures.end(),
237 [&data] (std::future<data_t> &f)
239 data.push_back(f.get());