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