1 | #include "globalScopeData.hpp" |
---|
2 | #include "xios_spl.hpp" |
---|
3 | #include "cxios.hpp" |
---|
4 | #include "client.hpp" |
---|
5 | #include <boost/functional/hash.hpp> |
---|
6 | #include "type.hpp" |
---|
7 | #include "context.hpp" |
---|
8 | #include "context_client.hpp" |
---|
9 | #include "oasis_cinterface.hpp" |
---|
10 | #include "mpi.hpp" |
---|
11 | #include "timer.hpp" |
---|
12 | #include "buffer_client.hpp" |
---|
13 | #include "log.hpp" |
---|
14 | |
---|
15 | |
---|
16 | namespace xios |
---|
17 | { |
---|
18 | extern int test_omp_rank; |
---|
19 | #pragma omp threadprivate(test_omp_rank) |
---|
20 | |
---|
21 | MPI_Comm CClient::intraComm ; |
---|
22 | MPI_Comm CClient::interComm ; |
---|
23 | std::list<MPI_Comm> *CClient::contextInterComms_ptr = 0; |
---|
24 | int CClient::serverLeader ; |
---|
25 | bool CClient::is_MPI_Initialized ; |
---|
26 | int CClient::rank = INVALID_RANK; |
---|
27 | StdOFStream CClient::m_infoStream; |
---|
28 | StdOFStream CClient::m_errorStream; |
---|
29 | |
---|
30 | StdOFStream CClient::array_infoStream[10]; |
---|
31 | |
---|
32 | void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
33 | { |
---|
34 | int initialized ; |
---|
35 | MPI_Initialized(&initialized) ; |
---|
36 | if (initialized) is_MPI_Initialized=true ; |
---|
37 | else is_MPI_Initialized=false ; |
---|
38 | |
---|
39 | // don't use OASIS |
---|
40 | if (!CXios::usingOasis) |
---|
41 | { |
---|
42 | // localComm doesn't given |
---|
43 | |
---|
44 | if (localComm == MPI_COMM_NULL) |
---|
45 | { |
---|
46 | if (!is_MPI_Initialized) |
---|
47 | { |
---|
48 | //MPI_Init(NULL, NULL); |
---|
49 | int return_level; |
---|
50 | MPI_Init_thread(NULL, NULL, 3, &return_level); |
---|
51 | assert(return_level == 3); |
---|
52 | } |
---|
53 | CTimer::get("XIOS").resume() ; |
---|
54 | CTimer::get("XIOS init").resume() ; |
---|
55 | boost::hash<string> hashString ; |
---|
56 | |
---|
57 | unsigned long hashClient=hashString(codeId) ; |
---|
58 | unsigned long hashServer=hashString(CXios::xiosCodeId) ; |
---|
59 | unsigned long* hashAll ; |
---|
60 | int size ; |
---|
61 | int myColor ; |
---|
62 | int i,c ; |
---|
63 | |
---|
64 | MPI_Comm_size(CXios::globalComm,&size); |
---|
65 | MPI_Comm_rank(CXios::globalComm,&rank); |
---|
66 | |
---|
67 | |
---|
68 | hashAll=new unsigned long[size] ; |
---|
69 | |
---|
70 | MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ; |
---|
71 | |
---|
72 | map<unsigned long, int> colors ; |
---|
73 | map<unsigned long, int> leaders ; |
---|
74 | |
---|
75 | for(i=0,c=0;i<size;i++) |
---|
76 | { |
---|
77 | if (colors.find(hashAll[i])==colors.end()) |
---|
78 | { |
---|
79 | colors[hashAll[i]] =c ; |
---|
80 | leaders[hashAll[i]]=i ; |
---|
81 | c++ ; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | // Verify whether we are on server mode or not |
---|
86 | CXios::setNotUsingServer(); |
---|
87 | for (i=0; i < size; ++i) |
---|
88 | { |
---|
89 | if (hashServer == hashAll[i]) |
---|
90 | { |
---|
91 | CXios::setUsingServer(); |
---|
92 | break; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | myColor=colors[hashClient] ; |
---|
97 | |
---|
98 | MPI_Comm_split(CXios::globalComm,myColor,rank,&intraComm) ; |
---|
99 | |
---|
100 | if (CXios::usingServer) |
---|
101 | { |
---|
102 | int clientLeader=leaders[hashClient] ; |
---|
103 | serverLeader=leaders[hashServer] ; |
---|
104 | |
---|
105 | int intraCommSize, intraCommRank ; |
---|
106 | MPI_Comm_size(intraComm,&intraCommSize) ; |
---|
107 | MPI_Comm_rank(intraComm,&intraCommRank) ; |
---|
108 | |
---|
109 | #pragma omp critical(_output) |
---|
110 | { |
---|
111 | info(10)<<"intercommCreate::client "<<test_omp_rank<< " "<< &test_omp_rank <<" intraCommSize : "<<intraCommSize |
---|
112 | <<" intraCommRank :"<<intraCommRank<<" serverLeader "<< serverLeader |
---|
113 | <<" globalComm : "<< &(CXios::globalComm) << endl ; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ; |
---|
119 | |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
124 | } |
---|
125 | delete [] hashAll ; |
---|
126 | } |
---|
127 | // localComm argument is given |
---|
128 | else |
---|
129 | { |
---|
130 | if (CXios::usingServer) |
---|
131 | { |
---|
132 | //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ; |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | MPI_Comm_dup(localComm,&intraComm) ; |
---|
137 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | // using OASIS |
---|
142 | else |
---|
143 | { |
---|
144 | // localComm doesn't given |
---|
145 | if (localComm == MPI_COMM_NULL) |
---|
146 | { |
---|
147 | if (!is_MPI_Initialized) oasis_init(codeId) ; |
---|
148 | oasis_get_localcomm(localComm) ; |
---|
149 | } |
---|
150 | MPI_Comm_dup(localComm,&intraComm) ; |
---|
151 | |
---|
152 | CTimer::get("XIOS").resume() ; |
---|
153 | CTimer::get("XIOS init").resume() ; |
---|
154 | |
---|
155 | if (CXios::usingServer) |
---|
156 | { |
---|
157 | MPI_Status status ; |
---|
158 | MPI_Comm_rank(intraComm,&rank) ; |
---|
159 | |
---|
160 | oasis_get_intercomm(interComm,CXios::xiosCodeId) ; |
---|
161 | if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ; |
---|
162 | MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ; |
---|
163 | |
---|
164 | } |
---|
165 | else MPI_Comm_dup(intraComm,&interComm) ; |
---|
166 | } |
---|
167 | |
---|
168 | MPI_Comm_dup(intraComm,&returnComm) ; |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | void CClient::registerContext(const string& id,MPI_Comm contextComm) |
---|
174 | { |
---|
175 | CContext::setCurrent(id) ; |
---|
176 | CContext* context = CContext::create(id); |
---|
177 | |
---|
178 | int tmp_rank; |
---|
179 | MPI_Comm_rank(contextComm,&tmp_rank) ; |
---|
180 | |
---|
181 | StdString idServer(id); |
---|
182 | idServer += "_server"; |
---|
183 | |
---|
184 | if (!CXios::isServer) |
---|
185 | { |
---|
186 | int size,rank,globalRank ; |
---|
187 | //size_t message_size ; |
---|
188 | //int leaderRank ; |
---|
189 | MPI_Comm contextInterComm ; |
---|
190 | |
---|
191 | MPI_Comm_size(contextComm,&size) ; |
---|
192 | MPI_Comm_rank(contextComm,&rank) ; |
---|
193 | MPI_Comm_rank(CXios::globalComm,&globalRank) ; |
---|
194 | if (rank!=0) globalRank=0 ; |
---|
195 | |
---|
196 | |
---|
197 | CMessage msg ; |
---|
198 | msg<<idServer<<size<<globalRank ; |
---|
199 | |
---|
200 | |
---|
201 | int messageSize=msg.size() ; |
---|
202 | char * buff = new char[messageSize] ; |
---|
203 | CBufferOut buffer((void*)buff,messageSize) ; |
---|
204 | buffer<<msg ; |
---|
205 | |
---|
206 | MPI_Send((void*)buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ; |
---|
207 | delete [] buff ; |
---|
208 | |
---|
209 | MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ; |
---|
210 | |
---|
211 | #pragma omp critical(_output) |
---|
212 | info(10)<<" RANK "<< tmp_rank<<" Register new Context : "<<id<<endl ; |
---|
213 | |
---|
214 | |
---|
215 | MPI_Comm inter ; |
---|
216 | MPI_Intercomm_merge(contextInterComm,0,&inter) ; |
---|
217 | MPI_Barrier(inter) ; |
---|
218 | |
---|
219 | |
---|
220 | context->initClient(contextComm,contextInterComm) ; |
---|
221 | |
---|
222 | |
---|
223 | if(contextInterComms_ptr == NULL) contextInterComms_ptr = new std::list<MPI_Comm>; |
---|
224 | contextInterComms_ptr->push_back(contextInterComm); |
---|
225 | |
---|
226 | MPI_Comm_free(&inter); |
---|
227 | } |
---|
228 | else |
---|
229 | { |
---|
230 | MPI_Comm contextInterComm ; |
---|
231 | MPI_Comm_dup(contextComm,&contextInterComm) ; |
---|
232 | CContext* contextServer = CContext::create(idServer); |
---|
233 | |
---|
234 | // Firstly, initialize context on client side |
---|
235 | context->initClient(contextComm,contextInterComm, contextServer); |
---|
236 | |
---|
237 | // Secondly, initialize context on server side |
---|
238 | contextServer->initServer(contextComm,contextInterComm, context); |
---|
239 | |
---|
240 | // Finally, we should return current context to context client |
---|
241 | CContext::setCurrent(id); |
---|
242 | |
---|
243 | if(contextInterComms_ptr == NULL) contextInterComms_ptr = new std::list<MPI_Comm>; |
---|
244 | contextInterComms_ptr->push_back(contextInterComm); |
---|
245 | |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | void CClient::finalize(void) |
---|
250 | { |
---|
251 | int rank ; |
---|
252 | int msg=0 ; |
---|
253 | |
---|
254 | MPI_Comm_rank(intraComm,&rank) ; |
---|
255 | |
---|
256 | if (!CXios::isServer) |
---|
257 | { |
---|
258 | MPI_Comm_rank(intraComm,&rank) ; |
---|
259 | if (rank==0) |
---|
260 | { |
---|
261 | MPI_Send(&msg,1,MPI_INT,0,0,interComm) ; |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | for (std::list<MPI_Comm>::iterator it = contextInterComms_ptr->begin(); it != contextInterComms_ptr->end(); ++it) |
---|
266 | MPI_Comm_free(&(*it)); |
---|
267 | |
---|
268 | MPI_Comm_free(&interComm); |
---|
269 | MPI_Comm_free(&intraComm); |
---|
270 | |
---|
271 | CTimer::get("XIOS finalize").suspend() ; |
---|
272 | CTimer::get("XIOS").suspend() ; |
---|
273 | |
---|
274 | if (!is_MPI_Initialized) |
---|
275 | { |
---|
276 | if (CXios::usingOasis) oasis_finalize(); |
---|
277 | else MPI_Finalize(); |
---|
278 | } |
---|
279 | |
---|
280 | #pragma omp critical (_output) |
---|
281 | info(20) << "Client "<<rank<<" : Client side context is finalized "<< endl ; |
---|
282 | |
---|
283 | /* #pragma omp critical (_output) |
---|
284 | { |
---|
285 | report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ; |
---|
286 | report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ; |
---|
287 | report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS").getCumulatedTime()*100.<<" %"<<endl ; |
---|
288 | report(0)<< " Performance report : This ratio must be close to zero. Otherwise it may be usefull to increase buffer size or numbers of server"<<endl ; |
---|
289 | report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ; |
---|
290 | report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ; |
---|
291 | report(0)<< " Memory report : increasing it by a factor will increase performance, depending of the volume of data wrote in file at each time step of the file"<<endl ; |
---|
292 | } |
---|
293 | */ |
---|
294 | } |
---|
295 | |
---|
296 | int CClient::getRank() |
---|
297 | { |
---|
298 | return rank; |
---|
299 | } |
---|
300 | |
---|
301 | /*! |
---|
302 | * Open a file specified by a suffix and an extension and use it for the given file buffer. |
---|
303 | * The file name will be suffix+rank+extension. |
---|
304 | * |
---|
305 | * \param fileName[in] protype file name |
---|
306 | * \param ext [in] extension of the file |
---|
307 | * \param fb [in/out] the file buffer |
---|
308 | */ |
---|
309 | void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb) |
---|
310 | { |
---|
311 | StdStringStream fileNameClient; |
---|
312 | int numDigit = 0; |
---|
313 | int size = 0; |
---|
314 | MPI_Comm_size(CXios::globalComm, &size); |
---|
315 | while (size) |
---|
316 | { |
---|
317 | size /= 10; |
---|
318 | ++numDigit; |
---|
319 | } |
---|
320 | |
---|
321 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext; |
---|
322 | |
---|
323 | fb->open(fileNameClient.str().c_str(), std::ios::out); |
---|
324 | if (!fb->is_open()) |
---|
325 | ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)", |
---|
326 | << std::endl << "Can not open <" << fileNameClient << "> file to write the client log(s)."); |
---|
327 | } |
---|
328 | |
---|
329 | /*! |
---|
330 | * \brief Open a file stream to write the info logs |
---|
331 | * Open a file stream with a specific file name suffix+rank |
---|
332 | * to write the info logs. |
---|
333 | * \param fileName [in] protype file name |
---|
334 | */ |
---|
335 | void CClient::openInfoStream(const StdString& fileName) |
---|
336 | { |
---|
337 | //std::filebuf* fb = m_infoStream.rdbuf(); |
---|
338 | |
---|
339 | info_FB[omp_get_thread_num()] = array_infoStream[omp_get_thread_num()].rdbuf(); |
---|
340 | |
---|
341 | openStream(fileName, ".out", info_FB[omp_get_thread_num()]); |
---|
342 | |
---|
343 | info.write2File(info_FB[omp_get_thread_num()]); |
---|
344 | report.write2File(info_FB[omp_get_thread_num()]); |
---|
345 | |
---|
346 | } |
---|
347 | |
---|
348 | //! Write the info logs to standard output |
---|
349 | void CClient::openInfoStream() |
---|
350 | { |
---|
351 | info.write2StdOut(); |
---|
352 | report.write2StdOut(); |
---|
353 | } |
---|
354 | |
---|
355 | //! Close the info logs file if it opens |
---|
356 | void CClient::closeInfoStream() |
---|
357 | { |
---|
358 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
359 | } |
---|
360 | |
---|
361 | /*! |
---|
362 | * \brief Open a file stream to write the error log |
---|
363 | * Open a file stream with a specific file name suffix+rank |
---|
364 | * to write the error log. |
---|
365 | * \param fileName [in] protype file name |
---|
366 | */ |
---|
367 | void CClient::openErrorStream(const StdString& fileName) |
---|
368 | { |
---|
369 | std::filebuf* fb = m_errorStream.rdbuf(); |
---|
370 | openStream(fileName, ".err", fb); |
---|
371 | |
---|
372 | error.write2File(fb); |
---|
373 | } |
---|
374 | |
---|
375 | //! Write the error log to standard error output |
---|
376 | void CClient::openErrorStream() |
---|
377 | { |
---|
378 | error.write2StdErr(); |
---|
379 | } |
---|
380 | |
---|
381 | //! Close the error log file if it opens |
---|
382 | void CClient::closeErrorStream() |
---|
383 | { |
---|
384 | if (m_errorStream.is_open()) m_errorStream.close(); |
---|
385 | } |
---|
386 | } |
---|