[300] | 1 | #include "context_server.hpp" |
---|
| 2 | #include "buffer_in.hpp" |
---|
| 3 | #include "type.hpp" |
---|
| 4 | #include "context.hpp" |
---|
[352] | 5 | #include "object_template.hpp" |
---|
| 6 | #include "group_template.hpp" |
---|
| 7 | #include "attribute_template.hpp" |
---|
[300] | 8 | #include "domain.hpp" |
---|
[352] | 9 | #include "field.hpp" |
---|
| 10 | #include "file.hpp" |
---|
| 11 | #include "grid.hpp" |
---|
[1328] | 12 | #include "mpi.hpp" |
---|
[347] | 13 | #include "tracer.hpp" |
---|
| 14 | #include "timer.hpp" |
---|
[401] | 15 | #include "cxios.hpp" |
---|
[492] | 16 | #include "event_scheduler.hpp" |
---|
| 17 | #include "server.hpp" |
---|
| 18 | #include <boost/functional/hash.hpp> |
---|
[300] | 19 | |
---|
[1328] | 20 | using namespace ep_lib; |
---|
[300] | 21 | |
---|
[335] | 22 | namespace xios |
---|
[300] | 23 | { |
---|
| 24 | |
---|
[1328] | 25 | CContextServer::CContextServer(CContext* parent,MPI_Comm intraComm_,MPI_Comm interComm_) |
---|
[300] | 26 | { |
---|
[549] | 27 | context=parent; |
---|
| 28 | intraComm=intraComm_; |
---|
| 29 | MPI_Comm_size(intraComm,&intraCommSize); |
---|
| 30 | MPI_Comm_rank(intraComm,&intraCommRank); |
---|
[1460] | 31 | |
---|
[549] | 32 | interComm=interComm_; |
---|
| 33 | int flag; |
---|
| 34 | MPI_Comm_test_inter(interComm,&flag); |
---|
[300] | 35 | if (flag) MPI_Comm_remote_size(interComm,&commSize); |
---|
[549] | 36 | else MPI_Comm_size(interComm,&commSize); |
---|
[1460] | 37 | |
---|
[549] | 38 | currentTimeLine=0; |
---|
| 39 | scheduled=false; |
---|
| 40 | finished=false; |
---|
| 41 | boost::hash<string> hashString; |
---|
[1460] | 42 | if (CServer::serverLevel == 1) |
---|
| 43 | hashId=hashString(context->getId() + boost::lexical_cast<string>(context->clientPrimServer.size())); |
---|
| 44 | else |
---|
| 45 | hashId=hashString(context->getId()); |
---|
| 46 | } |
---|
[492] | 47 | |
---|
[300] | 48 | void CContextServer::setPendingEvent(void) |
---|
| 49 | { |
---|
[549] | 50 | pendingEvent=true; |
---|
[300] | 51 | } |
---|
[489] | 52 | |
---|
[300] | 53 | bool CContextServer::hasPendingEvent(void) |
---|
| 54 | { |
---|
[549] | 55 | return pendingEvent; |
---|
[300] | 56 | } |
---|
[489] | 57 | |
---|
[597] | 58 | bool CContextServer::hasFinished(void) |
---|
| 59 | { |
---|
| 60 | return finished; |
---|
| 61 | } |
---|
| 62 | |
---|
[1033] | 63 | bool CContextServer::eventLoop(bool enableEventsProcessing /*= true*/) |
---|
[300] | 64 | { |
---|
[549] | 65 | listen(); |
---|
| 66 | checkPendingRequest(); |
---|
[1033] | 67 | if (enableEventsProcessing) |
---|
| 68 | processEvents(); |
---|
[549] | 69 | return finished; |
---|
[300] | 70 | } |
---|
| 71 | |
---|
| 72 | void CContextServer::listen(void) |
---|
| 73 | { |
---|
| 74 | int rank; |
---|
[549] | 75 | int flag; |
---|
| 76 | int count; |
---|
| 77 | char * addr; |
---|
[1328] | 78 | MPI_Status status; |
---|
[300] | 79 | map<int,CServerBuffer*>::iterator it; |
---|
[1328] | 80 | bool okLoop; |
---|
[489] | 81 | |
---|
[1328] | 82 | traceOff(); |
---|
| 83 | MPI_Iprobe(-2, 20,interComm,&flag,&status); |
---|
| 84 | traceOn(); |
---|
| 85 | |
---|
| 86 | if (flag==true) |
---|
[300] | 87 | { |
---|
[1328] | 88 | #ifdef _usingMPI |
---|
| 89 | rank=status.MPI_SOURCE ; |
---|
| 90 | #elif _usingEP |
---|
| 91 | rank=status.ep_src ; |
---|
| 92 | #endif |
---|
| 93 | okLoop = true; |
---|
[300] | 94 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
[1328] | 95 | okLoop = !listenPendingRequest(status) ; |
---|
| 96 | if (okLoop) |
---|
[300] | 97 | { |
---|
[1328] | 98 | for(rank=0;rank<commSize;rank++) |
---|
[300] | 99 | { |
---|
[1328] | 100 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
[300] | 101 | { |
---|
[1328] | 102 | |
---|
| 103 | traceOff(); |
---|
| 104 | MPI_Iprobe(rank, 20,interComm,&flag,&status); |
---|
| 105 | traceOn(); |
---|
| 106 | if (flag==true) listenPendingRequest(status) ; |
---|
[300] | 107 | } |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | } |
---|
[489] | 112 | |
---|
[1328] | 113 | bool CContextServer::listenPendingRequest(MPI_Status& status) |
---|
| 114 | { |
---|
| 115 | int count; |
---|
| 116 | char * addr; |
---|
| 117 | map<int,CServerBuffer*>::iterator it; |
---|
| 118 | #ifdef _usingMPI |
---|
| 119 | int rank=status.MPI_SOURCE ; |
---|
| 120 | #elif _usingEP |
---|
| 121 | int rank=status.ep_src; |
---|
[1538] | 122 | #endif |
---|
[1328] | 123 | |
---|
| 124 | it=buffers.find(rank); |
---|
| 125 | if (it==buffers.end()) // Receive the buffer size and allocate the buffer |
---|
| 126 | { |
---|
| 127 | StdSize buffSize = 0; |
---|
[1520] | 128 | MPI_Request request; |
---|
[1538] | 129 | |
---|
[1520] | 130 | MPI_Irecv(&buffSize, 1, MPI_LONG, rank, 20, interComm, &request); |
---|
| 131 | MPI_Wait(&request, &status); |
---|
[1328] | 132 | mapBufferSize_.insert(std::make_pair(rank, buffSize)); |
---|
| 133 | it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(buffSize)))).first; |
---|
| 134 | return true; |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
| 139 | if (it->second->isBufferFree(count)) |
---|
| 140 | { |
---|
| 141 | addr=(char*)it->second->getBuffer(count); |
---|
| 142 | MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]); |
---|
| 143 | bufferRequest[rank]=addr; |
---|
| 144 | return true; |
---|
| 145 | } |
---|
| 146 | else |
---|
| 147 | return false; |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
[1460] | 151 | |
---|
[300] | 152 | void CContextServer::checkPendingRequest(void) |
---|
| 153 | { |
---|
[1328] | 154 | map<int,MPI_Request>::iterator it; |
---|
[549] | 155 | list<int> recvRequest; |
---|
[300] | 156 | list<int>::iterator itRecv; |
---|
[549] | 157 | int rank; |
---|
| 158 | int flag; |
---|
| 159 | int count; |
---|
[1328] | 160 | MPI_Status status; |
---|
[489] | 161 | |
---|
[1328] | 162 | for(it=pendingRequest.begin();it!=pendingRequest.end();it++) |
---|
[300] | 163 | { |
---|
[549] | 164 | rank=it->first; |
---|
| 165 | traceOff(); |
---|
[1328] | 166 | MPI_Test(& it->second, &flag, &status); |
---|
[549] | 167 | traceOn(); |
---|
[300] | 168 | if (flag==true) |
---|
| 169 | { |
---|
[549] | 170 | recvRequest.push_back(rank); |
---|
[1328] | 171 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
[549] | 172 | processRequest(rank,bufferRequest[rank],count); |
---|
[300] | 173 | } |
---|
| 174 | } |
---|
[489] | 175 | |
---|
| 176 | for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++) |
---|
[300] | 177 | { |
---|
[549] | 178 | pendingRequest.erase(*itRecv); |
---|
| 179 | bufferRequest.erase(*itRecv); |
---|
[300] | 180 | } |
---|
| 181 | } |
---|
[489] | 182 | |
---|
[300] | 183 | void CContextServer::processRequest(int rank, char* buff,int count) |
---|
| 184 | { |
---|
[489] | 185 | |
---|
[549] | 186 | CBufferIn buffer(buff,count); |
---|
| 187 | char* startBuffer,endBuffer; |
---|
| 188 | int size, offset; |
---|
| 189 | size_t timeLine; |
---|
| 190 | map<size_t,CEventServer*>::iterator it; |
---|
[489] | 191 | |
---|
[1460] | 192 | CTimer::get("Process request").resume(); |
---|
[300] | 193 | while(count>0) |
---|
| 194 | { |
---|
[549] | 195 | char* startBuffer=(char*)buffer.ptr(); |
---|
| 196 | CBufferIn newBuffer(startBuffer,buffer.remain()); |
---|
| 197 | newBuffer>>size>>timeLine; |
---|
[300] | 198 | |
---|
[549] | 199 | it=events.find(timeLine); |
---|
| 200 | if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first; |
---|
| 201 | it->second->push(rank,buffers[rank],startBuffer,size); |
---|
[300] | 202 | |
---|
[549] | 203 | buffer.advance(size); |
---|
| 204 | count=buffer.remain(); |
---|
[489] | 205 | } |
---|
[1460] | 206 | CTimer::get("Process request").suspend(); |
---|
[300] | 207 | } |
---|
[489] | 208 | |
---|
[300] | 209 | void CContextServer::processEvents(void) |
---|
| 210 | { |
---|
[549] | 211 | map<size_t,CEventServer*>::iterator it; |
---|
| 212 | CEventServer* event; |
---|
[489] | 213 | |
---|
[549] | 214 | it=events.find(currentTimeLine); |
---|
[489] | 215 | if (it!=events.end()) |
---|
[300] | 216 | { |
---|
[549] | 217 | event=it->second; |
---|
[509] | 218 | |
---|
[300] | 219 | if (event->isFull()) |
---|
| 220 | { |
---|
[597] | 221 | if (!scheduled && CServer::eventScheduler) // Skip event scheduling for attached mode and reception on client side |
---|
[492] | 222 | { |
---|
[549] | 223 | CServer::eventScheduler->registerEvent(currentTimeLine,hashId); |
---|
| 224 | scheduled=true; |
---|
[492] | 225 | } |
---|
[597] | 226 | else if (!CServer::eventScheduler || CServer::eventScheduler->queryEvent(currentTimeLine,hashId) ) |
---|
[492] | 227 | { |
---|
[851] | 228 | // When using attached mode, synchronise the processes to avoid that differents event be scheduled by differents processes |
---|
| 229 | // The best way to properly solve this problem will be to use the event scheduler also in attached mode |
---|
| 230 | // for now just set up a MPI barrier |
---|
[998] | 231 | if (!CServer::eventScheduler && CXios::isServer) MPI_Barrier(intraComm) ; |
---|
[851] | 232 | |
---|
[549] | 233 | CTimer::get("Process events").resume(); |
---|
| 234 | dispatchEvent(*event); |
---|
| 235 | CTimer::get("Process events").suspend(); |
---|
| 236 | pendingEvent=false; |
---|
| 237 | delete event; |
---|
| 238 | events.erase(it); |
---|
| 239 | currentTimeLine++; |
---|
| 240 | scheduled = false; |
---|
[492] | 241 | } |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | } |
---|
[489] | 245 | |
---|
[300] | 246 | CContextServer::~CContextServer() |
---|
| 247 | { |
---|
[549] | 248 | map<int,CServerBuffer*>::iterator it; |
---|
| 249 | for(it=buffers.begin();it!=buffers.end();++it) delete it->second; |
---|
[489] | 250 | } |
---|
[300] | 251 | |
---|
| 252 | void CContextServer::dispatchEvent(CEventServer& event) |
---|
| 253 | { |
---|
[549] | 254 | string contextName; |
---|
| 255 | string buff; |
---|
| 256 | int MsgSize; |
---|
| 257 | int rank; |
---|
| 258 | list<CEventServer::SSubEvent>::iterator it; |
---|
[1460] | 259 | StdString ctxId = context->getId(); |
---|
| 260 | CContext::setCurrent(ctxId); |
---|
| 261 | StdSize totalBuf = 0; |
---|
[489] | 262 | |
---|
[300] | 263 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
| 264 | { |
---|
[597] | 265 | finished=true; |
---|
[1342] | 266 | #pragma omp critical (_output) |
---|
[1460] | 267 | info(20)<<" CContextServer: Receive context <"<<context->getId()<<"> finalize."<<endl; |
---|
| 268 | context->finalize(); |
---|
[511] | 269 | std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
[1460] | 270 | iteMap = mapBufferSize_.end(), itMap; |
---|
[511] | 271 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
| 272 | { |
---|
[1460] | 273 | rank = itMap->first; |
---|
| 274 | #pragma omp critical (_output) |
---|
| 275 | report(10)<< " Memory report : Context <"<<ctxId<<"> : server side : memory used for buffer of each connection to client" << endl |
---|
| 276 | << " +) With client of rank " << rank << " : " << itMap->second << " bytes " << endl; |
---|
[511] | 277 | totalBuf += itMap->second; |
---|
| 278 | } |
---|
[1460] | 279 | #pragma omp critical (_output) |
---|
| 280 | report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl; |
---|
[300] | 281 | } |
---|
[549] | 282 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event); |
---|
| 283 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event); |
---|
| 284 | else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event); |
---|
| 285 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event); |
---|
| 286 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event); |
---|
| 287 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event); |
---|
| 288 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event); |
---|
[887] | 289 | else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event); |
---|
| 290 | else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event); |
---|
[549] | 291 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event); |
---|
| 292 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event); |
---|
| 293 | else if (event.classId==CField::GetType()) CField::dispatchEvent(event); |
---|
| 294 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event); |
---|
| 295 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event); |
---|
| 296 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event); |
---|
| 297 | else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event); |
---|
[300] | 298 | else |
---|
| 299 | { |
---|
[549] | 300 | ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl); |
---|
[300] | 301 | } |
---|
| 302 | } |
---|
| 303 | } |
---|