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