xref: /phasta/phSolver/common/phio_posix.cc (revision ceb6e96d119bd50a1dc4b1707143fdca9be570c4)
1 #include "phIO.h"
2 #include "phio_base.h"
3 #include "phio_posix.h"
4 #include "phComm.h"
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <phastaIO.h>
10 #include <sstream>
11 #include <string>
12 
13 namespace {
14   std::string appendPosix(const char* phrase) {
15     std::stringstream ss;
16     ss << phrase << "?";
17     return ss.str();
18   }
19   std::string appendRank(const char* phrase) {
20     std::stringstream ss;
21     ss << phrase << phcomm_rank()+1;
22     return ss.str();
23   }
24 }
25 
26 static phio_ops posix_ops = {
27   posix_readheader,
28   posix_writeheader,
29   posix_readdatablock,
30   posix_writedatablock,
31   posix_restartname,
32   posix_closefile_read,
33   posix_closefile_write
34 };
35 
36 void posix_readheader(
37     int* fileDescriptor,
38     const  char keyphrase[],
39     void* valueArray,
40     int*  nItems,
41     const char  datatype[],
42     const char  iotype[] ) {
43   std::string posixPhrase = appendPosix(keyphrase);
44   readheader(fileDescriptor, posixPhrase.c_str(),
45       valueArray, nItems, datatype, iotype);
46 }
47 
48 void posix_writeheader(
49       const int* fileDescriptor,
50       const char keyphrase[],
51       const void* valueArray,
52       const int* nItems,
53       const int* ndataItems,
54       const char datatype[],
55       const char iotype[] ) {
56   std::string posixPhrase = appendPosix(keyphrase);
57   writeheader(fileDescriptor, posixPhrase.c_str(),
58       valueArray, nItems, ndataItems, datatype, iotype);
59 }
60 
61 
62 void posix_readdatablock(
63     int*  fileDescriptor,
64     const char keyphrase[],
65     void* valueArray,
66     int*  nItems,
67     const char  datatype[],
68     const char  iotype[] ) {
69   std::string posixPhrase = appendPosix(keyphrase);
70   readdatablock(fileDescriptor, posixPhrase.c_str(),
71       valueArray, nItems, datatype, iotype);
72 }
73 
74 void posix_writedatablock(
75     const int* fileDescriptor,
76     const char keyphrase[],
77     const void* valueArray,
78     const int* nItems,
79     const char datatype[],
80     const char iotype[]) {
81   std::string posixPhrase = appendPosix(keyphrase);
82   writedatablock(fileDescriptor, posixPhrase.c_str(),
83       valueArray, nItems, datatype, iotype);
84 }
85 
86 void posix_openfile_read(
87     const char filename[],
88     int* numFiles,
89     phio_fp* fileDescriptor) {
90   *fileDescriptor =
91     (struct phio_file*) malloc(sizeof(struct phio_file));
92   (*fileDescriptor)->ops = &posix_ops;
93   (*fileDescriptor)->file = (int*) malloc(sizeof(int*));
94   const char* mode = "read";
95   std::string posixName = appendRank(filename);
96   openfile(posixName.c_str(), mode, (*fileDescriptor)->file);
97 }
98 
99 void posix_openfile_write(
100     const char filename[],
101     int* numFiles,
102     int* numFields,
103     int* numPPF,
104     int* fileDescriptor) {
105   //TODO - define a good upper bound
106   assert(*numFields > 0 && *numFields < 1024);
107   assert(*numPPF > 0 && *numPPF < 1024);
108   const char* mode = "write";
109   std::string posixName = appendRank(filename);
110   openfile(posixName.c_str(), mode, fileDescriptor);
111 }
112 
113 void posix_restartname(int* step, char* filename) {
114   std::stringstream ss;
115   ss << "restart.dat." << *step << '.';
116   std::string s = ss.str();
117   strcpy(filename, s.c_str());
118 }
119 
120 void posix_closefile_read(phio_fp f) {
121   const char* mode = "read";
122   closefile(f->file, mode);
123   free(f->file);
124   free(f);
125 }
126 
127 void posix_closefile_write(int* fileDescriptor) {
128   const char* mode = "write";
129   closefile(fileDescriptor, mode);
130 }
131