Skip to content

Commit ec3ec3a

Browse files
committed
Get the number of threads from accelerate
1 parent f94f2ab commit ec3ec3a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/threading.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,24 @@ LBT_DLLEXPORT int32_t lbt_get_num_threads() {
148148
int nthreads = fptr_acc();
149149

150150
if(nthreads == ACCELERATE_BLAS_THREADING_MULTI_THREADED) {
151-
// This number is arbitrary right now, but greater than 1 to mean multi-threaded.
152-
// TODO: Can we guestimate the number of threads from the APPLE_NTHREADS symbol in accelerate?
153-
max_threads = 2;
151+
int (*fptr_acc_nthreads)(void) = lookup_symbol(lib->handle, "APPLE_NTHREADS");
152+
if (fptr_acc != NULL) {
153+
// In Accelerate, there is a symbol called APPLE_NTHREADS, which appears to be a function we
154+
// can call to get an integer saying the number of CPU threads. There is no documentation for this
155+
// anywhere accessible online, but testing two different CPUs seem to suggest it is CPU cores.
156+
//
157+
// Doing this:
158+
// julia> @ccall AppleAccelerate.libacc.APPLE_NTHREADS()::Int
159+
//
160+
// The M2 Max returned 12, M4 Max returned 16, which is the total number of cores (both big and little)
161+
// in each processor.
162+
int nthreads = fptr_acc_nthreads();
163+
max_threads = max(max_threads, nthreads);
164+
} else {
165+
// This number is arbitrary because we have no idea how many threads are actually in use,
166+
// but greater than 1 to mean multi-threaded.
167+
max_threads = max(max_threads, 2);
168+
}
154169
} else {
155170
// Single-threaded
156171
max_threads = max(max_threads, 1);

0 commit comments

Comments
 (0)