1 #include "phIO.h" 2 #include "phio_sync.h" 3 #include "phComm.h" 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <assert.h> 8 #include <phastaIO.h> 9 #include <sstream> 10 #include <string> 11 12 namespace { 13 void appendRank(std::stringstream& ss, const char* phrase) { 14 ss << phrase << "@" << phcomm_rank()+1; 15 } 16 std::string appendSync(const char* phrase) { 17 std::stringstream ss; 18 appendRank(ss,phrase); 19 ss << "?"; 20 return ss.str(); 21 } 22 std::string appendSyncWrite(const char* phrase) { 23 std::stringstream ss; 24 appendRank(ss,phrase); 25 return ss.str(); 26 } 27 std::string appendColor(const char* phrase, int numFiles) { 28 const int color = computeColor(phcomm_rank(), phcomm_size(), numFiles); 29 std::stringstream ss; 30 ss << phrase << color+1; 31 return ss.str(); 32 } 33 void close(sync_fp f, const char* mode) { 34 int* file = f->file; 35 closefile(file, mode); 36 finalizephmpiio(file); 37 free(file); 38 free(f); 39 } 40 } 41 42 void sync_openfile_read( 43 const char filename[], 44 phio_fp f) { 45 sync_fp sf = (sync_fp) f; 46 std::string syncName = appendColor(filename, sf->nfiles); 47 int nfields=0; 48 int nppf=0; 49 queryphmpiio(syncName.c_str(), &nfields, &nppf); 50 const char* mode = "read"; 51 int* file = sf->file; 52 initphmpiio(&nfields, &nppf, &(sf->nfiles), file, mode); 53 openfile(syncName.c_str(), mode, file); 54 } 55 56 void sync_openfile_write( 57 const char filename[], 58 phio_fp f) { 59 sync_fp sf = (sync_fp) f; 60 std::string syncName = appendColor(filename, sf->nfiles); 61 const char* mode = "write"; 62 int* file = sf->file; 63 initphmpiio(&(sf->nfields), &(sf->nppf), 64 &(sf->nfiles), file, mode); 65 openfile(syncName.c_str(), mode, file); 66 } 67 68 void sync_closefile(phio_fp f) { 69 sync_fp sf = (sync_fp) f; 70 const char m = sf->mode; 71 if(m == 'r') 72 close(sf, "read"); 73 else if(m == 'w') 74 close(sf, "write"); 75 else { 76 fprintf(stderr, "ERROR unsupported file mode in %s on line %d" 77 "... exiting", __FILE__, __LINE__); 78 exit(EXIT_FAILURE); 79 } 80 } 81 82 void sync_readheader( 83 int* fileDescriptor, 84 const char keyphrase[], 85 void* valueArray, 86 int* nItems, 87 const char datatype[], 88 const char iotype[] ) { 89 std::string syncPhrase = appendSync(keyphrase); 90 readheader(fileDescriptor, syncPhrase.c_str(), 91 valueArray, nItems, datatype, iotype); 92 } 93 94 void sync_writeheader( 95 const int* fileDescriptor, 96 const char keyphrase[], 97 const void* valueArray, 98 const int* nItems, 99 const int* ndataItems, 100 const char datatype[], 101 const char iotype[] ) { 102 std::string syncPhrase = appendSyncWrite(keyphrase); 103 writeheader(fileDescriptor, syncPhrase.c_str(), 104 valueArray, nItems, ndataItems, datatype, iotype); 105 } 106 107 void sync_readdatablock( 108 int* fileDescriptor, 109 const char keyphrase[], 110 void* valueArray, 111 int* nItems, 112 const char datatype[], 113 const char iotype[] ) { 114 std::string syncPhrase = appendSync(keyphrase); 115 readdatablock(fileDescriptor, syncPhrase.c_str(), 116 valueArray, nItems, datatype, iotype); 117 } 118 119 void sync_writedatablock( 120 const int* fileDescriptor, 121 const char keyphrase[], 122 const void* valueArray, 123 const int* nItems, 124 const char datatype[], 125 const char iotype[]) { 126 std::string syncPhrase = appendSyncWrite(keyphrase); 127 writedatablock(fileDescriptor, syncPhrase.c_str(), 128 valueArray, nItems, datatype, iotype); 129 } 130 131 void sync_constructname( 132 const char* in, 133 char* out) { 134 std::string fullname(in); 135 fullname.append("-dat."); 136 sprintf(out, "%s", fullname.c_str()); 137 } 138