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