[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 | { |
---|
| 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) ; |
---|
| 34 | if (flag) MPI_Comm_remote_size(interComm,&commSize); |
---|
| 35 | else MPI_Comm_size(interComm,&commSize) ; |
---|
| 36 | currentTimeLine=0 ; |
---|
[492] | 37 | scheduled=false ; |
---|
[300] | 38 | finished=false ; |
---|
[509] | 39 | |
---|
[492] | 40 | boost::hash<string> hashString ; |
---|
| 41 | hashId=hashString(context->getId()) ; |
---|
| 42 | |
---|
[300] | 43 | } |
---|
| 44 | void CContextServer::setPendingEvent(void) |
---|
| 45 | { |
---|
| 46 | pendingEvent=true ; |
---|
| 47 | } |
---|
[489] | 48 | |
---|
[300] | 49 | bool CContextServer::hasPendingEvent(void) |
---|
| 50 | { |
---|
| 51 | return pendingEvent ; |
---|
| 52 | } |
---|
[489] | 53 | |
---|
[300] | 54 | bool CContextServer::eventLoop(void) |
---|
| 55 | { |
---|
| 56 | listen() ; |
---|
| 57 | checkPendingRequest() ; |
---|
| 58 | processEvents() ; |
---|
| 59 | return finished ; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | void CContextServer::listen(void) |
---|
| 63 | { |
---|
| 64 | int rank; |
---|
| 65 | int flag ; |
---|
| 66 | int count ; |
---|
| 67 | char * addr ; |
---|
[489] | 68 | MPI_Status status; |
---|
[300] | 69 | map<int,CServerBuffer*>::iterator it; |
---|
[489] | 70 | |
---|
[300] | 71 | for(rank=0;rank<commSize;rank++) |
---|
| 72 | { |
---|
| 73 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
| 74 | { |
---|
[347] | 75 | traceOff() ; |
---|
[489] | 76 | MPI_Iprobe(rank,20,interComm,&flag,&status); |
---|
[347] | 77 | traceOn() ; |
---|
[300] | 78 | if (flag==true) |
---|
| 79 | { |
---|
| 80 | it=buffers.find(rank) ; |
---|
[489] | 81 | if (it==buffers.end()) |
---|
[300] | 82 | { |
---|
[509] | 83 | StdSize buffSize = 0; |
---|
| 84 | MPI_Recv(&buffSize, 1, MPI_LONG, rank, 20, interComm, &status); |
---|
| 85 | it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(buffSize)))).first ; |
---|
[300] | 86 | } |
---|
[509] | 87 | else |
---|
| 88 | { |
---|
| 89 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 90 | if (it->second->isBufferFree(count)) |
---|
| 91 | { |
---|
| 92 | addr=(char*)it->second->getBuffer(count) ; |
---|
| 93 | MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]) ; |
---|
| 94 | bufferRequest[rank]=addr ; |
---|
| 95 | } |
---|
| 96 | } |
---|
[300] | 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | } |
---|
[489] | 101 | |
---|
[300] | 102 | void CContextServer::checkPendingRequest(void) |
---|
| 103 | { |
---|
| 104 | map<int,MPI_Request>::iterator it; |
---|
| 105 | list<int> recvRequest ; |
---|
| 106 | list<int>::iterator itRecv; |
---|
| 107 | int rank ; |
---|
| 108 | int flag ; |
---|
| 109 | int count ; |
---|
| 110 | MPI_Status status ; |
---|
[489] | 111 | |
---|
[300] | 112 | for(it=pendingRequest.begin();it!=pendingRequest.end();it++) |
---|
| 113 | { |
---|
| 114 | rank=it->first ; |
---|
[347] | 115 | traceOff() ; |
---|
[300] | 116 | MPI_Test(& it->second, &flag, &status) ; |
---|
[347] | 117 | traceOn() ; |
---|
[300] | 118 | if (flag==true) |
---|
| 119 | { |
---|
| 120 | recvRequest.push_back(rank) ; |
---|
| 121 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 122 | processRequest(rank,bufferRequest[rank],count) ; |
---|
| 123 | } |
---|
| 124 | } |
---|
[489] | 125 | |
---|
| 126 | for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++) |
---|
[300] | 127 | { |
---|
| 128 | pendingRequest.erase(*itRecv) ; |
---|
| 129 | bufferRequest.erase(*itRecv) ; |
---|
| 130 | } |
---|
| 131 | } |
---|
[489] | 132 | |
---|
[300] | 133 | void CContextServer::processRequest(int rank, char* buff,int count) |
---|
| 134 | { |
---|
[489] | 135 | |
---|
[300] | 136 | CBufferIn buffer(buff,count) ; |
---|
| 137 | char* startBuffer,endBuffer ; |
---|
| 138 | int size, offset ; |
---|
| 139 | size_t timeLine ; |
---|
| 140 | map<size_t,CEventServer*>::iterator it ; |
---|
[489] | 141 | |
---|
[300] | 142 | while(count>0) |
---|
| 143 | { |
---|
| 144 | char* startBuffer=(char*)buffer.ptr() ; |
---|
| 145 | CBufferIn newBuffer(startBuffer,buffer.remain()) ; |
---|
| 146 | newBuffer>>size>>timeLine ; |
---|
| 147 | |
---|
| 148 | it=events.find(timeLine) ; |
---|
| 149 | if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first ; |
---|
| 150 | it->second->push(rank,buffers[rank],startBuffer,size) ; |
---|
| 151 | |
---|
| 152 | buffer.advance(size) ; |
---|
[489] | 153 | count=buffer.remain() ; |
---|
| 154 | } |
---|
| 155 | |
---|
[300] | 156 | } |
---|
[489] | 157 | |
---|
[300] | 158 | void CContextServer::processEvents(void) |
---|
| 159 | { |
---|
| 160 | map<size_t,CEventServer*>::iterator it ; |
---|
| 161 | CEventServer* event ; |
---|
[489] | 162 | |
---|
[300] | 163 | it=events.find(currentTimeLine) ; |
---|
[489] | 164 | if (it!=events.end()) |
---|
[300] | 165 | { |
---|
| 166 | event=it->second ; |
---|
[509] | 167 | |
---|
[300] | 168 | if (event->isFull()) |
---|
| 169 | { |
---|
[492] | 170 | if (!scheduled && !CXios::isServer) |
---|
| 171 | { |
---|
[509] | 172 | CServer::eventScheduler->registerEvent(currentTimeLine,hashId) ; |
---|
[492] | 173 | scheduled=true ; |
---|
| 174 | } |
---|
[509] | 175 | else if (CXios::isServer || CServer::eventScheduler->queryEvent(currentTimeLine,hashId) ) |
---|
[492] | 176 | { |
---|
[347] | 177 | CTimer::get("Process events").resume() ; |
---|
[300] | 178 | dispatchEvent(*event) ; |
---|
[347] | 179 | CTimer::get("Process events").suspend() ; |
---|
[300] | 180 | pendingEvent=false ; |
---|
| 181 | delete event ; |
---|
| 182 | events.erase(it) ; |
---|
| 183 | currentTimeLine++ ; |
---|
[492] | 184 | scheduled = false ; |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | } |
---|
[489] | 189 | |
---|
[300] | 190 | CContextServer::~CContextServer() |
---|
| 191 | { |
---|
| 192 | map<int,CServerBuffer*>::iterator it ; |
---|
[489] | 193 | for(it=buffers.begin();it!=buffers.end();++it) delete it->second ; |
---|
| 194 | } |
---|
[300] | 195 | |
---|
| 196 | |
---|
| 197 | void CContextServer::dispatchEvent(CEventServer& event) |
---|
| 198 | { |
---|
| 199 | string contextName ; |
---|
| 200 | string buff ; |
---|
| 201 | int MsgSize ; |
---|
| 202 | int rank ; |
---|
| 203 | list<CEventServer::SSubEvent>::iterator it ; |
---|
[346] | 204 | CContext::setCurrent(context->getId()) ; |
---|
[489] | 205 | |
---|
[300] | 206 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
| 207 | { |
---|
| 208 | info(20)<<"Server Side context <"<<context->getId()<<"> finalized"<<endl ; |
---|
| 209 | context->finalize() ; |
---|
| 210 | finished=true ; |
---|
[401] | 211 | report(0)<< " Memory report : Context <"<<context->getId()<<"> : server side : total memory used for buffer "<<buffers.size()*CXios::bufferSize<<" bytes"<<endl ; |
---|
[300] | 212 | } |
---|
| 213 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event) ; |
---|
| 214 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event) ; |
---|
| 215 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event) ; |
---|
| 216 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event) ; |
---|
| 217 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event) ; |
---|
| 218 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event) ; |
---|
| 219 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event) ; |
---|
| 220 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event) ; |
---|
| 221 | else if (event.classId==CField::GetType()) CField::dispatchEvent(event) ; |
---|
| 222 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event) ; |
---|
| 223 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event) ; |
---|
| 224 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event) ; |
---|
[489] | 225 | else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event) ; |
---|
[300] | 226 | else |
---|
| 227 | { |
---|
| 228 | ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl) ; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | } |
---|