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