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
  • proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/CheckNumMatlabArguments.cpp

     
     1/*!\file CheckNumMatlabArguments.cpp:
     2 * \brief: check number of arguments and report an usage error message.
     3 */
     4
     5#ifdef HAVE_CONFIG_H
     6        #include <config.h>
     7#else
     8#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
     9#endif
     10
     11
     12#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
     13
     14#include "../../shared/Exceptions/exceptions.h"
     15#include "../../include/include.h"
     16#include "mex.h"
     17
     18int CheckNumMatlabArguments(int nlhs,int NLHS, int nrhs,int NRHS, const char* __FUNCT__, void (*function)( void )){
     19       
     20        /*checks on arguments on the matlab side: */
     21        if (nrhs==0 && nlhs==0) {
     22                /*unless NLHS=0 and NRHS=0, we are just asking for documentation: */
     23                if (NRHS==0 && NLHS==0)return 1;
     24                /* special case: */
     25                function();
     26                _error_("usage: see above");
     27        }
     28        else if (nlhs!=NLHS || nrhs!=NRHS ) {
     29                function();
     30                _error_("usage error.");
     31        }
     32        return 1;
     33}
     34
     35#endif
  • proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/mxGetAssignedField.cpp

     
     1/*!\file: mxGetAssignedField.c:
     2 * \brief: abstract interface on parallel side for i/o, so it ressembles the serial i/o.
     3 *
     4 * In serial mode, this routine takes care of returning the field coming
     5 * from the model. If largesize is 1, we are running out of core models in
     6 * matlab, and we need to call the subsref private method from the model object
     7 * in order to correctly load the data from disk.
     8 */
     9
     10#ifdef HAVE_CONFIG_H
     11#include <config.h>
     12#else
     13#error "Cannot compile without HAVE_CONFIG_H symbol! run configure first!"
     14#endif
     15
     16#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
     17#include "mex.h"
     18
     19mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number,const char* field){
     20
     21        //output
     22        mxArray* mxfield=NULL;
     23       
     24        //input
     25        mxArray    *inputs[2];
     26        mxArray    *pindex      = NULL;
     27        const char *fnames[2];
     28        mwSize      ndim        = 2;
     29        mwSize      onebyone[2] = {1,1};
     30
     31        //We want to call the subsasgn method, and get the returned array.This ensures that if we are running
     32        //large sized problems, the data is truly loaded from disk by the model subsasgn class method.
     33        inputs[0]=(mxArray*)pmxa_array; //this is the model
     34
     35        //create index structure used in the assignment (index.type='.' and index.subs='x' for field x for ex)
     36        fnames[0] = "type";
     37        fnames[1] = "subs";
     38        pindex=mxCreateStructArray( ndim,onebyone,2,fnames);
     39        mxSetField( pindex, 0, "type",mxCreateString("."));
     40        mxSetField( pindex, 0, "subs",mxCreateString(field));
     41        inputs[1]=pindex;
     42
     43        mexCallMATLAB( 1, &mxfield, 2, (mxArray**)inputs, "subsref");
     44
     45        return mxfield;
     46}
     47#endif
  • proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/PrintfFunction.cpp

     
     1/*\file PrintfFunction.c
     2 *\brief: this function is used by the _printf_ macro, to take into account the
     3 *fact we may be running on a cluster.
     4 */
     5
     6#include <stdarg.h>
     7#include <stdio.h>
     8#include "../../shared/shared.h"
     9#include "../../include/include.h"
     10
     11#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
     12#include "mex.h"
     13#endif
     14
     15int PrintfFunction(char* format,...){
     16        /*http://linux.die.net/man/3/vsnprintf*/
     17
     18        /*string to be printed: */
     19        char *buffer = NULL;
     20        int   n,size = 100;
     21        int         string_size;
     22        extern int  my_rank;
     23        extern int  num_procs;
     24                                                                                                                                                                                                     
     25        //variable list of arguments
     26        va_list args;
     27
     28        while(true){
     29
     30                /*allocate buffer for given string size*/
     31                buffer=(char*)xmalloc(size*sizeof(char));
     32
     33                /* Try to print in the allocated space. */
     34                va_start(args, format);
     35#ifndef WIN32
     36                n=vsnprintf(buffer,size,format,args);
     37#else
     38                n=vsnprintf(buffer,size,format,args);
     39#endif
     40                va_end(args);
     41
     42                /* If that worked, return the string. */
     43                if(n>-1 && n<size) break;
     44
     45                /* Else try again with more space. */
     46                if(n>-1)   /* glibc 2.1 */
     47                 size=n+1; /* precisely what is needed */
     48                else       /* glibc 2.0 */
     49                 size*=2;  /* twice the old size */
     50
     51                xfree((void**)&buffer);
     52        }
     53
     54        /*Ok, if we are running in parallel, get node 0 to print*/
     55#if defined(_PARALLEL_)
     56        if(my_rank==0)printf(buffer);
     57#else
     58        mexPrintf(buffer);
     59#endif
     60
     61        /*Clean up and return*/
     62        xfree((void**)&buffer);
     63        return 1;
     64}
  • proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Matlab/matlabio.h

     
    5757OptionChar*     OptionCharParse( char* name, const mxArray* prhs[]);
    5858OptionStruct*   OptionStructParse( char* name, const mxArray* prhs[]);
    5959OptionCell*     OptionCellParse( char* name, const mxArray* prhs[]);
     60
     61mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number, const char* field);
     62int CheckNumMatlabArguments(int nlhs,int NLHS, int nrhs,int NRHS, const char* THISFUNCTION, void (*function)( void ));
     63
    6064#endif
    6165#endif  /* _IO_H_ */
  • proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/pythonio.h

     
    2222#if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
    2323
    2424//void WriteData(mxArray** pdataref,DataSet* dataset);
    25 //void WriteData(mxArray** pdataref,Matrix* matrix);
    2625//void WriteData(mxArray** pdataref,double* matrix, int M,int N);
    2726//void WriteData(mxArray** pdataref,int*    matrix, int M,int N);
    28 //void WriteData(mxArray** pdataref,Vector* vector);
    2927//void WriteData(mxArray** pdataref,double* vector, int M);
    3028//void WriteData(mxArray** pdataref,int integer);
    3129//void WriteData(mxArray** pdataref,bool boolean);
    3230//void WriteData(mxArray** pdataref,double scalar);
    33 //void WriteData(mxArray** pdataref,char* string);
    3431//void WriteData(DataHandle* pdataref,Parameters* parameters);
     32void WriteData(PyObject* py_tuple, int index, char* string);
     33void WriteData(PyObject* py_tuple, int index, Matrix* matrix);
     34void WriteData(PyObject* py_tuple, int index, Vector* vector);
    3535
     36
    3637//void FetchData(DataSet** pdataset,const mxArray* dataref);
    3738//void FetchData(double** pmatrix,int* pM,int *pN,const mxArray* dataref);
    3839//void FetchData(double** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref);
     
    4748//void FetchData(Vector** pvector,const mxArray* dataref);
    4849//void FetchData(char** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref);
    4950//void FetchData(Parameters** pparameters, DataHandle dataref);
    50 void FetchData(char** pstring,char* string);
    51 void FetchData(double* pscalar,double scalar);
    52 void FetchData(int* pinteger,int integer);
    53 void FetchData(bool* pbool,bool boolean);
     51void FetchData(char** pstring,PyObject* py_unicode);
     52void FetchData(double* pscalar,PyObject* py_float);
     53void FetchData(int* pinteger,PyObject* py_long);
     54void FetchData(bool* pbool,PyObject* py_boolean);
     55
     56int CheckNumPythonArguments(PyObject* inputs,int NRHS, void (*function)( void ));
     57
    5458#endif
    5559
    5660#endif  /* _IO_H_ */
  • proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/CheckNumPythonArguments.cpp

     
     1/*!\file CheckNumPythonArguments.cpp:
     2 * \brief: check number of arguments and report an usage error message.
     3 */
     4
     5#ifdef HAVE_CONFIG_H
     6        #include <config.h>
     7#else
     8#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
     9#endif
     10
     11
     12#if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
     13
     14#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
     15#define NO_IMPORT
     16
     17#include "../../toolkits/toolkits.h"
     18#include "../../shared/Exceptions/exceptions.h"
     19#include "../../include/include.h"
     20
     21int CheckNumPythonArguments(PyObject* inputs,int NRHS, void (*function)( void )){
     22
     23        Py_ssize_t size=0;
     24
     25        /*figure out size of tuple in input: */
     26        size=PyTuple_Size(inputs);
     27       
     28        /*check on requested size: */
     29        if (size==0){
     30                function();
     31                _error_("usage: see above");
     32        }
     33        else if (size!=NRHS ) {
     34                function();
     35                _error_("usage error.");
     36        }
     37        return 1;
     38}
     39
     40#endif
  • proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/FetchPythonData.cpp

     
    88#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
    99#endif
    1010
     11#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
     12#define NO_IMPORT
     13
     14#include "../../toolkits/toolkits.h"
     15#include "../../include/include.h"
    1116#include "../../shared/shared.h"
    12 #include "../../include/include.h"
    1317
    1418#if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
    15 /*FUNCTION FetchData(char** pstring,char* string){{{1*/
    16 void FetchData(char** pstring,char* string){
     19/*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{1*/
     20void FetchData(char** pstring,PyObject* py_unicode){
    1721
    18         /*Copy string into string: */
    19         char* outstring=NULL;
     22        PyObject* py_bytes;
     23        char* string=NULL;
    2024
    21         outstring=(char*)xmalloc((strlen(string)+1)*sizeof(char));
    22         strcpy(outstring,string);
     25       
     26        /*convert to bytes format: */
     27        PyUnicode_FSConverter(py_unicode,&py_bytes);
    2328
    24         *pstring=outstring;
     29        /*convert from bytes to string: */
     30        string=PyBytes_AS_STRING(py_bytes);
     31       
     32        *pstring=string;
    2533}
    26 /*FUNCTION FetchData(double* pscalar,double scale){{{1*/
    27 void FetchData(double* pscalar,double scalar){
     34/*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{1*/
     35void FetchData(double* pscalar,PyObject* py_float){
    2836
    29         /*simple copy: */
     37        double scalar;
     38
     39        /*return internal value: */
     40        scalar=PyFloat_AsDouble(py_float);
     41
     42        /*output: */
    3043        *pscalar=scalar;
    3144}
    3245/*}}}*/
    33 /*FUNCTION FetchData(int* pinteger,int integer){{{1*/
    34 void FetchData(int* pinteger, int integer){
     46/*FUNCTION FetchData(int* pinteger,PyObject* py_long){{{1*/
     47void FetchData(int* pinteger, PyObject* py_long){
    3548
    36         /*simple copy: */
     49        int integer;
     50
     51        /*return internal value: */
     52        integer=(int)PyLong_AsLong(py_long);
     53
     54        /*output: */
    3755        *pinteger=integer;
    3856}
    3957/*}}}*/
    40 /*FUNCTION FetchData(bool* pboolean,bool boolean){{{1*/
    41 void FetchData(bool* pboolean,bool boolean){
     58/*FUNCTION FetchData(bool* pboolean,PyObject* py_boolean){{{1*/
     59void FetchData(bool* pboolean,PyObject* py_boolean){
    4260
     61        bool boolean;
     62       
     63        /*check this is indeed a subtype of long type: */
     64        if(!PyBool_Check(py_boolean))_error_("expecting a boolean in input!");
     65
     66        /*extract boolean: */
     67        boolean=(bool)PyLong_AsLong(py_boolean);
     68
    4369        /*simple copy: */
    4470        *pboolean=boolean;
    4571       
  • proj/ice/larour/issm-uci-clean/trunk-jpl/src/c/io/Python/WritePythonData.cpp

     
    88#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
    99#endif
    1010
     11#if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
     12
     13#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
     14#define NO_IMPORT
     15
     16#include "../../toolkits/toolkits.h"
    1117#include "../../include/include.h"
     18#include "../../modules/modules.h"
     19#include "../../Container/Container.h"
    1220#include "../../shared/shared.h"
     21#include "../../io/io.h"
     22#include "../../EnumDefinitions/EnumDefinitions.h"
    1323
    14 #if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
    15 #include <mex.h>
    1624
     25/*FUNCTION WriteData(PyObject* py_tuple,int index,char* string){{{1*/
     26void WriteData(PyObject* py_tuple, int index, char* string){
     27       
     28        PyTuple_SetItem(py_tuple, index, PyUnicode_FromString(string));
     29
     30}
     31/*}}}*/
     32/*FUNCTION WriteData(PyObject* tuple,int index,Matrix* matrix){{{1*/
     33void WriteData(PyObject* tuple,int index,Matrix* matrix){
     34       
     35        int M,N;
     36        double* buffer=NULL;
     37        npy_intp dims[2]={0,0};
     38        PyObject* array=NULL;
     39       
     40        buffer=matrix->ToSerial();
     41        matrix->GetSize(&M,&N);
     42        dims[0]=(npy_intp)M;
     43        dims[1]=(npy_intp)N;
     44        array=PyArray_SimpleNewFromData(2,dims,NPY_DOUBLE,buffer);
     45       
     46        PyTuple_SetItem(tuple, index, array);
     47
     48
     49}
     50/*FUNCTION WriteData(PyObject* py_tuple,int index,Vector* vector){{{1*/
     51void WriteData(PyObject* tuple,int index,Vector* vector){
     52       
     53        int M;
     54        double* buffer=NULL;
     55        npy_intp dim=10;
     56        PyObject* array=NULL;
     57       
     58        buffer=vector->ToMPISerial();
     59        vector->GetSize(&M);
     60        dim=(npy_intp)M;
     61        array=PyArray_SimpleNewFromData(1,&dim,NPY_DOUBLE,buffer);
     62       
     63        PyTuple_SetItem(tuple, index, array);
     64
     65
     66}
     67/*}}}*/
     68
    1769#endif
Note: See TracBrowser for help on using the repository browser.