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