[591] | 1 | #include "xios_spl.hpp" |
---|
[300] | 2 | #include "exception.hpp" |
---|
[380] | 3 | #include "log.hpp" |
---|
[300] | 4 | #include "buffer_out.hpp" |
---|
| 5 | #include "buffer_client.hpp" |
---|
[317] | 6 | #include "cxios.hpp" |
---|
[382] | 7 | #include "mpi.hpp" |
---|
[347] | 8 | #include "tracer.hpp" |
---|
[300] | 9 | |
---|
[335] | 10 | namespace xios |
---|
[300] | 11 | { |
---|
[732] | 12 | size_t CClientBuffer::maxRequestSize = 0; |
---|
[509] | 13 | |
---|
[917] | 14 | CClientBuffer::CClientBuffer(MPI_Comm interComm, int serverRank, StdSize bufferSize, StdSize maxBufferedEvents) |
---|
| 15 | : interComm(interComm) |
---|
| 16 | , serverRank(serverRank) |
---|
| 17 | , bufferSize(bufferSize) |
---|
| 18 | , current(0) |
---|
| 19 | , count(0) |
---|
| 20 | , bufferedEvents(0) |
---|
| 21 | , maxBufferedEvents(maxBufferedEvents) |
---|
| 22 | , pending(false) |
---|
[300] | 23 | { |
---|
[732] | 24 | buffer[0] = new char[bufferSize]; // transform it with MPI_ALLOC_MEM later |
---|
| 25 | buffer[1] = new char[bufferSize]; |
---|
| 26 | retBuffer = new CBufferOut(buffer[current], bufferSize); |
---|
[917] | 27 | info(10) << "CClientBuffer: allocated 2 x " << bufferSize << " bytes for server " << serverRank << " with a maximum of " << maxBufferedEvents << " buffered events" << endl; |
---|
[300] | 28 | } |
---|
[509] | 29 | |
---|
[300] | 30 | CClientBuffer::~CClientBuffer() |
---|
| 31 | { |
---|
[732] | 32 | delete [] buffer[0]; |
---|
| 33 | delete [] buffer[1]; |
---|
| 34 | delete retBuffer; |
---|
[300] | 35 | } |
---|
[509] | 36 | |
---|
[300] | 37 | int CClientBuffer::remain(void) |
---|
| 38 | { |
---|
[732] | 39 | return bufferSize - count; |
---|
[300] | 40 | } |
---|
[509] | 41 | |
---|
[300] | 42 | bool CClientBuffer::isBufferFree(int size) |
---|
| 43 | { |
---|
[732] | 44 | if (size > maxRequestSize) maxRequestSize = size; |
---|
[509] | 45 | |
---|
[732] | 46 | if (size > bufferSize) |
---|
| 47 | ERROR("bool CClientBuffer::isBufferFree(int size)", |
---|
| 48 | << "The requested size (" << size << " bytes) is too big to fit the buffer (" << bufferSize << " bytes), please increase the client buffer size." << endl); |
---|
[509] | 49 | |
---|
[917] | 50 | return (size <= remain() && bufferedEvents < maxBufferedEvents); |
---|
[300] | 51 | } |
---|
[509] | 52 | |
---|
| 53 | |
---|
[732] | 54 | CBufferOut* CClientBuffer::getBuffer(int size) |
---|
[300] | 55 | { |
---|
[732] | 56 | if (size <= remain()) |
---|
[300] | 57 | { |
---|
[732] | 58 | retBuffer->realloc(buffer[current] + count, size); |
---|
| 59 | count += size; |
---|
[917] | 60 | bufferedEvents++; |
---|
[732] | 61 | return retBuffer; |
---|
[300] | 62 | } |
---|
| 63 | else |
---|
| 64 | { |
---|
[732] | 65 | ERROR("CBufferOut* CClientBuffer::getBuffer(int size)", |
---|
| 66 | << "Not enough space in buffer, this should not have happened..."); |
---|
| 67 | return NULL; |
---|
[300] | 68 | } |
---|
[509] | 69 | } |
---|
| 70 | |
---|
[300] | 71 | bool CClientBuffer::checkBuffer(void) |
---|
| 72 | { |
---|
[732] | 73 | MPI_Status status; |
---|
| 74 | int flag; |
---|
[509] | 75 | |
---|
[300] | 76 | if (pending) |
---|
| 77 | { |
---|
[732] | 78 | traceOff(); |
---|
[1130] | 79 | MPI_Test(&request, &flag, &status); |
---|
[732] | 80 | traceOn(); |
---|
| 81 | if (flag == true) pending = false; |
---|
[300] | 82 | } |
---|
| 83 | |
---|
| 84 | if (!pending) |
---|
| 85 | { |
---|
[732] | 86 | if (count > 0) |
---|
[300] | 87 | { |
---|
[1130] | 88 | MPI_Issend(buffer[current], count, MPI_CHAR, serverRank, 20, interComm, &request); |
---|
[732] | 89 | pending = true; |
---|
| 90 | if (current == 1) current = 0; |
---|
| 91 | else current = 1; |
---|
| 92 | count = 0; |
---|
[917] | 93 | bufferedEvents = 0; |
---|
[300] | 94 | } |
---|
| 95 | } |
---|
[732] | 96 | |
---|
| 97 | return pending; |
---|
[300] | 98 | } |
---|
[509] | 99 | |
---|
[300] | 100 | bool CClientBuffer::hasPendingRequest(void) |
---|
| 101 | { |
---|
[732] | 102 | return (pending || count > 0); |
---|
[300] | 103 | } |
---|
[509] | 104 | } |
---|