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