28 omp_set_num_threads(1);
47 const char *val = getenv(name);
48 if (!val || !val[0]) {
54 long n = strtol(val, &
end, 10);
55 if (errno != 0 ||
end == val || n <= 0 || n > (1L << 20)) {
64 int physical_cores = 0;
65 int logical_cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
66 if (logical_cores <= 0) {
71 FILE *f = fopen(
"/proc/cpuinfo",
"r");
83 const int seen_cap = (int)(
sizeof(seen) /
sizeof(seen[0]));
86 #define CK_ADD_PAIR(pid, cid) \
88 if ((pid) >= 0 && (cid) >= 0) { \
90 for (int ii = 0; ii < seen_count; ++ii) { \
91 if (seen[ii].physical_id == (pid) && \
92 seen[ii].core_id == (cid)) { \
97 if (!exists && seen_count < seen_cap) { \
98 seen[seen_count].physical_id = (pid); \
99 seen[seen_count].core_id = (cid); \
105 while (fgets(line,
sizeof(line), f)) {
109 if (line[0] ==
'\n' || line[0] ==
'\0') {
116 if (sscanf(line,
"physical id : %d", &val) == 1) {
120 if (sscanf(line,
"core id : %d", &val) == 1) {
132 physical_cores = seen_count;
137 if (physical_cores <= 1 && logical_cores > 1) {
138 return logical_cores;
141 if (physical_cores > 1) {
142 return physical_cores;
145 return logical_cores;
151 if (num_threads <= 0) {
156 if (env_threads <= 0) {
167 omp_set_num_threads(num_threads);
171 mkl_set_num_threads(num_threads);
174 fprintf(stderr,
"[CK] Set %d threads (auto=%d)\n",
Persistent pthread thread pool for CK-Engine inference.
void ck_threadpool_global_destroy(void)
ck_threadpool_t * ck_threadpool_global(void)
static int ck_parse_env_int(const char *name)
void ck_threadpool_init(void)
void ck_threadpool_shutdown(void)
void ck_set_num_threads(int num_threads)
static int ck_strict_parity
int ck_get_physical_cores(void)
void ck_set_strict_parity(int enabled)
ck_threadpool_t * ck_get_threadpool(void)
#define CK_ADD_PAIR(pid, cid)
static int g_threads_initialized
int ck_get_num_threads(void)
int ck_strict_parity_enabled(void)