1 | /* ************************************************************************** * |
---|
2 | * Copyright © IPSL/LSCE, xios, Avril 2010 - Octobre 2011 * |
---|
3 | * ************************************************************************** */ |
---|
4 | |
---|
5 | #include <boost/multi_array.hpp> |
---|
6 | #include <boost/shared_ptr.hpp> |
---|
7 | #include <string> |
---|
8 | #include <cstring> |
---|
9 | #include <iostream> |
---|
10 | |
---|
11 | |
---|
12 | #include "xmlioserver.hpp" |
---|
13 | #include "oasis_cinterface.hpp" |
---|
14 | |
---|
15 | #include "attribute_template.hpp" |
---|
16 | #include "object_template.hpp" |
---|
17 | #include "group_template.hpp" |
---|
18 | |
---|
19 | #include "icutil.hpp" |
---|
20 | #include "cxios.hpp" |
---|
21 | #include "client.hpp" |
---|
22 | #include "field.hpp" |
---|
23 | #include "context.hpp" |
---|
24 | #include "context_client.hpp" |
---|
25 | #include "mpi.hpp" |
---|
26 | #include "timer.hpp" |
---|
27 | #include "array_new.hpp" |
---|
28 | |
---|
29 | |
---|
30 | extern "C" |
---|
31 | { |
---|
32 | // /////////////////////////////// Définitions ////////////////////////////// // |
---|
33 | |
---|
34 | // ----------------------- Redéfinition de types ---------------------------- |
---|
35 | |
---|
36 | typedef enum { NETCDF4 = 0 } XFileType; |
---|
37 | |
---|
38 | typedef xios::CContext * XContextPtr; |
---|
39 | |
---|
40 | // -------------------- Traitement des données ------------------------------ |
---|
41 | void cxios_init_server(void) |
---|
42 | { |
---|
43 | CXios::initServerSide(); |
---|
44 | } |
---|
45 | |
---|
46 | void cxios_init_client(const char * client_id , int len_client_id, MPI_Fint* f_local_comm, MPI_Fint* f_return_comm ) |
---|
47 | { |
---|
48 | std::string str; |
---|
49 | MPI_Comm local_comm ; |
---|
50 | MPI_Comm return_comm ; |
---|
51 | |
---|
52 | if (!cstr2string(client_id, len_client_id, str)) return; |
---|
53 | |
---|
54 | int initialized ; |
---|
55 | MPI_Initialized(&initialized) ; |
---|
56 | if (initialized) local_comm=MPI_Comm_f2c(*f_local_comm) ; |
---|
57 | else local_comm=MPI_COMM_NULL ; |
---|
58 | CXios::initClientSide(str,local_comm,return_comm); |
---|
59 | *f_return_comm=MPI_Comm_c2f(return_comm) ; |
---|
60 | CTimer::get("XIOS init").suspend() ; |
---|
61 | CTimer::get("XIOS").suspend() ; |
---|
62 | } |
---|
63 | |
---|
64 | void cxios_context_initialize(const char * context_id , int len_context_id, MPI_Fint* f_comm) |
---|
65 | { |
---|
66 | std::string str; |
---|
67 | MPI_Comm comm ; |
---|
68 | |
---|
69 | if (!cstr2string(context_id, len_context_id, str)) return; |
---|
70 | CTimer::get("XIOS").resume() ; |
---|
71 | CTimer::get("XIOS init context").resume() ; |
---|
72 | comm=MPI_Comm_f2c(*f_comm) ; |
---|
73 | CClient::registerContext(str,comm) ; |
---|
74 | CTimer::get("XIOS init context").suspend() ; |
---|
75 | CTimer::get("XIOS").suspend() ; |
---|
76 | } |
---|
77 | |
---|
78 | void cxios_context_is_initialized(const char * context_id , int len_context_id, bool* initialized) |
---|
79 | { |
---|
80 | std::string str; |
---|
81 | |
---|
82 | if (!cstr2string(context_id, len_context_id, str)) return; |
---|
83 | CTimer::get("XIOS").resume() ; |
---|
84 | CContext* context = CContext::get(str,str) ; |
---|
85 | *initialized=context->isInitialized() ; |
---|
86 | CTimer::get("XIOS").suspend() ; |
---|
87 | } |
---|
88 | |
---|
89 | void cxios_context_close_definition() |
---|
90 | { |
---|
91 | CTimer::get("XIOS").resume() ; |
---|
92 | CTimer::get("XIOS close definition").resume() ; |
---|
93 | CContext* context = CContext::getCurrent() ; |
---|
94 | context->closeDefinition() ; |
---|
95 | CTimer::get("XIOS close definition").suspend() ; |
---|
96 | CTimer::get("XIOS").suspend() ; |
---|
97 | } |
---|
98 | |
---|
99 | void cxios_context_finalize() |
---|
100 | { |
---|
101 | CTimer::get("XIOS").resume() ; |
---|
102 | CTimer::get("XIOS context finalize").resume() ; |
---|
103 | CContext* context = CContext::getCurrent() ; |
---|
104 | context->finalize() ; |
---|
105 | CTimer::get("XIOS context finalize").suspend() ; |
---|
106 | CTimer::get("XIOS").suspend() ; |
---|
107 | } |
---|
108 | |
---|
109 | void cxios_finalize() |
---|
110 | { |
---|
111 | CTimer::get("XIOS").resume() ; |
---|
112 | CTimer::get("XIOS finalize").resume() ; |
---|
113 | CXios::clientFinalize() ; |
---|
114 | } |
---|
115 | |
---|
116 | void cxios_solve_inheritance() |
---|
117 | { |
---|
118 | CTimer::get("XIOS").resume() ; |
---|
119 | CContext* context = CContext::getCurrent() ; |
---|
120 | context->solveAllInheritance(false) ; |
---|
121 | CTimer::get("XIOS").suspend() ; |
---|
122 | } |
---|
123 | |
---|
124 | /*! \brief This group of functions retrieve variable information from the configuration file (.xml) |
---|
125 | * |
---|
126 | * These functions provide intermediate C interfaces to get variable information of the configuration file (e.x iodef.xml), |
---|
127 | * from a Fortran one, for example the value of a variable with id = "using_server". |
---|
128 | * Each function corresponds to each basic type. |
---|
129 | * \param varId [in] id of the variable that we'd like to get |
---|
130 | * \param varIdSize [in] size of the variable type (integer, float, double, string) |
---|
131 | * \param dataInt [in/out] the retrieved data |
---|
132 | * \param isVarExisted [in/out] Verify whether variable with varId exists |
---|
133 | */ |
---|
134 | void cxios_get_variable_data_k8(const char * varId, int varIdSize, double * data, bool * isVarExisted) |
---|
135 | { |
---|
136 | std::string varIdStr; |
---|
137 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
138 | |
---|
139 | CTimer::get("XIOS").resume(); |
---|
140 | CTimer::get("XIOS get variable data").resume(); |
---|
141 | |
---|
142 | CContext* context = CContext::getCurrent(); |
---|
143 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
144 | |
---|
145 | if (*isVarExisted) |
---|
146 | { |
---|
147 | *data = CVariable::get(context->getId(),varIdStr)->getData<double>(); |
---|
148 | } |
---|
149 | |
---|
150 | CTimer::get("XIOS get variable data").suspend() ; |
---|
151 | CTimer::get("XIOS").suspend() ; |
---|
152 | } |
---|
153 | |
---|
154 | void cxios_get_variable_data_k4(const char * varId, int varIdSize, float * data, bool * isVarExisted) |
---|
155 | { |
---|
156 | std::string varIdStr; |
---|
157 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
158 | |
---|
159 | CTimer::get("XIOS").resume(); |
---|
160 | CTimer::get("XIOS get variable data").resume(); |
---|
161 | |
---|
162 | CContext* context = CContext::getCurrent(); |
---|
163 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
164 | |
---|
165 | if (*isVarExisted) |
---|
166 | { |
---|
167 | *data = CVariable::get(context->getId(),varIdStr)->getData<float>(); |
---|
168 | } |
---|
169 | |
---|
170 | CTimer::get("XIOS get variable data").suspend() ; |
---|
171 | CTimer::get("XIOS").suspend() ; |
---|
172 | } |
---|
173 | |
---|
174 | void cxios_get_variable_data_int(const char * varId, int varIdSize, int * data, bool * isVarExisted) |
---|
175 | { |
---|
176 | std::string varIdStr; |
---|
177 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
178 | |
---|
179 | CTimer::get("XIOS").resume(); |
---|
180 | CTimer::get("XIOS get variable data").resume(); |
---|
181 | |
---|
182 | CContext* context = CContext::getCurrent(); |
---|
183 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
184 | |
---|
185 | if (*isVarExisted) |
---|
186 | { |
---|
187 | *data = CVariable::get(context->getId(),varIdStr)->getData<int>(); |
---|
188 | } |
---|
189 | |
---|
190 | CTimer::get("XIOS get variable data").suspend() ; |
---|
191 | CTimer::get("XIOS").suspend() ; |
---|
192 | } |
---|
193 | |
---|
194 | void cxios_get_variable_data_logic(const char * varId, int varIdSize, bool * data, bool * isVarExisted) |
---|
195 | { |
---|
196 | std::string varIdStr; |
---|
197 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
198 | |
---|
199 | CTimer::get("XIOS").resume(); |
---|
200 | CTimer::get("XIOS get variable data").resume(); |
---|
201 | |
---|
202 | CContext* context = CContext::getCurrent(); |
---|
203 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
204 | |
---|
205 | if (*isVarExisted) |
---|
206 | { |
---|
207 | *data = CVariable::get(context->getId(),varIdStr)->getData<bool>(); |
---|
208 | } |
---|
209 | |
---|
210 | CTimer::get("XIOS get variable data").suspend() ; |
---|
211 | CTimer::get("XIOS").suspend() ; |
---|
212 | } |
---|
213 | |
---|
214 | void cxios_get_variable_data_char(const char * varId, int varIdSize, char * data, int dataSizeIn, bool * isVarExisted) |
---|
215 | { |
---|
216 | std::string varIdStr; |
---|
217 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
218 | |
---|
219 | CTimer::get("XIOS").resume(); |
---|
220 | CTimer::get("XIOS get variable data").resume(); |
---|
221 | |
---|
222 | CContext* context = CContext::getCurrent(); |
---|
223 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
224 | |
---|
225 | if (*isVarExisted) |
---|
226 | { |
---|
227 | int dataSizeOut = CVariable::get(context->getId(),varIdStr)->getData<string>().length(); |
---|
228 | strncpy(data, CVariable::get(context->getId(),varIdStr)->getData<string>().c_str(), std::min(dataSizeIn, dataSizeOut)); |
---|
229 | } |
---|
230 | |
---|
231 | CTimer::get("XIOS get variable data").suspend() ; |
---|
232 | CTimer::get("XIOS").suspend() ; |
---|
233 | } |
---|
234 | |
---|
235 | /*! \brief This group of functions write information into existing variable in the configuration file (.xml) |
---|
236 | * |
---|
237 | * These functions provide intermediate C interfaces to get variable information of the configuration file (e.x iodef.xml), |
---|
238 | * from a Fortran one, for example the value of a variable with id = "using_server". |
---|
239 | * Each function corresponds to each basic type. |
---|
240 | * \param varId [in] id of the variable that we'd like to get |
---|
241 | * \param varIdSize [in] size of the variable type (integer, float, double, string) |
---|
242 | * \param data [in] the input data |
---|
243 | * \param isVarExisted [in/out] Verify whether variable with varId exists |
---|
244 | */ |
---|
245 | void cxios_set_variable_data_k8(const char * varId, int varIdSize, double data, bool * isVarExisted) |
---|
246 | { |
---|
247 | std::string varIdStr; |
---|
248 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
249 | |
---|
250 | CTimer::get("XIOS").resume(); |
---|
251 | CTimer::get("XIOS set variable data").resume(); |
---|
252 | |
---|
253 | CContext* context = CContext::getCurrent() ; |
---|
254 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
255 | |
---|
256 | if (*isVarExisted) |
---|
257 | { |
---|
258 | CVariable::get(context->getId(),varIdStr)->setData<double>(data); |
---|
259 | //CVariable::get(context->getId(),varIdStr)->sendValue(); |
---|
260 | } |
---|
261 | |
---|
262 | CTimer::get("XIOS set variable data").suspend() ; |
---|
263 | CTimer::get("XIOS").suspend() ; |
---|
264 | } |
---|
265 | |
---|
266 | void cxios_set_variable_data_k4(const char * varId, int varIdSize, float data, bool * isVarExisted) |
---|
267 | { |
---|
268 | std::string varIdStr; |
---|
269 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
270 | |
---|
271 | CTimer::get("XIOS").resume(); |
---|
272 | CTimer::get("XIOS set variable data").resume(); |
---|
273 | |
---|
274 | CContext* context = CContext::getCurrent() ; |
---|
275 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
276 | |
---|
277 | if (*isVarExisted) |
---|
278 | { |
---|
279 | CVariable::get(context->getId(),varIdStr)->setData<float>(data); |
---|
280 | //CVariable::get(context->getId(),varIdStr)->sendValue(); |
---|
281 | } |
---|
282 | |
---|
283 | CTimer::get("XIOS set variable data").suspend() ; |
---|
284 | CTimer::get("XIOS").suspend() ; |
---|
285 | } |
---|
286 | |
---|
287 | void cxios_set_variable_data_int(const char * varId, int varIdSize, int data, bool * isVarExisted) |
---|
288 | { |
---|
289 | std::string varIdStr; |
---|
290 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
291 | |
---|
292 | CTimer::get("XIOS").resume(); |
---|
293 | CTimer::get("XIOS set variable data").resume(); |
---|
294 | |
---|
295 | CContext* context = CContext::getCurrent() ; |
---|
296 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
297 | |
---|
298 | if (*isVarExisted) |
---|
299 | { |
---|
300 | CVariable::get(context->getId(),varIdStr)->setData<int>(data); |
---|
301 | //CVariable::get(context->getId(),varIdStr)->sendValue(); |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | CTimer::get("XIOS set variable data").suspend() ; |
---|
306 | CTimer::get("XIOS").suspend() ; |
---|
307 | } |
---|
308 | |
---|
309 | void cxios_set_variable_data_logic(const char * varId, int varIdSize, bool data, bool * isVarExisted) |
---|
310 | { |
---|
311 | std::string varIdStr; |
---|
312 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
313 | |
---|
314 | CTimer::get("XIOS").resume(); |
---|
315 | CTimer::get("XIOS set variable data").resume(); |
---|
316 | |
---|
317 | CContext* context = CContext::getCurrent() ; |
---|
318 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
319 | |
---|
320 | if (*isVarExisted) |
---|
321 | { |
---|
322 | CVariable::get(context->getId(),varIdStr)->setData<bool>(data); |
---|
323 | //CVariable::get(context->getId(),varIdStr)->sendValue(); |
---|
324 | } |
---|
325 | |
---|
326 | CTimer::get("XIOS set variable data").suspend() ; |
---|
327 | CTimer::get("XIOS").suspend() ; |
---|
328 | } |
---|
329 | |
---|
330 | void cxios_set_variable_data_char(const char * varId, int varIdSize, const char * data, int dataSizeIn, bool * isVarExisted) |
---|
331 | { |
---|
332 | std::string varIdStr, dataStr; |
---|
333 | if (!cstr2string(varId, varIdSize, varIdStr)) return; |
---|
334 | if (!cstr2string(data, dataSizeIn, dataStr)) |
---|
335 | { |
---|
336 | *isVarExisted = false; |
---|
337 | return; |
---|
338 | } |
---|
339 | |
---|
340 | CTimer::get("XIOS").resume(); |
---|
341 | CTimer::get("XIOS set variable data").resume(); |
---|
342 | |
---|
343 | CContext* context = CContext::getCurrent() ; |
---|
344 | *isVarExisted = CVariable::has(context->getId(), varIdStr); |
---|
345 | |
---|
346 | if (*isVarExisted) |
---|
347 | { |
---|
348 | CVariable::get(context->getId(),varIdStr)->setData<string>(dataStr); |
---|
349 | //CVariable::get(context->getId(),varIdStr)->sendValue(); |
---|
350 | } |
---|
351 | |
---|
352 | CTimer::get("XIOS set variable data").suspend() ; |
---|
353 | CTimer::get("XIOS").suspend() ; |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | // ---------------------- Ecriture des données ------------------------------ |
---|
358 | |
---|
359 | void cxios_write_data_k81(const char * fieldid, int fieldid_size, double * data_k8, int data_Xsize) |
---|
360 | { |
---|
361 | std::string fieldid_str; |
---|
362 | if (!cstr2string(fieldid, fieldid_size, fieldid_str)) return; |
---|
363 | |
---|
364 | CTimer::get("XIOS").resume() ; |
---|
365 | CTimer::get("XIOS send field").resume() ; |
---|
366 | CContext* context = CContext::getCurrent() ; |
---|
367 | if (!context->hasServer) context->client->checkBuffers() ; |
---|
368 | CArray<double,(StdSize)1> data(data_k8,shape(data_Xsize),neverDeleteData) ; |
---|
369 | CField::get(fieldid_str)->setData(data) ; |
---|
370 | CTimer::get("XIOS send field").suspend() ; |
---|
371 | CTimer::get("XIOS").suspend() ; |
---|
372 | } |
---|
373 | |
---|
374 | void cxios_write_data_k82(const char * fieldid, int fieldid_size, double * data_k8, int data_Xsize, int data_Ysize) |
---|
375 | { |
---|
376 | std::string fieldid_str; |
---|
377 | if (!cstr2string(fieldid, fieldid_size, fieldid_str)) return; |
---|
378 | |
---|
379 | CTimer::get("XIOS").resume() ; |
---|
380 | CTimer::get("XIOS send field").resume() ; |
---|
381 | CContext* context = CContext::getCurrent() ; |
---|
382 | if (!context->hasServer) context->client->checkBuffers() ; |
---|
383 | |
---|
384 | CArray<double,2>data(data_k8,shape(data_Xsize,data_Ysize),neverDeleteData) ; |
---|
385 | CField::get(fieldid_str)->setData(data) ; |
---|
386 | CTimer::get("XIOS send field").suspend() ; |
---|
387 | CTimer::get("XIOS").suspend() ; |
---|
388 | } |
---|
389 | |
---|
390 | void cxios_write_data_k83(const char * fieldid, int fieldid_size, double * data_k8, int data_Xsize, int data_Ysize, int data_Zsize) |
---|
391 | { |
---|
392 | std::string fieldid_str; |
---|
393 | if (!cstr2string(fieldid, fieldid_size, fieldid_str)) return; |
---|
394 | |
---|
395 | CTimer::get("XIOS").resume() ; |
---|
396 | CTimer::get("XIOS send field").resume() ; |
---|
397 | CContext* context = CContext::getCurrent() ; |
---|
398 | if (!context->hasServer) context->client->checkBuffers() ; |
---|
399 | |
---|
400 | CArray<double,3>data(data_k8,shape(data_Xsize,data_Ysize,data_Zsize),neverDeleteData) ; |
---|
401 | CField::get(fieldid_str)->setData(data) ; |
---|
402 | CTimer::get("XIOS send field").suspend() ; |
---|
403 | CTimer::get("XIOS").suspend() ; |
---|
404 | } |
---|
405 | |
---|
406 | void cxios_write_data_k41(const char * fieldid, int fieldid_size, float * data_k4, int data_Xsize) |
---|
407 | { |
---|
408 | std::string fieldid_str; |
---|
409 | if (!cstr2string(fieldid, fieldid_size, fieldid_str)) return; |
---|
410 | |
---|
411 | CTimer::get("XIOS").resume() ; |
---|
412 | CTimer::get("XIOS send field").resume() ; |
---|
413 | CContext* context = CContext::getCurrent() ; |
---|
414 | if (!context->hasServer) context->client->checkBuffers() ; |
---|
415 | |
---|
416 | CArray<float,1> data_tmp(data_k4,shape(data_Xsize),neverDeleteData) ; |
---|
417 | CArray<double,1> data(data_Xsize) ; |
---|
418 | data=data_tmp ; |
---|
419 | CField::get(fieldid_str)->setData(data) ; |
---|
420 | CTimer::get("XIOS send field").suspend() ; |
---|
421 | CTimer::get("XIOS").suspend() ; |
---|
422 | } |
---|
423 | |
---|
424 | void cxios_write_data_k42(const char * fieldid, int fieldid_size, float * data_k4, int data_Xsize, int data_Ysize) |
---|
425 | { |
---|
426 | std::string fieldid_str; |
---|
427 | if (!cstr2string(fieldid, fieldid_size, fieldid_str)) return; |
---|
428 | |
---|
429 | CTimer::get("XIOS").resume() ; |
---|
430 | CTimer::get("XIOS send field").resume() ; |
---|
431 | CContext* context = CContext::getCurrent() ; |
---|
432 | if (!context->hasServer) context->client->checkBuffers() ; |
---|
433 | |
---|
434 | CArray<float,2> data_tmp(data_k4,shape(data_Xsize,data_Ysize),neverDeleteData) ; |
---|
435 | CArray<double,2> data(data_Xsize,data_Ysize) ; |
---|
436 | data=data_tmp ; |
---|
437 | CField::get(fieldid_str)->setData(data) ; |
---|
438 | CTimer::get("XIOS send field").suspend() ; |
---|
439 | CTimer::get("XIOS").suspend() ; |
---|
440 | } |
---|
441 | |
---|
442 | void cxios_write_data_k43(const char * fieldid, int fieldid_size, float * data_k4, int data_Xsize, int data_Ysize, int data_Zsize) |
---|
443 | { |
---|
444 | std::string fieldid_str; |
---|
445 | |
---|
446 | if (!cstr2string(fieldid, fieldid_size, fieldid_str)) return; |
---|
447 | |
---|
448 | CTimer::get("XIOS").resume() ; |
---|
449 | CTimer::get("XIOS send field").resume() ; |
---|
450 | CContext* context = CContext::getCurrent() ; |
---|
451 | if (!context->hasServer) context->client->checkBuffers() ; |
---|
452 | |
---|
453 | CArray<float,3> data_tmp(data_k4,shape(data_Xsize,data_Ysize,data_Zsize),neverDeleteData) ; |
---|
454 | CArray<double,3> data(data_Xsize,data_Ysize,data_Zsize) ; |
---|
455 | data=data_tmp ; |
---|
456 | |
---|
457 | CField::get(fieldid_str)->setData(data) ; |
---|
458 | CTimer::get("XIOS send field").suspend() ; |
---|
459 | CTimer::get("XIOS").suspend() ; |
---|
460 | |
---|
461 | } |
---|
462 | |
---|
463 | } // extern "C" |
---|