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 | StdSize CXios::minBufferSize = 1024 * sizeof(double); |
---|
29 | bool CXios::printLogs2Files; |
---|
30 | bool CXios::isOptPerformance = true; |
---|
31 | CRegistry* CXios::globalRegistry = 0; |
---|
32 | double CXios::recvFieldTimeout = 10.0; |
---|
33 | |
---|
34 | //! Parse configuration file and create some objects from it |
---|
35 | void CXios::initialize() |
---|
36 | { |
---|
37 | set_new_handler(noMemory); |
---|
38 | parseFile(rootFile); |
---|
39 | parseXiosConfig(); |
---|
40 | } |
---|
41 | |
---|
42 | /*! |
---|
43 | \brief Parse xios part of configuration file (.iodef.xml) |
---|
44 | Both client and server need information returned from this function |
---|
45 | */ |
---|
46 | void CXios::parseXiosConfig() |
---|
47 | { |
---|
48 | usingOasis=getin<bool>("using_oasis",false) ; |
---|
49 | usingServer=getin<bool>("using_server",false) ; |
---|
50 | info.setLevel(getin<int>("info_level",0)) ; |
---|
51 | report.setLevel(getin<int>("info_level",50)); |
---|
52 | printLogs2Files=getin<bool>("print_file",false); |
---|
53 | |
---|
54 | StdString bufMemory("memory"); |
---|
55 | StdString bufPerformance("performance"); |
---|
56 | StdString bufOpt = getin<StdString>("optimal_buffer_size", bufPerformance); |
---|
57 | std::transform(bufOpt.begin(), bufOpt.end(), bufOpt.begin(), ::tolower); |
---|
58 | if (0 == bufOpt.compare(bufMemory)) isOptPerformance = false; |
---|
59 | else if (0 != bufOpt.compare(bufPerformance)) |
---|
60 | { |
---|
61 | ERROR("CXios::parseXiosConfig()", << "optimal_buffer_size must be memory or performance "<< endl ); |
---|
62 | } |
---|
63 | |
---|
64 | bufferSizeFactor = getin<double>("buffer_size_factor", defaultBufferSizeFactor); |
---|
65 | minBufferSize = getin<int>("min_buffer_size", 1024 * sizeof(double)); |
---|
66 | recvFieldTimeout = getin<double>("recv_field_timeout", 10.0); |
---|
67 | if (recvFieldTimeout < 0.0) |
---|
68 | ERROR("CXios::parseXiosConfig()", "recv_field_timeout cannot be negative."); |
---|
69 | |
---|
70 | globalComm=MPI_COMM_WORLD ; |
---|
71 | } |
---|
72 | |
---|
73 | /*! |
---|
74 | Initialize client |
---|
75 | \param [in] codeId identity of context |
---|
76 | \param [in] localComm local communicator |
---|
77 | \param [in/out] returnComm communicator corresponding to group of client with same codeId |
---|
78 | */ |
---|
79 | void CXios::initClientSide(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
80 | { |
---|
81 | initialize() ; |
---|
82 | |
---|
83 | isClient = true; |
---|
84 | |
---|
85 | CClient::initialize(codeId,localComm,returnComm) ; |
---|
86 | if (CClient::getRank()==0) globalRegistry = new CRegistry(returnComm) ; |
---|
87 | |
---|
88 | // If there are no server processes then we are in attached mode |
---|
89 | // and the clients are also servers |
---|
90 | isServer = !usingServer; |
---|
91 | |
---|
92 | if (printLogs2Files) |
---|
93 | { |
---|
94 | CClient::openInfoStream(clientFile); |
---|
95 | CClient::openErrorStream(clientFile); |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | CClient::openInfoStream(); |
---|
100 | CClient::openErrorStream(); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | void CXios::clientFinalize(void) |
---|
105 | { |
---|
106 | CClient::finalize() ; |
---|
107 | if (CClient::getRank()==0) |
---|
108 | { |
---|
109 | info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ; |
---|
110 | globalRegistry->toFile("xios_registry.bin") ; |
---|
111 | delete globalRegistry ; |
---|
112 | } |
---|
113 | |
---|
114 | #ifdef XIOS_MEMTRACK |
---|
115 | |
---|
116 | #ifdef XIOS_MEMTRACK_LIGHT |
---|
117 | report(10) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
118 | report(10) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
119 | #endif |
---|
120 | |
---|
121 | #ifdef XIOS_MEMTRACK_FULL |
---|
122 | MemTrack::TrackListMemoryUsage() ; |
---|
123 | MemTrack::TrackDumpBlocks(); |
---|
124 | #endif |
---|
125 | |
---|
126 | CClient::closeInfoStream(); |
---|
127 | |
---|
128 | #endif |
---|
129 | } |
---|
130 | |
---|
131 | //! Init server by parsing only xios part of config file |
---|
132 | void CXios::initServer() |
---|
133 | { |
---|
134 | set_new_handler(noMemory); |
---|
135 | std::set<StdString> parseList; |
---|
136 | parseList.insert("xios"); |
---|
137 | xml::CXMLParser::ParseFile(rootFile, parseList); |
---|
138 | parseXiosConfig(); |
---|
139 | } |
---|
140 | |
---|
141 | //! Initialize server then put it into listening state |
---|
142 | void CXios::initServerSide(void) |
---|
143 | { |
---|
144 | initServer(); |
---|
145 | isClient = false; |
---|
146 | isServer = true; |
---|
147 | |
---|
148 | // Initialize all aspects MPI |
---|
149 | CServer::initialize(); |
---|
150 | if (CServer::getRank()==0) globalRegistry = new CRegistry(CServer::intraComm) ; |
---|
151 | |
---|
152 | if (printLogs2Files) |
---|
153 | { |
---|
154 | CServer::openInfoStream(serverFile); |
---|
155 | CServer::openErrorStream(serverFile); |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | CServer::openInfoStream(); |
---|
160 | CServer::openErrorStream(); |
---|
161 | } |
---|
162 | |
---|
163 | // Enter the loop to listen message from Client |
---|
164 | CServer::eventLoop(); |
---|
165 | |
---|
166 | // Finalize |
---|
167 | if (CServer::getRank()==0) |
---|
168 | { |
---|
169 | info(80)<<"Write data base Registry"<<endl<<globalRegistry->toString()<<endl ; |
---|
170 | globalRegistry->toFile("xios_registry.bin") ; |
---|
171 | delete globalRegistry ; |
---|
172 | } |
---|
173 | CServer::finalize(); |
---|
174 | |
---|
175 | #ifdef XIOS_MEMTRACK |
---|
176 | |
---|
177 | #ifdef XIOS_MEMTRACK_LIGHT |
---|
178 | report(10) << " Memory report : current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
179 | report(10) << " Memory report : maximum memory used by XIOS : "<< MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ; |
---|
180 | #endif |
---|
181 | |
---|
182 | #ifdef XIOS_MEMTRACK_FULL |
---|
183 | MemTrack::TrackListMemoryUsage() ; |
---|
184 | MemTrack::TrackDumpBlocks(); |
---|
185 | #endif |
---|
186 | #endif |
---|
187 | CServer::closeInfoStream(); |
---|
188 | } |
---|
189 | |
---|
190 | //! Parse configuration file |
---|
191 | void CXios::parseFile(const string& filename) |
---|
192 | { |
---|
193 | xml::CXMLParser::ParseFile(filename); |
---|
194 | } |
---|
195 | |
---|
196 | //! Set using server |
---|
197 | void CXios::setUsingServer() |
---|
198 | { |
---|
199 | usingServer = true; |
---|
200 | } |
---|
201 | |
---|
202 | //! Unset using server |
---|
203 | void CXios::setNotUsingServer() |
---|
204 | { |
---|
205 | usingServer = false; |
---|
206 | } |
---|
207 | } |
---|