Skip to content

Commit afb597b

Browse files
committed
Document openblas_set_num_threads_local(). See #4425
Also see #5907 (comment)
1 parent f8674a7 commit afb597b

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

cblas.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,39 @@ extern "C" {
4040
/*Set the number of threads on runtime.*/
4141
void openblas_set_num_threads(int num_threads);
4242
void goto_set_num_threads(int num_threads);
43+
/*
44+
* Intent:
45+
* Sets the thread-local number of threads that OpenBLAS functions should request.
46+
* This thread-local number only affects the current execution thread
47+
* and, when set, overrides the global default.
48+
* This is useful for controlling nested parallelism.
49+
* For example when the caller's own (e.g. OpenMP) threads each invoke
50+
* OpenBLAS, `num_threads` determines how many threads OpenBLAS may use
51+
* per such call, instead of the process-wide default of
52+
* `omp_get_max_threads()`.
53+
*
54+
* The intent of this was likely to match MKL's `mkl_set_num_threads_local()`
55+
* but implementation may not live up to the original intent yet.
56+
*
57+
* TODO:
58+
* Currently the implementation doesn't really do any of that:
59+
* * The setting is NOT thread-local.
60+
* * This function currently just calls `openblas_set_num_threads(num_threads)`,
61+
* changing the same process-wide thread count as that function,
62+
* and additionally records `num_threads` in an internal (global, not per-thread)
63+
* variable that is only consulted when OpenBLAS is invoked from within
64+
* a caller's OpenMP parallel region and the user has not made an
65+
* explicit thread-count choice.
66+
* * There is no special handling of `num_threads == 0`
67+
* (or other MKL-style reset values); the value is forwarded to
68+
* `openblas_set_num_threads()`, where values < 1 are treated
69+
* as "use the default" and large values are clamped to the number of cores.
70+
*
71+
* Returns the *previous* number of threads (as reported by
72+
* `openblas_get_num_threads()`), so the caller can save and later restore it.
73+
*
74+
* On single-threaded OpenBLAS builds this is a no-op that returns 1.
75+
*/
4376
int openblas_set_num_threads_local(int num_threads);
4477

4578
/*Get the number of threads on runtime.*/

driver/others/blas_server_omp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
int blas_server_avail = 0;
7171
int blas_omp_number_max = 0;
72-
int blas_omp_threads_local = 1;
72+
int blas_omp_threads_local = 1; // num threads to use when already inside omp_in_parallel()
7373
int blas_is_num_threads_set_explicitly = 0; // tracks whether the user called openblas_set_num_threads()
7474

7575
extern int openblas_omp_adaptive_env(void);

0 commit comments

Comments
 (0)