xref: /phasta/phSolver/common/phIO.cc (revision 0bfd29162c763803e760e1c0b5b3d9fbb074d255)
1 #include "phIO.h"
2 #include "phComm.h"
3 #include <stdio.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <phastaIO.h>
7 #include <sstream>
8 #include <string>
9 
10 namespace {
11   void appendRank(std::stringstream& ss, const char* phrase) {
12     ss << phrase << "@" << phcomm_rank()+1;
13   }
14   std::string appendSync(const char* phrase) {
15     std::stringstream ss;
16     appendRank(ss,phrase);
17     ss << "?";
18     return ss.str();
19   }
20   std::string appendSyncWrite(const char* phrase) {
21     std::stringstream ss;
22     appendRank(ss,phrase);
23     return ss.str();
24   }
25   std::string appendColor(const char* phrase, int numFiles) {
26     const int color = computeColor(phcomm_rank(), phcomm_size(), numFiles);
27     std::stringstream ss;
28     ss << phrase << color+1;
29     return ss.str();
30   }
31 }
32 
33 void phio_readheader(
34     int* fileDescriptor,
35     const  char keyphrase[],
36     void* valueArray,
37     int*  nItems,
38     const char  datatype[],
39     const char  iotype[] ) {
40   std::string syncPhrase = appendSync(keyphrase);
41   readheader(fileDescriptor, syncPhrase.c_str(),
42       valueArray, nItems, datatype, iotype);
43 }
44 
45 void phio_writeheader(
46       const int* fileDescriptor,
47       const char keyphrase[],
48       const void* valueArray,
49       const int* nItems,
50       const int* ndataItems,
51       const char datatype[],
52       const char iotype[] ) {
53   std::string syncPhrase = appendSyncWrite(keyphrase);
54   writeheader(fileDescriptor, syncPhrase.c_str(),
55       valueArray, nItems, ndataItems, datatype, iotype);
56 }
57 
58 
59 void phio_readdatablock(
60     int*  fileDescriptor,
61     const char keyphrase[],
62     void* valueArray,
63     int*  nItems,
64     const char  datatype[],
65     const char  iotype[] ) {
66   std::string syncPhrase = appendSync(keyphrase);
67   readdatablock(fileDescriptor, syncPhrase.c_str(),
68       valueArray, nItems, datatype, iotype);
69 }
70 
71 void phio_writedatablock(
72     const int* fileDescriptor,
73     const char keyphrase[],
74     const void* valueArray,
75     const int* nItems,
76     const char datatype[],
77     const char iotype[]) {
78   std::string syncPhrase = appendSyncWrite(keyphrase);
79   writedatablock(fileDescriptor, syncPhrase.c_str(),
80       valueArray, nItems, datatype, iotype);
81 }
82 
83 void phio_openfile_read(
84     const char filename[],
85     int* numFiles,
86     int* fileDescriptor) {
87   std::string syncName = appendColor(filename, *numFiles);
88   int nfields=0;
89   int nppf=0;
90   queryphmpiio(syncName.c_str(), &nfields, &nppf);
91   const char* mode = "read";
92   initphmpiio(&nfields, &nppf, numFiles, fileDescriptor, mode);
93   openfile(syncName.c_str(), mode, fileDescriptor);
94 }
95 
96 void phio_openfile_write(
97     const char filename[],
98     int* numFiles,
99     int* numFields,
100     int* numPPF,
101     int* fileDescriptor) {
102   std::string syncName = appendColor(filename, *numFiles);
103   //TODO - define a good upper bound
104   assert(*numFields > 0 && *numFields < 1024);
105   assert(*numPPF > 0 && *numPPF < 1024);
106   const char* mode = "write";
107   initphmpiio(numFields, numPPF, numFiles, fileDescriptor, mode);
108   openfile(syncName.c_str(), mode, fileDescriptor);
109 }
110 
111 void phio_restartname(int* step, char* filename) {
112   std::stringstream ss;
113   ss << "restart-dat." << *step << '.';
114   std::string s = ss.str();
115   strcpy(filename, s.c_str());
116 }
117 
118 void phio_closefile_read(int* fileDescriptor) {
119   const char* mode = "read";
120   closefile(fileDescriptor, mode);
121   finalizephmpiio(fileDescriptor);
122 }
123 
124 void phio_closefile_write(int* fileDescriptor) {
125   const char* mode = "write";
126   closefile(fileDescriptor, mode);
127   finalizephmpiio(fileDescriptor);
128 }
129