Skip to content

Commit a76f4d7

Browse files
authored
Merge pull request #1990 from edgargabriel/topic/mt-io
steps towards making file I/O operations thread safe
2 parents deae1ab + c3d4ee3 commit a76f4d7

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

ompi/file/file.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Copyright (c) 2009-2012 Cisco Systems, Inc. All rights reserved.
1515
* Copyright (c) 2015 Research Organization for Information Science
1616
* and Technology (RIST). All rights reserved.
17+
* Copyright (c) 2016 University of Houston. All rights reserved.
1718
* $COPYRIGHT$
1819
*
1920
* Additional copyrights may follow
@@ -31,6 +32,10 @@
3132
#include "ompi/mca/io/base/base.h"
3233
#include "ompi/info/info.h"
3334

35+
36+
opal_mutex_t ompi_mpi_file_bootstrap_mutex = OPAL_MUTEX_STATIC_INIT;
37+
38+
3439
/*
3540
* Table for Fortran <-> C file handle conversion
3641
*/
@@ -102,6 +107,7 @@ int ompi_file_open(struct ompi_communicator_t *comm, const char *filename,
102107
return OMPI_ERR_OUT_OF_RESOURCE;
103108
}
104109

110+
105111
/* Save the params */
106112

107113
file->f_comm = comm;
@@ -127,6 +133,9 @@ int ompi_file_open(struct ompi_communicator_t *comm, const char *filename,
127133
return OMPI_ERR_OUT_OF_RESOURCE;
128134
}
129135

136+
/* Create the mutex */
137+
OBJ_CONSTRUCT(&file->f_mutex, opal_mutex_t);
138+
130139
/* Select a module and actually open the file */
131140

132141
if (OMPI_SUCCESS != (ret = mca_io_base_file_select(file, NULL))) {
@@ -146,6 +155,9 @@ int ompi_file_open(struct ompi_communicator_t *comm, const char *filename,
146155
*/
147156
int ompi_file_close(ompi_file_t **file)
148157
{
158+
159+
OBJ_DESTRUCT(&(*file)->f_mutex);
160+
149161
(*file)->f_flags |= OMPI_FILE_ISCLOSED;
150162
OBJ_RELEASE(*file);
151163
*file = &ompi_mpi_file_null.file;

ompi/file/file.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Copyright (c) 2009-2012 Cisco Systems, Inc. All rights reserved.
1515
* Copyright (c) 2015 Research Organization for Information Science
1616
* and Technology (RIST). All rights reserved.
17+
* Copyright (c) 2016 University of Houston. All rights reserved.
1718
* $COPYRIGHT$
1819
*
1920
* Additional copyrights may follow
@@ -78,6 +79,10 @@ struct ompi_file_t {
7879
indicates what member to look at in the union, below) */
7980
mca_io_base_version_t f_io_version;
8081

82+
/** Mutex to be used to protect access to the selected component
83+
on a per file-handle basis */
84+
opal_mutex_t f_mutex;
85+
8186
/** The selected component (note that this is a union) -- we need
8287
this to add and remove the component from the list of
8388
components currently in use by the io framework for

ompi/mca/io/base/io_base_file_select.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "ompi/mca/sharedfp/sharedfp.h"
4747
#include "ompi/mca/sharedfp/base/base.h"
4848

49+
opal_mutex_t ompi_mpi_ompio_bootstrap_mutex = OPAL_MUTEX_STATIC_INIT;
4950
/*
5051
* Local types
5152
*/
@@ -201,18 +202,24 @@ int mca_io_base_file_select(ompi_file_t *file,
201202
"ompio")) {
202203
int ret;
203204

205+
opal_mutex_lock(&ompi_mpi_ompio_bootstrap_mutex);
204206
if (OMPI_SUCCESS != (ret = mca_base_framework_open(&ompi_fs_base_framework, 0))) {
207+
opal_mutex_unlock(&ompi_mpi_ompio_bootstrap_mutex);
205208
return err;
206209
}
207210
if (OMPI_SUCCESS != (ret = mca_base_framework_open(&ompi_fcoll_base_framework, 0))) {
211+
opal_mutex_unlock(&ompi_mpi_ompio_bootstrap_mutex);
208212
return err;
209213
}
210214
if (OMPI_SUCCESS != (ret = mca_base_framework_open(&ompi_fbtl_base_framework, 0))) {
215+
opal_mutex_unlock(&ompi_mpi_ompio_bootstrap_mutex);
211216
return err;
212217
}
213218
if (OMPI_SUCCESS != (ret = mca_base_framework_open(&ompi_sharedfp_base_framework, 0))) {
219+
opal_mutex_unlock(&ompi_mpi_ompio_bootstrap_mutex);
214220
return err;
215221
}
222+
opal_mutex_unlock(&ompi_mpi_ompio_bootstrap_mutex);
216223

217224
if (OMPI_SUCCESS !=
218225
(ret = mca_fs_base_find_available(OPAL_ENABLE_PROGRESS_THREADS, 1))) {
@@ -230,6 +237,7 @@ int mca_io_base_file_select(ompi_file_t *file,
230237
(ret = mca_sharedfp_base_find_available(OPAL_ENABLE_PROGRESS_THREADS, 1))) {
231238
return err;
232239
}
240+
233241
}
234242
/* Finally -- intialize the selected module. */
235243

ompi/mca/io/romio314/src/io_romio314_file_open.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ mca_io_romio314_file_open (ompi_communicator_t *comm,
3838
mca_io_romio314_data_t *data;
3939

4040
data = (mca_io_romio314_data_t *) fh->f_io_selected_data;
41-
OPAL_THREAD_LOCK (&mca_io_romio314_mutex);
41+
// OPAL_THREAD_LOCK (&mca_io_romio314_mutex);
4242
ret = ROMIO_PREFIX(MPI_File_open)(comm, filename, amode, info,
4343
&data->romio_fh);
44-
OPAL_THREAD_UNLOCK (&mca_io_romio314_mutex);
44+
// OPAL_THREAD_UNLOCK (&mca_io_romio314_mutex);
4545

4646
return ret;
4747
}

ompi/mpi/c/file_open.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* reserved.
1616
* Copyright (c) 2015 Research Organization for Information Science
1717
* and Technology (RIST). All rights reserved.
18+
* Copyright (c) 2016 University of Houston. All rights reserved.
1819
* $COPYRIGHT$
1920
*
2021
* Additional copyrights may follow
@@ -34,6 +35,9 @@
3435
#include "ompi/mca/io/base/base.h"
3536
#include "ompi/memchecker.h"
3637

38+
39+
extern opal_mutex_t ompi_mpi_file_bootstrap_mutex;
40+
3741
#if OMPI_BUILD_MPI_PROFILING
3842
#if OPAL_HAVE_WEAK_SYMBOLS
3943
#pragma weak MPI_File_open = PMPI_File_open
@@ -81,10 +85,20 @@ int MPI_File_open(MPI_Comm comm, const char *filename, int amode,
8185
and MPI_FILE_DELETE are the only two places that it will be
8286
initialized). */
8387

88+
/* For multi-threaded scenarios, initializing the file i/o
89+
framework and mca infrastructure needs to be protected
90+
by a mutex, similarly to the other frameworks in
91+
ompi/runtime/ompi_mpi_init.c
92+
*/
93+
94+
opal_mutex_lock(&ompi_mpi_file_bootstrap_mutex);
95+
8496
rc = mca_base_framework_open(&ompi_io_base_framework, 0);
8597
if (OMPI_SUCCESS != rc) {
98+
opal_mutex_unlock(&ompi_mpi_file_bootstrap_mutex);
8699
return OMPI_ERRHANDLER_INVOKE(MPI_FILE_NULL, rc, FUNC_NAME);
87100
}
101+
opal_mutex_unlock(&ompi_mpi_file_bootstrap_mutex);
88102

89103
OPAL_CR_ENTER_LIBRARY();
90104

@@ -96,6 +110,5 @@ int MPI_File_open(MPI_Comm comm, const char *filename, int amode,
96110
/* Creating the file handle also selects a component to use,
97111
creates a module, and calls file_open() on the module. So
98112
we're good to go. */
99-
100113
OMPI_ERRHANDLER_RETURN(rc, *fh, rc, FUNC_NAME);
101114
}

0 commit comments

Comments
 (0)