xref: /phasta/phSolver/common/test/phIOwriteReadZeroSz.cc (revision 0d3dac81473124a0f4ed9d0cfde0af6b7ff6f837)
156813794SCameron Smith #include <mpi.h>
256813794SCameron Smith #include <stdio.h>
356813794SCameron Smith #include <stdlib.h>
456813794SCameron Smith #include <unistd.h>
556813794SCameron Smith #include <cassert>
656813794SCameron Smith #include "phIO.h"
756813794SCameron Smith #include "phstream.h" //for makeRStream and makeGRStream
856813794SCameron Smith #include "syncio.h"
956813794SCameron Smith #include "posixio.h"
1056813794SCameron Smith #include "streamio.h"
1156813794SCameron Smith 
1256813794SCameron Smith int main(int argc, char* argv[]) {
1356813794SCameron Smith   MPI_Init(&argc,&argv);
1456813794SCameron Smith   int rank;
1556813794SCameron Smith   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
1656813794SCameron Smith   int size;
1756813794SCameron Smith   MPI_Comm_size(MPI_COMM_WORLD, &size);
1856813794SCameron Smith   if( argc != 2 ) {
1956813794SCameron Smith     fprintf(stderr, "Usage: %s <numSyncFiles>\n",argv[0]);
2056813794SCameron Smith     MPI_Finalize();
2156813794SCameron Smith     return 1;
2256813794SCameron Smith   }
2356813794SCameron Smith   const char* phrase = "number of fishes";
2456813794SCameron Smith   const char* type = "double";
2556813794SCameron Smith   const char* iotype = "binary";
2656813794SCameron Smith   int zero = 0;
2756813794SCameron Smith   int one = 1;
2856813794SCameron Smith   int numFish = 0;
2956813794SCameron Smith   double fishWeight = 1.23;
3056813794SCameron Smith   int nfiles = atoi(argv[1]);
3156813794SCameron Smith   int ppf = size/nfiles;
3256813794SCameron Smith   const char* filename[2] = {"water-dat.", "water.dat."};
3356813794SCameron Smith   rstream rs = makeRStream();
3456813794SCameron Smith   grstream grs = makeGRStream();
3556813794SCameron Smith   phio_fp file[3];
3656813794SCameron Smith   syncio_setup_write(nfiles, one, ppf, &(file[0]));
3756813794SCameron Smith   posixio_setup(&(file[1]), 'w');
3856813794SCameron Smith   streamio_setup_write(&(file[2]), rs);
39*0d3dac81SCameron Smith   for(int i=0; i<3; i++) {
4056813794SCameron Smith     phio_openfile(filename[i], file[i]);
4156813794SCameron Smith     phio_writeheader(file[i], phrase, &zero, &one, &zero, type, iotype);
4256813794SCameron Smith     phio_writedatablock(file[i], phrase, &fishWeight, &zero, type, iotype);
4356813794SCameron Smith     phio_closefile(file[i]);
4456813794SCameron Smith   }
4556813794SCameron Smith   syncio_setup_read(nfiles, &(file[0]));
4656813794SCameron Smith   posixio_setup(&(file[1]), 'r');
4756813794SCameron Smith   streamio_read_r(&(file[2]), rs);
48*0d3dac81SCameron Smith   for(int i=0; i<3; i++) {
4956813794SCameron Smith     phio_openfile(filename[i], file[i]);
5056813794SCameron Smith     phio_readheader(file[i], phrase, &numFish, &one, type, iotype);
5156813794SCameron Smith     assert(!numFish);
5256813794SCameron Smith     phio_readdatablock(file[i], phrase, &fishWeight, &numFish, type, iotype);
5356813794SCameron Smith     assert(fishWeight == 1.23);
5456813794SCameron Smith     phio_closefile(file[i]);
5556813794SCameron Smith   }
5656813794SCameron Smith   MPI_Finalize();
5756813794SCameron Smith   return 0;
5856813794SCameron Smith }
59