[490] | 1 | #include "globalScopeData.hpp" |
---|
[300] | 2 | #include "xmlioserver_spl.hpp" |
---|
| 3 | #include "cxios.hpp" |
---|
[342] | 4 | #include "client.hpp" |
---|
[300] | 5 | #include <boost/functional/hash.hpp> |
---|
| 6 | #include "type.hpp" |
---|
| 7 | #include "context.hpp" |
---|
| 8 | #include "context_client.hpp" |
---|
| 9 | #include "oasis_cinterface.hpp" |
---|
[382] | 10 | #include "mpi.hpp" |
---|
[347] | 11 | #include "timer.hpp" |
---|
[400] | 12 | #include "buffer_client.hpp" |
---|
[300] | 13 | |
---|
[335] | 14 | namespace xios |
---|
[490] | 15 | { |
---|
[300] | 16 | |
---|
| 17 | MPI_Comm CClient::intraComm ; |
---|
| 18 | MPI_Comm CClient::interComm ; |
---|
| 19 | int CClient::serverLeader ; |
---|
| 20 | bool CClient::is_MPI_Initialized ; |
---|
[490] | 21 | int CClient::rank = INVALID_RANK; |
---|
| 22 | StdOFStream CClient::m_infoStream; |
---|
| 23 | |
---|
[300] | 24 | void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm) |
---|
| 25 | { |
---|
| 26 | int initialized ; |
---|
| 27 | MPI_Initialized(&initialized) ; |
---|
| 28 | if (initialized) is_MPI_Initialized=true ; |
---|
| 29 | else is_MPI_Initialized=false ; |
---|
[490] | 30 | |
---|
[300] | 31 | // don't use OASIS |
---|
| 32 | if (!CXios::usingOasis) |
---|
| 33 | { |
---|
| 34 | // localComm doesn't given |
---|
| 35 | if (localComm == MPI_COMM_NULL) |
---|
| 36 | { |
---|
[490] | 37 | if (!is_MPI_Initialized) |
---|
[300] | 38 | { |
---|
| 39 | int argc=0; |
---|
| 40 | char** argv=NULL; |
---|
| 41 | MPI_Init(&argc,&argv) ; |
---|
| 42 | } |
---|
[359] | 43 | CTimer::get("XIOS").resume() ; |
---|
| 44 | CTimer::get("XIOS init").resume() ; |
---|
[490] | 45 | boost::hash<string> hashString ; |
---|
| 46 | |
---|
[300] | 47 | unsigned long hashClient=hashString(codeId) ; |
---|
| 48 | unsigned long hashServer=hashString(CXios::xiosCodeId) ; |
---|
| 49 | unsigned long* hashAll ; |
---|
| 50 | int size ; |
---|
| 51 | int myColor ; |
---|
| 52 | int i,c ; |
---|
| 53 | MPI_Comm newComm ; |
---|
[490] | 54 | |
---|
[300] | 55 | MPI_Comm_size(CXios::globalComm,&size) ; |
---|
| 56 | MPI_Comm_rank(CXios::globalComm,&rank); |
---|
[490] | 57 | |
---|
[300] | 58 | hashAll=new unsigned long[size] ; |
---|
[490] | 59 | |
---|
[300] | 60 | MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ; |
---|
| 61 | |
---|
| 62 | map<unsigned long, int> colors ; |
---|
| 63 | map<unsigned long, int> leaders ; |
---|
[490] | 64 | |
---|
[300] | 65 | for(i=0,c=0;i<size;i++) |
---|
| 66 | { |
---|
| 67 | if (colors.find(hashAll[i])==colors.end()) |
---|
| 68 | { |
---|
| 69 | colors[hashAll[i]] =c ; |
---|
| 70 | leaders[hashAll[i]]=i ; |
---|
| 71 | c++ ; |
---|
| 72 | } |
---|
| 73 | } |
---|
[490] | 74 | |
---|
[491] | 75 | // Verify whether we are on server mode or not |
---|
| 76 | CXios::setNotUsingServer(); |
---|
| 77 | for (i=0; i < size; ++i) |
---|
| 78 | { |
---|
| 79 | if (hashServer == hashAll[i]) |
---|
| 80 | { |
---|
| 81 | CXios::setUsingServer(); |
---|
| 82 | break; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
[300] | 86 | myColor=colors[hashClient] ; |
---|
[490] | 87 | |
---|
[300] | 88 | MPI_Comm_split(CXios::globalComm,myColor,rank,&intraComm) ; |
---|
| 89 | |
---|
| 90 | if (CXios::usingServer) |
---|
[490] | 91 | { |
---|
[300] | 92 | int clientLeader=leaders[hashClient] ; |
---|
| 93 | serverLeader=leaders[hashServer] ; |
---|
[493] | 94 | |
---|
| 95 | int intraCommSize, intraCommRank ; |
---|
| 96 | MPI_Comm_size(intraComm,&intraCommSize) ; |
---|
| 97 | MPI_Comm_rank(intraComm,&intraCommRank) ; |
---|
| 98 | info(50)<<"intercommCreate::client "<<rank<<" intraCommSize : "<<intraCommSize |
---|
| 99 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< serverLeader<<endl ; |
---|
[300] | 100 | MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ; |
---|
| 101 | } |
---|
| 102 | else |
---|
| 103 | { |
---|
| 104 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
| 105 | } |
---|
| 106 | delete [] hashAll ; |
---|
| 107 | } |
---|
| 108 | // localComm argument is given |
---|
[490] | 109 | else |
---|
[300] | 110 | { |
---|
| 111 | if (CXios::usingServer) |
---|
[490] | 112 | { |
---|
[300] | 113 | //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ; |
---|
| 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
| 117 | MPI_Comm_dup(localComm,&intraComm) ; |
---|
| 118 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | // using OASIS |
---|
| 123 | else |
---|
| 124 | { |
---|
| 125 | // localComm doesn't given |
---|
| 126 | if (localComm == MPI_COMM_NULL) |
---|
| 127 | { |
---|
| 128 | if (!is_MPI_Initialized) oasis_init(codeId) ; |
---|
| 129 | oasis_get_localcomm(intraComm) ; |
---|
| 130 | } |
---|
| 131 | else MPI_Comm_dup(localComm,&intraComm) ; |
---|
[359] | 132 | CTimer::get("XIOS").resume() ; |
---|
| 133 | CTimer::get("XIOS init").resume() ; |
---|
[507] | 134 | |
---|
| 135 | if (CXios::usingServer) |
---|
[300] | 136 | { |
---|
| 137 | MPI_Status status ; |
---|
[491] | 138 | MPI_Comm_rank(intraComm,&rank) ; |
---|
[507] | 139 | |
---|
[300] | 140 | oasis_get_intercomm(interComm,CXios::xiosCodeId) ; |
---|
| 141 | if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ; |
---|
| 142 | MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ; |
---|
[490] | 143 | |
---|
[300] | 144 | } |
---|
| 145 | else MPI_Comm_dup(intraComm,&interComm) ; |
---|
| 146 | } |
---|
[490] | 147 | |
---|
[300] | 148 | MPI_Comm_dup(intraComm,&returnComm) ; |
---|
| 149 | } |
---|
[490] | 150 | |
---|
| 151 | |
---|
[300] | 152 | void CClient::registerContext(const string& id,MPI_Comm contextComm) |
---|
| 153 | { |
---|
[346] | 154 | CContext::setCurrent(id) ; |
---|
[347] | 155 | CContext* context=CContext::create(id) ; |
---|
[490] | 156 | |
---|
[300] | 157 | if (!CXios::isServer) |
---|
| 158 | { |
---|
| 159 | int size,rank,globalRank ; |
---|
| 160 | size_t message_size ; |
---|
| 161 | int leaderRank ; |
---|
| 162 | MPI_Comm contextInterComm ; |
---|
[490] | 163 | |
---|
[300] | 164 | MPI_Comm_size(contextComm,&size) ; |
---|
| 165 | MPI_Comm_rank(contextComm,&rank) ; |
---|
| 166 | MPI_Comm_rank(CXios::globalComm,&globalRank) ; |
---|
| 167 | if (rank!=0) globalRank=0 ; |
---|
[490] | 168 | |
---|
| 169 | |
---|
[300] | 170 | CMessage msg ; |
---|
| 171 | msg<<id<<size<<globalRank ; |
---|
| 172 | |
---|
| 173 | int messageSize=msg.size() ; |
---|
| 174 | void * buff = new char[messageSize] ; |
---|
| 175 | CBufferOut buffer(buff,messageSize) ; |
---|
| 176 | buffer<<msg ; |
---|
[490] | 177 | |
---|
[300] | 178 | MPI_Send(buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ; |
---|
| 179 | delete [] buff ; |
---|
[490] | 180 | |
---|
[300] | 181 | MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ; |
---|
| 182 | info(10)<<"Register new Context : "<<id<<endl ; |
---|
[490] | 183 | |
---|
[300] | 184 | MPI_Comm inter ; |
---|
| 185 | MPI_Intercomm_merge(contextInterComm,0,&inter) ; |
---|
| 186 | MPI_Barrier(inter) ; |
---|
| 187 | |
---|
| 188 | context->initClient(contextComm,contextInterComm) ; |
---|
| 189 | } |
---|
| 190 | else |
---|
| 191 | { |
---|
| 192 | MPI_Comm contextInterComm ; |
---|
| 193 | MPI_Comm_dup(contextComm,&contextInterComm) ; |
---|
| 194 | context->initClient(contextComm,contextInterComm) ; |
---|
| 195 | context->initServer(contextComm,contextInterComm) ; |
---|
| 196 | } |
---|
| 197 | } |
---|
[490] | 198 | |
---|
[300] | 199 | void CClient::finalize(void) |
---|
| 200 | { |
---|
| 201 | int rank ; |
---|
| 202 | int msg=0 ; |
---|
[332] | 203 | if (!CXios::isServer) |
---|
[300] | 204 | { |
---|
[490] | 205 | MPI_Comm_rank(intraComm,&rank) ; |
---|
| 206 | if (rank==0) |
---|
[332] | 207 | { |
---|
| 208 | MPI_Send(&msg,1,MPI_INT,0,0,interComm) ; |
---|
| 209 | } |
---|
[300] | 210 | } |
---|
[490] | 211 | |
---|
[361] | 212 | CTimer::get("XIOS finalize").suspend() ; |
---|
| 213 | CTimer::get("XIOS").suspend() ; |
---|
| 214 | |
---|
[300] | 215 | if (!is_MPI_Initialized) |
---|
| 216 | { |
---|
| 217 | if (CXios::usingOasis) oasis_finalize(); |
---|
| 218 | else MPI_Finalize() ; |
---|
| 219 | } |
---|
| 220 | info(20) << "Client side context is finalized"<<endl ; |
---|
[490] | 221 | report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ; |
---|
[347] | 222 | report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ; |
---|
| 223 | report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS").getCumulatedTime()*100.<<" %"<<endl ; |
---|
| 224 | report(0)<< " Performance report : This ratio must be close to zero. Otherwise it may be usefull to increase buffer size or numbers of server"<<endl ; |
---|
[400] | 225 | report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ; |
---|
| 226 | report(0)<< " Memory report : Minimum buffer size required : "<<maxRequestSize*2<<endl ; |
---|
| 227 | report(0)<< " Memory report : increasing it by a factor will increase performance, depending of the volume of data wrote in file at each time step of the file"<<endl ; |
---|
| 228 | } |
---|
[490] | 229 | |
---|
| 230 | int CClient::getRank() |
---|
| 231 | { |
---|
| 232 | return rank; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /*! |
---|
| 236 | * \brief Open file stream to write in |
---|
| 237 | * Opening a file stream with a specific file name suffix-client+rank |
---|
| 238 | * \param [in] protype file name |
---|
| 239 | */ |
---|
| 240 | void CClient::openInfoStream(const StdString& fileName) |
---|
| 241 | { |
---|
| 242 | std::filebuf* fb = m_infoStream.rdbuf(); |
---|
| 243 | StdStringStream fileNameClient; |
---|
[497] | 244 | int numDigit = 0; |
---|
| 245 | int size = 0; |
---|
| 246 | MPI_Comm_size(CXios::globalComm, &size); |
---|
| 247 | while (size) |
---|
| 248 | { |
---|
| 249 | size /= 10; |
---|
| 250 | ++numDigit; |
---|
| 251 | } |
---|
| 252 | |
---|
[499] | 253 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ".out"; |
---|
[490] | 254 | fb->open(fileNameClient.str().c_str(), std::ios::out); |
---|
| 255 | if (!fb->is_open()) |
---|
| 256 | ERROR("void CClient::openInfoStream(const StdString& fileName)", |
---|
| 257 | <<endl<< "Can not open <"<<fileNameClient<<"> file to write" ); |
---|
| 258 | |
---|
| 259 | info.write2File(fb); |
---|
[499] | 260 | report.write2File(fb); |
---|
[490] | 261 | } |
---|
| 262 | |
---|
| 263 | //! Write out to standard output |
---|
| 264 | void CClient::openInfoStream() |
---|
| 265 | { |
---|
| 266 | info.write2StdOut(); |
---|
[499] | 267 | report.write2StdOut(); |
---|
[490] | 268 | } |
---|
| 269 | |
---|
| 270 | //! Close file if it opens |
---|
| 271 | void CClient::closeInfoStream() |
---|
| 272 | { |
---|
| 273 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | |
---|
[300] | 277 | } |
---|