SIONlib  1.7.0
Scalable I/O library for parallel access to task-local files
sion_ompi_gen.c
Go to the documentation of this file.
1 /****************************************************************************
2 ** SIONLIB http://www.fz-juelich.de/jsc/sionlib **
3 *****************************************************************************
4 ** Copyright (c) 2008-2016 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** See the file COPYRIGHT in the package base directory for details **
8 ****************************************************************************/
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #include <time.h>
22 
23 #include "mpi.h"
24 
25 #include <sys/time.h>
26 
27 #include <sys/types.h>
28 #include <fcntl.h>
29 
30 #include <unistd.h>
31 
32 #include "sion.h"
33 #include "sion_debug.h"
34 #include "sion_error_handler.h"
35 #include "sion_internal.h"
36 #include "sion_fd.h"
37 #include "sion_filedesc.h"
38 #include "sion_printts.h"
39 #include "sion_flags.h"
40 
41 #ifdef SION_OMPI
42 
43 #include "sion_generic.h"
44 
45 #include "sion_ompi.h"
46 #include "sion_ompi_internal_gen.h"
47 #include "sion_ompi_cb_gen.h"
48 #include "sion_lock.h"
49 
50 #include "omp.h"
51 
52 int _sion_ompi_api_aid = -1;
53 static omp_lock_t _sion_ompi_lock_data;
54 
55 static void * __ompi_thread_sync_struct;
56 
57 int _sion_ompi_user_lock(void * data) {
58  int rc=SION_SUCCESS;
59  omp_set_lock(&_sion_ompi_lock_data);
60  return(rc);
61 }
62 int _sion_ompi_user_unlock(void * data) {
63  int rc=SION_SUCCESS;
64  omp_unset_lock(&_sion_ompi_lock_data);
65  return(rc);
66 }
67 
88 #define DFUNCTION "sion_paropen_ompi"
89 int sion_paropen_ompi(const char* fname,
90  const char* file_mode,
91  int* numFiles,
92  MPI_Comm gComm,
93  const MPI_Comm* lComm,
94  sion_int64* chunksize,
95  sion_int32* fsblksize,
96  int* globalrank,
97  FILE** fileptr,
98  char** newfname)
99 {
100 
101  /* gRank and lRank refer to the MPI process rank in the global and local communicator respectively */
102  int rc, sid = -1;
103  int filenumber, gRank, lRank, lSize, gSize;
104 
105  _sion_flags_store* flags_store = NULL;
106 
107  _ompi_api_commdata *gen_gcomm;
108  _ompi_api_commdata *gen_lcomm=NULL;
109 
110  int num_threads, thread_num;
111  __ompi_thread_sync *thread_sync;
112 
113 
114 
115  thread_num = omp_get_thread_num();
116 
117  #pragma omp master
118  {
119 
120  _sion_debug_set_query_thread_num_function(omp_get_thread_num);
121  _sion_error_set_query_thread_num_function(omp_get_thread_num);
122  omp_init_lock(&_sion_ompi_lock_data);
123  sion_lock_register_lock_callbacks(_sion_ompi_user_lock,_sion_ompi_user_unlock,&_sion_ompi_lock_data);
124 
125  MPI_Comm_size(gComm, &gSize);
126  MPI_Comm_rank(gComm, &gRank);
127  num_threads = omp_get_num_threads();
128 
129  thread_sync = malloc(sizeof(__ompi_thread_sync));
130  if(thread_sync==NULL) (_sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_ABORT,"sion_paropen_ompi: cannot allocate struct of size %lu (__ompi_thread_sync), aborting...",
131  sizeof(__ompi_thread_sync)));
132 
133  thread_sync->grank_master_mpi = gRank;
134  thread_sync->gsize_mpi = gSize;
135  thread_sync->grank_master_ompi = _sion_map_rank_mpi_to_ompi(gRank,num_threads,thread_num);
136  thread_sync->gsize_ompi = _sion_get_size_ompi(gSize,num_threads);
137  thread_sync->num_threads = num_threads;
138  thread_sync->numFiles = *numFiles;
139  __ompi_thread_sync_struct = thread_sync;
140  }
141  /* sync to ensure that info in thread_sync is accessible */
142  {
143 #pragma omp barrier
144  }
145 
146 
147  /* this is actually not necessary, but it makes for cleaner code by preventing us from doing lots of typecasts */
148  thread_sync = (__ompi_thread_sync *) __ompi_thread_sync_struct;
149 
150  DPRINTFP((1, "sion_paropen_ompi", thread_sync->grank_master_ompi+thread_num, "thread %d enters parallel open of file %s\n", thread_num, fname));
151 
152 
153  /* check parameters */
154  if (lComm == NULL) {
155  return(_sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_ABORT,"sion_paropen_ompi: No lComm variable given"));
156  }
157  if (numFiles == NULL) {
158  return(_sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_ABORT,"sion_paropen_ompi: No numFiles variable given"));
159  }
160  flags_store = _sion_parse_flags(file_mode);
161  if ( ! flags_store ) {
162  return(_sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"sion_paropen_mpi: could not parse file mode in %s, aborting ...\n", file_mode));
163  }
164 
165  /* create generic API */
166  #pragma omp master
167  {
168  /* register callbacks for generic interface */
169  if(_sion_ompi_api_aid<0) _sion_ompi_api_aid=_sion_register_callbacks_ompi();
170  }
171 
172  /* create global generic communicator container on all threads */
173  gen_gcomm = (_ompi_api_commdata *) malloc(sizeof(_ompi_api_commdata));
174  if (gen_gcomm != NULL) {
175  gen_gcomm->commset=0;
176  gen_gcomm->local=0;
177  gen_gcomm->rank=thread_sync->grank_master_ompi+thread_num;
178  gen_gcomm->size=thread_sync->gsize_ompi;
179  gen_gcomm->num_threads=thread_sync->num_threads;
180  gen_gcomm->thread_num=thread_num;
181  gen_gcomm->lcommgroup=NULL;
182  } else {
183  _sion_flags_destroy_store(&flags_store);
184  return(_sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_RETURN,
185  "cannot allocate ompi internal data structure of size %lu (_omp_api_commdata), aborting ...\n",
186  (unsigned long) sizeof(_ompi_api_commdata)));
187  }
188 
189  /* store MPI communicator in global generic communicator container on master thread */
190 #pragma omp master
191  {
192  gen_gcomm->comm=gComm;
193  }
194 
195 
196 
197  /* sync to ensure that aid is accessible */
198  {
199 #pragma omp barrier
200  }
201 
202  if (flags_store->mask&_SION_FMODE_WRITE) {
203  /* file mode WRITE */
204 
205  /* create generic local communicator container on each thread */
206  if (*numFiles <= 0) {
207  gen_lcomm = (_ompi_api_commdata *) malloc(sizeof(_ompi_api_commdata));
208  if (gen_lcomm != NULL) {
209  gen_lcomm->commset=1;
210  gen_lcomm->commcreated=0;
211  gen_lcomm->local=1;
212  gen_lcomm->num_threads=gen_gcomm->num_threads;
213  gen_lcomm->thread_num=thread_num;
214  gen_gcomm->lcommgroup=gen_lcomm; /* store pointer in global comm group */
215  } else {
216  _sion_flags_destroy_store(&flags_store);
217  return(_sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"cannot allocate ompi internal data structure of size %lu (_ompi_api_commdata), aborting ...\n",
218  (unsigned long) sizeof(_ompi_api_commdata)));
219  }
220  }
221 
222 #pragma omp master
223  {
224 
225  if (*numFiles <= 0) {
226  /* lComm contains local communicator */
227 
228  rc = _sion_get_info_from_splitted_comm_ompi(gComm, *lComm, numFiles, &filenumber, &lRank, &lSize);
229  if(rc != SION_SUCCESS) _sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"sion_paropen_ompi: error in _sion_get_info_from_splitted_comm_ompi");
230  DPRINTFP((1, DFUNCTION, gRank, "%d local communicators found\n", *numFiles));
231 
232  gen_lcomm->comm=*lComm;
233 
234  } else {
235  /* number of files is given */
236  rc = _sion_gen_info_from_gcomm_ompi(*numFiles, gComm, &filenumber, &lRank, &lSize);
237  if(rc != SION_SUCCESS) _sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"sion_paropen_ompi: error in _sion_gen_info_from_gcomm_ompi");
238  DPRINTFP((1, "sion_paropen_ompi", gRank, "Global communicator divided in %d local communicators\n", *numFiles));
239  }
240 
241  thread_sync->filenumber = filenumber;
242  thread_sync->numFiles = *numFiles;
243  thread_sync->lrank_master_mpi = lRank;
244  thread_sync->lsize_mpi = lSize;
245  thread_sync->lrank_master_ompi = _sion_map_rank_mpi_to_ompi(lRank,num_threads,thread_num);
246  thread_sync->lsize_ompi = _sion_get_size_ompi(lSize,num_threads);
247 
248  } /* OMP MASTER END */
249 
250  {
251 #pragma omp barrier
252  }
253 
254 
255  /* set up parameters of call to generic open (OMPI values) */
256  gRank = thread_sync->grank_master_ompi+thread_num;
257  gSize = thread_sync->gsize_ompi;
258  lRank = thread_sync->lrank_master_ompi+thread_num;
259  lSize = thread_sync->lsize_ompi;
260  filenumber = thread_sync->filenumber;
261  *numFiles = thread_sync->numFiles;
262 
263  if (gen_lcomm != NULL) {
264  gen_lcomm->rank=thread_sync->lrank_master_ompi+thread_num;
265  gen_lcomm->size=thread_sync->lsize_ompi;
266  }
267 
268  } else if (flags_store->mask&_SION_FMODE_READ) {
269 
270  /* file mode READ */
271  /* set up parameters of call to generic open (OMPI values) */
272  gRank = thread_sync->grank_master_ompi+thread_num;
273  gSize = thread_sync->gsize_ompi;
274  lRank = -1; /* which determined after opening file by sion_generic_paropen */
275  lSize = -1; /* " */
276  filenumber = -1; /* " */
277  *numFiles = -1; /* " */
278 
279  } else {
280  _sion_flags_destroy_store(&flags_store);
281  return(_sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"sion_paropen_ompi: unknown file mode"));
282  }
283  _sion_flags_destroy_store(&flags_store);
284 
285 
286  DPRINTFP((1, DFUNCTION, gRank, "enter parallel open of %d files (current name %s) in %s mode\n", *numFiles, fname, file_mode));
287  DPRINTFP((2, DFUNCTION, gRank, "enter parallel parameters: grank=%d gsize=%d fnum=%d numfiles=%d lrank=%d lsize=%d chunksize=%d\n",
288  gRank, gSize,filenumber, *numFiles, lRank, lSize, (int) *chunksize));
289  sid = sion_generic_paropen(_sion_ompi_api_aid, fname, file_mode, chunksize, fsblksize, gen_gcomm,
290  gRank, gSize, &filenumber, numFiles, &lRank, &lSize,
291  fileptr, newfname);
292  DPRINTFP((1, DFUNCTION, gRank, "leave parallel open of %d files in %s mode #tasks=%d sid=%d\n", *numFiles, file_mode, lSize, sid));
293 
294  /* test return code from internal open */
295  if ( sid == SION_ID_NOT_VALID ) {
296  return(_sion_errorprint_ompi(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"sion_paropen_mpi: invalid return code from internal open %d", rc));
297  }
298 
299  /* return parameter */
300  *globalrank=gRank;
301 
302  DPRINTFP((1, "sion_paropen_ompi", gRank, "leave parallel open of file %s sid=%d globalrank=%d\n", fname, sid,*globalrank));
303 
304  return (sid);
305 }
306 #undef DFUNCTION
307 
308 
315 int sion_parclose_ompi(int sid)
316 {
317  int rc=0;
318 
319  DPRINTFP((1, "sion_parclose_ompi", _SION_DEFAULT_RANK, "enter parallel close of sid %d\n", sid));
320 
321  rc = sion_generic_parclose(sid);
322 
323  DPRINTFP((1, "sion_parclose_ompi", _SION_DEFAULT_RANK, "leave parallel close of sid %d rc=%d\n", sid, rc));
324 
325  return (rc);
326 }
327 
328 /* end of ifdef OMPI */
329 #endif
int sion_generic_paropen(int aid, const char *fname, const char *file_mode, sion_int64 *chunksize, sion_int32 *fsblksize, void *gcommgroup, int grank, int gsize, int *filenumber, int *numfiles, const int *lrank, const int *lsize, FILE **fileptr, char **newfname)
Open a sion file a generic interface.
Definition: sion_generic.c:349
int sion_lock_register_lock_callbacks(int lock(void *), int unlock(void *), void *lock_data)
Function which registers callback funtions for lock and unlock internal access to shared data structu...
Definition: sion_common.c:1181
_sion_flags_store * _sion_parse_flags(const char *flags)
Parse flags and return a flags store with key value pairs.
Definition: sion_flags.c:288
Sion Time Stamp Header.