Skip to content

Commit 69edc5b

Browse files
authored
Restore dropped patches in the non-TLS branch of memory.c (#2004)
* Restore dropped patches in the non-TLS branch of memory.c As discovered in #2002, the reintroduction of the "original" non-TLS version of memory.c as an alternate branch had inadvertently used ba1f91f rather than a8002e2 , thereby dropping the commits for #1450, #1468, #1501, #1504 and #1520.
1 parent 641767f commit 69edc5b

File tree

1 file changed

+55
-22
lines changed

1 file changed

+55
-22
lines changed

driver/others/memory.c

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,9 +1603,11 @@ void gotoblas_dummy_for_PGI(void) {
16031603
#endif
16041604

16051605
#else
1606+
/* USE_TLS / COMPILE_TLS not set */
1607+
16061608
#include <errno.h>
16071609

1608-
#ifdef OS_WINDOWS
1610+
#if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
16091611
#define ALLOC_WINDOWS
16101612
#ifndef MEM_LARGE_PAGES
16111613
#define MEM_LARGE_PAGES 0x20000000
@@ -1619,7 +1621,7 @@ void gotoblas_dummy_for_PGI(void) {
16191621
#include <stdio.h>
16201622
#include <fcntl.h>
16211623

1622-
#ifndef OS_WINDOWS
1624+
#if !defined(OS_WINDOWS) || defined(OS_CYGWIN_NT)
16231625
#include <sys/mman.h>
16241626
#ifndef NO_SYSV_IPC
16251627
#include <sys/shm.h>
@@ -1639,7 +1641,7 @@ void gotoblas_dummy_for_PGI(void) {
16391641
#include <sys/resource.h>
16401642
#endif
16411643

1642-
#if defined(OS_FREEBSD) || defined(OS_DARWIN)
1644+
#if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN)
16431645
#include <sys/sysctl.h>
16441646
#include <sys/resource.h>
16451647
#endif
@@ -1678,9 +1680,12 @@ void gotoblas_dummy_for_PGI(void) {
16781680
#elif (defined(OS_DARWIN) || defined(OS_SUNOS)) && defined(C_GCC)
16791681
#define CONSTRUCTOR __attribute__ ((constructor))
16801682
#define DESTRUCTOR __attribute__ ((destructor))
1681-
#else
1683+
#elif __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
16821684
#define CONSTRUCTOR __attribute__ ((constructor(101)))
16831685
#define DESTRUCTOR __attribute__ ((destructor(101)))
1686+
#else
1687+
#define CONSTRUCTOR __attribute__ ((constructor))
1688+
#define DESTRUCTOR __attribute__ ((destructor))
16841689
#endif
16851690

16861691
#ifdef DYNAMIC_ARCH
@@ -1740,7 +1745,8 @@ int i,n;
17401745
size = CPU_ALLOC_SIZE(nums);
17411746
ret = sched_getaffinity(0,size,cpusetp);
17421747
if (ret!=0) return nums;
1743-
nums = CPU_COUNT_S(size,cpusetp);
1748+
ret = CPU_COUNT_S(size,cpusetp);
1749+
if (ret > 0 && ret < nums) nums = ret;
17441750
CPU_FREE(cpusetp);
17451751
return nums;
17461752
#endif
@@ -1756,7 +1762,7 @@ int get_num_procs(void) {
17561762
return nums;
17571763
}
17581764
#endif
1759-
1765+
17601766
#ifdef OS_HAIKU
17611767
int get_num_procs(void) {
17621768
static int nums = 0;
@@ -1793,7 +1799,7 @@ int get_num_procs(void) {
17931799

17941800
#endif
17951801

1796-
#if defined(OS_FREEBSD)
1802+
#if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY)
17971803

17981804
int get_num_procs(void) {
17991805

@@ -1870,7 +1876,7 @@ void openblas_fork_handler()
18701876
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
18711877
// In the mean time build with USE_OPENMP=0 or link against another
18721878
// implementation of OpenMP.
1873-
#if !(defined(OS_WINDOWS) || defined(OS_ANDROID)) && defined(SMP_SERVER)
1879+
#if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER)
18741880
int err;
18751881
err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, NULL);
18761882
if(err != 0)
@@ -1883,19 +1889,19 @@ extern int openblas_goto_num_threads_env();
18831889
extern int openblas_omp_num_threads_env();
18841890

18851891
int blas_get_cpu_number(void){
1886-
#if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
1892+
#if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID)
18871893
int max_num;
18881894
#endif
18891895
int blas_goto_num = 0;
18901896
int blas_omp_num = 0;
18911897

18921898
if (blas_num_threads) return blas_num_threads;
18931899

1894-
#if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
1900+
#if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID)
18951901
max_num = get_num_procs();
18961902
#endif
18971903

1898-
blas_goto_num = 0;
1904+
// blas_goto_num = 0;
18991905
#ifndef USE_OPENMP
19001906
blas_goto_num=openblas_num_threads_env();
19011907
if (blas_goto_num < 0) blas_goto_num = 0;
@@ -1907,15 +1913,15 @@ int blas_get_cpu_number(void){
19071913

19081914
#endif
19091915

1910-
blas_omp_num = 0;
1916+
// blas_omp_num = 0;
19111917
blas_omp_num=openblas_omp_num_threads_env();
19121918
if (blas_omp_num < 0) blas_omp_num = 0;
19131919

19141920
if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
19151921
else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
19161922
else blas_num_threads = MAX_CPU_NUMBER;
19171923

1918-
#if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
1924+
#if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID)
19191925
if (blas_num_threads > max_num) blas_num_threads = max_num;
19201926
#endif
19211927

@@ -2002,11 +2008,15 @@ static void *alloc_mmap(void *address){
20022008
}
20032009

20042010
if (map_address != (void *)-1) {
2011+
#if defined(SMP) && !defined(USE_OPENMP)
20052012
LOCK_COMMAND(&alloc_lock);
2013+
#endif
20062014
release_info[release_pos].address = map_address;
20072015
release_info[release_pos].func = alloc_mmap_free;
20082016
release_pos ++;
2017+
#if defined(SMP) && !defined(USE_OPENMP)
20092018
UNLOCK_COMMAND(&alloc_lock);
2019+
#endif
20102020
}
20112021

20122022
#ifdef OS_LINUX
@@ -2148,14 +2158,18 @@ static void *alloc_mmap(void *address){
21482158
#if defined(OS_LINUX) && !defined(NO_WARMUP)
21492159
}
21502160
#endif
2151-
LOCK_COMMAND(&alloc_lock);
21522161

21532162
if (map_address != (void *)-1) {
2163+
#if defined(SMP) && !defined(USE_OPENMP)
2164+
LOCK_COMMAND(&alloc_lock);
2165+
#endif
21542166
release_info[release_pos].address = map_address;
21552167
release_info[release_pos].func = alloc_mmap_free;
21562168
release_pos ++;
2169+
#if defined(SMP) && !defined(USE_OPENMP)
2170+
UNLOCK_COMMAND(&alloc_lock);
2171+
#endif
21572172
}
2158-
UNLOCK_COMMAND(&alloc_lock);
21592173

21602174
return map_address;
21612175
}
@@ -2554,6 +2568,11 @@ void *blas_memory_alloc(int procpos){
25542568
NULL,
25552569
};
25562570
void *(**func)(void *address);
2571+
2572+
#if defined(USE_OPENMP)
2573+
if (!memory_initialized) {
2574+
#endif
2575+
25572576
LOCK_COMMAND(&alloc_lock);
25582577

25592578
if (!memory_initialized) {
@@ -2589,6 +2608,9 @@ void *blas_memory_alloc(int procpos){
25892608

25902609
}
25912610
UNLOCK_COMMAND(&alloc_lock);
2611+
#if defined(USE_OPENMP)
2612+
}
2613+
#endif
25922614

25932615
#ifdef DEBUG
25942616
printf("Alloc Start ...\n");
@@ -2603,13 +2625,17 @@ void *blas_memory_alloc(int procpos){
26032625
26042626
do {
26052627
if (!memory[position].used && (memory[position].pos == mypos)) {
2628+
#if defined(SMP) && !defined(USE_OPENMP)
26062629
LOCK_COMMAND(&alloc_lock);
2607-
// blas_lock(&memory[position].lock);
2608-
2630+
#else
2631+
blas_lock(&memory[position].lock);
2632+
#endif
26092633
if (!memory[position].used) goto allocation;
2610-
2634+
#if defined(SMP) && !defined(USE_OPENMP)
26112635
UNLOCK_COMMAND(&alloc_lock);
2612-
// blas_unlock(&memory[position].lock);
2636+
#else
2637+
blas_unlock(&memory[position].lock);
2638+
#endif
26132639
}
26142640
26152641
position ++;
@@ -2647,7 +2673,6 @@ void *blas_memory_alloc(int procpos){
26472673
memory[position].used = 1;
26482674

26492675
UNLOCK_COMMAND(&alloc_lock);
2650-
/* blas_unlock(&memory[position].lock);*/
26512676

26522677
if (!memory[position].addr) {
26532678
do {
@@ -2693,9 +2718,13 @@ void *blas_memory_alloc(int procpos){
26932718

26942719
} while ((BLASLONG)map_address == -1);
26952720

2721+
#if defined(SMP) && !defined(USE_OPENMP)
26962722
LOCK_COMMAND(&alloc_lock);
2723+
#endif
26972724
memory[position].addr = map_address;
2725+
#if defined(SMP) && !defined(USE_OPENMP)
26982726
UNLOCK_COMMAND(&alloc_lock);
2727+
#endif
26992728

27002729
#ifdef DEBUG
27012730
printf(" Mapping Succeeded. %p(%d)\n", (void *)memory[position].addr, position);
@@ -2749,8 +2778,9 @@ void blas_memory_free(void *free_area){
27492778
#endif
27502779

27512780
position = 0;
2781+
#if defined(SMP) && !defined(USE_OPENMP)
27522782
LOCK_COMMAND(&alloc_lock);
2753-
2783+
#endif
27542784
while ((position < NUM_BUFFERS) && (memory[position].addr != free_area))
27552785
position++;
27562786

@@ -2764,7 +2794,9 @@ void blas_memory_free(void *free_area){
27642794
WMB;
27652795

27662796
memory[position].used = 0;
2797+
#if defined(SMP) && !defined(USE_OPENMP)
27672798
UNLOCK_COMMAND(&alloc_lock);
2799+
#endif
27682800

27692801
#ifdef DEBUG
27702802
printf("Unmap Succeeded.\n\n");
@@ -2779,8 +2811,9 @@ void blas_memory_free(void *free_area){
27792811
for (position = 0; position < NUM_BUFFERS; position++)
27802812
printf("%4ld %p : %d\n", position, memory[position].addr, memory[position].used);
27812813
#endif
2814+
#if defined(SMP) && !defined(USE_OPENMP)
27822815
UNLOCK_COMMAND(&alloc_lock);
2783-
2816+
#endif
27842817
return;
27852818
}
27862819

0 commit comments

Comments
 (0)