SIONlib  1.6.2
Scalable I/O library for parallel access to task-local files
sion_internal_seek.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 
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <time.h>
19 
20 #include <sys/time.h>
21 
22 #include "sion.h"
23 #include "sion_debug.h"
24 #include "sion_internal.h"
25 #include "sion_metadata.h"
26 #include "sion_filedesc.h"
27 #include "sion_fd.h"
28 #include "sion_file.h"
29 #include "sion_printts.h"
30 #include "sion_buffer.h"
31 
32 #include "sion_internal_seek.h"
33 
34 
35 #define DFUNCTION "_sion_seek_on_all_ranks_read"
36 
50  int rank,
51  int blocknr,
52  sion_int64 posinblk ) {
53  int rc = SION_SUCCESS;
54  int blknum;
55 
56  DPRINTFP((2, DFUNCTION, -1, "enter seek r=%d b=%d p=%ld fn=%s\n",
57  rank,blocknr, (long) posinblk,sion_filedesc->fname));
58 
59  if ((sion_filedesc->all_blockcount == NULL)
60  || (sion_filedesc->all_blocksizes == NULL)) {
61  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
62  "sion_seek: internal error, data structure not initialized, aborting ...\n"));
63  }
64 
65  /* check if RANK changed */
66  if ( (rank != SION_CURRENT_RANK) && (rank != sion_filedesc->rank) ) {
67 
68  DPRINTFP((32, DFUNCTION, -1, "rank has changed %d -> %d\n", sion_filedesc->rank, rank));
69 
70  if ((rank<0) || (rank >= sion_filedesc->ntasks)) {
71  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: parameter rank %d (max. %d) out of range, aborting ...\n",
72  rank, sion_filedesc->ntasks));
73  }
74 
75  /* check and update current position */
76  _sion_update_fileposition(sion_filedesc);
77 
78  /* store current position in all_* vectors */
79  sion_filedesc->all_currentpos[sion_filedesc->rank] = sion_filedesc->currentpos;
80  sion_filedesc->all_currentblocknr[sion_filedesc->rank] = sion_filedesc->currentblocknr;
81 
82  /* pointer to keyval structure */
83  if(sion_filedesc->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc->all_keyvalptr[sion_filedesc->rank] = sion_filedesc->keyvalptr;
84 
85  /* switch to new rank and restore current position of this rank */
86  sion_filedesc->rank = rank;
87  sion_filedesc->currentblocknr = sion_filedesc->all_currentblocknr[sion_filedesc->rank];
88  sion_filedesc->currentpos = sion_filedesc->all_currentpos[sion_filedesc->rank];
89  sion_filedesc->lastchunknr = sion_filedesc->all_blockcount[sion_filedesc->rank]-1;
90  sion_filedesc->startpos = sion_filedesc->all_startpointers[sion_filedesc->rank];
91  sion_filedesc->chunksize = sion_filedesc->all_chunksizes[sion_filedesc->rank];
92 
93  /* pointer to keyval structure */
94  if(sion_filedesc->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc->keyvalptr=sion_filedesc->all_keyvalptr[sion_filedesc->rank];
95 
96  for (blknum = 0; blknum <= sion_filedesc->lastchunknr; blknum++) {
97  sion_filedesc->blocksizes[blknum] = sion_filedesc->all_blocksizes[sion_filedesc->ntasks * blknum + sion_filedesc->rank];
98  }
99 
100  /* /\* do nothing on empty blocks *\/ */
101  /* if (!sion_filedesc->lastchunknr) { */
102  /* rc = SION_NOT_SUCCESS; */
103  /* DPRINTFP((2, DFUNCTION, -1, "leave seek rc=%d\n",rc)); */
104  /* return rc; */
105  /* } */
106 
107  /* rank has changed, therefore this information could not get from fileposition */
108  if (blocknr == SION_CURRENT_BLK) {
109  blocknr=sion_filedesc->currentblocknr;
110  }
111  if (posinblk == SION_CURRENT_POS) {
112  posinblk=sion_filedesc->currentpos - (sion_filedesc->startpos + sion_filedesc->currentblocknr * sion_filedesc->globalskip);
113 
114  if(sion_filedesc->keyvalmode==SION_KEYVAL_NONE) {
115 
116  /* check if just behind current block, go to next block */
117  if(posinblk>=sion_filedesc->blocksizes[blocknr]) {
118  posinblk=0;
119  /* do no more checks on empty blocks */
120  if (sion_filedesc->lastchunknr) {
121  blocknr++;
122  if(blocknr > sion_filedesc->lastchunknr) {
123  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: seek after end of file, returning ...\n"));
124  }
125  }
126  }
127 
128  }
129  }
130 
131  }
132 
133  /* seek new position in current rank, sets in all cases the filepointer */
134  rc=_sion_seek_on_current_rank_read(sion_filedesc,SION_CURRENT_RANK,blocknr,posinblk);
135 
136  DPRINTFP((2, DFUNCTION, -1, "leave seek rc=%d\n",rc));
137 
138  return(rc);
139 
140 }
141 #undef DFUNCTION
142 
143 #define DFUNCTION "_sion_seek_on_all_ranks_read_master"
144 
158  int rank,
159  int blocknr,
160  sion_int64 posinblk ) {
161  int rc = SION_SUCCESS;
162  int blknum, lfile, lrank;
163 
164  DPRINTFP((2, DFUNCTION, -1, "enter seek r=%d b=%d p=%ld fn=%s\n",
165  rank,blocknr, (long) posinblk,sion_filedesc->fname));
166 
167 #if 0
168  if ((sion_filedesc->all_blockcount == NULL)
169  || (sion_filedesc->all_blocksizes == NULL)) {
170  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
171  "sion_seek: internal error, data structure not initialized, aborting ...\n"));
172  }
173 #endif
174 
175  /* lookup file which contains current rank */
176  lfile=sion_filedesc->mapping[sion_filedesc->rank*2+0];
177  lrank=sion_filedesc->mapping[sion_filedesc->rank*2+1];
178 
179  /* check if RANK changed */
180  if ( (rank != SION_CURRENT_RANK) && (rank != sion_filedesc->rank) ) {
181 
182  if ((rank<0) || (rank >= sion_filedesc->ntasks)) {
183  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: parameter rank %d (max. %d) out of range, aborting ...\n",
184  rank, sion_filedesc->ntasks));
185  }
186 
187  /* check and update current position */
188  _sion_update_fileposition(sion_filedesc);
189 
190  /* store current position in all_* vectors */
191  sion_filedesc->multifiles[lfile]->all_currentpos[lrank] = sion_filedesc->currentpos;
192  sion_filedesc->multifiles[lfile]->all_currentblocknr[lrank] = sion_filedesc->currentblocknr;
193 
194  /* pointer to keyval structure */
195  if(sion_filedesc->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc->multifiles[lfile]->all_keyvalptr[lrank] = sion_filedesc->keyvalptr;
196 
197 
198  /* switch to new rank and restore current position of this rank */
199  sion_filedesc->rank = rank;
200 
201  /* lookup again file which contains current rank */
202  lfile=sion_filedesc->mapping[sion_filedesc->rank*2+0];
203  lrank=sion_filedesc->mapping[sion_filedesc->rank*2+1];
204 
205 
206  sion_filedesc->currentblocknr = sion_filedesc->multifiles[lfile]->all_currentblocknr[lrank];
207  sion_filedesc->currentpos = sion_filedesc->multifiles[lfile]->all_currentpos[lrank];
208  sion_filedesc->lastchunknr = sion_filedesc->multifiles[lfile]->all_blockcount[lrank]-1;
209  sion_filedesc->startpos = sion_filedesc->multifiles[lfile]->all_startpointers[lrank];
210  sion_filedesc->chunksize = sion_filedesc->multifiles[lfile]->all_chunksizes[lrank];
211  sion_filedesc->globalskip = sion_filedesc->multifiles[lfile]->globalskip;
212  for (blknum = 0; blknum < sion_filedesc->multifiles[lfile]->all_blockcount[lrank]; blknum++) {
213  sion_filedesc->blocksizes[blknum] = sion_filedesc->multifiles[lfile]->all_blocksizes[sion_filedesc->multifiles[lfile]->ntasks * blknum + lrank];
214  }
215  sion_filedesc->fileptr = sion_filedesc->multifiles[lfile]->fileptr;
216 
217  DPRINTFP((32, DFUNCTION, -1, "switch to file %d and lrank %d currentpos=%ld, currentblocknr=%d\n",lfile, lrank,(long) sion_filedesc->currentpos, sion_filedesc->currentblocknr ));
218 
219  /* pointer to keyval structure */
220  if(sion_filedesc->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc->keyvalptr=sion_filedesc->multifiles[lfile]->all_keyvalptr[lrank];
221 
222  /* set rank info in sub-file */
223  sion_filedesc->multifiles[lfile]->rank=lrank;
224 
225  /* rank has changed, therefore this information could not get from fileposition */
226  if (blocknr == SION_CURRENT_BLK) {
227  blocknr=sion_filedesc->currentblocknr;
228  }
229  if (posinblk == SION_CURRENT_POS) {
230  posinblk=sion_filedesc->currentpos - (sion_filedesc->startpos + sion_filedesc->currentblocknr * sion_filedesc->globalskip);
231 
232  if(sion_filedesc->keyvalmode==SION_KEYVAL_NONE) {
233 
234  /* check if just behind current block, go to next block */
235  if(posinblk>=sion_filedesc->blocksizes[blocknr]) {
236  posinblk=0;blocknr++;
237  if(blocknr > sion_filedesc->lastchunknr) {
238  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: seek after end of file, returning ...\n"));
239  }
240  }
241 
242  }
243  }
244 
245  }
246 
247  /* seek new position in current rank in sub-file, sets in all cases the filepointer */
248  rc=_sion_seek_on_current_rank_read(sion_filedesc,SION_CURRENT_RANK,blocknr,posinblk);
249 
250  DPRINTFP((2, DFUNCTION, -1, "leave seek rc=%d\n",rc));
251 
252  return(rc);
253 
254 }
255 #undef DFUNCTION
256 
257 
258 #define DFUNCTION "_sion_seek_on_current_rank_read"
259 
271  int rank,
272  int blocknr,
273  sion_int64 posinblk ) {
274  int rc = SION_SUCCESS;
275  int newblocknr;
276  sion_int64 newposinblk=-1;
277 
278  DPRINTFP((2, DFUNCTION, -1, "enter seek r=%d b=%d p=%ld fn=%s\n",rank,blocknr, (long) posinblk,sion_filedesc->fname));
279 
280 
281  /* check RANK */
282  if ( (rank != SION_CURRENT_RANK) && (rank != sion_filedesc->rank) ) {
283  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
284  "sion_seek: parameter rank is different from current rank in parallel openened file, returning ...\n"));
285  }
286 
287  /* check requested BLOCK NUMBER */
288  if (blocknr == SION_ABSOLUTE_POS) {
289  /* search absolute position */
290  if(!_sion_seek_search_abs_pos(sion_filedesc,posinblk,&newblocknr,&newposinblk)) {
291  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN, "sion_seek: error in searching abs pos, returning ...\n"));
292  } else posinblk=newposinblk; /* for checking posinblk later */
293  DPRINTFP((32, DFUNCTION, -1, "sion_absolute_pos newblocknr=%d newpos=%ld fn=%s\n",newblocknr, (long) newposinblk, sion_filedesc->fname));
294  }
295  else if (blocknr == SION_END_POS) {
296  /* search position relative to end */
297  if (!_sion_seek_search_end_pos(sion_filedesc, posinblk, &newblocknr, &newposinblk)) {
298  return _sion_errorprint(SION_NOT_SUCCESS, _SION_ERROR_RETURN, "sion_seek: error in searching end pos, returning ...\n");
299  }
300  else { posinblk = newposinblk; /* for checking posinblk later */
301  }
302  DPRINTFP((32, DFUNCTION, -1, "sion_end_pos newblocknr=%d newpos=%ld fn=%s\n", newblocknr, (long)newposinblk, sion_filedesc->fname));
303  }
304  else {
305  if (blocknr == SION_CURRENT_BLK) {
306  newblocknr=sion_filedesc->currentblocknr;
307  DPRINTFP((32, DFUNCTION, -1, "sion_current_blk newblocknr=%d newpos=%ld fn=%s\n",newblocknr, (long) newposinblk, sion_filedesc->fname));
308  } else {
309  /* a blocknr is specified */
310  if ( (blocknr >= 0) && (blocknr <= sion_filedesc->lastchunknr) ) {
311  newblocknr=blocknr;
312  } else {
313  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
314  "sion_seek: parameter chunk number (%d) is out of range (0 .. %d), returning ...\n",
315  blocknr,sion_filedesc->lastchunknr));
316  }
317  DPRINTFP((32, DFUNCTION, -1, "new blocknr newblocknr=%d newpos=%ld fn=%s\n",newblocknr, (long) newposinblk, sion_filedesc->fname));
318  }
319  }
320 
321 
322  /* check requested POSITION IN BLOCK */
323  if (posinblk == SION_CURRENT_POS) {
324  _sion_update_fileposition(sion_filedesc);
325  newposinblk=sion_filedesc->currentpos - (sion_filedesc->startpos + sion_filedesc->currentblocknr * sion_filedesc->globalskip);
326  DPRINTFP((32, DFUNCTION, -1, "sion_current_pos newblocknr=%d newpos=%ld fn=%s %ld %ld %ld %ld\n",newblocknr, (long) newposinblk, sion_filedesc->fname,
327  (long) sion_filedesc->currentpos, (long) sion_filedesc->startpos,(long) sion_filedesc->currentblocknr,(long) sion_filedesc->globalskip));
328  } else {
329  /* a posinblk is specified */
330  if ( (sion_filedesc->keyvalmode!=SION_KEYVAL_NONE) || /* allow position outside block for keyval, file could already be scanned completly */
331  ( (posinblk >= 0) && (posinblk <= sion_filedesc->blocksizes[newblocknr]) )
332  )
333  {
334  newposinblk=posinblk;
335  } else {
336  DPRINTFP((2, DFUNCTION, -1, "sion_seek: parameter posinblk (%lld) is out of range (0 .. %lld), aborting ...\n",
337  posinblk, sion_filedesc->blocksizes[newblocknr]));
338  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
339  "sion_seek: parameter posinblk (%lld) is out of range (0 .. %lld), aborting ...\n",
340  posinblk, sion_filedesc->blocksizes[newblocknr]));
341  }
342  DPRINTFP((2, DFUNCTION, -1, "new posinblk newblocknr=%d newpos=%ld fn=%s\n",newblocknr, (long) newposinblk, sion_filedesc->fname));
343  }
344 
345  DPRINTFP((32, DFUNCTION, -1,
346  " before set pos: rank=%4d startpos=%ld currentblocknr=%d globalskip=%ld newposinblk=%ld\n", sion_filedesc->rank
347  ,(long) sion_filedesc->startpos, sion_filedesc->currentblocknr, (long) sion_filedesc->globalskip, (long) newposinblk));
348 
349  /* SET NEW POSITION */
350  DPRINTFP((32, DFUNCTION, 0, "file pos=%ld (%ld)\n", (long) _sion_file_get_position(sion_filedesc->fileptr), (long) sion_filedesc->currentpos));
351  sion_filedesc->currentblocknr = newblocknr;
352  sion_filedesc->currentpos = sion_filedesc->startpos
353  + sion_filedesc->currentblocknr * sion_filedesc->globalskip
354  + newposinblk;
355  _sion_file_purge(sion_filedesc->fileptr);
356  _sion_file_set_position(sion_filedesc->fileptr, sion_filedesc->currentpos);
357 
358  DPRINTFP((32, DFUNCTION, -1,
359  " set pos: rank=%4d newblocknr=%4d newposinblk=%4ld, set fileptr to position %14ld (%14ld) file=%s\n",
360  sion_filedesc->rank, sion_filedesc->currentblocknr, (long) newposinblk,
361  (long) _sion_file_get_position(sion_filedesc->fileptr),(long) sion_filedesc->currentpos,
362  sion_filedesc->fname));
363 
364 
365  DPRINTFP((2, DFUNCTION, -1, "leave seek rc=%d\n",rc));
366 
367  return(rc);
368 
369 }
370 #undef DFUNCTION
371 
372 #define DFUNCTION "_sion_seek_on_all_ranks_read_mapped"
373 
387  int rank,
388  int blocknr,
389  sion_int64 posinblk ) {
390  int rc = SION_SUCCESS;
391  int lfile, lrank, blknum, filenr, t;
392  _sion_filedesc *sion_filedesc_sub;
393 
394  DPRINTFP((2, DFUNCTION, -1, "enter seek r=%d b=%d p=%ld fn=%s\n",
395  rank,blocknr, (long) posinblk,sion_filedesc_master->fname));
396 
397  /* check if RANK changed */
398  if ( (rank != SION_CURRENT_RANK) && (rank != sion_filedesc_master->globalrank) ) {
399 
400  DPRINTFP((32, DFUNCTION, -1, "rank has changed %d -> %d\n", sion_filedesc_master->globalrank, rank));
401 
402  if ((rank<0) || (rank >= sion_filedesc_master->ntotaltasksinfile)) {
403  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: parameter rank %d (max. %d) out of range, aborting ...\n",
404  rank, sion_filedesc_master->ntotaltasksinfile));
405  }
406 
407  /* check and update current position */
408  _sion_update_fileposition(sion_filedesc_master);
409 
410  /* transfer meta data to corresponding sub datastructure */
411  lfile=sion_filedesc_master->filenumber;
412  lrank=sion_filedesc_master->rank; /* index to local list */
413  sion_filedesc_sub=sion_filedesc_master->multifiles[lfile];
414 
415  sion_filedesc_sub->currentpos = sion_filedesc_master->currentpos;
416  sion_filedesc_sub->currentblocknr = sion_filedesc_master->currentblocknr;
417 
418  /* pointer to keyval structure */
419  DPRINTFP((32, DFUNCTION, -1, "set keyvalptr on sub to master lfile=%d,lrank=%d to %x\n",lfile,lrank,sion_filedesc_master->keyvalptr));
420  if(sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_sub->keyvalptr = sion_filedesc_master->keyvalptr;
421 
422  DPRINTFP((32, DFUNCTION, -1, "on file %d: sub,maxchunk=%d master,maxchunk=%d \n", lfile,sion_filedesc_sub->maxchunks, sion_filedesc_master->maxchunks));
423 
424  /* store data of current rank on sub datastructure */
425  DPRINTFP((32, DFUNCTION, -1, "store current information lrank=%d lastchunknr=%d\n", lrank,sion_filedesc_sub->lastchunknr));
426  sion_filedesc_sub->all_currentpos[lrank] = sion_filedesc_sub->currentpos;
427  sion_filedesc_sub->all_currentblocknr[lrank] = sion_filedesc_sub->currentblocknr;
428 
429  /* pointer to keyval structure -> all_keyvalptr */
430  DPRINTFP((32, DFUNCTION, -1, "keyvalptr sub file=%d all_keyvalptr[%d] = %x\n",lfile,lrank,sion_filedesc_sub->keyvalptr));
431  if(sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_sub->all_keyvalptr[lrank] = sion_filedesc_sub->keyvalptr;
432 
433  /* lookup file which contains new global rank and set master */
434  sion_filedesc_sub=NULL;
435  lfile=lrank=-1;
436  DPRINTFP((32, DFUNCTION, -1, " nfiles=%d\n", sion_filedesc_master->nfiles));
437 
438  for(filenr=0;( (filenr<sion_filedesc_master->nfiles) && (lrank==-1) );filenr++) {
439  DPRINTFP((4, DFUNCTION, -1, " filenr=%d nlocaltasksinfile\n",
440  filenr,sion_filedesc_master->multifiles[filenr]->nlocaltasksinfile));
441 
442  for(t=0;( (t<sion_filedesc_master->multifiles[filenr]->nlocaltasksinfile) && (lrank==-1) );t++) {
443  DPRINTFP((4, DFUNCTION, -1, " check filenr=%d t=%d grank=%d with rank=%d\n",
444  filenr,t,sion_filedesc_master->multifiles[filenr]->all_globalranks[t],rank));
445  if(sion_filedesc_master->multifiles[filenr]->all_globalranks[t]==rank) {
446  sion_filedesc_sub=sion_filedesc_master->multifiles[filenr];
447  lfile=filenr;
448  lrank=t;
449  break;
450  }
451  }
452  }
453  DPRINTFP((32, DFUNCTION, -1, "grank %d is found in file %d with lrank %d\n", rank,lfile,lrank));
454  if(lrank==-1) {
455  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: parameter rank %d is not opened on this tasks (mapped mode), aborting ...\n",
456  rank));
457  }
458 
459  /* switch to new rank and restore current position of this rank */
460  sion_filedesc_master->globalrank = sion_filedesc_sub->all_globalranks[lrank];
461  sion_filedesc_master->rank = lrank;
462  sion_filedesc_master->filenumber = lfile;
463  sion_filedesc_master->currentblocknr = sion_filedesc_sub->all_currentblocknr[lrank];
464  sion_filedesc_master->currentpos = sion_filedesc_sub->all_currentpos[lrank];
465  sion_filedesc_master->lastchunknr = sion_filedesc_sub->all_blockcount[lrank]-1;
466  sion_filedesc_master->startpos = sion_filedesc_sub->all_startpointers[lrank];
467  sion_filedesc_master->chunksize = sion_filedesc_sub->all_chunksizes[lrank];
468 
469  /* pointer to keyval structure */
470  if(sion_filedesc_master->keyvalmode!=SION_KEYVAL_NONE) {
471  DPRINTFP((32, DFUNCTION, -1, "set keyvalptr on lfile=%d,lrank=%d to %x\n",lfile,lrank,sion_filedesc_sub->all_keyvalptr[lrank]));
472  sion_filedesc_master->keyvalptr = sion_filedesc_sub->all_keyvalptr[lrank];
473  }
474 
475  if(sion_filedesc_sub->maxchunks > sion_filedesc_master->maxchunks) {
476  _sion_realloc_filedesc_blocklist(sion_filedesc_master, sion_filedesc_sub->maxchunks);
477  }
478  for (blknum = 0; blknum <= sion_filedesc_master->lastchunknr; blknum++) {
479  sion_filedesc_master->blocksizes[blknum] = sion_filedesc_sub->all_blocksizes[sion_filedesc_sub->ntasks * blknum + lrank];
480  }
481  sion_filedesc_master->globalskip = sion_filedesc_sub->globalskip;
482  sion_filedesc_master->fileptr = sion_filedesc_sub->fileptr;
483 
484  /* rank has changed, therefore this information could not get from fileposition */
485  if (blocknr == SION_CURRENT_BLK) {
486  blocknr=sion_filedesc_master->currentblocknr;
487  }
488  if (posinblk == SION_CURRENT_POS) {
489  posinblk=sion_filedesc_master->currentpos - (sion_filedesc_master->startpos + sion_filedesc_master->currentblocknr * sion_filedesc_master->globalskip);
490 
491  if(sion_filedesc_master->keyvalmode==SION_KEYVAL_NONE) {
492 
493  /* check if just behind current block, go to next block */
494  if(posinblk>=sion_filedesc_master->blocksizes[blocknr]) {
495  posinblk=0;blocknr++;
496  if(blocknr > sion_filedesc_master->lastchunknr) {
497  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: seek after end of file, returning ...\n"));
498 
499  }
500  }
501 
502  }
503  }
504 
505  }
506 
507  _sion_print_filedesc(sion_filedesc_master, 512, "_sion_seek_on_all_ranks_read_mapped", 1);
508 
509  /* seek new position in current rank, set in all cases the filepointer */
510  _sion_seek_on_current_rank_read(sion_filedesc_master,SION_CURRENT_RANK,blocknr,posinblk);
511 
512  DPRINTFP((2, DFUNCTION, -1, "leave seek\n"));
513 
514  return(rc);
515 
516 }
517 #undef DFUNCTION
518 
519 
520 #define DFUNCTION "_sion_seek_on_all_ranks_write"
521 
535  int rank,
536  int blocknr,
537  sion_int64 posinblk ) {
538  int rc = SION_SUCCESS;
539  int blknum;
540 
541  DPRINTFP((2, DFUNCTION, -1, "enter seek r=%d b=%d p=%ld fn=%s\n",
542  rank,blocknr, (long) posinblk,sion_filedesc->fname));
543 
544  if ((sion_filedesc->all_blockcount == NULL)
545  || (sion_filedesc->all_blocksizes == NULL)) {
546  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
547  "sion_seek: internal error, data structure not initialized, aborting ...\n"));
548  }
549 
550  /* check if RANK changed */
551  if ( (rank != SION_CURRENT_RANK) && (rank != sion_filedesc->rank) ) {
552 
553  DPRINTFP((32, DFUNCTION, -1, "rank has changed %d -> %d\n", sion_filedesc->rank, rank));
554 
555  if ((rank<0) || (rank >= sion_filedesc->ntasks)) {
556  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: parameter rank %d (max. %d) out of range, aborting ...\n",
557  rank, sion_filedesc->ntasks));
558  }
559 
560  /* flush write buffer */
561  if (sion_filedesc->usebuffer) {
562  _sion_buffer_flush(sion_filedesc);
563  }
564 
565  /* update meta data of current rank */
566  _sion_flush_block(sion_filedesc);
567 
568  /* store current position in all_* vectors */
569  DPRINTFP((32, DFUNCTION, -1, "store data for rank %d currentpos=%d currentblocknr=%d lastchunknr=%d\n", sion_filedesc->rank,
570  (int) sion_filedesc->currentpos,
571  (int) sion_filedesc->currentblocknr,
572  (int) sion_filedesc->lastchunknr ));
573  sion_filedesc->all_currentpos[sion_filedesc->rank] = sion_filedesc->currentpos;
574  sion_filedesc->all_currentblocknr[sion_filedesc->rank] = sion_filedesc->currentblocknr;
575 
576  /* pointer to keyval structure */
577  if(sion_filedesc->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc->all_keyvalptr[sion_filedesc->rank] = sion_filedesc->keyvalptr;
578 
579  sion_filedesc->all_blockcount[sion_filedesc->rank] = sion_filedesc->lastchunknr + 1;
580  for (blknum = 0; blknum <= sion_filedesc->lastchunknr; blknum++) {
581  sion_filedesc->all_blocksizes[sion_filedesc->ntasks * blknum + sion_filedesc->rank] = sion_filedesc->blocksizes[blknum];
582  sion_filedesc->blocksizes[blknum]=0; /* reset entry */
583  }
584 
585  /* switch to new rank and restore current position of this rank */
586  sion_filedesc->rank = rank;
587  sion_filedesc->currentblocknr = sion_filedesc->all_currentblocknr[sion_filedesc->rank];
588  sion_filedesc->currentpos = sion_filedesc->all_currentpos[sion_filedesc->rank];
589  sion_filedesc->lastchunknr = sion_filedesc->all_blockcount[sion_filedesc->rank]-1;
590  sion_filedesc->startpos = sion_filedesc->all_startpointers[sion_filedesc->rank];
591  sion_filedesc->chunksize = sion_filedesc->all_chunksizes[sion_filedesc->rank];
592 
593  /* pointer to keyval structure */
594  if(sion_filedesc->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc->keyvalptr = sion_filedesc->all_keyvalptr[sion_filedesc->rank];
595 
596  for (blknum = 0; blknum <= sion_filedesc->lastchunknr; blknum++) {
597  sion_filedesc->blocksizes[blknum] = sion_filedesc->all_blocksizes[sion_filedesc->ntasks * blknum + sion_filedesc->rank];
598  }
599 
600  }
601 
602  /* seek new position in current rank, set in all cases the filepointer */
603  _sion_seek_on_current_rank_write(sion_filedesc,SION_CURRENT_RANK,blocknr,posinblk);
604 
605  DPRINTFP((2, DFUNCTION, -1, "leave seek\n"));
606 
607  return(rc);
608 
609 }
610 #undef DFUNCTION
611 
612 #define DFUNCTION "_sion_seek_on_all_ranks_write_mapped"
613 
627  int rank,
628  int blocknr,
629  sion_int64 posinblk ) {
630  int rc = SION_SUCCESS;
631  int lfile, lrank, blknum, filenr, t;
632  _sion_filedesc *sion_filedesc_sub;
633 
634  DPRINTFP((2, DFUNCTION, -1, "enter seek r=%d b=%d p=%ld fn=%s\n",
635  rank,blocknr, (long) posinblk,sion_filedesc_master->fname));
636 
637  /* check if RANK changed */
638  if ( (rank != SION_CURRENT_RANK) && (rank != sion_filedesc_master->globalrank) ) {
639 
640  DPRINTFP((32, DFUNCTION, -1, "rank has changed %d -> %d\n", sion_filedesc_master->globalrank, rank));
641 
642  if ((rank<0) || (rank >= sion_filedesc_master->ntotaltasksinfile)) {
643  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: parameter rank %d (max. %d) out of range, aborting ...\n",
644  rank, sion_filedesc_master->ntotaltasksinfile));
645  }
646 
647  /* flush write buffer */
648  if (sion_filedesc_master->usebuffer) {
649  _sion_buffer_flush(sion_filedesc_master);
650  }
651 
652  /* update meta data of current rank */
653  _sion_flush_block(sion_filedesc_master);
654 
655  /* transfer meta data to corresponding sub datastructure */
656  lfile=sion_filedesc_master->filenumber;
657  lrank=sion_filedesc_master->rank; /* index to local list */
658  sion_filedesc_sub=sion_filedesc_master->multifiles[lfile];
659 
660  sion_filedesc_sub->currentpos = sion_filedesc_master->currentpos;
661  sion_filedesc_sub->currentblocknr = sion_filedesc_master->currentblocknr;
662  sion_filedesc_sub->lastchunknr = sion_filedesc_master->lastchunknr;
663 
664  /* pointer to keyval structure */
665  if(sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_sub->keyvalptr = sion_filedesc_master->keyvalptr;
666 
667  DPRINTFP((32, DFUNCTION, -1, "on file %d: sub,maxchunk=%d master,maxchunk=%d \n", lfile,sion_filedesc_sub->maxchunks, sion_filedesc_master->maxchunks));
668  if(sion_filedesc_sub->maxchunks < sion_filedesc_master->maxchunks) {
669  _sion_realloc_filedesc_blocklist(sion_filedesc_sub, sion_filedesc_master->maxchunks);
670  }
671 
672  /* store data of current rank on sub datastructure */
673  DPRINTFP((32, DFUNCTION, -1, "store current information lrank=%d lastchunknr=%d\n", lrank,sion_filedesc_sub->lastchunknr));
674  sion_filedesc_sub->all_currentpos[lrank] = sion_filedesc_sub->currentpos;
675  sion_filedesc_sub->all_currentblocknr[lrank] = sion_filedesc_sub->lastchunknr;
676  sion_filedesc_sub->all_blockcount[lrank] = sion_filedesc_sub->lastchunknr + 1;
677 
678  /* pointer to keyval structure */
679  if(sion_filedesc_sub->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_sub->all_keyvalptr[lrank] = sion_filedesc_sub->keyvalptr;
680 
681  for (blknum = 0; blknum <= sion_filedesc_sub->lastchunknr; blknum++) {
682  sion_filedesc_sub->blocksizes[blknum] = sion_filedesc_master->blocksizes[blknum];
683  sion_filedesc_sub->all_blocksizes[sion_filedesc_sub->ntasks * blknum + lrank] = sion_filedesc_master->blocksizes[blknum];
684  }
685 
686  /* lookup file which contains new global rank and set master */
687  sion_filedesc_sub=NULL;
688  lfile=lrank=-1;
689  DPRINTFP((32, DFUNCTION, -1, " nfiles=%d\n", sion_filedesc_master->nfiles));
690 
691  for(filenr=0;( (filenr<sion_filedesc_master->nfiles) && (lrank==-1) );filenr++) {
692  DPRINTFP((32, DFUNCTION, -1, " filenr=%d nlocaltasksinfile\n",
693  filenr,sion_filedesc_master->multifiles[filenr]->nlocaltasksinfile));
694 
695  for(t=0;( (t<sion_filedesc_master->multifiles[filenr]->nlocaltasksinfile) && (lrank==-1) );t++) {
696  DPRINTFP((32, DFUNCTION, -1, " check filenr=%d t=%d grank=%d with rank=%d\n",
697  filenr,t,sion_filedesc_master->multifiles[filenr]->all_globalranks[t],rank));
698  if(sion_filedesc_master->multifiles[filenr]->all_globalranks[t]==rank) {
699  sion_filedesc_sub=sion_filedesc_master->multifiles[filenr];
700  lfile=filenr;
701  lrank=t;
702  break;
703  }
704  }
705  }
706  DPRINTFP((32, DFUNCTION, -1, "grank %d is found in file %d with lrank %d\n", rank,lfile,lrank));
707  if(lrank==-1) {
708  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"sion_seek: parameter rank %d is not opened on this tasks (mapped mode), aborting ...\n",
709  rank));
710  }
711 
712  /* switch to new rank and restore current position of this rank */
713  sion_filedesc_master->globalrank = sion_filedesc_sub->all_globalranks[lrank];
714  sion_filedesc_master->rank = lrank;
715  sion_filedesc_master->filenumber = lfile;
716  sion_filedesc_master->currentblocknr = sion_filedesc_sub->all_currentblocknr[lrank];
717  sion_filedesc_master->currentpos = sion_filedesc_sub->all_currentpos[lrank];
718  sion_filedesc_master->lastchunknr = sion_filedesc_sub->all_blockcount[lrank]-1;
719  sion_filedesc_master->startpos = sion_filedesc_sub->all_startpointers[lrank];
720  sion_filedesc_master->chunksize = sion_filedesc_sub->all_chunksizes[lrank];
721 
722  /* pointer to keyval structure */
723  if(sion_filedesc_master->keyvalmode!=SION_KEYVAL_NONE) sion_filedesc_master->keyvalptr = sion_filedesc_sub->all_keyvalptr[lrank];
724 
725  if(sion_filedesc_sub->maxchunks > sion_filedesc_master->maxchunks) {
726  _sion_realloc_filedesc_blocklist(sion_filedesc_master, sion_filedesc_sub->maxchunks);
727  }
728  for (blknum = 0; blknum <= sion_filedesc_sub->lastchunknr; blknum++) {
729  sion_filedesc_master->blocksizes[blknum] = sion_filedesc_sub->all_blocksizes[sion_filedesc_sub->ntasks * blknum + lrank];
730  }
731 
732  sion_filedesc_master->globalskip = sion_filedesc_sub->globalskip;
733  sion_filedesc_master->fileptr = sion_filedesc_sub->fileptr;
734 
735  }
736 
737 
738  _sion_print_filedesc(sion_filedesc_master, 512, "_sion_seek_on_all_ranks_write_mapped", 1);
739 
740  /* seek new position in current rank, set in all cases the filepointer */
741  _sion_seek_on_current_rank_write(sion_filedesc_master,SION_CURRENT_RANK,blocknr,posinblk);
742 
743  DPRINTFP((2, DFUNCTION, -1, "leave seek\n"));
744 
745  return(rc);
746 
747 }
748 #undef DFUNCTION
749 
750 
751 #define DFUNCTION "_sion_seek_on_current_rank_write"
752 
764  int rank,
765  int blocknr,
766  sion_int64 posinblk ) {
767  int rc = SION_SUCCESS;
768 
769  DPRINTFP((2, DFUNCTION, -1, "enter seek r=%d b=%d p=%ld fn=%s\n",rank,blocknr, (long) posinblk,sion_filedesc->fname));
770 
771  /* check RANK */
772  if ( (rank != SION_CURRENT_RANK) && (rank != sion_filedesc->rank) ) {
773  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
774  "sion_seek: parameter rank is different from current rank in parallel openened file, returning ...\n"));
775  }
776 
777  /* check requested BLOCK NUMBER */
778  if (blocknr != SION_CURRENT_BLK) {
779  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
780  "sion_seek: serial write to file currently only for SION_CURRENT_BLK implemented, aborting ...\n"));
781  }
782 
783  /* check requested POSITION IN BLOCK */
784  if (posinblk != SION_CURRENT_POS) {
785  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
786  "sion_seek: serial write to file currently only for SION_CURRENT_POS implemented, aborting ...\n"));
787  }
788 
789  /* flush write buffer */
790  if (sion_filedesc->usebuffer) {
791  _sion_buffer_flush(sion_filedesc);
792  }
793 
794  /* SET NEW POSITION */
795  /* currently not necessary, due to that movements
796  inside rank currently not allowed for write-mode*/
797 
798  /* SET NEW POSITION */
799  _sion_file_purge(sion_filedesc->fileptr);
800  _sion_file_set_position(sion_filedesc->fileptr, sion_filedesc->currentpos);
801 
802  DPRINTFP((2, DFUNCTION, -1, "leave seek\n"));
803 
804  return(rc);
805 
806 }
807 #undef DFUNCTION
808 
809 #define DFUNCTION "_sion_seek_search_abs_pos"
810 
822  sion_int64 abspos,
823  int *newblocknr,
824  sion_int64 *newposinblk ) {
825  int rc = SION_SUCCESS;
826  sion_int64 bytes=-1;
827  sion_int64 bytesinblock = 0;
828 
829  if (abspos>=0) {
830  *newposinblk=abspos;
831  bytes=0;*newblocknr=0;
832  bytesinblock = sion_filedesc->blocksizes[*newblocknr];
833  while ( (*newblocknr<sion_filedesc->lastchunknr) && (bytes + bytesinblock < *newposinblk) ) {
834  bytes += bytesinblock;
835  (*newblocknr)++;
836  bytesinblock = sion_filedesc->blocksizes[*newblocknr];
837  }
838  *newposinblk-=bytes;
839  } else {
840  return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
841  "sion_seek: parameter pos in chunk (%d) is negative, aborting ...\n", (int) abspos));
842  }
843 
844 
845  return(rc);
846 }
847 #undef DFUNCTION
848 
849 #define DFUNCTION "_sion_seek_search_end_pos"
850 
862  sion_int64 posend,
863  int* newblocknr,
864  sion_int64* newposinblk)
865 {
866  int rc = SION_SUCCESS;
867 
868  if (posend <= 0) {
869  *newposinblk = posend;
870  *newblocknr = sion_filedesc->lastchunknr;
871  do {
872  *newposinblk += sion_filedesc->blocksizes[*newblocknr];
873  (*newblocknr)--;
874  }
875  while ((*newblocknr >= 0) && (*newposinblk < 0));
876  /* Add one after loop */
877  (*newblocknr)++;
878  if (*newposinblk < 0) {
879  return _sion_errorprint(SION_NOT_SUCCESS, _SION_ERROR_RETURN,
880  "sion_seek: seek before beginning of file (posend = %ld, abspos = %ld), aborting ...\n", posend, *newposinblk);
881  }
882  }
883  else {
884  return _sion_errorprint(SION_NOT_SUCCESS, _SION_ERROR_RETURN,
885  "sion_seek: seek past end of file (%ld > 0), aborting ...\n", posend);
886  }
887 
888 
889  return rc;
890 }
891 #undef DFUNCTION
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.
int _sion_seek_on_all_ranks_read(_sion_filedesc *sion_filedesc, int rank, int blocknr, sion_int64 posinblk)
Function to set the file pointer to a new position, updates 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 File Descriptor Structure.
Definition: sion_filedesc.h:77
sion_int64 * all_blockcount
int _sion_file_purge(_sion_fileptr *sion_fileptr)
Purge data to file.
Definition: sion_file.c:322
int _sion_seek_on_all_ranks_read_mapped(_sion_filedesc *sion_filedesc_master, int rank, int blocknr, sion_int64 posinblk)
Function to set the file pointer to a new position, updates internal data structure.
sion_int64 * all_currentpos
int _sion_seek_on_all_ranks_write_mapped(_sion_filedesc *sion_filedesc_master, int rank, int blocknr, sion_int64 posinblk)
Function to set the file pointer to a new position, updates internal data structure.
sion_int64 * all_globalranks
sion_int64 * all_currentblocknr
#define SION_CURRENT_BLK
Definition: sion_const.h:67
int _sion_realloc_filedesc_blocklist(_sion_filedesc *sion_filedesc, sion_int32 maxchunks)
Increase the memory used by the internal sion structure for the blocklist.
#define SION_END_POS
Definition: sion_const.h:72
#define SION_KEYVAL_NONE
Definition: sion_const.h:80
int _sion_seek_search_abs_pos(_sion_filedesc *sion_filedesc, sion_int64 abspos, int *newblocknr, sion_int64 *newposinblk)
Find block and position inside block for absolute position abspos.
sion_int64 * all_startpointers
#define SION_CURRENT_RANK
Definition: sion_const.h:66
#define SION_CURRENT_POS
Definition: sion_const.h:70
int _sion_errorprint(int rc, int level, const char *format,...)
Internal SION error.
int _sion_update_fileposition(_sion_filedesc *sion_filedesc)
Update the internal data structure (check fileposition)
int _sion_seek_search_end_pos(_sion_filedesc *sion_filedesc, sion_int64 posend, int *newblocknr, sion_int64 *newposinblk)
Find block and position inside block position relative to end.
int _sion_seek_on_all_ranks_read_master(_sion_filedesc *sion_filedesc, int rank, int blocknr, sion_int64 posinblk)
Function to set the file pointer to a new position, updates internal data structure.
#define SION_ABSOLUTE_POS
Definition: sion_const.h:71
sion_int64 * blocksizes
Definition: sion_filedesc.h:98
sion_int32 currentblocknr
Definition: sion_filedesc.h:95
sion_int64 * all_chunksizes
int _sion_print_filedesc(_sion_filedesc *sion_filedesc, int level, char *desc, int flag)
Print the initialized sion file description.
int _sion_seek_on_current_rank_read(_sion_filedesc *sion_filedesc, int rank, int blocknr, sion_int64 posinblk)
Function to set the file pointer to a new position in the same rank, updates internal data structure.
Sion Time Stamp Header.
int _sion_seek_on_all_ranks_write(_sion_filedesc *sion_filedesc, int rank, int blocknr, sion_int64 posinblk)
Function to set the file pointer to a new position, updates internal data structure.
sion_int64 * all_blocksizes
sion_int32 ntotaltasksinfile
sion_int32 nlocaltasksinfile
_sion_filedesc ** multifiles
_sion_fileptr * fileptr
Definition: sion_filedesc.h:80
int _sion_seek_on_current_rank_write(_sion_filedesc *sion_filedesc, int rank, int blocknr, sion_int64 posinblk)
Function to set the file pointer to a new position in the same rank, updates internal data structure.