SIONlib  1.7.1
Scalable I/O library for parallel access to task-local files
sioncat.c
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 ****************************************************************************/
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 
14 #include "sion.h"
15 #include "sion_debug.h"
16 #include "sion_file.h"
17 
18 #define FILENAME_LENGTH 1024
19 
20 static void usage(char *name);
21 
22 int main(int argc, char **argv)
23 {
24  _sion_fileptr *outfp;
25  char infilename[FILENAME_LENGTH];
26  char outfilename[FILENAME_LENGTH];
27 
28  int i, rank, blk, startrank, endrank, startblk, endblk;
29  char *localbuffer;
30  sion_int64 chunksize = 0;
31  sion_int64 left, bread, bsumread, bwrote;
32 
33  /* options */
34  int verbose = 0;
35  int use_outfile = 0;
36  int tasknum = -1;
37  int blknum = -1;
38 
39  /* for file infomation */
40  int sid, ntasks, nfiles, maxblocks;
41  sion_int32 fsblksize;
42  sion_int64 globalskip;
43  sion_int64 start_of_varheader;
44  sion_int64 *sion_localsizes;
45  sion_int64 *sion_globalranks;
46  sion_int64 *sion_blockcount;
47  sion_int64 *sion_blocksizes;
48  sion_int64 *sioncat_sum_bytes_per_task;
49  sion_int64 sioncat_sum_bytes;
50  sion_int64 sioncat_filesize;
51 
52 
53  /* parse command line */
54  i = 1;
55  if (argc < 2)
56  usage(argv[0]);
57 
58  while (i < argc) {
59  if (argv[i][0] == '-') {
60  switch (argv[i][1]) {
61  case 'o':
62  use_outfile = 1;
63  strcpy(outfilename,argv[++i]);
64  break;
65  case 't':
66  tasknum=atoi(argv[++i]);
67  break;
68  case 'b':
69  blknum=atoi(argv[++i]);
70  break;
71  case 'v':
72  verbose++;
73  break;
74  case 'V':
75  fprintf(stderr, "SIONlib utility %s (Version %d.%dp%d, fileformat version %d)\n", argv[0],
76  SION_MAIN_VERSION,SION_SUB_VERSION,
77  SION_VERSION_PATCHLEVEL,SION_FILEFORMAT_VERSION);
78  exit(1);
79  case 'h':
80  usage(argv[0]);
81  break;
82  default:
83  usage(argv[0]);
84  }
85  }
86  i++;
87  }
88 
89  strcpy(infilename, argv[argc - 1]);
90 
91  if(verbose) fprintf(stderr,"sioncat: filename: %-30s\n", infilename);
92  if(verbose) fprintf(stderr,"sioncat: outfile: %-30s\n", outfilename);
93 
94  sid = sion_open(infilename, "rb,posix", &nfiles, &ntasks, NULL, &fsblksize, NULL, NULL);
95 
96  if(verbose) fprintf(stderr,"sioncat: sid: %d\n", sid);
97  if(verbose) fprintf(stderr,"sioncat: filename: %-30s\n", infilename);
98  if(verbose) fprintf(stderr,"sioncat: number of tasks: %d\n", ntasks);
99  if(verbose) fprintf(stderr,"sioncat: write data of task: %d\n", tasknum);
100  if(verbose) fprintf(stderr,"sioncat: write data of block: %d\n", blknum);
101  if(verbose) fprintf(stderr,"sioncat: current endianness: %s\n", (sion_get_endianness())? "big" : "little");
102  if(verbose) fprintf(stderr,"sioncat: file endianness: %s\n", (sion_get_file_endianness(sid)) ? "big" : "little");
103  if(verbose) fprintf(stderr,"sioncat: fsblksize: %lu bytes (%6.2f MB)\n", (unsigned long) fsblksize, fsblksize / 1024.0 / 1024.0);
104 
105  sion_get_locations(sid, &ntasks, &maxblocks, &globalskip, &start_of_varheader, &sion_localsizes, &sion_globalranks, &sion_blockcount,
106  &sion_blocksizes);
107 
108  if(verbose) fprintf(stderr,"sioncat: max number of chunks: %d\n", maxblocks);
109 
110  /* analysis */
111  sioncat_sum_bytes_per_task = (sion_int64 *) malloc(ntasks * sizeof(sion_int64));
112  for (rank = 0; rank < ntasks; rank++)
113  sioncat_sum_bytes_per_task[rank] = 0;
114  for (rank = 0; rank < ntasks; rank++) {
115  for (blk = 0; blk < maxblocks; blk++) {
116  sioncat_sum_bytes_per_task[rank] += sion_blocksizes[ntasks * blk + rank];
117  }
118  }
119 
120  sioncat_sum_bytes = 0;
121  for (rank = 0; rank < ntasks; rank++)
122  sioncat_sum_bytes += sioncat_sum_bytes_per_task[rank];
123 
124  if(verbose) fprintf(stderr,"sioncat: datasize in file (aggr.): %lld bytes (%6.2f MB)\n", sioncat_sum_bytes, sioncat_sum_bytes / 1024.0 / 1024.0);
125 
126  sioncat_filesize = start_of_varheader + (maxblocks + 1) * rank * sizeof(sion_int64);
127 
128  if(verbose) fprintf(stderr,"sioncat: start_of_varheader: %lld bytes (%6.2f MB)\n", start_of_varheader, start_of_varheader / 1024.0 / 1024.0);
129  if(verbose) fprintf(stderr,"sioncat: size of file: %lld bytes (%6.2f MB)\n", sioncat_filesize, sioncat_filesize / 1024.0 / 1024.0);
130 
131  if (sioncat_filesize > 0) {
132  if(verbose) fprintf(stderr,"sioncat: file usage: %8.6f%%\n", (double) sioncat_sum_bytes / (double) sioncat_filesize * 100.0);
133  }
134  chunksize = 0;
135 
136  for (rank = 0; rank < ntasks; rank++) {
137  if (chunksize<sion_localsizes[rank]) chunksize=sion_localsizes[rank];
138  if (chunksize<sion_blocksizes[rank]) chunksize=sion_blocksizes[rank];
139  }
140  if(verbose) fprintf(stderr,"sioncat: max chunksize: %lld\n", chunksize);
141  localbuffer = (char *) malloc(chunksize * sizeof(char));
142  if (localbuffer == NULL) {
143  fprintf(stderr, "cannot allocate localbuffer of size %lld , aborting ...\n", chunksize * sizeof(char));
144  free(sioncat_sum_bytes_per_task);
145  return (1);
146  }
147 
148 
149  if(use_outfile) {
151  if (outfp == NULL) {
152  fprintf(stderr, "cannot open outfile %s , aborting ...\n", outfilename);
153  free(localbuffer);
154  free(sioncat_sum_bytes_per_task);
155  return (1);
156  }
157  }
158  if(tasknum>=0) {
159  if (tasknum>=ntasks) {
160  fprintf(stderr, "task number %d out of range %d .. %d , aborting ...\n", tasknum,0,ntasks-1);
161  free(localbuffer);
162  free(sioncat_sum_bytes_per_task);
163  return (1);
164  }
165  startrank=tasknum;
166  endrank=tasknum;
167  } else {
168  startrank=0;
169  endrank=ntasks-1;
170  }
171 
172  for (rank = startrank; rank <= endrank; rank++) {
173 
174  if(verbose) fprintf(stderr, "sioncat: processing task: %6d\n", rank);
175 
176  if(blknum>=0) {
177  if (blknum>=sion_blockcount[rank]) {
178  fprintf(stderr, "blk number %d out of range %d .. %d , aborting ...\n", blknum,0,(int) sion_blockcount[rank]-1);
179  free(sioncat_sum_bytes_per_task);
180  return (1);
181  }
182  startblk=blknum;
183  endblk=blknum;
184  } else {
185  startblk=0;
186  endblk=sion_blockcount[rank]-1;
187  }
188 
189  for (blk = startblk; blk <= endblk; blk++) {
190 
191  /* seek position of block */
192  sion_seek(sid, rank, blk, 0);
193  DPRINTFP((1, "sioncat", 0, "after sion_seek sid=%d rank=%d blknum=%d fileposition=%lld\n",
194  sid, rank, blk, sion_get_position(sid)));
195 
196  /* read data from block */
197  left = sion_blocksizes[ntasks * blk + rank];
198  bsumread = 0;
199  while (left > 0) {
200  DPRINTFP((8, "sioncat", 0, "will read %lld bytes localbuffer+%lld\n", left, bsumread));
201  bread = sion_fread(localbuffer + bsumread, 1, left, sid);
202  left -= bread;
203  bsumread += bread;
204  if(verbose) fprintf(stderr,"sioncat: %lld read left=%lld \n", bread, left);
205  }
206 
207  /* write data to outfile */
208  left = sion_blocksizes[ntasks * blk + rank];
209  if(use_outfile) {
210  _sion_file_write(localbuffer, left, outfp);
211  } else {
212  bwrote = fwrite(localbuffer, 1, left, stdout);
213  if (bwrote != left) {
214  fprintf(stderr, "problems writing data of size %d on stdout (rc=%d) , aborting ...\n", (int) left, (int) bwrote);
215  free(sioncat_sum_bytes_per_task);
216  free(localbuffer);
217  return (1);
218  }
219  }
220 
221  }
222 
223 
224  }
225 
226  if(use_outfile) {
227  _sion_file_close(outfp);
228  }
229 
230  free(sioncat_sum_bytes_per_task);
231  free(localbuffer);
232  sion_close(sid);
233 
234  return (0);
235 }
236 
237 void usage(char *name)
238 {
239  fprintf(stderr, "Usage: %s options <sionfn> \n\n", name);
240 
241  fprintf(stderr, "%s <sionfn> extracts data from SIONlib file <sionfn>. With -t the\n", name);
242  fprintf(stderr, "output can be restricted to a single task and with -b to a single\n");
243  fprintf(stderr, "block. %s does not extract any SIONlib meta data.\n\n", name);
244 
245  fprintf(stderr, "Options:\n");
246  fprintf(stderr, " [-v] verbose mode \n");
247  fprintf(stderr, " [-t <tasknum>] write only data of task <tasknum>\n");
248  fprintf(stderr, " [-b <blknum>] write only data of block <blknum>\n");
249  fprintf(stderr, " [-o <outfile>] file data will be written to, if not specified stdout\n");
250  fprintf(stderr, " is used\n");
251  fprintf(stderr, " [-V] show version of SIONlib\n");
252  fprintf(stderr, " [-h] show this help\n");
253  exit(1);
254 }
sion_int64 _sion_file_write(const void *data, sion_int64 bytes, _sion_fileptr *sion_fileptr)
Write data to file.
Definition: sion_file.c:148
_sion_fileptr * _sion_file_open(const char *fname, unsigned int flags, unsigned int addflags)
Create and open a new file for writing.
Definition: sion_file.c:41
int sion_get_endianness(void)
Return endianness.
Definition: sion_tools.c:30
sion_int64 sion_get_position(int sid)
Function that returns the current file position.
Definition: sion_common.c:891
#define SION_FILE_FLAG_WRITE
Definition: sion_file.h:23
int sion_get_file_endianness(int sid)
Returns edianness of data in file sid.
Definition: sion_common.c:255
int sion_close(int sid)
Close a sion file.
Definition: sion_serial.c:113
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
#define SION_FILE_FLAG_ANSI
Definition: sion_file.h:19
#define SION_FILE_FLAG_CREATE
Definition: sion_file.h:22
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
int _sion_file_close(_sion_fileptr *sion_fileptr)
Close file and destroys fileptr structure.
Definition: sion_file.c:118
size_t sion_fread(void *data, size_t size, size_t nitems, int sid)
Read data from sion file.
Definition: sion_common.c:591
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