xref: /phasta/phSolver/common/phio_posix.cc (revision 3005da6c320ed384301240a737eeef833e960ed3)
1064bab1dSCameron Smith #include "phIO.h"
2064bab1dSCameron Smith #include "phio_base.h"
3064bab1dSCameron Smith #include "phio_posix.h"
4064bab1dSCameron Smith #include "phComm.h"
5064bab1dSCameron Smith #include <stdio.h>
6064bab1dSCameron Smith #include <stdlib.h>
7064bab1dSCameron Smith #include <string.h>
8064bab1dSCameron Smith #include <assert.h>
9064bab1dSCameron Smith #include <phastaIO.h>
10064bab1dSCameron Smith #include <sstream>
11064bab1dSCameron Smith #include <string>
12064bab1dSCameron Smith 
13064bab1dSCameron Smith namespace {
14064bab1dSCameron Smith   std::string appendPosix(const char* phrase) {
15064bab1dSCameron Smith     std::stringstream ss;
16064bab1dSCameron Smith     ss << phrase << "?";
17064bab1dSCameron Smith     return ss.str();
18064bab1dSCameron Smith   }
19064bab1dSCameron Smith   std::string appendRank(const char* phrase) {
20064bab1dSCameron Smith     std::stringstream ss;
21064bab1dSCameron Smith     ss << phrase << phcomm_rank()+1;
22064bab1dSCameron Smith     return ss.str();
23064bab1dSCameron Smith   }
2457517afcSCameron Smith   void close(phio_fp f, const char* mode) {
2557517afcSCameron Smith     closefile(f->file, mode);
2657517afcSCameron Smith     free(f->file);
2757517afcSCameron Smith     free(f);
2857517afcSCameron Smith   }
29064bab1dSCameron Smith }
30064bab1dSCameron Smith 
31064bab1dSCameron Smith static phio_ops posix_ops = {
32064bab1dSCameron Smith   posix_readheader,
33064bab1dSCameron Smith   posix_writeheader,
34064bab1dSCameron Smith   posix_readdatablock,
35064bab1dSCameron Smith   posix_writedatablock,
36064bab1dSCameron Smith   posix_restartname,
37064bab1dSCameron Smith   posix_closefile_read,
38064bab1dSCameron Smith   posix_closefile_write
39064bab1dSCameron Smith };
40064bab1dSCameron Smith 
41064bab1dSCameron Smith void posix_readheader(
42064bab1dSCameron Smith     int* fileDescriptor,
43064bab1dSCameron Smith     const  char keyphrase[],
44064bab1dSCameron Smith     void* valueArray,
45064bab1dSCameron Smith     int*  nItems,
46064bab1dSCameron Smith     const char  datatype[],
47064bab1dSCameron Smith     const char  iotype[] ) {
48064bab1dSCameron Smith   std::string posixPhrase = appendPosix(keyphrase);
49064bab1dSCameron Smith   readheader(fileDescriptor, posixPhrase.c_str(),
50064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
51064bab1dSCameron Smith }
52064bab1dSCameron Smith 
53064bab1dSCameron Smith void posix_writeheader(
54064bab1dSCameron Smith       const int* fileDescriptor,
55064bab1dSCameron Smith       const char keyphrase[],
56064bab1dSCameron Smith       const void* valueArray,
57064bab1dSCameron Smith       const int* nItems,
58064bab1dSCameron Smith       const int* ndataItems,
59064bab1dSCameron Smith       const char datatype[],
60064bab1dSCameron Smith       const char iotype[] ) {
61*3005da6cSCameron Smith   std::string posixPhrase = appendPosix(keyphrase);
62*3005da6cSCameron Smith   writeheader(fileDescriptor, posixPhrase.c_str(), valueArray,
6357517afcSCameron Smith       nItems, ndataItems, datatype, iotype);
64064bab1dSCameron Smith }
65064bab1dSCameron Smith 
66064bab1dSCameron Smith 
67064bab1dSCameron Smith void posix_readdatablock(
68064bab1dSCameron Smith     int*  fileDescriptor,
69064bab1dSCameron Smith     const char keyphrase[],
70064bab1dSCameron Smith     void* valueArray,
71064bab1dSCameron Smith     int*  nItems,
72064bab1dSCameron Smith     const char  datatype[],
73064bab1dSCameron Smith     const char  iotype[] ) {
74064bab1dSCameron Smith   std::string posixPhrase = appendPosix(keyphrase);
75064bab1dSCameron Smith   readdatablock(fileDescriptor, posixPhrase.c_str(),
76064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
77064bab1dSCameron Smith }
78064bab1dSCameron Smith 
79064bab1dSCameron Smith void posix_writedatablock(
80064bab1dSCameron Smith     const int* fileDescriptor,
81064bab1dSCameron Smith     const char keyphrase[],
82064bab1dSCameron Smith     const void* valueArray,
83064bab1dSCameron Smith     const int* nItems,
84064bab1dSCameron Smith     const char datatype[],
85064bab1dSCameron Smith     const char iotype[]) {
86*3005da6cSCameron Smith   std::string posixPhrase = appendPosix(keyphrase);
87*3005da6cSCameron Smith   writedatablock(fileDescriptor, posixPhrase.c_str(), valueArray,
8857517afcSCameron Smith       nItems, datatype, iotype);
89064bab1dSCameron Smith }
90064bab1dSCameron Smith 
91064bab1dSCameron Smith void posix_openfile_read(
92064bab1dSCameron Smith     const char filename[],
93064bab1dSCameron Smith     phio_fp* fileDescriptor) {
94064bab1dSCameron Smith   *fileDescriptor =
95064bab1dSCameron Smith     (struct phio_file*) malloc(sizeof(struct phio_file));
96064bab1dSCameron Smith   (*fileDescriptor)->ops = &posix_ops;
97064bab1dSCameron Smith   (*fileDescriptor)->file = (int*) malloc(sizeof(int*));
98064bab1dSCameron Smith   const char* mode = "read";
99064bab1dSCameron Smith   std::string posixName = appendRank(filename);
100064bab1dSCameron Smith   openfile(posixName.c_str(), mode, (*fileDescriptor)->file);
101064bab1dSCameron Smith }
102064bab1dSCameron Smith 
103064bab1dSCameron Smith void posix_openfile_write(
104064bab1dSCameron Smith     const char filename[],
10557517afcSCameron Smith     phio_fp* fileDescriptor) {
10657517afcSCameron Smith   *fileDescriptor =
10757517afcSCameron Smith     (struct phio_file*) malloc(sizeof(struct phio_file));
10857517afcSCameron Smith   (*fileDescriptor)->ops = &posix_ops;
10957517afcSCameron Smith   (*fileDescriptor)->file = (int*) malloc(sizeof(int*));
110064bab1dSCameron Smith   const char* mode = "write";
111064bab1dSCameron Smith   std::string posixName = appendRank(filename);
11257517afcSCameron Smith   openfile(posixName.c_str(), mode, (*fileDescriptor)->file);
113064bab1dSCameron Smith }
114064bab1dSCameron Smith 
115064bab1dSCameron Smith void posix_restartname(int* step, char* filename) {
116064bab1dSCameron Smith   std::stringstream ss;
117064bab1dSCameron Smith   ss << "restart.dat." << *step << '.';
118064bab1dSCameron Smith   std::string s = ss.str();
119064bab1dSCameron Smith   strcpy(filename, s.c_str());
120064bab1dSCameron Smith }
121064bab1dSCameron Smith 
122064bab1dSCameron Smith void posix_closefile_read(phio_fp f) {
12357517afcSCameron Smith   close(f, "read");
124064bab1dSCameron Smith }
125064bab1dSCameron Smith 
12657517afcSCameron Smith void posix_closefile_write(phio_fp f) {
12757517afcSCameron Smith   close(f, "write");
128064bab1dSCameron Smith }
129