Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
thread_pool.hpp File Reference

Thread-safe queue and worker thread pool for MPI+thread hybrid parallelism. More...

#include <condition_variable>
#include <cstdlib>
#include <deque>
#include <functional>
#include <mutex>
#include <optional>
#include <thread>
#include <type_traits>
#include <variant>
#include <vector>
Include dependency graph for thread_pool.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  bertini::parallel::ThreadSafeQueue< T >
 A simple thread-safe (mutex + condition-variable) FIFO queue. More...
struct  bertini::parallel::PoolShutdownSentinel
class  bertini::parallel::WorkerThreadPool< TaskT, ResultT, StateFactory, TrackFn >
 Thread pool for parallel path tracking within a single MPI worker rank. More...

Namespaces

namespace  bertini
namespace  bertini::parallel

Functions

unsigned bertini::parallel::AvailableCpuCount ()
 The number of CPUs this process may run on.
unsigned bertini::parallel::EffectiveThreadCount (unsigned configured=0)
 Resolve a configured thread count to the number of worker threads to actually spawn.

Detailed Description

Thread-safe queue and worker thread pool for MPI+thread hybrid parallelism.

ThreadSafeQueue<T>: blocking FIFO backed by std::deque + std::mutex + std::condition_variable.

WorkerThreadPool<TaskT, ResultT, StateFactory, TrackFn>: spawns N tracking threads. Each thread calls StateFactory() once at startup to build its own self-contained tracking state (typically a struct holding a System copy and a Tracker copy that references that System). The MPI communication thread submits tasks via submit() and collects results via collect(). All MPI calls remain on the main thread (compatible with MPI_THREAD_FUNNELED).

The StateFactory-based design avoids the "tracker holds a reference to an external System" problem: the factory returns a heap-owned state struct (unique_ptr), so the System copy and the Tracker copy that references it live together — at a stable address — for the lifetime of the thread.

Example: struct TrackState { System sys; AMPTracker tracker; }; auto factory = [&]() { auto s = std::unique_ptr<TrackState>(new TrackState{ base_system, base_tracker }); s->tracker.SetSystem(s->sys); // repoint the copy at its own System SetThreadPrecision(DefaultPrecision()); return s; }; auto track_fn = [&](std::unique_ptr<TrackState>& state, SolnIndT const& idx) -> BeforeResult { ... track using state->tracker ... }; WorkerThreadPool<SolnIndT, BeforeResult, decltype(factory), decltype(track_fn)> pool(n_threads, factory, track_fn);