Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/CheckNumMatlabArguments.cpp =================================================================== --- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/CheckNumMatlabArguments.cpp (revision 0) +++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/CheckNumMatlabArguments.cpp (revision 11972) @@ -0,0 +1,35 @@ +/*!\file CheckNumMatlabArguments.cpp: + * \brief: check number of arguments and report an usage error message. + */ + +#ifdef HAVE_CONFIG_H + #include +#else +#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!" +#endif + + +#if defined(_HAVE_MATLAB_) && defined(_SERIAL_) + +#include "../../shared/Exceptions/exceptions.h" +#include "../../include/include.h" +#include "mex.h" + +int CheckNumMatlabArguments(int nlhs,int NLHS, int nrhs,int NRHS, const char* __FUNCT__, void (*function)( void )){ + + /*checks on arguments on the matlab side: */ + if (nrhs==0 && nlhs==0) { + /*unless NLHS=0 and NRHS=0, we are just asking for documentation: */ + if (NRHS==0 && NLHS==0)return 1; + /* special case: */ + function(); + _error_("usage: see above"); + } + else if (nlhs!=NLHS || nrhs!=NRHS ) { + function(); + _error_("usage error."); + } + return 1; +} + +#endif Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/mxGetAssignedField.cpp =================================================================== --- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/mxGetAssignedField.cpp (revision 0) +++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/mxGetAssignedField.cpp (revision 11972) @@ -0,0 +1,47 @@ +/*!\file: mxGetAssignedField.c: + * \brief: abstract interface on parallel side for i/o, so it ressembles the serial i/o. + * + * In serial mode, this routine takes care of returning the field coming + * from the model. If largesize is 1, we are running out of core models in + * matlab, and we need to call the subsref private method from the model object + * in order to correctly load the data from disk. + */ + +#ifdef HAVE_CONFIG_H +#include +#else +#error "Cannot compile without HAVE_CONFIG_H symbol! run configure first!" +#endif + +#if defined(_HAVE_MATLAB_) && defined(_SERIAL_) +#include "mex.h" + +mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number,const char* field){ + + //output + mxArray* mxfield=NULL; + + //input + mxArray *inputs[2]; + mxArray *pindex = NULL; + const char *fnames[2]; + mwSize ndim = 2; + mwSize onebyone[2] = {1,1}; + + //We want to call the subsasgn method, and get the returned array.This ensures that if we are running + //large sized problems, the data is truly loaded from disk by the model subsasgn class method. + inputs[0]=(mxArray*)pmxa_array; //this is the model + + //create index structure used in the assignment (index.type='.' and index.subs='x' for field x for ex) + fnames[0] = "type"; + fnames[1] = "subs"; + pindex=mxCreateStructArray( ndim,onebyone,2,fnames); + mxSetField( pindex, 0, "type",mxCreateString(".")); + mxSetField( pindex, 0, "subs",mxCreateString(field)); + inputs[1]=pindex; + + mexCallMATLAB( 1, &mxfield, 2, (mxArray**)inputs, "subsref"); + + return mxfield; +} +#endif Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/PrintfFunction.cpp =================================================================== --- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/PrintfFunction.cpp (revision 0) +++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/PrintfFunction.cpp (revision 11972) @@ -0,0 +1,64 @@ +/*\file PrintfFunction.c + *\brief: this function is used by the _printf_ macro, to take into account the + *fact we may be running on a cluster. + */ + +#include +#include +#include "../../shared/shared.h" +#include "../../include/include.h" + +#if defined(_HAVE_MATLAB_) && defined(_SERIAL_) +#include "mex.h" +#endif + +int PrintfFunction(char* format,...){ + /*http://linux.die.net/man/3/vsnprintf*/ + + /*string to be printed: */ + char *buffer = NULL; + int n,size = 100; + int string_size; + extern int my_rank; + extern int num_procs; + + //variable list of arguments + va_list args; + + while(true){ + + /*allocate buffer for given string size*/ + buffer=(char*)xmalloc(size*sizeof(char)); + + /* Try to print in the allocated space. */ + va_start(args, format); +#ifndef WIN32 + n=vsnprintf(buffer,size,format,args); +#else + n=vsnprintf(buffer,size,format,args); +#endif + va_end(args); + + /* If that worked, return the string. */ + if(n>-1 && n-1) /* glibc 2.1 */ + size=n+1; /* precisely what is needed */ + else /* glibc 2.0 */ + size*=2; /* twice the old size */ + + xfree((void**)&buffer); + } + + /*Ok, if we are running in parallel, get node 0 to print*/ +#if defined(_PARALLEL_) + if(my_rank==0)printf(buffer); +#else + mexPrintf(buffer); +#endif + + /*Clean up and return*/ + xfree((void**)&buffer); + return 1; +} Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/matlabio.h =================================================================== --- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/matlabio.h (revision 11971) +++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/matlabio.h (revision 11972) @@ -57,5 +57,9 @@ OptionChar* OptionCharParse( char* name, const mxArray* prhs[]); OptionStruct* OptionStructParse( char* name, const mxArray* prhs[]); OptionCell* OptionCellParse( char* name, const mxArray* prhs[]); + +mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number, const char* field); +int CheckNumMatlabArguments(int nlhs,int NLHS, int nrhs,int NRHS, const char* THISFUNCTION, void (*function)( void )); + #endif #endif /* _IO_H_ */ Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/pythonio.h =================================================================== --- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/pythonio.h (revision 11971) +++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/pythonio.h (revision 11972) @@ -22,17 +22,18 @@ #if defined(_HAVE_PYTHON_) && defined(_SERIAL_) //void WriteData(mxArray** pdataref,DataSet* dataset); -//void WriteData(mxArray** pdataref,Matrix* matrix); //void WriteData(mxArray** pdataref,double* matrix, int M,int N); //void WriteData(mxArray** pdataref,int* matrix, int M,int N); -//void WriteData(mxArray** pdataref,Vector* vector); //void WriteData(mxArray** pdataref,double* vector, int M); //void WriteData(mxArray** pdataref,int integer); //void WriteData(mxArray** pdataref,bool boolean); //void WriteData(mxArray** pdataref,double scalar); -//void WriteData(mxArray** pdataref,char* string); //void WriteData(DataHandle* pdataref,Parameters* parameters); +void WriteData(PyObject* py_tuple, int index, char* string); +void WriteData(PyObject* py_tuple, int index, Matrix* matrix); +void WriteData(PyObject* py_tuple, int index, Vector* vector); + //void FetchData(DataSet** pdataset,const mxArray* dataref); //void FetchData(double** pmatrix,int* pM,int *pN,const mxArray* dataref); //void FetchData(double** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref); @@ -47,10 +48,13 @@ //void FetchData(Vector** pvector,const mxArray* dataref); //void FetchData(char** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref); //void FetchData(Parameters** pparameters, DataHandle dataref); -void FetchData(char** pstring,char* string); -void FetchData(double* pscalar,double scalar); -void FetchData(int* pinteger,int integer); -void FetchData(bool* pbool,bool boolean); +void FetchData(char** pstring,PyObject* py_unicode); +void FetchData(double* pscalar,PyObject* py_float); +void FetchData(int* pinteger,PyObject* py_long); +void FetchData(bool* pbool,PyObject* py_boolean); + +int CheckNumPythonArguments(PyObject* inputs,int NRHS, void (*function)( void )); + #endif #endif /* _IO_H_ */ Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/CheckNumPythonArguments.cpp =================================================================== --- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/CheckNumPythonArguments.cpp (revision 0) +++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/CheckNumPythonArguments.cpp (revision 11972) @@ -0,0 +1,40 @@ +/*!\file CheckNumPythonArguments.cpp: + * \brief: check number of arguments and report an usage error message. + */ + +#ifdef HAVE_CONFIG_H + #include +#else +#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!" +#endif + + +#if defined(_HAVE_PYTHON_) && defined(_SERIAL_) + +#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol +#define NO_IMPORT + +#include "../../toolkits/toolkits.h" +#include "../../shared/Exceptions/exceptions.h" +#include "../../include/include.h" + +int CheckNumPythonArguments(PyObject* inputs,int NRHS, void (*function)( void )){ + + Py_ssize_t size=0; + + /*figure out size of tuple in input: */ + size=PyTuple_Size(inputs); + + /*check on requested size: */ + if (size==0){ + function(); + _error_("usage: see above"); + } + else if (size!=NRHS ) { + function(); + _error_("usage error."); + } + return 1; +} + +#endif Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/FetchPythonData.cpp =================================================================== --- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/FetchPythonData.cpp (revision 11971) +++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/FetchPythonData.cpp (revision 11972) @@ -8,38 +8,64 @@ #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!" #endif +#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol +#define NO_IMPORT + +#include "../../toolkits/toolkits.h" +#include "../../include/include.h" #include "../../shared/shared.h" -#include "../../include/include.h" #if defined(_HAVE_PYTHON_) && defined(_SERIAL_) -/*FUNCTION FetchData(char** pstring,char* string){{{1*/ -void FetchData(char** pstring,char* string){ +/*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{1*/ +void FetchData(char** pstring,PyObject* py_unicode){ - /*Copy string into string: */ - char* outstring=NULL; + PyObject* py_bytes; + char* string=NULL; - outstring=(char*)xmalloc((strlen(string)+1)*sizeof(char)); - strcpy(outstring,string); + + /*convert to bytes format: */ + PyUnicode_FSConverter(py_unicode,&py_bytes); - *pstring=outstring; + /*convert from bytes to string: */ + string=PyBytes_AS_STRING(py_bytes); + + *pstring=string; } -/*FUNCTION FetchData(double* pscalar,double scale){{{1*/ -void FetchData(double* pscalar,double scalar){ +/*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{1*/ +void FetchData(double* pscalar,PyObject* py_float){ - /*simple copy: */ + double scalar; + + /*return internal value: */ + scalar=PyFloat_AsDouble(py_float); + + /*output: */ *pscalar=scalar; } /*}}}*/ -/*FUNCTION FetchData(int* pinteger,int integer){{{1*/ -void FetchData(int* pinteger, int integer){ +/*FUNCTION FetchData(int* pinteger,PyObject* py_long){{{1*/ +void FetchData(int* pinteger, PyObject* py_long){ - /*simple copy: */ + int integer; + + /*return internal value: */ + integer=(int)PyLong_AsLong(py_long); + + /*output: */ *pinteger=integer; } /*}}}*/ -/*FUNCTION FetchData(bool* pboolean,bool boolean){{{1*/ -void FetchData(bool* pboolean,bool boolean){ +/*FUNCTION FetchData(bool* pboolean,PyObject* py_boolean){{{1*/ +void FetchData(bool* pboolean,PyObject* py_boolean){ + bool boolean; + + /*check this is indeed a subtype of long type: */ + if(!PyBool_Check(py_boolean))_error_("expecting a boolean in input!"); + + /*extract boolean: */ + boolean=(bool)PyLong_AsLong(py_boolean); + /*simple copy: */ *pboolean=boolean; Index: /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/WritePythonData.cpp =================================================================== --- /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/WritePythonData.cpp (revision 11971) +++ /proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/WritePythonData.cpp (revision 11972) @@ -8,10 +8,62 @@ #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!" #endif +#if defined(_HAVE_PYTHON_) && defined(_SERIAL_) + +#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol +#define NO_IMPORT + +#include "../../toolkits/toolkits.h" #include "../../include/include.h" +#include "../../modules/modules.h" +#include "../../Container/Container.h" #include "../../shared/shared.h" +#include "../../io/io.h" +#include "../../EnumDefinitions/EnumDefinitions.h" -#if defined(_HAVE_MATLAB_) && defined(_SERIAL_) -#include +/*FUNCTION WriteData(PyObject* py_tuple,int index,char* string){{{1*/ +void WriteData(PyObject* py_tuple, int index, char* string){ + + PyTuple_SetItem(py_tuple, index, PyUnicode_FromString(string)); + +} +/*}}}*/ +/*FUNCTION WriteData(PyObject* tuple,int index,Matrix* matrix){{{1*/ +void WriteData(PyObject* tuple,int index,Matrix* matrix){ + + int M,N; + double* buffer=NULL; + npy_intp dims[2]={0,0}; + PyObject* array=NULL; + + buffer=matrix->ToSerial(); + matrix->GetSize(&M,&N); + dims[0]=(npy_intp)M; + dims[1]=(npy_intp)N; + array=PyArray_SimpleNewFromData(2,dims,NPY_DOUBLE,buffer); + + PyTuple_SetItem(tuple, index, array); + + +} +/*FUNCTION WriteData(PyObject* py_tuple,int index,Vector* vector){{{1*/ +void WriteData(PyObject* tuple,int index,Vector* vector){ + + int M; + double* buffer=NULL; + npy_intp dim=10; + PyObject* array=NULL; + + buffer=vector->ToMPISerial(); + vector->GetSize(&M); + dim=(npy_intp)M; + array=PyArray_SimpleNewFromData(1,&dim,NPY_DOUBLE,buffer); + + PyTuple_SetItem(tuple, index, array); + + +} +/*}}}*/ + #endif