xref: /phasta/phSolver/common/phio_posix.cc (revision 16223cb9c3f88b34f2cb94151b5cf5ffc1aac5e2)
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   void close(phio_fp f, const char* mode) {
25     closefile(f->file, mode);
26     free(f->file);
27     free(f);
28   }
29 }
30 
31 static phio_ops posix_ops = {
32   posix_readheader,
33   posix_writeheader,
34   posix_readdatablock,
35   posix_writedatablock,
36   posix_closefile_read,
37   posix_closefile_write
38 };
39 
40 void posix_readheader(
41     int* fileDescriptor,
42     const  char keyphrase[],
43     void* valueArray,
44     int*  nItems,
45     const char  datatype[],
46     const char  iotype[] ) {
47   std::string posixPhrase = appendPosix(keyphrase);
48   readheader(fileDescriptor, posixPhrase.c_str(),
49       valueArray, nItems, datatype, iotype);
50 }
51 
52 void posix_writeheader(
53       const int* fileDescriptor,
54       const char keyphrase[],
55       const void* valueArray,
56       const int* nItems,
57       const int* ndataItems,
58       const char datatype[],
59       const char iotype[] ) {
60   std::string posixPhrase = appendPosix(keyphrase);
61   writeheader(fileDescriptor, posixPhrase.c_str(), valueArray,
62       nItems, ndataItems, datatype, iotype);
63 }
64 
65 
66 void posix_readdatablock(
67     int*  fileDescriptor,
68     const char keyphrase[],
69     void* valueArray,
70     int*  nItems,
71     const char  datatype[],
72     const char  iotype[] ) {
73   std::string posixPhrase = appendPosix(keyphrase);
74   readdatablock(fileDescriptor, posixPhrase.c_str(),
75       valueArray, nItems, datatype, iotype);
76 }
77 
78 void posix_writedatablock(
79     const int* fileDescriptor,
80     const char keyphrase[],
81     const void* valueArray,
82     const int* nItems,
83     const char datatype[],
84     const char iotype[]) {
85   std::string posixPhrase = appendPosix(keyphrase);
86   writedatablock(fileDescriptor, posixPhrase.c_str(), valueArray,
87       nItems, datatype, iotype);
88 }
89 
90 void posix_openfile_read(
91     const char filename[],
92     phio_fp* fileDescriptor) {
93   *fileDescriptor =
94     (struct phio_file*) malloc(sizeof(struct phio_file));
95   (*fileDescriptor)->ops = &posix_ops;
96   (*fileDescriptor)->file = (int*) malloc(sizeof(int*));
97   const char* mode = "read";
98   std::string posixName = appendRank(filename);
99   openfile(posixName.c_str(), mode, (*fileDescriptor)->file);
100 }
101 
102 void posix_openfile_write(
103     const char filename[],
104     phio_fp* fileDescriptor) {
105   *fileDescriptor =
106     (struct phio_file*) malloc(sizeof(struct phio_file));
107   (*fileDescriptor)->ops = &posix_ops;
108   (*fileDescriptor)->file = (int*) malloc(sizeof(int*));
109   const char* mode = "write";
110   std::string posixName = appendRank(filename);
111   openfile(posixName.c_str(), mode, (*fileDescriptor)->file);
112 }
113 
114 void posix_closefile_read(phio_fp f) {
115   close(f, "read");
116 }
117 
118 void posix_closefile_write(phio_fp f) {
119   close(f, "write");
120 }
121