[300] | 1 | |
---|
[591] | 2 | #include "xios_spl.hpp" |
---|
[300] | 3 | #include "cxios.hpp" |
---|
[342] | 4 | #include "client.hpp" |
---|
| 5 | #include "server.hpp" |
---|
[346] | 6 | #include "xml_parser.hpp" |
---|
[300] | 7 | #include <boost/functional/hash.hpp> |
---|
[382] | 8 | #include "mpi.hpp" |
---|
[400] | 9 | #include "memory.hpp" |
---|
| 10 | #include <new> |
---|
[401] | 11 | #include "memtrack.hpp" |
---|
[2428] | 12 | #include "mem_checker.hpp" |
---|
[697] | 13 | #include "registry.hpp" |
---|
[300] | 14 | |
---|
[1704] | 15 | #include "graphviz.hpp" |
---|
| 16 | |
---|
[335] | 17 | namespace xios |
---|
[300] | 18 | { |
---|
| 19 | string CXios::rootFile="./iodef.xml" ; |
---|
| 20 | string CXios::xiosCodeId="xios.x" ; |
---|
[499] | 21 | string CXios::clientFile="./xios_client"; |
---|
| 22 | string CXios::serverFile="./xios_server"; |
---|
[1021] | 23 | string CXios::serverPrmFile="./xios_server1"; |
---|
| 24 | string CXios::serverSndFile="./xios_server2"; |
---|
[490] | 25 | |
---|
[1622] | 26 | bool CXios::xiosStack = true; |
---|
| 27 | bool CXios::systemStack = false; |
---|
| 28 | |
---|
[300] | 29 | bool CXios::isClient ; |
---|
| 30 | bool CXios::isServer ; |
---|
[1639] | 31 | MPI_Comm CXios::globalComm ; |
---|
[300] | 32 | bool CXios::usingOasis ; |
---|
[491] | 33 | bool CXios::usingServer = false; |
---|
[1021] | 34 | bool CXios::usingServer2 = false; |
---|
| 35 | int CXios::ratioServer2 = 50; |
---|
[1243] | 36 | int CXios::nbPoolsServer2 = 1; |
---|
[718] | 37 | double CXios::bufferSizeFactor = 1.0; |
---|
| 38 | const double CXios::defaultBufferSizeFactor = 1.0; |
---|
[719] | 39 | StdSize CXios::minBufferSize = 1024 * sizeof(double); |
---|
[1227] | 40 | StdSize CXios::maxBufferSize = std::numeric_limits<int>::max() ; |
---|
[523] | 41 | bool CXios::printLogs2Files; |
---|
[511] | 42 | bool CXios::isOptPerformance = true; |
---|
[697] | 43 | CRegistry* CXios::globalRegistry = 0; |
---|
[1375] | 44 | double CXios::recvFieldTimeout = 300.0; |
---|
[1377] | 45 | bool CXios::checkEventSync=false ; |
---|
[2330] | 46 | bool CXios::checkSumRecv=false ; |
---|
| 47 | bool CXios::checkSumSend=false ; |
---|
[2428] | 48 | bool CXios::logMemory=false ; |
---|
[1376] | 49 | |
---|
[512] | 50 | //! Parse configuration file and create some objects from it |
---|
[300] | 51 | void CXios::initialize() |
---|
| 52 | { |
---|
[400] | 53 | set_new_handler(noMemory); |
---|
[346] | 54 | parseFile(rootFile); |
---|
[511] | 55 | parseXiosConfig(); |
---|
| 56 | } |
---|
| 57 | |
---|
[512] | 58 | /*! |
---|
| 59 | \brief Parse xios part of configuration file (.iodef.xml) |
---|
| 60 | Both client and server need information returned from this function |
---|
| 61 | */ |
---|
[511] | 62 | void CXios::parseXiosConfig() |
---|
| 63 | { |
---|
[311] | 64 | usingOasis=getin<bool>("using_oasis",false) ; |
---|
[506] | 65 | usingServer=getin<bool>("using_server",false) ; |
---|
[1021] | 66 | usingServer2=getin<bool>("using_server2",false) ; |
---|
| 67 | ratioServer2=getin<int>("ratio_server2",50); |
---|
[1519] | 68 | nbPoolsServer2=getin<int>("number_pools_server2",0); |
---|
[311] | 69 | info.setLevel(getin<int>("info_level",0)) ; |
---|
[512] | 70 | report.setLevel(getin<int>("info_level",50)); |
---|
[523] | 71 | printLogs2Files=getin<bool>("print_file",false); |
---|
[512] | 72 | |
---|
[1622] | 73 | xiosStack=getin<bool>("xios_stack",true) ; |
---|
| 74 | systemStack=getin<bool>("system_stack",false) ; |
---|
| 75 | if (xiosStack && systemStack) |
---|
| 76 | { |
---|
| 77 | xiosStack = false; |
---|
| 78 | } |
---|
| 79 | |
---|
[511] | 80 | StdString bufMemory("memory"); |
---|
| 81 | StdString bufPerformance("performance"); |
---|
| 82 | StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance); |
---|
| 83 | std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower); |
---|
| 84 | if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false; |
---|
| 85 | else if (0 != bufOpt.compare(bufPerformance)) |
---|
| 86 | { |
---|
| 87 | ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl ); |
---|
| 88 | } |
---|
| 89 | |
---|
[718] | 90 | bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor); |
---|
[719] | 91 | minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double)); |
---|
[1227] | 92 | maxBufferSize = getin<int>("max_buffer_size", std::numeric_limits<int>::max()); |
---|
[1376] | 93 | recvFieldTimeout = getin<double>("recv_field_timeout", recvFieldTimeout); |
---|
[1158] | 94 | if (recvFieldTimeout < 0.0) |
---|
| 95 | ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative."); |
---|
[718] | 96 | |
---|
[1377] | 97 | checkEventSync = getin<bool>("check_event_sync", checkEventSync); |
---|
[1639] | 98 | |
---|
[2330] | 99 | checkSumSend = getin<bool>("checksum_send_fields", false); |
---|
| 100 | checkSumRecv = getin<bool>("checksum_recv_fields", false); |
---|
[2428] | 101 | |
---|
| 102 | logMemory = getin<bool>("log_memory", false); |
---|
[2330] | 103 | |
---|
[300] | 104 | globalComm=MPI_COMM_WORLD ; |
---|
| 105 | } |
---|
| 106 | |
---|
[512] | 107 | /*! |
---|
| 108 | Initialize client |
---|
| 109 | \param [in] codeId identity of context |
---|
| 110 | \param [in] localComm local communicator |
---|
| 111 | \param [in/out] returnComm communicator corresponding to group of client with same codeId |
---|
| 112 | */ |
---|
[1639] | 113 | void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
[1622] | 114 | TRY |
---|
[300] | 115 | { |
---|
| 116 | initialize() ; |
---|
[490] | 117 | |
---|
[1639] | 118 | isClient = true; |
---|
[491] | 119 | |
---|
| 120 | CClient::initialize(codeId,localComm,returnComm) ; |
---|
[697] | 121 | if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ; |
---|
[491] | 122 | |
---|
[956] | 123 | // If there are no server processes then we are in attached mode |
---|
| 124 | // and the clients are also servers |
---|
| 125 | isServer = !usingServer; |
---|
[490] | 126 | |
---|
[523] | 127 | if (printLogs2Files) |
---|
| 128 | { |
---|
[499] | 129 | CClient::openInfoStream(clientFile); |
---|
[523] | 130 | CClient::openErrorStream(clientFile); |
---|
| 131 | } |
---|
[490] | 132 | else |
---|
[523] | 133 | { |
---|
[490] | 134 | CClient::openInfoStream(); |
---|
[523] | 135 | CClient::openErrorStream(); |
---|
| 136 | } |
---|
[2428] | 137 | CMemChecker::logMem("CXios::initClientSide"); |
---|
[490] | 138 | } |
---|
[1622] | 139 | CATCH |
---|
[300] | 140 | |
---|
| 141 | void CXios::clientFinalize(void) |
---|
| 142 | { |
---|
[2428] | 143 | CMemChecker::logMem("CXios::clientFinalize", true); |
---|
[490] | 144 | CClient::finalize() ; |
---|
[697] | 145 | if (CClient::getRank()==0) |
---|
| 146 | { |
---|
| 147 | info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ; |
---|
| 148 | globalRegistry->toFile("xios_registry.bin") ; |
---|
| 149 | delete globalRegistry ; |
---|
[1704] | 150 | CGraphviz::buildWorkflowGraphVisjs_with_info(); |
---|
[697] | 151 | } |
---|
[490] | 152 | |
---|
[401] | 153 | #ifdef XIOS_MEMTRACK |
---|
[1158] | 154 | |
---|
| 155 | #ifdef XIOS_MEMTRACK_LIGHT |
---|
| 156 | report(10) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
| 157 | report(10) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
| 158 | #endif |
---|
| 159 | |
---|
| 160 | #ifdef XIOS_MEMTRACK_FULL |
---|
[401] | 161 | MemTrack::TrackListMemoryUsage() ; |
---|
[490] | 162 | MemTrack::TrackDumpBlocks(); |
---|
[401] | 163 | #endif |
---|
[1158] | 164 | |
---|
| 165 | CClient::closeInfoStream(); |
---|
| 166 | |
---|
| 167 | #endif |
---|
[490] | 168 | } |
---|
| 169 | |
---|
[512] | 170 | //! Init server by parsing only xios part of config file |
---|
[509] | 171 | void CXios::initServer() |
---|
| 172 | { |
---|
| 173 | set_new_handler(noMemory); |
---|
| 174 | std::set<StdString> parseList; |
---|
| 175 | parseList.insert("xios"); |
---|
| 176 | xml::CXMLParser::ParseFile(rootFile, parseList); |
---|
[511] | 177 | parseXiosConfig(); |
---|
[509] | 178 | } |
---|
| 179 | |
---|
[512] | 180 | //! Initialize server then put it into listening state |
---|
[1054] | 181 | void CXios::initServerSide(void) |
---|
[300] | 182 | { |
---|
[1639] | 183 | initServer(); |
---|
[1638] | 184 | isClient = false; |
---|
| 185 | isServer = true; |
---|
| 186 | |
---|
[1021] | 187 | // Initialize all aspects MPI |
---|
| 188 | CServer::initialize(); |
---|
[1330] | 189 | if (CServer::getRank()==0 && CServer::serverLevel != 1) globalRegistry = new CRegistry(CServer::intraComm) ; |
---|
[697] | 190 | |
---|
[523] | 191 | if (printLogs2Files) |
---|
| 192 | { |
---|
[1021] | 193 | if (CServer::serverLevel == 0) |
---|
[983] | 194 | { |
---|
| 195 | CServer::openInfoStream(serverFile); |
---|
| 196 | CServer::openErrorStream(serverFile); |
---|
| 197 | } |
---|
[1021] | 198 | else if (CServer::serverLevel == 1) |
---|
[983] | 199 | { |
---|
[1021] | 200 | CServer::openInfoStream(serverPrmFile); |
---|
| 201 | CServer::openErrorStream(serverPrmFile); |
---|
[983] | 202 | } |
---|
| 203 | else |
---|
| 204 | { |
---|
[1021] | 205 | CServer::openInfoStream(serverSndFile); |
---|
| 206 | CServer::openErrorStream(serverSndFile); |
---|
[983] | 207 | } |
---|
[523] | 208 | } |
---|
[490] | 209 | else |
---|
[523] | 210 | { |
---|
[490] | 211 | CServer::openInfoStream(); |
---|
[523] | 212 | CServer::openErrorStream(); |
---|
| 213 | } |
---|
[2428] | 214 | CMemChecker::logMem( "CServer::initialize" ); |
---|
[490] | 215 | |
---|
[491] | 216 | // Enter the loop to listen message from Client |
---|
[490] | 217 | CServer::eventLoop(); |
---|
| 218 | |
---|
[491] | 219 | // Finalize |
---|
[1234] | 220 | if (CServer::serverLevel == 0) |
---|
[1168] | 221 | { |
---|
| 222 | if (CServer::getRank()==0) |
---|
| 223 | { |
---|
| 224 | info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ; |
---|
| 225 | globalRegistry->toFile("xios_registry.bin") ; |
---|
| 226 | delete globalRegistry ; |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | else |
---|
| 230 | { |
---|
[1243] | 231 | // If using two server levels: |
---|
| 232 | // (1) merge registries on each pool |
---|
| 233 | // (2) send merged registries to the first pool |
---|
| 234 | // (3) merge received registries on the first pool |
---|
[1168] | 235 | if (CServer::serverLevel == 2) |
---|
| 236 | { |
---|
| 237 | vector<int>& secondaryServerGlobalRanks = CServer::getSecondaryServerGlobalRanks(); |
---|
| 238 | int firstPoolGlobalRank = secondaryServerGlobalRanks[0]; |
---|
| 239 | int rankGlobal; |
---|
[1639] | 240 | MPI_Comm_rank(globalComm, &rankGlobal); |
---|
[1168] | 241 | |
---|
[1243] | 242 | // Merge registries defined on each pools |
---|
| 243 | CRegistry globalRegistrySndServers (CServer::intraComm); |
---|
| 244 | |
---|
[1168] | 245 | // All pools (except the first): send globalRegistry to the first pool |
---|
[1243] | 246 | for (int i=1; i<secondaryServerGlobalRanks.size(); i++) |
---|
[1168] | 247 | { |
---|
[1243] | 248 | if (rankGlobal == secondaryServerGlobalRanks[i]) |
---|
| 249 | { |
---|
| 250 | globalRegistrySndServers.mergeRegistry(*globalRegistry) ; |
---|
| 251 | int registrySize = globalRegistrySndServers.size(); |
---|
[1639] | 252 | MPI_Send(®istrySize,1,MPI_LONG,firstPoolGlobalRank,15,CXios::globalComm) ; |
---|
[1243] | 253 | CBufferOut buffer(registrySize) ; |
---|
| 254 | globalRegistrySndServers.toBuffer(buffer) ; |
---|
[1639] | 255 | MPI_Send(buffer.start(),registrySize,MPI_CHAR,firstPoolGlobalRank,15,CXios::globalComm) ; |
---|
[1243] | 256 | } |
---|
[1168] | 257 | } |
---|
| 258 | |
---|
| 259 | // First pool: receive globalRegistry of all secondary server pools, merge and write the resultant registry |
---|
[1243] | 260 | if (rankGlobal == firstPoolGlobalRank) |
---|
[1168] | 261 | { |
---|
[1639] | 262 | MPI_Status status; |
---|
[1168] | 263 | char* recvBuff; |
---|
| 264 | |
---|
| 265 | globalRegistrySndServers.mergeRegistry(*globalRegistry) ; |
---|
| 266 | |
---|
| 267 | for (int i=1; i< secondaryServerGlobalRanks.size(); i++) |
---|
| 268 | { |
---|
| 269 | int rank = secondaryServerGlobalRanks[i]; |
---|
| 270 | int registrySize = 0; |
---|
[1639] | 271 | MPI_Recv(®istrySize, 1, MPI_LONG, rank, 15, CXios::globalComm, &status); |
---|
[1168] | 272 | recvBuff = new char[registrySize]; |
---|
[1639] | 273 | MPI_Recv(recvBuff, registrySize, MPI_CHAR, rank, 15, CXios::globalComm, &status); |
---|
[1168] | 274 | CBufferIn buffer(recvBuff, registrySize) ; |
---|
| 275 | CRegistry recvRegistry; |
---|
| 276 | recvRegistry.fromBuffer(buffer) ; |
---|
| 277 | globalRegistrySndServers.mergeRegistry(recvRegistry) ; |
---|
| 278 | delete[] recvBuff; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | info(80)<<"Write data base Registry"<<endl<<globalRegistrySndServers.toString()<<endl ; |
---|
| 282 | globalRegistrySndServers.toFile("xios_registry.bin") ; |
---|
| 283 | |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | delete globalRegistry; |
---|
| 287 | } |
---|
[490] | 288 | CServer::finalize(); |
---|
[1158] | 289 | |
---|
| 290 | #ifdef XIOS_MEMTRACK |
---|
| 291 | |
---|
| 292 | #ifdef XIOS_MEMTRACK_LIGHT |
---|
| 293 | report(10) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
| 294 | report(10) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
| 295 | #endif |
---|
| 296 | |
---|
| 297 | #ifdef XIOS_MEMTRACK_FULL |
---|
| 298 | MemTrack::TrackListMemoryUsage() ; |
---|
| 299 | MemTrack::TrackDumpBlocks(); |
---|
| 300 | #endif |
---|
| 301 | #endif |
---|
[490] | 302 | CServer::closeInfoStream(); |
---|
| 303 | } |
---|
| 304 | |
---|
[512] | 305 | //! Parse configuration file |
---|
[346] | 306 | void CXios::parseFile(const string& filename) |
---|
| 307 | { |
---|
[490] | 308 | xml::CXMLParser::ParseFile(filename); |
---|
[346] | 309 | } |
---|
[491] | 310 | |
---|
[512] | 311 | //! Set using server |
---|
[491] | 312 | void CXios::setUsingServer() |
---|
| 313 | { |
---|
| 314 | usingServer = true; |
---|
| 315 | } |
---|
| 316 | |
---|
[512] | 317 | //! Unset using server |
---|
[491] | 318 | void CXios::setNotUsingServer() |
---|
| 319 | { |
---|
| 320 | usingServer = false; |
---|
| 321 | } |
---|
[300] | 322 | } |
---|