xref: /phasta/phSolver/common/test/phIOwriteReadZeroSz.cc (revision 3fa9f015e40130b5c3932eefbe408e97bed85814)
1 #include <mpi.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <cassert>
6 #include "phIO.h"
7 #include "phstream.h" //for makeRStream and makeGRStream
8 #include "syncio.h"
9 #include "posixio.h"
10 #include "streamio.h"
11 
12 int main(int argc, char* argv[]) {
13   MPI_Init(&argc,&argv);
14   int rank;
15   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
16   int size;
17   MPI_Comm_size(MPI_COMM_WORLD, &size);
18   if( argc != 2 ) {
19     fprintf(stderr, "Usage: %s <numSyncFiles>\n",argv[0]);
20     MPI_Finalize();
21     return 1;
22   }
23   const char* phrase = "number of fishes";
24   const char* type = "double";
25   const char* iotype = "binary";
26   int zero = 0;
27   int one = 1;
28   int numFish = 0;
29   double fishWeight = 1.23;
30   int nfiles = atoi(argv[1]);
31   int ppf = size/nfiles;
32   const char* filename[2] = {"water-dat.", "water.dat."};
33   rstream rs = makeRStream();
34   phio_fp file[3];
35   const char* modes[3]={"syncio", "posixio", "streamio"};
36   syncio_setup_write(nfiles, one, ppf, &(file[0]));
37   posixio_setup(&(file[1]), 'w');
38   streamio_setup_r(&(file[2]), rs, 'w');
39   for(int i=0; i<3; i++) {
40     if(!rank) fprintf(stderr, "%s\n", modes[i]);
41     phio_initStats();
42     phio_openfile(filename[i], file[i]);
43     phio_writeheader(file[i], phrase, &zero, &one, &zero, type, iotype);
44     phio_writedatablock(file[i], phrase, &fishWeight, &zero, type, iotype);
45     phio_closefile(file[i]);
46     phio_printStats();
47   }
48   syncio_setup_read(nfiles, &(file[0]));
49   posixio_setup(&(file[1]), 'r');
50   streamio_setup_r(&(file[2]), rs, 'r');
51   for(int i=0; i<3; i++) {
52     if(!rank) fprintf(stderr, "%s\n", modes[i]);
53     phio_initStats();
54     phio_openfile(filename[i], file[i]);
55     phio_readheader(file[i], phrase, &numFish, &one, type, iotype);
56     assert(!numFish);
57     phio_readdatablock(file[i], phrase, &fishWeight, &numFish, type, iotype);
58     assert(fishWeight == 1.23);
59     phio_closefile(file[i]);
60     phio_printStats();
61   }
62   MPI_Finalize();
63   return 0;
64 }
65