diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-16 19:01:34 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-16 19:01:34 +0200 |
| commit | 0b36622b697db9a47db0354d8c594fa225da3dfc (patch) | |
| tree | 2bc7d4b72ab16325b51c25ab2213c095e36921b6 /src/threadpool.h | |
| parent | ea902c49d752980a664671d21ed081877ee8cb96 (diff) | |
| download | ffwd-0b36622b697db9a47db0354d8c594fa225da3dfc.tar.gz ffwd-0b36622b697db9a47db0354d8c594fa225da3dfc.zip | |
kernels: extract the persistent thread pool into threadpool.{c,h}
Diffstat (limited to 'src/threadpool.h')
| -rw-r--r-- | src/threadpool.h | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/threadpool.h b/src/threadpool.h new file mode 100644 index 0000000..1813bb0 --- /dev/null +++ b/src/threadpool.h @@ -0,0 +1,36 @@ +#ifndef EMBED_THREADPOOL_H +#define EMBED_THREADPOOL_H + +/* Persistent worker pool for the CPU math kernels: a fixed set of threads that + * sleep until parallel_for() hands them a callback, plus the CPU-count helper + * that sizes the pool (cgroup-quota aware). Separated from the math so the + * kernels depend only on the parallel_for / embed_num_threads interface, not on + * the pool's internals. kernels.h includes this, so kernel callers need no + * extra include. */ + +#ifndef EMBED_API +#if defined(__GNUC__) +#define EMBED_API __attribute__((visibility("default"))) +#else +#define EMBED_API +#endif +#endif + +/* Worker callback: (thread index, total threads, user arg). */ +typedef void (*parallel_fn_t)(int tid, int n_threads, void *arg); + +/* Set the persistent pool size (default 1). Creates/rebuilds the pool; call + * before inference. */ +EMBED_API void embed_set_threads(int n); + +/* Available CPU cores, capped to a cgroup CPU quota when one is tighter. */ +EMBED_API int embed_get_num_cpus(void); + +/* Current pool size (1 == serial). Kernels gate parallelism on this. */ +int embed_num_threads(void); + +/* Run fn across the pool; the calling thread participates as tid 0. Blocks + * until every worker has finished. */ +void parallel_for(parallel_fn_t fn, void *arg); + +#endif /* EMBED_THREADPOOL_H */ |
