[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 | { |
---|
[490] | 16 | |
---|
[1134] | 17 | extern int test_omp_rank; |
---|
| 18 | #pragma omp threadprivate(test_omp_rank) |
---|
| 19 | |
---|
| 20 | const string CXios::rootFile="./iodef.xml" ; |
---|
| 21 | const string CXios::xiosCodeId="xios.x" ; |
---|
| 22 | const string CXios::clientFile="./xios_client"; |
---|
| 23 | const string CXios::serverFile="./xios_server"; |
---|
| 24 | |
---|
| 25 | |
---|
[300] | 26 | bool CXios::isClient ; |
---|
| 27 | bool CXios::isServer ; |
---|
[1134] | 28 | |
---|
| 29 | |
---|
[300] | 30 | MPI_Comm CXios::globalComm ; |
---|
[1134] | 31 | |
---|
| 32 | |
---|
[300] | 33 | bool CXios::usingOasis ; |
---|
[491] | 34 | bool CXios::usingServer = false; |
---|
[1134] | 35 | |
---|
| 36 | |
---|
[718] | 37 | double CXios::bufferSizeFactor = 1.0; |
---|
| 38 | const double CXios::defaultBufferSizeFactor = 1.0; |
---|
[719] | 39 | StdSize CXios::minBufferSize = 1024 * sizeof(double); |
---|
[1134] | 40 | |
---|
| 41 | |
---|
[523] | 42 | bool CXios::printLogs2Files; |
---|
[511] | 43 | bool CXios::isOptPerformance = true; |
---|
[697] | 44 | CRegistry* CXios::globalRegistry = 0; |
---|
[1029] | 45 | double CXios::recvFieldTimeout = 10.0; |
---|
[490] | 46 | |
---|
[512] | 47 | //! Parse configuration file and create some objects from it |
---|
[300] | 48 | void CXios::initialize() |
---|
| 49 | { |
---|
[400] | 50 | set_new_handler(noMemory); |
---|
[1134] | 51 | |
---|
| 52 | |
---|
| 53 | #pragma omp critical |
---|
| 54 | { |
---|
| 55 | parseFile(rootFile); |
---|
| 56 | } |
---|
| 57 | #pragma omp barrier |
---|
[511] | 58 | parseXiosConfig(); |
---|
| 59 | } |
---|
| 60 | |
---|
[512] | 61 | /*! |
---|
| 62 | \brief Parse xios part of configuration file (.iodef.xml) |
---|
| 63 | Both client and server need information returned from this function |
---|
| 64 | */ |
---|
[511] | 65 | void CXios::parseXiosConfig() |
---|
| 66 | { |
---|
[311] | 67 | usingOasis=getin<bool>("using_oasis",false) ; |
---|
[506] | 68 | usingServer=getin<bool>("using_server",false) ; |
---|
[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 | |
---|
[511] | 73 | StdString bufMemory("memory"); |
---|
| 74 | StdString bufPerformance("performance"); |
---|
| 75 | StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance); |
---|
| 76 | std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower); |
---|
| 77 | if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false; |
---|
| 78 | else if (0 != bufOpt.compare(bufPerformance)) |
---|
| 79 | { |
---|
| 80 | ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl ); |
---|
| 81 | } |
---|
| 82 | |
---|
[718] | 83 | bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor); |
---|
[719] | 84 | minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double)); |
---|
[1029] | 85 | recvFieldTimeout = getin<double>("recv_field_timeout", 10.0); |
---|
| 86 | if (recvFieldTimeout < 0.0) |
---|
| 87 | ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative."); |
---|
[718] | 88 | |
---|
[1134] | 89 | |
---|
| 90 | int num_ep; |
---|
| 91 | if(isClient) |
---|
| 92 | { |
---|
| 93 | num_ep = omp_get_num_threads(); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | if(isServer) |
---|
| 97 | { |
---|
| 98 | num_ep = omp_get_num_threads(); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | MPI_Info info; |
---|
| 102 | #pragma omp master |
---|
| 103 | { |
---|
| 104 | MPI_Comm *ep_comm; |
---|
| 105 | MPI_Comm_create_endpoints(MPI_COMM_WORLD, num_ep, info, ep_comm); // servers should reach here too. |
---|
| 106 | passage = ep_comm; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | #pragma omp barrier |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | CXios::globalComm = passage[omp_get_thread_num()]; |
---|
| 113 | |
---|
| 114 | int tmp_rank; |
---|
| 115 | MPI_Comm_rank(CXios::globalComm, &tmp_rank); |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | test_omp_rank = tmp_rank; |
---|
| 119 | |
---|
[300] | 120 | } |
---|
| 121 | |
---|
[512] | 122 | /*! |
---|
| 123 | Initialize client |
---|
| 124 | \param [in] codeId identity of context |
---|
| 125 | \param [in] localComm local communicator |
---|
| 126 | \param [in/out] returnComm communicator corresponding to group of client with same codeId |
---|
| 127 | */ |
---|
[300] | 128 | void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
| 129 | { |
---|
[1134] | 130 | isClient = true; |
---|
| 131 | |
---|
[300] | 132 | initialize() ; |
---|
[490] | 133 | |
---|
[1134] | 134 | CClient::initialize(codeId,localComm,returnComm) ; |
---|
[491] | 135 | |
---|
[697] | 136 | if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ; |
---|
[491] | 137 | |
---|
[956] | 138 | // If there are no server processes then we are in attached mode |
---|
| 139 | // and the clients are also servers |
---|
| 140 | isServer = !usingServer; |
---|
[490] | 141 | |
---|
[523] | 142 | if (printLogs2Files) |
---|
| 143 | { |
---|
[1134] | 144 | #pragma omp critical |
---|
[499] | 145 | CClient::openInfoStream(clientFile); |
---|
[523] | 146 | CClient::openErrorStream(clientFile); |
---|
| 147 | } |
---|
[490] | 148 | else |
---|
[523] | 149 | { |
---|
[490] | 150 | CClient::openInfoStream(); |
---|
[523] | 151 | CClient::openErrorStream(); |
---|
| 152 | } |
---|
[490] | 153 | } |
---|
[300] | 154 | |
---|
| 155 | void CXios::clientFinalize(void) |
---|
| 156 | { |
---|
[490] | 157 | CClient::finalize() ; |
---|
[697] | 158 | if (CClient::getRank()==0) |
---|
| 159 | { |
---|
[1134] | 160 | #pragma omp critical (_output) |
---|
[697] | 161 | info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ; |
---|
| 162 | globalRegistry->toFile("xios_registry.bin") ; |
---|
| 163 | delete globalRegistry ; |
---|
| 164 | } |
---|
[1160] | 165 | CClient::closeInfoStream(); |
---|
| 166 | |
---|
| 167 | |
---|
[401] | 168 | #ifdef XIOS_MEMTRACK |
---|
[1156] | 169 | |
---|
| 170 | #ifdef XIOS_MEMTRACK_LIGHT |
---|
| 171 | report(10) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
| 172 | report(10) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
| 173 | #endif |
---|
| 174 | |
---|
| 175 | #ifdef XIOS_MEMTRACK_FULL |
---|
[401] | 176 | MemTrack::TrackListMemoryUsage() ; |
---|
[490] | 177 | MemTrack::TrackDumpBlocks(); |
---|
[401] | 178 | #endif |
---|
[1161] | 179 | |
---|
| 180 | #endif |
---|
[490] | 181 | } |
---|
| 182 | |
---|
[512] | 183 | //! Init server by parsing only xios part of config file |
---|
[509] | 184 | void CXios::initServer() |
---|
| 185 | { |
---|
[1134] | 186 | int initialized; |
---|
| 187 | MPI_Initialized(&initialized); |
---|
| 188 | if (initialized) CServer::is_MPI_Initialized=true ; |
---|
| 189 | else CServer::is_MPI_Initialized=false ; |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | if(!CServer::is_MPI_Initialized) |
---|
| 193 | { |
---|
| 194 | MPI_Init(NULL, NULL); |
---|
| 195 | } |
---|
| 196 | |
---|
[509] | 197 | set_new_handler(noMemory); |
---|
| 198 | std::set<StdString> parseList; |
---|
| 199 | parseList.insert("xios"); |
---|
| 200 | xml::CXMLParser::ParseFile(rootFile, parseList); |
---|
[511] | 201 | parseXiosConfig(); |
---|
[509] | 202 | } |
---|
| 203 | |
---|
[512] | 204 | //! Initialize server then put it into listening state |
---|
[300] | 205 | void CXios::initServerSide(void) |
---|
| 206 | { |
---|
[1134] | 207 | |
---|
[956] | 208 | isClient = false; |
---|
| 209 | isServer = true; |
---|
[1134] | 210 | |
---|
| 211 | initServer(); |
---|
| 212 | |
---|
| 213 | |
---|
[491] | 214 | // Initialize all aspects MPI |
---|
[490] | 215 | CServer::initialize(); |
---|
[697] | 216 | if (CServer::getRank()==0) globalRegistry = new CRegistry(CServer::intraComm) ; |
---|
| 217 | |
---|
[523] | 218 | if (printLogs2Files) |
---|
| 219 | { |
---|
[499] | 220 | CServer::openInfoStream(serverFile); |
---|
[523] | 221 | CServer::openErrorStream(serverFile); |
---|
| 222 | } |
---|
[490] | 223 | else |
---|
[523] | 224 | { |
---|
[490] | 225 | CServer::openInfoStream(); |
---|
[523] | 226 | CServer::openErrorStream(); |
---|
| 227 | } |
---|
[490] | 228 | |
---|
[491] | 229 | // Enter the loop to listen message from Client |
---|
[490] | 230 | CServer::eventLoop(); |
---|
| 231 | |
---|
[491] | 232 | // Finalize |
---|
[697] | 233 | if (CServer::getRank()==0) |
---|
| 234 | { |
---|
| 235 | info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ; |
---|
| 236 | globalRegistry->toFile("xios_registry.bin") ; |
---|
| 237 | delete globalRegistry ; |
---|
| 238 | } |
---|
[1134] | 239 | |
---|
[1157] | 240 | CServer::finalize(); |
---|
[1160] | 241 | |
---|
[490] | 242 | CServer::closeInfoStream(); |
---|
| 243 | } |
---|
| 244 | |
---|
[512] | 245 | //! Parse configuration file |
---|
[346] | 246 | void CXios::parseFile(const string& filename) |
---|
| 247 | { |
---|
[490] | 248 | xml::CXMLParser::ParseFile(filename); |
---|
[346] | 249 | } |
---|
[491] | 250 | |
---|
[512] | 251 | //! Set using server |
---|
[491] | 252 | void CXios::setUsingServer() |
---|
| 253 | { |
---|
| 254 | usingServer = true; |
---|
| 255 | } |
---|
| 256 | |
---|
[512] | 257 | //! Unset using server |
---|
[491] | 258 | void CXios::setNotUsingServer() |
---|
| 259 | { |
---|
| 260 | usingServer = false; |
---|
| 261 | } |
---|
[300] | 262 | } |
---|