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