1 | #ifndef __P2P_CLIENT_BUFFER_HPP__ |
---|
2 | #define __P2P_CLIENT_BUFFER_HPP__ |
---|
3 | |
---|
4 | #include "xios_spl.hpp" |
---|
5 | #include "buffer_out.hpp" |
---|
6 | #include "mpi.hpp" |
---|
7 | #include "cxios.hpp" |
---|
8 | #include "event_client.hpp" |
---|
9 | #include "p2p_cs_buffer_base.hpp" |
---|
10 | |
---|
11 | namespace xios |
---|
12 | { |
---|
13 | extern CLogType logProtocol ; |
---|
14 | |
---|
15 | class CP2pClientBuffer : public CP2pCSBufferBase |
---|
16 | { |
---|
17 | class CBuffer |
---|
18 | { |
---|
19 | char* buffer_ ; |
---|
20 | size_t start_ ; |
---|
21 | size_t end_ ; |
---|
22 | size_t count_ ; |
---|
23 | size_t size_ ; |
---|
24 | bool fixed_ ; |
---|
25 | char* window_ ; |
---|
26 | |
---|
27 | public: |
---|
28 | CBuffer(char* window, size_t size, bool fixed) : start_(size), end_(0), count_(0), size_(size), fixed_(fixed), window_(window) |
---|
29 | { |
---|
30 | size_t trueSize = (size/8 + 1)*8 + 8 ; // seems to have some problem with OpenMPi/UCX when trying to tranfer 1 byte |
---|
31 | // at the end of the buffer. Alignment problem ? Or simple bug ? Seems that allocate |
---|
32 | // greater buffer solves the problem |
---|
33 | MPI_Alloc_mem(trueSize, MPI_INFO_NULL, &buffer_) ; |
---|
34 | info(logProtocol)<<"Attach memory to windows : addr="<<(MPI_Aint)buffer_<<" count="<<size<<endl ; |
---|
35 | |
---|
36 | //MPI_Win_attach(window_, buffer_, trueSize) ; |
---|
37 | |
---|
38 | } |
---|
39 | ~CBuffer() |
---|
40 | { |
---|
41 | if (count_>0) ERROR("COneSidedClientBuffer::~CBuffer()",<<"Try to delete buffer that is not empty"<<std::endl) ; |
---|
42 | //MPI_Win_detach(window_, buffer_) ; |
---|
43 | MPI_Free_mem(buffer_) ; |
---|
44 | info(logProtocol)<<"Detach memory from windows : addr="<<(MPI_Aint)buffer_<<" count="<<size_<<endl ; |
---|
45 | } |
---|
46 | |
---|
47 | void write(char** buffer, size_t& size, MPI_Aint& addr, size_t& start, size_t& count) |
---|
48 | { |
---|
49 | addr = 0 ; |
---|
50 | count = 0 ; |
---|
51 | if (end_ < start_) |
---|
52 | { |
---|
53 | if (start_-end_ >= size) |
---|
54 | { |
---|
55 | count=size ; |
---|
56 | size = 0 ; |
---|
57 | start=end_ ; |
---|
58 | MPI_Get_address(&buffer_[start], &addr) ; |
---|
59 | end_ += count ; |
---|
60 | count_+= count ; |
---|
61 | memcpy(&buffer_[start], *buffer, count) ; |
---|
62 | *buffer+=count ; |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | count = start_-end_ ; |
---|
67 | size -= count ; |
---|
68 | start=end_ ; |
---|
69 | MPI_Get_address(&buffer_[start], &addr) ; |
---|
70 | end_ = start_ ; |
---|
71 | count_+=count ; |
---|
72 | memcpy(&buffer_[start], *buffer, count) ; |
---|
73 | *buffer+=count ; |
---|
74 | } |
---|
75 | } |
---|
76 | else if ( end_> start_ ) |
---|
77 | { |
---|
78 | if (size_-end_ >= size) |
---|
79 | { |
---|
80 | count = size ; |
---|
81 | size = 0; |
---|
82 | start=end_ ; |
---|
83 | MPI_Get_address(&buffer_[start], &addr) ; |
---|
84 | end_ += count ; |
---|
85 | count_ += count ; |
---|
86 | memcpy(&buffer_[start], *buffer, count) ; |
---|
87 | *buffer+=count ; |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | count = size_ - end_ ; |
---|
92 | size -= count ; |
---|
93 | start=end_ ; |
---|
94 | MPI_Get_address(&buffer_[start], &addr) ; |
---|
95 | end_ = 0 ; |
---|
96 | count_+= count ; |
---|
97 | memcpy(&buffer_[start], *buffer, count) ; |
---|
98 | *buffer+=count ; |
---|
99 | } |
---|
100 | } |
---|
101 | else if (end_==start_) |
---|
102 | { |
---|
103 | count = 0 ; |
---|
104 | } |
---|
105 | |
---|
106 | // check |
---|
107 | |
---|
108 | if (count!=0) |
---|
109 | { |
---|
110 | MPI_Aint startBufferAddr,endBufferAddr ; |
---|
111 | MPI_Get_address(&buffer_[0], &startBufferAddr) ; |
---|
112 | MPI_Get_address(&buffer_[size_-1], &endBufferAddr) ; |
---|
113 | |
---|
114 | if (addr<startBufferAddr || MPI_Aint_add(addr,count-1)>endBufferAddr) |
---|
115 | { |
---|
116 | ERROR("CP2pClientBuffer::CBuffer::write(char** buffer, size_t& size, MPI_Aint& addr, size_t& start, size_t& count)",<<" out of bounds"<<std::endl) ; |
---|
117 | } |
---|
118 | |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | void free(size_t start, size_t count) |
---|
123 | { |
---|
124 | start_ = start+count-1 ; |
---|
125 | count_ -= count ; |
---|
126 | } |
---|
127 | |
---|
128 | size_t remain(void) { return size_-count_; } |
---|
129 | size_t getSize(void) { return size_ ;} |
---|
130 | size_t getCount(void) {return count_ ;} |
---|
131 | size_t isFixed(void) {return fixed_;} |
---|
132 | } ; |
---|
133 | |
---|
134 | public: |
---|
135 | CP2pClientBuffer(MPI_Comm& interComm, int serverRank, MPI_Comm& commSelf, MPI_Comm& interCommMerged, int intraServerRank) ; |
---|
136 | void newBuffer(size_t size, bool fixed) ; |
---|
137 | bool isBufferFree(size_t size) ; |
---|
138 | int writeBuffer(char* buff, size_t size) ; |
---|
139 | void freeBuffer(MPI_Aint addr) ; |
---|
140 | bool freeBloc(MPI_Aint addr) ; |
---|
141 | bool writeEvent(size_t timeLine, CEventClient& event) ; |
---|
142 | bool isEmpty(void) { return blocs_.empty() ;} |
---|
143 | bool isNotifiedFinalized(void) { eventLoop() ; return buffers_.empty() && isFinalized_ ;} |
---|
144 | void setFixed(size_t size) { fixed_=true ; fixedSize_=size ;} |
---|
145 | void setGrowable(double growingFactor) { fixed_= false ; growingFactor_=growingFactor;} |
---|
146 | void setGrowingFactor(double growingFactor) {growingFactor_=growingFactor;} |
---|
147 | void eventLoop(void) ; |
---|
148 | void sendTimelineEvent(size_t timeline, int nbSenders, int nbBlocs) ; |
---|
149 | void sendResizeBufferEvent(size_t timeline, size_t currentBufferSize_) ; |
---|
150 | void sendNewBuffer(void) ; |
---|
151 | void createWindow(MPI_Comm& commSelf, MPI_Comm& interCommMerged, int intraServerRank ) ; |
---|
152 | void listenFinalize(void) ; |
---|
153 | |
---|
154 | private : |
---|
155 | |
---|
156 | struct SBloc |
---|
157 | { |
---|
158 | MPI_Aint addr ; |
---|
159 | CBuffer* buffer ; |
---|
160 | size_t start ; |
---|
161 | int count ; |
---|
162 | int window ; |
---|
163 | } ; |
---|
164 | |
---|
165 | struct SRequest |
---|
166 | { |
---|
167 | CBufferOut* buffer ; |
---|
168 | MPI_Request mpiRequest ; |
---|
169 | } ; |
---|
170 | |
---|
171 | struct SBlocRequest |
---|
172 | { |
---|
173 | MPI_Aint addr ; |
---|
174 | MPI_Request mpiRequest ; |
---|
175 | } ; |
---|
176 | |
---|
177 | MPI_Aint* control_ ; |
---|
178 | |
---|
179 | MPI_Comm interComm_; |
---|
180 | MPI_Comm winComm_ ; |
---|
181 | |
---|
182 | //MPI_Win window_ ; |
---|
183 | //vector<MPI_Win> windows_ ; |
---|
184 | vector<char*> windows_ ; |
---|
185 | vector<bool> usedWindows_ ; |
---|
186 | int currentWindow_ ; |
---|
187 | int maxWindows_ ; |
---|
188 | |
---|
189 | //MPI_Win winControl_ ; |
---|
190 | int serverRank_ ; |
---|
191 | |
---|
192 | MPI_Comm interCommMerged_; |
---|
193 | int intraServerRank_ ; |
---|
194 | |
---|
195 | std::list<CBuffer*> buffers_ ; |
---|
196 | std::list<SBloc> blocs_ ; |
---|
197 | std::list<SBloc>::iterator lastBlocEvent_ ; |
---|
198 | CBuffer* currentBuffer_=nullptr ; |
---|
199 | std::list<SRequest> requests_ ; |
---|
200 | |
---|
201 | std::list<SBlocRequest> sentBlocRequest_ ; |
---|
202 | MPI_Request finalizeRequest_ ; |
---|
203 | |
---|
204 | bool fixed_=false; |
---|
205 | size_t fixedSize_ = 0 ; |
---|
206 | size_t currentBufferSize_= 0 ; |
---|
207 | double growingFactor_ = 1.2 ; |
---|
208 | MPI_Aint lastFreedBloc_=0 ; |
---|
209 | bool isFinalized_ = false ; |
---|
210 | int maxSentBlocRequests_ = 10000 ; |
---|
211 | |
---|
212 | } ; |
---|
213 | |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | #endif |
---|