[300] | 1 | #include "xmlioserver_spl.hpp" |
---|
| 2 | #include "cxios.hpp" |
---|
| 3 | #include "client_ym.hpp" |
---|
| 4 | #include <boost/functional/hash.hpp> |
---|
| 5 | #include "type.hpp" |
---|
| 6 | #include "context.hpp" |
---|
| 7 | #include "context_client.hpp" |
---|
| 8 | #include "tree_manager.hpp" |
---|
| 9 | #include "oasis_cinterface.hpp" |
---|
| 10 | #include <mpi.h> |
---|
| 11 | |
---|
| 12 | namespace xmlioserver |
---|
| 13 | { |
---|
| 14 | namespace ym |
---|
| 15 | { |
---|
| 16 | |
---|
| 17 | MPI_Comm CClient::intraComm ; |
---|
| 18 | MPI_Comm CClient::interComm ; |
---|
| 19 | int CClient::serverLeader ; |
---|
| 20 | bool CClient::is_MPI_Initialized ; |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm) |
---|
| 24 | { |
---|
| 25 | int initialized ; |
---|
| 26 | MPI_Initialized(&initialized) ; |
---|
| 27 | if (initialized) is_MPI_Initialized=true ; |
---|
| 28 | else is_MPI_Initialized=false ; |
---|
| 29 | |
---|
| 30 | // don't use OASIS |
---|
| 31 | if (!CXios::usingOasis) |
---|
| 32 | { |
---|
| 33 | // localComm doesn't given |
---|
| 34 | if (localComm == MPI_COMM_NULL) |
---|
| 35 | { |
---|
| 36 | if (!is_MPI_Initialized) |
---|
| 37 | { |
---|
| 38 | int argc=0; |
---|
| 39 | char** argv=NULL; |
---|
| 40 | MPI_Init(&argc,&argv) ; |
---|
| 41 | } |
---|
| 42 | boost::hash<string> hashString ; |
---|
| 43 | |
---|
| 44 | unsigned long hashClient=hashString(codeId) ; |
---|
| 45 | unsigned long hashServer=hashString(CXios::xiosCodeId) ; |
---|
| 46 | unsigned long* hashAll ; |
---|
| 47 | int rank ; |
---|
| 48 | int size ; |
---|
| 49 | int myColor ; |
---|
| 50 | int i,c ; |
---|
| 51 | MPI_Comm newComm ; |
---|
| 52 | |
---|
| 53 | MPI_Comm_size(CXios::globalComm,&size) ; |
---|
| 54 | MPI_Comm_rank(CXios::globalComm,&rank); |
---|
| 55 | hashAll=new unsigned long[size] ; |
---|
| 56 | |
---|
| 57 | MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ; |
---|
| 58 | |
---|
| 59 | map<unsigned long, int> colors ; |
---|
| 60 | map<unsigned long, int> leaders ; |
---|
| 61 | |
---|
| 62 | for(i=0,c=0;i<size;i++) |
---|
| 63 | { |
---|
| 64 | if (colors.find(hashAll[i])==colors.end()) |
---|
| 65 | { |
---|
| 66 | colors[hashAll[i]] =c ; |
---|
| 67 | leaders[hashAll[i]]=i ; |
---|
| 68 | c++ ; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | myColor=colors[hashClient] ; |
---|
| 73 | |
---|
| 74 | MPI_Comm_split(CXios::globalComm,myColor,rank,&intraComm) ; |
---|
| 75 | |
---|
| 76 | if (CXios::usingServer) |
---|
| 77 | { |
---|
| 78 | int clientLeader=leaders[hashClient] ; |
---|
| 79 | serverLeader=leaders[hashServer] ; |
---|
| 80 | MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ; |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
| 84 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
| 85 | } |
---|
| 86 | delete [] hashAll ; |
---|
| 87 | } |
---|
| 88 | // localComm argument is given |
---|
| 89 | else |
---|
| 90 | { |
---|
| 91 | if (CXios::usingServer) |
---|
| 92 | { |
---|
| 93 | //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ; |
---|
| 94 | } |
---|
| 95 | else |
---|
| 96 | { |
---|
| 97 | MPI_Comm_dup(localComm,&intraComm) ; |
---|
| 98 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | // using OASIS |
---|
| 103 | else |
---|
| 104 | { |
---|
| 105 | // localComm doesn't given |
---|
| 106 | if (localComm == MPI_COMM_NULL) |
---|
| 107 | { |
---|
| 108 | if (!is_MPI_Initialized) oasis_init(codeId) ; |
---|
| 109 | oasis_get_localcomm(intraComm) ; |
---|
| 110 | } |
---|
| 111 | else MPI_Comm_dup(localComm,&intraComm) ; |
---|
| 112 | |
---|
| 113 | if (CXios::usingServer) |
---|
| 114 | { |
---|
| 115 | MPI_Status status ; |
---|
| 116 | int rank ; |
---|
| 117 | MPI_Comm_rank(intraComm,&rank) ; |
---|
| 118 | |
---|
| 119 | oasis_get_intercomm(interComm,CXios::xiosCodeId) ; |
---|
| 120 | if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ; |
---|
| 121 | MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ; |
---|
| 122 | |
---|
| 123 | } |
---|
| 124 | else MPI_Comm_dup(intraComm,&interComm) ; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | MPI_Comm_dup(intraComm,&returnComm) ; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | void CClient::registerContext(const string& id,MPI_Comm contextComm) |
---|
| 132 | { |
---|
| 133 | CObjectFactory::SetCurrentContextId(id); |
---|
| 134 | shared_ptr<CContext> context=tree::CTreeManager::CreateContext(id) ; |
---|
| 135 | |
---|
| 136 | if (!CXios::isServer) |
---|
| 137 | { |
---|
| 138 | int size,rank,globalRank ; |
---|
| 139 | size_t message_size ; |
---|
| 140 | int leaderRank ; |
---|
| 141 | MPI_Comm contextInterComm ; |
---|
| 142 | |
---|
| 143 | MPI_Comm_size(contextComm,&size) ; |
---|
| 144 | MPI_Comm_rank(contextComm,&rank) ; |
---|
| 145 | MPI_Comm_rank(CXios::globalComm,&globalRank) ; |
---|
| 146 | if (rank!=0) globalRank=0 ; |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | CMessage msg ; |
---|
| 150 | msg<<id<<size<<globalRank ; |
---|
| 151 | |
---|
| 152 | int messageSize=msg.size() ; |
---|
| 153 | void * buff = new char[messageSize] ; |
---|
| 154 | CBufferOut buffer(buff,messageSize) ; |
---|
| 155 | buffer<<msg ; |
---|
| 156 | |
---|
| 157 | MPI_Send(buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ; |
---|
| 158 | delete [] buff ; |
---|
| 159 | |
---|
| 160 | MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ; |
---|
| 161 | info(10)<<"Register new Context : "<<id<<endl ; |
---|
| 162 | |
---|
| 163 | MPI_Comm inter ; |
---|
| 164 | MPI_Intercomm_merge(contextInterComm,0,&inter) ; |
---|
| 165 | MPI_Barrier(inter) ; |
---|
| 166 | |
---|
| 167 | context->initClient(contextComm,contextInterComm) ; |
---|
| 168 | } |
---|
| 169 | else |
---|
| 170 | { |
---|
| 171 | MPI_Comm contextInterComm ; |
---|
| 172 | MPI_Comm_dup(contextComm,&contextInterComm) ; |
---|
| 173 | context->initClient(contextComm,contextInterComm) ; |
---|
| 174 | context->initServer(contextComm,contextInterComm) ; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | void CClient::finalize(void) |
---|
| 179 | { |
---|
| 180 | int rank ; |
---|
| 181 | int msg=0 ; |
---|
| 182 | MPI_Comm_rank(intraComm,&rank) ; |
---|
| 183 | if (rank==0) |
---|
| 184 | { |
---|
| 185 | MPI_Send(&msg,1,MPI_INT,0,0,interComm) ; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | if (!is_MPI_Initialized) |
---|
| 189 | { |
---|
| 190 | if (CXios::usingOasis) oasis_finalize(); |
---|
| 191 | else MPI_Finalize() ; |
---|
| 192 | } |
---|
| 193 | info(20) << "Client side context is finalized"<<endl ; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | } |
---|