SIONlib  1.7.0
Scalable I/O library for parallel access to task-local files
sion_generic_mapped.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 ****************************************************************************/
9 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 
20 #include "sion.h"
21 #include "sion_debug.h"
22 #include "sion_error_handler.h"
23 #include "sion_file.h"
24 #include "sion_filedesc.h"
25 #include "sion_fd.h"
26 #include "sion_metadata.h"
27 #include "sion_internal.h"
28 #include "sion_printts.h"
29 
30 #include "sion_buffer.h"
31 
32 #include "sion_filedesc.h"
33 #include "sion_keyvalue.h"
34 
35 #include "sion_generic_internal.h"
37 
38 #define _SION_CONST_MAX_INT32 2147483647
39 
64  int sid,
65  char *fname,
66  sion_int64 file_mode_flags,
67  char *prefix,
68  int *numFiles,
69  int *nlocaltasks,
70  int **globalranks,
71  sion_int64 **chunksizes,
72  int **mapping_filenrs,
73  int **mapping_lranks,
74  sion_int32 *fsblksize,
75  int rank,
76  int ntasks,
77  int flag,
78  FILE **fileptr,
79  _sion_generic_gendata *sion_gendata) {
80  int rc=SION_SUCCESS;
81  int filenr, root, task;
82 
83  _sion_filedesc *sion_filedesc_sub,*sion_filedesc_master;
84  _sion_fileptr *sion_fileptr = NULL;
85  sion_int64 helpint64, apiflag, new_fsblocksize, lstartpointer;
86  /* sion_int64 lchunksize, lstartpointer, lglobalrank, helpint64; */
87  sion_int32 *sion_count = NULL;
88  sion_int32 helpint32;
89  sion_int64 *sion_tmpintfield2 = NULL;
90  sion_int64 *sion_tmpintfield3 = NULL;
91 
92  /* for write */
93  int *lfilecounter,*lfilemanager, ltask, tmpsize, blknum;
94 
95  if (file_mode_flags&_SION_FMODE_POSIX) apiflag=SION_FILE_FLAG_POSIX;
96  else apiflag=SION_FILE_FLAG_ANSI;
97 
98  DPRINTFP((2, "_sion_paropen_mapped_generic", rank, "enter parallel open sid=%d\n", sid));
99 
100  sion_filedesc_master = _sion_alloc_filedesc();
101  if (sion_filedesc_master == NULL) {
102  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate filedescriptor structure of size %lu (sion_filedesc_master), aborting ...\n",
103  (unsigned long) sizeof(sion_filedesc_master)));
104  }
105 
106  _sion_init_filedesc(sion_filedesc_master);
107  sion_filedesc_master->fname = strdup(fname); /* Set the filename */
108  sion_filedesc_master->state = SION_FILESTATE_PAROPENMAPPEDMASTER;
109 
110 
111  if (file_mode_flags&_SION_FMODE_WRITE) {
112  /* **************** WRITE mode **************** */
113 
114  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " starting open for write #tasks=%d\n", ntasks));
115 
116 
117  for(ltask=0;ltask<*nlocaltasks;ltask++) {
118  DPRINTFP((128, "_sion_paropen_mapped_generic", rank, "input: %2d: f=%d gtask=%2d ltask=%d --> %d\n", ltask,
119  (int) (*mapping_filenrs)[ltask],
120  (int) (*globalranks)[ltask],
121  (int) (*mapping_lranks)[ltask],
122  (int) (*chunksizes)[ltask]
123  ));
124  }
125 
126 
127  /* init and allocate master filedesc */
128  sion_filedesc_master->mode = SION_FILEMODE_WRITE;
129  _sion_reassignvcd(sid,sion_filedesc_master, SION_FILEDESCRIPTOR);
130  sion_filedesc_master->sid=sid;
131  sion_filedesc_master->nfiles=*numFiles;
132 
133  sion_filedesc_master->multifiles = (_sion_filedesc **) malloc(sion_filedesc_master->nfiles * sizeof(_sion_filedesc*));
134  if (sion_filedesc_master->multifiles == NULL) {
135  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"cannot allocate filedescriptor structure vector of size %lu (chunksizes), aborting ...\n",
136  (unsigned long) sion_filedesc_master->nfiles * sizeof(_sion_filedesc*)));
137  }
138 
139  /* loop over all files:
140  - collect chunksizes, ... on task 0 of file
141  - calculate startpointers
142  - distribute info
143  */
144 
145  lfilecounter = (int *) malloc(*numFiles * sizeof(int));
146  if (lfilecounter == NULL) {
147  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate temporary memory of size %lu (lfilecounter), aborting ...\n",
148  (unsigned long) *numFiles * sizeof(int)));
149  }
150  lfilemanager = (int *) malloc(*numFiles * sizeof(int));
151  if (lfilemanager == NULL) {
152  free(lfilecounter);
153  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate temporary memory of size %lu (lfilemanager), aborting ...\n",
154  (unsigned long) *numFiles * sizeof(int)));
155  }
156 
157  sion_count = (sion_int32 *) malloc(ntasks * sizeof(sion_int32));
158  if (sion_count == NULL) {
159  free(lfilemanager);
160  free(lfilecounter);
161  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate temporary memory of size %lu (sion_count), aborting ...\n",
162  (unsigned long) ntasks * sizeof(sion_int32)));
163  }
164 
165 
166  /* get number of local tasks writing to a physical file */
167  for(filenr=0;filenr<*numFiles;filenr++) lfilecounter[filenr]=lfilemanager[filenr]=0;
168  for(ltask=0;ltask<*nlocaltasks;ltask++) {
169  filenr=(*mapping_filenrs)[ltask];
170  if((filenr<0) || (filenr>=*numFiles)){
171  free(sion_count);
172  free(lfilemanager);
173  free(lfilecounter);
174  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: on task %d: wrong file number index (%d) in mapping for task %d, aborting ...\n",
175  rank,filenr,ltask));
176  }
177  lfilecounter[filenr]++;
178  if((*mapping_lranks)[ltask]==0) {
179  lfilemanager[filenr]=1;
180  }
181  }
182 
183  sion_filedesc_master->mapping_size=0;
184 
185  /* check for keyval parameter also on master */
186  _sion_keyval_check_env(sion_filedesc_master, file_mode_flags);
187 
188  /* LOOP over files */
189  for(filenr=0;filenr<*numFiles;filenr++) {
190 
191  DPRINTFP((4, "_sion_paropen_mapped_generic", rank, " starting init of SION #%d of %d (%d local tasks) \n", filenr,*numFiles,lfilecounter[filenr]));
192 
193  /* determine which task handle this file */
194  root=-1;
195  helpint32 = lfilemanager[filenr];
196  sion_gendata->apidesc->gatherr_cb(&helpint32, sion_count, sion_gendata->comm_data_global, _SION_INT32, 1, 0);
197  if (rank == 0) {
198  for(task=0;task<ntasks;task++) {
199  if(sion_count[task]==1) root=task;
200  }
201  helpint64=root;
202  }
203  sion_gendata->apidesc->bcastr_cb(&helpint64, sion_gendata->comm_data_global, _SION_INT64,1 , 0); root=helpint64;
204  if(root<0){
205  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: on task %d: local rank 0 of file number index (%d) will not used by any tasks, aborting ...\n",
206  rank,filenr));
207  }
208 
209  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " file #%d will be managed by task %d \n", filenr,root));
210 
211  /* allocate and initialise internal data structure with default values (NULL and -1) */
212  sion_filedesc_sub = _sion_alloc_filedesc();
213  if (sion_filedesc_sub == NULL) {
214  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate filedescriptor structure of size %lu (sion_filedesc), aborting ...\n",
215  (unsigned long) sizeof(sion_filedesc_sub)));
216  }
217  sion_filedesc_master->multifiles[filenr]=sion_filedesc_sub;
218 
219  _sion_init_filedesc(sion_filedesc_sub);
220 
221  sion_filedesc_sub->fname = (sion_gendata->apidesc->get_multi_filename_cb?sion_gendata->apidesc->get_multi_filename_cb:_sion_get_multi_filename)(fname, filenr); /* Set the filename */
222  if(rank == root ){
223  sion_filedesc_sub->state = SION_FILESTATE_PAROPENMAPPEDMANAGED;
224 
225  } else {
226  sion_filedesc_sub->state = SION_FILESTATE_PAROPENMAPPED;
227  }
228  sion_filedesc_sub->mode = SION_FILEMODE_WRITE;
229  sion_filedesc_sub->endianness = _sion_get_endianness_with_flags(file_mode_flags); /* Endianness */
230  sion_filedesc_sub->fsblksize = *fsblksize;
231  sion_filedesc_sub->swapbytes = 0; /* Endianness, swapping bytes */
232  sion_filedesc_sub->nfiles = *numFiles;
233  sion_filedesc_sub->filenumber = filenr;
234  sion_filedesc_sub->prefix = strdup(prefix);
235  sion_filedesc_sub->compress = file_mode_flags&_SION_FMODE_COMPRESS;
236  sion_filedesc_sub->usecoll = file_mode_flags&_SION_FMODE_COLLECTIVE;
237  sion_filedesc_sub->filemanagedbytask = root;
238 
239  /* check for keyval parameter */
240  _sion_keyval_check_env(sion_filedesc_sub, file_mode_flags);
241 
242  /* allocate memory for storing MAXCHUNKS chunksize infos in internal structure */
243  _sion_realloc_filedesc_blocklist(sion_filedesc_sub, MAXCHUNKS);
244 
245  /* the local datastructure for one sion file contains only info for a subset of tasks writing at all to this file.
246  All parameter of this local data structures all set set as if the file consists only of the local maintained
247  tasks of the file. The data structure element all_localranks contains a mapping from local rank number
248  to the overall rank in this file. The data structure element all_globalranks contains the corresponding globalrank
249  number of all tasks of all files.
250  */
251 
252  /* get number of tasks writing to this file */
253  helpint32 = lfilecounter[filenr];
254  sion_filedesc_sub->nlocaltasksinfile = helpint32;
255  sion_gendata->apidesc->gatherr_cb(&helpint32, sion_count, sion_gendata->comm_data_global, _SION_INT32, 1, root);
256  if (rank == root) {
257  helpint64=0;
258  for(task=0;task<ntasks;task++) helpint64 += sion_count[task];
259  }
260  sion_gendata->apidesc->bcastr_cb(&helpint64, sion_gendata->comm_data_global, _SION_INT64, 1, root);
261  sion_filedesc_sub->ntotaltasksinfile = helpint64;
262  sion_filedesc_master->mapping_size+=helpint64;
263  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, "mapping size increased to %d\n",sion_filedesc_master->mapping_size));
264 
265  /* root stores in sion_count the number of local task of this file for each task */
266 
267  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " file #%d (%s) will store chunks from %d tasks (%d local) \n",
268  filenr,sion_filedesc_sub->fname,
269  sion_filedesc_sub->ntotaltasksinfile,sion_filedesc_sub->nlocaltasksinfile));
270 
271  /* check and distribute new fsblksize */
272  if(rank == root) {
273  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " create file #%d (%s) \n", filenr,sion_filedesc_sub->fname));
274  sion_fileptr = _sion_file_open(sion_filedesc_sub->fname,apiflag|SION_FILE_FLAG_WRITE|SION_FILE_FLAG_CREATE,0);
275  if (!sion_fileptr) {
276  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"sion_paropen_generic: cannot open %s for writing, aborting ...\n", fname));
277  }
278  if(sion_filedesc_sub->fsblksize<=-1) {
279  /* check with fstat fsblksize */
280  new_fsblocksize=(sion_int64) _sion_file_get_opt_blksize(sion_fileptr);
281  if((new_fsblocksize<0) || (new_fsblocksize>SION_MAX_FSBLOCKSIZE)) new_fsblocksize=SION_DEFAULT_FSBLOCKSIZE;
282  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " found new_fsblksize of %d\n", (int) new_fsblocksize));
283  }
284  if (sion_filedesc_sub->usebuffer) {
285  _sion_buffer_flush(sion_filedesc_sub);
286  }
287  _sion_file_close(sion_fileptr);
288  }
289  sion_gendata->apidesc->barrier_cb(sion_gendata->comm_data_global);
290  if(sion_filedesc_sub->fsblksize==-1) {
291  sion_gendata->apidesc->bcastr_cb(&new_fsblocksize, sion_gendata->comm_data_global, _SION_INT64, 1, root);
292  sion_filedesc_sub->fsblksize = new_fsblocksize;
293  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " setting fsblksize to %d\n", (int) new_fsblocksize));
294  }
295 
296 
297 
298  /* collect local ranks/chunksizes */
299  tmpsize=3*sion_filedesc_sub->nlocaltasksinfile;
300  sion_tmpintfield2 = (sion_int64 *) malloc(tmpsize * sizeof(sion_int64));
301  if (sion_tmpintfield2 == NULL) {
302  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate temporary memory of size %lu (sion_tmpintfield2), aborting ...\n",
303  (unsigned long) tmpsize * sizeof(sion_int64)));
304  }
305  if (rank == root) {
306  tmpsize=3*sion_filedesc_sub->ntotaltasksinfile;
307  sion_tmpintfield3 = (sion_int64 *) malloc(tmpsize * sizeof(sion_int64));
308  if (sion_tmpintfield3 == NULL) {
309  free(sion_tmpintfield2);
310  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate temporary memory of size %lu (sion_tmpintfield3), aborting ...\n",
311  (unsigned long) tmpsize * sizeof(sion_int64)));
312  }
313  } else sion_tmpintfield3 = NULL;
314 
315  { int p,lfilenr;
316  p=0;
317  for(ltask=0;ltask<*nlocaltasks;ltask++) {
318  lfilenr=(int) (*mapping_filenrs)[ltask];
319  if(lfilenr==filenr) {
320  sion_tmpintfield2[p*3+0]=(sion_int64) (*mapping_lranks)[ltask];
321  sion_tmpintfield2[p*3+1]=(sion_int64) (*globalranks)[ltask];
322  sion_tmpintfield2[p*3+2]=(sion_int64) (*chunksizes)[ltask];
323  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " prepare %d --> lrank=%d %d %d\n", p,(int) sion_tmpintfield2[p*3+0],(int) sion_tmpintfield2[p*3+1],(int) sion_tmpintfield2[p*3+2]));
324 
325  p++;
326  }
327  }
328  }
329  /* for(ltask=0;ltask<3*sion_filedesc_sub->nlocaltasks;ltask++) {
330  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " debug in %d --> %d\n", ltask, (int) sion_tmpintfield2[ltask] ));
331  }*/
332  if (rank == root) for(task=0;task<ntasks;task++) sion_count[task]*=3;
333  sion_gendata->apidesc->gathervr_cb(sion_tmpintfield2, sion_tmpintfield3, sion_gendata->comm_data_global, _SION_INT64,sion_count,3*sion_filedesc_sub->nlocaltasksinfile, root);
334  if (rank == root) for(task=0;task<ntasks;task++) sion_count[task]/=3;
335  /* if (rank == root) {
336  for(ltask=0;ltask<3*sion_filedesc_sub->ntasks;ltask++) {
337  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " debug out %d --> %d\n", ltask, (int) sion_tmpintfield3[ltask] ));
338  }
339 
340  } */
341 
342  if (rank == root) {
343  int lrank;
344 
345  /* memory allocation for internal fields (large size) */
346  sion_filedesc_sub->ntasks=sion_filedesc_sub->ntotaltasksinfile;
347  _sion_alloc_filedesc_arrays(sion_filedesc_sub);
348 
349  /* sort data into filedesc vectors */
350  for(ltask=0;ltask<sion_filedesc_sub->ntotaltasksinfile;ltask++) {
351  lrank=(int) sion_tmpintfield3[ltask*3+0];
352  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " sort in ltask=%d --> lrank=%d\n", ltask,lrank));
353  sion_filedesc_sub->all_globalranks[lrank]=sion_tmpintfield3[ltask*3+1];
354  sion_filedesc_sub->all_chunksizes[lrank] =sion_tmpintfield3[ltask*3+2];
355  }
356 
357  /* calculate startpointers */
358  _sion_calculate_startpointers(sion_filedesc_sub);
359  _sion_print_filedesc(sion_filedesc_sub, 512, "start of paropen_mapped", 1);
360  }
361 
362  /* open file on all ranks */
363  /* */ DPRINTFTS(rank, "before open");
364  sion_fileptr = _sion_file_open(sion_filedesc_sub->fname,apiflag|SION_FILE_FLAG_WRITE,0);
365  if (!sion_fileptr) {
366  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"sion_paropen_generic: cannot open %s for reading, aborting ...\n", fname));
367  }
368  sion_gendata->apidesc->barrier_cb(sion_gendata->comm_data_global);
369  /* */ DPRINTFTS(rank, "after open");
370 
371  /* store data in static data structure (sid) */
372  sion_filedesc_sub->fileptr = sion_fileptr;
373 
374  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " store for file %s fileprt=%x\n",sion_filedesc_sub->fname, sion_fileptr ));
375 
376  /* write header */
377  if (rank == root) {
378 
379  /* */ DPRINTFTS(rank, "before writeh");
380  /* writes serial metadata, all_globalranks, all_chunksizes */
381  _sion_write_header(sion_filedesc_sub);
382  /* */ DPRINTFTS(rank, "after writeh");
383 
384  /* needed for writing pointer to var part of metadata at the end of the file */
385  sion_filedesc_sub->end_of_header = _sion_file_get_position(sion_filedesc_sub->fileptr);
386  sion_filedesc_sub->start_of_data = sion_filedesc_sub->all_startpointers[0];
387 
388  /*set file pointer to end of file (max. file size with one chunk) */
389  lstartpointer = sion_filedesc_sub->all_startpointers[sion_filedesc_sub->ntasks - 1]
390  + sion_filedesc_sub->all_chunksizes[sion_filedesc_sub->ntasks - 1];
391  /* */ DPRINTFTS(rank, "before setp(0)");
392  _sion_file_flush(sion_filedesc_sub->fileptr);
393  _sion_file_set_position(sion_filedesc_sub->fileptr, lstartpointer);
394  /* */ DPRINTFTS(rank, "after setp(0)");
395 
396  /* free on root some of the large fields */
397  _sion_free_filedesc_all_globalranks(sion_filedesc_sub);
398 
399  /* and realloc with less memory */
400  sion_filedesc_sub->ntasks=sion_filedesc_sub->nlocaltasksinfile;
401  _sion_alloc_filedesc_all_globalranks(sion_filedesc_sub);
402  _sion_alloc_filedesc_all_localranks(sion_filedesc_sub);
403 
404  }
405 
406  if(rank!=root) {
407  /* allocate internal arrays (small size) */
408  sion_filedesc_sub->ntasks=sion_filedesc_sub->nlocaltasksinfile;
409  _sion_alloc_filedesc_arrays(sion_filedesc_sub);
410  _sion_alloc_filedesc_all_localranks(sion_filedesc_sub);
411  }
412 
413  /* allocate memory for keyval if necessary (small size) */
414  if(sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) {
415  _sion_alloc_filedesc_all_keyvalptr(sion_filedesc_sub);
416  }
417 
418  /* store data in internal data structure (rank info) */
419  for(ltask=0;ltask<sion_filedesc_sub->nlocaltasksinfile;ltask++) {
420  sion_filedesc_sub->all_localranks[ltask] = sion_tmpintfield2[ltask*3+0];
421  sion_filedesc_sub->all_globalranks[ltask] = sion_tmpintfield2[ltask*3+1];
422  }
423 
424  /* distribute start_pointer and chunk sizes */
425  if (rank == root) {
426  int lrank;
427  /* get data out of filedesc vectors */
428  for(ltask=0;ltask<sion_filedesc_sub->ntotaltasksinfile;ltask++) {
429  lrank=(int) sion_tmpintfield3[ltask*3+0];
430  sion_tmpintfield3[ltask*3+1]=sion_filedesc_sub->all_startpointers[lrank];
431  sion_tmpintfield3[ltask*3+2]=sion_filedesc_sub->all_chunksizes[lrank]; /* possible changed by calculate_startpointers */
432  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " sort out ltask=%d --> lrank=%d startptr=%ld chunksize=%ld\n",
433  ltask,lrank,(long) sion_tmpintfield3[ltask*3+1],sion_tmpintfield3[ltask*3+2]));
434  }
435  }
436  if (rank == root) for(task=0;task<ntasks;task++) sion_count[task]*=3;
437  sion_gendata->apidesc->scattervr_cb(sion_tmpintfield2, sion_tmpintfield3, sion_gendata->comm_data_global, _SION_INT64,sion_count,3*sion_filedesc_sub->nlocaltasksinfile, root);
438  if (rank == root) for(task=0;task<ntasks;task++) sion_count[task]/=3;
439 
440  if (rank == root) {
441  /* free on root the last large fields */
442  _sion_free_filedesc_all_startpointers(sion_filedesc_sub);
443  _sion_free_filedesc_all_chunksizes(sion_filedesc_sub);
444  /* and realloc with less memory */
445  _sion_alloc_filedesc_all_startpointers(sion_filedesc_sub);
446  _sion_alloc_filedesc_all_chunksizes(sion_filedesc_sub);
447  }
448 
449  for(ltask=0;ltask<sion_filedesc_sub->nlocaltasksinfile;ltask++) {
450  sion_filedesc_sub->all_startpointers[ltask] = sion_tmpintfield2[ltask*3+1];
451  sion_filedesc_sub->all_chunksizes[ltask] = sion_tmpintfield2[ltask*3+2];
452  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " get startpointer[%d] --> %ld\n", ltask, sion_filedesc_sub->all_startpointers[ltask]));
453  }
454 
455  /* distribute globalskip */
456  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->globalskip, sion_gendata->comm_data_global, _SION_INT64, 1, root);
457 
458 
459  /* set filepointer on each task */
460  /* */ DPRINTFTS(rank, "before setp");
461  sion_gendata->apidesc->barrier_cb(sion_gendata->comm_data_global);
462 
463  /* initalize current positions */
464  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " allocating block arrays of size %d\n", sion_filedesc_sub->ntasks));
465  _sion_alloc_filedesc_block_arrays(sion_filedesc_sub);
466  for (ltask = 0; ltask < sion_filedesc_sub->nlocaltasksinfile; ltask++) {
467  sion_filedesc_sub->all_blockcount[ltask] = 1;
468  sion_filedesc_sub->all_currentpos[ltask] = sion_filedesc_sub->all_startpointers[ltask];
469  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " set all_currentpos[%d]=%d\n",ltask,sion_filedesc_sub->all_currentpos[ltask] ));
470  sion_filedesc_sub->all_currentblocknr[ltask] = 0;
471  sion_filedesc_sub->all_blocksizes[0 * sion_filedesc_sub->nlocaltasksinfile + ltask] = 0;
472  }
473 
474  /* set position to first block of rank 0 */
475  sion_filedesc_sub->rank = 0;
476  sion_filedesc_sub->globalrank = sion_filedesc_sub->all_globalranks[sion_filedesc_sub->rank];
477  sion_filedesc_sub->chunksize = sion_filedesc_sub->all_chunksizes[0];
478  sion_filedesc_sub->startpos = sion_filedesc_sub->all_startpointers[0];
479  sion_filedesc_sub->currentpos = sion_filedesc_sub->startpos;
480  sion_filedesc_sub->lastchunknr = 0;
481  sion_filedesc_sub->currentblocknr = 0;
482 
483  _sion_file_purge(sion_fileptr);
484  _sion_file_set_position(sion_fileptr, sion_filedesc_sub->currentpos);
485 
486  sion_gendata->apidesc->barrier_cb(sion_gendata->comm_data_global);
487 
488  if(sion_tmpintfield2) free(sion_tmpintfield2);
489  if(sion_tmpintfield3) free(sion_tmpintfield3);
490 
491  DPRINTFP((4, "_sion_paropen_mapped_generic", rank, " ending init of SION #%d of %d \n", filenr,*numFiles));
492 
493  } /* for each file */
494 
495 
496  /* set master to first file, first available rank */
497 
498 
499  /* lookup file which contains current rank and set master */
500  { int lfile=-1, lrank=-1, blknum;
501  sion_filedesc_sub=NULL;
502  for(filenr=0;filenr<*numFiles;filenr++) {
503  sion_filedesc_sub=sion_filedesc_master->multifiles[filenr];
504  lfile=filenr;
505  lrank=0;
506  if(sion_filedesc_sub->nlocaltasksinfile>0) {
507  lfile=filenr;
508  lrank=0;
509  break;
510  }
511  }
512 
513  if((sion_filedesc_sub) && (lrank>=0) && (lfile>=0)) {
514  _sion_realloc_filedesc_blocklist(sion_filedesc_master, MAXCHUNKS);
515 
516  sion_filedesc_master->ntotaltasksinfile = sion_filedesc_master->mapping_size;
517  sion_filedesc_master->globalrank = sion_filedesc_sub->all_globalranks[lrank];
518  sion_filedesc_master->rank = lrank;
519  sion_filedesc_master->fsblksize = sion_filedesc_sub->fsblksize;
520  sion_filedesc_master->swapbytes = sion_filedesc_sub->swapbytes;
521  sion_filedesc_master->keyvalmode = sion_filedesc_sub->keyvalmode;
522  sion_filedesc_master->filenumber = lfile;
523 
524  /* set info for current rank and position */
525  sion_filedesc_master->chunksize = sion_filedesc_sub->all_chunksizes[lrank];
526  sion_filedesc_master->startpos = sion_filedesc_sub->all_startpointers[lrank];
527  sion_filedesc_master->currentpos = sion_filedesc_master->startpos;
528  sion_filedesc_master->globalskip = sion_filedesc_sub->globalskip;
529 
530  sion_filedesc_master->currentblocknr = 0;
531  sion_filedesc_master->lastchunknr = sion_filedesc_sub->all_blockcount[lrank]-1;
532 
533  sion_filedesc_master->maxusedchunks = sion_filedesc_sub->maxusedchunks;
534 
535  for (blknum = 0; blknum < sion_filedesc_sub->all_blockcount[lrank]; blknum++) {
536  sion_filedesc_master->blocksizes[blknum] = sion_filedesc_sub->all_blocksizes[sion_filedesc_sub->nlocaltasksinfile * blknum + lrank];
537  }
538 
539  /* set file pointer */
540  sion_filedesc_master->fileptr = sion_filedesc_master->multifiles[lfile]->fileptr;
541 
542  /* set position */
543  _sion_file_flush(sion_filedesc_master->fileptr);
544 
545  DPRINTFP((32,"sion_paropen_mapped_generic:",rank,"set startpointer to %d\n",(int) sion_filedesc_master->currentpos));
546  _sion_file_set_position(sion_filedesc_master->fileptr, sion_filedesc_master->currentpos);
547 
548  if(fileptr!=NULL) {
549  if(sion_filedesc_master->fileptr->flags&&SION_FILE_FLAG_ANSI) {
550  *fileptr=sion_filedesc_master->fileptr->fileptr;
551  sion_filedesc_master->fileptr_exported=1;
552  } else {
553  *fileptr=NULL;
554  sion_filedesc_master->fileptr_exported=0;
555  }
556  }
557 
558  }
559  }
560  _sion_print_filedesc(sion_filedesc_master, 512, "_sion_paropen_mapped_generic", 1);
561 
562  if(sion_count) free(sion_count);
563 
564  if(lfilecounter) free(lfilecounter);
565  if(lfilemanager) free(lfilemanager);
566 
567  /* ******************** end of WRITE ************************* */
568 
569  } else if (file_mode_flags&_SION_FMODE_READ) {
570 
571  /* ******************** start of READ ************************* */
572  int mapping_size,mapsid=-1, rootcounter;
573  int lnfiles,lntasks;
574  sion_int32 lfsblksize;
575  FILE *lfileptr;
576  sion_int32 *mapping;
577 
578  DPRINTFP((1, "_sion_paropen_mapped_generic", rank, "read mode ntasks=%d\n",ntasks));
579 
580  for(ltask=0;ltask<*nlocaltasks;ltask++) {
581  DPRINTFP((128, "_sion_paropen_mapped_generic", rank, "input: %2d: gtask=%2d \n", ltask, (int) (*globalranks)[ltask]));
582  }
583 
584  /* init and allocate master filedesc */
585  sion_filedesc_master->mode = SION_FILEMODE_READ;
586  _sion_reassignvcd(sid,sion_filedesc_master, SION_FILEDESCRIPTOR);
587  sion_filedesc_master->sid=sid;
588 
589  /* allocate temp fields */
590  sion_count = (sion_int32 *) malloc(ntasks * sizeof(sion_int32));
591  if (sion_count == NULL) {
592  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate temporary memory of size %lu (sion_count), aborting ...\n",
593  (unsigned long) ntasks * sizeof(sion_int32)));
594  }
595  lfilemanager=NULL;
596 
597  /* 1. Read mapping info from file */
598 
599  if(rank == 0) {
600  /* open and get mapping of sion file */
601  DPRINTFP((1, "_sion_paropen_mapped_generic", rank, "before open\n"));
602  mapsid=_sion_open_read(fname,_SION_FMODE_READ|_SION_FMODE_ANSI,_SION_READ_MASTER_ONLY_OF_MULTI_FILES,
603  &lntasks,&lnfiles,NULL,&lfsblksize,NULL,&lfileptr);
604  if(mapsid>=0) {
605  DPRINTFP((1, "_sion_paropen_mapped_generic", rank, "after open\n"));
606  rc=sion_get_mapping(mapsid,&mapping_size,&mapping,numFiles);
607  DPRINTFP((1, "_sion_paropen_mapped_generic", rank, "sion file %d files #tasks=%d rc=%d\n", *numFiles,mapping_size,rc));
608 
609  /* allocate vector to distribute info about which task is managing which file */
610  lfilemanager = (int *) malloc(*numFiles * sizeof(int));
611  if (lfilemanager == NULL) {
612  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate temporary memory of size %lu (lfilemanager), aborting ...\n",
613  (unsigned long) *numFiles * sizeof(int)));
614  }
615 
616  sion_filedesc_master->keyvalmode=sion_get_keyval_mode(mapsid);
617 
618  if(*numFiles>1) {
619  /* 1a. search filemanager for files, granks with lrank=0 for each file in mapping (tasks reading first sion task of multi file) */
620  for(task=0;task<mapping_size;task++) {
621  if(mapping[task*2+1]==0) {
622  lfilemanager[mapping[task*2+0]]=task;
623  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " manager of file %d is grank=%d\n",mapping[task*2+0],task));
624  }
625  }
626  sion_filedesc_master->mapping_size=mapping_size;
627 
628  } else {
629  /* only one file */
630  sion_filedesc_master->mapping_size=lntasks;
631  lfilemanager[0]=0;
632  }
633 
634  } else {
635  *numFiles=-1;
636  free(sion_count);
637  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot find file %s , aborting ...\n",fname));
638  }
639  }
640 
641  /* broadcast information about mapping (number of global sion tasks) */
642  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_master->mapping_size, sion_gendata->comm_data_global, _SION_INT32, 1, 0);
643  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " mapping_size is=%d\n",sion_filedesc_master->mapping_size));
644 
645  /* and close file directly */
646  if(rank == 0) {
647  if (mapsid>=0) _sion_close_sid(mapsid); /* frees also mapping vector */
648  }
649 
650  if(*nlocaltasks<0) {
651  /* 2. If not globalranks parameter given */
652  /* 2.a. compute distribution */
653  /* --> simple blockwise distribution, later improvements with task-to-file relation */
654  int tasks_per_task = sion_filedesc_master->mapping_size / ntasks;
655  int startpos = rank*tasks_per_task;
656  int endpos = (rank+1)*tasks_per_task;
657  if(rank+1==ntasks) {
658  /* remaining tasks are added to last communicator */
659  if(endpos<sion_filedesc_master->mapping_size) endpos=sion_filedesc_master->mapping_size;
660  }
661  *nlocaltasks=endpos-startpos;
662  DPRINTFP((32,"sion_paropen_mapped_generic:",rank,"pre-compute distribution: startpos=%d endpos=%d nlocaltasks=%d\n",startpos, endpos, (int) *nlocaltasks ));
663 
664  if (globalranks == NULL) {
665  free(lfilemanager);
666  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"pre-compute distribution: cannot allocate globalranks, not pointer parameter given, aborting ...\n"));
667  }
668  if (mapping_filenrs == NULL) {
669  free(lfilemanager);
670  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"pre-compute distribution: cannot allocate mapping_filenrs, not pointer parameter given, aborting ...\n"));
671  }
672  if (mapping_lranks == NULL) {
673  free(lfilemanager);
674  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"pre-compute distribution: cannot allocate mapping_lranks, not pointer parameter given, aborting ...\n"));
675  }
676  if ((*globalranks) != NULL) {
677  free(lfilemanager);
678  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"pre-compute distribution: cannot allocate globalranks, memory allready allocated (unknown size), aborting ...\n"));
679  }
680  if ((*mapping_filenrs) != NULL) {
681  free(lfilemanager);
682  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"pre-compute distribution: cannot allocate mapping_filenrs, memory allready allocated (unknown size), aborting ...\n"));
683  }
684  if ((*mapping_lranks) != NULL) {
685  free(lfilemanager);
686  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"pre-compute distribution: cannot allocate mapping_lranks, memory allready allocated (unknown size), aborting ...\n"));
687  }
688 
689 
690  DPRINTFP((32,"sion_paropen_mapped_generic:",rank,"allocate globalranks field for parameter size=%d\n",(int) *nlocaltasks ));
691  *globalranks = (sion_int32 *) malloc(*nlocaltasks * sizeof(sion_int32));
692  if (*globalranks == NULL) {
693  free(lfilemanager);
694  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"cannot allocate memory of size %lu (globalranks), aborting ...\n", (unsigned long) sizeof(sion_int32)));
695  }
696  /* init globalranks vector */
697  ltask=0;
698  for(task=startpos;task<endpos;task++) {
699  (*globalranks)[ltask]=task;
700  DPRINTFP((32,"sion_paropen_mapped_generic:",rank,"set *globalranks[%d]=%d\n",(int) ltask,(*globalranks)[ltask]));
701  ltask++;
702  }
703 
704  }
705 
706  /* broadcast keyvalmode from file */
707  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_master->keyvalmode, sion_gendata->comm_data_global, _SION_INT32, 1, 0);
708  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " keyvalmode is=%d\n",sion_filedesc_master->keyvalmode));
709 
710  /* check for keyval parameter also on master */
711  _sion_keyval_check_env(sion_filedesc_master, file_mode_flags);
712 
713  /* 4. bcast numfiles to all */
714  if (rank == 0) helpint64=*numFiles;
715  sion_gendata->apidesc->bcastr_cb(&helpint64, sion_gendata->comm_data_global, _SION_INT64, 1, 0); *numFiles=helpint64;
716  sion_filedesc_master->nfiles=*numFiles;
717 
718  /* allocate on each task filedesc structure for each multifile */
719  sion_filedesc_master->multifiles = (_sion_filedesc **) malloc(sion_filedesc_master->nfiles * sizeof(_sion_filedesc*));
720  if (sion_filedesc_master->multifiles == NULL) {
721  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"cannot allocate filedescriptor structure vector of size %lu (chunksizes), aborting ...\n",
722  (unsigned long) sion_filedesc_master->nfiles * sizeof(_sion_filedesc*)));
723  }
724 
725  /* 5. Loop over files */
726  rootcounter=0; /* for round robin */
727  for(filenr=0;filenr<sion_filedesc_master->nfiles;filenr++) {
728 
729  DPRINTFP((4, "_sion_paropen_mapped_generic", rank, " starting init of SION #%d of %d\n", filenr,*numFiles));
730 
731 #ifdef _SION_FIND_ROOT_LRANK_NULL_DO_NOT_USE
732  {
733  int grankroot;
734  /* WARNING: this algorithm has some problems if no rank reads lrank-0 from file */
735  /* bcast grank with lrank=0 for this file */
736  grankroot=-1;
737  if (rank == 0) {
738  helpint64=lfilemanager[filenr];
739  }
740  sion_gendata->apidesc->bcastr_cb(&helpint64, sion_gendata->comm_data_global, _SION_INT64, 1, 0);
741  grankroot=helpint64;
742 
743  /* search for grankroot in my globalranks */
744  helpint32=-1;
745  for(task=0;task<*nlocaltasks;task++) {
746  if((*globalranks)[task]==grankroot) helpint32=1;
747  }
748 
749  /* gather on task 0 vector containing -1 or ranknumber for each task to determine which task is handling this file */
750  sion_gendata->apidesc->gatherr_cb(&helpint32, sion_count, sion_gendata->comm_data_global, _SION_INT32, 1, 0);
751  if (rank == 0) {
752  for(task=0;task<ntasks;task++) {
753  if(sion_count[task]==1) {
754  root=task;
755  break;
756  }
757  }
758  helpint32=root;
759  }
760  }
761 #endif
762 
763  /* round robin distribution of master role for a multi-file */
764  rootcounter=( (rootcounter+1) % ntasks);
765  helpint64=rootcounter;
766 
767  /* bcast root number for this file */
768  sion_gendata->apidesc->bcastr_cb(&helpint64, sion_gendata->comm_data_global, _SION_INT64, 1, 0);
769  root=helpint64;
770  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " manager of file %d is rank=%d\n",filenr,root));
771 
772  /* allocate and initialise internal data structure with default values (NULL and -1) */
773  sion_filedesc_sub = _sion_alloc_filedesc();
774  if (sion_filedesc_sub == NULL) {
775  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate filedescriptor structure of size %lu (sion_filedesc), aborting ...\n",
776  (unsigned long) sizeof(sion_filedesc_sub)));
777  }
778  sion_filedesc_master->multifiles[filenr]=sion_filedesc_sub;
779  _sion_init_filedesc(sion_filedesc_sub);
780  sion_filedesc_sub->fname = (sion_gendata->apidesc->get_multi_filename_cb?sion_gendata->apidesc->get_multi_filename_cb:_sion_get_multi_filename)(fname, filenr); /* Set the filename */
781  if(rank == root ){
782  sion_filedesc_sub->state = SION_FILESTATE_PAROPENMAPPEDMANAGED;
783  } else {
784  sion_filedesc_sub->state = SION_FILESTATE_PAROPENMAPPED;
785  }
786  sion_filedesc_sub->mode = SION_FILEMODE_READ;
787 
788  /* open file on each task */
789  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " starting open for read on file %s\n", sion_filedesc_sub->fname));
790  /* */ DPRINTFTS(rank, "before open");
791  sion_filedesc_sub->fileptr=NULL;
792  if (rank == root) {
793  sion_fileptr = _sion_file_open(sion_filedesc_sub->fname,apiflag|SION_FILE_FLAG_READ,0);
794  if (!sion_fileptr) {
795  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot open %s for reading, aborting ...\n", fname));
796  }
797  sion_filedesc_sub->fileptr = sion_fileptr;
798  }
799  sion_gendata->apidesc->barrier_cb(sion_gendata->comm_data_global);
800  /* */ DPRINTFTS(rank, "after open root");
801 
802  /* get meta data of file (1st Block ) on managing task */
803  if (rank == root) {
804  rc = _sion_read_header_fix_part(sion_filedesc_sub);
805  if (rc!=SION_SUCCESS) {
806  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot read header from file %s, aborting ...\n", fname));
807  }
808  DPRINTFP((32, "_sion_paropen_mapped_generic", rank,
809  " read, after read of fix header part endianness=0x%x blksize=%d ntasks=%d\n", sion_filedesc_sub->endianness, sion_filedesc_sub->fsblksize, sion_filedesc_sub->ntasks));
810 
811  /* */ DPRINTFTS(rank, "before alloc");
812  /* memory allocation (large fields, all sion tasks of file) */
813  _sion_alloc_filedesc_arrays(sion_filedesc_sub);
814  /* */ DPRINTFTS(rank, "after alloc");
815 
816  rc = _sion_read_header_var_part(sion_filedesc_sub);
817  if (rc != SION_SUCCESS) {
818  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot read header from file %s, aborting ...\n", fname));
819  }
820 
821  _sion_coll_check_env(sion_filedesc_sub);
822  if(sion_filedesc_sub->usecoll) _sion_alloc_filedesc_coll_arrays(sion_filedesc_sub);
823 
824  /* collective */
825  if (!sion_filedesc_sub->usecoll) _sion_calculate_startpointers(sion_filedesc_sub);
826  else _sion_calculate_startpointers_collective(sion_filedesc_sub);
827  /* */ DPRINTFTS(rank, "after calculate");
828 
829  /* check for keyval parameter */
830  _sion_keyval_check_env(sion_filedesc_sub, file_mode_flags);
831 
832  }
833 
834  /* distribute globalskip */
835  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->globalskip, sion_gendata->comm_data_global, _SION_INT64, 1, root);
836 
837  /* broadcast information read from file */
838  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->endianness, sion_gendata->comm_data_global, _SION_INT32, 1, root);
839  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->swapbytes, sion_gendata->comm_data_global, _SION_INT32, 1, root);
840  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->fsblksize, sion_gendata->comm_data_global, _SION_INT32, 1, root);
841  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->ntasks, sion_gendata->comm_data_global, _SION_INT32, 1, root);
842  sion_filedesc_sub->ntotaltasksinfile=sion_filedesc_sub->ntasks;
843  sion_filedesc_sub->nlocaltasksinfile=-1; /* has to be computed after distribution of globalranks */
844  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->fileversion, sion_gendata->comm_data_global, _SION_INT32, 1, root);
845  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->nfiles, sion_gendata->comm_data_global, _SION_INT32, 1, root);
846  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->filenumber, sion_gendata->comm_data_global, _SION_INT32, 1, root);
847  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->flag1, sion_gendata->comm_data_global, _SION_INT64, 1, root);
848  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->flag2, sion_gendata->comm_data_global, _SION_INT64, 1, root);
849  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->maxusedchunks, sion_gendata->comm_data_global, _SION_INT32, 1, root);
850  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->start_of_varheader, sion_gendata->comm_data_global, _SION_INT64, 1, root);
851  DPRINTFP((32, "_sion_paropen_mapped_generic", rank,
852  " read, after read of maxusedchunks=%d maxchunks=%d (%d) start_of_varheader=%d\n", sion_filedesc_sub->maxusedchunks,sion_filedesc_sub->maxchunks, MAXCHUNKS,(int) sion_filedesc_sub->start_of_varheader ));
853  if (sion_filedesc_sub->maxusedchunks > sion_filedesc_sub->maxchunks) _sion_realloc_filedesc_blocklist(sion_filedesc_sub, sion_filedesc_sub->maxusedchunks);
854  /* */ DPRINTFTS(rank, "after bcast");
855 
856  /* allocate temp field to receive data of all tasks in file; Data
857  distribution will be done by data sieving: meta data about all sion
858  tasks in file will be sent to all task and meta data about local sion
859  tasks will be selected from these fields */
860  tmpsize=sion_filedesc_sub->ntotaltasksinfile;
861  sion_tmpintfield2 = (sion_int64 *) malloc(tmpsize * sizeof(sion_int64));
862  if (sion_tmpintfield2 == NULL) {
863  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate temporary memory of size %lu (sion_tmpintfield2), aborting ...\n",
864  (unsigned long) tmpsize * sizeof(sion_int64)));
865  }
866 
867  /* bcast GLOBALRANKS number in this file */
868  if(rank==root) for(task=0;task<sion_filedesc_sub->ntotaltasksinfile;task++) sion_tmpintfield2[task]=sion_filedesc_sub->all_globalranks[task];
869  sion_gendata->apidesc->bcastr_cb(sion_tmpintfield2, sion_gendata->comm_data_global, _SION_INT64, sion_filedesc_sub->ntotaltasksinfile, root);
870 
871  /* compare globalrank number list with list of globalrank to open on this task */
872  sion_filedesc_sub->nlocaltasksinfile=0;
873  for(task=0;task<sion_filedesc_sub->ntotaltasksinfile;task++) {
874  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " scan for gtask=%d\n", (int) sion_tmpintfield2[task]));
875  for(ltask=0;ltask<*nlocaltasks;ltask++) {
876  if( (int) sion_tmpintfield2[task]==(*globalranks)[ltask]) {
877  sion_tmpintfield2[task]=-1 * (sion_tmpintfield2[task] + 1); /* set mask (set to negative value) */
878  sion_filedesc_sub->nlocaltasksinfile++;
879  }
880  }
881  }
882  DPRINTFP((32, "_sion_paropen_mapped_generic", rank, " found %d globalranks for file %s\n", sion_filedesc_sub->nlocaltasksinfile, sion_filedesc_sub->fname));
883 
884  /* */ DPRINTFTS(rank, "before open non-root");
885  /* if really necessary (not needed when on this task no globalranks are stored in this file) */
886  if ( (rank != root) && (sion_filedesc_sub->nlocaltasksinfile>0) ) {
887  sion_fileptr = _sion_file_open(sion_filedesc_sub->fname,apiflag|SION_FILE_FLAG_READ,0);
888  if (!sion_fileptr) {
889  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot open %s for reading, aborting ...\n", fname));
890  }
891  sion_filedesc_sub->fileptr = sion_fileptr;
892  }
893  /* sion_gendata->apidesc->barrier_cb(sion_gendata->comm_data_global); */
894  /* */ DPRINTFTS(rank, "after open non-root");
895 
896 
897  /* allocate internal arrays (small size, only sion tasks needed by this task) */
898  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
899  /* free on root some of the large fields */
900  _sion_free_filedesc_all_globalranks(sion_filedesc_sub);
901  /* and realloc with less memory */
902  sion_filedesc_sub->ntasks=sion_filedesc_sub->nlocaltasksinfile;
903  _sion_alloc_filedesc_all_globalranks(sion_filedesc_sub);
904  _sion_alloc_filedesc_all_localranks(sion_filedesc_sub);
905  /* all_chunksizes will follow later */
906  } else {
907  sion_filedesc_sub->ntasks=sion_filedesc_sub->nlocaltasksinfile;
908  _sion_alloc_filedesc_arrays(sion_filedesc_sub);
909  _sion_alloc_filedesc_all_localranks(sion_filedesc_sub);
910  }
911 
912  /* store position of local tasks in this file */
913  ltask=0;
914  for(task=0;task<sion_filedesc_sub->ntotaltasksinfile;task++) {
915  if(sion_tmpintfield2[task]<0) {
916  sion_filedesc_sub->all_localranks[ltask]=task;
917  sion_filedesc_sub->all_globalranks[ltask]= (-1 * sion_tmpintfield2[task]) - 1;
918  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " all_globalranks[%d]=%d all_localranks[%d]=%d\n",
919  ltask,(int) sion_filedesc_sub->all_globalranks[ltask],ltask,(int) sion_filedesc_sub->all_localranks[ltask]));
920  ltask++;
921  }
922  }
923 
924  /* bcast CHUNKSIZES */
925  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
926  for(task=0;task<sion_filedesc_sub->ntotaltasksinfile;task++) sion_tmpintfield2[task]=sion_filedesc_sub->all_chunksizes[task];
927  }
928  sion_gendata->apidesc->bcastr_cb(sion_tmpintfield2, sion_gendata->comm_data_global, _SION_INT64, sion_filedesc_sub->ntotaltasksinfile, root);
929  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
930  _sion_free_filedesc_all_chunksizes(sion_filedesc_sub); /* free on root large field */
931  _sion_alloc_filedesc_all_chunksizes(sion_filedesc_sub); /* and realloc with less memory */
932  }
933  /* store in local field */
934  for(ltask=0;ltask<sion_filedesc_sub->nlocaltasksinfile;ltask++) {
935  task=sion_filedesc_sub->all_localranks[ltask];
936  sion_filedesc_sub->all_chunksizes[ltask]=sion_tmpintfield2[task];
937  }
938 
939  /* bcast STARTPOINTERS */
940  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
941  for(task=0;task<sion_filedesc_sub->ntotaltasksinfile;task++) sion_tmpintfield2[task]=sion_filedesc_sub->all_startpointers[task];
942  }
943  sion_gendata->apidesc->bcastr_cb(sion_tmpintfield2, sion_gendata->comm_data_global, _SION_INT64, sion_filedesc_sub->ntotaltasksinfile, root);
944  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
945  _sion_free_filedesc_all_startpointers(sion_filedesc_sub); /* free on root large field */
946  _sion_alloc_filedesc_all_startpointers(sion_filedesc_sub); /* and realloc with less memory */
947  }
948  /* store in local field */
949  for(ltask=0;ltask<sion_filedesc_sub->nlocaltasksinfile;ltask++) {
950  task=sion_filedesc_sub->all_localranks[ltask];
951  sion_filedesc_sub->all_startpointers[ltask]=sion_tmpintfield2[task];
952  }
953 
954  /* allocate memory for block info */
955  _sion_alloc_filedesc_block_arrays(sion_filedesc_sub);
956 
957  /* bcast BLOCKCOUNT */
958  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
959  sion_filedesc_sub->ntasks=sion_filedesc_sub->ntotaltasksinfile;
960  _sion_read_header_var_part_blockcount_to_field(sion_filedesc_sub, sion_filedesc_sub->ntotaltasksinfile, sion_tmpintfield2);
961  sion_filedesc_sub->ntasks=sion_filedesc_sub->nlocaltasksinfile;
962  for(task=0;task<sion_filedesc_sub->ntotaltasksinfile;task++)
963  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " got from file blockcount[%d]=%d\n",task,(int) sion_tmpintfield2[task]));
964  }
965  sion_gendata->apidesc->bcastr_cb(sion_tmpintfield2, sion_gendata->comm_data_global, _SION_INT64, sion_filedesc_sub->ntotaltasksinfile, root);
966  /* store in local field */
967  for(ltask=0;ltask<sion_filedesc_sub->nlocaltasksinfile;ltask++) {
968  task=sion_filedesc_sub->all_localranks[ltask];
969  sion_filedesc_sub->all_blockcount[ltask]=sion_tmpintfield2[task];
970  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " store blockcount task=%d -> ltask=%d cnt=%d\n",task,ltask,(int) sion_tmpintfield2[task]));
971 
972  }
973 
974  /* bcast BLOCKSIZES */
975  for (blknum = 0; blknum < sion_filedesc_sub->maxusedchunks; blknum++) {
976  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
977  sion_filedesc_sub->ntasks=sion_filedesc_sub->ntotaltasksinfile;
978  _sion_read_header_var_part_nextblocksizes_to_field(sion_filedesc_sub, sion_filedesc_sub->ntotaltasksinfile, sion_tmpintfield2);
979  sion_filedesc_sub->ntasks=sion_filedesc_sub->nlocaltasksinfile;
980  }
981  sion_gendata->apidesc->bcastr_cb(sion_tmpintfield2, sion_gendata->comm_data_global, _SION_INT64, sion_filedesc_sub->ntotaltasksinfile, root);
982  /* store in local field */
983  for(ltask=0;ltask<sion_filedesc_sub->nlocaltasksinfile;ltask++) {
984  task=sion_filedesc_sub->all_localranks[ltask];
985  sion_filedesc_sub->all_blocksizes[sion_filedesc_sub->nlocaltasksinfile * blknum + ltask]=sion_tmpintfield2[task];
986  }
987  }
988 
989  /* distribute keyval options */
990  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->keyvalmode, sion_gendata->comm_data_global, _SION_INT32, 1, root);
991 
992  /* distribute collective options */
993  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->usecoll, sion_gendata->comm_data_global, _SION_INT32, 1, root);
994  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->collsize, sion_gendata->comm_data_global, _SION_INT32, 1, root);
995 
996  if(sion_filedesc_sub->usecoll) {
997 
998  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
999  for(task=0;task<sion_filedesc_sub->ntotaltasksinfile;task++)
1000  sion_tmpintfield2[task]= (sion_int64) (sion_filedesc_sub->all_coll_collector[task]*_SION_CONST_MAX_INT32) + sion_filedesc_sub->collsize;
1001  }
1002  sion_gendata->apidesc->bcastr_cb(sion_tmpintfield2, sion_gendata->comm_data_global, _SION_INT64, sion_filedesc_sub->ntotaltasksinfile, root);
1003  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
1004  _sion_free_filedesc_coll_arrays(sion_filedesc_sub); /* free on root large field */
1005  }
1006  _sion_alloc_filedesc_all_chunksizes(sion_filedesc_sub); /* and alloc with less memory on all tasks */
1007 
1008  /* store in local field */
1009  DPRINTFP((64, "_sion_paropen_mapped_generic", rank, " store collsize+collector for %d elements\n",sion_filedesc_sub->nlocaltasksinfile));
1010  for(ltask=0;ltask<sion_filedesc_sub->nlocaltasksinfile;ltask++) {
1011  task=sion_filedesc_sub->all_localranks[ltask];
1012  sion_filedesc_sub->all_coll_collsize[sion_filedesc_sub->nlocaltasksinfile * blknum + ltask]=(sion_int32) sion_tmpintfield2[task]%_SION_CONST_MAX_INT32;
1013  sion_filedesc_sub->all_coll_collector[sion_filedesc_sub->nlocaltasksinfile * blknum + ltask]= (sion_int32)
1014  (sion_tmpintfield2[task]-sion_filedesc_sub->all_coll_collsize[sion_filedesc_sub->nlocaltasksinfile * blknum + ltask])/_SION_CONST_MAX_INT32;
1015  }
1016  }
1017 
1018  /* init rest of data structure */
1019  if(sion_filedesc_sub->nlocaltasksinfile>0) {
1020  for (ltask = 0; ltask < sion_filedesc_sub->nlocaltasksinfile; ltask++) {
1021  sion_filedesc_sub->all_currentpos[ltask] = sion_filedesc_sub->all_startpointers[ltask];
1022  sion_filedesc_sub->all_currentblocknr[ltask] = 0;
1023  }
1024  sion_filedesc_sub->rank = 0;
1025  sion_filedesc_sub->globalrank = sion_filedesc_sub->all_globalranks[sion_filedesc_sub->rank];
1026  sion_filedesc_sub->chunksize = sion_filedesc_sub->all_chunksizes[0];
1027  sion_filedesc_sub->startpos = sion_filedesc_sub->all_startpointers[0];
1028  sion_filedesc_sub->currentpos = sion_filedesc_sub->startpos;
1029  sion_filedesc_sub->currentblocknr = 0;
1030  sion_filedesc_sub->lastchunknr = sion_filedesc_sub->all_blockcount[sion_filedesc_sub->rank]-1;
1031 
1032  /* allocate memory for keyval if necessary (small size) */
1033  if(sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) {
1034  _sion_alloc_filedesc_all_keyvalptr(sion_filedesc_sub);
1035  }
1036 
1037 
1038  _sion_file_purge(sion_fileptr);
1039  _sion_file_set_position(sion_fileptr, sion_filedesc_sub->currentpos);
1040  }
1041  if(sion_tmpintfield2) free(sion_tmpintfield2);
1042 
1043  } /* filenr */
1044 
1045  /* set master to first file, first available rank */
1046 
1047  /* lookup file which contains current rank and set master */
1048  {
1049  int lfile=-1,lrank=-1, blknum;
1050  sion_filedesc_sub=NULL;
1051  for(filenr=0;filenr<sion_filedesc_master->nfiles;filenr++) {
1052  sion_filedesc_sub=sion_filedesc_master->multifiles[filenr];
1053  if(sion_filedesc_sub->nlocaltasksinfile>0) {
1054  lfile=filenr;
1055  lrank=0;
1056  break;
1057  }
1058  }
1059 
1060  DPRINTFP((32,"sion_paropen_mapped_generic",rank,"init to first local rank in first file (%d, %d)\n",lfile,lrank));
1061 
1062  if((sion_filedesc_sub) && (lrank>=0) && (lfile>=0)) {
1063  DPRINTFP((32,"sion_paropen_mapped_generic",rank,"sion_filedesc_master->mapping_size=%d\n",sion_filedesc_master->mapping_size));
1064 
1065  sion_filedesc_master->ntotaltasksinfile = sion_filedesc_master->mapping_size;
1066  sion_filedesc_master->endianness = sion_filedesc_sub->endianness;
1067  sion_filedesc_master->swapbytes = sion_filedesc_sub->swapbytes;
1068  sion_filedesc_master->globalrank = sion_filedesc_sub->all_globalranks[lrank];
1069  sion_filedesc_master->fileversion = sion_filedesc_sub->fileversion;
1070  sion_filedesc_master->filesionversion = sion_filedesc_sub->filesionversion;
1071  sion_filedesc_master->filesionpatchlevel = sion_filedesc_sub->filesionpatchlevel;
1072  sion_filedesc_master->rank = lrank;
1073  sion_filedesc_master->fsblksize = sion_filedesc_sub->fsblksize;
1074  sion_filedesc_master->filenumber = lfile;
1075 
1076  /* set info for current rank and position */
1077  sion_filedesc_master->chunksize = sion_filedesc_sub->all_chunksizes[lrank];
1078  sion_filedesc_master->startpos = sion_filedesc_sub->all_startpointers[lrank];
1079  sion_filedesc_master->currentpos = sion_filedesc_master->startpos;
1080  sion_filedesc_master->globalskip = sion_filedesc_sub->globalskip;
1081 
1082  sion_filedesc_master->currentblocknr = 0;
1083  sion_filedesc_master->lastchunknr = sion_filedesc_sub->all_blockcount[lrank]-1;
1084 
1085  sion_filedesc_master->start_of_varheader = sion_filedesc_sub->start_of_varheader;
1086 
1087 
1088  /* set maxusedchunks to maxusedchunks of all files */
1089  sion_filedesc_master->maxusedchunks = sion_filedesc_sub->maxusedchunks;
1090  for(filenr=0;filenr<sion_filedesc_sub->nfiles;filenr++) {
1091  if (sion_filedesc_master->maxusedchunks < sion_filedesc_master->multifiles[filenr]->maxusedchunks)
1092  sion_filedesc_master->maxusedchunks = sion_filedesc_master->multifiles[filenr]->maxusedchunks;
1093  }
1094  _sion_realloc_filedesc_blocklist(sion_filedesc_master, sion_filedesc_master->maxusedchunks);
1095  for (blknum = 0; blknum < sion_filedesc_sub->all_blockcount[lrank]; blknum++) {
1096  sion_filedesc_master->blocksizes[blknum] = sion_filedesc_sub->all_blocksizes[sion_filedesc_sub->nlocaltasksinfile * blknum + lrank];
1097  }
1098 
1099 
1100  /* set file pointer */
1101  sion_filedesc_master->fileptr = sion_filedesc_master->multifiles[lfile]->fileptr;
1102 
1103  /* set position */
1104  _sion_file_flush(sion_filedesc_master->fileptr);
1105 
1106  DPRINTFP((32,"sion_paropen_mapped_generic",rank,"set startpointer to %d\n",(int) sion_filedesc_master->currentpos));
1107  _sion_file_set_position(sion_filedesc_master->fileptr, sion_filedesc_master->currentpos);
1108 
1109  }
1110  } /* block */
1111 
1112  /* OUTPUT parameters */
1113  if(fileptr!=NULL) {
1114  if ( (sion_filedesc_master->fileptr!=NULL) && (sion_filedesc_master->fileptr->flags&&SION_FILE_FLAG_ANSI) ) {
1115  *fileptr=sion_filedesc_master->fileptr->fileptr;
1116  sion_filedesc_master->fileptr_exported=1;
1117  } else {
1118  *fileptr=NULL;
1119  sion_filedesc_master->fileptr_exported=0;
1120  }
1121  }
1122 
1123  {
1124  sion_int64 *helpptr_chunksize = NULL;
1125  sion_int32 *helpptr_mapping_filenrs = NULL;
1126  sion_int32 *helpptr_mapping_lranks = NULL;
1127 
1128 
1129  if (chunksizes != NULL) {
1130  if ((*chunksizes) == NULL) {
1131  DPRINTFP((32,"sion_paropen_mapped_generic:",rank,"allocate chunksizes field for parameter size=%d\n",(int) *nlocaltasks ));
1132  helpptr_chunksize = (sion_int64 *) malloc(*nlocaltasks * sizeof(sion_int64));
1133  if (helpptr_chunksize == NULL) {
1134  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"cannot allocate filedescriptor structure of size %lu (chunksizes), aborting ...\n", (unsigned long) sizeof(sion_int64)));
1135  }
1136  *chunksizes = helpptr_chunksize;
1137  } else {
1138  helpptr_chunksize = *chunksizes;
1139  }
1140  }
1141 
1142  if (mapping_filenrs != NULL) {
1143  if ((*mapping_filenrs) == NULL) {
1144  DPRINTFP((32,"sion_paropen_mapped_generic:",rank,"allocate mapping_filenrs field for parameter size=%d\n",(int) *nlocaltasks ));
1145  helpptr_mapping_filenrs = (sion_int32 *) malloc(*nlocaltasks * sizeof(sion_int32));
1146  if (helpptr_mapping_filenrs == NULL) {
1147  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"cannot allocate filedescriptor structure of size %lu (mapping_filenrs), aborting ...\n", (unsigned long) sizeof(sion_int32)));
1148  }
1149  *mapping_filenrs = helpptr_mapping_filenrs;
1150  } else {
1151  helpptr_mapping_filenrs = *mapping_filenrs;
1152  }
1153  }
1154 
1155  if (mapping_lranks != NULL) {
1156  if ((*mapping_lranks) == NULL) {
1157  DPRINTFP((32,"sion_paropen_mapped_generic:",rank,"allocate mapping_lranks field for parameter size=%d\n",(int) *nlocaltasks ));
1158  helpptr_mapping_lranks = (sion_int32 *) malloc(*nlocaltasks * sizeof(sion_int32));
1159  if (helpptr_mapping_lranks == NULL) {
1160  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"cannot allocate filedescriptor structure of size %lu (mapping_lranks), aborting ...\n", (unsigned long) sizeof(sion_int32)));
1161  }
1162  *mapping_lranks = helpptr_mapping_lranks;
1163  } else {
1164  helpptr_mapping_lranks = *mapping_lranks;
1165  }
1166  }
1167 
1168 
1169  if(helpptr_chunksize || helpptr_mapping_filenrs || helpptr_mapping_lranks) {
1170  for(filenr=0;filenr<sion_filedesc_master->nfiles;filenr++) {
1171  sion_filedesc_sub=sion_filedesc_master->multifiles[filenr];
1172  for(ltask=0;ltask<sion_filedesc_sub->nlocaltasksinfile;ltask++) {
1173  for(task=0;task<*nlocaltasks;task++) {
1174  if((*globalranks)[task]==sion_filedesc_sub->all_globalranks[ltask]) {
1175  DPRINTFP((32,"sion_paropen_mapped_generic:",rank," set chunksizes[%d]=%4d (filenr=%d, ltask=%d lrank=%d)\n",
1176  (int) task, (int) sion_filedesc_sub->all_chunksizes[ltask],filenr,ltask, sion_filedesc_sub->all_localranks[ltask]));
1177  if(helpptr_chunksize) helpptr_chunksize[task] = sion_filedesc_sub->all_chunksizes[ltask];
1178  if(helpptr_mapping_filenrs) helpptr_mapping_filenrs[task] = filenr;
1179  if(helpptr_mapping_lranks) helpptr_mapping_lranks[task] = sion_filedesc_sub->all_localranks[ltask];
1180  }
1181  }
1182  }
1183  }
1184  }
1185 
1186  } /* block */
1187 
1188  _sion_print_filedesc(sion_filedesc_master, 512, "_sion_paropen_mapped_generic", _SION_DEBUG_PRINT_ALL|_SION_DEBUG_PRINT_RECURSIVE);
1189 
1190 
1191  if(sion_count) free(sion_count);
1192 
1193  if(lfilemanager) free(lfilemanager);
1194 
1195 
1196 
1197  } else {
1198  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"sion_paropen_multi_mpi: unknown file mode"));
1199  }
1200 
1201 
1202  return (sid);
1203 }
1204 
1205 int _sion_parclose_mapped_generic( int sid,
1206  int rank,
1207  int ntasks,
1208  _sion_generic_gendata *sion_gendata ) {
1209  int rc=SION_SUCCESS;
1210  int lfile, grank, lrank, blknum, filenr, tmpsize, root, ltask, mappingroot, task;
1211  _sion_filedesc *sion_filedesc_master;
1212  _sion_filedesc *sion_filedesc_sub;
1213  sion_int64 *sion_tmpintfield_send = NULL;
1214  sion_int64 *sion_tmpintfield_lrank_recv = NULL;
1215  sion_int64 *sion_tmpintfield_data_recv = NULL;
1216  sion_int64 *sion_tmpintfield_data = NULL;
1217  sion_int32 *mapping = NULL;
1218  sion_int32 *sion_tmpint32field_send = NULL;
1219  sion_int32 *sion_tmpint32field_data_recv = NULL;
1220  sion_int32 *sion_count = NULL;
1221  sion_int32 helpint32;
1222 
1223  DPRINTFP((2, "_sion_parclose_mapped_generic", rank, "enter parallel close sid=%d\n", sid));
1224 
1225  if ((_sion_vcdtype(sid) != SION_FILEDESCRIPTOR) || !(sion_filedesc_master = _sion_vcdtovcon(sid))) {
1226  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_parclose_generic: invalid sion_filedesc_master, aborting %d ...\n", sid));
1227  }
1228 
1229 #ifdef SION_DEBUG
1230  {
1231  int numbytes, numfds;
1232  sion_get_sizeof(sid, &numbytes, &numfds);
1233  DPRINTFP((2, "_sion_parclose_mapped_generic", rank, "internal data size of sid %2d (%d bytes, %d fds) \n", sid,numbytes, numfds));
1234  }
1235 #endif
1236 
1237  if ((sion_filedesc_master->state != SION_FILESTATE_PAROPENMAPPED)
1238  && (sion_filedesc_master->state != SION_FILESTATE_PAROPENMAPPEDMASTER)
1239  && (sion_filedesc_master->state != SION_FILESTATE_PAROPENMAPPEDMANAGED)
1240  ) {
1241  return(_sion_errorprint_on_rank(SION_NOT_SUCCESS,_SION_ERROR_RETURN,sion_filedesc_master->rank,"sion_parclose_mapped: invalid file open state (!PAROPENMAPPED), aborting %d ...", sid));
1242  }
1243 
1244  /* READ MODE: close files on all tasks */
1245  if (sion_filedesc_master->mode == SION_FILEMODE_READ) {
1246 
1247  _sion_print_filedesc(sion_filedesc_master, 512, "_sion_parclose_mapped_generic", _SION_DEBUG_PRINT_ALL|_SION_DEBUG_PRINT_RECURSIVE);
1248 
1249  if(sion_filedesc_master->state == SION_FILESTATE_PAROPENMAPPEDMASTER) {
1250 
1251 
1252  /* pointer to keyval structure -> all_keyvalptr */
1253  if(sion_filedesc_master->keyvalmode!=SION_KEYVAL_NONE) {
1254  lfile=sion_filedesc_master->filenumber;
1255  lrank=sion_filedesc_master->rank; /* index to local list */
1256  if((lrank>=0) && (lfile>=0)) {
1257  sion_filedesc_sub=sion_filedesc_master->multifiles[lfile];
1258  sion_filedesc_sub->all_keyvalptr[lrank] = sion_filedesc_master->keyvalptr;
1259  }
1260  }
1261 
1262  /* loop again over files to close and collect mapping data*/
1263  for(filenr=0;filenr<sion_filedesc_master->nfiles;filenr++) {
1264  sion_filedesc_sub=sion_filedesc_master->multifiles[filenr];
1265 
1266  DPRINTFP((32, "_sion_parclose_mapped_generic", rank, " parallel close (read mode) call fclose on file %s (fileptr=%x)\n", sion_filedesc_sub->fname,sion_filedesc_sub->fileptr));
1267  if (sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_sub->keyvalptr = NULL;
1268  if(sion_filedesc_sub->fileptr!=NULL) {
1269  _sion_file_close(sion_filedesc_sub->fileptr);
1270  sion_filedesc_sub->fileptr = NULL;
1271  }
1272  sion_filedesc_sub->state = SION_FILESTATE_CLOSE;
1273  if(sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE && (sion_filedesc_sub->keyvalptr!=NULL) && (sion_filedesc_sub->all_keyvalptr!=NULL))
1274  sion_filedesc_sub->all_keyvalptr[sion_filedesc_sub->rank] = sion_filedesc_sub->keyvalptr;
1275 
1276  _sion_free_filedesc(sion_filedesc_sub);
1277  }
1278  }
1279  _SION_SAFE_FREE(mapping, NULL);
1280  _SION_SAFE_FREE(sion_filedesc_master->multifiles, NULL);
1281 
1282  /* the top-level keyvalptr is a pointer to one of the all_keyvalptr of the subfiles, which are already freed */
1283  if(sion_filedesc_master->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_master->keyvalptr = NULL;
1284 
1285  _sion_free_filedesc(sion_filedesc_master);
1286  sion_filedesc_master = NULL;
1287 
1288  } else {
1289  /* WRITE MODE: gather data from all tasks and close files on all tasks */
1290 
1291  /* update meta data of current rank on master */
1292  _sion_flush_block(sion_filedesc_master);
1293 
1294  if (sion_filedesc_master->usebuffer) {
1295  _sion_buffer_flush(sion_filedesc_master);
1296  }
1297 
1298  _sion_print_filedesc(sion_filedesc_master, 512, "_sion_parclose_mapped_generic", _SION_DEBUG_PRINT_ALL|_SION_DEBUG_PRINT_RECURSIVE);
1299 
1300  /* transfer meta data to corresponding sub datastructure */
1301  lfile=sion_filedesc_master->filenumber;
1302  lrank=sion_filedesc_master->rank; /* index to local list */
1303  sion_filedesc_sub=sion_filedesc_master->multifiles[lfile];
1304 
1305  /* pointer to keyval structure */
1306  if(sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_sub->keyvalptr = sion_filedesc_master->keyvalptr;
1307 
1308  sion_filedesc_sub->currentpos = sion_filedesc_master->currentpos;
1309  sion_filedesc_sub->currentblocknr = sion_filedesc_master->currentblocknr;
1310  sion_filedesc_sub->lastchunknr = sion_filedesc_master->lastchunknr;
1311 
1312  /* pointer to keyval structure -> all_keyvalptr */
1313  if(sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_sub->all_keyvalptr[lrank] = sion_filedesc_sub->keyvalptr;
1314 
1315  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, "on file %d: sub,maxchunk=%d master,maxchunk=%d \n", lfile,sion_filedesc_sub->maxchunks, sion_filedesc_master->maxchunks));
1316  if(sion_filedesc_sub->maxchunks < sion_filedesc_master->maxchunks) {
1317  _sion_realloc_filedesc_blocklist(sion_filedesc_sub, sion_filedesc_master->maxchunks);
1318  }
1319 
1320  /* store data of current rank on sub datastructure */
1321  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, "store current information lrank=%d lastchunknr=%d\n", lrank,sion_filedesc_sub->lastchunknr));
1322  sion_filedesc_sub->all_currentpos[lrank] = sion_filedesc_sub->currentpos;
1323  sion_filedesc_sub->all_currentblocknr[lrank] = sion_filedesc_sub->lastchunknr;
1324  sion_filedesc_sub->all_blockcount[lrank] = sion_filedesc_sub->lastchunknr + 1;
1325  for (blknum = 0; blknum <= sion_filedesc_sub->lastchunknr; blknum++) {
1326  sion_filedesc_sub->blocksizes[blknum] = sion_filedesc_master->blocksizes[blknum];
1327  sion_filedesc_sub->all_blocksizes[sion_filedesc_sub->ntasks * blknum + lrank] = sion_filedesc_master->blocksizes[blknum];
1328  }
1329 
1330  sion_count = (sion_int32 *) malloc(ntasks * sizeof(sion_int32));
1331  if (sion_count == NULL) {
1332  return(_sion_errorprint(SION_ID_NOT_VALID,_SION_ERROR_RETURN,"_sion_paropen_mapped_generic: cannot allocate temporary memory of size %lu (sion_count), aborting ...\n",
1333  (unsigned long) ntasks * sizeof(sion_int32)));
1334  }
1335 
1336  /* loop over all files */
1337  for(filenr=0;filenr<sion_filedesc_master->nfiles;filenr++) {
1338 
1339  sion_filedesc_sub=sion_filedesc_master->multifiles[filenr];
1340  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, " starting close for file %d: %s \n", filenr,sion_filedesc_sub->fname));
1341 
1342  if (sion_filedesc_sub->usebuffer) {
1343  _sion_buffer_flush(sion_filedesc_sub);
1344  }
1345 
1346  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPED) {
1347  DPRINTFP((32, "_sion_parclose_mapped_generic", rank, ">parallel close (write mode, not managed) call fclose on file %s (fileptr=%x)\n", sion_filedesc_sub->fname,sion_filedesc_sub->fileptr));
1348  _sion_file_close(sion_filedesc_sub->fileptr);
1349  DPRINTFP((32, "_sion_parclose_mapped_generic", rank, "<parallel close (write mode, not managed) call fclose on file %s (fileptr=%x)\n", sion_filedesc_sub->fname,sion_filedesc_sub->fileptr));
1350  sion_filedesc_sub->fileptr = NULL;
1351  sion_filedesc_sub->state = SION_FILESTATE_CLOSE;
1352 
1353  }
1354 
1355  sion_gendata->apidesc->barrier_cb(sion_gendata->comm_data_global);
1356 
1357  /* allocate send field */
1358  tmpsize=sion_filedesc_sub->nlocaltasksinfile;
1359  sion_tmpintfield_send = (sion_int64 *) malloc(tmpsize * sizeof(sion_int64));
1360  if (sion_tmpintfield_send == NULL) {
1361  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_parclose_generic: cannot allocate temporary memory of size %lu (sion_tmpintfield_send), aborting ...\n",
1362  (unsigned long) tmpsize * sizeof(sion_int64)));
1363  }
1364  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, " file %d: allocate send field for all local tasks of file (%d) --> %x\n", filenr, tmpsize, sion_tmpintfield_send));
1365 
1366  root=sion_filedesc_sub->filemanagedbytask;
1367  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
1368 
1369  /* allocate receive fields */
1370  tmpsize=sion_filedesc_sub->ntotaltasksinfile;
1371  sion_tmpintfield_lrank_recv = (sion_int64 *) malloc(tmpsize * sizeof(sion_int64));
1372  if (sion_tmpintfield_lrank_recv == NULL) {
1373  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_parclose_generic: cannot allocate temporary memory of size %lu (sion_tmpintfield_lrank_recv), aborting ...\n",
1374  (unsigned long) tmpsize * sizeof(sion_int64)));
1375  }
1376  sion_tmpintfield_data_recv = (sion_int64 *) malloc(tmpsize * sizeof(sion_int64));
1377  if (sion_tmpintfield_data_recv == NULL) {
1378  free(sion_tmpintfield_lrank_recv);
1379  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_parclose_generic: cannot allocate temporary memory of size %lu (sion_tmpintfield_data_recv), aborting ...\n",
1380  (unsigned long) tmpsize * sizeof(sion_int64)));
1381  }
1382  sion_tmpintfield_data = (sion_int64 *) malloc(tmpsize * sizeof(sion_int64));
1383  if (sion_tmpintfield_data == NULL) {
1384  free(sion_tmpintfield_data_recv);
1385  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_parclose_generic: cannot allocate temporary memory of size %lu (sion_tmpintfield_data), aborting ...\n",
1386  (unsigned long) tmpsize * sizeof(sion_int64)));
1387  }
1388  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, " file %d: allocate fields for all tasks of file (%d)\n", filenr,tmpsize));
1389 
1390 
1391  } else sion_tmpintfield_lrank_recv=sion_tmpintfield_data_recv=sion_tmpintfield_data=NULL;
1392 
1393  /* get number of tasks writing to this file */
1394  helpint32 = sion_filedesc_sub->nlocaltasksinfile;
1395  sion_gendata->apidesc->gatherr_cb(&helpint32, sion_count, sion_gendata->comm_data_global, _SION_INT32, 1, root);
1396 
1397  /* collect local rank mapping */
1398  for (ltask = 0; ltask < sion_filedesc_sub->nlocaltasksinfile; ltask++) {
1399  sion_tmpintfield_send[ltask]=sion_filedesc_sub->all_localranks[ltask];
1400  }
1401  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, " file %d: before call of gathervr_cb %x %x %x %d %d %d\n",
1402  filenr,sion_tmpintfield_lrank_recv, sion_tmpintfield_send, sion_gendata->comm_data_global, _SION_INT64, sion_filedesc_sub->nlocaltasksinfile, root));
1403 
1404  sion_gendata->apidesc->gathervr_cb(sion_tmpintfield_send, sion_tmpintfield_lrank_recv, sion_gendata->comm_data_global, _SION_INT64, sion_count, sion_filedesc_sub->nlocaltasksinfile, root);
1405 
1406  /* collect number of written chunks (blocks) for each file task */
1407  for (ltask = 0; ltask < sion_filedesc_sub->nlocaltasksinfile; ltask++) {
1408  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, " send all_blockcount[%d]: %d\n", ltask, sion_filedesc_sub->all_blockcount[ltask]));
1409  sion_tmpintfield_send[ltask]=sion_filedesc_sub->all_blockcount[ltask];
1410  }
1411  sion_gendata->apidesc->gathervr_cb(sion_tmpintfield_send, sion_tmpintfield_data_recv, sion_gendata->comm_data_global, _SION_INT64, sion_count, sion_filedesc_sub->nlocaltasksinfile, root);
1412 
1413  /* search maxusedchunks */
1414  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
1415  sion_filedesc_sub->maxusedchunks = -1;
1416  for (ltask = 0; ltask < sion_filedesc_sub->ntotaltasksinfile; ltask++) {
1417  if (sion_tmpintfield_data_recv[ltask] > sion_filedesc_sub->maxusedchunks) {
1418  sion_filedesc_sub->maxusedchunks = (int) sion_tmpintfield_data_recv[ltask];
1419  }
1420 
1421  }
1422  }
1423  sion_gendata->apidesc->bcastr_cb(&sion_filedesc_sub->maxusedchunks, sion_gendata->comm_data_global, _SION_INT32, 1, root);
1424  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, " file %d: maxusedchunks=%d\n", filenr,sion_filedesc_sub->maxusedchunks));
1425 
1426  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
1427 
1428  /* sort data */
1429  for(ltask=0;ltask<sion_filedesc_sub->ntotaltasksinfile;ltask++) {
1430  lrank=(int) sion_tmpintfield_lrank_recv[ltask];
1431  DPRINTFP((64, "_sion_parclose_mapped_generic", rank, " sort in ltask=%d --> lrank=%d blkcount=%d\n",
1432  ltask,lrank,sion_tmpintfield_data_recv[ltask]));
1433  sion_tmpintfield_data[lrank]=sion_tmpintfield_data_recv[ltask];
1434 
1435  }
1436  /* calculate and set start_of_varheader */
1437  sion_filedesc_sub->start_of_varheader = sion_filedesc_sub->start_of_data + sion_filedesc_sub->maxusedchunks * sion_filedesc_sub->globalskip;
1438 
1439  /* adjust ntasks to total number so that internal write routines work correctly */
1440  sion_filedesc_sub->ntasks=sion_filedesc_sub->ntotaltasksinfile;
1441 
1442  /* write rest of first meta data block on rank 0 */
1443  _sion_write_header_var_info(sion_filedesc_sub);
1444 
1445  _sion_write_header_var_part_blockcount_from_field(sion_filedesc_sub,sion_filedesc_sub->ntotaltasksinfile,sion_tmpintfield_data);
1446  }
1447 
1448  for (blknum = 0; blknum < sion_filedesc_sub->maxusedchunks; blknum++) {
1449 
1450  DPRINTFP((32, "_sion_parclose_mapped_generic", rank, " collect blocksize step %d of %d\n", blknum+1,sion_filedesc_sub->maxusedchunks));
1451 
1452  /* collect number of written chunks (blocks) for each file task */
1453  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank,"sion_filedesc_sub->nlocaltasksinfile=%d\n",sion_filedesc_sub->nlocaltasksinfile));
1454  for (ltask = 0; ltask < sion_filedesc_sub->nlocaltasksinfile; ltask++) {
1455  sion_tmpintfield_send[ltask]=sion_filedesc_sub->all_blocksizes[sion_filedesc_sub->nlocaltasksinfile * blknum + ltask];
1456  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank,"sort in blksize[%d][%d]=%d\n",blknum, ltask, sion_tmpintfield_send[ltask]));
1457  }
1458  sion_gendata->apidesc->gathervr_cb(sion_tmpintfield_send, sion_tmpintfield_data_recv, sion_gendata->comm_data_global, _SION_INT64, sion_count, sion_filedesc_sub->nlocaltasksinfile, root);
1459 
1460 
1461  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
1462 
1463  /* sort data */
1464  for(ltask=0;ltask<sion_filedesc_sub->ntotaltasksinfile;ltask++) {
1465  lrank=(int) sion_tmpintfield_lrank_recv[ltask];
1466  sion_tmpintfield_data[lrank]=sion_tmpintfield_data_recv[ltask];
1467  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank,"prepare blksize[%d][%d]=%ld\n",blknum,lrank,sion_tmpintfield_data[lrank]));
1468  }
1469 
1470  _sion_write_header_var_part_nextblocksizes_from_field(sion_filedesc_sub,sion_filedesc_sub->ntotaltasksinfile,sion_tmpintfield_data);
1471 
1472  }
1473 
1474  }
1475 
1476  if(sion_tmpintfield_lrank_recv) free(sion_tmpintfield_lrank_recv);
1477  if(sion_tmpintfield_data_recv) free(sion_tmpintfield_data_recv);
1478  if(sion_tmpintfield_data) free(sion_tmpintfield_data);
1479  if(sion_tmpintfield_send) free(sion_tmpintfield_send);
1480 
1481  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, " ending close for file %d: %s \n", filenr,sion_filedesc_sub->fname));
1482 
1483  } /* for */
1484 
1485  /* for mapping table */
1486  mappingroot=sion_filedesc_master->multifiles[0]->filemanagedbytask;
1487  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank,"mappingroot=%d rank=%d\n", mappingroot, rank ));
1488  if(rank==mappingroot) {
1489  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank,"allocate mapping of size %d\n",sion_filedesc_master->mapping_size));
1490  mapping = (sion_int32 *) malloc(sion_filedesc_master->mapping_size * 2 * sizeof(sion_int32));
1491  if (mapping == NULL) {
1492  free(sion_count);
1493  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_ABORT,"_sion_parclose_generic_mapped: Cannot allocate memory for mapping"));
1494  }
1495  }
1496 
1497  /* loop again over files to close and collect mapping data */
1498  for(filenr=0;filenr<sion_filedesc_master->nfiles;filenr++) {
1499  sion_filedesc_sub=sion_filedesc_master->multifiles[filenr];
1500 
1501  /* allocate send field */
1502  tmpsize=2 * sion_filedesc_sub->nlocaltasksinfile;
1503  sion_tmpint32field_send = (sion_int32 *) malloc(tmpsize * sizeof(sion_int32));
1504  if (sion_tmpint32field_send == NULL) {
1505  free(mapping);
1506  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_parclose_generic_mapped: cannot allocate temporary memory of size %lu (sion_tmpintfield_send), aborting ...\n",
1507  (unsigned long) tmpsize * sizeof(sion_int32)));
1508  }
1509 
1510  /* get number of tasks writing to this file */
1511  helpint32 = sion_filedesc_sub->nlocaltasksinfile;
1512  sion_gendata->apidesc->gatherr_cb(&helpint32, sion_count, sion_gendata->comm_data_global, _SION_INT32, 1, mappingroot);
1513 
1514  DPRINTFP((4, "_sion_parclose_mapped_generic", rank, " file %d: allocate send field for all local tasks of file (%d) --> %x\n", filenr, tmpsize, sion_tmpint32field_send));
1515 
1516  if(rank==mappingroot) {
1517  /* allocate receive fields */
1518  tmpsize=2 * sion_filedesc_sub->ntotaltasksinfile;
1519  sion_tmpint32field_data_recv = (sion_int32 *) malloc(tmpsize * sizeof(sion_int32));
1520  if (sion_tmpint32field_data_recv == NULL) {
1521  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_parclose_generic_mapped: cannot allocate temporary memory of size %lu (sion_tmpintfield_data_recv), aborting ...\n",
1522  (unsigned long) tmpsize * sizeof(sion_int32)));
1523  }
1524  } else sion_tmpint32field_data_recv=NULL;
1525 
1526  /* collect local mapping information */
1527  for (ltask = 0; ltask < sion_filedesc_sub->nlocaltasksinfile; ltask++) {
1528  sion_tmpint32field_send[ltask*2+0]= (int) sion_filedesc_sub->all_localranks[ltask];
1529  sion_tmpint32field_send[ltask*2+1]= (int) sion_filedesc_sub->all_globalranks[ltask];
1530  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank,"local lrank[%d]=%d grank[%d]=%d\n",ltask,(int) sion_filedesc_sub->all_localranks[ltask],ltask,(int) sion_filedesc_sub->all_globalranks[ltask]));
1531  }
1532 
1533  for(ltask=0;ltask<sion_filedesc_sub->nlocaltasksinfile*2;ltask++) {
1534  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank," sion_tmpint32field_send: %d -> %d \n",ltask, sion_tmpint32field_send[ltask]));
1535  }
1536 
1537  if (rank == mappingroot) for(task=0;task<ntasks;task++) sion_count[task]*=2;
1538  sion_gendata->apidesc->gathervr_cb(sion_tmpint32field_send, sion_tmpint32field_data_recv, sion_gendata->comm_data_global, _SION_INT32, sion_count, 2*sion_filedesc_sub->nlocaltasksinfile, mappingroot);
1539  if (rank == mappingroot) for(task=0;task<ntasks;task++) sion_count[task]/=2;
1540 
1541  if(rank==mappingroot) {
1542  /* sort data into mapping vector */
1543  for(ltask=0;ltask<sion_filedesc_sub->ntotaltasksinfile*2;ltask++) {
1544  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank," sion_tmpint32field_send: %d -> %d \n",ltask, sion_tmpint32field_data_recv[ltask]));
1545  }
1546  for(ltask=0;ltask<sion_filedesc_sub->ntotaltasksinfile;ltask++) {
1547  lrank=(int) sion_tmpint32field_data_recv[ltask*2+0];
1548  grank=(int) sion_tmpint32field_data_recv[ltask*2+1];
1549  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank,"store mapping[%d]=(%d,%d)\n",grank,filenr,lrank));
1550  mapping[grank*2+0]=filenr;
1551  mapping[grank*2+1]=lrank;
1552  }
1553  }
1554 
1555  if(sion_tmpint32field_data_recv) free(sion_tmpint32field_data_recv);
1556  if(sion_tmpint32field_send) free(sion_tmpint32field_send);
1557  }
1558 
1559  /* loop again over files to close and collect mapping data*/
1560  for(filenr=0;filenr<sion_filedesc_master->nfiles;filenr++) {
1561  sion_filedesc_sub=sion_filedesc_master->multifiles[filenr];
1562 
1563  if(sion_filedesc_sub->state == SION_FILESTATE_PAROPENMAPPEDMANAGED) {
1564 
1565  /* write mapping */
1566  if(filenr==0) {
1567  DPRINTFP((32,"_sion_parclose_mapped_generic:",rank,"mapping size is %d\n",sion_filedesc_master->mapping_size));
1568  _sion_write_header_var_part_mapping(sion_filedesc_sub, sion_filedesc_master->mapping_size, mapping);
1569  }
1570 
1571  DPRINTFP((32, "_sion_parclose_mapped_generic", rank, " parallel close (write mode, managed) call fclose on file %s (fileptr=%x)\n", sion_filedesc_sub->fname,sion_filedesc_sub->fileptr));
1572  _sion_file_close(sion_filedesc_sub->fileptr);
1573  sion_filedesc_sub->fileptr = NULL;
1574  }
1575 
1576  sion_filedesc_sub->state = SION_FILESTATE_CLOSE;
1577  if (sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_sub->keyvalptr = NULL;
1578 
1579  /* revert ntasks to local number so that free routines work correctly */
1580  sion_filedesc_sub->ntasks=sion_filedesc_sub->nlocaltasksinfile;
1581 
1582  _sion_free_filedesc(sion_filedesc_sub);
1583  }
1584  if (sion_gendata->apidesc->free_lcg_cb && sion_gendata->comm_data_local) {
1585  sion_gendata->apidesc->free_lcg_cb(sion_gendata->comm_data_local);
1586  }
1587  _SION_SAFE_FREE(mapping, NULL);
1588  _SION_SAFE_FREE(sion_count, NULL);
1589  _SION_SAFE_FREE(sion_filedesc_master->multifiles, NULL);
1590 
1591  if (sion_filedesc_master->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_master->keyvalptr = NULL;
1592  _sion_free_filedesc(sion_filedesc_master);
1593  sion_filedesc_master = NULL;
1594 
1595  } /* write */
1596 
1597  DPRINTFP((2, "_sion_parclose_mapped_generic", rank, "leave parallel close sid=%d\n", sid));
1598 
1599  return (rc);
1600 }
1601 
1602 /* END OF _sion_parclose_generic */
1603 
long _sion_file_get_opt_blksize(_sion_fileptr *sion_fileptr)
Get optional file system block size for a file.
Definition: sion_file.c:190
sion_int32 filemanagedbytask
sion_int64 _sion_file_get_position(_sion_fileptr *sion_fileptr)
Get new position in file.
Definition: sion_file.c:272
int _sion_buffer_flush(_sion_filedesc *sion_filedesc)
Flush buffer.
Definition: sion_buffer.c:164
int _sion_flush_block(_sion_filedesc *sion_filedesc)
Update the internal data structure.
sion_int64 _sion_file_set_position(_sion_fileptr *sion_fileptr, sion_int64 startpointer)
Set new position in file.
Definition: sion_file.c:239
_sion_filedesc * _sion_alloc_filedesc()
Allocates memory for internal sion structure.
int _sion_reassignvcd(int sid, void *data, int type)
Definition: sion_fd.c:61
_sion_fileptr * _sion_file_open(const char *fname, unsigned int flags, unsigned int addflags)
Create and open a new file for writing.
Definition: sion_file.c:41
Sion File Descriptor Structure.
Definition: sion_filedesc.h:77
sion_int64 * all_blockcount
#define SION_FILE_FLAG_WRITE
Definition: sion_file.h:23
int _sion_read_header_var_part(_sion_filedesc *sion_filedesc)
Read the second part of SION Meta Block 1.
int _sion_write_header_var_part_mapping(_sion_filedesc *sion_filedesc, sion_int32 mapping_size, sion_int32 *mapping)
Write mapping into the SION Meta Block 2.
int _sion_file_purge(_sion_fileptr *sion_fileptr)
Purge data to file.
Definition: sion_file.c:328
sion_int64 * all_currentpos
sion_int32 filesionpatchlevel
int _sion_paropen_mapped_generic(int sid, char *fname, sion_int64 file_mode_flags, char *prefix, int *numFiles, int *nlocaltasks, int **globalranks, sion_int64 **chunksizes, int **mapping_filenrs, int **mapping_lranks, sion_int32 *fsblksize, int rank, int ntasks, int flag, FILE **fileptr, _sion_generic_gendata *sion_gendata)
Generic parallel open of one direct access file. Mapping sion files to environment with less tasks.
sion_int64 * all_globalranks
int _sion_write_header(_sion_filedesc *sion_filedesc)
Write the SION Meta Block 1.
Definition: sion_metadata.c:36
int _sion_alloc_filedesc_arrays(_sion_filedesc *sion_filedesc)
Allocate memory for the internal sion arrays.
sion_int64 * all_currentblocknr
#define SION_FILE_FLAG_READ
Definition: sion_file.h:24
int _sion_realloc_filedesc_blocklist(_sion_filedesc *sion_filedesc, sion_int32 maxchunks)
Increase the memory used by the internal sion structure for the blocklist.
int _sion_vcdtype(int sid)
Definition: sion_fd.c:56
sion_int32 * all_coll_collector
sion_int32 fileptr_exported
sion_int32 _sion_get_endianness_with_flags(sion_int64 flags)
Return endianness including possible choice via flags.
#define SION_FILE_FLAG_POSIX
Definition: sion_file.h:21
#define SION_KEYVAL_NONE
Definition: sion_const.h:80
int sion_get_mapping(int sid, int *mapping_size, sion_int32 **mapping, int *numfiles)
Returns pointers to the internal field mapping.
Definition: sion_common.c:221
int _sion_write_header_var_part_nextblocksizes_from_field(_sion_filedesc *sion_filedesc, int field_size, sion_int64 *field)
Write the next set of blocksizes from Meta Block 2 Assuming that filepointer is at the correct positi...
#define MAXCHUNKS
Definition: sion_common.h:35
void * _sion_vcdtovcon(int sid)
Definition: sion_fd.c:51
#define SION_FILE_FLAG_ANSI
Definition: sion_file.h:19
#define SION_FILE_FLAG_CREATE
Definition: sion_file.h:22
#define SION_FILEMODE_READ
Definition: sion_filedesc.h:33
sion_int64 * all_startpointers
int _sion_read_header_var_part_blockcount_to_field(_sion_filedesc *sion_filedesc, int field_size, sion_int64 *field)
Read the block sizes from Meta Block 2.
int _sion_file_close(_sion_fileptr *sion_fileptr)
Close file and destroys fileptr structure.
Definition: sion_file.c:118
#define SION_FILESTATE_PAROPENMAPPEDMANAGED
Definition: sion_filedesc.h:29
int sion_get_sizeof(int sid, int *numbytes, int *numfds)
Function returns size of internal data structure for sid.
Definition: sion_common.c:1084
int _sion_write_header_var_info(_sion_filedesc *sion_filedesc)
Write the SION Meta Block 1.
int _sion_free_filedesc_coll_arrays(_sion_filedesc *sion_filedesc)
free memory for the internal sion arrays
int _sion_read_header_fix_part(_sion_filedesc *sion_filedesc)
Read part of the SION Meta Block 1.
sion_int64 * blocksizes
Definition: sion_filedesc.h:98
int _sion_write_header_var_part_blockcount_from_field(_sion_filedesc *sion_filedesc, int field_size, sion_int64 *field)
Write the block sizes from Meta Block 2.
#define SION_FILESTATE_PAROPENMAPPEDMASTER
Definition: sion_filedesc.h:28
sion_int32 currentblocknr
Definition: sion_filedesc.h:95
int _sion_alloc_filedesc_block_arrays(_sion_filedesc *sion_filedesc)
Allocate memory for the internal sion structure, fields for all chunksizes of all tasks.
int sion_get_keyval_mode(int sid)
Return selected mode for key value.
char * _sion_get_multi_filename(const char *fname, int filenumber)
generates the multi filename
#define SION_FILEDESCRIPTOR
Definition: sion_fd.h:17
sion_int64 * all_chunksizes
int _sion_print_filedesc(_sion_filedesc *sion_filedesc, int level, char *desc, int flag)
Print the initialized sion file description.
#define SION_FILESTATE_CLOSE
Definition: sion_filedesc.h:31
sion_int64 * all_localranks
int _sion_alloc_filedesc_coll_arrays(_sion_filedesc *sion_filedesc)
Allocate memory for the internal sion arrays.
int _sion_init_filedesc(_sion_filedesc *sion_filedesc)
Initialize the sion file description.
Definition: sion_filedesc.c:35
#define SION_FILEMODE_WRITE
Definition: sion_filedesc.h:34
int _sion_read_header_var_part_nextblocksizes_to_field(_sion_filedesc *sion_filedesc, int field_size, sion_int64 *field)
Read the next set of blocksizes from Meta Block 2 Assuming that filepointer is at the correct positio...
sion_int32 filesionversion
Sion Time Stamp Header.
sion_int32 * all_coll_collsize
sion_int64 start_of_varheader
sion_int64 * all_blocksizes
sion_int32 ntotaltasksinfile
sion_int32 nlocaltasksinfile
int _sion_file_flush(_sion_fileptr *sion_fileptr)
Flush data to file.
Definition: sion_file.c:304
_sion_filedesc ** multifiles
_sion_fileptr * fileptr
Definition: sion_filedesc.h:80
#define SION_FILESTATE_PAROPENMAPPED
Definition: sion_filedesc.h:30
int _sion_open_read(const char *fname, sion_int64 file_mode_flags, int read_all, int *ntasks, int *nfiles, sion_int64 **chunksizes, sion_int32 *fsblksize, int **globalranks, FILE **fileptr)
internal sion serial open function for reading on one or more files