Skip to content
This repository was archived by the owner on Sep 30, 2022. It is now read-only.

Commit 170161e

Browse files
committed
opal/thread: clean up and add additional OPAL_THREAD macros
This commit expands the OPAL_THREAD macros to include 32- and 64-bit atomic swap. Additionally, macro declararations have been updated to include both OPAL_THREAD_* and OPAL_ATOMIC_*. Before this commit the former was used with add and the later with cmpset. This commit remove the OMPI_THREAD_MULTIPLE macros around the OPAL_THREAD_* function. This should not make a difference in performance as opal_uses_threads is #defined to false when OMPI_THREAD_MULTIPLE is 0. The compiler should optimize out the check on opal_using_threads. Signed-off-by: Nathan Hjelm <[email protected]> (cherry picked from commit aac6112) Signed-off-by: Nathan Hjelm <[email protected]>
1 parent 1c3965b commit 170161e

File tree

8 files changed

+93
-108
lines changed

8 files changed

+93
-108
lines changed

ompi/mca/osc/sm/osc_sm_active_target.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
22
/*
33
* Copyright (c) 2012 Sandia National Laboratories. All rights reserved.
4-
* Copyright (c) 2014-2015 Los Alamos National Security, LLC. All rights
4+
* Copyright (c) 2014-2016 Los Alamos National Security, LLC. All rights
55
* reserved.
66
* Copyright (c) 2014 The University of Tennessee and The University
77
* of Tennessee Research Foundation. All rights
@@ -133,7 +133,7 @@ ompi_osc_sm_start(struct ompi_group_t *group,
133133

134134
OBJ_RETAIN(group);
135135

136-
if (!OPAL_ATOMIC_CMPSET(&module->start_group, NULL, group)) {
136+
if (!OPAL_ATOMIC_CMPSET_PTR(&module->start_group, NULL, group)) {
137137
OBJ_RELEASE(group);
138138
return OMPI_ERR_RMA_SYNC;
139139
}
@@ -185,7 +185,7 @@ ompi_osc_sm_complete(struct ompi_win_t *win)
185185
opal_atomic_mb();
186186

187187
group = module->start_group;
188-
if (NULL == group || !OPAL_ATOMIC_CMPSET(&module->start_group, group, NULL)) {
188+
if (NULL == group || !OPAL_ATOMIC_CMPSET_PTR(&module->start_group, group, NULL)) {
189189
return OMPI_ERR_RMA_SYNC;
190190
}
191191

ompi/mpi/c/comm_get_errhandler.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2007-2009 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -64,7 +67,7 @@ int MPI_Comm_get_errhandler(MPI_Comm comm, MPI_Errhandler *errhandler)
6467
error_handler became atomic. */
6568
do {
6669
tmp = comm->error_handler;
67-
} while (!OPAL_ATOMIC_CMPSET(&(comm->error_handler), tmp, tmp));
70+
} while (!OPAL_ATOMIC_CMPSET_PTR(&(comm->error_handler), tmp, tmp));
6871

6972
/* Retain the errhandler, corresponding to object refcount decrease
7073
in errhandler_free.c. */

ompi/mpi/c/comm_set_errhandler.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -11,6 +12,8 @@
1112
* All rights reserved.
1213
* Copyright (c) 2015 Research Organization for Information Science
1314
* and Technology (RIST). All rights reserved.
15+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
16+
* reserved.
1417
* $COPYRIGHT$
1518
*
1619
* Additional copyrights may follow
@@ -67,9 +70,7 @@ int MPI_Comm_set_errhandler(MPI_Comm comm, MPI_Errhandler errhandler)
6770
/* Ditch the old errhandler, and decrement its refcount. On 64
6871
bits environments we have to make sure the reading of the
6972
error_handler became atomic. */
70-
do {
71-
tmp = comm->error_handler;
72-
} while (!OPAL_ATOMIC_CMPSET(&(comm->error_handler), tmp, errhandler));
73+
tmp = OPAL_ATOMIC_SWAP_PTR(&comm->error_handler, errhandler);
7374
OBJ_RELEASE(tmp);
7475

7576
/* All done */

ompi/mpi/c/file_get_errhandler.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2008 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -63,7 +66,7 @@ int MPI_File_get_errhandler( MPI_File file, MPI_Errhandler *errhandler)
6366
error_handler became atomic. */
6467
do {
6568
tmp = file->error_handler;
66-
} while (!OPAL_ATOMIC_CMPSET(&(file->error_handler), tmp, tmp));
69+
} while (!OPAL_ATOMIC_CMPSET_PTR(&(file->error_handler), tmp, tmp));
6770

6871
/* Retain the errhandler, corresponding to object refcount
6972
decrease in errhandler_free.c. */

ompi/mpi/c/file_set_errhandler.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2008 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -66,9 +69,7 @@ int MPI_File_set_errhandler( MPI_File file, MPI_Errhandler errhandler)
6669
/* Ditch the old errhandler, and decrement its refcount. On 64
6770
bits environments we have to make sure the reading of the
6871
error_handler became atomic. */
69-
do {
70-
tmp = file->error_handler;
71-
} while (!OPAL_ATOMIC_CMPSET(&(file->error_handler), tmp, errhandler));
72+
tmp = OPAL_ATOMIC_SWAP_PTR (&file->error_handler, errhandler);
7273
OBJ_RELEASE(tmp);
7374

7475
/* All done */

ompi/mpi/c/win_get_errhandler.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -56,7 +59,7 @@ int MPI_Win_get_errhandler(MPI_Win win, MPI_Errhandler *errhandler)
5659
error_handler became atomic. */
5760
do {
5861
tmp = win->error_handler;
59-
} while (!OPAL_ATOMIC_CMPSET(&(win->error_handler), tmp, tmp));
62+
} while (!OPAL_ATOMIC_CMPSET_PTR(&(win->error_handler), tmp, tmp));
6063

6164
/* Retain the errhandler, corresponding to object refcount
6265
decrease in errhandler_free.c. */

ompi/mpi/c/win_set_errhandler.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -12,6 +13,8 @@
1213
* Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2015 Research Organization for Information Science
1415
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1518
* $COPYRIGHT$
1619
*
1720
* Additional copyrights may follow
@@ -61,9 +64,7 @@ int MPI_Win_set_errhandler(MPI_Win win, MPI_Errhandler errhandler)
6164
/* Ditch the old errhandler, and decrement its refcount. On 64
6265
bits environments we have to make sure the reading of the
6366
error_handler became atomic. */
64-
do {
65-
tmp = win->error_handler;
66-
} while (!OPAL_ATOMIC_CMPSET(&(win->error_handler), tmp, errhandler));
67+
tmp = OPAL_ATOMIC_SWAP_PTR(&win->error_handler, errhandler);
6768
OBJ_RELEASE(tmp);
6869

6970
/* All done */

opal/threads/thread_usage.h

Lines changed: 66 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define OPAL_THREAD_USAGE_H
2727

2828
#include "opal/sys/atomic.h"
29+
#include "opal/prefetch.h"
2930
#include "opal_config.h"
3031

3132
/**
@@ -101,113 +102,85 @@ static inline bool opal_set_using_threads(bool have)
101102
* Use an atomic operation for increment/decrement if opal_using_threads()
102103
* indicates that threads are in use by the application or library.
103104
*/
105+
#define OPAL_THREAD_DEFINE_ATOMIC_ADD(type, suffix) \
106+
static inline type opal_thread_add_ ## suffix (volatile type *addr, type delta) \
107+
{ \
108+
if (OPAL_UNLIKELY(opal_using_threads())) { \
109+
return opal_atomic_add_ ## suffix (addr, delta); \
110+
} \
111+
\
112+
return (*addr += delta); \
113+
}
104114

105-
#if OMPI_ENABLE_THREAD_MULTIPLE
106-
static inline int32_t
107-
OPAL_THREAD_ADD32(volatile int32_t *addr, int delta)
108-
{
109-
int32_t ret;
110-
111-
if (opal_using_threads()) {
112-
ret = opal_atomic_add_32(addr, delta);
113-
} else {
114-
ret = (*addr += delta);
115-
}
116-
117-
return ret;
115+
#define OPAL_THREAD_DEFINE_ATOMIC_CMPSET(type, addr_type, suffix) \
116+
static inline bool opal_thread_cmpset_bool_ ## suffix (volatile addr_type *addr, type compare, type value) \
117+
{ \
118+
if (OPAL_UNLIKELY(opal_using_threads())) { \
119+
return opal_atomic_cmpset_ ## suffix ((volatile type *) addr, compare, value); \
120+
} \
121+
\
122+
if ((type) *addr == compare) { \
123+
((type *) addr)[0] = value; \
124+
return true; \
125+
} \
126+
\
127+
return false; \
118128
}
119-
#else
120-
static inline int32_t
121-
OPAL_THREAD_ADD32(volatile int32_t *addr, int delta)
122-
{
123-
int32_t ret;
124-
ret = (*addr += delta);
125-
return ret;
129+
130+
#define OPAL_THREAD_DEFINE_ATOMIC_SWAP(type, addr_type, suffix) \
131+
static inline type opal_thread_swap_ ## suffix (volatile addr_type *ptr, type newvalue) \
132+
{ \
133+
if (opal_using_threads ()) { \
134+
return opal_atomic_swap_ ## suffix ((volatile type *) ptr, newvalue); \
135+
} \
136+
\
137+
type old = ((type *) ptr)[0]; \
138+
((type *) ptr)[0] = newvalue; \
139+
\
140+
return old; \
126141
}
127-
#endif
128142

129-
#if OPAL_HAVE_ATOMIC_MATH_64
130-
#if OMPI_ENABLE_THREAD_MULTIPLE
131-
static inline int64_t
132-
OPAL_THREAD_ADD64(volatile int64_t *addr, int delta)
133-
{
134-
int64_t ret;
143+
OPAL_THREAD_DEFINE_ATOMIC_ADD(int32_t, 32)
144+
OPAL_THREAD_DEFINE_ATOMIC_ADD(size_t, size_t)
145+
OPAL_THREAD_DEFINE_ATOMIC_CMPSET(int32_t, int32_t, 32)
146+
OPAL_THREAD_DEFINE_ATOMIC_CMPSET(void *, intptr_t, ptr)
147+
OPAL_THREAD_DEFINE_ATOMIC_SWAP(int32_t, int32_t, 32)
148+
OPAL_THREAD_DEFINE_ATOMIC_SWAP(void *, intptr_t, ptr)
135149

136-
if (opal_using_threads()) {
137-
ret = opal_atomic_add_64(addr, delta);
138-
} else {
139-
ret = (*addr += delta);
140-
}
150+
#define OPAL_THREAD_ADD32 opal_thread_add_32
151+
#define OPAL_ATOMIC_ADD32 opal_thread_add_32
141152

142-
return ret;
143-
}
144-
#else
145-
static inline int64_t
146-
OPAL_THREAD_ADD64(volatile int64_t *addr, int delta)
147-
{
148-
int64_t ret;
149-
ret = (*addr += delta);
150-
return ret;
151-
}
152-
#endif
153-
#endif
153+
#define OPAL_THREAD_ADD_SIZE_T opal_thread_add_size_t
154+
#define OPAL_ATOMIC_ADD_SIZE_T opal_thread_add_size_t
154155

155-
#if OMPI_ENABLE_THREAD_MULTIPLE
156-
static inline size_t
157-
OPAL_THREAD_ADD_SIZE_T(volatile size_t *addr, int delta)
158-
{
159-
size_t ret;
156+
#define OPAL_THREAD_CMPSET_32 opal_thread_cmpset_bool_32
157+
#define OPAL_ATOMIC_CMPSET_32 opal_thread_cmpset_bool_32
160158

161-
if (opal_using_threads()) {
162-
ret = opal_atomic_add_size_t(addr, delta);
163-
} else {
164-
ret = (*addr += delta);
165-
}
159+
#define OPAL_THREAD_CMPSET_PTR(x, y, z) opal_thread_cmpset_bool_ptr ((volatile intptr_t *) x, (void *) y, (void *) z)
160+
#define OPAL_ATOMIC_CMPSET_PTR OPAL_THREAD_CMPSET_PTR
166161

167-
return ret;
168-
}
169-
#else
170-
static inline size_t
171-
OPAL_THREAD_ADD_SIZE_T(volatile size_t *addr, int delta)
172-
{
173-
size_t ret;
174-
ret = (*addr += delta);
175-
return ret;
176-
}
177-
#endif
162+
#define OPAL_THREAD_SWAP_32 opal_thread_swap_32
163+
#define OPAL_ATOMIC_SWAP_32 opal_thread_swap_32
178164

179-
/* BWB: FIX ME: remove if possible */
180-
#define OPAL_CMPSET(x, y, z) ((*(x) == (y)) ? ((*(x) = (z)), 1) : 0)
165+
#define OPAL_THREAD_SWAP_PTR(x, y) opal_thread_swap_ptr ((volatile intptr_t *) x, (void *) y)
166+
#define OPAL_ATOMIC_SWAP_PTR OPAL_THREAD_SWAP_PTR
181167

182-
#if OPAL_HAVE_ATOMIC_CMPSET_32
183-
#define OPAL_ATOMIC_CMPSET_32(x, y, z) \
184-
(opal_using_threads() ? opal_atomic_cmpset_32(x, y, z) : OPAL_CMPSET(x, y, z))
185-
#endif
186-
#if OPAL_HAVE_ATOMIC_CMPSET_64
187-
#define OPAL_ATOMIC_CMPSET_64(x, y, z) \
188-
(opal_using_threads() ? opal_atomic_cmpset_64(x, y, z) : OPAL_CMPSET(x, y, z))
189-
#endif
190-
#if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
191-
#define OPAL_ATOMIC_CMPSET(x, y, z) \
192-
(opal_using_threads() ? opal_atomic_cmpset(x, y, z) : OPAL_CMPSET(x, y, z))
193-
#endif
194-
#if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
195-
#define OPAL_ATOMIC_CMPSET_PTR(x, y, z) \
196-
(opal_using_threads() ? opal_atomic_cmpset_ptr(x, y, z) : OPAL_CMPSET(x, y, z))
197-
#endif
168+
/* define 64-bit macros is 64-bit atomic math is available */
169+
#if OPAL_HAVE_ATOMIC_MATH_64
198170

199-
static inline void *opal_thread_swap_ptr (volatile void *ptr, void *newvalue)
200-
{
201-
if (opal_using_threads ()) {
202-
return opal_atomic_swap_ptr (ptr, newvalue);
203-
}
171+
OPAL_THREAD_DEFINE_ATOMIC_ADD(int64_t, 64)
172+
OPAL_THREAD_DEFINE_ATOMIC_CMPSET(int64_t, int64_t, 64)
173+
OPAL_THREAD_DEFINE_ATOMIC_SWAP(int64_t, int64_t, 64)
204174

205-
void *old = ((void **) ptr)[0];
206-
((void **) ptr)[0] = newvalue;
175+
#define OPAL_THREAD_ADD64 opal_thread_add_64
176+
#define OPAL_ATOMIC_ADD64 opal_thread_add_64
207177

208-
return old;
209-
}
178+
#define OPAL_THREAD_CMPSET_64 opal_thread_cmpset_bool_64
179+
#define OPAL_ATOMIC_CMPSET_64 opal_thread_cmpset_bool_64
210180

211-
#define OPAL_ATOMIC_SWAP_PTR(x, y) opal_thread_swap_ptr (x, y)
181+
#define OPAL_THREAD_SWAP_64 opal_thread_swap_64
182+
#define OPAL_ATOMIC_SWAP_64 opal_thread_swap_64
183+
184+
#endif
212185

213186
#endif /* !defined(OPAL_THREAD_USAGE_H) */

0 commit comments

Comments
 (0)