Skip to content

Commit 05196a8

Browse files
committed
Refs #716. Only call getenv at init function.
1 parent db9b611 commit 05196a8

File tree

7 files changed

+110
-33
lines changed

7 files changed

+110
-33
lines changed

driver/others/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(COMMON_SOURCES
3333
xerbla.c
3434
openblas_set_num_threads.c
3535
openblas_error_handle.c
36+
openblas_env.c
3637
openblas_get_num_procs.c
3738
openblas_get_num_threads.c
3839
)

driver/others/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
TOPDIR = ../..
22
include ../../Makefile.system
33

4-
COMMONOBJS = memory.$(SUFFIX) xerbla.$(SUFFIX) c_abs.$(SUFFIX) z_abs.$(SUFFIX) openblas_set_num_threads.$(SUFFIX) openblas_get_num_threads.$(SUFFIX) openblas_get_num_procs.$(SUFFIX) openblas_get_config.$(SUFFIX) openblas_get_parallel.$(SUFFIX) openblas_error_handle.$(SUFFIX)
4+
COMMONOBJS = memory.$(SUFFIX) xerbla.$(SUFFIX) c_abs.$(SUFFIX) z_abs.$(SUFFIX) openblas_set_num_threads.$(SUFFIX) openblas_get_num_threads.$(SUFFIX) openblas_get_num_procs.$(SUFFIX) openblas_get_config.$(SUFFIX) openblas_get_parallel.$(SUFFIX) openblas_error_handle.$(SUFFIX) openblas_env.$(SUFFIX)
55

66
#COMMONOBJS += slamch.$(SUFFIX) slamc3.$(SUFFIX) dlamch.$(SUFFIX) dlamc3.$(SUFFIX)
77

driver/others/blas_server.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9292
#endif
9393
#endif
9494

95+
extern unsigned int openblas_thread_timeout();
96+
9597
#ifdef SMP_SERVER
9698

9799
#undef MONITOR
@@ -524,6 +526,7 @@ static int blas_monitor(void *arg){
524526
int blas_thread_init(void){
525527
BLASLONG i;
526528
int ret;
529+
int thread_timeout_env;
527530
#ifdef NEED_STACKATTR
528531
pthread_attr_t attr;
529532
#endif
@@ -540,22 +543,12 @@ int blas_thread_init(void){
540543

541544
if (!blas_server_avail){
542545

543-
env_var_t p;
544-
545-
if (readenv(p,"THREAD_TIMEOUT")) {
546-
thread_timeout = atoi(p);
547-
if (thread_timeout < 4) thread_timeout = 4;
548-
if (thread_timeout > 30) thread_timeout = 30;
549-
thread_timeout = (1 << thread_timeout);
550-
}else{
551-
if (readenv(p,"GOTO_THREAD_TIMEOUT")) {
552-
thread_timeout = atoi(p);
553-
if (thread_timeout < 4) thread_timeout = 4;
554-
if (thread_timeout > 30) thread_timeout = 30;
555-
thread_timeout = (1 << thread_timeout);
556-
}
557-
}
558-
546+
thread_timeout_env=openblas_thread_timeout();
547+
if (thread_timeout_env>0) {
548+
if (thread_timeout_env < 4) thread_timeout_env = 4;
549+
if (thread_timeout_env > 30) thread_timeout_env = 30;
550+
thread_timeout = (1 << thread_timeout_env);
551+
}
559552

560553
for(i = 0; i < blas_num_threads - 1; i++){
561554

driver/others/memory.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,11 @@ void openblas_fork_handler()
294294
#endif
295295
}
296296

297+
extern int openblas_num_threads_env();
298+
extern int openblas_goto_num_threads_env();
299+
extern int openblas_omp_num_threads_env();
300+
297301
int blas_get_cpu_number(void){
298-
env_var_t p;
299302
#if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
300303
int max_num;
301304
#endif
@@ -310,18 +313,18 @@ int blas_get_cpu_number(void){
310313

311314
blas_goto_num = 0;
312315
#ifndef USE_OPENMP
313-
if (readenv(p,"OPENBLAS_NUM_THREADS")) blas_goto_num = atoi(p);
316+
blas_goto_num=openblas_num_threads_env();
314317
if (blas_goto_num < 0) blas_goto_num = 0;
315318

316319
if (blas_goto_num == 0) {
317-
if (readenv(p,"GOTO_NUM_THREADS")) blas_goto_num = atoi(p);
318-
if (blas_goto_num < 0) blas_goto_num = 0;
320+
blas_goto_num=openblas_goto_num_threads_env();
321+
if (blas_goto_num < 0) blas_goto_num = 0;
319322
}
320323

321324
#endif
322325

323326
blas_omp_num = 0;
324-
if (readenv(p,"OMP_NUM_THREADS")) blas_omp_num = atoi(p);
327+
blas_omp_num=openblas_omp_num_threads_env();
325328
if (blas_omp_num < 0) blas_omp_num = 0;
326329

327330
if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
@@ -1340,6 +1343,7 @@ static void gotoblas_memory_init(void) {
13401343
/* Initialization for all function; this function should be called before main */
13411344

13421345
static int gotoblas_initialized = 0;
1346+
extern void openblas_read_env();
13431347

13441348
void CONSTRUCTOR gotoblas_init(void) {
13451349

@@ -1349,6 +1353,8 @@ void CONSTRUCTOR gotoblas_init(void) {
13491353
openblas_fork_handler();
13501354
#endif
13511355

1356+
openblas_read_env();
1357+
13521358
#ifdef PROFILE
13531359
moncontrol (0);
13541360
#endif

driver/others/openblas_env.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/***************************************************************************
2+
Copyright (c) 2011-2016, The OpenBLAS Project
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are
7+
met:
8+
9+
1. Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright
13+
notice, this list of conditions and the following disclaimer in
14+
the documentation and/or other materials provided with the
15+
distribution.
16+
17+
3. Neither the name of the OpenBLAS project nor the names of
18+
its contributors may be used to endorse or promote products
19+
derived from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24+
ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
25+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30+
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
*****************************************************************************/
33+
34+
#include "common.h"
35+
36+
static int openblas_env_verbose=0;
37+
static unsigned int openblas_env_thread_timeout=0;
38+
static int openblas_env_block_factor=0;
39+
static int openblas_env_openblas_num_threads=0;
40+
static int openblas_env_goto_num_threads=0;
41+
static int openblas_env_omp_num_threads=0;
42+
43+
int openblas_verbose() { return openblas_env_verbose;}
44+
unsigned int openblas_thread_timeout() { return openblas_env_thread_timeout;}
45+
int openblas_block_factor() { return openblas_env_block_factor;}
46+
int openblas_num_threads_env() { return openblas_env_openblas_num_threads;}
47+
int openblas_goto_num_threads_env() { return openblas_env_goto_num_threads;}
48+
int openblas_omp_num_threads_env() { return openblas_env_omp_num_threads;}
49+
50+
void openblas_read_env() {
51+
int ret=0;
52+
env_var_t p;
53+
if (readenv(p,"OPENBLAS_VERBOSE")) ret = atoi(p);
54+
if(ret<0) ret=0;
55+
openblas_env_verbose=ret;
56+
57+
ret=0;
58+
if (readenv(p,"OPENBLAS_BLOCK_FACTOR")) ret = atoi(p);
59+
if(ret<0) ret=0;
60+
openblas_env_block_factor=ret;
61+
62+
ret=0;
63+
if (readenv(p,"OPENBLAS_THREAD_TIMEOUT")) ret = atoi(p);
64+
if(ret<0) ret=0;
65+
openblas_env_thread_timeout=(unsigned int)ret;
66+
67+
ret=0;
68+
if (readenv(p,"OPENBLAS_NUM_THREADS")) ret = atoi(p);
69+
if(ret<0) ret=0;
70+
openblas_env_openblas_num_threads=ret;
71+
72+
ret=0;
73+
if (readenv(p,"GOTO_NUM_THREADS")) ret = atoi(p);
74+
if(ret<0) ret=0;
75+
openblas_env_goto_num_threads=ret;
76+
77+
ret=0;
78+
if (readenv(p,"OMP_NUM_THREADS")) ret = atoi(p);
79+
if(ret<0) ret=0;
80+
openblas_env_omp_num_threads=ret;
81+
82+
}
83+
84+

driver/others/openblas_error_handle.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3333

3434
#include "common.h"
3535

36-
int openblas_verbose() {
37-
int ret=0;
38-
env_var_t p;
39-
if (readenv(p,"OPENBLAS_VERBOSE")) ret = atoi(p);
40-
if(ret<0) ret=0;
41-
return ret;
42-
}
36+
extern int openblas_verbose();
4337

4438
void openblas_warning(int verbose, const char * msg) {
4539
int current_verbose;

driver/others/parameter.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <string.h>
4141
#include "common.h"
4242

43+
extern int openblas_block_factor();
4344
int get_L2_size(void);
4445

4546
#define DEFAULT_GEMM_P 128
@@ -249,7 +250,6 @@ int get_L2_size(void){
249250

250251
void blas_set_parameter(void){
251252

252-
env_var_t p;
253253
int factor;
254254
#if defined(BULLDOZER) || defined(PILEDRIVER) || defined(SANDYBRIDGE) || defined(NEHALEM) || defined(HASWELL) || defined(STEAMROLLER)
255255
int size = 16;
@@ -468,9 +468,8 @@ void blas_set_parameter(void){
468468
#endif
469469
#endif
470470

471-
472-
if (readenv(p,"GOTO_BLOCK_FACTOR")) {
473-
factor = atoi(p);
471+
factor=openblas_block_factor();
472+
if (factor>0) {
474473
if (factor < 10) factor = 10;
475474
if (factor > 200) factor = 200;
476475

0 commit comments

Comments
 (0)