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