1*064bab1dSCameron Smith #include "phIO.h" 2*064bab1dSCameron Smith #include "phio_base.h" 3*064bab1dSCameron Smith #include "phio_posix.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 std::string appendPosix(const char* phrase) { 15*064bab1dSCameron Smith std::stringstream ss; 16*064bab1dSCameron Smith ss << phrase << "?"; 17*064bab1dSCameron Smith return ss.str(); 18*064bab1dSCameron Smith } 19*064bab1dSCameron Smith std::string appendRank(const char* phrase) { 20*064bab1dSCameron Smith std::stringstream ss; 21*064bab1dSCameron Smith ss << phrase << phcomm_rank()+1; 22*064bab1dSCameron Smith return ss.str(); 23*064bab1dSCameron Smith } 24*064bab1dSCameron Smith } 25*064bab1dSCameron Smith 26*064bab1dSCameron Smith static phio_ops posix_ops = { 27*064bab1dSCameron Smith posix_readheader, 28*064bab1dSCameron Smith posix_writeheader, 29*064bab1dSCameron Smith posix_readdatablock, 30*064bab1dSCameron Smith posix_writedatablock, 31*064bab1dSCameron Smith posix_restartname, 32*064bab1dSCameron Smith posix_closefile_read, 33*064bab1dSCameron Smith posix_closefile_write 34*064bab1dSCameron Smith }; 35*064bab1dSCameron Smith 36*064bab1dSCameron Smith void posix_readheader( 37*064bab1dSCameron Smith int* fileDescriptor, 38*064bab1dSCameron Smith const char keyphrase[], 39*064bab1dSCameron Smith void* valueArray, 40*064bab1dSCameron Smith int* nItems, 41*064bab1dSCameron Smith const char datatype[], 42*064bab1dSCameron Smith const char iotype[] ) { 43*064bab1dSCameron Smith std::string posixPhrase = appendPosix(keyphrase); 44*064bab1dSCameron Smith readheader(fileDescriptor, posixPhrase.c_str(), 45*064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 46*064bab1dSCameron Smith } 47*064bab1dSCameron Smith 48*064bab1dSCameron Smith void posix_writeheader( 49*064bab1dSCameron Smith const int* fileDescriptor, 50*064bab1dSCameron Smith const char keyphrase[], 51*064bab1dSCameron Smith const void* valueArray, 52*064bab1dSCameron Smith const int* nItems, 53*064bab1dSCameron Smith const int* ndataItems, 54*064bab1dSCameron Smith const char datatype[], 55*064bab1dSCameron Smith const char iotype[] ) { 56*064bab1dSCameron Smith std::string posixPhrase = appendPosix(keyphrase); 57*064bab1dSCameron Smith writeheader(fileDescriptor, posixPhrase.c_str(), 58*064bab1dSCameron Smith valueArray, nItems, ndataItems, datatype, iotype); 59*064bab1dSCameron Smith } 60*064bab1dSCameron Smith 61*064bab1dSCameron Smith 62*064bab1dSCameron Smith void posix_readdatablock( 63*064bab1dSCameron Smith int* fileDescriptor, 64*064bab1dSCameron Smith const char keyphrase[], 65*064bab1dSCameron Smith void* valueArray, 66*064bab1dSCameron Smith int* nItems, 67*064bab1dSCameron Smith const char datatype[], 68*064bab1dSCameron Smith const char iotype[] ) { 69*064bab1dSCameron Smith std::string posixPhrase = appendPosix(keyphrase); 70*064bab1dSCameron Smith readdatablock(fileDescriptor, posixPhrase.c_str(), 71*064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 72*064bab1dSCameron Smith } 73*064bab1dSCameron Smith 74*064bab1dSCameron Smith void posix_writedatablock( 75*064bab1dSCameron Smith const int* fileDescriptor, 76*064bab1dSCameron Smith const char keyphrase[], 77*064bab1dSCameron Smith const void* valueArray, 78*064bab1dSCameron Smith const int* nItems, 79*064bab1dSCameron Smith const char datatype[], 80*064bab1dSCameron Smith const char iotype[]) { 81*064bab1dSCameron Smith std::string posixPhrase = appendPosix(keyphrase); 82*064bab1dSCameron Smith writedatablock(fileDescriptor, posixPhrase.c_str(), 83*064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 84*064bab1dSCameron Smith } 85*064bab1dSCameron Smith 86*064bab1dSCameron Smith void posix_openfile_read( 87*064bab1dSCameron Smith const char filename[], 88*064bab1dSCameron Smith int* numFiles, 89*064bab1dSCameron Smith phio_fp* fileDescriptor) { 90*064bab1dSCameron Smith *fileDescriptor = 91*064bab1dSCameron Smith (struct phio_file*) malloc(sizeof(struct phio_file)); 92*064bab1dSCameron Smith (*fileDescriptor)->ops = &posix_ops; 93*064bab1dSCameron Smith (*fileDescriptor)->file = (int*) malloc(sizeof(int*)); 94*064bab1dSCameron Smith const char* mode = "read"; 95*064bab1dSCameron Smith std::string posixName = appendRank(filename); 96*064bab1dSCameron Smith openfile(posixName.c_str(), mode, (*fileDescriptor)->file); 97*064bab1dSCameron Smith } 98*064bab1dSCameron Smith 99*064bab1dSCameron Smith void posix_openfile_write( 100*064bab1dSCameron Smith const char filename[], 101*064bab1dSCameron Smith int* numFiles, 102*064bab1dSCameron Smith int* numFields, 103*064bab1dSCameron Smith int* numPPF, 104*064bab1dSCameron Smith int* fileDescriptor) { 105*064bab1dSCameron Smith //TODO - define a good upper bound 106*064bab1dSCameron Smith assert(*numFields > 0 && *numFields < 1024); 107*064bab1dSCameron Smith assert(*numPPF > 0 && *numPPF < 1024); 108*064bab1dSCameron Smith const char* mode = "write"; 109*064bab1dSCameron Smith std::string posixName = appendRank(filename); 110*064bab1dSCameron Smith openfile(posixName.c_str(), mode, fileDescriptor); 111*064bab1dSCameron Smith } 112*064bab1dSCameron Smith 113*064bab1dSCameron Smith void posix_restartname(int* step, char* filename) { 114*064bab1dSCameron Smith std::stringstream ss; 115*064bab1dSCameron Smith ss << "restart.dat." << *step << '.'; 116*064bab1dSCameron Smith std::string s = ss.str(); 117*064bab1dSCameron Smith strcpy(filename, s.c_str()); 118*064bab1dSCameron Smith } 119*064bab1dSCameron Smith 120*064bab1dSCameron Smith void posix_closefile_read(phio_fp f) { 121*064bab1dSCameron Smith const char* mode = "read"; 122*064bab1dSCameron Smith closefile(f->file, mode); 123*064bab1dSCameron Smith free(f->file); 124*064bab1dSCameron Smith free(f); 125*064bab1dSCameron Smith } 126*064bab1dSCameron Smith 127*064bab1dSCameron Smith void posix_closefile_write(int* fileDescriptor) { 128*064bab1dSCameron Smith const char* mode = "write"; 129*064bab1dSCameron Smith closefile(fileDescriptor, mode); 130*064bab1dSCameron Smith } 131