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