[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 | |
---|
[1328] | 10 | |
---|
| 11 | using namespace ep_lib; |
---|
| 12 | |
---|
[335] | 13 | namespace xios |
---|
[300] | 14 | { |
---|
[732] | 15 | size_t CClientBuffer::maxRequestSize = 0; |
---|
[509] | 16 | |
---|
[1355] | 17 | CClientBuffer::CClientBuffer(MPI_Comm in_interComm, int serverRank, StdSize bufferSize, StdSize estimatedMaxEventSize, StdSize maxBufferedEvents): |
---|
| 18 | //interComm(interComm) |
---|
| 19 | serverRank(serverRank) |
---|
[917] | 20 | , bufferSize(bufferSize) |
---|
[1205] | 21 | , estimatedMaxEventSize(estimatedMaxEventSize) |
---|
| 22 | , maxEventSize(0) |
---|
[917] | 23 | , current(0) |
---|
| 24 | , count(0) |
---|
| 25 | , bufferedEvents(0) |
---|
| 26 | , maxBufferedEvents(maxBufferedEvents) |
---|
| 27 | , pending(false) |
---|
[300] | 28 | { |
---|
[1355] | 29 | interComm = in_interComm; |
---|
[1360] | 30 | *(static_cast< ::MPI_Comm*>(interComm.mpi_comm)) = *(static_cast< ::MPI_Comm*>(in_interComm.mpi_comm)); |
---|
| 31 | *(static_cast< ::MPI_Comm*>(interComm.ep_comm_ptr->intercomm->mpi_inter_comm)) = *(static_cast< ::MPI_Comm*>(in_interComm.ep_comm_ptr->intercomm->mpi_inter_comm)); |
---|
[732] | 32 | buffer[0] = new char[bufferSize]; // transform it with MPI_ALLOC_MEM later |
---|
| 33 | buffer[1] = new char[bufferSize]; |
---|
| 34 | retBuffer = new CBufferOut(buffer[current], bufferSize); |
---|
[1342] | 35 | #pragma omp critical (_output) |
---|
| 36 | info(10) << "CClientBuffer: allocated 2 x " << bufferSize << " bytes for server " << serverRank << " with a maximum of " << maxBufferedEvents << " buffered events" << endl; |
---|
[300] | 37 | } |
---|
[509] | 38 | |
---|
[300] | 39 | CClientBuffer::~CClientBuffer() |
---|
| 40 | { |
---|
[732] | 41 | delete [] buffer[0]; |
---|
| 42 | delete [] buffer[1]; |
---|
| 43 | delete retBuffer; |
---|
[300] | 44 | } |
---|
[509] | 45 | |
---|
[300] | 46 | int CClientBuffer::remain(void) |
---|
| 47 | { |
---|
[732] | 48 | return bufferSize - count; |
---|
[300] | 49 | } |
---|
[509] | 50 | |
---|
[300] | 51 | bool CClientBuffer::isBufferFree(int size) |
---|
| 52 | { |
---|
[732] | 53 | if (size > bufferSize) |
---|
| 54 | ERROR("bool CClientBuffer::isBufferFree(int size)", |
---|
| 55 | << "The requested size (" << size << " bytes) is too big to fit the buffer (" << bufferSize << " bytes), please increase the client buffer size." << endl); |
---|
[509] | 56 | |
---|
[1205] | 57 | if (size > maxEventSize) |
---|
| 58 | { |
---|
| 59 | maxEventSize = size; |
---|
| 60 | |
---|
| 61 | if (size > estimatedMaxEventSize) |
---|
| 62 | error(0) << "WARNING: Unexpected event of size " << size << " for server " << serverRank |
---|
| 63 | << " (estimated max event size = " << estimatedMaxEventSize << ")" << std::endl; |
---|
| 64 | |
---|
| 65 | if (size > maxRequestSize) maxRequestSize = size; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
[917] | 69 | return (size <= remain() && bufferedEvents < maxBufferedEvents); |
---|
[300] | 70 | } |
---|
[509] | 71 | |
---|
| 72 | |
---|
[732] | 73 | CBufferOut* CClientBuffer::getBuffer(int size) |
---|
[300] | 74 | { |
---|
[732] | 75 | if (size <= remain()) |
---|
[300] | 76 | { |
---|
[732] | 77 | retBuffer->realloc(buffer[current] + count, size); |
---|
| 78 | count += size; |
---|
[917] | 79 | bufferedEvents++; |
---|
[732] | 80 | return retBuffer; |
---|
[300] | 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
[732] | 84 | ERROR("CBufferOut* CClientBuffer::getBuffer(int size)", |
---|
| 85 | << "Not enough space in buffer, this should not have happened..."); |
---|
| 86 | return NULL; |
---|
[300] | 87 | } |
---|
[509] | 88 | } |
---|
| 89 | |
---|
[300] | 90 | bool CClientBuffer::checkBuffer(void) |
---|
| 91 | { |
---|
[732] | 92 | MPI_Status status; |
---|
| 93 | int flag; |
---|
[509] | 94 | |
---|
[300] | 95 | if (pending) |
---|
| 96 | { |
---|
[732] | 97 | traceOff(); |
---|
| 98 | MPI_Test(&request, &flag, &status); |
---|
| 99 | traceOn(); |
---|
| 100 | if (flag == true) pending = false; |
---|
[300] | 101 | } |
---|
| 102 | |
---|
| 103 | if (!pending) |
---|
| 104 | { |
---|
[732] | 105 | if (count > 0) |
---|
[300] | 106 | { |
---|
[732] | 107 | MPI_Issend(buffer[current], count, MPI_CHAR, serverRank, 20, interComm, &request); |
---|
| 108 | pending = true; |
---|
| 109 | if (current == 1) current = 0; |
---|
| 110 | else current = 1; |
---|
| 111 | count = 0; |
---|
[917] | 112 | bufferedEvents = 0; |
---|
[300] | 113 | } |
---|
| 114 | } |
---|
[732] | 115 | |
---|
| 116 | return pending; |
---|
[300] | 117 | } |
---|
[509] | 118 | |
---|
[300] | 119 | bool CClientBuffer::hasPendingRequest(void) |
---|
| 120 | { |
---|
[732] | 121 | return (pending || count > 0); |
---|
[300] | 122 | } |
---|
[509] | 123 | } |
---|