xref: /phasta/phSolver/common/phio_sync.cc (revision d7abaf6c7709145d1e6e6b7740bd56c3f238d064)
1 #include "phIO.h"
2 #include "phio_base.h"
3 #include "phio_sync.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   void appendRank(std::stringstream& ss, const char* phrase) {
15     ss << phrase << "@" << phcomm_rank()+1;
16   }
17   std::string appendSync(const char* phrase) {
18     std::stringstream ss;
19     appendRank(ss,phrase);
20     ss << "?";
21     return ss.str();
22   }
23   std::string appendSyncWrite(const char* phrase) {
24     std::stringstream ss;
25     appendRank(ss,phrase);
26     return ss.str();
27   }
28   std::string appendColor(const char* phrase, int numFiles) {
29     const int color = computeColor(phcomm_rank(), phcomm_size(), numFiles);
30     std::stringstream ss;
31     ss << phrase << color+1;
32     return ss.str();
33   }
34   void close(sync_fp f, const char* mode) {
35     int* file = f->base->file;
36     closefile(file, mode);
37     finalizephmpiio(file);
38     free(file);
39     free(f);
40   }
41 }
42 
43 void sync_openfile_read(
44     const char filename[],
45     phio_fp f) {
46   sync_fp sf = (sync_fp) f;
47   std::string syncName = appendColor(filename, sf->nfiles);
48   int nfields=0;
49   int nppf=0;
50   queryphmpiio(syncName.c_str(), &nfields, &nppf);
51   const char* mode = "read";
52   int* file = sf->base->file;
53   initphmpiio(&nfields, &nppf, &(sf->nfiles), file, mode);
54   openfile(syncName.c_str(), mode, file);
55 }
56 
57 void sync_openfile_write(
58     const char filename[],
59     phio_fp f) {
60   sync_fp sf = (sync_fp) f;
61   std::string syncName = appendColor(filename, sf->nfiles);
62   const char* mode = "write";
63   int* file = sf->base->file;
64   initphmpiio(&(sf->nfields), &(sf->nppf),
65       &(sf->nfiles), file, mode);
66   openfile(syncName.c_str(), mode, file);
67 }
68 
69 void sync_closefile(phio_fp f) {
70   sync_fp sf = (sync_fp) f;
71   const char m = sf->base->mode;
72   if(m == 'r')
73     close(sf, "read");
74   else if(m == 'w')
75     close(sf, "write");
76   else {
77     fprintf(stderr, "ERROR unsupported file mode in %s on line %d"
78         "... exiting", __FILE__, __LINE__);
79     exit(EXIT_FAILURE);
80   }
81 }
82 
83 void sync_readheader(
84     int* fileDescriptor,
85     const  char keyphrase[],
86     void* valueArray,
87     int*  nItems,
88     const char  datatype[],
89     const char  iotype[] ) {
90   std::string syncPhrase = appendSync(keyphrase);
91   readheader(fileDescriptor, syncPhrase.c_str(),
92       valueArray, nItems, datatype, iotype);
93 }
94 
95 void sync_writeheader(
96       const int* fileDescriptor,
97       const char keyphrase[],
98       const void* valueArray,
99       const int* nItems,
100       const int* ndataItems,
101       const char datatype[],
102       const char iotype[] ) {
103   std::string syncPhrase = appendSyncWrite(keyphrase);
104   writeheader(fileDescriptor, syncPhrase.c_str(),
105       valueArray, nItems, ndataItems, datatype, iotype);
106 }
107 
108 void sync_readdatablock(
109     int* fileDescriptor,
110     const char keyphrase[],
111     void* valueArray,
112     int*  nItems,
113     const char  datatype[],
114     const char  iotype[] ) {
115   std::string syncPhrase = appendSync(keyphrase);
116   readdatablock(fileDescriptor, syncPhrase.c_str(),
117       valueArray, nItems, datatype, iotype);
118 }
119 
120 void sync_writedatablock(
121     const int* fileDescriptor,
122     const char keyphrase[],
123     const void* valueArray,
124     const int* nItems,
125     const char datatype[],
126     const char iotype[]) {
127   std::string syncPhrase = appendSyncWrite(keyphrase);
128   writedatablock(fileDescriptor, syncPhrase.c_str(),
129       valueArray, nItems, datatype, iotype);
130 }
131