[591] | 1 | #include "xios_spl.hpp" |
---|
[300] | 2 | #include "context_client.hpp" |
---|
| 3 | #include "context_server.hpp" |
---|
| 4 | #include "event_client.hpp" |
---|
| 5 | #include "buffer_out.hpp" |
---|
| 6 | #include "buffer_client.hpp" |
---|
| 7 | #include "type.hpp" |
---|
| 8 | #include "event_client.hpp" |
---|
| 9 | #include "context.hpp" |
---|
[382] | 10 | #include "mpi.hpp" |
---|
[347] | 11 | #include "timer.hpp" |
---|
[401] | 12 | #include "cxios.hpp" |
---|
[300] | 13 | |
---|
[335] | 14 | namespace xios |
---|
[300] | 15 | { |
---|
[512] | 16 | /*! |
---|
| 17 | \param [in] parent Pointer to context on client side |
---|
| 18 | \param [in] intraComm_ communicator of group client |
---|
| 19 | \param [in] interComm_ communicator of group server |
---|
| 20 | \cxtSer [in] cxtSer Pointer to context of server side. (It is only used on case of attached mode) |
---|
| 21 | */ |
---|
[595] | 22 | CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer) |
---|
[511] | 23 | : mapBufferSize_(), parentServer(cxtSer) |
---|
[300] | 24 | { |
---|
[595] | 25 | context = parent; |
---|
| 26 | intraComm = intraComm_; |
---|
| 27 | interComm = interComm_; |
---|
| 28 | MPI_Comm_rank(intraComm, &clientRank); |
---|
| 29 | MPI_Comm_size(intraComm, &clientSize); |
---|
[509] | 30 | |
---|
[595] | 31 | int flag; |
---|
| 32 | MPI_Comm_test_inter(interComm, &flag); |
---|
| 33 | if (flag) MPI_Comm_remote_size(interComm, &serverSize); |
---|
| 34 | else MPI_Comm_size(interComm, &serverSize); |
---|
[509] | 35 | |
---|
[595] | 36 | if (clientSize < serverSize) |
---|
| 37 | { |
---|
| 38 | int serverByClient = serverSize / clientSize; |
---|
| 39 | int remain = serverSize % clientSize; |
---|
| 40 | int rankStart = serverByClient * clientRank; |
---|
[300] | 41 | |
---|
[595] | 42 | if (clientRank < remain) |
---|
| 43 | { |
---|
| 44 | serverByClient++; |
---|
| 45 | rankStart += clientRank; |
---|
| 46 | } |
---|
| 47 | else |
---|
| 48 | rankStart += remain; |
---|
| 49 | |
---|
| 50 | for (int i = 0; i < serverByClient; i++) |
---|
| 51 | ranksServerLeader.push_back(rankStart + i); |
---|
| 52 | } |
---|
| 53 | else |
---|
| 54 | { |
---|
| 55 | int clientByServer = clientSize / serverSize; |
---|
| 56 | int remain = clientSize % serverSize; |
---|
| 57 | |
---|
| 58 | if (clientRank < (clientByServer + 1) * remain) |
---|
| 59 | { |
---|
| 60 | if (clientRank % (clientByServer + 1) == 0) |
---|
| 61 | ranksServerLeader.push_back(clientRank / (clientByServer + 1)); |
---|
| 62 | } |
---|
| 63 | else |
---|
| 64 | { |
---|
| 65 | int rank = clientRank - (clientByServer + 1) * remain; |
---|
| 66 | if (rank % clientByServer == 0) |
---|
| 67 | ranksServerLeader.push_back(remain + rank / clientByServer); |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | timeLine = 0; |
---|
[697] | 72 | |
---|
[300] | 73 | } |
---|
| 74 | |
---|
[697] | 75 | |
---|
[512] | 76 | /*! |
---|
| 77 | In case of attached mode, the current context must be reset to context for client |
---|
| 78 | \param [in] event Event sent to server |
---|
| 79 | */ |
---|
[300] | 80 | void CContextClient::sendEvent(CEventClient& event) |
---|
| 81 | { |
---|
[595] | 82 | list<int>::iterator itServer; |
---|
| 83 | list<int> ranks; |
---|
| 84 | list<int> sizes; |
---|
| 85 | list<int>::iterator itSize; |
---|
[509] | 86 | |
---|
[595] | 87 | ranks = event.getRanks(); |
---|
| 88 | if (!event.isEmpty()) |
---|
[300] | 89 | { |
---|
[595] | 90 | sizes = event.getSizes(); |
---|
| 91 | CMessage msg; |
---|
[300] | 92 | |
---|
[595] | 93 | msg << *(sizes.begin()) << timeLine; |
---|
| 94 | for (list<int>::iterator it = sizes.begin(); it != sizes.end(); it++) *it += msg.size(); |
---|
| 95 | list<CBufferOut*> buffList = getBuffers(ranks, sizes); |
---|
[509] | 96 | |
---|
[595] | 97 | list<CBufferOut*>::iterator it; |
---|
| 98 | for (it = buffList.begin(), itSize = sizes.begin(); it != buffList.end(); ++it, ++itSize) |
---|
[300] | 99 | { |
---|
[595] | 100 | **it << *itSize << timeLine; |
---|
[300] | 101 | } |
---|
[595] | 102 | event.send(buffList); |
---|
| 103 | checkBuffers(ranks); |
---|
[300] | 104 | } |
---|
| 105 | |
---|
[704] | 106 | if (isAttachedModeEnabled()) |
---|
[511] | 107 | { |
---|
| 108 | waitEvent(ranks); |
---|
| 109 | CContext::setCurrent(context->getId()); |
---|
| 110 | } |
---|
| 111 | |
---|
[595] | 112 | timeLine++; |
---|
[300] | 113 | } |
---|
[509] | 114 | |
---|
[512] | 115 | /*! |
---|
| 116 | Special function to setup size of buffer not only on client side but also on server side |
---|
| 117 | corresponding to the connection |
---|
| 118 | */ |
---|
[509] | 119 | void CContextClient::sendBufferSizeEvent() |
---|
| 120 | { |
---|
[595] | 121 | std::map<int,CClientBuffer*>::iterator it, itE; |
---|
| 122 | std::map<int,StdSize>::const_iterator itMap = mapBufferSize_.begin(), iteMap = mapBufferSize_.end(); |
---|
[509] | 123 | |
---|
| 124 | if (itMap == iteMap) |
---|
[595] | 125 | ERROR("void CContextClient::sendBufferSizeEvent()", |
---|
[509] | 126 | <<"No information about server buffer, that should not happen..."); |
---|
| 127 | |
---|
[512] | 128 | for (; itMap != iteMap; ++itMap) |
---|
[509] | 129 | { |
---|
| 130 | if (buffers.end() == buffers.find(itMap->first)) |
---|
| 131 | newBuffer(itMap->first); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | CBufferOut* bufOut(NULL); |
---|
| 135 | itE = buffers.end(); |
---|
| 136 | for (it = buffers.begin(); it != itE; ++it) |
---|
| 137 | { |
---|
| 138 | bufOut = (it->second)->getBuffer(sizeof(StdSize)); |
---|
| 139 | bufOut->put(mapBufferSize_[it->first]); // Stupid C++ |
---|
| 140 | (it->second)->checkBuffer(); |
---|
| 141 | } |
---|
[596] | 142 | |
---|
[704] | 143 | if (isAttachedModeEnabled()) |
---|
[596] | 144 | { |
---|
| 145 | while (checkBuffers()) |
---|
| 146 | { |
---|
| 147 | parentServer->server->listen(); |
---|
| 148 | } |
---|
| 149 | CContext::setCurrent(context->getId()); |
---|
| 150 | } |
---|
[509] | 151 | } |
---|
| 152 | |
---|
[512] | 153 | /*! |
---|
| 154 | If client is also server (attached mode), after sending event, it should process right away |
---|
| 155 | the incoming event. |
---|
| 156 | \param [in] ranks list rank of server connected this client |
---|
| 157 | */ |
---|
[300] | 158 | void CContextClient::waitEvent(list<int>& ranks) |
---|
| 159 | { |
---|
[595] | 160 | parentServer->server->setPendingEvent(); |
---|
| 161 | while (checkBuffers(ranks)) |
---|
[300] | 162 | { |
---|
[595] | 163 | parentServer->server->listen(); |
---|
| 164 | parentServer->server->checkPendingRequest(); |
---|
[300] | 165 | } |
---|
[386] | 166 | |
---|
[595] | 167 | while (parentServer->server->hasPendingEvent()) |
---|
[386] | 168 | { |
---|
[595] | 169 | parentServer->server->eventLoop(); |
---|
[386] | 170 | } |
---|
[300] | 171 | } |
---|
| 172 | |
---|
[512] | 173 | /*! |
---|
| 174 | Setup buffer for each connection to server and verify their state to put content into them |
---|
| 175 | \param [in] serverList list of rank of connected server |
---|
| 176 | \param [in] sizeList size of message corresponding to each connection |
---|
| 177 | \return List of buffer input which event can be placed |
---|
| 178 | */ |
---|
[300] | 179 | list<CBufferOut*> CContextClient::getBuffers(list<int>& serverList, list<int>& sizeList) |
---|
| 180 | { |
---|
[595] | 181 | list<int>::iterator itServer, itSize; |
---|
| 182 | list<CClientBuffer*> bufferList; |
---|
| 183 | map<int,CClientBuffer*>::iterator it; |
---|
| 184 | list<CClientBuffer*>::iterator itBuffer; |
---|
| 185 | list<CBufferOut*> retBuffer; |
---|
| 186 | bool free; |
---|
[300] | 187 | |
---|
[595] | 188 | for (itServer = serverList.begin(); itServer != serverList.end(); itServer++) |
---|
[300] | 189 | { |
---|
[595] | 190 | it = buffers.find(*itServer); |
---|
| 191 | if (it == buffers.end()) |
---|
[300] | 192 | { |
---|
[595] | 193 | newBuffer(*itServer); |
---|
| 194 | it = buffers.find(*itServer); |
---|
[509] | 195 | } |
---|
[595] | 196 | bufferList.push_back(it->second); |
---|
[300] | 197 | } |
---|
[595] | 198 | free = false; |
---|
[347] | 199 | |
---|
| 200 | CTimer::get("Blocking time").resume(); |
---|
[595] | 201 | while (!free) |
---|
[300] | 202 | { |
---|
[595] | 203 | free = true; |
---|
| 204 | for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++) |
---|
[300] | 205 | { |
---|
[595] | 206 | (*itBuffer)->checkBuffer(); |
---|
| 207 | free &= (*itBuffer)->isBufferFree(*itSize); |
---|
[300] | 208 | } |
---|
[597] | 209 | if (!free) |
---|
| 210 | context->server->listen(); |
---|
[300] | 211 | } |
---|
[347] | 212 | CTimer::get("Blocking time").suspend(); |
---|
| 213 | |
---|
[595] | 214 | for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++) |
---|
[300] | 215 | { |
---|
[595] | 216 | retBuffer.push_back((*itBuffer)->getBuffer(*itSize)); |
---|
[300] | 217 | } |
---|
[595] | 218 | return retBuffer; |
---|
[300] | 219 | } |
---|
[509] | 220 | |
---|
[512] | 221 | /*! |
---|
| 222 | Make a new buffer for a certain connection to server with specific rank |
---|
| 223 | \param [in] rank rank of connected server |
---|
| 224 | */ |
---|
[300] | 225 | void CContextClient::newBuffer(int rank) |
---|
| 226 | { |
---|
[595] | 227 | buffers[rank] = new CClientBuffer(interComm, rank, mapBufferSize_[rank]); |
---|
[509] | 228 | } |
---|
[300] | 229 | |
---|
[512] | 230 | /*! |
---|
| 231 | Verify state of buffers. Buffer is under pending state if there is no message on it |
---|
| 232 | \return state of buffers, pending(true), ready(false) |
---|
| 233 | */ |
---|
[300] | 234 | bool CContextClient::checkBuffers(void) |
---|
| 235 | { |
---|
[595] | 236 | map<int,CClientBuffer*>::iterator itBuff; |
---|
| 237 | bool pending = false; |
---|
| 238 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) pending |= itBuff->second->checkBuffer(); |
---|
| 239 | return pending; |
---|
[509] | 240 | } |
---|
[300] | 241 | |
---|
[512] | 242 | //! Release all buffers |
---|
[300] | 243 | void CContextClient::releaseBuffers(void) |
---|
| 244 | { |
---|
[595] | 245 | map<int,CClientBuffer*>::iterator itBuff; |
---|
| 246 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) delete itBuff->second; |
---|
[509] | 247 | } |
---|
[300] | 248 | |
---|
[512] | 249 | /*! |
---|
| 250 | Verify state of buffers corresponding to a connection |
---|
| 251 | \param [in] ranks list rank of server to which client connects to |
---|
| 252 | \return state of buffers, pending(true), ready(false) |
---|
| 253 | */ |
---|
[300] | 254 | bool CContextClient::checkBuffers(list<int>& ranks) |
---|
| 255 | { |
---|
[595] | 256 | list<int>::iterator it; |
---|
| 257 | bool pending = false; |
---|
| 258 | for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->checkBuffer(); |
---|
| 259 | return pending; |
---|
[509] | 260 | } |
---|
[300] | 261 | |
---|
[512] | 262 | /*! |
---|
| 263 | Set buffer size for each connection |
---|
| 264 | \param [in] mapSize mapping rank of connected server to size of allocated buffer |
---|
| 265 | */ |
---|
[595] | 266 | void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize) |
---|
[509] | 267 | { |
---|
| 268 | mapBufferSize_ = mapSize; |
---|
| 269 | sendBufferSizeEvent(); |
---|
| 270 | } |
---|
| 271 | |
---|
[595] | 272 | /*! |
---|
| 273 | Get leading server in the group of connected server |
---|
| 274 | \return ranks of leading servers |
---|
| 275 | */ |
---|
| 276 | const std::list<int>& CContextClient::getRanksServerLeader(void) const |
---|
| 277 | { |
---|
| 278 | return ranksServerLeader; |
---|
| 279 | } |
---|
[509] | 280 | |
---|
[595] | 281 | /*! |
---|
| 282 | Check if client connects to leading server |
---|
| 283 | \return connected(true), not connected (false) |
---|
| 284 | */ |
---|
| 285 | bool CContextClient::isServerLeader(void) const |
---|
| 286 | { |
---|
| 287 | return !ranksServerLeader.empty(); |
---|
| 288 | } |
---|
[300] | 289 | |
---|
[704] | 290 | /*! |
---|
| 291 | * Check if the attached mode is used. |
---|
| 292 | * |
---|
| 293 | * \return true if and only if attached mode is used |
---|
| 294 | */ |
---|
| 295 | bool CContextClient::isAttachedModeEnabled() const |
---|
| 296 | { |
---|
| 297 | return (parentServer != 0); |
---|
| 298 | } |
---|
[697] | 299 | |
---|
[512] | 300 | /*! |
---|
| 301 | Finalize context client and do some reports |
---|
| 302 | */ |
---|
[697] | 303 | |
---|
[300] | 304 | void CContextClient::finalize(void) |
---|
| 305 | { |
---|
[595] | 306 | map<int,CClientBuffer*>::iterator itBuff; |
---|
| 307 | bool stop = true; |
---|
[697] | 308 | |
---|
[595] | 309 | CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE); |
---|
[300] | 310 | if (isServerLeader()) |
---|
| 311 | { |
---|
[595] | 312 | CMessage msg; |
---|
| 313 | const std::list<int>& ranks = getRanksServerLeader(); |
---|
| 314 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 315 | event.push(*itRank, 1, msg); |
---|
| 316 | sendEvent(event); |
---|
[300] | 317 | } |
---|
[595] | 318 | else sendEvent(event); |
---|
[509] | 319 | |
---|
[347] | 320 | CTimer::get("Blocking time").resume(); |
---|
[595] | 321 | while (stop) |
---|
[300] | 322 | { |
---|
[595] | 323 | checkBuffers(); |
---|
| 324 | stop = false; |
---|
| 325 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) stop |= itBuff->second->hasPendingRequest(); |
---|
[300] | 326 | } |
---|
[347] | 327 | CTimer::get("Blocking time").suspend(); |
---|
[509] | 328 | |
---|
[595] | 329 | std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
| 330 | iteMap = mapBufferSize_.end(), itMap; |
---|
[511] | 331 | StdSize totalBuf = 0; |
---|
| 332 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
| 333 | { |
---|
[595] | 334 | report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl |
---|
| 335 | << " +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl; |
---|
[511] | 336 | totalBuf += itMap->second; |
---|
| 337 | } |
---|
[595] | 338 | report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl; |
---|
[511] | 339 | |
---|
[595] | 340 | releaseBuffers(); |
---|
[697] | 341 | |
---|
[300] | 342 | } |
---|
[509] | 343 | } |
---|