1 #include "phIO.h" 2 #include "phio_base.h" 3 #include "phio_sync.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 void appendRank(std::stringstream& ss, const char* phrase) { 15 ss << phrase << "@" << phcomm_rank()+1; 16 } 17 std::string appendSync(const char* phrase) { 18 std::stringstream ss; 19 appendRank(ss,phrase); 20 ss << "?"; 21 return ss.str(); 22 } 23 std::string appendSyncWrite(const char* phrase) { 24 std::stringstream ss; 25 appendRank(ss,phrase); 26 return ss.str(); 27 } 28 std::string appendColor(const char* phrase, int numFiles) { 29 const int color = computeColor(phcomm_rank(), phcomm_size(), numFiles); 30 std::stringstream ss; 31 ss << phrase << color+1; 32 return ss.str(); 33 } 34 } 35 36 static struct phio_ops sync_ops = { 37 &sync_readheader, 38 sync_writeheader, 39 sync_readdatablock, 40 sync_writedatablock, 41 sync_restartname, 42 sync_closefile_read, 43 sync_closefile_write 44 }; 45 46 void sync_readheader( 47 int* fileDescriptor, 48 const char keyphrase[], 49 void* valueArray, 50 int* nItems, 51 const char datatype[], 52 const char iotype[] ) { 53 std::string syncPhrase = appendSync(keyphrase); 54 readheader(fileDescriptor, syncPhrase.c_str(), 55 valueArray, nItems, datatype, iotype); 56 } 57 58 void sync_writeheader( 59 const int* fileDescriptor, 60 const char keyphrase[], 61 const void* valueArray, 62 const int* nItems, 63 const int* ndataItems, 64 const char datatype[], 65 const char iotype[] ) { 66 std::string syncPhrase = appendSyncWrite(keyphrase); 67 writeheader(fileDescriptor, syncPhrase.c_str(), 68 valueArray, nItems, ndataItems, datatype, iotype); 69 } 70 71 72 void sync_readdatablock( 73 int* fileDescriptor, 74 const char keyphrase[], 75 void* valueArray, 76 int* nItems, 77 const char datatype[], 78 const char iotype[] ) { 79 std::string syncPhrase = appendSync(keyphrase); 80 readdatablock(fileDescriptor, syncPhrase.c_str(), 81 valueArray, nItems, datatype, iotype); 82 } 83 84 void sync_writedatablock( 85 const int* fileDescriptor, 86 const char keyphrase[], 87 const void* valueArray, 88 const int* nItems, 89 const char datatype[], 90 const char iotype[]) { 91 std::string syncPhrase = appendSyncWrite(keyphrase); 92 writedatablock(fileDescriptor, syncPhrase.c_str(), 93 valueArray, nItems, datatype, iotype); 94 } 95 96 void sync_openfile_read( 97 const char filename[], 98 int* numFiles, 99 phio_fp* fileDescriptor) { 100 *fileDescriptor = 101 (struct phio_file*) malloc(sizeof(struct phio_file)); 102 (*fileDescriptor)->ops = &sync_ops; 103 (*fileDescriptor)->file = (int*) malloc(sizeof(int*)); 104 std::string syncName = appendColor(filename, *numFiles); 105 int nfields=0; 106 int nppf=0; 107 queryphmpiio(syncName.c_str(), &nfields, &nppf); 108 const char* mode = "read"; 109 initphmpiio(&nfields, &nppf, numFiles, (*fileDescriptor)->file, mode); 110 openfile(syncName.c_str(), mode, (*fileDescriptor)->file); 111 } 112 113 void sync_openfile_write( 114 const char filename[], 115 int* numFiles, 116 int* numFields, 117 int* numPPF, 118 int* fileDescriptor) { 119 std::string syncName = appendColor(filename, *numFiles); 120 //TODO - define a good upper bound 121 assert(*numFields > 0 && *numFields < 1024); 122 assert(*numPPF > 0 && *numPPF < 1024); 123 const char* mode = "write"; 124 initphmpiio(numFields, numPPF, numFiles, fileDescriptor, mode); 125 openfile(syncName.c_str(), mode, fileDescriptor); 126 } 127 128 void sync_restartname(int* step, char* filename) { 129 std::stringstream ss; 130 ss << "restart-dat." << *step << '.'; 131 std::string s = ss.str(); 132 strcpy(filename, s.c_str()); 133 } 134 135 void sync_closefile_read(phio_fp f) { 136 const char* mode = "read"; 137 closefile(f->file, mode); 138 finalizephmpiio(f->file); 139 free(f->file); 140 free(f); 141 } 142 143 void sync_closefile_write(int* fileDescriptor) { 144 const char* mode = "write"; 145 closefile(fileDescriptor, mode); 146 finalizephmpiio(fileDescriptor); 147 } 148