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 writeheader(fileDescriptor, keyphrase, valueArray, 62 nItems, ndataItems, datatype, iotype); 63 } 64 65 66 void posix_readdatablock( 67 int* fileDescriptor, 68 const char keyphrase[], 69 void* valueArray, 70 int* nItems, 71 const char datatype[], 72 const char iotype[] ) { 73 std::string posixPhrase = appendPosix(keyphrase); 74 readdatablock(fileDescriptor, posixPhrase.c_str(), 75 valueArray, nItems, datatype, iotype); 76 } 77 78 void posix_writedatablock( 79 const int* fileDescriptor, 80 const char keyphrase[], 81 const void* valueArray, 82 const int* nItems, 83 const char datatype[], 84 const char iotype[]) { 85 writedatablock(fileDescriptor, keyphrase, valueArray, 86 nItems, datatype, iotype); 87 } 88 89 void posix_openfile_read( 90 const char filename[], 91 phio_fp* fileDescriptor) { 92 *fileDescriptor = 93 (struct phio_file*) malloc(sizeof(struct phio_file)); 94 (*fileDescriptor)->ops = &posix_ops; 95 (*fileDescriptor)->file = (int*) malloc(sizeof(int*)); 96 const char* mode = "read"; 97 std::string posixName = appendRank(filename); 98 openfile(posixName.c_str(), mode, (*fileDescriptor)->file); 99 } 100 101 void posix_openfile_write( 102 const char filename[], 103 phio_fp* fileDescriptor) { 104 *fileDescriptor = 105 (struct phio_file*) malloc(sizeof(struct phio_file)); 106 (*fileDescriptor)->ops = &posix_ops; 107 (*fileDescriptor)->file = (int*) malloc(sizeof(int*)); 108 const char* mode = "write"; 109 std::string posixName = appendRank(filename); 110 openfile(posixName.c_str(), mode, (*fileDescriptor)->file); 111 } 112 113 void posix_restartname(int* step, char* filename) { 114 std::stringstream ss; 115 ss << "restart.dat." << *step << '.'; 116 std::string s = ss.str(); 117 strcpy(filename, s.c_str()); 118 } 119 120 void posix_closefile_read(phio_fp f) { 121 close(f, "read"); 122 } 123 124 void posix_closefile_write(phio_fp f) { 125 close(f, "write"); 126 } 127