1064bab1dSCameron Smith #include "phIO.h"
2064bab1dSCameron Smith #include "phio_sync.h"
3064bab1dSCameron Smith #include "phComm.h"
4064bab1dSCameron Smith #include <stdio.h>
5064bab1dSCameron Smith #include <stdlib.h>
6064bab1dSCameron Smith #include <string.h>
7064bab1dSCameron Smith #include <assert.h>
8064bab1dSCameron Smith #include <phastaIO.h>
9064bab1dSCameron Smith #include <sstream>
10064bab1dSCameron Smith #include <string>
11064bab1dSCameron Smith
12064bab1dSCameron Smith namespace {
appendRank(std::stringstream & ss,const char * phrase)13064bab1dSCameron Smith void appendRank(std::stringstream& ss, const char* phrase) {
14064bab1dSCameron Smith ss << phrase << "@" << phcomm_rank()+1;
15064bab1dSCameron Smith }
appendSync(const char * phrase)16064bab1dSCameron Smith std::string appendSync(const char* phrase) {
17064bab1dSCameron Smith std::stringstream ss;
18064bab1dSCameron Smith appendRank(ss,phrase);
19064bab1dSCameron Smith ss << "?";
20064bab1dSCameron Smith return ss.str();
21064bab1dSCameron Smith }
appendSyncWrite(const char * phrase)22064bab1dSCameron Smith std::string appendSyncWrite(const char* phrase) {
23064bab1dSCameron Smith std::stringstream ss;
24064bab1dSCameron Smith appendRank(ss,phrase);
25064bab1dSCameron Smith return ss.str();
26064bab1dSCameron Smith }
appendColor(const char * phrase,int numFiles)27064bab1dSCameron Smith std::string appendColor(const char* phrase, int numFiles) {
28064bab1dSCameron Smith const int color = computeColor(phcomm_rank(), phcomm_size(), numFiles);
29064bab1dSCameron Smith std::stringstream ss;
30064bab1dSCameron Smith ss << phrase << color+1;
31064bab1dSCameron Smith return ss.str();
32064bab1dSCameron Smith }
close(sync_fp f,const char * mode)33ab645d52SCameron Smith void close(sync_fp f, const char* mode) {
349a3ccc9bSCameron Smith int* file = f->file;
35ab645d52SCameron Smith closefile(file, mode);
36ab645d52SCameron Smith finalizephmpiio(file);
37ab645d52SCameron Smith free(file);
3857517afcSCameron Smith free(f);
3957517afcSCameron Smith }
40064bab1dSCameron Smith }
41064bab1dSCameron Smith
sync_openfile_read(const char filename[],phio_fp f)42ab645d52SCameron Smith void sync_openfile_read(
43ab645d52SCameron Smith const char filename[],
44ab645d52SCameron Smith phio_fp f) {
45ab645d52SCameron Smith sync_fp sf = (sync_fp) f;
46ab645d52SCameron Smith std::string syncName = appendColor(filename, sf->nfiles);
47ab645d52SCameron Smith int nfields=0;
48ab645d52SCameron Smith int nppf=0;
49ab645d52SCameron Smith queryphmpiio(syncName.c_str(), &nfields, &nppf);
50ab645d52SCameron Smith const char* mode = "read";
519a3ccc9bSCameron Smith int* file = sf->file;
52ab645d52SCameron Smith initphmpiio(&nfields, &nppf, &(sf->nfiles), file, mode);
53ab645d52SCameron Smith openfile(syncName.c_str(), mode, file);
54ab645d52SCameron Smith }
55ab645d52SCameron Smith
sync_openfile_write(const char filename[],phio_fp f)56ab645d52SCameron Smith void sync_openfile_write(
57ab645d52SCameron Smith const char filename[],
58ab645d52SCameron Smith phio_fp f) {
59ab645d52SCameron Smith sync_fp sf = (sync_fp) f;
60ab645d52SCameron Smith std::string syncName = appendColor(filename, sf->nfiles);
61ab645d52SCameron Smith const char* mode = "write";
629a3ccc9bSCameron Smith int* file = sf->file;
63ab645d52SCameron Smith initphmpiio(&(sf->nfields), &(sf->nppf),
64ab645d52SCameron Smith &(sf->nfiles), file, mode);
65ab645d52SCameron Smith openfile(syncName.c_str(), mode, file);
66ab645d52SCameron Smith }
67ab645d52SCameron Smith
sync_closefile(phio_fp f)68ab645d52SCameron Smith void sync_closefile(phio_fp f) {
69ab645d52SCameron Smith sync_fp sf = (sync_fp) f;
709a3ccc9bSCameron Smith const char m = sf->mode;
71ab645d52SCameron Smith if(m == 'r')
72ab645d52SCameron Smith close(sf, "read");
73ab645d52SCameron Smith else if(m == 'w')
74ab645d52SCameron Smith close(sf, "write");
75ab645d52SCameron Smith else {
76ab645d52SCameron Smith fprintf(stderr, "ERROR unsupported file mode in %s on line %d"
77ab645d52SCameron Smith "... exiting", __FILE__, __LINE__);
78ab645d52SCameron Smith exit(EXIT_FAILURE);
79ab645d52SCameron Smith }
80ab645d52SCameron Smith }
81064bab1dSCameron Smith
sync_readheader(int * fileDescriptor,const char keyphrase[],void * valueArray,int * nItems,const char datatype[],const char iotype[])82064bab1dSCameron Smith void sync_readheader(
83064bab1dSCameron Smith int* fileDescriptor,
84064bab1dSCameron Smith const char keyphrase[],
85064bab1dSCameron Smith void* valueArray,
86064bab1dSCameron Smith int* nItems,
87064bab1dSCameron Smith const char datatype[],
88064bab1dSCameron Smith const char iotype[] ) {
89064bab1dSCameron Smith std::string syncPhrase = appendSync(keyphrase);
90064bab1dSCameron Smith readheader(fileDescriptor, syncPhrase.c_str(),
91064bab1dSCameron Smith valueArray, nItems, datatype, iotype);
92064bab1dSCameron Smith }
93064bab1dSCameron Smith
sync_writeheader(const int * fileDescriptor,const char keyphrase[],const void * valueArray,const int * nItems,const int * ndataItems,const char datatype[],const char iotype[])94064bab1dSCameron Smith void sync_writeheader(
95064bab1dSCameron Smith const int* fileDescriptor,
96064bab1dSCameron Smith const char keyphrase[],
97064bab1dSCameron Smith const void* valueArray,
98064bab1dSCameron Smith const int* nItems,
99064bab1dSCameron Smith const int* ndataItems,
100064bab1dSCameron Smith const char datatype[],
101064bab1dSCameron Smith const char iotype[] ) {
102064bab1dSCameron Smith std::string syncPhrase = appendSyncWrite(keyphrase);
103064bab1dSCameron Smith writeheader(fileDescriptor, syncPhrase.c_str(),
104064bab1dSCameron Smith valueArray, nItems, ndataItems, datatype, iotype);
105064bab1dSCameron Smith }
106064bab1dSCameron Smith
sync_readdatablock(int * fileDescriptor,const char keyphrase[],void * valueArray,int * nItems,const char datatype[],const char iotype[])107064bab1dSCameron Smith void sync_readdatablock(
108064bab1dSCameron Smith int* fileDescriptor,
109064bab1dSCameron Smith const char keyphrase[],
110064bab1dSCameron Smith void* valueArray,
111064bab1dSCameron Smith int* nItems,
112064bab1dSCameron Smith const char datatype[],
113064bab1dSCameron Smith const char iotype[] ) {
114064bab1dSCameron Smith std::string syncPhrase = appendSync(keyphrase);
115064bab1dSCameron Smith readdatablock(fileDescriptor, syncPhrase.c_str(),
116064bab1dSCameron Smith valueArray, nItems, datatype, iotype);
117064bab1dSCameron Smith }
118064bab1dSCameron Smith
sync_writedatablock(const int * fileDescriptor,const char keyphrase[],const void * valueArray,const int * nItems,const char datatype[],const char iotype[])119064bab1dSCameron Smith void sync_writedatablock(
120064bab1dSCameron Smith const int* fileDescriptor,
121064bab1dSCameron Smith const char keyphrase[],
122064bab1dSCameron Smith const void* valueArray,
123064bab1dSCameron Smith const int* nItems,
124064bab1dSCameron Smith const char datatype[],
125064bab1dSCameron Smith const char iotype[]) {
126064bab1dSCameron Smith std::string syncPhrase = appendSyncWrite(keyphrase);
127064bab1dSCameron Smith writedatablock(fileDescriptor, syncPhrase.c_str(),
128064bab1dSCameron Smith valueArray, nItems, datatype, iotype);
129064bab1dSCameron Smith }
130*a93de25bSCameron Smith
sync_constructname(const char * in,char * out)131*a93de25bSCameron Smith void sync_constructname(
132*a93de25bSCameron Smith const char* in,
133*a93de25bSCameron Smith char* out) {
134*a93de25bSCameron Smith std::string fullname(in);
135*a93de25bSCameron Smith fullname.append("-dat.");
136*a93de25bSCameron Smith sprintf(out, "%s", fullname.c_str());
137*a93de25bSCameron Smith }
138