1 | |
---|
2 | #include "xios_spl.hpp" |
---|
3 | #include "cxios.hpp" |
---|
4 | #include "client.hpp" |
---|
5 | #include "server.hpp" |
---|
6 | #include "xml_parser.hpp" |
---|
7 | #include <boost/functional/hash.hpp> |
---|
8 | #include "mpi.hpp" |
---|
9 | #include "memory.hpp" |
---|
10 | #include <new> |
---|
11 | #include "memtrack.hpp" |
---|
12 | #include "registry.hpp" |
---|
13 | |
---|
14 | namespace xios |
---|
15 | { |
---|
16 | string CXios::rootFile="./iodef.xml" ; |
---|
17 | string CXios::xiosCodeId="xios.x" ; |
---|
18 | string CXios::clientFile="./xios_client"; |
---|
19 | string CXios::serverFile="./xios_server"; |
---|
20 | |
---|
21 | bool CXios::isClient ; |
---|
22 | bool CXios::isServer ; |
---|
23 | MPI_Comm CXios::globalComm ; |
---|
24 | bool CXios::usingOasis ; |
---|
25 | bool CXios::usingServer = false; |
---|
26 | double CXios::bufferSizeFactor = 1.0; |
---|
27 | const double CXios::defaultBufferSizeFactor = 1.0; |
---|
28 | bool CXios::printLogs2Files; |
---|
29 | bool CXios::isOptPerformance = true; |
---|
30 | CRegistry* CXios::globalRegistry = 0; |
---|
31 | |
---|
32 | //! Parse configuration file and create some objects from it |
---|
33 | void CXios::initialize() |
---|
34 | { |
---|
35 | set_new_handler(noMemory); |
---|
36 | parseFile(rootFile); |
---|
37 | parseXiosConfig(); |
---|
38 | } |
---|
39 | |
---|
40 | /*! |
---|
41 | \brief Parse xios part of configuration file (.iodef.xml) |
---|
42 | Both client and server need information returned from this function |
---|
43 | */ |
---|
44 | void CXios::parseXiosConfig() |
---|
45 | { |
---|
46 | usingOasis=getin<bool>("using_oasis",false) ; |
---|
47 | usingServer=getin<bool>("using_server",false) ; |
---|
48 | info.setLevel(getin<int>("info_level",0)) ; |
---|
49 | report.setLevel(getin<int>("info_level",50)); |
---|
50 | printLogs2Files=getin<bool>("print_file",false); |
---|
51 | |
---|
52 | StdString bufMemory("memory"); |
---|
53 | StdString bufPerformance("performance"); |
---|
54 | StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance); |
---|
55 | std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower); |
---|
56 | if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false; |
---|
57 | else if (0 != bufOpt.compare(bufPerformance)) |
---|
58 | { |
---|
59 | ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl ); |
---|
60 | } |
---|
61 | |
---|
62 | bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor); |
---|
63 | |
---|
64 | globalComm=MPI_COMM_WORLD ; |
---|
65 | } |
---|
66 | |
---|
67 | /*! |
---|
68 | Initialize client |
---|
69 | \param [in] codeId identity of context |
---|
70 | \param [in] localComm local communicator |
---|
71 | \param [in/out] returnComm communicator corresponding to group of client with same codeId |
---|
72 | */ |
---|
73 | void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
74 | { |
---|
75 | initialize() ; |
---|
76 | |
---|
77 | isClient=true; |
---|
78 | |
---|
79 | CClient::initialize(codeId,localComm,returnComm) ; |
---|
80 | if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ; |
---|
81 | |
---|
82 | if (usingServer) isServer=false; |
---|
83 | else isServer=true; |
---|
84 | |
---|
85 | if (printLogs2Files) |
---|
86 | { |
---|
87 | CClient::openInfoStream(clientFile); |
---|
88 | CClient::openErrorStream(clientFile); |
---|
89 | } |
---|
90 | else |
---|
91 | { |
---|
92 | CClient::openInfoStream(); |
---|
93 | CClient::openErrorStream(); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | void CXios::clientFinalize(void) |
---|
98 | { |
---|
99 | CClient::finalize() ; |
---|
100 | if (CClient::getRank()==0) |
---|
101 | { |
---|
102 | info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ; |
---|
103 | globalRegistry->toFile("xios_registry.bin") ; |
---|
104 | delete globalRegistry ; |
---|
105 | } |
---|
106 | CClient::closeInfoStream(); |
---|
107 | |
---|
108 | |
---|
109 | #ifdef XIOS_MEMTRACK |
---|
110 | MemTrack::TrackListMemoryUsage() ; |
---|
111 | MemTrack::TrackDumpBlocks(); |
---|
112 | #endif |
---|
113 | } |
---|
114 | |
---|
115 | //! Init server by parsing only xios part of config file |
---|
116 | void CXios::initServer() |
---|
117 | { |
---|
118 | set_new_handler(noMemory); |
---|
119 | std::set<StdString> parseList; |
---|
120 | parseList.insert("xios"); |
---|
121 | xml::CXMLParser::ParseFile(rootFile, parseList); |
---|
122 | parseXiosConfig(); |
---|
123 | } |
---|
124 | |
---|
125 | //! Initialize server then put it into listening state |
---|
126 | void CXios::initServerSide(void) |
---|
127 | { |
---|
128 | initServer(); |
---|
129 | isClient=true; |
---|
130 | isServer=false ; |
---|
131 | |
---|
132 | // Initialize all aspects MPI |
---|
133 | CServer::initialize(); |
---|
134 | if (CServer::getRank()==0) globalRegistry = new CRegistry(CServer::intraComm) ; |
---|
135 | |
---|
136 | if (printLogs2Files) |
---|
137 | { |
---|
138 | CServer::openInfoStream(serverFile); |
---|
139 | CServer::openErrorStream(serverFile); |
---|
140 | } |
---|
141 | else |
---|
142 | { |
---|
143 | CServer::openInfoStream(); |
---|
144 | CServer::openErrorStream(); |
---|
145 | } |
---|
146 | |
---|
147 | // Enter the loop to listen message from Client |
---|
148 | CServer::eventLoop(); |
---|
149 | |
---|
150 | // Finalize |
---|
151 | if (CServer::getRank()==0) |
---|
152 | { |
---|
153 | info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ; |
---|
154 | globalRegistry->toFile("xios_registry.bin") ; |
---|
155 | delete globalRegistry ; |
---|
156 | } |
---|
157 | CServer::finalize(); |
---|
158 | CServer::closeInfoStream(); |
---|
159 | } |
---|
160 | |
---|
161 | //! Parse configuration file |
---|
162 | void CXios::parseFile(const string& filename) |
---|
163 | { |
---|
164 | xml::CXMLParser::ParseFile(filename); |
---|
165 | } |
---|
166 | |
---|
167 | //! Set using server |
---|
168 | void CXios::setUsingServer() |
---|
169 | { |
---|
170 | usingServer = true; |
---|
171 | } |
---|
172 | |
---|
173 | //! Unset using server |
---|
174 | void CXios::setNotUsingServer() |
---|
175 | { |
---|
176 | usingServer = false; |
---|
177 | } |
---|
178 | } |
---|