SIONlib  1.6.1
Scalable I/O library for parallel access to task-local files
sion_fortran.c
Go to the documentation of this file.
1 /****************************************************************************
2 ** SIONLIB http://www.fz-juelich.de/jsc/sionlib **
3 *****************************************************************************
4 ** Copyright (c) 2008-2015 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** See the file COPYRIGHT in the package base directory for details **
8 ****************************************************************************/
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 /* #define _FORTRANUNDERSCORE
24  #define _FORTRANNOCAPS
25  #define _FORTRANCAPS
26  #define _FORTRANDOUBLEUNDERSCORE */
27 #include "sion.h"
28 #include "sion_debug.h"
29 #include "sion_fd.h"
30 #include "sion_filedesc.h"
31 #include "sion_fortran.h"
32 
33 #define DFUNCTION "fsion_open_c"
34 
54 void fsion_open_c(char *fname,
55  char *file_mode,
56  int *ntasks,
57  int *nfiles,
58  sion_int64 *chunksizes,
59  sion_int32 *fsblksize,
60  int *globalranks,
61  int *sid,
62  int fname_len,
63  int file_mode_len)
64 {
65 
66  FILE *fileptr;
67  /* Fortran strings are not NULL-terminated => insert \0 at the end */
68  char *fname_tmp, *fmode_tmp;
69 
70 
71  fname_tmp = (char *) malloc((size_t) ((fname_len + 1) * sizeof(char)));
72  if(fname_tmp == NULL) {
73  fprintf(stderr, "could not allocate memory for internal filename buffer, returning ...\n");
74  *sid=-1;
75  return;
76  }
77 
78  fmode_tmp = (char *) malloc((size_t) ((file_mode_len + 1) * sizeof(char)));
79  if(fmode_tmp == NULL) {
80  fprintf(stderr, "could not allocate memory for internal filemode buffer, returning ...\n");
81  *sid=-1;
82  return;
83  }
84 
85 
86  /* printf("sizeof(fname_len)=%d\n",sizeof(fname_len)); */
87  /* printf("%s fname_len=%d %x %d\n",fname, fname_len,fname_tmp,((fname_len + 1) * sizeof(char))); */
88  /* printf("file_mode_len=%d %x %d\n",file_mode_len,fmode_tmp,((file_mode_len + 1) * sizeof(char))); */
89 
90  /* Copy the strings to the new buffers and pad with nulls */
91  strncpy(fname_tmp, fname, fname_len);
92  strncpy(fmode_tmp, file_mode, file_mode_len);
93 
94  fname_tmp[fname_len] = '\0';
95  fmode_tmp[file_mode_len] = '\0';
96 
97 
98 
99  DPRINTFP((2, DFUNCTION, -1, "params: %s %s %d %ld \n", fname_tmp, fmode_tmp, (int) *ntasks, (long) *fsblksize));
100 
101  if (!strcmp(fmode_tmp, "bw") || !strcmp(fmode_tmp, "wb")) {
102  /* **************** WRITE mode **************** */
103  (*sid) = sion_open(fname_tmp, fmode_tmp, ntasks, nfiles, &chunksizes, fsblksize, &globalranks, &fileptr);
104  } else {
105  (*sid) = sion_open(fname_tmp, fmode_tmp, ntasks, nfiles, NULL, fsblksize, NULL, &fileptr);
106  }
107  DPRINTFP((2, DFUNCTION, -1, "sid: %d\n", *sid));
108 
109  /* Free the used memory */
110  free(fname_tmp); fname_tmp=NULL;
111  free(fmode_tmp); fmode_tmp=NULL;
112 }
113 #undef DFUNCTION
114 
115 
142 void fsion_open_rank_c(char *fname,
143  char *file_mode,
144  sion_int64 *chunksize,
145  sion_int32 *fsblksize,
146  int *rank,
147  int *sid,
148  int fname_len,
149  int file_mode_len)
150 {
151 
152  FILE *fileptr;
153  /* Fortran strings are not NULL-terminated => insert \0 at the end */
154  char *fname_tmp, *fmode_tmp;
155 
156  fname_tmp = (char *) malloc((size_t) ((fname_len + 1) * sizeof(char)));
157  fmode_tmp = (char *) malloc((size_t) ((file_mode_len + 1) * sizeof(char)));
158  /* Copy the strings to the new buffers and pad with nulls */
159  strncpy(fname_tmp, fname, fname_len);
160  strncpy(fmode_tmp, file_mode, file_mode_len);
161  fname_tmp[fname_len] = '\0';
162  fmode_tmp[file_mode_len] = '\0';
163 
164  (*sid) = sion_open_rank(fname_tmp, fmode_tmp, chunksize, fsblksize, rank, &fileptr);
165 
166  /* Free the used memory */
167  free(fname_tmp);
168  free(fmode_tmp);
169 }
170 
184 void fsion_close_c(int *sid,
185  int *ierr)
186 {
187  (*ierr) = sion_close(*sid);
188 }
189 
206 void fsion_feof_c(int *sid,
207  int *eof)
208 {
209 
210  *eof = sion_feof(*sid);
211 }
212 
213 #define DFUNCTION "fsion_seek_c"
214 
230 void fsion_seek_c(int *sid,
231  int *rank,
232  int *currentblocknr,
233  sion_int64 *posinblk,
234  int *ierr)
235 {
236 
237  DPRINTFP((2, DFUNCTION, -1, "params: %d %d %d %lld\n", *sid, *rank, *currentblocknr, *posinblk));
238 
239  (*ierr) = sion_seek(*sid, *rank, *currentblocknr, *posinblk);
240 }
241 #undef DFUNCTION
242 
260  sion_int64 *bytes,
261  int *ierr)
262 {
263 
264  (*ierr) = sion_ensure_free_space(*sid, *bytes);
265 }
266 
267 
268 
278 void fsion_flush_c(int *sid,
279  int *ierr)
280 {
281 
282  (*ierr) = sion_flush(*sid);
283 
284 }
285 
286 
297 sion_int64 fsion_bytes_avail_in_block_c(int *sid)
298 {
299 
300  return sion_bytes_avail_in_block(*sid);
301 }
302 
324 void fsion_get_locations_c(int *sid,
325  int *ntasks,
326  int *maxblocks,
327  sion_int64 *globalskip,
328  sion_int64 *start_of_varheader,
329  sion_int64 **sion_localsizes,
330  sion_int64 **sion_globalranks,
331  sion_int64 **sion_chunkcount,
332  sion_int64 **sion_chunksizes,
333  int *ierr)
334 {
335 
336  (*ierr) = sion_get_locations(*sid, ntasks, maxblocks, globalskip, start_of_varheader, sion_localsizes,
337  sion_globalranks, sion_chunkcount, sion_chunksizes);
338 }
339 
348 sion_int64 fsion_get_position_c(int *sid)
349 {
350 
351  return sion_get_position(*sid);
352 }
353 
354 /* OTHER UNDOCUMENTED FUNCTIONS */
355 
356 void fsion_get_current_locations_c(int *sid,
357  int *ntasks,
358  sion_int64 **sion_currentpos,
359  sion_int64 **sion_currentblocknr,
360  int *ierr)
361 {
362 
363  (*ierr) = sion_get_current_locations(*sid, ntasks, sion_currentpos, sion_currentblocknr);
364 }
365 
366 
367 void fsion_get_chunksizes_c(int *sid,
368  sion_int64 *chunksizes,
369  int *ierr)
370 {
371  _sion_filedesc *sion_filedesc;
372  int i;
373  ierr=0;
374 
375  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
376  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
377  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
378  *ierr=-1;
379  return;
380  }
381  if(sion_filedesc->all_chunksizes) {
382  for(i=0;i<sion_filedesc->ntasks;i++) {
383  chunksizes[i]=sion_filedesc->all_chunksizes[i];
384  }
385  }
386 
387 }
388 
389 void fsion_get_globalranks_c(int *sid,
390  int *globalranks,
391  int *ierr)
392 {
393  _sion_filedesc *sion_filedesc;
394  int i;
395  ierr=0;
396 
397  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
398  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
399  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
400  *ierr=-1;
401  return;
402  }
403  if(sion_filedesc->all_globalranks) {
404  for(i=0;i<sion_filedesc->ntasks;i++) {
405  globalranks[i]=sion_filedesc->all_globalranks[i];
406  }
407  }
408 
409 }
410 
411 void fsion_get_mapping_spec_c(int *sid,
412  int *mapping_size,
413  int *numfiles,
414  int *ierr)
415 {
416  ierr=0;
417  _sion_filedesc *sion_filedesc;
418  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
419  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
420  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
421  *ierr=-1;
422  return;
423  }
424  *mapping_size = sion_filedesc->mapping_size;
425  *numfiles = sion_filedesc->nfiles;
426 
427 }
428 
429 #define DFUNCTION "fsion_get_mapping_c"
430 void fsion_get_mapping_c(int *sid,
431  sion_int32 *mapping,
432  int *ierr)
433 {
434  _sion_filedesc *sion_filedesc;
435  int rank;
436 
437  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
438  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
439  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
440  *ierr=-1;
441  return;
442  }
443  if(sion_filedesc->nfiles>1) {
444  for (rank = 0; rank < sion_filedesc->mapping_size; rank++) {
445  DPRINTFP((32, DFUNCTION, 0, " mapping[%d] = %d , %d \n", rank,sion_filedesc->mapping[rank*2+0],sion_filedesc->mapping[rank*2+1]));
446  mapping[rank*2+0]=sion_filedesc->mapping[rank*2+0];
447  mapping[rank*2+1]=sion_filedesc->mapping[rank*2+1];
448  }
449  } else {
450  *ierr=-1;
451  }
452 }
453 #undef DFUNCTION
454 
463 void fsion_get_fileno_c(int *sid,
464  int *filenumber)
465 {
466  _sion_filedesc *sion_filedesc;
467  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
468  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
469  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
470  *filenumber=-1;
471  return;
472  }
473  *filenumber = _sion_file_get_fd(sion_filedesc->fileptr);
474  return;
475 }
476 
477 
485  int *endianness)
486 {
487  *endianness = sion_get_file_endianness(*sid);
488  return;
489 }
490 
491 
497 void fsion_get_endianness_c(int *endianness)
498 {
499  *endianness = sion_get_endianness();
500  return;
501 }
502 
503 
511  int *needed)
512 {
513  *needed = sion_endianness_swap_needed(*sid);
514  return;
515 }
516 
517 
533 void fsion_swap_c(void *target,
534  void *source,
535  int *size,
536  int *n,
537  int *aflag,
538  int *rc)
539 {
540  (*rc) = 1;
541  sion_swap( (void *) target,
542  (void *) source,
543  (int) *size,
544  (int) *n,
545  (int) *aflag
546  );
547 }
int sion_feof(int sid)
Function that indicates whether the end of file is reached for this task.
Definition: sion_common.c:731
sion_int64 sion_bytes_avail_in_block(int sid)
Return the number of bytes available in the current chunk.
Definition: sion_common.c:801
Sion File Descriptor Structure.
Definition: sion_filedesc.h:72
void fsion_get_locations_c(int *sid, int *ntasks, int *maxblocks, sion_int64 *globalskip, sion_int64 *start_of_varheader, sion_int64 **sion_localsizes, sion_int64 **sion_globalranks, sion_int64 **sion_chunkcount, sion_int64 **sion_chunksizes, int *ierr)
Fortran procedure that returns pointers to internal fields.
Definition: sion_fortran.c:324
int sion_get_endianness(void)
Return endianness.
Definition: sion_tools.c:26
int sion_ensure_free_space(int sid, sion_int64 bytes)
Funtion to ensure that enough space is available for writing.
Definition: sion_common.c:975
sion_int64 sion_get_position(int sid)
Function that returns the current file position.
Definition: sion_common.c:852
void fsion_close_c(int *sid, int *ierr)
Fortran procedure to close a sion file.
Definition: sion_fortran.c:184
void fsion_flush_c(int *sid, int *ierr)
Fortran procedure to flush a sion file.
Definition: sion_fortran.c:278
int sion_get_file_endianness(int sid)
Returns edianness of data in file sid.
Definition: sion_common.c:246
sion_int64 * all_globalranks
int sion_close(int sid)
Close a sion file.
Definition: sion_serial.c:113
void fsion_get_endianness_c(int *endianness)
Fortran function that returns current endianness (1-> big endian, 0 ->little endian)
Definition: sion_fortran.c:497
void fsion_seek_c(int *sid, int *rank, int *currentblocknr, sion_int64 *posinblk, int *ierr)
Fortran procedure to set the file pointer to a new position.
Definition: sion_fortran.c:230
int sion_get_current_locations(int sid, int *ntasks, sion_int64 **sion_currentpos, sion_int64 **sion_currentblocknr)
Returns current position in file and pointer fiels containing chunk sizes.
Definition: sion_common.c:300
int sion_get_locations(int sid, int *ntasks, int *maxchunks, sion_int64 *globalskip, sion_int64 *start_of_varheader, sion_int64 **sion_chunksizes, sion_int64 **sion_globalranks, sion_int64 **sion_blockcount, sion_int64 **sion_blocksizes)
Returns pointers to internal fields.
Definition: sion_common.c:80
void fsion_open_c(char *fname, char *file_mode, int *ntasks, int *nfiles, sion_int64 *chunksizes, sion_int32 *fsblksize, int *globalranks, int *sid, int fname_len, int file_mode_len)
Fortran procedure to open a sion file in serial mode.
Definition: sion_fortran.c:54
int sion_endianness_swap_needed(int sid)
Returns whether or not byte swapping is needed for sid.
Definition: sion_common.c:270
Fortran API.
void fsion_swap_c(void *target, void *source, int *size, int *n, int *aflag, int *rc)
Fortran procedure to swap endianness of data.
Definition: sion_fortran.c:533
void fsion_ensure_free_space_c(int *sid, sion_int64 *bytes, int *ierr)
Fortran procedure to ensure that enough space is available.
Definition: sion_fortran.c:259
int sion_open_rank(char *fname, const char *file_mode, sion_int64 *chunksize, sion_int32 *fsblksize, int *rank, FILE **fileptr)
Open a sion file for a specific rank.
Definition: sion_serial.c:90
int sion_seek(int sid, int rank, int currentblocknr, sion_int64 posinblk)
Function to set the file pointer to a new position.
Definition: sion_common.c:620
void fsion_open_rank_c(char *fname, char *file_mode, sion_int64 *chunksize, sion_int32 *fsblksize, int *rank, int *sid, int fname_len, int file_mode_len)
Fortran procedure to open a sion file for a specific rank.
Definition: sion_fortran.c:142
sion_int64 fsion_bytes_avail_in_block_c(int *sid)
Fortran function that returns the number of bytes available in the current chunk.
Definition: sion_fortran.c:297
sion_int64 * all_chunksizes
void fsion_endianness_swap_needed_c(int *sid, int *needed)
Fortran function that returns whether or not byte swapping is needed (1 -> needed,...
Definition: sion_fortran.c:510
int sion_open(char *fname, const char *file_mode, int *ntasks, int *nfiles, sion_int64 **chunksizes, sion_int32 *fsblksize, int **globalranks, FILE **fileptr)
Open a sion file in serial mode.
Definition: sion_serial.c:61
sion_int64 fsion_get_position_c(int *sid)
Fortran function that returns the current file position.
Definition: sion_fortran.c:348
void sion_swap(void *target, void *source, int size, int n, int aflag)
Definition: sion_convert.c:36
void fsion_get_file_endianness_c(int *sid, int *endianness)
Fortran function that returns endianness of a sion file (1-> big endian, 0 ->little endian)
Definition: sion_fortran.c:484
_sion_fileptr * fileptr
Definition: sion_filedesc.h:75
void fsion_feof_c(int *sid, int *eof)
Fortran function that indicates the end of file for this task.
Definition: sion_fortran.c:206
void fsion_get_fileno_c(int *sid, int *filenumber)
Fortran function that returns the current file number.
Definition: sion_fortran.c:463
int sion_flush(int sid)
Flushed sion file.
Definition: sion_common.c:952