[300] | 1 | #include "xmlioserver_spl.hpp" |
---|
| 2 | #include "context_client.hpp" |
---|
| 3 | #include "context_server.hpp" |
---|
| 4 | #include "event_client.hpp" |
---|
| 5 | #include "buffer_out.hpp" |
---|
| 6 | #include "buffer_client.hpp" |
---|
| 7 | #include "type.hpp" |
---|
| 8 | #include "message.hpp" |
---|
| 9 | #include "event_client.hpp" |
---|
| 10 | #include "context.hpp" |
---|
| 11 | #include <mpi.h> |
---|
| 12 | |
---|
| 13 | namespace xmlioserver |
---|
| 14 | { |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | CContextClient::CContextClient(tree::CContext* parent,MPI_Comm intraComm_, MPI_Comm interComm_) |
---|
| 18 | { |
---|
| 19 | context=parent ; |
---|
| 20 | intraComm=intraComm_ ; |
---|
| 21 | interComm=interComm_ ; |
---|
| 22 | MPI_Comm_rank(intraComm,&clientRank) ; |
---|
| 23 | MPI_Comm_size(intraComm,&clientSize) ; |
---|
| 24 | |
---|
| 25 | int flag ; |
---|
| 26 | MPI_Comm_test_inter(interComm,&flag) ; |
---|
| 27 | if (flag) MPI_Comm_remote_size(interComm,&serverSize); |
---|
| 28 | else MPI_Comm_size(interComm,&serverSize) ; |
---|
| 29 | |
---|
| 30 | timeLine=0 ; |
---|
| 31 | |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | void CContextClient::sendEvent(CEventClient& event) |
---|
| 36 | { |
---|
| 37 | list<int>::iterator itServer ; |
---|
| 38 | list<int> ranks ; |
---|
| 39 | list<int> sizes ; |
---|
| 40 | list<int>::iterator itSize ; |
---|
| 41 | |
---|
| 42 | ranks=event.getRanks() ; |
---|
| 43 | if (! event.isEmpty()) |
---|
| 44 | { |
---|
| 45 | sizes=event.getSizes() ; |
---|
| 46 | CMessage msg ; |
---|
| 47 | |
---|
| 48 | msg<<*(sizes.begin())<<timeLine ; |
---|
| 49 | for(list<int>::iterator it=sizes.begin();it!=sizes.end();it++) *it+=msg.size() ; |
---|
| 50 | list<CBufferOut*> buffList=getBuffers(ranks,sizes) ; |
---|
| 51 | |
---|
| 52 | list<CBufferOut*>::iterator it ; |
---|
| 53 | for(it=buffList.begin(),itSize=sizes.begin();it!=buffList.end();++it,++itSize) |
---|
| 54 | { |
---|
| 55 | **it<<*itSize<<timeLine ; |
---|
| 56 | } |
---|
| 57 | event.send(buffList) ; |
---|
| 58 | checkBuffers(ranks) ; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | if (context->hasServer) waitEvent(ranks) ; |
---|
| 62 | timeLine++ ; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void CContextClient::waitEvent(list<int>& ranks) |
---|
| 66 | { |
---|
| 67 | context->server->setPendingEvent() ; |
---|
| 68 | while(checkBuffers(ranks) || context->server->hasPendingEvent()) |
---|
| 69 | { |
---|
| 70 | context->server->eventLoop() ; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | list<CBufferOut*> CContextClient::getBuffers(list<int>& serverList, list<int>& sizeList) |
---|
| 75 | { |
---|
| 76 | list<int>::iterator itServer,itSize ; |
---|
| 77 | list<CClientBuffer*> bufferList ; |
---|
| 78 | map<int,CClientBuffer*>::iterator it ; |
---|
| 79 | list<CClientBuffer*>::iterator itBuffer ; |
---|
| 80 | list<CBufferOut*> retBuffer ; |
---|
| 81 | bool free ; |
---|
| 82 | |
---|
| 83 | for(itServer=serverList.begin();itServer!=serverList.end();itServer++) |
---|
| 84 | { |
---|
| 85 | it=buffers.find(*itServer) ; |
---|
| 86 | if (it==buffers.end()) |
---|
| 87 | { |
---|
| 88 | newBuffer(*itServer) ; |
---|
| 89 | it=buffers.find(*itServer) ; |
---|
| 90 | } |
---|
| 91 | bufferList.push_back(it->second) ; |
---|
| 92 | } |
---|
| 93 | free=false ; |
---|
| 94 | while(!free) |
---|
| 95 | { |
---|
| 96 | free=true ; |
---|
| 97 | for(itBuffer=bufferList.begin(),itSize=sizeList.begin(); itBuffer!=bufferList.end();itBuffer++,itSize++) |
---|
| 98 | { |
---|
| 99 | (*itBuffer)->checkBuffer() ; |
---|
| 100 | free&=(*itBuffer)->isBufferFree(*itSize) ; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | for(itBuffer=bufferList.begin(),itSize=sizeList.begin(); itBuffer!=bufferList.end();itBuffer++,itSize++) |
---|
| 104 | { |
---|
| 105 | retBuffer.push_back((*itBuffer)->getBuffer(*itSize)) ; |
---|
| 106 | } |
---|
| 107 | return retBuffer ; |
---|
| 108 | |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | void CContextClient::newBuffer(int rank) |
---|
| 112 | { |
---|
| 113 | buffers[rank]=new CClientBuffer(interComm,rank) ; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | bool CContextClient::checkBuffers(void) |
---|
| 117 | { |
---|
| 118 | map<int,CClientBuffer*>::iterator itBuff ; |
---|
| 119 | bool pending=false ; |
---|
| 120 | for(itBuff=buffers.begin();itBuff!=buffers.end();itBuff++) pending|=itBuff->second->checkBuffer() ; |
---|
| 121 | return pending ; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | void CContextClient::releaseBuffers(void) |
---|
| 125 | { |
---|
| 126 | map<int,CClientBuffer*>::iterator itBuff ; |
---|
| 127 | for(itBuff=buffers.begin();itBuff!=buffers.end();itBuff++) delete itBuff->second ; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | bool CContextClient::checkBuffers(list<int>& ranks) |
---|
| 131 | { |
---|
| 132 | list<int>::iterator it ; |
---|
| 133 | bool pending=false ; |
---|
| 134 | for(it=ranks.begin();it!=ranks.end();it++) pending|=buffers[*it]->checkBuffer() ; |
---|
| 135 | return pending ; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | int CContextClient::getServerLeader(void) |
---|
| 139 | { |
---|
| 140 | int clientByServer=clientSize/serverSize ; |
---|
| 141 | int remain=clientSize%serverSize ; |
---|
| 142 | |
---|
| 143 | if (clientRank<(clientByServer+1)*remain) |
---|
| 144 | { |
---|
| 145 | return clientRank/(clientByServer+1) ; |
---|
| 146 | } |
---|
| 147 | else |
---|
| 148 | { |
---|
| 149 | int rank=clientRank-(clientByServer+1)*remain ; |
---|
| 150 | int nbServer=serverSize-remain ; |
---|
| 151 | return remain+rank/clientByServer ; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | bool CContextClient::isServerLeader(void) |
---|
| 156 | { |
---|
| 157 | int clientByServer=clientSize/serverSize ; |
---|
| 158 | int remain=clientSize%serverSize ; |
---|
| 159 | |
---|
| 160 | if (clientRank<(clientByServer+1)*remain) |
---|
| 161 | { |
---|
| 162 | if (clientRank%(clientByServer+1)==0) return true ; |
---|
| 163 | else return false ; |
---|
| 164 | } |
---|
| 165 | else |
---|
| 166 | { |
---|
| 167 | int rank=clientRank-(clientByServer+1)*remain ; |
---|
| 168 | int nbServer=serverSize-remain ; |
---|
| 169 | if (rank%clientByServer==0) return true ; |
---|
| 170 | else return false ; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | void CContextClient::finalize(void) |
---|
| 175 | { |
---|
| 176 | |
---|
| 177 | map<int,CClientBuffer*>::iterator itBuff ; |
---|
| 178 | bool stop=true ; |
---|
| 179 | |
---|
| 180 | CEventClient event(CContext::GetType(),CContext::EVENT_ID_CONTEXT_FINALIZE) ; |
---|
| 181 | if (isServerLeader()) |
---|
| 182 | { |
---|
| 183 | CMessage msg ; |
---|
| 184 | event.push(getServerLeader(),1,msg) ; |
---|
| 185 | } |
---|
| 186 | sendEvent(event) ; |
---|
| 187 | |
---|
| 188 | while(stop) |
---|
| 189 | { |
---|
| 190 | checkBuffers() ; |
---|
| 191 | stop=false ; |
---|
| 192 | for(itBuff=buffers.begin();itBuff!=buffers.end();itBuff++) stop|=itBuff->second->hasPendingRequest() ; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | releaseBuffers() ; |
---|
| 196 | } |
---|
| 197 | } |
---|