Skip to content

File dp_parallel.h

FileList > doppler > dp_parallel.h

Go to the source code of this file

A minimal bounded parallel-for over an index range, and a persistent pool with the same contract. More...

  • #include "doppler/clib_common.h"
  • #include "doppler/dp_thread.h"
  • #include <stddef.h>
  • #include <stdlib.h>
  • #include <stdatomic.h>

Classes

Type Name
struct dp_pf_shared_t
One parallel-for run: the range, the work, and the shared cursor.
struct dp_pool
dp_pool_t's fields; all internal.

Public Types

Type Name
typedef struct dp_pool dp_pool_t
A persistent worker pool for repeated parallel-for runs.

Public Functions

Type Name
DP_THREAD_FN (dp_pf_worker, arg)
DP_THREAD_FN (dp_pool_worker, arg)

Public Static Functions

Type Name
void dp_parallel_for (size_t n, void(*)(size_t, void *) body, void * ctx, int max_threads)
Run body(i, ctx) for every i in [0, n) across up to max_threads workers.
dp_pool_t * dp_pool_create (int max_threads)
Create a pool of max_threads workers (the caller counts as one).
void dp_pool_destroy (dp_pool_t * p)
Stop and join the helpers, free the pool.
void dp_pool_run (dp_pool_t * p, size_t n, void(*)(size_t, void *) body, void * ctx)
Run body(i, ctx) for every i in [0, n) across the pool's threads.
int dp_pool_threads (const dp_pool_t * p)
Workers the pool runs work on, the caller included.

Detailed Description

doppler is single-threaded C by default; this is the one place that fans a genuinely independent, CPU-bound workload across cores (the per-source signal build in wfm_plan's prepare(); the searcher's roll per thread). The contract is deliberately narrow: run body(i, ctx) for every i in [0, n), possibly concurrently, and return only once all n have completed. body MUST be free of cross-i data races — it may read shared read-only state through ctx and must write only to per-i outputs (a distinct slot per i). Given that, the observable result is bit-identical to the plain serial loop no matter how the work is scheduled.

Portability: POSIX threads on the two supported platforms (linux, macos). Every path to not running parallel — a single online core, n <= 1, a forced-serial caller, or a malloc/thread-create failure — degrades to running the whole range on the calling thread, so the function always does all the work. Header-only (static) so the sole caller pulls it in with no separate translation unit; link Threads::Threads on that target.

Public Types Documentation

typedef dp_pool_t

A persistent worker pool for repeated parallel-for runs.

typedef struct dp_pool dp_pool_t;

Create with dp_pool_create(), run with dp_pool_run(), free with dp_pool_destroy(). All fields are internal.

From C++ the type is opaque and nothing else in this file is declared: the pool's run state is a C11 atomic_size_t, which C++ has only from C++23, and every function here is a static inline body. A component that carries a pool holds a dp_pool_t *, which means the same in both languages, so its own struct is laid out identically (doppler#3108).


Public Functions Documentation

function DP_THREAD_FN

DP_THREAD_FN (
    dp_pf_worker,
    arg
) 

A worker drains indices off the shared cursor until the range is exhausted. fetch-add is the whole synchronization story: each index is handed to exactly one worker, so there is no contention on the outputs.

Declared through DP_THREAD_FN, so it is also a thread entry point; the caller of dp_parallel_for() runs it directly as the last worker.


function DP_THREAD_FN

DP_THREAD_FN (
    dp_pool_worker,
    arg
) 

A pool helper's thread body: park on wake until dp_pool_run() bumps gen or dp_pool_destroy() sets stop, drain the run's cursor with dp_pf_worker(), decrement busy (signaling done at zero), repeat. Returns only on stop.


Public Static Functions Documentation

function dp_parallel_for

Run body(i, ctx) for every i in [0, n) across up to max_threads workers.

static inline void dp_parallel_for (
    size_t n,
    void(*)(size_t, void *) body,
    void * ctx,
    int max_threads
) 

max_threads <= 0 auto-selects the online core count; 1 forces serial. The calling thread is itself a worker, so total concurrency is max_threads (max_threads - 1 helpers spawned + the caller draining alongside them), capped at n. Whenever helpers cannot be spawned the caller alone drains the full range, so the range is always completed. Helpers are created and joined within this call.

Parameters:

  • n Number of indices; 0 returns at once.
  • body Per-index work. Each index is passed to exactly one call, in no guaranteed order, possibly concurrently.
  • ctx Opaque context passed to every body() call.
  • max_threads Total worker count including the caller; <= 0 means the online core count, 1 means serial.

function dp_pool_create

Create a pool of max_threads workers (the caller counts as one).

static inline dp_pool_t * dp_pool_create (
    int max_threads
) 

Spawns max_threads - 1 helper threads that park on a condition variable until dp_pool_run(). A helper that fails to start leaves a smaller pool; dp_pool_threads() reports the size actually achieved.

Parameters:

  • max_threads Total worker count including the caller; <= 0 auto-selects the online core count; 1 creates no helper.

Returns:

The pool, owned by the caller (free with dp_pool_destroy()). Never NULL: the pool's own allocations are fixed-size and abort on OOM.


function dp_pool_destroy

Stop and join the helpers, free the pool.

static inline void dp_pool_destroy (
    dp_pool_t * p
) 

Parameters:

  • p The pool, or NULL (a no-op). Must not be mid-run.

function dp_pool_run

Run body(i, ctx) for every i in [0, n) across the pool's threads.

static inline void dp_pool_run (
    dp_pool_t * p,
    size_t n,
    void(*)(size_t, void *) body,
    void * ctx
) 

Returns once all n have completed; dp_parallel_for()'s contract exactly. A NULL pool, a pool with no helpers, or n == 1 runs serially on the caller. Not reentrant: one run at a time, from one thread.

Parameters:

  • p The pool, or NULL for serial execution.
  • n Number of indices; 0 returns at once.
  • body Per-index work; each index goes to exactly one call.
  • ctx Opaque context passed to every body() call.

function dp_pool_threads

Workers the pool runs work on, the caller included.

static inline int dp_pool_threads (
    const dp_pool_t * p
) 

Parameters:

  • p The pool, or NULL.

Returns:

helpers + 1; 1 for a NULL pool.



The documentation for this class was generated from the following file native/inc/doppler/dp_parallel.h