aboutsummaryrefslogtreecommitdiff
path: root/src/threadpool.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-20 20:10:56 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-21 05:57:19 +0200
commit6b969d25ce136eaf695222bd1a10bff1ecac0220 (patch)
treec23afb50c28b9deaddae5c6526a74ee11cf2d865 /src/threadpool.c
parentd4bdb8dae753462f621507d5fc3582e0e53da35c (diff)
downloadffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.tar.gz
ffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.zip
all: split Makefile and reorg library/tools
Diffstat (limited to '')
-rw-r--r--libffwd/threadpool.c (renamed from src/threadpool.c)59
1 files changed, 21 insertions, 38 deletions
diff --git a/src/threadpool.c b/libffwd/threadpool.c
index 40d22c7..2521a6d 100644
--- a/src/threadpool.c
+++ b/libffwd/threadpool.c
@@ -1,5 +1,9 @@
+/*
+ * tp.c - CPU thread pool
+ */
+
#if defined(__linux__) && !defined(_GNU_SOURCE)
-#define _GNU_SOURCE
+# define _GNU_SOURCE
#endif
#include "threadpool.h"
@@ -11,28 +15,23 @@
#include <string.h>
#ifdef __APPLE__
-#include <sys/sysctl.h>
+# include <sys/sysctl.h>
#else
-#ifdef __linux__
-#include <sched.h>
-#endif
-#include <unistd.h>
+# ifdef __linux__
+# include <sched.h>
+# endif
+# include <unistd.h>
#endif
#if defined(USE_OPENBLAS)
void openblas_set_num_threads(int num_threads);
#endif
-/* The global verbose flag (defined in model.c); used only for optional pool
- * diagnostics below. Declared here to keep the thread pool independent of the
- * kernel/model headers. */
-extern int embed_verbose;
-
-#define EMBED_MAX_THREADS 16
+#define TP_MAX_THREADS 16
static struct {
- pthread_t threads[EMBED_MAX_THREADS - 1];
- int tids[EMBED_MAX_THREADS - 1];
+ pthread_t threads[TP_MAX_THREADS - 1];
+ int tids[TP_MAX_THREADS - 1];
int n_threads;
int shutdown;
@@ -80,11 +79,11 @@ static void *worker_loop(void *arg) {
}
}
-void embed_set_threads(int n) {
+void tp_set_threads(int n) {
if (n < 1)
n = 1;
- if (n > EMBED_MAX_THREADS)
- n = EMBED_MAX_THREADS;
+ if (n > TP_MAX_THREADS)
+ n = TP_MAX_THREADS;
#if defined(USE_OPENBLAS)
/* OpenBLAS owns the heavy F32 GEMM parallelism on Linux. Keep the
@@ -107,29 +106,13 @@ void embed_set_threads(int n) {
}
tp.n_threads = n;
- if (n <= 1) {
- if (embed_verbose >= 2) {
-#if defined(USE_OPENBLAS)
- fprintf(stderr, "Thread pool: %d threads, OpenBLAS: %d threads\n", n, blas_threads);
-#else
- fprintf(stderr, "Thread pool: %d threads\n", n);
-#endif
- }
+ if (n <= 1)
return;
- }
for (int i = 0; i < n - 1; i++) {
tp.tids[i] = i + 1;
pthread_create(&tp.threads[i], NULL, worker_loop, &tp.tids[i]);
}
-
- if (embed_verbose >= 2) {
-#if defined(USE_OPENBLAS)
- fprintf(stderr, "Thread pool: %d threads, OpenBLAS: %d threads\n", n, blas_threads);
-#else
- fprintf(stderr, "Thread pool: %d threads\n", n);
-#endif
- }
}
#ifndef __APPLE__
@@ -177,7 +160,7 @@ static int cgroup_cpu_quota(void) {
}
#endif
-int embed_get_num_cpus(void) {
+int tp_get_num_cpus(void) {
#ifdef __APPLE__
int n = 0;
size_t len = sizeof(n);
@@ -187,14 +170,14 @@ int embed_get_num_cpus(void) {
int online = (int)sysconf(_SC_NPROCESSORS_ONLN);
if (online < 1)
online = 1;
-#ifdef __linux__
+# ifdef __linux__
cpu_set_t set;
if (sched_getaffinity(0, sizeof(set), &set) == 0) {
int affinity = CPU_COUNT(&set);
if (affinity >= 1 && affinity < online)
online = affinity;
}
-#endif
+# endif
/* Honor a cgroup CPU quota when it is tighter than the visible core count,
* so the default thread pool does not oversubscribe a CPU-limited
* container and get throttled. */
@@ -205,7 +188,7 @@ int embed_get_num_cpus(void) {
#endif
}
-int embed_num_threads(void) { return tp.n_threads; }
+int tp_num_threads(void) { return tp.n_threads; }
/* Dispatch work to all threads; main thread is tid=0 */
void parallel_for(parallel_fn_t fn, void *arg) {