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