[3408] | 1 | #include "context_server.hpp" |
---|
| 2 | #include "buffer_in.hpp" |
---|
| 3 | #include "type.hpp" |
---|
| 4 | #include "context.hpp" |
---|
| 5 | #include "object_template_impl.hpp" |
---|
| 6 | #include "group_template_impl.hpp" |
---|
| 7 | #include "attribute_template_impl.hpp" |
---|
| 8 | #include "domain.hpp" |
---|
| 9 | |
---|
| 10 | #include <mpi.h> |
---|
| 11 | #include "tracer.hpp" |
---|
| 12 | #include "timer.hpp" |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | namespace xios |
---|
| 17 | { |
---|
| 18 | |
---|
| 19 | CContextServer::CContextServer(CContext* parent,MPI_Comm intraComm_,MPI_Comm interComm_) |
---|
| 20 | { |
---|
| 21 | context=parent ; |
---|
| 22 | intraComm=intraComm_ ; |
---|
| 23 | MPI_Comm_size(intraComm,&intraCommSize) ; |
---|
| 24 | MPI_Comm_rank(intraComm,&intraCommRank) ; |
---|
| 25 | interComm=interComm_ ; |
---|
| 26 | int flag ; |
---|
| 27 | MPI_Comm_test_inter(interComm,&flag) ; |
---|
| 28 | if (flag) MPI_Comm_remote_size(interComm,&commSize); |
---|
| 29 | else MPI_Comm_size(interComm,&commSize) ; |
---|
| 30 | currentTimeLine=0 ; |
---|
| 31 | finished=false ; |
---|
| 32 | } |
---|
| 33 | void CContextServer::setPendingEvent(void) |
---|
| 34 | { |
---|
| 35 | pendingEvent=true ; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | bool CContextServer::hasPendingEvent(void) |
---|
| 39 | { |
---|
| 40 | return pendingEvent ; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | bool CContextServer::eventLoop(void) |
---|
| 44 | { |
---|
| 45 | listen() ; |
---|
| 46 | checkPendingRequest() ; |
---|
| 47 | processEvents() ; |
---|
| 48 | return finished ; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void CContextServer::listen(void) |
---|
| 52 | { |
---|
| 53 | int rank; |
---|
| 54 | int flag ; |
---|
| 55 | int count ; |
---|
| 56 | char * addr ; |
---|
| 57 | MPI_Status status; |
---|
| 58 | map<int,CServerBuffer*>::iterator it; |
---|
| 59 | |
---|
| 60 | for(rank=0;rank<commSize;rank++) |
---|
| 61 | { |
---|
| 62 | if (pendingRequest.find(rank)==pendingRequest.end()) |
---|
| 63 | { |
---|
| 64 | traceOff() ; |
---|
| 65 | MPI_Iprobe(rank,20,interComm,&flag,&status); |
---|
| 66 | traceOn() ; |
---|
| 67 | if (flag==true) |
---|
| 68 | { |
---|
| 69 | it=buffers.find(rank) ; |
---|
| 70 | if (it==buffers.end()) |
---|
| 71 | it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer))).first ; |
---|
| 72 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 73 | if (it->second->isBufferFree(count)) |
---|
| 74 | { |
---|
| 75 | addr=(char*)it->second->getBuffer(count) ; |
---|
| 76 | MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]) ; |
---|
| 77 | bufferRequest[rank]=addr ; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void CContextServer::checkPendingRequest(void) |
---|
| 85 | { |
---|
| 86 | map<int,MPI_Request>::iterator it; |
---|
| 87 | list<int> recvRequest ; |
---|
| 88 | list<int>::iterator itRecv; |
---|
| 89 | int rank ; |
---|
| 90 | int flag ; |
---|
| 91 | int count ; |
---|
| 92 | MPI_Status status ; |
---|
| 93 | |
---|
| 94 | for(it=pendingRequest.begin();it!=pendingRequest.end();it++) |
---|
| 95 | { |
---|
| 96 | rank=it->first ; |
---|
| 97 | traceOff() ; |
---|
| 98 | MPI_Test(& it->second, &flag, &status) ; |
---|
| 99 | traceOn() ; |
---|
| 100 | if (flag==true) |
---|
| 101 | { |
---|
| 102 | recvRequest.push_back(rank) ; |
---|
| 103 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 104 | processRequest(rank,bufferRequest[rank],count) ; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++) |
---|
| 109 | { |
---|
| 110 | pendingRequest.erase(*itRecv) ; |
---|
| 111 | bufferRequest.erase(*itRecv) ; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void CContextServer::processRequest(int rank, char* buff,int count) |
---|
| 116 | { |
---|
| 117 | |
---|
| 118 | CBufferIn buffer(buff,count) ; |
---|
| 119 | char* startBuffer,endBuffer ; |
---|
| 120 | int size, offset ; |
---|
| 121 | size_t timeLine ; |
---|
| 122 | map<size_t,CEventServer*>::iterator it ; |
---|
| 123 | |
---|
| 124 | while(count>0) |
---|
| 125 | { |
---|
| 126 | char* startBuffer=(char*)buffer.ptr() ; |
---|
| 127 | CBufferIn newBuffer(startBuffer,buffer.remain()) ; |
---|
| 128 | newBuffer>>size>>timeLine ; |
---|
| 129 | |
---|
| 130 | it=events.find(timeLine) ; |
---|
| 131 | if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first ; |
---|
| 132 | it->second->push(rank,buffers[rank],startBuffer,size) ; |
---|
| 133 | |
---|
| 134 | buffer.advance(size) ; |
---|
| 135 | count=buffer.remain() ; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void CContextServer::processEvents(void) |
---|
| 141 | { |
---|
| 142 | map<size_t,CEventServer*>::iterator it ; |
---|
| 143 | CEventServer* event ; |
---|
| 144 | |
---|
| 145 | it=events.find(currentTimeLine) ; |
---|
| 146 | if (it!=events.end()) |
---|
| 147 | { |
---|
| 148 | event=it->second ; |
---|
| 149 | if (event->isFull()) |
---|
| 150 | { |
---|
| 151 | CTimer::get("Process events").resume() ; |
---|
| 152 | dispatchEvent(*event) ; |
---|
| 153 | CTimer::get("Process events").suspend() ; |
---|
| 154 | pendingEvent=false ; |
---|
| 155 | delete event ; |
---|
| 156 | events.erase(it) ; |
---|
| 157 | currentTimeLine++ ; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | CContextServer::~CContextServer() |
---|
| 163 | { |
---|
| 164 | map<int,CServerBuffer*>::iterator it ; |
---|
| 165 | for(it=buffers.begin();it!=buffers.end();++it) delete it->second ; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | void CContextServer::dispatchEvent(CEventServer& event) |
---|
| 170 | { |
---|
| 171 | string contextName ; |
---|
| 172 | string buff ; |
---|
| 173 | int MsgSize ; |
---|
| 174 | int rank ; |
---|
| 175 | list<CEventServer::SSubEvent>::iterator it ; |
---|
| 176 | CContext::setCurrent(context->getId()) ; |
---|
| 177 | |
---|
| 178 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
| 179 | { |
---|
| 180 | info(20)<<"Server Side context <"<<context->getId()<<"> finalized"<<endl ; |
---|
| 181 | context->finalize() ; |
---|
| 182 | finished=true ; |
---|
| 183 | } |
---|
| 184 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event) ; |
---|
| 185 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event) ; |
---|
| 186 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event) ; |
---|
| 187 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event) ; |
---|
| 188 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event) ; |
---|
| 189 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event) ; |
---|
| 190 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event) ; |
---|
| 191 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event) ; |
---|
| 192 | else if (event.classId==CField::GetType()) CField::dispatchEvent(event) ; |
---|
| 193 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event) ; |
---|
| 194 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event) ; |
---|
| 195 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event) ; |
---|
| 196 | else |
---|
| 197 | { |
---|
| 198 | ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl) ; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | } |
---|