Skip to content

Commit 143a93f

Browse files
committed
opal/sync: remove usage of OPAL_ENABLE_MULTI_THREADS
The OPAL_ENABLE_MULTI_THREADS macro is always defined as 1. This was causing us to always use the multi-thread path for synchronization objects. The code has been updated to use the opal_using_threads() function. When MPI_THREAD_MULTIPLE support is disabled at build time (2.x only) this function is a macro evaluating to false so the compiler will optimize out the MT-path in this case. The OPAL_ATOMIC_ADD_32 macro has been removed and replaced by the existing OPAL_THREAD_ADD32 macro. Signed-off-by: Nathan Hjelm <[email protected]>
1 parent 679a66c commit 143a93f

File tree

4 files changed

+49
-45
lines changed

4 files changed

+49
-45
lines changed

ompi/request/request.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ static inline int ompi_request_complete(ompi_request_t* request, bool with_signa
416416

417417
if( OPAL_LIKELY(with_signal) ) {
418418
if(!OPAL_ATOMIC_CMPSET_PTR(&request->req_complete, REQUEST_PENDING, REQUEST_COMPLETED)) {
419-
ompi_wait_sync_t *tmp_sync = (ompi_wait_sync_t *) OPAL_ATOMIC_SWP_PTR(&request->req_complete,
420-
REQUEST_COMPLETED);
419+
ompi_wait_sync_t *tmp_sync = (ompi_wait_sync_t *) OPAL_ATOMIC_SWAP_PTR(&request->req_complete,
420+
REQUEST_COMPLETED);
421421
/* In the case where another thread concurrently changed the request to REQUEST_PENDING */
422422
if( REQUEST_PENDING != tmp_sync )
423423
wait_sync_update(tmp_sync, 1, request->req_status.MPI_ERROR);

opal/threads/mutex.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,20 @@ OPAL_THREAD_ADD_SIZE_T(volatile size_t *addr, int delta)
324324
#endif
325325

326326

327+
static inline void *opal_thread_swap_ptr (volatile void *ptr, void *newvalue)
328+
{
329+
if (opal_using_threads ()) {
330+
return opal_atomic_swap_ptr (ptr, newvalue);
331+
}
332+
333+
void *old = ((void **) ptr)[0];
334+
((void **) ptr)[0] = newvalue;
335+
336+
return old;
337+
}
338+
339+
#define OPAL_ATOMIC_SWAP_PTR(x, y) opal_thread_swap_ptr (x, y)
340+
327341
END_C_DECLS
328342

329343
#endif /* OPAL_MUTEX_H */

opal/threads/wait_sync.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright (c) 2014-2016 The University of Tennessee and The University
44
* of Tennessee Research Foundation. All rights
55
* reserved.
6+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
7+
* reserved.
68
* $COPYRIGHT$
79
*
810
* Additional copyrights may follow
@@ -21,15 +23,6 @@ static ompi_wait_sync_t* wait_sync_list = NULL;
2123
pthread_mutex_unlock( &(who)->lock); \
2224
} while(0)
2325

24-
25-
int sync_wait_st(ompi_wait_sync_t *sync)
26-
{
27-
while(sync->count > 0) {
28-
opal_progress();
29-
}
30-
return (0 == sync->status) ? OPAL_SUCCESS : OPAL_ERROR;
31-
}
32-
3326
int sync_wait_mt(ompi_wait_sync_t *sync)
3427
{
3528
if(sync->count <= 0)

opal/threads/wait_sync.h

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright (c) 2014-2016 The University of Tennessee and The University
44
* of Tennessee Research Foundation. All rights
55
* reserved.
6+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
7+
* reserved.
68
* $COPYRIGHT$
79
*
810
* Additional copyrights may follow
@@ -27,50 +29,44 @@ typedef struct ompi_wait_sync_t {
2729
#define REQUEST_PENDING (void*)0L
2830
#define REQUEST_COMPLETED (void*)1L
2931

30-
#if OPAL_ENABLE_MULTI_THREADS
31-
32-
#define OPAL_ATOMIC_ADD_32(a,b) opal_atomic_add_32(a,b)
33-
#define OPAL_ATOMIC_SWP_PTR(a,b) opal_atomic_swap_ptr(a,b)
34-
#define SYNC_WAIT(sync) sync_wait_mt(sync)
35-
#define PTHREAD_COND_INIT(a,b) pthread_cond_init(a,b)
36-
#define PTHREAD_MUTEX_INIT(a,b) pthread_mutex_init(a,b)
32+
#define SYNC_WAIT(sync) (opal_using_threads() ? sync_wait_mt (sync) : sync_wait_st (sync))
33+
#define PTHREAD_COND_INIT(a,b) (opal_using_threads() ? pthread_cond_init (a,b) : 0)
34+
#define PTHREAD_MUTEX_INIT(a,b) (opal_using_threads() ? pthread_mutex_init (a,b) : 0)
3735

3836
#define WAIT_SYNC_RELEASE(sync) \
39-
do { \
37+
if (opal_using_threads()) { \
4038
pthread_cond_destroy(&(sync)->condition); \
4139
pthread_mutex_destroy(&(sync)->lock); \
42-
} while(0)
40+
}
4341

4442
#define WAIT_SYNC_SIGNAL(sync) \
45-
do { \
43+
if (opal_using_threads()) { \
4644
pthread_mutex_lock(&(sync->lock)); \
4745
pthread_cond_signal(&sync->condition); \
4846
pthread_mutex_unlock(&(sync->lock)); \
49-
} while(0)
50-
51-
#else
47+
}
5248

53-
#define OPAL_ATOMIC_ADD_32(a,b) (*(a) += (b))
54-
#define OPAL_ATOMIC_SWP_PTR(a,b) *(a) = (b)
55-
#define PTHREAD_COND_INIT(a,b)
56-
#define PTHREAD_MUTEX_INIT(a,b)
57-
#define SYNC_WAIT(sync) sync_wait_st(sync)
58-
#define WAIT_SYNC_RELEASE(sync)
59-
#define WAIT_SYNC_SIGNAL(sync)
49+
OPAL_DECLSPEC int sync_wait_mt(ompi_wait_sync_t *sync);
50+
static inline int sync_wait_st (ompi_wait_sync_t *sync)
51+
{
52+
while (sync->count > 0) {
53+
opal_progress();
54+
}
6055

61-
#endif /* OPAL_ENABLE_MULTI_THREADS */
56+
return sync->status;
57+
}
6258

63-
OPAL_DECLSPEC int sync_wait_mt(ompi_wait_sync_t *sync);
64-
OPAL_DECLSPEC int sync_wait_st(ompi_wait_sync_t *sync);
6559

66-
#define WAIT_SYNC_INIT(sync,c) \
67-
do { \
68-
(sync)->count = c; \
69-
(sync)->next = NULL; \
70-
(sync)->prev = NULL; \
71-
(sync)->status = 0; \
72-
PTHREAD_COND_INIT(&(sync)->condition, NULL); \
73-
PTHREAD_MUTEX_INIT(&(sync)->lock, NULL); \
60+
#define WAIT_SYNC_INIT(sync,c) \
61+
do { \
62+
(sync)->count = c; \
63+
(sync)->next = NULL; \
64+
(sync)->prev = NULL; \
65+
(sync)->status = 0; \
66+
if (opal_using_threads()) { \
67+
PTHREAD_COND_INIT(&(sync)->condition, NULL); \
68+
PTHREAD_MUTEX_INIT(&(sync)->lock, NULL); \
69+
} \
7470
} while(0)
7571

7672
/**
@@ -82,12 +78,13 @@ OPAL_DECLSPEC int sync_wait_st(ompi_wait_sync_t *sync);
8278
static inline void wait_sync_update(ompi_wait_sync_t *sync, int updates, int status)
8379
{
8480
if( OPAL_LIKELY(OPAL_SUCCESS == status) ) {
85-
if( 0 != (OPAL_ATOMIC_ADD_32(&sync->count, -updates)) ) {
81+
if( 0 != (OPAL_THREAD_ADD32(&sync->count, -updates)) ) {
8682
return;
8783
}
8884
} else {
89-
OPAL_ATOMIC_CMPSET_32(&(sync->count), 0, 0);
90-
sync->status = -1;
85+
/* this is an error path so just use the atomic */
86+
opal_atomic_swap_32 (&sync->count, 0);
87+
sync->status = OPAL_ERROR;
9188
}
9289
WAIT_SYNC_SIGNAL(sync);
9390
}

0 commit comments

Comments
 (0)