Skip to content

Commit 969dd61

Browse files
authored
Merge pull request #2136 from martin-frbg/issue2126
Add option to allow combining USE_THREAD=0 with thread locking support
2 parents d8d5682 + f66c11f commit 969dd61

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

Makefile.rule

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ VERSION = 0.3.7.dev
5858
# For force setting for multi threaded, specify USE_THREAD = 1
5959
# USE_THREAD = 0
6060

61+
# If you want to build a single-threaded OpenBLAS, but expect to call this
62+
# from several concurrent threads in some other program, comment this in for
63+
# thread safety. (This is done automatically for USE_THREAD=1 , and should not
64+
# be necessary when USE_OPENMP=1)
65+
# USE_LOCKING = 1
66+
6167
# If you're going to use this library with OpenMP, please comment it in.
6268
# This flag is always set for POWER8. Don't set USE_OPENMP = 0 if you're targeting POWER8.
6369
# USE_OPENMP = 1

Makefile.system

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ SMP = 1
237237
endif
238238
endif
239239

240+
ifeq ($(SMP), 1)
241+
USE_LOCKING =
242+
endif
243+
240244
ifndef NEED_PIC
241245
NEED_PIC = 1
242246
endif
@@ -388,6 +392,12 @@ ifneq ($(MAX_STACK_ALLOC), 0)
388392
CCOMMON_OPT += -DMAX_STACK_ALLOC=$(MAX_STACK_ALLOC)
389393
endif
390394

395+
ifdef USE_LOCKING
396+
ifneq ($(USE_LOCKING), 0)
397+
CCOMMON_OPT += -DUSE_LOCKING
398+
endif
399+
endif
400+
391401
#
392402
# Architecture dependent settings
393403
#

cmake/system.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ endif ()
136136

137137
if (USE_THREAD)
138138
message(STATUS "Multi-threading enabled with ${NUM_THREADS} threads.")
139+
else()
140+
if (${USE_LOCKING})
141+
set(CCOMMON_OPT "${CCOMMON_OPT} -DUSE_LOCKING")
142+
endif ()
139143
endif ()
140144

141145
include("${PROJECT_SOURCE_DIR}/cmake/prebuild.cmake")

common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extern "C" {
131131
#include <time.h>
132132
#include <unistd.h>
133133
#include <math.h>
134-
#ifdef SMP
134+
#if defined(SMP) || defined(USE_LOCKING)
135135
#include <pthread.h>
136136
#endif
137137
#endif
@@ -200,7 +200,7 @@ extern "C" {
200200
#error "You can't specify both LOCK operation!"
201201
#endif
202202

203-
#ifdef SMP
203+
#if defined(SMP) || defined(USE_LOCKING)
204204
#define USE_PTHREAD_LOCK
205205
#undef USE_PTHREAD_SPINLOCK
206206
#endif

driver/others/memory.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,13 +2062,13 @@ static void *alloc_mmap(void *address){
20622062
}
20632063

20642064
if (map_address != (void *)-1) {
2065-
#if defined(SMP) && !defined(USE_OPENMP)
2065+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
20662066
LOCK_COMMAND(&alloc_lock);
20672067
#endif
20682068
release_info[release_pos].address = map_address;
20692069
release_info[release_pos].func = alloc_mmap_free;
20702070
release_pos ++;
2071-
#if defined(SMP) && !defined(USE_OPENMP)
2071+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
20722072
UNLOCK_COMMAND(&alloc_lock);
20732073
#endif
20742074
}
@@ -2214,13 +2214,13 @@ static void *alloc_mmap(void *address){
22142214
#endif
22152215

22162216
if (map_address != (void *)-1) {
2217-
#if defined(SMP) && !defined(USE_OPENMP)
2217+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
22182218
LOCK_COMMAND(&alloc_lock);
22192219
#endif
22202220
release_info[release_pos].address = map_address;
22212221
release_info[release_pos].func = alloc_mmap_free;
22222222
release_pos ++;
2223-
#if defined(SMP) && !defined(USE_OPENMP)
2223+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
22242224
UNLOCK_COMMAND(&alloc_lock);
22252225
#endif
22262226
}
@@ -2701,7 +2701,7 @@ void *blas_memory_alloc(int procpos){
27012701

27022702
position = 0;
27032703

2704-
#if defined(SMP) && !defined(USE_OPENMP)
2704+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
27052705
LOCK_COMMAND(&alloc_lock);
27062706
#endif
27072707
do {
@@ -2718,7 +2718,7 @@ void *blas_memory_alloc(int procpos){
27182718
position ++;
27192719

27202720
} while (position < NUM_BUFFERS);
2721-
#if defined(SMP) && !defined(USE_OPENMP)
2721+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
27222722
UNLOCK_COMMAND(&alloc_lock);
27232723
#endif
27242724
goto error;
@@ -2730,7 +2730,7 @@ void *blas_memory_alloc(int procpos){
27302730
#endif
27312731

27322732
memory[position].used = 1;
2733-
#if defined(SMP) && !defined(USE_OPENMP)
2733+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
27342734
UNLOCK_COMMAND(&alloc_lock);
27352735
#else
27362736
blas_unlock(&memory[position].lock);
@@ -2779,11 +2779,11 @@ void *blas_memory_alloc(int procpos){
27792779

27802780
} while ((BLASLONG)map_address == -1);
27812781

2782-
#if defined(SMP) && !defined(USE_OPENMP)
2782+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
27832783
LOCK_COMMAND(&alloc_lock);
27842784
#endif
27852785
memory[position].addr = map_address;
2786-
#if defined(SMP) && !defined(USE_OPENMP)
2786+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
27872787
UNLOCK_COMMAND(&alloc_lock);
27882788
#endif
27892789

@@ -2839,7 +2839,7 @@ void blas_memory_free(void *free_area){
28392839
#endif
28402840

28412841
position = 0;
2842-
#if defined(SMP) && !defined(USE_OPENMP)
2842+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
28432843
LOCK_COMMAND(&alloc_lock);
28442844
#endif
28452845
while ((position < NUM_BUFFERS) && (memory[position].addr != free_area))
@@ -2855,7 +2855,7 @@ void blas_memory_free(void *free_area){
28552855
WMB;
28562856

28572857
memory[position].used = 0;
2858-
#if defined(SMP) && !defined(USE_OPENMP)
2858+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
28592859
UNLOCK_COMMAND(&alloc_lock);
28602860
#endif
28612861

@@ -2872,7 +2872,7 @@ void blas_memory_free(void *free_area){
28722872
for (position = 0; position < NUM_BUFFERS; position++)
28732873
printf("%4ld %p : %d\n", position, memory[position].addr, memory[position].used);
28742874
#endif
2875-
#if defined(SMP) && !defined(USE_OPENMP)
2875+
#if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
28762876
UNLOCK_COMMAND(&alloc_lock);
28772877
#endif
28782878
return;
@@ -2924,7 +2924,7 @@ void blas_shutdown(void){
29242924

29252925
#if defined(OS_LINUX) && !defined(NO_WARMUP)
29262926

2927-
#ifdef SMP
2927+
#if defined(SMP) || defined(USE_LOCKING)
29282928
#if defined(USE_PTHREAD_LOCK)
29292929
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
29302930
#elif defined(USE_PTHREAD_SPINLOCK)
@@ -2949,7 +2949,7 @@ static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
29492949
if (hot_alloc != 2) {
29502950
#endif
29512951

2952-
#ifdef SMP
2952+
#if defined(SMP) || defined(USE_LOCKING)
29532953
LOCK_COMMAND(&init_lock);
29542954
#endif
29552955

@@ -2959,7 +2959,7 @@ static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
29592959
size -= PAGESIZE;
29602960
}
29612961

2962-
#ifdef SMP
2962+
#if defined(SMP) || defined(USE_LOCKING)
29632963
UNLOCK_COMMAND(&init_lock);
29642964
#endif
29652965

0 commit comments

Comments
 (0)