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