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