Skip to content

Commit 82b1eb4

Browse files
authored
Merge pull request #10340 from hoopoepg/topic/fixed-race-in-datatype-init-v4.1
PML/UCX/DATATYPE: fixed potential race - v4.1
2 parents 8a1f456 + 32ff5f8 commit 82b1eb4

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

ompi/mca/pml/ucx/pml_ucx_datatype.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <inttypes.h>
1717
#include <math.h>
18+
#include <pthread.h>
1819

1920
#ifdef HAVE_UCP_REQUEST_PARAM_T
2021
#define PML_UCX_DATATYPE_SET_VALUE(_datatype, _val) \
@@ -205,11 +206,19 @@ pml_ucx_datatype_t *mca_pml_ucx_init_nbx_datatype(ompi_datatype_t *datatype,
205206

206207
ucp_datatype_t mca_pml_ucx_init_datatype(ompi_datatype_t *datatype)
207208
{
209+
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
208210
size_t size = 0; /* init to suppress compiler warning */
209211
ucp_datatype_t ucp_datatype;
210212
ucs_status_t status;
211213
int ret;
212214

215+
pthread_mutex_lock(&lock);
216+
217+
if (datatype->pml_data != PML_UCX_DATATYPE_INVALID) {
218+
/* datatype is already initialized in concurrent thread */
219+
goto out;
220+
}
221+
213222
if (mca_pml_ucx_datatype_is_contig(datatype)) {
214223
ompi_datatype_type_size(datatype, &size);
215224
PML_UCX_ASSERT(size > 0);
@@ -259,7 +268,10 @@ ucp_datatype_t mca_pml_ucx_init_datatype(ompi_datatype_t *datatype)
259268
datatype->pml_data = ucp_datatype;
260269
#endif
261270

262-
return ucp_datatype;
271+
out:
272+
pthread_mutex_unlock(&lock);
273+
274+
return mca_pml_ucx_from_ompi_datatype(datatype);
263275
}
264276

265277
static void mca_pml_ucx_convertor_construct(mca_pml_ucx_convertor_t *convertor)

ompi/mca/pml/ucx/pml_ucx_datatype.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,25 @@ OBJ_CLASS_DECLARATION(mca_pml_ucx_convertor_t);
4444

4545

4646
__opal_attribute_always_inline__
47-
static inline ucp_datatype_t mca_pml_ucx_get_datatype(ompi_datatype_t *datatype)
47+
static inline ucp_datatype_t
48+
mca_pml_ucx_from_ompi_datatype(ompi_datatype_t *datatype)
4849
{
4950
#ifdef HAVE_UCP_REQUEST_PARAM_T
50-
pml_ucx_datatype_t *ucp_type = (pml_ucx_datatype_t*)datatype->pml_data;
51-
52-
if (OPAL_LIKELY(ucp_type != PML_UCX_DATATYPE_INVALID)) {
53-
return ucp_type->datatype;
54-
}
51+
return ((pml_ucx_datatype_t*)datatype->pml_data)->datatype;
5552
#else
56-
ucp_datatype_t ucp_type = datatype->pml_data;
53+
return (ucp_datatype_t)datatype->pml_data;
54+
#endif
55+
}
5756

58-
if (OPAL_LIKELY(ucp_type != PML_UCX_DATATYPE_INVALID)) {
59-
return ucp_type;
57+
58+
__opal_attribute_always_inline__
59+
static inline ucp_datatype_t mca_pml_ucx_get_datatype(ompi_datatype_t *datatype)
60+
{
61+
if (OPAL_UNLIKELY(datatype->pml_data == PML_UCX_DATATYPE_INVALID)) {
62+
return mca_pml_ucx_init_datatype(datatype);
6063
}
61-
#endif
6264

63-
return mca_pml_ucx_init_datatype(datatype);
65+
return mca_pml_ucx_from_ompi_datatype(datatype);
6466
}
6567

6668
#ifdef HAVE_UCP_REQUEST_PARAM_T

0 commit comments

Comments
 (0)