xref: /phasta/phSolver/common/test/phIOwriteReadZeroSz.cc (revision a663ed547a5d6235aa3bcffc8f9fa412c3d80f1f)
156813794SCameron Smith #include <mpi.h>
256813794SCameron Smith #include <stdio.h>
356813794SCameron Smith #include <stdlib.h>
456813794SCameron Smith #include <unistd.h>
556813794SCameron Smith #include <cassert>
6*a663ed54SCameron Smith #include "phiostats.h"
756813794SCameron Smith #include "phIO.h"
856813794SCameron Smith #include "phstream.h" //for makeRStream and makeGRStream
956813794SCameron Smith #include "syncio.h"
1056813794SCameron Smith #include "posixio.h"
1156813794SCameron Smith #include "streamio.h"
1256813794SCameron Smith 
main(int argc,char * argv[])1356813794SCameron Smith int main(int argc, char* argv[]) {
1456813794SCameron Smith   MPI_Init(&argc,&argv);
1556813794SCameron Smith   int rank;
1656813794SCameron Smith   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
1756813794SCameron Smith   int size;
1856813794SCameron Smith   MPI_Comm_size(MPI_COMM_WORLD, &size);
1956813794SCameron Smith   if( argc != 2 ) {
2056813794SCameron Smith     fprintf(stderr, "Usage: %s <numSyncFiles>\n",argv[0]);
2156813794SCameron Smith     MPI_Finalize();
2256813794SCameron Smith     return 1;
2356813794SCameron Smith   }
2456813794SCameron Smith   const char* phrase = "number of fishes";
2556813794SCameron Smith   const char* type = "double";
2656813794SCameron Smith   const char* iotype = "binary";
2756813794SCameron Smith   int zero = 0;
2856813794SCameron Smith   int one = 1;
2956813794SCameron Smith   int numFish = 0;
3056813794SCameron Smith   double fishWeight = 1.23;
3156813794SCameron Smith   int nfiles = atoi(argv[1]);
3256813794SCameron Smith   int ppf = size/nfiles;
3356813794SCameron Smith   const char* filename[2] = {"water-dat.", "water.dat."};
3456813794SCameron Smith   rstream rs = makeRStream();
3556813794SCameron Smith   phio_fp file[3];
363fa9f015SCameron Smith   const char* modes[3]={"syncio", "posixio", "streamio"};
3756813794SCameron Smith   syncio_setup_write(nfiles, one, ppf, &(file[0]));
3856813794SCameron Smith   posixio_setup(&(file[1]), 'w');
3988262674SCameron Smith   streamio_setup_r(&(file[2]), rs, 'w');
400d3dac81SCameron Smith   for(int i=0; i<3; i++) {
413fa9f015SCameron Smith     if(!rank) fprintf(stderr, "%s\n", modes[i]);
42*a663ed54SCameron Smith     phastaio_initStats();
4356813794SCameron Smith     phio_openfile(filename[i], file[i]);
4456813794SCameron Smith     phio_writeheader(file[i], phrase, &zero, &one, &zero, type, iotype);
4556813794SCameron Smith     phio_writedatablock(file[i], phrase, &fishWeight, &zero, type, iotype);
4656813794SCameron Smith     phio_closefile(file[i]);
47*a663ed54SCameron Smith     phastaio_printStats();
4856813794SCameron Smith   }
4956813794SCameron Smith   syncio_setup_read(nfiles, &(file[0]));
5056813794SCameron Smith   posixio_setup(&(file[1]), 'r');
5188262674SCameron Smith   streamio_setup_r(&(file[2]), rs, 'r');
520d3dac81SCameron Smith   for(int i=0; i<3; i++) {
533fa9f015SCameron Smith     if(!rank) fprintf(stderr, "%s\n", modes[i]);
54*a663ed54SCameron Smith     phastaio_initStats();
5556813794SCameron Smith     phio_openfile(filename[i], file[i]);
5656813794SCameron Smith     phio_readheader(file[i], phrase, &numFish, &one, type, iotype);
5756813794SCameron Smith     assert(!numFish);
5856813794SCameron Smith     phio_readdatablock(file[i], phrase, &fishWeight, &numFish, type, iotype);
5956813794SCameron Smith     assert(fishWeight == 1.23);
6056813794SCameron Smith     phio_closefile(file[i]);
61*a663ed54SCameron Smith     phastaio_printStats();
6256813794SCameron Smith   }
6356813794SCameron Smith   MPI_Finalize();
6456813794SCameron Smith   return 0;
6556813794SCameron Smith }
66