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