source: issm/oecreview/Archive/11961-11984/ISSM-11971-11972.diff@ 11991

Last change on this file since 11991 was 11991, checked in by Eric.Larour, 13 years ago

oecreview from 11518 to present

File size: 13.7 KB
RevLine 
[11991]1Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/CheckNumMatlabArguments.cpp
2===================================================================
3--- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/CheckNumMatlabArguments.cpp (revision 0)
4+++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/CheckNumMatlabArguments.cpp (revision 11972)
5@@ -0,0 +1,35 @@
6+/*!\file CheckNumMatlabArguments.cpp:
7+ * \brief: check number of arguments and report an usage error message.
8+ */
9+
10+#ifdef HAVE_CONFIG_H
11+ #include <config.h>
12+#else
13+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
14+#endif
15+
16+
17+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
18+
19+#include "../../shared/Exceptions/exceptions.h"
20+#include "../../include/include.h"
21+#include "mex.h"
22+
23+int CheckNumMatlabArguments(int nlhs,int NLHS, int nrhs,int NRHS, const char* __FUNCT__, void (*function)( void )){
24+
25+ /*checks on arguments on the matlab side: */
26+ if (nrhs==0 && nlhs==0) {
27+ /*unless NLHS=0 and NRHS=0, we are just asking for documentation: */
28+ if (NRHS==0 && NLHS==0)return 1;
29+ /* special case: */
30+ function();
31+ _error_("usage: see above");
32+ }
33+ else if (nlhs!=NLHS || nrhs!=NRHS ) {
34+ function();
35+ _error_("usage error.");
36+ }
37+ return 1;
38+}
39+
40+#endif
41Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/mxGetAssignedField.cpp
42===================================================================
43--- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/mxGetAssignedField.cpp (revision 0)
44+++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/mxGetAssignedField.cpp (revision 11972)
45@@ -0,0 +1,47 @@
46+/*!\file: mxGetAssignedField.c:
47+ * \brief: abstract interface on parallel side for i/o, so it ressembles the serial i/o.
48+ *
49+ * In serial mode, this routine takes care of returning the field coming
50+ * from the model. If largesize is 1, we are running out of core models in
51+ * matlab, and we need to call the subsref private method from the model object
52+ * in order to correctly load the data from disk.
53+ */
54+
55+#ifdef HAVE_CONFIG_H
56+#include <config.h>
57+#else
58+#error "Cannot compile without HAVE_CONFIG_H symbol! run configure first!"
59+#endif
60+
61+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
62+#include "mex.h"
63+
64+mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number,const char* field){
65+
66+ //output
67+ mxArray* mxfield=NULL;
68+
69+ //input
70+ mxArray *inputs[2];
71+ mxArray *pindex = NULL;
72+ const char *fnames[2];
73+ mwSize ndim = 2;
74+ mwSize onebyone[2] = {1,1};
75+
76+ //We want to call the subsasgn method, and get the returned array.This ensures that if we are running
77+ //large sized problems, the data is truly loaded from disk by the model subsasgn class method.
78+ inputs[0]=(mxArray*)pmxa_array; //this is the model
79+
80+ //create index structure used in the assignment (index.type='.' and index.subs='x' for field x for ex)
81+ fnames[0] = "type";
82+ fnames[1] = "subs";
83+ pindex=mxCreateStructArray( ndim,onebyone,2,fnames);
84+ mxSetField( pindex, 0, "type",mxCreateString("."));
85+ mxSetField( pindex, 0, "subs",mxCreateString(field));
86+ inputs[1]=pindex;
87+
88+ mexCallMATLAB( 1, &mxfield, 2, (mxArray**)inputs, "subsref");
89+
90+ return mxfield;
91+}
92+#endif
93Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/PrintfFunction.cpp
94===================================================================
95--- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/PrintfFunction.cpp (revision 0)
96+++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/PrintfFunction.cpp (revision 11972)
97@@ -0,0 +1,64 @@
98+/*\file PrintfFunction.c
99+ *\brief: this function is used by the _printf_ macro, to take into account the
100+ *fact we may be running on a cluster.
101+ */
102+
103+#include <stdarg.h>
104+#include <stdio.h>
105+#include "../../shared/shared.h"
106+#include "../../include/include.h"
107+
108+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
109+#include "mex.h"
110+#endif
111+
112+int PrintfFunction(char* format,...){
113+ /*http://linux.die.net/man/3/vsnprintf*/
114+
115+ /*string to be printed: */
116+ char *buffer = NULL;
117+ int n,size = 100;
118+ int string_size;
119+ extern int my_rank;
120+ extern int num_procs;
121+
122+ //variable list of arguments
123+ va_list args;
124+
125+ while(true){
126+
127+ /*allocate buffer for given string size*/
128+ buffer=(char*)xmalloc(size*sizeof(char));
129+
130+ /* Try to print in the allocated space. */
131+ va_start(args, format);
132+#ifndef WIN32
133+ n=vsnprintf(buffer,size,format,args);
134+#else
135+ n=vsnprintf(buffer,size,format,args);
136+#endif
137+ va_end(args);
138+
139+ /* If that worked, return the string. */
140+ if(n>-1 && n<size) break;
141+
142+ /* Else try again with more space. */
143+ if(n>-1) /* glibc 2.1 */
144+ size=n+1; /* precisely what is needed */
145+ else /* glibc 2.0 */
146+ size*=2; /* twice the old size */
147+
148+ xfree((void**)&buffer);
149+ }
150+
151+ /*Ok, if we are running in parallel, get node 0 to print*/
152+#if defined(_PARALLEL_)
153+ if(my_rank==0)printf(buffer);
154+#else
155+ mexPrintf(buffer);
156+#endif
157+
158+ /*Clean up and return*/
159+ xfree((void**)&buffer);
160+ return 1;
161+}
162Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/matlabio.h
163===================================================================
164--- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/matlabio.h (revision 11971)
165+++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/matlabio.h (revision 11972)
166@@ -57,5 +57,9 @@
167 OptionChar* OptionCharParse( char* name, const mxArray* prhs[]);
168 OptionStruct* OptionStructParse( char* name, const mxArray* prhs[]);
169 OptionCell* OptionCellParse( char* name, const mxArray* prhs[]);
170+
171+mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number, const char* field);
172+int CheckNumMatlabArguments(int nlhs,int NLHS, int nrhs,int NRHS, const char* THISFUNCTION, void (*function)( void ));
173+
174 #endif
175 #endif /* _IO_H_ */
176Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/pythonio.h
177===================================================================
178--- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/pythonio.h (revision 11971)
179+++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/pythonio.h (revision 11972)
180@@ -22,17 +22,18 @@
181 #if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
182
183 //void WriteData(mxArray** pdataref,DataSet* dataset);
184-//void WriteData(mxArray** pdataref,Matrix* matrix);
185 //void WriteData(mxArray** pdataref,double* matrix, int M,int N);
186 //void WriteData(mxArray** pdataref,int* matrix, int M,int N);
187-//void WriteData(mxArray** pdataref,Vector* vector);
188 //void WriteData(mxArray** pdataref,double* vector, int M);
189 //void WriteData(mxArray** pdataref,int integer);
190 //void WriteData(mxArray** pdataref,bool boolean);
191 //void WriteData(mxArray** pdataref,double scalar);
192-//void WriteData(mxArray** pdataref,char* string);
193 //void WriteData(DataHandle* pdataref,Parameters* parameters);
194+void WriteData(PyObject* py_tuple, int index, char* string);
195+void WriteData(PyObject* py_tuple, int index, Matrix* matrix);
196+void WriteData(PyObject* py_tuple, int index, Vector* vector);
197
198+
199 //void FetchData(DataSet** pdataset,const mxArray* dataref);
200 //void FetchData(double** pmatrix,int* pM,int *pN,const mxArray* dataref);
201 //void FetchData(double** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref);
202@@ -47,10 +48,13 @@
203 //void FetchData(Vector** pvector,const mxArray* dataref);
204 //void FetchData(char** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref);
205 //void FetchData(Parameters** pparameters, DataHandle dataref);
206-void FetchData(char** pstring,char* string);
207-void FetchData(double* pscalar,double scalar);
208-void FetchData(int* pinteger,int integer);
209-void FetchData(bool* pbool,bool boolean);
210+void FetchData(char** pstring,PyObject* py_unicode);
211+void FetchData(double* pscalar,PyObject* py_float);
212+void FetchData(int* pinteger,PyObject* py_long);
213+void FetchData(bool* pbool,PyObject* py_boolean);
214+
215+int CheckNumPythonArguments(PyObject* inputs,int NRHS, void (*function)( void ));
216+
217 #endif
218
219 #endif /* _IO_H_ */
220Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/CheckNumPythonArguments.cpp
221===================================================================
222--- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/CheckNumPythonArguments.cpp (revision 0)
223+++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/CheckNumPythonArguments.cpp (revision 11972)
224@@ -0,0 +1,40 @@
225+/*!\file CheckNumPythonArguments.cpp:
226+ * \brief: check number of arguments and report an usage error message.
227+ */
228+
229+#ifdef HAVE_CONFIG_H
230+ #include <config.h>
231+#else
232+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
233+#endif
234+
235+
236+#if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
237+
238+#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
239+#define NO_IMPORT
240+
241+#include "../../toolkits/toolkits.h"
242+#include "../../shared/Exceptions/exceptions.h"
243+#include "../../include/include.h"
244+
245+int CheckNumPythonArguments(PyObject* inputs,int NRHS, void (*function)( void )){
246+
247+ Py_ssize_t size=0;
248+
249+ /*figure out size of tuple in input: */
250+ size=PyTuple_Size(inputs);
251+
252+ /*check on requested size: */
253+ if (size==0){
254+ function();
255+ _error_("usage: see above");
256+ }
257+ else if (size!=NRHS ) {
258+ function();
259+ _error_("usage error.");
260+ }
261+ return 1;
262+}
263+
264+#endif
265Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/FetchPythonData.cpp
266===================================================================
267--- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/FetchPythonData.cpp (revision 11971)
268+++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/FetchPythonData.cpp (revision 11972)
269@@ -8,38 +8,64 @@
270 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
271 #endif
272
273+#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
274+#define NO_IMPORT
275+
276+#include "../../toolkits/toolkits.h"
277+#include "../../include/include.h"
278 #include "../../shared/shared.h"
279-#include "../../include/include.h"
280
281 #if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
282-/*FUNCTION FetchData(char** pstring,char* string){{{1*/
283-void FetchData(char** pstring,char* string){
284+/*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{1*/
285+void FetchData(char** pstring,PyObject* py_unicode){
286
287- /*Copy string into string: */
288- char* outstring=NULL;
289+ PyObject* py_bytes;
290+ char* string=NULL;
291
292- outstring=(char*)xmalloc((strlen(string)+1)*sizeof(char));
293- strcpy(outstring,string);
294+
295+ /*convert to bytes format: */
296+ PyUnicode_FSConverter(py_unicode,&py_bytes);
297
298- *pstring=outstring;
299+ /*convert from bytes to string: */
300+ string=PyBytes_AS_STRING(py_bytes);
301+
302+ *pstring=string;
303 }
304-/*FUNCTION FetchData(double* pscalar,double scale){{{1*/
305-void FetchData(double* pscalar,double scalar){
306+/*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{1*/
307+void FetchData(double* pscalar,PyObject* py_float){
308
309- /*simple copy: */
310+ double scalar;
311+
312+ /*return internal value: */
313+ scalar=PyFloat_AsDouble(py_float);
314+
315+ /*output: */
316 *pscalar=scalar;
317 }
318 /*}}}*/
319-/*FUNCTION FetchData(int* pinteger,int integer){{{1*/
320-void FetchData(int* pinteger, int integer){
321+/*FUNCTION FetchData(int* pinteger,PyObject* py_long){{{1*/
322+void FetchData(int* pinteger, PyObject* py_long){
323
324- /*simple copy: */
325+ int integer;
326+
327+ /*return internal value: */
328+ integer=(int)PyLong_AsLong(py_long);
329+
330+ /*output: */
331 *pinteger=integer;
332 }
333 /*}}}*/
334-/*FUNCTION FetchData(bool* pboolean,bool boolean){{{1*/
335-void FetchData(bool* pboolean,bool boolean){
336+/*FUNCTION FetchData(bool* pboolean,PyObject* py_boolean){{{1*/
337+void FetchData(bool* pboolean,PyObject* py_boolean){
338
339+ bool boolean;
340+
341+ /*check this is indeed a subtype of long type: */
342+ if(!PyBool_Check(py_boolean))_error_("expecting a boolean in input!");
343+
344+ /*extract boolean: */
345+ boolean=(bool)PyLong_AsLong(py_boolean);
346+
347 /*simple copy: */
348 *pboolean=boolean;
349
350Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/WritePythonData.cpp
351===================================================================
352--- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/WritePythonData.cpp (revision 11971)
353+++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/WritePythonData.cpp (revision 11972)
354@@ -8,10 +8,62 @@
355 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
356 #endif
357
358+#if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
359+
360+#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
361+#define NO_IMPORT
362+
363+#include "../../toolkits/toolkits.h"
364 #include "../../include/include.h"
365+#include "../../modules/modules.h"
366+#include "../../Container/Container.h"
367 #include "../../shared/shared.h"
368+#include "../../io/io.h"
369+#include "../../EnumDefinitions/EnumDefinitions.h"
370
371-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
372-#include <mex.h>
373
374+/*FUNCTION WriteData(PyObject* py_tuple,int index,char* string){{{1*/
375+void WriteData(PyObject* py_tuple, int index, char* string){
376+
377+ PyTuple_SetItem(py_tuple, index, PyUnicode_FromString(string));
378+
379+}
380+/*}}}*/
381+/*FUNCTION WriteData(PyObject* tuple,int index,Matrix* matrix){{{1*/
382+void WriteData(PyObject* tuple,int index,Matrix* matrix){
383+
384+ int M,N;
385+ double* buffer=NULL;
386+ npy_intp dims[2]={0,0};
387+ PyObject* array=NULL;
388+
389+ buffer=matrix->ToSerial();
390+ matrix->GetSize(&M,&N);
391+ dims[0]=(npy_intp)M;
392+ dims[1]=(npy_intp)N;
393+ array=PyArray_SimpleNewFromData(2,dims,NPY_DOUBLE,buffer);
394+
395+ PyTuple_SetItem(tuple, index, array);
396+
397+
398+}
399+/*FUNCTION WriteData(PyObject* py_tuple,int index,Vector* vector){{{1*/
400+void WriteData(PyObject* tuple,int index,Vector* vector){
401+
402+ int M;
403+ double* buffer=NULL;
404+ npy_intp dim=10;
405+ PyObject* array=NULL;
406+
407+ buffer=vector->ToMPISerial();
408+ vector->GetSize(&M);
409+ dim=(npy_intp)M;
410+ array=PyArray_SimpleNewFromData(1,&dim,NPY_DOUBLE,buffer);
411+
412+ PyTuple_SetItem(tuple, index, array);
413+
414+
415+}
416+/*}}}*/
417+
418 #endif
Note: See TracBrowser for help on using the repository browser.