[591] | 1 | #include "xios_spl.hpp" |
---|
[300] | 2 | #include "exception.hpp" |
---|
| 3 | #include "buffer_server.hpp" |
---|
[2246] | 4 | #include "timer.hpp" |
---|
[2547] | 5 | #include "window_dynamic.hpp" |
---|
[300] | 6 | |
---|
[509] | 7 | |
---|
[335] | 8 | namespace xios |
---|
[300] | 9 | { |
---|
| 10 | |
---|
[2558] | 11 | CServerBuffer::CServerBuffer(int clientRank, vector<CWindowDynamic*>& windows, vector<MPI_Aint>& winAddress, int windowsRank, StdSize buffSize) |
---|
| 12 | : hasWindows(true), clientRank_(clientRank), windows_(windows), windowsRank_(windowsRank), winAddress_(winAddress) |
---|
[300] | 13 | { |
---|
[885] | 14 | size = 3 * buffSize; |
---|
[717] | 15 | first = 0; |
---|
| 16 | current = 1; |
---|
| 17 | end = size; |
---|
[1757] | 18 | used=0 ; |
---|
[2130] | 19 | MPI_Alloc_mem(size, MPI_INFO_NULL, &buffer) ; |
---|
[1757] | 20 | currentWindows=1 ; |
---|
[2547] | 21 | if (windows[0]==nullptr && windows[1]==nullptr) hasWindows=false ; |
---|
[300] | 22 | } |
---|
| 23 | |
---|
| 24 | CServerBuffer::~CServerBuffer() |
---|
| 25 | { |
---|
[2130] | 26 | MPI_Free_mem(buffer) ; |
---|
[300] | 27 | } |
---|
| 28 | |
---|
[1757] | 29 | void CServerBuffer::updateCurrentWindows(void) |
---|
| 30 | { |
---|
| 31 | if (currentWindows==0) currentWindows=1 ; |
---|
| 32 | else currentWindows=0 ; |
---|
| 33 | } |
---|
[509] | 34 | |
---|
[1757] | 35 | |
---|
[300] | 36 | bool CServerBuffer::isBufferFree(size_t count) |
---|
| 37 | { |
---|
| 38 | bool ret ; |
---|
[509] | 39 | |
---|
[300] | 40 | if (count==0) return true ; |
---|
[509] | 41 | |
---|
[300] | 42 | if (current>first) |
---|
| 43 | { |
---|
| 44 | if (current+count<size) |
---|
| 45 | { |
---|
| 46 | ret=true ; |
---|
| 47 | } |
---|
| 48 | else if (current+count==size) |
---|
| 49 | { |
---|
| 50 | if (first>0) |
---|
| 51 | { |
---|
| 52 | ret=true ; |
---|
| 53 | } |
---|
| 54 | else |
---|
| 55 | { |
---|
| 56 | ret=false ; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | else |
---|
| 60 | { |
---|
| 61 | if (count<first) |
---|
| 62 | { |
---|
| 63 | ret=true ; |
---|
| 64 | } |
---|
| 65 | else |
---|
| 66 | { |
---|
| 67 | ret=false ; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | else |
---|
| 72 | { |
---|
| 73 | if (current+count<first) |
---|
| 74 | { |
---|
| 75 | ret=true ; |
---|
| 76 | } |
---|
| 77 | else |
---|
| 78 | { |
---|
| 79 | ret=false ; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | return ret ; |
---|
| 84 | } |
---|
| 85 | |
---|
[1757] | 86 | bool CServerBuffer::isBufferEmpty(void) |
---|
| 87 | { |
---|
| 88 | if (used==0) return true ; |
---|
| 89 | else return false; |
---|
| 90 | } |
---|
[509] | 91 | |
---|
[300] | 92 | void* CServerBuffer::getBuffer(size_t count) |
---|
| 93 | { |
---|
| 94 | char* ret ; |
---|
[509] | 95 | |
---|
[300] | 96 | if (count==0) return buffer+current ; |
---|
[509] | 97 | |
---|
[300] | 98 | if (current>first) |
---|
| 99 | { |
---|
| 100 | if (current+count<size) |
---|
| 101 | { |
---|
| 102 | ret=buffer+current ; |
---|
| 103 | current+=count ; |
---|
| 104 | } |
---|
| 105 | else if (current+count==size) |
---|
| 106 | { |
---|
| 107 | if (first>0) |
---|
| 108 | { |
---|
| 109 | ret=buffer+current ; |
---|
| 110 | current=0 ; |
---|
| 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
| 114 | ERROR("void* CServerBuffer::getBuffer(size_t count)", |
---|
| 115 | <<"cannot allocate required size in buffer") ; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | else |
---|
| 119 | { |
---|
| 120 | end=current ; |
---|
| 121 | if (count<first) |
---|
| 122 | { |
---|
| 123 | ret=buffer ; |
---|
| 124 | current=count ; |
---|
| 125 | } |
---|
| 126 | else |
---|
| 127 | { |
---|
| 128 | ERROR("void* CServerBuffer::getBuffer(size_t count)", |
---|
| 129 | <<"cannot allocate required size in buffer") ; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | if (current+count<first) |
---|
| 136 | { |
---|
| 137 | ret=buffer+current ; |
---|
| 138 | current+=count ; |
---|
| 139 | } |
---|
| 140 | else |
---|
| 141 | { |
---|
| 142 | ERROR("void* CServerBuffer::getBuffer(size_t count)", |
---|
| 143 | <<"cannot allocate required size in buffer") ; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
[1757] | 147 | used+=count ; |
---|
[300] | 148 | return ret ; |
---|
| 149 | } |
---|
[509] | 150 | |
---|
[300] | 151 | void CServerBuffer::freeBuffer(size_t count) |
---|
| 152 | { |
---|
| 153 | if (count==0) return ; |
---|
| 154 | |
---|
[509] | 155 | if (first==end-1) |
---|
[300] | 156 | { |
---|
| 157 | first=0 ; |
---|
| 158 | count-- ; |
---|
[2309] | 159 | used-- ; |
---|
[300] | 160 | end=size ; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | if (first<=current) |
---|
| 164 | { |
---|
[509] | 165 | if (first+count <current) |
---|
[300] | 166 | { |
---|
| 167 | first+=count ; |
---|
| 168 | } |
---|
| 169 | else |
---|
| 170 | { |
---|
| 171 | ERROR("void CServerBuffer::freeBuffer(size_t count)", |
---|
| 172 | <<"cannot free required size in buffer") ; |
---|
| 173 | } |
---|
[509] | 174 | |
---|
[300] | 175 | } |
---|
| 176 | else |
---|
| 177 | { |
---|
| 178 | if (first+count<end) |
---|
| 179 | { |
---|
| 180 | first+=count ; |
---|
| 181 | } |
---|
| 182 | else |
---|
| 183 | { |
---|
| 184 | ERROR("void CServerBuffer::freeBuffer(size_t count)", |
---|
| 185 | <<"cannot free required size in buffer") ; |
---|
| 186 | } |
---|
| 187 | } |
---|
[1757] | 188 | used-=count ; |
---|
[509] | 189 | } |
---|
| 190 | |
---|
[2309] | 191 | void CServerBuffer::popBuffer(size_t count) |
---|
| 192 | { |
---|
| 193 | if (count==0) return ; |
---|
| 194 | |
---|
| 195 | if (current==0) |
---|
| 196 | { |
---|
| 197 | current = end ; |
---|
| 198 | end=size ; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | if (first<=current) |
---|
| 203 | { |
---|
| 204 | if (current-count >first) |
---|
| 205 | { |
---|
| 206 | current-=count ; |
---|
| 207 | } |
---|
| 208 | else |
---|
| 209 | { |
---|
| 210 | ERROR("void CServerBuffer::popBuffer(size_t count)", |
---|
| 211 | <<"cannot pop required size in buffer") ; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | } |
---|
| 215 | else |
---|
| 216 | { |
---|
| 217 | if (current-count>=0) |
---|
| 218 | { |
---|
| 219 | current-=count ; |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | { |
---|
| 223 | ERROR("void CServerBuffer::freeBuffer(size_t count)", |
---|
| 224 | <<"cannot pop required size in buffer") ; |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | used-=count ; |
---|
| 228 | } |
---|
| 229 | |
---|
[1757] | 230 | bool CServerBuffer::getBufferFromClient(size_t timeLine, char*& buffer, size_t& count) |
---|
| 231 | { |
---|
[2246] | 232 | count = -1 ; |
---|
[2130] | 233 | if (!hasWindows || resizingBuffer_) return false ; |
---|
[2246] | 234 | double time=MPI_Wtime() ; |
---|
| 235 | if (time-bufferFromClientTime_ < bufferFromClientLatency_ ) return false; |
---|
| 236 | bufferFromClientTime_ = time ; |
---|
| 237 | CTimer::get("getBufferFromClient").resume() ; |
---|
| 238 | size_t clientTimeline ; |
---|
| 239 | size_t clientCount ; |
---|
[1757] | 240 | bool ok=false ; |
---|
| 241 | |
---|
[2558] | 242 | info(100)<<"getBufferFromClient : check data in client buffer : clientRank "<<clientRank_<<" timeline "<<timeLine<<" ??"<< endl ; |
---|
[1757] | 243 | lockBuffer(); |
---|
[2246] | 244 | CTimer::get("getBufferFromClient_locked").resume() ; |
---|
[1757] | 245 | // lock is acquired |
---|
| 246 | |
---|
[2547] | 247 | windows_[currentWindows]->get(&clientTimeline, 1, MPI_LONG_LONG_INT, windowsRank_ , MPI_Aint_add(winAddress_[currentWindows],timeLineOffset_*sizeof(size_t)), 1, MPI_LONG_LONG_INT) ; |
---|
| 248 | windows_[currentWindows]->get(&clientCount, 1, MPI_LONG_LONG_INT, windowsRank_ , MPI_Aint_add(winAddress_[currentWindows],countOffset_*sizeof(size_t)), 1, MPI_LONG_LONG_INT) ; |
---|
| 249 | windows_[currentWindows]->flush(windowsRank_) ; |
---|
| 250 | |
---|
[1757] | 251 | if (timeLine==clientTimeline) |
---|
| 252 | { |
---|
| 253 | buffer=(char*)getBuffer(clientCount) ; |
---|
| 254 | count=clientCount ; |
---|
[2547] | 255 | windows_[currentWindows]->get(buffer, clientCount, MPI_CHAR, windowsRank_, MPI_Aint_add(winAddress_[currentWindows],4*sizeof(size_t)) , clientCount, MPI_CHAR) ; |
---|
| 256 | |
---|
[1757] | 257 | clientTimeline = 0 ; |
---|
| 258 | clientCount = 0 ; |
---|
[2547] | 259 | windows_[currentWindows]->put(&clientTimeline, 1, MPI_LONG_LONG_INT, windowsRank_ , MPI_Aint_add(winAddress_[currentWindows],timeLineOffset_*sizeof(size_t)), 1, MPI_LONG_LONG_INT) ; |
---|
| 260 | windows_[currentWindows]->put(&clientCount, 1, MPI_LONG_LONG_INT, windowsRank_ , MPI_Aint_add(winAddress_[currentWindows],countOffset_*sizeof(size_t)), 1, MPI_LONG_LONG_INT) ; |
---|
[1757] | 261 | |
---|
| 262 | // release lock |
---|
[2246] | 263 | CTimer::get("getBufferFromClient_locked").suspend() ; |
---|
| 264 | unlockBuffer() ; |
---|
[1757] | 265 | |
---|
| 266 | ok=true ; |
---|
| 267 | char checksum=0 ; |
---|
| 268 | for(size_t i=0;i<count;i++) checksum=checksum+buffer[i] ; |
---|
| 269 | char checksumFirst=0 ; |
---|
| 270 | for(size_t i=5; i<10 && i<count ;i++) checksumFirst=checksumFirst+buffer[i] ; |
---|
| 271 | char checksumLast=0 ; |
---|
| 272 | for(size_t i=(count<10)?0:count-10; i<count ; i++) checksumLast=checksumLast+buffer[i] ; |
---|
| 273 | |
---|
[2558] | 274 | info(40)<<"getBufferFromClient timeLine==clientTimeLine: clientRank "<<clientRank_<<" timeline "<<timeLine<<" clientTimeline " |
---|
[1757] | 275 | <<clientTimeline<<" clientCount "<<count<<" checksum "<<(int)checksum<<" " |
---|
| 276 | <<(int)buffer[0]<<" "<<(int)buffer[1]<<" "<<(int)buffer[2]<<" "<<(int)buffer[3]<<" "<<(int)buffer[4]<<" "<<(int)buffer[5]<<" " |
---|
| 277 | <<(int)buffer[6]<<" "<<(int)buffer[7]<<" "<<(int)buffer[8]<<" "<<(int)buffer[9]<<" "<<(int)buffer[10]<<" "<<(int)buffer[11]<<endl ; |
---|
| 278 | |
---|
| 279 | } |
---|
| 280 | else |
---|
| 281 | { |
---|
[2246] | 282 | count=0 ; |
---|
[1757] | 283 | |
---|
| 284 | // release lock |
---|
[2246] | 285 | CTimer::get("getBufferFromClient_locked").suspend() ; |
---|
[1757] | 286 | unlockBuffer() ; |
---|
| 287 | } |
---|
[2558] | 288 | CTimer::get("getBufferFromClient").suspend() ; |
---|
| 289 | info(100)<<"getBufferFromClient : check data in client buffer ==> done"<<endl ; |
---|
| 290 | |
---|
[1757] | 291 | if (ok) return true ; |
---|
| 292 | |
---|
| 293 | return false ; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | void CServerBuffer::lockBuffer(void) |
---|
| 297 | { |
---|
| 298 | if (!hasWindows) return ; |
---|
[2547] | 299 | //MPI_Win_lock(MPI_LOCK_EXCLUSIVE,windowsRank_,0,windows_[currentWindows]) ; |
---|
| 300 | windows_[currentWindows]->lockExclusive(windowsRank_) ; |
---|
[1757] | 301 | } |
---|
| 302 | |
---|
| 303 | void CServerBuffer::unlockBuffer(void) |
---|
| 304 | { |
---|
| 305 | if (!hasWindows) return ; |
---|
[2547] | 306 | windows_[currentWindows]->unlockExclusive(windowsRank_) ; |
---|
[1757] | 307 | } |
---|
| 308 | |
---|
| 309 | void CServerBuffer::notifyClientFinalize(void) |
---|
| 310 | { |
---|
| 311 | if (!hasWindows) return ; |
---|
[2246] | 312 | size_t notify=notifyFinalize_ ; |
---|
[1757] | 313 | lockBuffer(); |
---|
| 314 | // lock is acquired |
---|
[2547] | 315 | windows_[currentWindows]->put(¬ify, 1, MPI_LONG_LONG_INT, windowsRank_ , MPI_Aint_add(winAddress_[currentWindows], notifyOffset_*sizeof(size_t)), 1, MPI_LONG_LONG_INT) ; |
---|
[1757] | 316 | unlockBuffer() ; |
---|
| 317 | } |
---|
[2246] | 318 | |
---|
| 319 | void CServerBuffer::notifyBufferResizing(void) |
---|
| 320 | { |
---|
| 321 | resizingBuffer_=true ; |
---|
| 322 | if (!hasWindows) return ; |
---|
| 323 | size_t notify=notifyResizeBuffer_ ; |
---|
| 324 | lockBuffer(); |
---|
| 325 | // lock is acquired |
---|
[2547] | 326 | windows_[currentWindows]->put(¬ify, 1, MPI_LONG_LONG_INT, windowsRank_ , MPI_Aint_add(winAddress_[currentWindows], notifyOffset_*sizeof(size_t)), 1, MPI_LONG_LONG_INT) ; |
---|
[2246] | 327 | unlockBuffer() ; |
---|
| 328 | } |
---|
[300] | 329 | } |
---|