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