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