Skip to content

Commit cb19d6a

Browse files
committed
Use struct-wrapped C11 atomics with explicit ordering
1 parent 9977df7 commit cb19d6a

38 files changed

+276
-101
lines changed

ompi/communicator/comm_cid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#include "pmix.h"
5959

6060
/* for use when we don't have a PMIx that supports CID generation */
61-
opal_atomic_int64_t ompi_comm_next_base_cid = 1;
61+
opal_atomic_int64_t ompi_comm_next_base_cid = OPAL_ATOMIC_INIT(1);
6262

6363
/* A macro comparing two CIDs */
6464
#define OMPI_COMM_CID_IS_LOWER(comm1,comm2) ( ((comm1)->c_index < (comm2)->c_index)? 1:0)

ompi/communicator/comm_request.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#include "comm_request.h"
21+
#include "opal/sys/atomic.h"
2122

2223
#include "opal/class/opal_free_list.h"
2324
#include "opal/include/opal/sys/atomic.h"
@@ -109,7 +110,7 @@ int ompi_comm_request_schedule_append_w_flags(ompi_comm_request_t *request, ompi
109110
static int ompi_comm_request_progress (void)
110111
{
111112
ompi_comm_request_t *request, *next;
112-
static opal_atomic_int32_t progressing = 0;
113+
static opal_atomic_int32_t progressing = OPAL_ATOMIC_INIT(0);
113114
int completed = 0;
114115

115116
/* don't allow re-entry */
@@ -175,7 +176,7 @@ static int ompi_comm_request_progress (void)
175176
}
176177

177178
opal_mutex_unlock (&ompi_comm_request_mutex);
178-
progressing = 0;
179+
opal_atomic_store_32(&progressing, 0);
179180

180181
return completed;
181182
}

ompi/communicator/ft/comm_ft_detector.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "opal/mca/threads/threads.h"
1919

2020
#include "ompi/runtime/params.h"
21+
#include "opal/sys/atomic.h"
2122
#include "ompi/communicator/communicator.h"
2223
#include "ompi/mca/pml/pml.h"
2324
#include "ompi/mca/bml/bml.h"
@@ -94,7 +95,7 @@ static opal_event_base_t* fd_event_base = NULL;
9495
static void fd_event_cb(int fd, short flags, void* pdetector);
9596

9697
static bool comm_detector_use_thread = false;
97-
static opal_atomic_int32_t fd_thread_active = 0;
98+
static opal_atomic_int32_t fd_thread_active = OPAL_ATOMIC_INIT(0);
9899
static opal_thread_t fd_thread;
99100
static void* fd_progress(opal_object_t* obj);
100101

@@ -168,8 +169,9 @@ int ompi_comm_failure_detector_init(void) {
168169
fd_thread.t_arg = NULL;
169170
ret = opal_thread_start(&fd_thread);
170171
if( OPAL_SUCCESS != ret ) goto cleanup;
171-
while( 0 == fd_thread_active ); /* wait for the fd thread initialization */
172-
if( 0 > fd_thread_active ) goto cleanup;
172+
while (0 == opal_atomic_load_32(&fd_thread_active)) { /* wait for the fd thread initialization */
173+
}
174+
if (0 > opal_atomic_load_32(&fd_thread_active)) goto cleanup;
173175
}
174176

175177
return OMPI_SUCCESS;
@@ -218,18 +220,19 @@ int ompi_comm_failure_detector_finalize(void) {
218220
#endif
219221
while( observing == detector->hb_observing ) {
220222
/* If observed process changed, recheck if local*/
221-
if( !(0 < fd_thread_active) )
223+
if (!(0 < opal_atomic_load_32(&fd_thread_active)))
222224
{
223225
opal_progress();
224226
}
225227
}
226228
}
227229

228-
if( 0 < fd_thread_active ) {
230+
if (0 < opal_atomic_load_32(&fd_thread_active)) {
229231
void* tret;
230232
/* this is not a race condition. Accesses are serialized, we use the
231233
* atomic for the mfence part of it. */
232-
OPAL_THREAD_ADD_FETCH32(&fd_thread_active, -fd_thread_active);
234+
int32_t active = opal_atomic_load_32(&fd_thread_active);
235+
OPAL_THREAD_ADD_FETCH32(&fd_thread_active, -active);
233236
opal_event_base_loopbreak(fd_event_base);
234237
opal_thread_join(&fd_thread, &tret);
235238
}
@@ -587,9 +590,10 @@ void* fd_progress(opal_object_t* obj) {
587590
return OPAL_THREAD_CANCELLED;
588591
}
589592
OPAL_THREAD_ADD_FETCH32(&fd_thread_active, 1);
590-
while( 1 == fd_thread_active ); /* wait for init stage 2: start_detector */
593+
while (1 == opal_atomic_load_32(&fd_thread_active)) { /* wait for init stage 2: start_detector */
594+
}
591595
ret = MCA_PML_CALL(irecv(NULL, 0, MPI_BYTE, 0, MCA_COLL_BASE_TAG_FT_END, &ompi_mpi_comm_self.comm, &req));
592-
while( fd_thread_active ) {
596+
while (opal_atomic_load_32(&fd_thread_active)) {
593597
opal_event_loop(fd_event_base, OPAL_EVLOOP_ONCE);
594598
#if 0
595599
/* This test disabled because rdma emulation over TCP would not work without

ompi/communicator/ft/comm_ft_reliable_bcast.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ompi/mca/pml/pml.h"
1919
#include "ompi/mca/bml/bml.h"
2020
#include "ompi/mca/bml/base/base.h"
21+
#include "opal/sys/atomic.h"
2122
#include "ompi/mca/coll/base/base.h"
2223
#include "ompi/mca/coll/base/coll_tags.h"
2324

@@ -208,7 +209,7 @@ static void ompi_comm_rbcast_bml_recv_cb(
208209
* that we keep receiving messages after we deregistered the type.
209210
* Any other time, this is indicative of a problem.
210211
*/
211-
assert(ompi_mpi_state >= OMPI_MPI_STATE_FINALIZE_STARTED);
212+
assert(opal_atomic_load_32(&ompi_mpi_state) >= OMPI_MPI_STATE_FINALIZE_STARTED);
212213
}
213214
}
214215

ompi/errhandler/errhandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ extern opal_atomic_int32_t ompi_instance_count;
213213
*/
214214
#define OMPI_ERR_INIT_FINALIZE(name) \
215215
{ \
216-
if (OPAL_UNLIKELY(0 == ompi_instance_count)) { \
216+
if (OPAL_UNLIKELY(0 == opal_atomic_load_32(&ompi_instance_count))) { \
217217
ompi_errhandler_invoke(NULL, NULL, -1, \
218218
ompi_errcode_get_mpi_code(MPI_ERR_ARG), \
219219
name); \

ompi/errhandler/errhandler_invoke.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int ompi_errhandler_invoke(ompi_errhandler_t *errhandler, void *mpi_object,
4747
/* If we got no errorhandler, then route the error to the appropriate
4848
* predefined error handler */
4949
if (NULL == errhandler) {
50-
int32_t state = ompi_mpi_state;
50+
int32_t state = opal_atomic_load_32(&ompi_mpi_state);
5151
if (state >= OMPI_MPI_STATE_INIT_COMPLETED &&
5252
state < OMPI_MPI_STATE_FINALIZE_PAST_COMM_SELF_DESTRUCT) {
5353
comm = (ompi_mpi_compat_mpi3)? &ompi_mpi_comm_world.comm: &ompi_mpi_comm_self.comm;

ompi/errhandler/errhandler_predefined.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ void ompi_mpi_errors_return_instance_handler (struct ompi_instance_t **instance,
266266
static void out(char *str, char *arg)
267267
{
268268
if (ompi_rte_initialized &&
269-
ompi_mpi_state < OMPI_MPI_STATE_FINALIZE_PAST_COMM_SELF_DESTRUCT) {
269+
opal_atomic_load_32(&ompi_mpi_state) < OMPI_MPI_STATE_FINALIZE_PAST_COMM_SELF_DESTRUCT) {
270270
if (NULL != arg) {
271271
opal_output(0, str, arg);
272272
} else {
@@ -400,7 +400,7 @@ static void backend_abort_no_aggregate(int fatal, char *type,
400400
{
401401
char *arg;
402402

403-
int32_t state = ompi_mpi_state;
403+
int32_t state = opal_atomic_load_32(&ompi_mpi_state);
404404
assert(state < OMPI_MPI_STATE_INIT_COMPLETED ||
405405
state >= OMPI_MPI_STATE_FINALIZE_PAST_COMM_SELF_DESTRUCT);
406406

ompi/instance/instance.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ enum {
9090
OMPI_INSTANCE_FINALIZING = -2,
9191
};
9292

93-
opal_atomic_int32_t ompi_instance_count = 0;
93+
opal_atomic_int32_t ompi_instance_count = OPAL_ATOMIC_INIT(0);
9494

9595
static const char *ompi_instance_builtin_psets[] = {
9696
"mpi://WORLD",

ompi/mca/coll/hcoll/coll_hcoll_ops.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
#include "hcoll/api/hcoll_constants.h"
1717
#include "coll_hcoll_dtypes.h"
1818
#include "hcoll/api/hcoll_dte.h"
19+
#include "opal/sys/atomic.h"
1920
int mca_coll_hcoll_barrier(struct ompi_communicator_t *comm,
2021
mca_coll_base_module_t *module){
2122
int rc;
2223
mca_coll_hcoll_module_t *hcoll_module = (mca_coll_hcoll_module_t*)module;
2324
HCOL_VERBOSE(20,"RUNNING HCOL BARRIER");
2425

25-
if (OPAL_UNLIKELY(ompi_mpi_state >= OMPI_MPI_STATE_FINALIZE_STARTED)) {
26+
if (OPAL_UNLIKELY(opal_atomic_load_32(&ompi_mpi_state) >= OMPI_MPI_STATE_FINALIZE_STARTED)) {
2627
HCOL_VERBOSE(5, "In finalize, reverting to previous barrier");
2728
goto orig_barrier;
2829
}

ompi/mca/common/monitoring/common_monitoring.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
/*** Monitoring specific variables ***/
4141
/* Keep tracks of how many components are currently using the common part */
42-
static opal_atomic_int32_t mca_common_monitoring_hold = 0;
42+
static opal_atomic_int32_t mca_common_monitoring_hold = OPAL_ATOMIC_INIT(0);
4343
/* Output parameters */
4444
int mca_common_monitoring_output_stream_id = -1;
4545
static opal_output_stream_t mca_common_monitoring_output_stream_obj = {

0 commit comments

Comments
 (0)