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