[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 | { |
---|
[1054] | 24 | StdSize CContextServer::totalBuf_ = 0; |
---|
[300] | 25 | |
---|
[992] | 26 | CContextServer::CContextServer(CContext* parent, MPI_Comm intraComm_,MPI_Comm interComm_) |
---|
[300] | 27 | { |
---|
[549] | 28 | context=parent; |
---|
| 29 | intraComm=intraComm_; |
---|
| 30 | MPI_Comm_size(intraComm,&intraCommSize); |
---|
| 31 | MPI_Comm_rank(intraComm,&intraCommRank); |
---|
[1054] | 32 | |
---|
[549] | 33 | interComm=interComm_; |
---|
| 34 | int flag; |
---|
| 35 | MPI_Comm_test_inter(interComm,&flag); |
---|
[300] | 36 | if (flag) MPI_Comm_remote_size(interComm,&commSize); |
---|
[549] | 37 | else MPI_Comm_size(interComm,&commSize); |
---|
[983] | 38 | |
---|
[549] | 39 | currentTimeLine=0; |
---|
| 40 | scheduled=false; |
---|
| 41 | finished=false; |
---|
| 42 | boost::hash<string> hashString; |
---|
| 43 | hashId=hashString(context->getId()); |
---|
[300] | 44 | } |
---|
[992] | 45 | |
---|
[300] | 46 | void CContextServer::setPendingEvent(void) |
---|
| 47 | { |
---|
[549] | 48 | pendingEvent=true; |
---|
[300] | 49 | } |
---|
[489] | 50 | |
---|
[1054] | 51 | |
---|
[300] | 52 | bool CContextServer::hasPendingEvent(void) |
---|
| 53 | { |
---|
[549] | 54 | return pendingEvent; |
---|
[300] | 55 | } |
---|
[489] | 56 | |
---|
[597] | 57 | bool CContextServer::hasFinished(void) |
---|
| 58 | { |
---|
| 59 | return finished; |
---|
| 60 | } |
---|
| 61 | |
---|
[1054] | 62 | bool CContextServer::eventLoop(bool enableEventsProcessing /*= true*/) |
---|
[300] | 63 | { |
---|
[549] | 64 | listen(); |
---|
| 65 | checkPendingRequest(); |
---|
[1054] | 66 | if (enableEventsProcessing) |
---|
| 67 | processEvents(); |
---|
[549] | 68 | return finished; |
---|
[300] | 69 | } |
---|
| 70 | |
---|
| 71 | void CContextServer::listen(void) |
---|
| 72 | { |
---|
| 73 | int rank; |
---|
[549] | 74 | int flag; |
---|
| 75 | int count; |
---|
| 76 | char * addr; |
---|
[489] | 77 | MPI_Status status; |
---|
[300] | 78 | map<int,CServerBuffer*>::iterator it; |
---|
[489] | 79 | |
---|
[300] | 80 | for(rank=0;rank<commSize;rank++) |
---|
| 81 | { |
---|
| 82 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
| 83 | { |
---|
[549] | 84 | traceOff(); |
---|
[489] | 85 | MPI_Iprobe(rank,20,interComm,&flag,&status); |
---|
[549] | 86 | traceOn(); |
---|
[300] | 87 | if (flag==true) |
---|
| 88 | { |
---|
[549] | 89 | it=buffers.find(rank); |
---|
[597] | 90 | if (it==buffers.end()) // Receive the buffer size and allocate the buffer |
---|
[300] | 91 | { |
---|
[509] | 92 | StdSize buffSize = 0; |
---|
| 93 | MPI_Recv(&buffSize, 1, MPI_LONG, rank, 20, interComm, &status); |
---|
[511] | 94 | mapBufferSize_.insert(std::make_pair(rank, buffSize)); |
---|
[549] | 95 | it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(buffSize)))).first; |
---|
[300] | 96 | } |
---|
[509] | 97 | else |
---|
| 98 | { |
---|
[549] | 99 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
[509] | 100 | if (it->second->isBufferFree(count)) |
---|
| 101 | { |
---|
[549] | 102 | addr=(char*)it->second->getBuffer(count); |
---|
| 103 | MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]); |
---|
| 104 | bufferRequest[rank]=addr; |
---|
[509] | 105 | } |
---|
| 106 | } |
---|
[300] | 107 | } |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | } |
---|
[489] | 111 | |
---|
[300] | 112 | void CContextServer::checkPendingRequest(void) |
---|
| 113 | { |
---|
| 114 | map<int,MPI_Request>::iterator it; |
---|
[549] | 115 | list<int> recvRequest; |
---|
[300] | 116 | list<int>::iterator itRecv; |
---|
[549] | 117 | int rank; |
---|
| 118 | int flag; |
---|
| 119 | int count; |
---|
| 120 | MPI_Status status; |
---|
[489] | 121 | |
---|
[300] | 122 | for(it=pendingRequest.begin();it!=pendingRequest.end();it++) |
---|
| 123 | { |
---|
[549] | 124 | rank=it->first; |
---|
| 125 | traceOff(); |
---|
| 126 | MPI_Test(& it->second, &flag, &status); |
---|
| 127 | traceOn(); |
---|
[300] | 128 | if (flag==true) |
---|
| 129 | { |
---|
[549] | 130 | recvRequest.push_back(rank); |
---|
| 131 | MPI_Get_count(&status,MPI_CHAR,&count); |
---|
| 132 | processRequest(rank,bufferRequest[rank],count); |
---|
[300] | 133 | } |
---|
| 134 | } |
---|
[489] | 135 | |
---|
| 136 | for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++) |
---|
[300] | 137 | { |
---|
[549] | 138 | pendingRequest.erase(*itRecv); |
---|
| 139 | bufferRequest.erase(*itRecv); |
---|
[300] | 140 | } |
---|
| 141 | } |
---|
[489] | 142 | |
---|
[300] | 143 | void CContextServer::processRequest(int rank, char* buff,int count) |
---|
| 144 | { |
---|
[489] | 145 | |
---|
[549] | 146 | CBufferIn buffer(buff,count); |
---|
| 147 | char* startBuffer,endBuffer; |
---|
| 148 | int size, offset; |
---|
| 149 | size_t timeLine; |
---|
| 150 | map<size_t,CEventServer*>::iterator it; |
---|
[489] | 151 | |
---|
[300] | 152 | while(count>0) |
---|
| 153 | { |
---|
[549] | 154 | char* startBuffer=(char*)buffer.ptr(); |
---|
| 155 | CBufferIn newBuffer(startBuffer,buffer.remain()); |
---|
| 156 | newBuffer>>size>>timeLine; |
---|
[300] | 157 | |
---|
[549] | 158 | it=events.find(timeLine); |
---|
| 159 | if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first; |
---|
| 160 | it->second->push(rank,buffers[rank],startBuffer,size); |
---|
[300] | 161 | |
---|
[549] | 162 | buffer.advance(size); |
---|
| 163 | count=buffer.remain(); |
---|
[489] | 164 | } |
---|
[300] | 165 | } |
---|
[489] | 166 | |
---|
[300] | 167 | void CContextServer::processEvents(void) |
---|
| 168 | { |
---|
[549] | 169 | map<size_t,CEventServer*>::iterator it; |
---|
| 170 | CEventServer* event; |
---|
[1054] | 171 | boost::hash<string> hashString; |
---|
| 172 | size_t hashId=hashString(context->getId()); |
---|
[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 |
---|
[992] | 191 | // if (!CServer::eventScheduler) 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 | // CContext::setCurrent(context->getId()); |
---|
| 221 | StdString ctxId = context->getId(); |
---|
| 222 | CContext::setCurrent(ctxId); |
---|
[489] | 223 | |
---|
[300] | 224 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
| 225 | { |
---|
[597] | 226 | finished=true; |
---|
[1071] | 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; |
---|
[1071] | 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; |
---|
[1054] | 235 | totalBuf_ += itMap->second; |
---|
[511] | 236 | } |
---|
[549] | 237 | context->finalize(); |
---|
[1054] | 238 | |
---|
[1071] | 239 | // report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl; // moved to CContext::finalize() |
---|
[300] | 240 | } |
---|
[549] | 241 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event); |
---|
| 242 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event); |
---|
| 243 | else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event); |
---|
| 244 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event); |
---|
| 245 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event); |
---|
| 246 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event); |
---|
| 247 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event); |
---|
[887] | 248 | else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event); |
---|
| 249 | else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event); |
---|
[549] | 250 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event); |
---|
| 251 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event); |
---|
| 252 | else if (event.classId==CField::GetType()) CField::dispatchEvent(event); |
---|
| 253 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event); |
---|
| 254 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event); |
---|
| 255 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event); |
---|
| 256 | else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event); |
---|
[300] | 257 | else |
---|
| 258 | { |
---|
[549] | 259 | ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl); |
---|
[300] | 260 | } |
---|
| 261 | } |
---|
[1054] | 262 | |
---|
| 263 | size_t CContextServer::getTotalBuf(void) |
---|
| 264 | { |
---|
| 265 | return totalBuf_; |
---|
| 266 | } |
---|
| 267 | |
---|
[300] | 268 | } |
---|