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*57517afcSCameron Smith void close(phio_fp f, const char* mode) { 35*57517afcSCameron Smith closefile(f->file, mode); 36*57517afcSCameron Smith finalizephmpiio(f->file); 37*57517afcSCameron Smith free(f->file); 38*57517afcSCameron Smith free(f); 39*57517afcSCameron Smith } 40064bab1dSCameron Smith } 41064bab1dSCameron Smith 42064bab1dSCameron Smith static struct phio_ops sync_ops = { 43*57517afcSCameron Smith sync_readheader, 44064bab1dSCameron Smith sync_writeheader, 45064bab1dSCameron Smith sync_readdatablock, 46064bab1dSCameron Smith sync_writedatablock, 47064bab1dSCameron Smith sync_restartname, 48064bab1dSCameron Smith sync_closefile_read, 49064bab1dSCameron Smith sync_closefile_write 50064bab1dSCameron Smith }; 51064bab1dSCameron Smith 52064bab1dSCameron Smith void sync_readheader( 53064bab1dSCameron Smith int* fileDescriptor, 54064bab1dSCameron Smith const char keyphrase[], 55064bab1dSCameron Smith void* valueArray, 56064bab1dSCameron Smith int* nItems, 57064bab1dSCameron Smith const char datatype[], 58064bab1dSCameron Smith const char iotype[] ) { 59064bab1dSCameron Smith std::string syncPhrase = appendSync(keyphrase); 60064bab1dSCameron Smith readheader(fileDescriptor, syncPhrase.c_str(), 61064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 62064bab1dSCameron Smith } 63064bab1dSCameron Smith 64064bab1dSCameron Smith void sync_writeheader( 65064bab1dSCameron Smith const int* fileDescriptor, 66064bab1dSCameron Smith const char keyphrase[], 67064bab1dSCameron Smith const void* valueArray, 68064bab1dSCameron Smith const int* nItems, 69064bab1dSCameron Smith const int* ndataItems, 70064bab1dSCameron Smith const char datatype[], 71064bab1dSCameron Smith const char iotype[] ) { 72064bab1dSCameron Smith std::string syncPhrase = appendSyncWrite(keyphrase); 73064bab1dSCameron Smith writeheader(fileDescriptor, syncPhrase.c_str(), 74064bab1dSCameron Smith valueArray, nItems, ndataItems, datatype, iotype); 75064bab1dSCameron Smith } 76064bab1dSCameron Smith 77064bab1dSCameron Smith 78064bab1dSCameron Smith void sync_readdatablock( 79064bab1dSCameron Smith int* fileDescriptor, 80064bab1dSCameron Smith const char keyphrase[], 81064bab1dSCameron Smith void* valueArray, 82064bab1dSCameron Smith int* nItems, 83064bab1dSCameron Smith const char datatype[], 84064bab1dSCameron Smith const char iotype[] ) { 85064bab1dSCameron Smith std::string syncPhrase = appendSync(keyphrase); 86064bab1dSCameron Smith readdatablock(fileDescriptor, syncPhrase.c_str(), 87064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 88064bab1dSCameron Smith } 89064bab1dSCameron Smith 90064bab1dSCameron Smith void sync_writedatablock( 91064bab1dSCameron Smith const int* fileDescriptor, 92064bab1dSCameron Smith const char keyphrase[], 93064bab1dSCameron Smith const void* valueArray, 94064bab1dSCameron Smith const int* nItems, 95064bab1dSCameron Smith const char datatype[], 96064bab1dSCameron Smith const char iotype[]) { 97064bab1dSCameron Smith std::string syncPhrase = appendSyncWrite(keyphrase); 98064bab1dSCameron Smith writedatablock(fileDescriptor, syncPhrase.c_str(), 99064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 100064bab1dSCameron Smith } 101064bab1dSCameron Smith 102064bab1dSCameron Smith void sync_openfile_read( 103064bab1dSCameron Smith const char filename[], 104064bab1dSCameron Smith int* numFiles, 105064bab1dSCameron Smith phio_fp* fileDescriptor) { 106064bab1dSCameron Smith *fileDescriptor = 107064bab1dSCameron Smith (struct phio_file*) malloc(sizeof(struct phio_file)); 108064bab1dSCameron Smith (*fileDescriptor)->ops = &sync_ops; 109064bab1dSCameron Smith (*fileDescriptor)->file = (int*) malloc(sizeof(int*)); 110064bab1dSCameron Smith std::string syncName = appendColor(filename, *numFiles); 111064bab1dSCameron Smith int nfields=0; 112064bab1dSCameron Smith int nppf=0; 113064bab1dSCameron Smith queryphmpiio(syncName.c_str(), &nfields, &nppf); 114064bab1dSCameron Smith const char* mode = "read"; 115064bab1dSCameron Smith initphmpiio(&nfields, &nppf, numFiles, (*fileDescriptor)->file, mode); 116064bab1dSCameron Smith openfile(syncName.c_str(), mode, (*fileDescriptor)->file); 117064bab1dSCameron Smith } 118064bab1dSCameron Smith 119064bab1dSCameron Smith void sync_openfile_write( 120064bab1dSCameron Smith const char filename[], 121064bab1dSCameron Smith int* numFiles, 122064bab1dSCameron Smith int* numFields, 123064bab1dSCameron Smith int* numPPF, 124*57517afcSCameron Smith phio_fp* fileDescriptor) { 125*57517afcSCameron Smith *fileDescriptor = 126*57517afcSCameron Smith (struct phio_file*) malloc(sizeof(struct phio_file)); 127*57517afcSCameron Smith (*fileDescriptor)->ops = &sync_ops; 128*57517afcSCameron Smith (*fileDescriptor)->file = (int*) malloc(sizeof(int*)); 129064bab1dSCameron Smith std::string syncName = appendColor(filename, *numFiles); 130064bab1dSCameron Smith //TODO - define a good upper bound 131064bab1dSCameron Smith assert(*numFields > 0 && *numFields < 1024); 132064bab1dSCameron Smith assert(*numPPF > 0 && *numPPF < 1024); 133064bab1dSCameron Smith const char* mode = "write"; 134*57517afcSCameron Smith initphmpiio(numFields, numPPF, numFiles, (*fileDescriptor)->file, mode); 135*57517afcSCameron Smith openfile(syncName.c_str(), mode, (*fileDescriptor)->file); 136064bab1dSCameron Smith } 137064bab1dSCameron Smith 138064bab1dSCameron Smith void sync_restartname(int* step, char* filename) { 139064bab1dSCameron Smith std::stringstream ss; 140064bab1dSCameron Smith ss << "restart-dat." << *step << '.'; 141064bab1dSCameron Smith std::string s = ss.str(); 142064bab1dSCameron Smith strcpy(filename, s.c_str()); 143064bab1dSCameron Smith } 144064bab1dSCameron Smith 145064bab1dSCameron Smith void sync_closefile_read(phio_fp f) { 146*57517afcSCameron Smith close(f, "read"); 147064bab1dSCameron Smith } 148064bab1dSCameron Smith 149*57517afcSCameron Smith void sync_closefile_write(phio_fp f) { 150*57517afcSCameron Smith close(f, "write"); 151064bab1dSCameron Smith } 152