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