problem with thread-safe calls
Hallo,
On 09/26/2013 09:58 AM, Bert Wesarg wrote:
Hallo Wolfgang,
ein ./configure --enable-pthreads funktioniert bei build-linux-gomp-nompi nicht. weil scheinbar der SION_PTHREADS= Eintrag im mf/Makefile.defs.linux-gomp file fehlt.
nachdem ich nun ein 'thread-safe' SIONlib hatte, hatte ich doch keins, denn das allokieren des fd_data structs ist nicht thread-safe. Mit dem angehängten patch läuft OTF2 mit SIONlib in einem thread parallelen context recht stabil.
Grüße, Bert
-- Dipl.-Inf. Bert Wesarg wiss. Mitarbeiter
Technische Universität Dresden Zentrum für Informationsdienste und Hochleistungsrechnen (ZIH) 01062 Dresden Tel.: +49 (351) 463-42451 Fax: +49 (351) 463-37773 E-Mail: Bert.Wesarg@tu-dresden.de
convert-fd_data-into-static-initializer.patch
commit 56f0482ab36236fc8a504c51207eecdd8c11f104 Author: Bert Wesarg bert.wesarg@googlemail.com Date: Thu Sep 26 10:46:38 2013 +0200
Make the fd list initialization thread safe.
diff --git a/src/lib/sion_common.c b/src/lib/sion_common.c index aae14ca..67aa7b2 100644 src/lib/sion_common.c --- a/src/lib/sion_common.c +++ b/src/lib/sion_common.c @@ -84,9 +84,6 @@ int _sion_open_rank(char *fname, const char *file_mode, sion_int64 *chunksize, s return(_sion_errorprint_on_rank(-1,_SION_ERROR_RETURN,*rank,"_sion_open_rank: cannot allocate prefix string of size %lu , aborting ...\n", (unsigned long) SION_FILENAME_LENGTH)); }
- /* allocate and initialise internal static fd pointer list */
- _sion_alloc_fd_list();
- /* allocate and initialise internal data structure with default values (NULL and -1) */ sion_filedesc = _sion_alloc_filedesc(); if (sion_filedesc == NULL) { diff --git a/src/lib/sion_fd.c b/src/lib/sion_fd.c index 7e0fc38..15c19f8 100644 src/lib/sion_fd.c --- a/src/lib/sion_fd.c +++ b/src/lib/sion_fd.c @@ -23,55 +23,50 @@ #include "sion_fd.h"
#ifdef SION_PTHREADS +#include <pthread.h> +#define SION_FDDATA_INITIALIZER {0,0,-1,NULL,PTHREAD_MUTEX_INITIALIZER} #define _sion_fd_lock(fdd) pthread_mutex_lock(&fdd->lock) #define _sion_fd_unlock(fdd) pthread_mutex_unlock(&fdd->lock) #else +#define SION_FDDATA_INITIALIZER {0,0,-1,NULL} #define _sion_fd_lock(fdd) #define _sion_fd_unlock(fdd) #endif
/************************/ -static sion_fddata *fd_data = NULL; +static struct _sion_fddata {
- int size;
- int nfree;
- int last_freed;
- struct _sion_fd *list; +#ifdef SION_PTHREADS
- pthread_mutex_t lock; +#endif +} fd_data = SION_FDDATA_INITIALIZER;
static char text; / just a temp hack */
-int _sion_alloc_fd_list() -{
- if (fd_data == NULL) {
- fd_data = _sion_new_fd_list();
- }
- return (fd_data != NULL); -}
-int _sion_dealloc_fd_list() -{
- if (fd_data != NULL) {
- _sion_free_fd_list(fd_data);
- }
- return (fd_data == NULL); -}
/* some abreviations for fd functions */ int _sion_newvcd(void *data, int type) {
- return _sion_new_fd(fd_data, data, type);
- return _sion_new_fd(&fd_data, data, type); } int _sion_freevcd(int sid) {
- return _sion_free_fd(fd_data, sid, text);
- return _sion_free_fd(&fd_data, sid, text); } void *_sion_vcdtovcon(int sid) {
- return _sion_fd2ptr(fd_data, sid);
- return _sion_fd2ptr(&fd_data, sid); } int _sion_vcdtype(int sid) {
- return _sion_fd2type(fd_data, sid, text);
- return _sion_fd2type(&fd_data, sid, text); } int _sion_reassignvcd(int sid, void *data, int type) {
- return _sion_reassign_fd(fd_data, sid, data, type, text);
- return _sion_reassign_fd(&fd_data, sid, data, type, text); } /************************/
@@ -80,43 +75,6 @@ int _sion_reassignvcd(int sid, void *data, int type)
/*
-
- create an empty array of descriptors
- */ -sion_fddata *_sion_new_fd_list() -{
- sion_fddata *fdd;
- fdd = malloc(sizeof(sion_fddata));
- sion_assert(fdd, ("out of memory"));
- fdd->size = 0;
- fdd->nfree = 0;
- fdd->last_freed = -1;
- fdd->list = NULL; -#ifdef SION_PTHREADS
- pthread_mutex_init(&fdd->lock, NULL); -#endif
- return fdd; -}
-/*
-
- free an array of descriptors
- */ -void _sion_free_fd_list(sion_fddata *fdd) -{
- if (fdd) {
- if (fdd->list)
-
free(fdd->list);
-#ifdef SION_PTHREADS
- pthread_mutex_destroy(&fdd->lock); -#endif
- free(fdd); fdd=NULL;
- } -}
-/*
- get a new descriptor and assign data/type to it
- returns the new descriptor or -1 in case of an error / diff --git a/src/lib/sion_fd.h b/src/lib/sion_fd.h index 0ff197c..771f90c 100644 src/lib/sion_fd.h --- a/src/lib/sion_fd.h +++ b/src/lib/sion_fd.h @@ -12,18 +12,11 @@ #define SION_FILEDESCRIPTOR 11 /!< int id -> data structure mapping (from visit library) / #define SION_APIDESCRIPTOR 12 /!< int id -> data structure mapping (from visit library) */
-#ifdef SION_PTHREADS -#define SION_FDDATA_INITIALIZER {0,0,-1,NULL,PTHREAD_MUTEX_INITIALIZER} -#else -#define SION_FDDATA_INITIALIZER {0,0,-1,NULL} -#endif
#ifdef __cplusplus extern "C" { #endif
-int _sion_alloc_fd_list(); int _sion_newvcd(void *data, int type); int _sion_freevcd(int sid); void *_sion_vcdtovcon(int sid); @@ -39,23 +32,8 @@ struct _sion_fd { void *data; };
-struct _sion_fddata {
- int size;
- int nfree;
- int last_freed;
- struct _sion_fd *list; -#ifdef SION_PTHREADS
- pthread_mutex_t lock; -#endif -};
typedef struct _sion_fddata sion_fddata;
-sion_fddata *_sion_new_fd_list(); -int _sion_dealloc_fd_list();
-void _sion_free_fd_list(sion_fddata *fdd);
int _sion_new_fd(sion_fddata *fdd, void *data, int type); int _sion_set_fd(sion_fddata *fdd, int fd, void *data, int type, char *text); diff --git a/src/lib/sion_internal.c b/src/lib/sion_internal.c index 34d1482..fccd55a 100644 src/lib/sion_internal.c --- a/src/lib/sion_internal.c +++ b/src/lib/sion_internal.c @@ -96,9 +96,6 @@ int _sion_open_write(char *fname, sion_int64 file_mode_flags, int *ntasks, int * return(_sion_errorprint(-1,_SION_ERROR_RETURN,"sion_open: write with nfiles > 1 currently not supported (nfiles=%d), returning ...\n",(int) *nfiles)); }
-
/* allocate and initialise internal static fd pointer list */
-
_sion_alloc_fd_list();
-
/* allocate and initialise internal data structure with default values (NULL and -1) */ sion_filedesc = _sion_alloc_filedesc(); if (sion_filedesc == NULL) { @@ -222,9 +219,6 @@ int _sion_open_read(char *fname, sion_int64 file_mode_flags, int read_all, int * _sion_filedesc *sion_filedesc; _sion_fileptr *sion_fileptr;
-
/* allocate and initialise internal static fd pointer list */
-
_sion_alloc_fd_list();
-
/* allocate and initialise internal data structure with default values (NULL and -1) */ sion_filedesc = _sion_alloc_filedesc(); if (sion_filedesc == NULL) { diff --git a/src/parlib/sion_generic.c b/src/parlib/sion_generic.c index 08e0eaa..36d1b4b 100644 src/parlib/sion_generic.c --- a/src/parlib/sion_generic.c +++ b/src/parlib/sion_generic.c @@ -64,8 +64,6 @@ int sion_generic_create_api( char *name ) {
sion_apidesc->name=strdup(name);
-
/* allocate and initialise internal static fd pointer list */
-
_sion_alloc_fd_list(); apiid = _sion_newvcd(sion_apidesc,SION_APIDESCRIPTOR); sion_apidesc->aid=apiid;
@@ -336,9 +334,6 @@ int sion_generic_paropen(int aid, /* set up parameters of call to generic open */ nfname=_sion_get_multi_filename(fname,*filenumber);
-
/* allocate and initialise internal static fd pointer list */
-
_sion_alloc_fd_list();
-
sid = _sion_newvcd(NULL,SION_FILEDESCRIPTOR);
DPRINTFP((1, "sion_generic_paropen", grank, "enter parallel open of %d files (current name %s) in %s mode\n", *numfiles, nfname, file_mode)); @@ -538,9 +533,6 @@ int sion_generic_paropen_mapped(int aid, return(_sion_errorprint(-1,_SION_ERROR_RETURN,"sion_generic_paropen_mapped: unknown file mode")); }
-
/* allocate and initialise internal static fd pointer list */
-
_sion_alloc_fd_list();
-
sid = _sion_newvcd(NULL,SION_FILEDESCRIPTOR);
DPRINTFP((1, "sion_generic_paropen_mapped", grank, "enter parallel mapped open in %s mode\n", file_mode)); diff --git a/src/parlib/sion_mpi.c b/src/parlib/sion_mpi.c index dee228a..6d90548 100644 src/parlib/sion_mpi.c --- a/src/parlib/sion_mpi.c +++ b/src/parlib/sion_mpi.c @@ -137,9 +137,6 @@ int sion_paropen_mpi(char fname, sion_filedesc_mpiadd->lComm=lComm; sion_filedesc_mpiadd->gComm=gComm;
-
/* allocate ad initialise internal static fd pointer list */
-
_sion_alloc_fd_list();
-
sid = _sion_newvcd(NULL,0);
DPRINTFP((1, "sion_paropen_mpi", gRank, "enter parallel open of %d files (current name %s) in %s mode\n", *numFiles, nfname, file_mode)); @@ -369,8 +366,6 @@ int sion_paropen_mapped_mpi( char *fname, sion_filedesc_mpiadd->gComm=gComm;
-
/* allocate ad initialise internal static fd pointer list */
-
_sion_alloc_fd_list(); sid = _sion_newvcd(NULL,0);
DPRINTFP((1, "sion_paropen_mapped_mpi", gRank, "enter parallel open of %d files (current name %s) in %s mode (sid=%d)\n", *numFiles, fname, file_mode, sid)); diff --git a/src/parlib/sion_omp.c b/src/parlib/sion_omp.c index 44751df..5f81d95 100644 src/parlib/sion_omp.c --- a/src/parlib/sion_omp.c +++ b/src/parlib/sion_omp.c @@ -107,8 +107,6 @@ int sion_paropen_omp( { int i;
-
_sion_alloc_fd_list();
-
sid_list = malloc(num_threads*(sizeof(int))); if(sid_list==NULL) (_sion_errorprint_omp(-1,_SION_ERROR_ABORT,"sion_paropen_omp: cannot allocate struct of size %lu (int), aborting...", sizeof(int)));
diff --git a/src/parlib/sion_ompi.c b/src/parlib/sion_ompi.c index 5bcbd05..89cccd1 100644 src/parlib/sion_ompi.c --- a/src/parlib/sion_ompi.c +++ b/src/parlib/sion_ompi.c @@ -206,7 +206,6 @@ int sion_paropen_ompi( #pragma omp master { int i;
- _sion_alloc_fd_list(); thread_sync->sid_list = malloc(num_threads*(sizeof(int))); if(thread_sync->sid_list==NULL) (_sion_errorprint_ompi(-1,_SION_ERROR_ABORT,"sion_paropen_ompi: cannot allocate temporary memory of size %lu , aborting...", num_threadssizeof(int))); @@ -310,7 +309,6 @@ int sion_paropen_ompi( / allocate sion file handles */ { int i;
- _sion_alloc_fd_list(); thread_sync->sid_list = malloc(num_threads*(sizeof(int))); if(thread_sync->sid_list==NULL) (_sion_errorprint_ompi(-1,_SION_ERROR_ABORT,"sion_paropen_ompi: cannot allocate temporary memory of size %lu , aborting...", num_threads*sizeof(int)));