[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 |
---|
[983] | 20 | \cxtSer [in] cxtSer Pointer to context of server side. (It is only used in case of attached mode). |
---|
[512] | 21 | */ |
---|
[595] | 22 | CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer) |
---|
[917] | 23 | : mapBufferSize_(), parentServer(cxtSer), maxBufferedEvents(4) |
---|
[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; |
---|
[300] | 72 | } |
---|
| 73 | |
---|
[512] | 74 | /*! |
---|
| 75 | In case of attached mode, the current context must be reset to context for client |
---|
| 76 | \param [in] event Event sent to server |
---|
| 77 | */ |
---|
[300] | 78 | void CContextClient::sendEvent(CEventClient& event) |
---|
| 79 | { |
---|
[731] | 80 | list<int> ranks = event.getRanks(); |
---|
[595] | 81 | if (!event.isEmpty()) |
---|
[300] | 82 | { |
---|
[731] | 83 | list<int> sizes = event.getSizes(); |
---|
[300] | 84 | |
---|
[595] | 85 | list<CBufferOut*> buffList = getBuffers(ranks, sizes); |
---|
[509] | 86 | |
---|
[731] | 87 | event.send(timeLine, sizes, buffList); |
---|
| 88 | |
---|
[595] | 89 | checkBuffers(ranks); |
---|
[300] | 90 | } |
---|
| 91 | |
---|
[704] | 92 | if (isAttachedModeEnabled()) |
---|
[511] | 93 | { |
---|
| 94 | waitEvent(ranks); |
---|
| 95 | CContext::setCurrent(context->getId()); |
---|
| 96 | } |
---|
| 97 | |
---|
[595] | 98 | timeLine++; |
---|
[300] | 99 | } |
---|
[509] | 100 | |
---|
[512] | 101 | /*! |
---|
| 102 | If client is also server (attached mode), after sending event, it should process right away |
---|
| 103 | the incoming event. |
---|
| 104 | \param [in] ranks list rank of server connected this client |
---|
| 105 | */ |
---|
[300] | 106 | void CContextClient::waitEvent(list<int>& ranks) |
---|
| 107 | { |
---|
[595] | 108 | parentServer->server->setPendingEvent(); |
---|
| 109 | while (checkBuffers(ranks)) |
---|
[300] | 110 | { |
---|
[595] | 111 | parentServer->server->listen(); |
---|
| 112 | parentServer->server->checkPendingRequest(); |
---|
[300] | 113 | } |
---|
[386] | 114 | |
---|
[595] | 115 | while (parentServer->server->hasPendingEvent()) |
---|
[386] | 116 | { |
---|
[595] | 117 | parentServer->server->eventLoop(); |
---|
[386] | 118 | } |
---|
[300] | 119 | } |
---|
| 120 | |
---|
[512] | 121 | /*! |
---|
| 122 | Setup buffer for each connection to server and verify their state to put content into them |
---|
| 123 | \param [in] serverList list of rank of connected server |
---|
| 124 | \param [in] sizeList size of message corresponding to each connection |
---|
| 125 | \return List of buffer input which event can be placed |
---|
| 126 | */ |
---|
[300] | 127 | list<CBufferOut*> CContextClient::getBuffers(list<int>& serverList, list<int>& sizeList) |
---|
| 128 | { |
---|
[595] | 129 | list<int>::iterator itServer, itSize; |
---|
| 130 | list<CClientBuffer*> bufferList; |
---|
| 131 | map<int,CClientBuffer*>::iterator it; |
---|
| 132 | list<CClientBuffer*>::iterator itBuffer; |
---|
| 133 | list<CBufferOut*> retBuffer; |
---|
[884] | 134 | bool areBuffersFree; |
---|
[300] | 135 | |
---|
[595] | 136 | for (itServer = serverList.begin(); itServer != serverList.end(); itServer++) |
---|
[300] | 137 | { |
---|
[595] | 138 | it = buffers.find(*itServer); |
---|
| 139 | if (it == buffers.end()) |
---|
[300] | 140 | { |
---|
[595] | 141 | newBuffer(*itServer); |
---|
| 142 | it = buffers.find(*itServer); |
---|
[509] | 143 | } |
---|
[595] | 144 | bufferList.push_back(it->second); |
---|
[300] | 145 | } |
---|
[347] | 146 | |
---|
| 147 | CTimer::get("Blocking time").resume(); |
---|
[884] | 148 | do |
---|
[300] | 149 | { |
---|
[884] | 150 | areBuffersFree = true; |
---|
[595] | 151 | for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++) |
---|
[884] | 152 | areBuffersFree &= (*itBuffer)->isBufferFree(*itSize); |
---|
| 153 | |
---|
| 154 | if (!areBuffersFree) |
---|
[300] | 155 | { |
---|
[884] | 156 | checkBuffers(); |
---|
| 157 | context->server->listen(); |
---|
[300] | 158 | } |
---|
[884] | 159 | } while (!areBuffersFree); |
---|
[347] | 160 | CTimer::get("Blocking time").suspend(); |
---|
| 161 | |
---|
[595] | 162 | for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++) |
---|
[300] | 163 | { |
---|
[595] | 164 | retBuffer.push_back((*itBuffer)->getBuffer(*itSize)); |
---|
[300] | 165 | } |
---|
[595] | 166 | return retBuffer; |
---|
[300] | 167 | } |
---|
[509] | 168 | |
---|
[512] | 169 | /*! |
---|
| 170 | Make a new buffer for a certain connection to server with specific rank |
---|
| 171 | \param [in] rank rank of connected server |
---|
| 172 | */ |
---|
[300] | 173 | void CContextClient::newBuffer(int rank) |
---|
| 174 | { |
---|
[724] | 175 | if (!mapBufferSize_.count(rank)) |
---|
| 176 | { |
---|
| 177 | error(0) << "WARNING: Unexpected request for buffer to communicate with server " << rank << std::endl; |
---|
| 178 | mapBufferSize_[rank] = CXios::minBufferSize; |
---|
| 179 | } |
---|
[917] | 180 | CClientBuffer* buffer = buffers[rank] = new CClientBuffer(interComm, rank, mapBufferSize_[rank], maxBufferedEvents); |
---|
[725] | 181 | // Notify the server |
---|
| 182 | CBufferOut* bufOut = buffer->getBuffer(sizeof(StdSize)); |
---|
| 183 | bufOut->put(mapBufferSize_[rank]); // Stupid C++ |
---|
| 184 | buffer->checkBuffer(); |
---|
[509] | 185 | } |
---|
[300] | 186 | |
---|
[512] | 187 | /*! |
---|
| 188 | Verify state of buffers. Buffer is under pending state if there is no message on it |
---|
| 189 | \return state of buffers, pending(true), ready(false) |
---|
| 190 | */ |
---|
[300] | 191 | bool CContextClient::checkBuffers(void) |
---|
| 192 | { |
---|
[595] | 193 | map<int,CClientBuffer*>::iterator itBuff; |
---|
| 194 | bool pending = false; |
---|
| 195 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) pending |= itBuff->second->checkBuffer(); |
---|
| 196 | return pending; |
---|
[509] | 197 | } |
---|
[300] | 198 | |
---|
[512] | 199 | //! Release all buffers |
---|
[300] | 200 | void CContextClient::releaseBuffers(void) |
---|
| 201 | { |
---|
[595] | 202 | map<int,CClientBuffer*>::iterator itBuff; |
---|
| 203 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) delete itBuff->second; |
---|
[509] | 204 | } |
---|
[300] | 205 | |
---|
[512] | 206 | /*! |
---|
| 207 | Verify state of buffers corresponding to a connection |
---|
| 208 | \param [in] ranks list rank of server to which client connects to |
---|
| 209 | \return state of buffers, pending(true), ready(false) |
---|
| 210 | */ |
---|
[300] | 211 | bool CContextClient::checkBuffers(list<int>& ranks) |
---|
| 212 | { |
---|
[595] | 213 | list<int>::iterator it; |
---|
| 214 | bool pending = false; |
---|
| 215 | for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->checkBuffer(); |
---|
| 216 | return pending; |
---|
[509] | 217 | } |
---|
[300] | 218 | |
---|
[512] | 219 | /*! |
---|
[917] | 220 | * Set the buffer size for each connection. Warning: This function is collective. |
---|
| 221 | * |
---|
| 222 | * \param [in] mapSize maps the rank of the connected servers to the size of the correspoinding buffer |
---|
| 223 | * \param [in] maxEventSize maps the rank of the connected servers to the size of the biggest event |
---|
[512] | 224 | */ |
---|
[917] | 225 | void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize) |
---|
[509] | 226 | { |
---|
| 227 | mapBufferSize_ = mapSize; |
---|
[917] | 228 | |
---|
| 229 | // Compute the maximum number of events that can be safely buffered. |
---|
| 230 | double minBufferSizeEventSizeRatio = std::numeric_limits<double>::max(); |
---|
| 231 | for (std::map<int,StdSize>::const_iterator it = mapSize.begin(), ite = mapSize.end(); it != ite; ++it) |
---|
| 232 | { |
---|
| 233 | double ratio = double(it->second) / maxEventSize.at(it->first); |
---|
| 234 | if (ratio < minBufferSizeEventSizeRatio) minBufferSizeEventSizeRatio = ratio; |
---|
| 235 | } |
---|
| 236 | MPI_Allreduce(MPI_IN_PLACE, &minBufferSizeEventSizeRatio, 1, MPI_DOUBLE, MPI_MIN, intraComm); |
---|
| 237 | |
---|
| 238 | if (minBufferSizeEventSizeRatio < 1.0) |
---|
| 239 | ERROR("void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize)", |
---|
| 240 | << "The buffer sizes and the maximum events sizes are incoherent."); |
---|
| 241 | |
---|
| 242 | maxBufferedEvents = size_t(2 * minBufferSizeEventSizeRatio) // there is room for two local buffers on the server |
---|
| 243 | + size_t(minBufferSizeEventSizeRatio) // one local buffer can always be fully used |
---|
| 244 | + 1; // the other local buffer might contain only one event |
---|
[509] | 245 | } |
---|
| 246 | |
---|
[595] | 247 | /*! |
---|
| 248 | Get leading server in the group of connected server |
---|
| 249 | \return ranks of leading servers |
---|
| 250 | */ |
---|
| 251 | const std::list<int>& CContextClient::getRanksServerLeader(void) const |
---|
| 252 | { |
---|
| 253 | return ranksServerLeader; |
---|
| 254 | } |
---|
[509] | 255 | |
---|
[595] | 256 | /*! |
---|
| 257 | Check if client connects to leading server |
---|
| 258 | \return connected(true), not connected (false) |
---|
| 259 | */ |
---|
| 260 | bool CContextClient::isServerLeader(void) const |
---|
| 261 | { |
---|
| 262 | return !ranksServerLeader.empty(); |
---|
| 263 | } |
---|
[300] | 264 | |
---|
[704] | 265 | /*! |
---|
| 266 | * Check if the attached mode is used. |
---|
| 267 | * |
---|
| 268 | * \return true if and only if attached mode is used |
---|
| 269 | */ |
---|
| 270 | bool CContextClient::isAttachedModeEnabled() const |
---|
| 271 | { |
---|
| 272 | return (parentServer != 0); |
---|
| 273 | } |
---|
[697] | 274 | |
---|
[512] | 275 | /*! |
---|
| 276 | Finalize context client and do some reports |
---|
| 277 | */ |
---|
[300] | 278 | void CContextClient::finalize(void) |
---|
| 279 | { |
---|
[595] | 280 | map<int,CClientBuffer*>::iterator itBuff; |
---|
| 281 | bool stop = true; |
---|
[731] | 282 | |
---|
[595] | 283 | CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE); |
---|
[300] | 284 | if (isServerLeader()) |
---|
| 285 | { |
---|
[595] | 286 | CMessage msg; |
---|
| 287 | const std::list<int>& ranks = getRanksServerLeader(); |
---|
| 288 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 289 | event.push(*itRank, 1, msg); |
---|
| 290 | sendEvent(event); |
---|
[300] | 291 | } |
---|
[595] | 292 | else sendEvent(event); |
---|
[509] | 293 | |
---|
[347] | 294 | CTimer::get("Blocking time").resume(); |
---|
[595] | 295 | while (stop) |
---|
[300] | 296 | { |
---|
[595] | 297 | checkBuffers(); |
---|
| 298 | stop = false; |
---|
| 299 | for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) stop |= itBuff->second->hasPendingRequest(); |
---|
[300] | 300 | } |
---|
[347] | 301 | CTimer::get("Blocking time").suspend(); |
---|
[509] | 302 | |
---|
[595] | 303 | std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
| 304 | iteMap = mapBufferSize_.end(), itMap; |
---|
[511] | 305 | StdSize totalBuf = 0; |
---|
| 306 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
| 307 | { |
---|
[595] | 308 | report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl |
---|
| 309 | << " +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl; |
---|
[511] | 310 | totalBuf += itMap->second; |
---|
| 311 | } |
---|
[595] | 312 | report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl; |
---|
[511] | 313 | |
---|
[595] | 314 | releaseBuffers(); |
---|
[300] | 315 | } |
---|
[509] | 316 | } |
---|