File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff 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 );
You can’t perform that action at this time.
0 commit comments