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