1064bab1dSCameron Smith #include "phIO.h" 2064bab1dSCameron Smith #include "phio_base.h" 3064bab1dSCameron Smith #include "phio_posix.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 std::string appendPosix(const char* phrase) { 15064bab1dSCameron Smith std::stringstream ss; 16064bab1dSCameron Smith ss << phrase << "?"; 17064bab1dSCameron Smith return ss.str(); 18064bab1dSCameron Smith } 19064bab1dSCameron Smith std::string appendRank(const char* phrase) { 20064bab1dSCameron Smith std::stringstream ss; 21064bab1dSCameron Smith ss << phrase << phcomm_rank()+1; 22064bab1dSCameron Smith return ss.str(); 23064bab1dSCameron Smith } 24*57517afcSCameron Smith void close(phio_fp f, const char* mode) { 25*57517afcSCameron Smith closefile(f->file, mode); 26*57517afcSCameron Smith free(f->file); 27*57517afcSCameron Smith free(f); 28*57517afcSCameron Smith } 29064bab1dSCameron Smith } 30064bab1dSCameron Smith 31064bab1dSCameron Smith static phio_ops posix_ops = { 32064bab1dSCameron Smith posix_readheader, 33064bab1dSCameron Smith posix_writeheader, 34064bab1dSCameron Smith posix_readdatablock, 35064bab1dSCameron Smith posix_writedatablock, 36064bab1dSCameron Smith posix_restartname, 37064bab1dSCameron Smith posix_closefile_read, 38064bab1dSCameron Smith posix_closefile_write 39064bab1dSCameron Smith }; 40064bab1dSCameron Smith 41064bab1dSCameron Smith void posix_readheader( 42064bab1dSCameron Smith int* fileDescriptor, 43064bab1dSCameron Smith const char keyphrase[], 44064bab1dSCameron Smith void* valueArray, 45064bab1dSCameron Smith int* nItems, 46064bab1dSCameron Smith const char datatype[], 47064bab1dSCameron Smith const char iotype[] ) { 48064bab1dSCameron Smith std::string posixPhrase = appendPosix(keyphrase); 49064bab1dSCameron Smith readheader(fileDescriptor, posixPhrase.c_str(), 50064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 51064bab1dSCameron Smith } 52064bab1dSCameron Smith 53064bab1dSCameron Smith void posix_writeheader( 54064bab1dSCameron Smith const int* fileDescriptor, 55064bab1dSCameron Smith const char keyphrase[], 56064bab1dSCameron Smith const void* valueArray, 57064bab1dSCameron Smith const int* nItems, 58064bab1dSCameron Smith const int* ndataItems, 59064bab1dSCameron Smith const char datatype[], 60064bab1dSCameron Smith const char iotype[] ) { 61*57517afcSCameron Smith writeheader(fileDescriptor, keyphrase, valueArray, 62*57517afcSCameron Smith nItems, ndataItems, datatype, iotype); 63064bab1dSCameron Smith } 64064bab1dSCameron Smith 65064bab1dSCameron Smith 66064bab1dSCameron Smith void posix_readdatablock( 67064bab1dSCameron Smith int* fileDescriptor, 68064bab1dSCameron Smith const char keyphrase[], 69064bab1dSCameron Smith void* valueArray, 70064bab1dSCameron Smith int* nItems, 71064bab1dSCameron Smith const char datatype[], 72064bab1dSCameron Smith const char iotype[] ) { 73064bab1dSCameron Smith std::string posixPhrase = appendPosix(keyphrase); 74064bab1dSCameron Smith readdatablock(fileDescriptor, posixPhrase.c_str(), 75064bab1dSCameron Smith valueArray, nItems, datatype, iotype); 76064bab1dSCameron Smith } 77064bab1dSCameron Smith 78064bab1dSCameron Smith void posix_writedatablock( 79064bab1dSCameron Smith const int* fileDescriptor, 80064bab1dSCameron Smith const char keyphrase[], 81064bab1dSCameron Smith const void* valueArray, 82064bab1dSCameron Smith const int* nItems, 83064bab1dSCameron Smith const char datatype[], 84064bab1dSCameron Smith const char iotype[]) { 85*57517afcSCameron Smith writedatablock(fileDescriptor, keyphrase, valueArray, 86*57517afcSCameron Smith nItems, datatype, iotype); 87064bab1dSCameron Smith } 88064bab1dSCameron Smith 89064bab1dSCameron Smith void posix_openfile_read( 90064bab1dSCameron Smith const char filename[], 91064bab1dSCameron Smith phio_fp* fileDescriptor) { 92064bab1dSCameron Smith *fileDescriptor = 93064bab1dSCameron Smith (struct phio_file*) malloc(sizeof(struct phio_file)); 94064bab1dSCameron Smith (*fileDescriptor)->ops = &posix_ops; 95064bab1dSCameron Smith (*fileDescriptor)->file = (int*) malloc(sizeof(int*)); 96064bab1dSCameron Smith const char* mode = "read"; 97064bab1dSCameron Smith std::string posixName = appendRank(filename); 98064bab1dSCameron Smith openfile(posixName.c_str(), mode, (*fileDescriptor)->file); 99064bab1dSCameron Smith } 100064bab1dSCameron Smith 101064bab1dSCameron Smith void posix_openfile_write( 102064bab1dSCameron Smith const char filename[], 103*57517afcSCameron Smith phio_fp* fileDescriptor) { 104*57517afcSCameron Smith *fileDescriptor = 105*57517afcSCameron Smith (struct phio_file*) malloc(sizeof(struct phio_file)); 106*57517afcSCameron Smith (*fileDescriptor)->ops = &posix_ops; 107*57517afcSCameron Smith (*fileDescriptor)->file = (int*) malloc(sizeof(int*)); 108064bab1dSCameron Smith const char* mode = "write"; 109064bab1dSCameron Smith std::string posixName = appendRank(filename); 110*57517afcSCameron Smith openfile(posixName.c_str(), mode, (*fileDescriptor)->file); 111064bab1dSCameron Smith } 112064bab1dSCameron Smith 113064bab1dSCameron Smith void posix_restartname(int* step, char* filename) { 114064bab1dSCameron Smith std::stringstream ss; 115064bab1dSCameron Smith ss << "restart.dat." << *step << '.'; 116064bab1dSCameron Smith std::string s = ss.str(); 117064bab1dSCameron Smith strcpy(filename, s.c_str()); 118064bab1dSCameron Smith } 119064bab1dSCameron Smith 120064bab1dSCameron Smith void posix_closefile_read(phio_fp f) { 121*57517afcSCameron Smith close(f, "read"); 122064bab1dSCameron Smith } 123064bab1dSCameron Smith 124*57517afcSCameron Smith void posix_closefile_write(phio_fp f) { 125*57517afcSCameron Smith close(f, "write"); 126064bab1dSCameron Smith } 127