1*064bab1dSCameron Smith #include "phIO.h" 2*064bab1dSCameron Smith #include "phio_base.h" 3*064bab1dSCameron Smith #include "phio_sync.h" 4*064bab1dSCameron Smith #include "phComm.h" 5*064bab1dSCameron Smith #include <stdio.h> 6*064bab1dSCameron Smith #include <stdlib.h> 7*064bab1dSCameron Smith #include <string.h> 8*064bab1dSCameron Smith #include <assert.h> 9*064bab1dSCameron Smith #include <phastaIO.h> 10*064bab1dSCameron Smith #include <sstream> 11*064bab1dSCameron Smith #include <string> 12*064bab1dSCameron Smith 13*064bab1dSCameron Smith namespace { 14*064bab1dSCameron Smith void appendRank(std::stringstream& ss, const char* phrase) { 15*064bab1dSCameron Smith ss << phrase << "@" << phcomm_rank()+1; 16*064bab1dSCameron Smith } 17*064bab1dSCameron Smith std::string appendSync(const char* phrase) { 18*064bab1dSCameron Smith std::stringstream ss; 19*064bab1dSCameron Smith appendRank(ss,phrase); 20*064bab1dSCameron Smith ss << "?"; 21*064bab1dSCameron Smith return ss.str(); 22*064bab1dSCameron Smith } 23*064bab1dSCameron Smith std::string appendSyncWrite(const char* phrase) { 24*064bab1dSCameron Smith std::stringstream ss; 25*064bab1dSCameron Smith appendRank(ss,phrase); 26*064bab1dSCameron Smith return ss.str(); 27*064bab1dSCameron Smith } 28*064bab1dSCameron Smith std::string appendColor(const char* phrase, int numFiles) { 29*064bab1dSCameron Smith const int color = computeColor(phcomm_rank(), phcomm_size(), numFiles); 30*064bab1dSCameron Smith std::stringstream ss; 31*064bab1dSCameron Smith ss << phrase << color+1; 32*064bab1dSCameron Smith return ss.str(); 33*064bab1dSCameron Smith } 34*064bab1dSCameron Smith } 35*064bab1dSCameron Smith 36*064bab1dSCameron Smith static struct phio_ops sync_ops = { 37*064bab1dSCameron Smith &sync_readheader, 38*064bab1dSCameron Smith sync_writeheader, 39*064bab1dSCameron Smith sync_readdatablock, 40*064bab1dSCameron Smith sync_writedatablock, 41*064bab1dSCameron Smith sync_restartname, 42*064bab1dSCameron Smith sync_closefile_read, 43*064bab1dSCameron Smith sync_closefile_write 44*064bab1dSCameron Smith }; 45*064bab1dSCameron Smith 46*064bab1dSCameron Smith void sync_readheader( 47*064bab1dSCameron Smith int* fileDescriptor, 48*064bab1dSCameron Smith const char keyphrase[], 49*064bab1dSCameron Smith void* valueArray, 50*064bab1dSCameron Smith int* nItems, 51*064bab1dSCameron Smith const char datatype[], 52*064bab1dSCameron Smith const char iotype[] ) { 53*064bab1dSCameron Smith std::string syncPhrase = appendSync(keyphrase); 54*064bab1dSCameron Smith readheader(fileDescriptor, syncPhrase.c_str(), 55*064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 56*064bab1dSCameron Smith } 57*064bab1dSCameron Smith 58*064bab1dSCameron Smith void sync_writeheader( 59*064bab1dSCameron Smith const int* fileDescriptor, 60*064bab1dSCameron Smith const char keyphrase[], 61*064bab1dSCameron Smith const void* valueArray, 62*064bab1dSCameron Smith const int* nItems, 63*064bab1dSCameron Smith const int* ndataItems, 64*064bab1dSCameron Smith const char datatype[], 65*064bab1dSCameron Smith const char iotype[] ) { 66*064bab1dSCameron Smith std::string syncPhrase = appendSyncWrite(keyphrase); 67*064bab1dSCameron Smith writeheader(fileDescriptor, syncPhrase.c_str(), 68*064bab1dSCameron Smith valueArray, nItems, ndataItems, datatype, iotype); 69*064bab1dSCameron Smith } 70*064bab1dSCameron Smith 71*064bab1dSCameron Smith 72*064bab1dSCameron Smith void sync_readdatablock( 73*064bab1dSCameron Smith int* fileDescriptor, 74*064bab1dSCameron Smith const char keyphrase[], 75*064bab1dSCameron Smith void* valueArray, 76*064bab1dSCameron Smith int* nItems, 77*064bab1dSCameron Smith const char datatype[], 78*064bab1dSCameron Smith const char iotype[] ) { 79*064bab1dSCameron Smith std::string syncPhrase = appendSync(keyphrase); 80*064bab1dSCameron Smith readdatablock(fileDescriptor, syncPhrase.c_str(), 81*064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 82*064bab1dSCameron Smith } 83*064bab1dSCameron Smith 84*064bab1dSCameron Smith void sync_writedatablock( 85*064bab1dSCameron Smith const int* fileDescriptor, 86*064bab1dSCameron Smith const char keyphrase[], 87*064bab1dSCameron Smith const void* valueArray, 88*064bab1dSCameron Smith const int* nItems, 89*064bab1dSCameron Smith const char datatype[], 90*064bab1dSCameron Smith const char iotype[]) { 91*064bab1dSCameron Smith std::string syncPhrase = appendSyncWrite(keyphrase); 92*064bab1dSCameron Smith writedatablock(fileDescriptor, syncPhrase.c_str(), 93*064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 94*064bab1dSCameron Smith } 95*064bab1dSCameron Smith 96*064bab1dSCameron Smith void sync_openfile_read( 97*064bab1dSCameron Smith const char filename[], 98*064bab1dSCameron Smith int* numFiles, 99*064bab1dSCameron Smith phio_fp* fileDescriptor) { 100*064bab1dSCameron Smith *fileDescriptor = 101*064bab1dSCameron Smith (struct phio_file*) malloc(sizeof(struct phio_file)); 102*064bab1dSCameron Smith (*fileDescriptor)->ops = &sync_ops; 103*064bab1dSCameron Smith (*fileDescriptor)->file = (int*) malloc(sizeof(int*)); 104*064bab1dSCameron Smith std::string syncName = appendColor(filename, *numFiles); 105*064bab1dSCameron Smith int nfields=0; 106*064bab1dSCameron Smith int nppf=0; 107*064bab1dSCameron Smith queryphmpiio(syncName.c_str(), &nfields, &nppf); 108*064bab1dSCameron Smith const char* mode = "read"; 109*064bab1dSCameron Smith initphmpiio(&nfields, &nppf, numFiles, (*fileDescriptor)->file, mode); 110*064bab1dSCameron Smith openfile(syncName.c_str(), mode, (*fileDescriptor)->file); 111*064bab1dSCameron Smith } 112*064bab1dSCameron Smith 113*064bab1dSCameron Smith void sync_openfile_write( 114*064bab1dSCameron Smith const char filename[], 115*064bab1dSCameron Smith int* numFiles, 116*064bab1dSCameron Smith int* numFields, 117*064bab1dSCameron Smith int* numPPF, 118*064bab1dSCameron Smith int* fileDescriptor) { 119*064bab1dSCameron Smith std::string syncName = appendColor(filename, *numFiles); 120*064bab1dSCameron Smith //TODO - define a good upper bound 121*064bab1dSCameron Smith assert(*numFields > 0 && *numFields < 1024); 122*064bab1dSCameron Smith assert(*numPPF > 0 && *numPPF < 1024); 123*064bab1dSCameron Smith const char* mode = "write"; 124*064bab1dSCameron Smith initphmpiio(numFields, numPPF, numFiles, fileDescriptor, mode); 125*064bab1dSCameron Smith openfile(syncName.c_str(), mode, fileDescriptor); 126*064bab1dSCameron Smith } 127*064bab1dSCameron Smith 128*064bab1dSCameron Smith void sync_restartname(int* step, char* filename) { 129*064bab1dSCameron Smith std::stringstream ss; 130*064bab1dSCameron Smith ss << "restart-dat." << *step << '.'; 131*064bab1dSCameron Smith std::string s = ss.str(); 132*064bab1dSCameron Smith strcpy(filename, s.c_str()); 133*064bab1dSCameron Smith } 134*064bab1dSCameron Smith 135*064bab1dSCameron Smith void sync_closefile_read(phio_fp f) { 136*064bab1dSCameron Smith const char* mode = "read"; 137*064bab1dSCameron Smith closefile(f->file, mode); 138*064bab1dSCameron Smith finalizephmpiio(f->file); 139*064bab1dSCameron Smith free(f->file); 140*064bab1dSCameron Smith free(f); 141*064bab1dSCameron Smith } 142*064bab1dSCameron Smith 143*064bab1dSCameron Smith void sync_closefile_write(int* fileDescriptor) { 144*064bab1dSCameron Smith const char* mode = "write"; 145*064bab1dSCameron Smith closefile(fileDescriptor, mode); 146*064bab1dSCameron Smith finalizephmpiio(fileDescriptor); 147*064bab1dSCameron Smith } 148