xref: /phasta/phSolver/common/phIO.cc (revision fa18c5243221299308e719e7cfa85c9f753bb3b4)
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_openfile_read(
72     const char filename[],
73     int* numFiles,
74     int* fileDescriptor) {
75   std::string syncName = appendColor(filename, *numFiles);
76   int nfields=0;
77   int nppf=0;
78   queryphmpiio(syncName.c_str(), &nfields, &nppf);
79   const char* mode = "read";
80   initphmpiio(&nfields, &nppf, numFiles, fileDescriptor, mode);
81   openfile(syncName.c_str(), mode, fileDescriptor);
82 }
83 
84 void phio_openfile_write(
85     const char filename[],
86     int* numFiles,
87     int* numFields,
88     int* numPPF,
89     int* fileDescriptor) {
90   std::string syncName = appendColor(filename, *numFiles);
91   //TODO - define a good upper bound
92   assert(*numFields > 0 && *numFields < 1024);
93   assert(*numPPF > 0 && *numPPF < 1024);
94   const char* mode = "write";
95   initphmpiio(numFields, numPPF, numFiles, fileDescriptor, mode);
96   openfile(syncName.c_str(), mode, fileDescriptor);
97 }
98 
99 void phio_restartname(int* step, char* filename) {
100   std::stringstream ss;
101   ss << "restart-dat." << *step << '.';
102   std::string s = ss.str();
103   strcpy(filename, s.c_str());
104 }
105 
106 void phio_closefile_read(int* fileDescriptor) {
107   const char* mode = "read";
108   closefile(fileDescriptor, mode);
109   finalizephmpiio(fileDescriptor);
110 }
111 
112 void phio_closefile_write(int* fileDescriptor) {
113   const char* mode = "write";
114   closefile(fileDescriptor, mode);
115   finalizephmpiio(fileDescriptor);
116 }
117