xref: /phasta/phSolver/common/test/phIOwriteFields.cc (revision 53b6bf36171c1cc704df0bc0f2f65ad9f0daa21a)
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 zero = 0;
32   int numFish = 0;
33   double fishWeight = 1.23;
34   int nfiles = 1;
35   int ppf = size/nfiles;
36   const char* filename[3] = {"water.dat.", "water.","water-dat."};
37   rstream rs = makeRStream();
38      phio_fp file[3];
39   const char* modes[3]={"posixio", "streamio", "syncio"};
40   fprintf(stderr,"nfiles %d\n", nfiles);
41   fprintf(stderr,"ppf %d\n", ppf);
42   posixio_setup(&(file[0]), 'w');
43   streamio_setup_r(&(file[1]), rs, 'w');
44   syncio_setup_write(nfiles, one, ppf, &(file[2]));
45   fprintf(stderr, "%s\n" ,"Outside loop 1.0");
46   for(int i=0; i<3; i++) {
47     fprintf(stderr, "%s\n" ,"Within the i loop");
48     if(!rank) fprintf(stderr, "%s\n", modes[i]);
49     fprintf(stderr, "%s\n" ,"Before phastaio");
50     phastaio_initStats();
51     fprintf(stderr, "Opening files with %s\n", filename[i]);
52     phio_openfile(filename[i], file[i]);
53     char str [50];
54     int n;
55    for (int j = 0; j < 2 ; j++) {
56       fprintf(stderr,"%s\n", "Inside loop");
57       n = sprintf(str, " Number of times %d ", j);
58       assert(n);
59       fprintf(stderr,"str \'%s\'\n", str);
60       fprintf(stderr,"Printing the int zero %d\n", zero);
61       fprintf(stderr,"Printing the int one %d\n", one);
62       fprintf(stderr,"blockentries %d\n",blockEntries);
63       fprintf(stderr,"Printing the const char type %s\n", type);
64       fprintf(stderr,"Printing the const char iotype %s\n", iotype);
65       fprintf(stderr,"Calling writeheader\n");
66       phio_writeheader(file[i], str, &blockEntries, &one, &blockEntries, "integer", iotype);
67       fprintf(stderr,"Done calling writeheader\n");
68       fprintf(stderr,"Calling writedatablock\n");
69       phio_writedatablock(file[i], str, blockArray, &blockEntries, type, iotype);
70       fprintf(stderr,"Done Calling writedatablock\n");
71     }
72     phio_closefile(file[i]);
73     phastaio_printStats();
74     fprintf(stderr,"Done %s\n", modes[i]);
75   }
76   syncio_setup_read(nfiles, &(file[0]));
77   posixio_setup(&(file[1]), 'r');
78   streamio_setup_r(&(file[2]), rs, 'r');
79   for(int i=0; i<3; i++) {
80     if(!rank) fprintf(stderr, "%s\n", modes[i]);
81     phastaio_initStats();
82     phio_openfile(filename[i], file[i]);
83     //Str was added
84    // const char* str = "Number of times "+ nfiles ;
85    // for (int j = 0; j < nfiles ; j++) {
86     phio_readheader(file[i], phrase, &numFish, &one, type, iotype);
87     //  phio_readheader(file[i], str, &numFish, &one, type, iotype);
88      //Changing argument from file[i] to file[j]
89     assert(!numFish);
90     phio_readdatablock(file[i], phrase, &fishWeight, &numFish, type, iotype);
91     //  phio_readdatablock(file[i], str, &fishWeight, &numFish, type, iotype);
92     assert(fishWeight == 1.23);
93     phio_closefile(file[i]);
94     phastaio_printStats();
95   }
96   MPI_Finalize();
97   return 0;
98 }
99