xref: /phasta/phSolver/common/phio_sync.cc (revision ab645d527a403248f92b1511f47c35aaadb4947d)
1064bab1dSCameron Smith #include "phIO.h"
2064bab1dSCameron Smith #include "phio_base.h"
3064bab1dSCameron Smith #include "phio_sync.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   void appendRank(std::stringstream& ss, const char* phrase) {
15064bab1dSCameron Smith     ss << phrase << "@" << phcomm_rank()+1;
16064bab1dSCameron Smith   }
17064bab1dSCameron Smith   std::string appendSync(const char* phrase) {
18064bab1dSCameron Smith     std::stringstream ss;
19064bab1dSCameron Smith     appendRank(ss,phrase);
20064bab1dSCameron Smith     ss << "?";
21064bab1dSCameron Smith     return ss.str();
22064bab1dSCameron Smith   }
23064bab1dSCameron Smith   std::string appendSyncWrite(const char* phrase) {
24064bab1dSCameron Smith     std::stringstream ss;
25064bab1dSCameron Smith     appendRank(ss,phrase);
26064bab1dSCameron Smith     return ss.str();
27064bab1dSCameron Smith   }
28064bab1dSCameron Smith   std::string appendColor(const char* phrase, int numFiles) {
29064bab1dSCameron Smith     const int color = computeColor(phcomm_rank(), phcomm_size(), numFiles);
30064bab1dSCameron Smith     std::stringstream ss;
31064bab1dSCameron Smith     ss << phrase << color+1;
32064bab1dSCameron Smith     return ss.str();
33064bab1dSCameron Smith   }
34*ab645d52SCameron Smith   void close(sync_fp f, const char* mode) {
35*ab645d52SCameron Smith     int* file = f->base->file;
36*ab645d52SCameron Smith     closefile(file, mode);
37*ab645d52SCameron Smith     finalizephmpiio(file);
38*ab645d52SCameron Smith     free(file);
3957517afcSCameron Smith     free(f);
4057517afcSCameron Smith   }
41064bab1dSCameron Smith }
42064bab1dSCameron Smith 
43*ab645d52SCameron Smith void sync_openfile_read(
44*ab645d52SCameron Smith     const char filename[],
45*ab645d52SCameron Smith     phio_fp f) {
46*ab645d52SCameron Smith   sync_fp sf = (sync_fp) f;
47*ab645d52SCameron Smith   std::string syncName = appendColor(filename, sf->nfiles);
48*ab645d52SCameron Smith   int nfields=0;
49*ab645d52SCameron Smith   int nppf=0;
50*ab645d52SCameron Smith   queryphmpiio(syncName.c_str(), &nfields, &nppf);
51*ab645d52SCameron Smith   const char* mode = "read";
52*ab645d52SCameron Smith   int* file = sf->base->file;
53*ab645d52SCameron Smith   initphmpiio(&nfields, &nppf, &(sf->nfiles), file, mode);
54*ab645d52SCameron Smith   openfile(syncName.c_str(), mode, file);
55*ab645d52SCameron Smith }
56*ab645d52SCameron Smith 
57*ab645d52SCameron Smith void sync_openfile_write(
58*ab645d52SCameron Smith     const char filename[],
59*ab645d52SCameron Smith     phio_fp f) {
60*ab645d52SCameron Smith   sync_fp sf = (sync_fp) f;
61*ab645d52SCameron Smith   std::string syncName = appendColor(filename, sf->nfiles);
62*ab645d52SCameron Smith   const char* mode = "write";
63*ab645d52SCameron Smith   int* file = sf->base->file;
64*ab645d52SCameron Smith   initphmpiio(&(sf->nfields), &(sf->nppf),
65*ab645d52SCameron Smith       &(sf->nfiles), file, mode);
66*ab645d52SCameron Smith   openfile(syncName.c_str(), mode, file);
67*ab645d52SCameron Smith }
68*ab645d52SCameron Smith 
69*ab645d52SCameron Smith void sync_closefile(phio_fp f) {
70*ab645d52SCameron Smith   sync_fp sf = (sync_fp) f;
71*ab645d52SCameron Smith   const char m = sf->base->mode;
72*ab645d52SCameron Smith   if(m == 'r')
73*ab645d52SCameron Smith     close(sf, "read");
74*ab645d52SCameron Smith   else if(m == 'w')
75*ab645d52SCameron Smith     close(sf, "write");
76*ab645d52SCameron Smith   else {
77*ab645d52SCameron Smith     fprintf(stderr, "ERROR unsupported file mode in %s on line %d"
78*ab645d52SCameron Smith         "... exiting", __FILE__, __LINE__);
79*ab645d52SCameron Smith     exit(EXIT_FAILURE);
80*ab645d52SCameron Smith   }
81*ab645d52SCameron Smith }
82064bab1dSCameron Smith 
83064bab1dSCameron Smith void sync_readheader(
84064bab1dSCameron Smith     int* fileDescriptor,
85064bab1dSCameron Smith     const  char keyphrase[],
86064bab1dSCameron Smith     void* valueArray,
87064bab1dSCameron Smith     int*  nItems,
88064bab1dSCameron Smith     const char  datatype[],
89064bab1dSCameron Smith     const char  iotype[] ) {
90064bab1dSCameron Smith   std::string syncPhrase = appendSync(keyphrase);
91064bab1dSCameron Smith   readheader(fileDescriptor, syncPhrase.c_str(),
92064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
93064bab1dSCameron Smith }
94064bab1dSCameron Smith 
95064bab1dSCameron Smith void sync_writeheader(
96064bab1dSCameron Smith       const int* fileDescriptor,
97064bab1dSCameron Smith       const char keyphrase[],
98064bab1dSCameron Smith       const void* valueArray,
99064bab1dSCameron Smith       const int* nItems,
100064bab1dSCameron Smith       const int* ndataItems,
101064bab1dSCameron Smith       const char datatype[],
102064bab1dSCameron Smith       const char iotype[] ) {
103064bab1dSCameron Smith   std::string syncPhrase = appendSyncWrite(keyphrase);
104064bab1dSCameron Smith   writeheader(fileDescriptor, syncPhrase.c_str(),
105064bab1dSCameron Smith       valueArray, nItems, ndataItems, datatype, iotype);
106064bab1dSCameron Smith }
107064bab1dSCameron Smith 
108064bab1dSCameron Smith void sync_readdatablock(
109064bab1dSCameron Smith     int* fileDescriptor,
110064bab1dSCameron Smith     const char keyphrase[],
111064bab1dSCameron Smith     void* valueArray,
112064bab1dSCameron Smith     int*  nItems,
113064bab1dSCameron Smith     const char  datatype[],
114064bab1dSCameron Smith     const char  iotype[] ) {
115064bab1dSCameron Smith   std::string syncPhrase = appendSync(keyphrase);
116064bab1dSCameron Smith   readdatablock(fileDescriptor, syncPhrase.c_str(),
117064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
118064bab1dSCameron Smith }
119064bab1dSCameron Smith 
120064bab1dSCameron Smith void sync_writedatablock(
121064bab1dSCameron Smith     const int* fileDescriptor,
122064bab1dSCameron Smith     const char keyphrase[],
123064bab1dSCameron Smith     const void* valueArray,
124064bab1dSCameron Smith     const int* nItems,
125064bab1dSCameron Smith     const char datatype[],
126064bab1dSCameron Smith     const char iotype[]) {
127064bab1dSCameron Smith   std::string syncPhrase = appendSyncWrite(keyphrase);
128064bab1dSCameron Smith   writedatablock(fileDescriptor, syncPhrase.c_str(),
129064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
130064bab1dSCameron Smith }
131