xref: /phasta/phSolver/common/test/phIOwriteFields.cc (revision 61be316bf963701941ec3b100ac0f1882761d190)
1 #include <mpi.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <cassert>
6 #include "phiostats.h"
7 #include "phIO.h"
8 #include "phstream.h" //for makeRStream and makeGRStream
9 #include "syncio.h"
10 #include "posixio.h"
11 #include "streamio.h"
12 
13 int main(int argc, char* argv[]) {
14   MPI_Init(&argc,&argv);
15   int rank;
16   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
17   int size;
18   MPI_Comm_size(MPI_COMM_WORLD, &size);
19   if( argc != 2 ) {
20     fprintf(stderr, "Usage: %s <numSyncFiles>\n",argv[0]);
21     MPI_Finalize();
22     return 1;
23   }
24 
25   const char* phrase = "number of fishes";
26   const char* type = "double";
27   const char* iotype = "binary";
28   double blockArray[4] = {2.0,4.0,8.0,16.0};
29   int blockEntries = 4;
30   int one = 1;
31   int nfields = 2;
32   int zero = 0;
33   int numFish = 0;
34   double fishWeight = 1.23;
35   int nfiles = 1;
36   int ppf = size/nfiles;
37   const char* filename[3] = {"water.dat.", "water.","water-dat."};
38   rstream rs = makeRStream();
39      phio_fp file[3];
40   const char* modes[3]={"syncio","posixio","streamio"};
41   fprintf(stderr,"nfiles %d\n", nfiles);
42   fprintf(stderr,"ppf %d\n", ppf);
43   syncio_setup_write(nfiles, nfields, ppf, &(file[0]));
44   posixio_setup(&(file[1]), 'w');
45   streamio_setup_r(&(file[2]), rs, 'w');
46   fprintf(stderr, "%s\n" ,"Outside loop 1.0");
47   for(int i=0; i<3; i++) {
48     fprintf(stderr, "%s\n" ,"Within the i loop");
49     if(!rank) fprintf(stderr, "%s\n", modes[i]);
50     fprintf(stderr, "%s\n" ,"Before phastaio");
51     phastaio_initStats();
52     fprintf(stderr, "Opening files with %s\n", filename[i]);
53     phio_openfile(filename[i], file[i]);
54     char str [50];
55     int n;
56    for (int j = 0; j < 2 ; j++) {
57       fprintf(stderr,"%s\n", "Inside loop");
58       n = sprintf(str, " Number of times %d ", j);
59       assert(n);
60       fprintf(stderr,"str \'%s\'\n", str);
61       fprintf(stderr,"Printing the int zero %d\n", zero);
62       fprintf(stderr,"Printing the int one %d\n", one);
63       fprintf(stderr,"blockentries %d\n",blockEntries);
64       fprintf(stderr,"Printing the const char type %s\n", type);
65       fprintf(stderr,"Printing the const char iotype %s\n", iotype);
66       fprintf(stderr,"Calling writeheader\n");
67       phio_writeheader(file[i], str, &blockEntries, &one, &blockEntries, type, iotype);
68       fprintf(stderr,"Done calling writeheader\n");
69       fprintf(stderr,"Calling writedatablock\n");
70       phio_writedatablock(file[i], str, blockArray, &blockEntries, type, iotype);
71       fprintf(stderr,"Done Calling writedatablock\n");
72     }
73     phio_closefile(file[i]);
74     phastaio_printStats();
75     fprintf(stderr,"Done %s\n", modes[i]);
76   }
77   syncio_setup_read(nfiles, &(file[0]));
78   posixio_setup(&(file[1]), 'r');
79   streamio_setup_r(&(file[2]), rs, 'r');
80   for(int i=0; i<3; i++) {
81     if(!rank) fprintf(stderr, "%s\n", modes[i]);
82     phastaio_initStats();
83     phio_openfile(filename[i], file[i]);
84     //Str was added
85    // const char* str = "Number of times "+ nfiles ;
86    // for (int j = 0; j < nfiles ; j++) {
87     phio_readheader(file[i], phrase, &numFish, &one, type, iotype);
88     //  phio_readheader(file[i], str, &numFish, &one, type, iotype);
89      //Changing argument from file[i] to file[j]
90     assert(!numFish);
91     phio_readdatablock(file[i], phrase, &fishWeight, &numFish, type, iotype);
92     //  phio_readdatablock(file[i], str, &fishWeight, &numFish, type, iotype);
93     assert(fishWeight == 1.23);
94     phio_closefile(file[i]);
95     phastaio_printStats();
96   }
97   MPI_Finalize();
98   return 0;
99 }
100