Skip to content

Commit 8373ad4

Browse files
committed
Merge pull request #397 from vtjnash/develop
fix #394
2 parents d10db52 + f41f03a commit 8373ad4

File tree

6 files changed

+39
-35
lines changed

6 files changed

+39
-35
lines changed

common.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,15 @@ please https://github.com/xianyi/OpenBLAS/issues/246
388388
#include "common_arm64.h"
389389
#endif
390390

391+
#ifndef ASSEMBLER
392+
#ifdef OS_WINDOWS
393+
typedef char env_var_t[MAX_PATH];
394+
#define readenv(p, n) GetEnvironmentVariable((n), (p), sizeof(p))
395+
#else
396+
typedef char* env_var_t;
397+
#define readenv(p, n) ((p)=getenv(n))
398+
#endif
399+
#endif
391400

392401
#ifdef OS_LINUX
393402
#include "common_linux.h"
@@ -515,13 +524,9 @@ static __inline void blas_unlock(volatile BLASULONG *address){
515524
*address = 0;
516525
}
517526

518-
static __inline int readenv(char *env) {
519-
520-
char *p;
521-
522-
p = getenv(env);
523-
524-
if (p == NULL) return 0; else return atoi(p);
527+
static __inline int readenv_atoi(char *env) {
528+
env_var_t p;
529+
return readenv(p,env) ? 0 : atoi(p);
525530
}
526531

527532

@@ -687,8 +692,8 @@ extern int gotoblas_profile;
687692
#define PRINT_DEBUG_CNAME
688693
#define PRINT_DEBUG_NAME
689694
#else
690-
#define PRINT_DEBUG_CNAME if (readenv("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME)
691-
#define PRINT_DEBUG_NAME if (readenv("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME)
695+
#define PRINT_DEBUG_CNAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME)
696+
#define PRINT_DEBUG_NAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME)
692697
#endif
693698

694699
#ifdef __cplusplus

driver/others/blas_server.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,18 +533,15 @@ int blas_thread_init(void){
533533

534534
if (!blas_server_avail){
535535

536-
char *p;
536+
env_var_t p;
537537

538-
p = getenv("THREAD_TIMEOUT");
539-
540-
if (p) {
538+
if (readenv(p,"THREAD_TIMEOUT")) {
541539
thread_timeout = atoi(p);
542540
if (thread_timeout < 4) thread_timeout = 4;
543541
if (thread_timeout > 30) thread_timeout = 30;
544542
thread_timeout = (1 << thread_timeout);
545543
}else{
546-
p = getenv("GOTO_THREAD_TIMEOUT");
547-
if (p) {
544+
if (readenv(p,"GOTO_THREAD_TIMEOUT")) {
548545
thread_timeout = atoi(p);
549546
if (thread_timeout < 4) thread_timeout = 4;
550547
if (thread_timeout > 30) thread_timeout = 30;

driver/others/init.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,11 +698,11 @@ void gotoblas_affinity_init(void) {
698698
#ifdef USE_OPENMP
699699
numprocs = 0;
700700
#else
701-
numprocs = readenv("OPENBLAS_NUM_THREADS");
702-
if (numprocs == 0) numprocs = readenv("GOTO_NUM_THREADS");
701+
numprocs = readenv_atoi("OPENBLAS_NUM_THREADS");
702+
if (numprocs == 0) numprocs = readenv_atoi("GOTO_NUM_THREADS");
703703
#endif
704704

705-
if (numprocs == 0) numprocs = readenv("OMP_NUM_THREADS");
705+
if (numprocs == 0) numprocs = readenv_atoi("OMP_NUM_THREADS");
706706

707707
numnodes = 1;
708708

@@ -793,7 +793,7 @@ void gotoblas_affinity_init(void) {
793793

794794
setup_mempolicy();
795795

796-
if (readenv("OPENBLAS_MAIN_FREE") || readenv("GOTOBLAS_MAIN_FREE")) {
796+
if (readenv_atoi("OPENBLAS_MAIN_FREE") || readenv_atoi("GOTOBLAS_MAIN_FREE")) {
797797
sched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
798798
}
799799

driver/others/memory.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void openblas_fork_handler()
273273
}
274274

275275
int blas_get_cpu_number(void){
276-
char *p;
276+
env_var_t p;
277277
#if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN)
278278
int max_num;
279279
#endif
@@ -288,21 +288,18 @@ int blas_get_cpu_number(void){
288288

289289
blas_goto_num = 0;
290290
#ifndef USE_OPENMP
291-
p = getenv("OPENBLAS_NUM_THREADS");
292-
if (p) blas_goto_num = atoi(p);
291+
if (readenv(p,"OPENBLAS_NUM_THREADS")) blas_goto_num = atoi(p);
293292
if (blas_goto_num < 0) blas_goto_num = 0;
294293

295294
if (blas_goto_num == 0) {
296-
p = getenv("GOTO_NUM_THREADS");
297-
if (p) blas_goto_num = atoi(p);
295+
if (readenv(p,"GOTO_NUM_THREADS")) blas_goto_num = atoi(p);
298296
if (blas_goto_num < 0) blas_goto_num = 0;
299297
}
300298

301299
#endif
302300

303301
blas_omp_num = 0;
304-
p = getenv("OMP_NUM_THREADS");
305-
if (p) blas_omp_num = atoi(p);
302+
if (readenv(p,"OMP_NUM_THREADS")) blas_omp_num = atoi(p);
306303
if (blas_omp_num < 0) blas_omp_num = 0;
307304

308305
if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
@@ -769,16 +766,23 @@ static void *alloc_hugetlb(void *address){
769766
tp.PrivilegeCount = 1;
770767
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
771768

772-
if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) return (void *) -1;
769+
if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
770+
CloseHandle(hToken);
771+
return -1;
772+
}
773773

774-
if (AdjustTokenPrivileges(hToken, FALSE, (PTOKEN_PRIVILEGES)&tp, 0, NULL, NULL) != TRUE) return (void *) -1;
774+
if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
775+
CloseHandle(hToken);
776+
return -1;
777+
}
775778

776779
map_address = (void *)VirtualAlloc(address,
777780
BUFFER_SIZE,
778781
MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
779782
PAGE_READWRITE);
780783

781-
AdjustTokenPrivileges(hToken, TRUE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, NULL);
784+
tp.Privileges[0].Attributes = 0;
785+
AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
782786

783787
if (map_address == (void *)NULL) map_address = (void *)-1;
784788

driver/others/openblas_error_handle.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3535

3636
int openblas_verbose() {
3737
int ret=0;
38-
char *p;
39-
p = getenv("OPENBLAS_VERBOSE");
40-
if (p) ret = atoi(p);
38+
env_var_t p;
39+
if (readenv(p,"OPENBLAS_VERBOSE")) ret = atoi(p);
4140
if(ret<0) ret=0;
4241
return ret;
4342
}

driver/others/parameter.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ int get_L2_size(void){
248248

249249
void blas_set_parameter(void){
250250

251-
char *p;
251+
env_var_t p;
252252
int factor;
253253
int size = get_L2_size();
254254

@@ -463,9 +463,8 @@ void blas_set_parameter(void){
463463
#endif
464464
#endif
465465

466-
p = getenv("GOTO_BLOCK_FACTOR");
467466

468-
if (p) {
467+
if (readenv(p,"GOTO_BLOCK_FACTOR")) {
469468
factor = atoi(p);
470469
if (factor < 10) factor = 10;
471470
if (factor > 200) factor = 200;

0 commit comments

Comments
 (0)