SIONlib  2.0.0-rc.1
Scalable I/O library for parallel access to task-local files
sion_serial.c
1 /****************************************************************************
2 ** SIONLIB http://www.fz-juelich.de/jsc/sionlib **
3 *****************************************************************************
4 ** Copyright (c) 2008-2018 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** See the file COPYRIGHT in the package base directory for details **
8 ****************************************************************************/
9 
10 #define _XOPEN_SOURCE 700
11 
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include "sion_common.h"
18 #include "sion_const.h"
19 #include "sion_debug.h"
20 #include "sion_enums.h"
21 #include "sion_error_handler.h"
22 #include "sion_flags.h"
23 #include "sion_internal.h"
24 #include "sion_serial.h"
25 
26 int sion_open(char *fname, const char *file_mode, int *ntasks, int *nfiles, int64_t **chunksizes, int32_t *fsblksize,
27  int **globalranks, FILE **fileptr)
28 {
29  int sid = 0;
30  _sion_flags_store *flags_store = _sion_parse_flags(file_mode);
31  if (!flags_store) {
32  return _sion_errorprint(
33  SION_ID_NOT_VALID, _SION_ERROR_RETURN, "_sion_open: could not parse file mode in %s, aborting ...\n", file_mode);
34  }
35 
36  DPRINTFP((1, "sion_open", 0, "enter open of file %s in %s mode\n", fname, file_mode));
37 
38  sid = _sion_open(fname, flags_store, ntasks, nfiles, chunksizes, fsblksize, globalranks, fileptr);
39 
40  _sion_flags_destroy_store(&flags_store);
41 
42  return sid;
43 }
44 
45 int sion_open_rank(char *fname, const char *file_mode, int64_t *chunksize, int32_t *fsblksize, int *rank, FILE **fileptr)
46 {
47  int sid;
48 
49  DPRINTFTS(*rank, "before open rank");
50  DPRINTFP((1, "sion_open_rank", *rank, "enter open of file %s in %s mode\n", fname, file_mode));
51  sid = _sion_open_rank(fname, file_mode, chunksize, fsblksize, rank, fileptr);
52  DPRINTFP((1, "sion_open_rank", 0, "leave open of file %s in %s mode sid=%d\n", fname, file_mode, sid));
53  DPRINTFTS(*rank, "after open rank");
54 
55  return sid;
56 }
57 
58 int sion_close(int sid)
59 {
60  int rc = 0;
61  DPRINTFP((1, "sion_close", -1, "enter close sid=%d\n", sid));
62  rc = _sion_close_sid(sid);
63  DPRINTFP((1, "sion_close", -1, "leave close sid=%d\n", sid));
64 
65  sion_dclose();
66 
67  return rc;
68 }
69 
70 sion_options sion_options_new()
71 {
72  return (sion_options){.n_chunksizes = 0,
73  .chunksizes = NULL, // FIXME: default to fsblksize (which is only known on open)
74  .fsblksize = -1,
75 
76  .n_multifile = 1,
77 
78  .keyval_mode = SION_KEYVAL_MODE_NONE,
79 
80  .lowlevel_api = SION_LOWLEVEL_API_C};
81 }
82 
83 void sion_options_set_chunksizes(sion_options *options, int n, int64_t chunksizes[])
84 {
85  options->chunksizes = chunksizes;
86  options->n_chunksizes = n;
87 }
88 
89 void sion_options_set_fsblksize(sion_options *options, int32_t fsblksize)
90 {
91  options->fsblksize = fsblksize;
92 }
93 
94 void sion_options_set_multifile_number(sion_options *options, int multifile_number)
95 {
96  options->n_multifile = multifile_number;
97 }
98 
99 void sion_options_set_keyval_mode(sion_options *options, sion_keyval_mode keyval_mode)
100 {
101  options->keyval_mode = keyval_mode;
102 }
103 
104 void sion_options_set_lowlevel_api(sion_options *options, sion_lowlevel_api lowlevel_api)
105 {
106  options->lowlevel_api = lowlevel_api;
107 }
108 
109 _sion_flags_store *_sion_options_into_flags_store(sion_open_mode mode, const sion_options *options)
110 {
111  _sion_flags_store *flags_store = _sion_flags_create_store();
112  if (!flags_store) {
113  /* FIXME: add error message */
114  fprintf(stderr, "could not create flags store.\n");
115  return NULL;
116  }
117 
118  switch (mode) {
119  case SION_OPEN_READ:
120  _sion_flags_add(flags_store, "br", "");
121 
122  switch (options->keyval_mode) {
123  case SION_KEYVAL_MODE_NONE:
124  /* do nothing */
125  break;
126  case SION_KEYVAL_MODE_DEFAULT:
127  /* FALLTHROUGH */
128  case SION_KEYVAL_MODE_INLINE:
129  _sion_flags_add(flags_store, "keyval", "inline");
130  break;
131  case SION_KEYVAL_MODE_META:
132  _sion_flags_add(flags_store, "keyval", "meta");
133  break;
134  case SION_KEYVAL_MODE_HASH:
135  _sion_flags_add(flags_store, "keyval", "hash");
136  break;
137  case SION_KEYVAL_MODE_UNKNOWN:
138  _sion_flags_add(flags_store, "keyval", "unknown");
139  break;
140  default:
141  /* FIXME: add error message */
142  fprintf(stderr, "wrong keyval mode in read mode.\n");
143  _sion_flags_destroy_store(&flags_store);
144  return NULL;
145  }
146 
147  break;
148  case SION_OPEN_WRITE:
149  _sion_flags_add(flags_store, "bw", "");
150 
151  switch (options->keyval_mode) {
152  case SION_KEYVAL_MODE_NONE:
153  /* do nothing */
154  break;
155  case SION_KEYVAL_MODE_DEFAULT:
156  /* FALLTHROUGH */
157  case SION_KEYVAL_MODE_INLINE:
158  _sion_flags_add(flags_store, "keyval", "inline");
159  break;
160  case SION_KEYVAL_MODE_META:
161  _sion_flags_add(flags_store, "keyval", "meta");
162  break;
163  case SION_KEYVAL_MODE_HASH:
164  _sion_flags_add(flags_store, "keyval", "hash");
165  break;
166  default: /* 'unknown' keyval mode is not allowed in write mode */
167  /* FIXME: add error message */
168  fprintf(stderr, "wrong keyval mode in read mode.\n");
169  _sion_flags_destroy_store(&flags_store);
170  return NULL;
171  }
172 
173  break;
174  }
175 
176  switch (options->lowlevel_api) {
177  case SION_LOWLEVEL_API_C:
178  _sion_flags_add(flags_store, "ansi", "");
179  break;
180  case SION_LOWLEVEL_API_POSIX:
181  _sion_flags_add(flags_store, "posix", "");
182  break;
183  }
184 
185  _sion_flags_update_mask(flags_store);
186 
187  return flags_store;
188 }
189 
190 int sion_open_with_options(const char *fname, sion_open_mode mode, int n, const sion_options *options_)
191 {
192  // FIXME: need local copy to satisfy signature of sion_open()
193  sion_options options = (options_) ? *options_ : sion_options_new();
194 
195  int64_t *chunksizes = NULL;
196  if (!options.chunksizes) {
197  chunksizes = malloc(n * sizeof(int64_t));
198  if (chunksizes == NULL) {
199  return _sion_errorprint(SION_ID_NOT_VALID, _SION_ERROR_RETURN, "%s: could not allocate memory for chunksizes\n", __func__);
200  }
201  for (size_t i = 0; i < n; ++i) {
202  chunksizes[i] = 1024 * 1024; // FIXME: default to fsblksize (which is only known on open)
203  }
204  } else {
205  if (options.n_chunksizes != n) {
206  return _sion_errorprint(SION_ID_NOT_VALID, _SION_ERROR_RETURN,
207  "%s: size of chunksizes array in options (%d) does not match number of logical files (%d)\n", __func__,
208  options.n_chunksizes, n);
209  }
210  }
211 
212  _sion_flags_store *flags_store = _sion_options_into_flags_store(mode, &options);
213  if (!flags_store) {
214  return -1;
215  }
216 
217  int *globalranks = calloc(n, sizeof(int));
218  if (globalranks == NULL) {
219  free(chunksizes);
220  return _sion_errorprint(SION_ID_NOT_VALID, _SION_ERROR_RETURN, "%s: could not allocate memory for globalranks\n", __func__);
221  }
222 
223  int sid = _sion_open(fname, flags_store, &n, &options.n_multifile, (options.chunksizes) ? &options.chunksizes : &chunksizes,
224  &options.fsblksize, &globalranks, NULL);
225 
226  free(globalranks);
227  free(chunksizes);
228 
229  _sion_flags_destroy_store(&flags_store);
230  return sid;
231 }
int sion_open_rank(char *fname, const char *file_mode, int64_t *chunksize, int32_t *fsblksize, int *rank, FILE **fileptr)
Open a sion file for a specific rank.
Definition: sion_serial.c:45
int sion_close(int sid)
Close a sion file.
Definition: sion_serial.c:58
int sion_open(char *fname, const char *file_mode, int *ntasks, int *nfiles, int64_t **chunksizes, int32_t *fsblksize, int **globalranks, FILE **fileptr)
Open a sion file in serial mode.
Definition: sion_serial.c:26