Skip to content

Commit d2ce931

Browse files
committed
Add OPENBLAS_DEFAULT_NUM_THREADS
This allows Julia to set a default number of threads (usually `1`) to be used when no other thread counts are specified [0], to short-circuit the default OpenBLAS thread initialization routine that spins up a different number of threads than Julia would otherwise choose. The reason to add a new environment variable is that we want to be able to configure OpenBLAS to avoid performing its initial memory allocation/thread startup, as that can consume significant amounts of memory, but we still want to be sensitive to legacy codebases that set things like `OMP_NUM_THREADS` or `GOTOBLAS_NUM_THREADS`. Creating a new environment variable that is openblas-specific and is not already publicly used to control the overall number of threads of programs like Julia seems to be the best way forward. [0] JuliaLang/julia#46844
1 parent 667d0e0 commit d2ce931

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

driver/others/init.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@ void gotoblas_affinity_init(void) {
823823

824824
if (numprocs == 0) numprocs = readenv_atoi("OMP_NUM_THREADS");
825825

826+
if (numprocs == 0) numprocs = readenv_atoi("OPENBLAS_DEFAULT_NUM_THREADS");
827+
826828
numnodes = 1;
827829

828830
if (numprocs == 1) {

driver/others/openblas_env.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ void openblas_read_env() {
6767
openblas_env_thread_timeout=(unsigned int)ret;
6868

6969
ret=0;
70-
if (readenv(p,"OPENBLAS_NUM_THREADS")) ret = atoi(p);
70+
if (readenv(p,"OPENBLAS_DEFAULT_NUM_THREADS")) ret = atoi(p);
7171
if(ret<0) ret=0;
7272
openblas_env_openblas_num_threads=ret;
7373

74+
ret=0;
75+
if (readenv(p,"OPENBLAS_NUM_THREADS")) ret = atoi(p);
76+
if(ret<0) ret=0;
77+
if(ret != 0 || openblas_env_openblas_num_threads == 0)
78+
openblas_env_openblas_num_threads=ret;
79+
7480
ret=0;
7581
if (readenv(p,"GOTO_NUM_THREADS")) ret = atoi(p);
7682
if(ret<0) ret=0;

0 commit comments

Comments
 (0)