Persistent pthread thread pool for CK-Engine inference. More...
#include "ck_threadpool.h"#include <stdlib.h>#include <string.h>#include <stdio.h>#include <errno.h>Go to the source code of this file.
Macros | |
| #define | CK_SPIN_PAUSE() ((void)0) |
Functions | |
| static void | barrier_init (ck_barrier_t *b, int n_threads) |
| static void | barrier_wait (ck_barrier_t *b) |
| int | ck_get_num_threads (void) |
| int | ck_get_physical_cores (void) |
| void | ck_threadpool_barrier (ck_threadpool_t *pool) |
| ck_threadpool_t * | ck_threadpool_create (int n_threads) |
| void | ck_threadpool_destroy (ck_threadpool_t *pool) |
| void | ck_threadpool_dispatch (ck_threadpool_t *pool, ck_work_fn_t fn, void *args) |
| ck_threadpool_t * | ck_threadpool_global (void) |
| void | ck_threadpool_global_destroy (void) |
| int | ck_threadpool_n_threads (const ck_threadpool_t *pool) |
| void | ck_threadpool_pause (ck_threadpool_t *pool) |
| void | ck_threadpool_resume (ck_threadpool_t *pool) |
| int | ck_threadpool_thread_id (const ck_threadpool_t *pool) |
| static void | global_pool_init (void) |
| static void * | worker_main (void *arg) |
Variables | |
| static ck_threadpool_t * | g_threadpool = NULL |
| static pthread_once_t | g_threadpool_once = PTHREAD_ONCE_INIT |
Persistent pthread thread pool for CK-Engine inference.
Architecture:
Based on the ggml_threadpool design from llama.cpp, adapted for CK-Engine's kernel dispatch model.
Definition in file ck_threadpool.c.
| #define CK_SPIN_PAUSE | ( | ) | ((void)0) |
Definition at line 27 of file ck_threadpool.c.
|
static |
Definition at line 78 of file ck_threadpool.c.
Referenced by ck_threadpool_create(), and ck_threadpool_dispatch().
|
static |
Spin-wait barrier. All threads must call this. Uses phase counter to allow re-use without reset.
Definition at line 89 of file ck_threadpool.c.
References CK_SPIN_PAUSE, and CK_THREADPOOL_SPIN_COUNT.
Referenced by ck_threadpool_barrier().
| int ck_get_num_threads | ( | void | ) |
| int ck_get_physical_cores | ( | void | ) |
Definition at line 62 of file ckernel_strict.c.
Referenced by ck_set_num_threads(), and ck_threadpool_create().
| void ck_threadpool_barrier | ( | ck_threadpool_t * | pool | ) |
Barrier synchronization within a dispatched work function.
ALL threads must call this at the same point. Threads spin-wait until all have arrived, then proceed.
Must only be called from within a work function (during dispatch).
| pool | Thread pool |
Definition at line 320 of file ck_threadpool.c.
References barrier_wait().
| ck_threadpool_t* ck_threadpool_create | ( | int | n_threads | ) |
Create a thread pool with n_threads total threads. Thread 0 is the calling (main) thread; n_threads-1 workers are spawned.
| n_threads | Total thread count (including main). Must be >= 1. Pass 0 for auto-detect (physical cores). |
Definition at line 183 of file ck_threadpool.c.
References barrier_init(), CK_CACHE_LINE, ck_get_physical_cores(), CK_THREADPOOL_MAX_THREADS, and worker_main().
Referenced by global_pool_init().
| void ck_threadpool_destroy | ( | ck_threadpool_t * | pool | ) |
Destroy the thread pool. Signals all workers to exit and joins them. Safe to call with NULL.
Definition at line 243 of file ck_threadpool.c.
Referenced by ck_threadpool_global_destroy().
| void ck_threadpool_dispatch | ( | ck_threadpool_t * | pool, |
| ck_work_fn_t | fn, | ||
| void * | args | ||
| ) |
Dispatch work to all threads and wait for completion.
This is a blocking call — returns when ALL threads have finished.
| pool | Thread pool |
| fn | Work function (called on each thread) |
| args | Argument passed to fn |
Definition at line 271 of file ck_threadpool.c.
References barrier_init(), CK_SPIN_PAUSE, and CK_THREADPOOL_SPIN_COUNT.
| ck_threadpool_t* ck_threadpool_global | ( | void | ) |
Get or create the global thread pool. Thread-safe (uses pthread_once internally). Uses ck_get_num_threads() for auto-detection.
Definition at line 383 of file ck_threadpool.c.
References g_threadpool, g_threadpool_once, and global_pool_init().
Referenced by ck_get_threadpool(), and ck_threadpool_init().
| void ck_threadpool_global_destroy | ( | void | ) |
Destroy the global thread pool. Called during engine shutdown.
Definition at line 389 of file ck_threadpool.c.
References ck_threadpool_destroy(), g_threadpool, and g_threadpool_once.
Referenced by ck_threadpool_shutdown().
| int ck_threadpool_n_threads | ( | const ck_threadpool_t * | pool | ) |
Get total thread count (including main thread)
Definition at line 351 of file ck_threadpool.c.
| void ck_threadpool_pause | ( | ck_threadpool_t * | pool | ) |
Pause workers — they sleep on condvar (0% CPU). Call between batches or during interactive waiting. Workers wake on next dispatch or resume.
Definition at line 330 of file ck_threadpool.c.
| void ck_threadpool_resume | ( | ck_threadpool_t * | pool | ) |
Resume workers — transition from sleep to spin-wait. Call before starting a new batch of work.
Definition at line 336 of file ck_threadpool.c.
| int ck_threadpool_thread_id | ( | const ck_threadpool_t * | pool | ) |
Get thread index for current thread (0 = main, -1 if not in pool)
Definition at line 356 of file ck_threadpool.c.
|
static |
Definition at line 377 of file ck_threadpool.c.
References ck_get_num_threads(), ck_threadpool_create(), and g_threadpool.
Referenced by ck_threadpool_global().
|
static |
Definition at line 117 of file ck_threadpool.c.
References CK_SPIN_PAUSE, and CK_THREADPOOL_SPIN_COUNT.
Referenced by ck_threadpool_create().
|
static |
Definition at line 372 of file ck_threadpool.c.
Referenced by ck_threadpool_global(), ck_threadpool_global_destroy(), and global_pool_init().
|
static |
Definition at line 373 of file ck_threadpool.c.
Referenced by ck_threadpool_global(), and ck_threadpool_global_destroy().