SIONlib  1.7.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-2016 **
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  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, &fileptr);
105  } else {
106  (*sid) = sion_open(fname_tmp, fmode_tmp, ntasks, nfiles, NULL, fsblksize, NULL, &fileptr);
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 
153  FILE *fileptr;
154  /* Fortran strings are not NULL-terminated => insert \0 at the end */
155  char *fname_tmp, *fmode_tmp;
156 
157  fname_tmp = (char *) malloc((size_t) ((fname_len + 1) * sizeof(char)));
158  fmode_tmp = (char *) malloc((size_t) ((file_mode_len + 1) * sizeof(char)));
159  /* Copy the strings to the new buffers and pad with nulls */
160  strncpy(fname_tmp, fname, fname_len);
161  strncpy(fmode_tmp, file_mode, file_mode_len);
162  fname_tmp[fname_len] = '\0';
163  fmode_tmp[file_mode_len] = '\0';
164 
165  (*sid) = sion_open_rank(fname_tmp, fmode_tmp, chunksize, fsblksize, rank, &fileptr);
166 
167  /* Free the used memory */
168  free(fname_tmp);
169  free(fmode_tmp);
170 }
171 
185 void fsion_close_c(int *sid,
186  int *ierr)
187 {
188  (*ierr) = sion_close(*sid);
189 }
190 
207 void fsion_feof_c(int *sid,
208  int *eof)
209 {
210 
211  *eof = sion_feof(*sid);
212 }
213 
214 #define DFUNCTION "fsion_seek_c"
215 
231 void fsion_seek_c(int *sid,
232  int *rank,
233  int *currentblocknr,
234  sion_int64 *posinblk,
235  int *ierr)
236 {
237 
238  DPRINTFP((2, DFUNCTION, -1, "params: %d %d %d %lld\n", *sid, *rank, *currentblocknr, *posinblk));
239 
240  (*ierr) = sion_seek(*sid, *rank, *currentblocknr, *posinblk);
241 }
242 #undef DFUNCTION
243 
261  sion_int64 *bytes,
262  int *ierr)
263 {
264 
265  (*ierr) = sion_ensure_free_space(*sid, *bytes);
266 }
267 
268 
269 
279 void fsion_flush_c(int *sid,
280  int *ierr)
281 {
282 
283  (*ierr) = sion_flush(*sid);
284 
285 }
286 
287 
298 sion_int64 fsion_bytes_avail_in_block_c(int *sid)
299 {
300 
301  return sion_bytes_avail_in_block(*sid);
302 }
303 
325 void fsion_get_locations_c(int *sid,
326  int *ntasks,
327  int *maxblocks,
328  sion_int64 *globalskip,
329  sion_int64 *start_of_varheader,
330  sion_int64 **sion_localsizes,
331  sion_int64 **sion_globalranks,
332  sion_int64 **sion_chunkcount,
333  sion_int64 **sion_chunksizes,
334  int *ierr)
335 {
336 
337  (*ierr) = sion_get_locations(*sid, ntasks, maxblocks, globalskip, start_of_varheader, sion_localsizes,
338  sion_globalranks, sion_chunkcount, sion_chunksizes);
339 }
340 
349 sion_int64 fsion_get_position_c(int *sid)
350 {
351 
352  return sion_get_position(*sid);
353 }
354 
355 /* OTHER UNDOCUMENTED FUNCTIONS */
356 
357 void fsion_get_current_locations_c(int *sid,
358  int *ntasks,
359  sion_int64 **sion_currentpos,
360  sion_int64 **sion_currentblocknr,
361  int *ierr)
362 {
363 
364  (*ierr) = sion_get_current_locations(*sid, ntasks, sion_currentpos, sion_currentblocknr);
365 }
366 
367 
368 void fsion_get_chunksizes_c(int *sid,
369  sion_int64 *chunksizes,
370  int *ierr)
371 {
372  _sion_filedesc *sion_filedesc;
373 
374  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
375  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
376  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
377  if (ierr) {
378  *ierr=-1;
379  }
380  return;
381  }
382  if(sion_filedesc->all_chunksizes) {
383  int i;
384  for(i=0;i<sion_filedesc->ntasks;i++) {
385  chunksizes[i]=sion_filedesc->all_chunksizes[i];
386  }
387  }
388 
389 }
390 
391 void fsion_get_globalranks_c(int *sid,
392  int *globalranks,
393  int *ierr)
394 {
395  _sion_filedesc *sion_filedesc;
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  if (ierr) {
401  *ierr=-1;
402  }
403  return;
404  }
405  if(sion_filedesc->all_globalranks) {
406  int i;
407  for(i=0;i<sion_filedesc->ntasks;i++) {
408  globalranks[i]=sion_filedesc->all_globalranks[i];
409  }
410  }
411 
412 }
413 
414 void fsion_get_mapping_spec_c(int *sid,
415  int *mapping_size,
416  int *numfiles,
417  int *ierr)
418 {
419  _sion_filedesc *sion_filedesc;
420  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
421  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
422  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
423  if (ierr) {
424  *ierr=-1;
425  }
426  return;
427  }
428  *mapping_size = sion_filedesc->mapping_size;
429  *numfiles = sion_filedesc->nfiles;
430 
431 }
432 
433 #define DFUNCTION "fsion_get_mapping_c"
434 void fsion_get_mapping_c(int *sid,
435  sion_int32 *mapping,
436  int *ierr)
437 {
438  _sion_filedesc *sion_filedesc;
439 
440  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
441  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
442  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
443  *ierr=-1;
444  return;
445  }
446  if(sion_filedesc->nfiles>1) {
447  int rank;
448  for (rank = 0; rank < sion_filedesc->mapping_size; rank++) {
449  DPRINTFP((32, DFUNCTION, 0, " mapping[%d] = %d , %d \n", rank,sion_filedesc->mapping[rank*2+0],sion_filedesc->mapping[rank*2+1]));
450  mapping[rank*2+0]=sion_filedesc->mapping[rank*2+0];
451  mapping[rank*2+1]=sion_filedesc->mapping[rank*2+1];
452  }
453  } else {
454  *ierr=-1;
455  }
456 }
457 #undef DFUNCTION
458 
467 void fsion_get_fileno_c(int *sid,
468  int *filenumber)
469 {
470  _sion_filedesc *sion_filedesc;
471  if ((_sion_vcdtype(*sid) != SION_FILEDESCRIPTOR)
472  || !(sion_filedesc = _sion_vcdtovcon(*sid))) {
473  fprintf(stderr, "invalid sion_filedesc, aborting %d ...\n", *sid);
474  *filenumber=-1;
475  return;
476  }
477  *filenumber = _sion_file_get_fd(sion_filedesc->fileptr);
478  return;
479 }
480 
481 
489  int *endianness)
490 {
491  *endianness = sion_get_file_endianness(*sid);
492  return;
493 }
494 
495 
501 void fsion_get_endianness_c(int *endianness)
502 {
503  *endianness = sion_get_endianness();
504  return;
505 }
506 
507 
515  int *needed)
516 {
517  *needed = sion_endianness_swap_needed(*sid);
518  return;
519 }
520 
521 
537 void fsion_swap_c(void *target,
538  void *source,
539  int *size,
540  int *n,
541  int *aflag,
542  int *rc)
543 {
544  (*rc) = 1;
545  sion_swap( (void *) target,
546  (void *) source,
547  (int) *size,
548  (int) *n,
549  (int) *aflag
550  );
551 }
int sion_feof(int sid)
Function that indicates whether the end of file is reached for this task.
Definition: sion_common.c:770
sion_int64 sion_bytes_avail_in_block(int sid)
Return the number of bytes available in the current chunk.
Definition: sion_common.c:840
Sion File Descriptor Structure.
Definition: sion_filedesc.h:77
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:325
int sion_get_endianness(void)
Return endianness.
Definition: sion_tools.c:30
int sion_ensure_free_space(int sid, sion_int64 bytes)
Funtion to ensure that enough space is available for writing.
Definition: sion_common.c:1014
sion_int64 sion_get_position(int sid)
Function that returns the current file position.
Definition: sion_common.c:891
void fsion_close_c(int *sid, int *ierr)
Fortran procedure to close a sion file.
Definition: sion_fortran.c:185
void fsion_flush_c(int *sid, int *ierr)
Fortran procedure to flush a sion file.
Definition: sion_fortran.c:279
int sion_get_file_endianness(int sid)
Returns edianness of data in file sid.
Definition: sion_common.c:255
sion_int64 * all_globalranks
int _sion_file_get_fd(_sion_fileptr *sion_fileptr)
Utility function: Get POSIX fp.
Definition: sion_file.c:426
int sion_close(int sid)
Close a sion file.
Definition: sion_serial.c:113
int _sion_vcdtype(int sid)
Definition: sion_fd.c:56
void fsion_get_endianness_c(int *endianness)
Fortran function that returns current endianness (1-> big endian, 0 ->little endian)
Definition: sion_fortran.c:501
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:231
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:309
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:86
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:279
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:537
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:260
void * _sion_vcdtovcon(int sid)
Definition: sion_fd.c:51
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:659
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
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:298
#define SION_FILEDESCRIPTOR
Definition: sion_fd.h:17
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:514
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:349
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:488
_sion_fileptr * fileptr
Definition: sion_filedesc.h:80
void fsion_feof_c(int *sid, int *eof)
Fortran function that indicates the end of file for this task.
Definition: sion_fortran.c:207
void fsion_get_fileno_c(int *sid, int *filenumber)
Fortran function that returns the current file number.
Definition: sion_fortran.c:467
int sion_flush(int sid)
Flushed sion file.
Definition: sion_common.c:991