SIONlib  1.7.7
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-2019 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** See the file COPYRIGHT in the package base directory for details **
8 ****************************************************************************/
19 #define _XOPEN_SOURCE 700
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 /* #define _FORTRANUNDERSCORE
26  #define _FORTRANNOCAPS
27  #define _FORTRANCAPS
28  #define _FORTRANDOUBLEUNDERSCORE */
29 #include "sion.h"
30 #include "sion_debug.h"
31 #include "sion_fd.h"
32 #include "sion_filedesc.h"
33 #include "sion_fortran.h"
34 
35 #define DFUNCTION "fsion_open_c"
56 void fsion_open_c(char *fname,
57  char *file_mode,
58  int *ntasks,
59  int *nfiles,
60  sion_int64 *chunksizes,
61  sion_int32 *fsblksize,
62  int *globalranks,
63  int *sid,
64  int fname_len,
65  int file_mode_len)
66 {
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  free(fname_tmp); fname_tmp=NULL;
83  return;
84  }
85 
86 
87  /* printf("sizeof(fname_len)=%d\n",sizeof(fname_len)); */
88  /* printf("%s fname_len=%d %x %d\n",fname, fname_len,fname_tmp,((fname_len + 1) * sizeof(char))); */
89  /* printf("file_mode_len=%d %x %d\n",file_mode_len,fmode_tmp,((file_mode_len + 1) * sizeof(char))); */
90 
91  /* Copy the strings to the new buffers and pad with nulls */
92  strncpy(fname_tmp, fname, fname_len);
93  strncpy(fmode_tmp, file_mode, file_mode_len);
94 
95  fname_tmp[fname_len] = '\0';
96  fmode_tmp[file_mode_len] = '\0';
97 
98 
99 
100  DPRINTFP((2, DFUNCTION, -1, "params: %s %s %d %ld \n", fname_tmp, fmode_tmp, (int) *ntasks, (long) *fsblksize));
101 
102  if (!strcmp(fmode_tmp, "bw") || !strcmp(fmode_tmp, "wb")) {
103  /* **************** WRITE mode **************** */
104  (*sid) = sion_open(fname_tmp, fmode_tmp, ntasks, nfiles, &chunksizes, fsblksize, &globalranks, NULL);
105  } else {
106  (*sid) = sion_open(fname_tmp, fmode_tmp, ntasks, nfiles, NULL, fsblksize, NULL, NULL);
107  }
108  DPRINTFP((2, DFUNCTION, -1, "sid: %d\n", *sid));
109 
110  /* Free the used memory */
111  free(fname_tmp); fname_tmp=NULL;
112  free(fmode_tmp); fmode_tmp=NULL;
113 }
114 #undef DFUNCTION
115 
116 
143 void fsion_open_rank_c(char *fname,
144  char *file_mode,
145  sion_int64 *chunksize,
146  sion_int32 *fsblksize,
147  int *rank,
148  int *sid,
149  int fname_len,
150  int file_mode_len)
151 {
152  /* Fortran strings are not NULL-terminated => insert \0 at the end */
153  char *fname_tmp, *fmode_tmp;
154 
155  fname_tmp = (char *) malloc((size_t) ((fname_len + 1) * sizeof(char)));
156  fmode_tmp = (char *) malloc((size_t) ((file_mode_len + 1) * sizeof(char)));
157  /* Copy the strings to the new buffers and pad with nulls */
158  strncpy(fname_tmp, fname, fname_len);
159  strncpy(fmode_tmp, file_mode, file_mode_len);
160  fname_tmp[fname_len] = '\0';
161  fmode_tmp[file_mode_len] = '\0';
162 
163  (*sid) = sion_open_rank(fname_tmp, fmode_tmp, chunksize, fsblksize, rank, NULL);
164 
165  /* Free the used memory */
166  free(fname_tmp);
167  free(fmode_tmp);
168 }
169 
183 void fsion_close_c(int *sid,
184  int *ierr)
185 {
186  (*ierr) = sion_close(*sid);
187 }
188 
205 void fsion_feof_c(int *sid,
206  int *eof)
207 {
208 
209  *eof = sion_feof(*sid);
210 }
211 
212 #define DFUNCTION "fsion_seek_c"
229 void fsion_seek_c(int *sid,
230  int *rank,
231  int *currentblocknr,
232  sion_int64 *posinblk,
233  int *ierr)
234 {
235 
236  DPRINTFP((2, DFUNCTION, -1, "params: %d %d %d %lld\n", *sid, *rank, *currentblocknr, *posinblk));
237 
238  (*ierr) = sion_seek(*sid, *rank, *currentblocknr, *posinblk);
239 }
240 #undef DFUNCTION
241 
259  sion_int64 *bytes,
260  int *ierr)
261 {
262 
263  (*ierr) = sion_ensure_free_space(*sid, *bytes);
264 }
265 
266 
267 
277 void fsion_flush_c(int *sid,
278  int *ierr)
279 {
280 
281  (*ierr) = sion_flush(*sid);
282 
283 }
284 
285 
296 sion_int64 fsion_bytes_avail_in_block_c(int *sid)
297 {
298 
299  return sion_bytes_avail_in_block(*sid);
300 }
301 
323 void fsion_get_locations_c(int *sid,
324  int *ntasks,
325  int *maxblocks,
326  sion_int64 *globalskip,
327  sion_int64 *start_of_varheader,
328  sion_int64 **sion_localsizes,
329  sion_int64 **sion_globalranks,
330  sion_int64 **sion_chunkcount,
331  sion_int64 **sion_chunksizes,
332  int *ierr)
333 {
334 
335  (*ierr) = sion_get_locations(*sid, ntasks, maxblocks, globalskip, start_of_varheader, sion_localsizes,
336  sion_globalranks, sion_chunkcount, sion_chunksizes);
337 }
338 
347 sion_int64 fsion_get_position_c(int *sid)
348 {
349 
350  return sion_get_position(*sid);
351 }
352 
353 /* OTHER UNDOCUMENTED FUNCTIONS */
354 
355 void fsion_get_current_locations_c(int *sid,
356  int *ntasks,
357  sion_int64 **sion_currentpos,
358  sion_int64 **sion_currentblocknr,
359  int *ierr)
360 {
361 
362  (*ierr) = sion_get_current_locations(*sid, ntasks, sion_currentpos, sion_currentblocknr);
363 }
364 
365 
366 void fsion_get_chunksizes_c(int *sid,
367  sion_int64 *chunksizes,
368  int *ierr)
369 {
370  _sion_filedesc *sion_filedesc;
371 
372  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
373  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
374  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
375  if (ierr) {
376  *ierr=-1;
377  }
378  return;
379  }
380  if(sion_filedesc->all_chunksizes) {
381  int i;
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 
395  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
396  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
397  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
398  if (ierr) {
399  *ierr=-1;
400  }
401  return;
402  }
403  if(sion_filedesc->all_globalranks) {
404  int i;
405  for(i=0;i<sion_filedesc->ntasks;i++) {
406  globalranks[i]=sion_filedesc->all_globalranks[i];
407  }
408  }
409 
410 }
411 
412 void fsion_get_mapping_spec_c(int *sid,
413  int *mapping_size,
414  int *numfiles,
415  int *ierr)
416 {
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  if (ierr) {
422  *ierr=-1;
423  }
424  return;
425  }
426  *mapping_size = sion_filedesc->mapping_size;
427  *numfiles = sion_filedesc->nfiles;
428 
429 }
430 
431 #define DFUNCTION "fsion_get_mapping_c"
432 void fsion_get_mapping_c(int *sid,
433  sion_int32 *mapping,
434  int *ierr)
435 {
436  _sion_filedesc *sion_filedesc;
437 
438  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
439  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
440  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
441  *ierr=-1;
442  return;
443  }
444  if(sion_filedesc->nfiles>1) {
445  int rank;
446  for (rank = 0; rank < sion_filedesc->mapping_size; rank++) {
447  DPRINTFP((32, DFUNCTION, 0, " mapping[%d] = %d , %d \n", rank,sion_filedesc->mapping[rank*2+0],sion_filedesc->mapping[rank*2+1]));
448  mapping[rank*2+0]=sion_filedesc->mapping[rank*2+0];
449  mapping[rank*2+1]=sion_filedesc->mapping[rank*2+1];
450  }
451  } else {
452  *ierr=-1;
453  }
454 }
455 #undef DFUNCTION
456 
465 void fsion_get_fileno_c(int *sid,
466  int *filenumber)
467 {
468  _sion_filedesc *sion_filedesc;
469  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
470  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
471  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
472  *filenumber=-1;
473  return;
474  }
475  *filenumber = _sion_file_get_fd(sion_filedesc->fileptr);
476  return;
477 }
478 
479 
487  int *endianness)
488 {
489  *endianness = sion_get_file_endianness(*sid);
490  return;
491 }
492 
493 
499 void fsion_get_endianness_c(int *endianness)
500 {
501  *endianness = sion_get_endianness();
502  return;
503 }
504 
505 
513  int *needed)
514 {
515  *needed = sion_endianness_swap_needed(*sid);
516  return;
517 }
518 
519 
536 void fsion_swap_c(void *target,
537  void *source,
538  int *size,
539  int *n,
540  int *do_swap,
541  int *rc)
542 {
543  (*rc) = 1;
544  sion_swap( (void *) target,
545  (void *) source,
546  (int) *size,
547  (int) *n,
548  (int) *do_swap
549  );
550 }
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:307
int sion_get_file_endianness(int sid)
Returns edianness of data in file sid.
Definition: sion_common.c:253
int sion_endianness_swap_needed(int sid)
Returns whether or not byte swapping is needed for sid.
Definition: sion_common.c:277
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:84
sion_int64 sion_get_position(int sid)
Function that returns the current file position.
Definition: sion_common.c:930
int sion_flush(int sid)
Flushed sion file.
Definition: sion_common.c:1030
int sion_ensure_free_space(int sid, sion_int64 bytes)
Funtion to ensure that enough space is available for writing.
Definition: sion_common.c:1053
int sion_feof(int sid)
Function that indicates whether the end of file is reached for this task.
Definition: sion_common.c:809
sion_int64 sion_bytes_avail_in_block(int sid)
Return the number of bytes available in the current chunk.
Definition: sion_common.c:879
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:698
int sion_get_endianness(void)
Return endianness.
Definition: sion_tools.c:32
void sion_swap(void *target, void *source, int size, int n, int do_swap)
Definition: sion_convert.c:44
void * _sion_vcdtovcon(int sid)
Definition: sion_fd.c:53
int _sion_vcdtype(int sid)
Definition: sion_fd.c:58
#define SION_FILEDESCRIPTOR
Definition: sion_fd.h:17
int _sion_file_get_fd(_sion_fileptr *sion_fileptr)
Utility function: Get POSIX fp.
Definition: sion_file.c:573
void fsion_get_endianness_c(int *endianness)
Fortran function that returns current endianness (1-> big endian, 0 ->little endian)
Definition: sion_fortran.c:499
void fsion_feof_c(int *sid, int *eof)
Fortran function that indicates the end of file for this task.
Definition: sion_fortran.c:205
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:296
void fsion_get_fileno_c(int *sid, int *filenumber)
Fortran function that returns the current file number.
Definition: sion_fortran.c:465
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:229
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:512
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:56
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:486
void fsion_flush_c(int *sid, int *ierr)
Fortran procedure to flush a sion file.
Definition: sion_fortran.c:277
sion_int64 fsion_get_position_c(int *sid)
Fortran function that returns the current file position.
Definition: sion_fortran.c:347
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:323
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:143
void fsion_swap_c(void *target, void *source, int *size, int *n, int *do_swap, int *rc)
Fortran procedure to swap endianness of data.
Definition: sion_fortran.c:536
void fsion_close_c(int *sid, int *ierr)
Fortran procedure to close a sion file.
Definition: sion_fortran.c:183
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:258
Fortran API.
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:83
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:54
int sion_close(int sid)
Close a sion file.
Definition: sion_serial.c:106
Sion File Descriptor Structure.
Definition: sion_filedesc.h:79
sion_int64 * all_chunksizes
sion_int64 * all_globalranks
_sion_fileptr * fileptr
Definition: sion_filedesc.h:82