1 | #include "globalScopeData.hpp" |
---|
2 | #include "xmlioserver_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 | |
---|
14 | namespace xios |
---|
15 | { |
---|
16 | |
---|
17 | MPI_Comm CClient::intraComm ; |
---|
18 | MPI_Comm CClient::interComm ; |
---|
19 | int CClient::serverLeader ; |
---|
20 | bool CClient::is_MPI_Initialized ; |
---|
21 | int CClient::rank = INVALID_RANK; |
---|
22 | StdOFStream CClient::m_infoStream; |
---|
23 | |
---|
24 | void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm) |
---|
25 | { |
---|
26 | int initialized ; |
---|
27 | MPI_Initialized(&initialized) ; |
---|
28 | if (initialized) is_MPI_Initialized=true ; |
---|
29 | else is_MPI_Initialized=false ; |
---|
30 | |
---|
31 | // don't use OASIS |
---|
32 | if (!CXios::usingOasis) |
---|
33 | { |
---|
34 | // localComm doesn't given |
---|
35 | if (localComm == MPI_COMM_NULL) |
---|
36 | { |
---|
37 | if (!is_MPI_Initialized) |
---|
38 | { |
---|
39 | int argc=0; |
---|
40 | char** argv=NULL; |
---|
41 | MPI_Init(&argc,&argv) ; |
---|
42 | } |
---|
43 | CTimer::get("XIOS").resume() ; |
---|
44 | CTimer::get("XIOS init").resume() ; |
---|
45 | boost::hash<string> hashString ; |
---|
46 | |
---|
47 | unsigned long hashClient=hashString(codeId) ; |
---|
48 | unsigned long hashServer=hashString(CXios::xiosCodeId) ; |
---|
49 | unsigned long* hashAll ; |
---|
50 | int size ; |
---|
51 | int myColor ; |
---|
52 | int i,c ; |
---|
53 | MPI_Comm newComm ; |
---|
54 | |
---|
55 | MPI_Comm_size(CXios::globalComm,&size) ; |
---|
56 | MPI_Comm_rank(CXios::globalComm,&rank); |
---|
57 | |
---|
58 | hashAll=new unsigned long[size] ; |
---|
59 | |
---|
60 | MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ; |
---|
61 | |
---|
62 | map<unsigned long, int> colors ; |
---|
63 | map<unsigned long, int> leaders ; |
---|
64 | |
---|
65 | for(i=0,c=0;i<size;i++) |
---|
66 | { |
---|
67 | if (colors.find(hashAll[i])==colors.end()) |
---|
68 | { |
---|
69 | colors[hashAll[i]] =c ; |
---|
70 | leaders[hashAll[i]]=i ; |
---|
71 | c++ ; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | // Verify whether we are on server mode or not |
---|
76 | CXios::setNotUsingServer(); |
---|
77 | for (i=0; i < size; ++i) |
---|
78 | { |
---|
79 | if (hashServer == hashAll[i]) |
---|
80 | { |
---|
81 | CXios::setUsingServer(); |
---|
82 | break; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | myColor=colors[hashClient] ; |
---|
87 | |
---|
88 | MPI_Comm_split(CXios::globalComm,myColor,rank,&intraComm) ; |
---|
89 | |
---|
90 | if (CXios::usingServer) |
---|
91 | { |
---|
92 | int clientLeader=leaders[hashClient] ; |
---|
93 | serverLeader=leaders[hashServer] ; |
---|
94 | |
---|
95 | int intraCommSize, intraCommRank ; |
---|
96 | MPI_Comm_size(intraComm,&intraCommSize) ; |
---|
97 | MPI_Comm_rank(intraComm,&intraCommRank) ; |
---|
98 | info(50)<<"intercommCreate::client "<<rank<<" intraCommSize : "<<intraCommSize |
---|
99 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< serverLeader<<endl ; |
---|
100 | MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ; |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
105 | } |
---|
106 | delete [] hashAll ; |
---|
107 | } |
---|
108 | // localComm argument is given |
---|
109 | else |
---|
110 | { |
---|
111 | if (CXios::usingServer) |
---|
112 | { |
---|
113 | //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ; |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | MPI_Comm_dup(localComm,&intraComm) ; |
---|
118 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | // using OASIS |
---|
123 | else |
---|
124 | { |
---|
125 | // localComm doesn't given |
---|
126 | if (localComm == MPI_COMM_NULL) |
---|
127 | { |
---|
128 | if (!is_MPI_Initialized) oasis_init(codeId) ; |
---|
129 | oasis_get_localcomm(intraComm) ; |
---|
130 | } |
---|
131 | else MPI_Comm_dup(localComm,&intraComm) ; |
---|
132 | CTimer::get("XIOS").resume() ; |
---|
133 | CTimer::get("XIOS init").resume() ; |
---|
134 | |
---|
135 | // Verify whether we are on server mode or not |
---|
136 | CXios::setNotUsingServer(); |
---|
137 | int interCommSize = 0, intraCommSize = 0; |
---|
138 | oasis_get_intercomm(interComm,CXios::xiosCodeId); |
---|
139 | MPI_Comm_size(interComm, &interCommSize); |
---|
140 | MPI_Comm_size(intraComm, &intraCommSize); |
---|
141 | if (interCommSize == intraCommSize) CXios::setUsingServer(); |
---|
142 | |
---|
143 | if (CXios::usingServer) |
---|
144 | { |
---|
145 | MPI_Status status ; |
---|
146 | MPI_Comm_rank(intraComm,&rank) ; |
---|
147 | oasis_get_intercomm(interComm,CXios::xiosCodeId) ; |
---|
148 | if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ; |
---|
149 | MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ; |
---|
150 | |
---|
151 | } |
---|
152 | else MPI_Comm_dup(intraComm,&interComm) ; |
---|
153 | } |
---|
154 | |
---|
155 | MPI_Comm_dup(intraComm,&returnComm) ; |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | void CClient::registerContext(const string& id,MPI_Comm contextComm) |
---|
160 | { |
---|
161 | CContext::setCurrent(id) ; |
---|
162 | CContext* context=CContext::create(id) ; |
---|
163 | |
---|
164 | if (!CXios::isServer) |
---|
165 | { |
---|
166 | int size,rank,globalRank ; |
---|
167 | size_t message_size ; |
---|
168 | int leaderRank ; |
---|
169 | MPI_Comm contextInterComm ; |
---|
170 | |
---|
171 | MPI_Comm_size(contextComm,&size) ; |
---|
172 | MPI_Comm_rank(contextComm,&rank) ; |
---|
173 | MPI_Comm_rank(CXios::globalComm,&globalRank) ; |
---|
174 | if (rank!=0) globalRank=0 ; |
---|
175 | |
---|
176 | |
---|
177 | CMessage msg ; |
---|
178 | msg<<id<<size<<globalRank ; |
---|
179 | |
---|
180 | int messageSize=msg.size() ; |
---|
181 | void * buff = new char[messageSize] ; |
---|
182 | CBufferOut buffer(buff,messageSize) ; |
---|
183 | buffer<<msg ; |
---|
184 | |
---|
185 | MPI_Send(buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ; |
---|
186 | delete [] buff ; |
---|
187 | |
---|
188 | MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ; |
---|
189 | info(10)<<"Register new Context : "<<id<<endl ; |
---|
190 | |
---|
191 | MPI_Comm inter ; |
---|
192 | MPI_Intercomm_merge(contextInterComm,0,&inter) ; |
---|
193 | MPI_Barrier(inter) ; |
---|
194 | |
---|
195 | context->initClient(contextComm,contextInterComm) ; |
---|
196 | } |
---|
197 | else |
---|
198 | { |
---|
199 | MPI_Comm contextInterComm ; |
---|
200 | MPI_Comm_dup(contextComm,&contextInterComm) ; |
---|
201 | context->initClient(contextComm,contextInterComm) ; |
---|
202 | context->initServer(contextComm,contextInterComm) ; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | void CClient::finalize(void) |
---|
207 | { |
---|
208 | int rank ; |
---|
209 | int msg=0 ; |
---|
210 | if (!CXios::isServer) |
---|
211 | { |
---|
212 | MPI_Comm_rank(intraComm,&rank) ; |
---|
213 | if (rank==0) |
---|
214 | { |
---|
215 | MPI_Send(&msg,1,MPI_INT,0,0,interComm) ; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | CTimer::get("XIOS finalize").suspend() ; |
---|
220 | CTimer::get("XIOS").suspend() ; |
---|
221 | |
---|
222 | if (!is_MPI_Initialized) |
---|
223 | { |
---|
224 | if (CXios::usingOasis) oasis_finalize(); |
---|
225 | else MPI_Finalize() ; |
---|
226 | } |
---|
227 | info(20) << "Client side context is finalized"<<endl ; |
---|
228 | report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ; |
---|
229 | report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ; |
---|
230 | report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS").getCumulatedTime()*100.<<" %"<<endl ; |
---|
231 | 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 ; |
---|
232 | report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ; |
---|
233 | report(0)<< " Memory report : Minimum buffer size required : "<<maxRequestSize*2<<endl ; |
---|
234 | 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 ; |
---|
235 | } |
---|
236 | |
---|
237 | int CClient::getRank() |
---|
238 | { |
---|
239 | return rank; |
---|
240 | } |
---|
241 | |
---|
242 | /*! |
---|
243 | * \brief Open file stream to write in |
---|
244 | * Opening a file stream with a specific file name suffix-client+rank |
---|
245 | * \param [in] protype file name |
---|
246 | */ |
---|
247 | void CClient::openInfoStream(const StdString& fileName) |
---|
248 | { |
---|
249 | std::filebuf* fb = m_infoStream.rdbuf(); |
---|
250 | StdStringStream fileNameClient; |
---|
251 | int numDigit = 0; |
---|
252 | int size = 0; |
---|
253 | MPI_Comm_size(CXios::globalComm, &size); |
---|
254 | while (size) |
---|
255 | { |
---|
256 | size /= 10; |
---|
257 | ++numDigit; |
---|
258 | } |
---|
259 | |
---|
260 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ".out"; |
---|
261 | fb->open(fileNameClient.str().c_str(), std::ios::out); |
---|
262 | if (!fb->is_open()) |
---|
263 | ERROR("void CClient::openInfoStream(const StdString& fileName)", |
---|
264 | <<endl<< "Can not open <"<<fileNameClient<<"> file to write" ); |
---|
265 | |
---|
266 | info.write2File(fb); |
---|
267 | report.write2File(fb); |
---|
268 | } |
---|
269 | |
---|
270 | //! Write out to standard output |
---|
271 | void CClient::openInfoStream() |
---|
272 | { |
---|
273 | info.write2StdOut(); |
---|
274 | report.write2StdOut(); |
---|
275 | } |
---|
276 | |
---|
277 | //! Close file if it opens |
---|
278 | void CClient::closeInfoStream() |
---|
279 | { |
---|
280 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | } |
---|