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"
9 #include "teca_owned_future.h"
19 #if defined(_GNU_SOURCE)
25 template <
typename task_t,
typename data_t>
28 template <
typename task_t,
typename data_t>
29 using p_teca_cpu_thread_pool = std::shared_ptr<teca_cpu_thread_pool<task_t, data_t>>;
32 template <
typename task_t,
typename data_t>
61 void push_task(task_t &task);
66 template <
template <
typename ... >
class container_t, typename ... args>
67 void wait_all(container_t<data_t, args ...> &
data);
78 template <
template <
typename ... >
class container_t, typename ... args>
79 int wait_some(long n_to_wait, long long poll_interval,
80 container_t<data_t, args ...> &
data);
83 unsigned int size() const noexcept
84 {
return m_threads.size(); }
88 void create_threads(MPI_Comm comm,
int n_threads,
bool bind,
bool verbose);
93 std::atomic<bool> m_live;
95 std::vector<owned_future<data_t>> m_futures;
96 std::vector<std::thread> m_threads;
100 template <
typename task_t,
typename data_t>
102 bool bind,
bool verbose) : m_live(true)
104 this->create_threads(comm, n, bind, verbose);
108 template <
typename task_t,
typename data_t>
110 int n_requested,
bool bind,
bool verbose)
112 #if defined(TECA_HAS_MPI)
114 if (comm == MPI_COMM_NULL)
120 int n_threads = n_requested;
122 std::deque<int> core_ids;
123 std::vector<int> device_ids;
126 n_requested, -1, -1, bind, verbose, n_threads, core_ids,
130 " Falling back to 1 thread, affinity disabled.")
137 for (
int i = 0; i < n_threads; ++i)
139 m_threads.push_back(std::thread([
this]()
142 while (m_live.load())
145 if (m_queue.try_pop(task))
148 std::this_thread::yield();
151 #if defined(_GNU_SOURCE)
155 int core_id = core_ids.front();
156 core_ids.pop_front();
159 CPU_ZERO(&core_mask);
160 CPU_SET(core_id, &core_mask);
162 if (pthread_setaffinity_np(m_threads[i].native_handle(),
163 sizeof(cpu_set_t), &core_mask))
173 template <
typename task_t,
typename data_t>
177 std::for_each(m_threads.begin(), m_threads.end(),
178 [](std::thread &t) { t.join(); });
182 template <
typename task_t,
typename data_t>
185 std::lock_guard<std::mutex> lock(m_mutex);
189 m_futures.emplace_back(
owned_future(task.get_future()));
190 m_queue.push(std::move(task));
194 template <
typename task_t,
typename data_t>
195 template <
template <
typename ... >
class container_t, typename ... args>
197 long long poll_interval, container_t<data_t, args ...> &data)
202 this->wait_all(
data);
207 size_t thread_valid = 1;
213 std::lock_guard<std::mutex> lock(m_mutex);
214 for (
auto it = m_futures.begin(); it != m_futures.end(); ++it)
216 if (it->owner() && it->m_future.valid())
218 if ((it->m_future.wait_for(std::chrono::seconds::zero())
219 == std::future_status::ready))
221 data.push_back(it->m_future.get());
233 if (
data.size() >=
static_cast<unsigned int>(n_to_wait))
238 std::this_thread::sleep_for(std::chrono::nanoseconds(poll_interval));
244 std::lock_guard<std::mutex> lock(m_mutex);
254 template <
typename task_t,
typename data_t>
255 template <
template <
typename ... >
class container_t, typename ... args>
259 std::lock_guard<std::mutex> lock(m_mutex);
261 std::for_each(m_futures.begin(), m_futures.end(),
266 data.push_back(f.m_future.get());
275 std::lock_guard<std::mutex> lock(m_mutex);
A class to manage a fixed size pool of threads that dispatch work.
Definition: teca_cpu_thread_pool.h:34
unsigned int size() const noexcept
get the number of threads
Definition: teca_cpu_thread_pool.h:83
void push_task(task_t &task)
Definition: teca_cpu_thread_pool.h:183
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
TECA_EXPORT int thread_parameters(MPI_Comm comm, int base_core_id, int n_requested, int n_threads_per_device, int n_ranks_per_device, bool bind, bool verbose, int &n_threads, std::deque< int > &affinity, std::vector< int > &device_ids)
auto data(V &&... args)
Definition: teca_variant_array_util.h:255
a future that is owned by a single thread
Definition: teca_owned_future.h:10
#define TECA_WARNING(_msg)
Constructs a warning message and sends it to the stderr stream.
Definition: teca_common.h:164