Changeset 22076


Ignore:
Timestamp:
09/13/17 14:24:39 (8 years ago)
Author:
Mathieu Morlighem
Message:

CHG: simplifying Options* interface, not finished yet

Location:
issm/trunk-jpl/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/classes/Options/OptionUtilities.cpp

    r20827 r22076  
    1515/*}}}*/
    1616
    17 int ColumnWiseDimsFromIndex(int* dims,int index,int* size,int ndims){/*{{{*/
    18 
    19         int   i;
    20         int   aprod=1;
    21 
    22         /*check for index too large  */
    23         for (i=0;i<ndims;i++) aprod*=size[i];
    24         if (index >= aprod) _error_("Index " << index << " exceeds number of elements " << aprod << ".");
    25 
    26         /*calculate the dimensions (being careful of integer division)  */
    27         for (i=ndims-1; i>=0; i--) {
    28                 aprod=reCast<int>(((IssmPDouble)aprod+0.5)/(IssmPDouble)size[i]);
    29                 dims[i]=(int)floor(((IssmPDouble)index+0.5)/(IssmPDouble)aprod);
    30                 index-=dims[i]*aprod;
    31         }
    32 
    33         return(0);
    34 }/*}}}*/
    35 int IndexFromColumnWiseDims(int* dims, int* size, int ndims) {/*{{{*/
    36 
    37         int   i;
    38         int   index=0;
    39 
    40         /*check for any dimension too large  */
    41         for (i=0;i<ndims;i++){
    42                 if (dims[i] >= size[i]) _error_("Dimension " << i << " of " << dims[i] << " exceeds size of " << size[i] << ".");
    43         }
    44 
    45         /*calculate the index  */
    46         for (i=ndims-1; i>=0; i--){
    47                 index*=size[i];
    48                 index+=dims[i];
    49         }
    50 
    51         return(index);
    52 }/*}}}*/
    53 int IndexFromRowWiseDims(int* dims, int* size, int ndims) {/*{{{*/
    54 
    55         int   i;
    56         int   index=0;
    57 
    58         /*check for any dimension too large  */
    59         for (i=0; i<ndims; i++){
    60                 if (dims[i] >= size[i]) _error_("Dimension " << i << " of " << dims[i] << " exceeds size of " << size[i] << ".");
    61         }
    62 
    63         /*calculate the index  */
    64         for (i=0; i<ndims; i++) {
    65                 index*=size[i];
    66                 index+=dims[i];
    67         }
    68 
    69         return(index);
    70 }/*}}}*/
    71 int RowWiseDimsFromIndex(int* dims, int index, int* size, int ndims) {/*{{{*/
    72 
    73         int   i;
    74         int   aprod=1;
    75 
    76         /*check for index too large  */
    77         for (i=0; i<ndims; i++) aprod*=size[i];
    78         if (index >= aprod) _error_("Index " << index << " exceeds number of elements " << aprod << ".");
    79 
    80         /*calculate the dimensions (being careful of integer division)  */
    81         for (i=0; i<ndims; i++) {
    82                 aprod=(int)(((IssmPDouble)aprod+0.5)/(IssmPDouble)size[i]);
    83                 dims[i]=(int)floor(((IssmPDouble)index+0.5)/(IssmPDouble)aprod);
    84                 index-=dims[i]*aprod;
    85         }
    86 
    87         return(0);
    88 }/*}}}*/
    89 int StringFromDims(char* cstr, int* dims, int ndims) {/*{{{*/
    90 
    91         sprintf(&cstr[0],"[");
    92         for(int i=0; i<ndims-1; i++) sprintf(&cstr[strlen(cstr)],"%d,",dims[i]);
    93         sprintf(&cstr[strlen(cstr)],"%d]",dims[ndims-1]);
    94 
    95         return(0);
    96 }/*}}}*/
    9717int StringFromSize(char* cstr, int* size, int ndims) {/*{{{*/
    9818
  • issm/trunk-jpl/src/c/classes/Options/OptionUtilities.h

    r20827 r22076  
    1313/*}}}*/
    1414
    15 int ColumnWiseDimsFromIndex(int* dims, int index, int* size, int ndims);
    16 int IndexFromColumnWiseDims(int* dims, int* size, int ndims);
    17 int IndexFromRowWiseDims(int* dims, int* size, int ndims);
    18 int RowWiseDimsFromIndex(int* dims, int index, int* size, int ndims);
    19 int StringFromDims(char* cstr, int* dims, int ndims);
    2015int StringFromSize(char* cstr, int* size, int ndims);
    2116
  • issm/trunk-jpl/src/c/classes/Options/Options.h

    r18521 r22076  
    77#include "./GenericOption.h"
    88
    9 /*!\brief Declaration of Options class.
    10  *
    11  * Declaration of Options class.  Options are vector lists (Containers) of Option objects.
    12  */
    139class Options: public DataSet{
    1410
     
    2016
    2117                /*numerics*/
    22                 int  AddOption(Option* in_oobject);
     18                int     AddOption(Option* in_oobject);
    2319                Option* GetOption(const char* name);
    2420
     
    3127                        if(genericoption){
    3228                                genericoption->Get(pvalue);
    33                         }
    34                         /*Else, the Option does not exist, no default provided*/
    35                         else{
    36                                 _error_("option of name \"" << name << "\" not found, and no default value has been provided");
    37                         }
    38                 }
    39                 /*}}}*/
    40                 template <class OptionType> void Get(OptionType* pvalue,int* pnumel, const char* name){ /*{{{*/
    41 
    42                         /*Get option*/
    43                         GenericOption<OptionType>* genericoption=xDynamicCast<GenericOption<OptionType>*>(GetOption(name));
    44 
    45                         /*If the pointer is not NULL, the option has been found*/
    46                         if(genericoption){
    47                                 genericoption->Get(pvalue);
    48                                 *pnumel=genericoption->NumEl();
    4929                        }
    5030                        /*Else, the Option does not exist, no default provided*/
     
    6545                        else{
    6646                                if(GetOption(name)) _printf_("WARNING: option "<<name<<" found but fetched format not consistent, defaulting...\n");
    67                                 *pvalue=default_value;
    68                         }
    69                 }
    70                 /*}}}*/
    71                 template <class OptionType> void Get(OptionType* pvalue,int* pnumel, const char* name,OptionType default_value){ /*{{{*/
    72 
    73                         /*Get option*/
    74                         GenericOption<OptionType>* genericoption=xDynamicCast<GenericOption<OptionType>*>(GetOption(name));
    75 
    76                         /*If the pointer is not NULL, the option has been found*/
    77                         if(genericoption){
    78                                 genericoption->Get(pvalue);
    79                                 *pnumel=genericoption->NumEl();
    80                         }
    81                         else{
    8247                                *pvalue=default_value;
    8348                        }
  • issm/trunk-jpl/src/wrappers/matlab/io/FetchMatlabData.cpp

    r22000 r22076  
    8888                        }
    8989                        /*Convert matlab n-dim array to double* matrix: */
    90                         MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
     90                        _error_("not supported");
     91                        //MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
    9192                }
    9293        }
     
    223224
    224225                        /*Convert matlab n-dim array to bool* matrix: */
    225                         MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
     226                        _error_("not supported");
     227                        //MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
    226228                }
    227229        }
     
    238240
    239241                        /*Convert matlab n-dim array to double* matrix: */
    240                         MatlabNArrayToNArray(&doublematrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
     242                        _error_("not supported");
     243                        //MatlabNArrayToNArray(&doublematrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
    241244
    242245                        /*Convert double matrix into bool matrix: */
     
    416419
    417420                /*Convert matlab n-dim array to char* matrix: */
    418                 MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
     421                _error_("not supported");
     422                //MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
    419423        }
    420424        else{
     
    757761        return 1;
    758762}/*}}}*/
    759 /*FUNCTION MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
    760 int MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
    761 
    762         int  i,j,rows,cols;
    763         int  numel,ndims;
    764         int *size,*dims;
    765         double* mxmatrix_ptr=NULL;
    766         const mwSize* ipt=NULL;
    767 
    768         /*output: */
    769         double* matrix=NULL;
    770 
    771         /*matlab indices: */
    772         mwIndex *ir    = NULL;
    773         mwIndex *jc    = NULL;
    774         double  *pr    = NULL;
    775         int      count;
    776 
    777         /*get Matlab matrix information: */
    778         numel=mxGetNumberOfElements(mxmatrix);
    779         ndims=mxGetNumberOfDimensions(mxmatrix);
    780         ipt  =mxGetDimensions(mxmatrix);
    781         size =xNew<int>(ndims);
    782         for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
    783 
    784         /*Ok, first check if we are dealing with a sparse or full matrix: */
    785         if (mxIsSparse(mxmatrix)){
    786 
    787                 /*Dealing with sparse matrix: recover size first: */
    788                 rows = mxGetM(mxmatrix);
    789                 cols = mxGetN(mxmatrix);
    790 
    791                 matrix=xNewZeroInit<double>(rows*cols);
    792 
    793                 /*Now, get ir,jc and pr: */
    794                 ir = mxGetIr(mxmatrix);
    795                 jc = mxGetJc(mxmatrix);
    796                 pr = mxGetPr(mxmatrix);
    797 
    798                 /*Now, start inserting data into double* matrix: */
    799                 count=0;
    800                 for(i=0;i<cols;i++){
    801                         for(j=0;j<(jc[i+1]-jc[i]);j++){
    802                                 *(matrix+rows*ir[count]+i)=pr[count];
    803                                 count++;
    804                         }
    805                 }
    806 
    807         }
    808         else{
    809 
    810                 /*Dealing with dense matrix: recover pointer and size: */
    811                 mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
    812 
    813                 /*Create serial matrix: */
    814                 matrix=xNewZeroInit<double>(numel);
    815 
    816                 dims=xNew<int>(ndims);
    817                 for(i=0;i<numel;i++){
    818                         ColumnWiseDimsFromIndex(dims,i,size,ndims);
    819                         j = IndexFromRowWiseDims(dims,size,ndims);
    820                         matrix[j]=(double)mxmatrix_ptr[i];
    821                 }
    822                 xDelete<int>(dims);
    823         }
    824 
    825         /*Assign output pointer: */
    826         *pmatrix       = matrix;
    827         *pmatrix_numel = numel;
    828         *pmatrix_ndims = ndims;
    829         *pmatrix_size  = size;
    830 
    831         return 1;
    832 }
    833 /*}}}*/
    834 /*FUNCTION MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
    835 int MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
    836 
    837         int  i,j,rows,cols;
    838         int  numel,ndims;
    839         int *size,*dims;
    840         bool* mxmatrix_ptr=NULL;
    841         const mwSize* ipt=NULL;
    842 
    843         /*output: */
    844         bool* matrix=NULL;
    845 
    846         /*matlab indices: */
    847         mwIndex *ir    = NULL;
    848         mwIndex *jc    = NULL;
    849         bool    *pm    = NULL;
    850         int      count;
    851 
    852         /*get Matlab matrix information: */
    853         numel = mxGetNumberOfElements(mxmatrix);
    854         ndims = mxGetNumberOfDimensions(mxmatrix);
    855         ipt   = mxGetDimensions(mxmatrix);
    856         size  = xNew<int>(ndims);
    857         for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
    858 
    859         /*Ok, first check if we are dealing with a sparse or full matrix: */
    860         if (mxIsSparse(mxmatrix)){
    861 
    862                 /*Dealing with sparse matrix: recover size first: */
    863                 rows=mxGetM(mxmatrix);
    864                 cols=mxGetN(mxmatrix);
    865                 matrix=xNewZeroInit<bool>(rows*cols);
    866 
    867                 /*Now, get ir,jc and pm: */
    868                 ir=mxGetIr(mxmatrix);
    869                 jc=mxGetJc(mxmatrix);
    870                 pm=(bool*)mxGetData(mxmatrix);
    871 
    872                 /*Now, start inserting data into bool* matrix: */
    873                 count=0;
    874                 for(i=0;i<cols;i++){
    875                         for(j=0;j<(jc[i+1]-jc[i]);j++){
    876                                 matrix[rows*ir[count]+i]=pm[count];
    877                                 count++;
    878                         }
    879                 }
    880         }
    881         else{
    882 
    883                 /*Dealing with dense matrix: recover pointer and size: */
    884                 mxmatrix_ptr=(bool*)mxGetData(mxmatrix);
    885 
    886                 /*Create serial matrix: */
    887                 matrix=xNew<bool>(numel);
    888                 dims=xNew<int>(ndims);
    889                 for(i=0;i<numel;i++){
    890                         ColumnWiseDimsFromIndex(dims,i,size,ndims);
    891                         j=IndexFromRowWiseDims(dims,size,ndims);
    892                         matrix[j]=(bool)mxmatrix_ptr[i];
    893                 }
    894                 xDelete<int>(dims);
    895         }
    896 
    897         /*Assign output pointer: */
    898         *pmatrix       = matrix;
    899         *pmatrix_numel = numel;
    900         *pmatrix_ndims = ndims;
    901         *pmatrix_size  = size;
    902 
    903         return 1;
    904 }
    905 /*}}}*/
    906 /*FUNCTION MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
    907 int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
    908 
    909         int           i,j,rows,cols;
    910         int           numel,ndims;
    911         int          *size , *dims;
    912         mxChar       *mxmatrix_ptr = NULL;
    913         const mwSize *ipt          = NULL;
    914 
    915         /*output: */
    916         char* matrix=NULL;
    917 
    918         /*matlab indices: */
    919         mwIndex *ir    = NULL;
    920         mwIndex *jc    = NULL;
    921         char    *pm    = NULL;
    922         int      count;
    923 
    924         /*get Matlab matrix information: */
    925         numel = mxGetNumberOfElements(mxmatrix);
    926         ndims = mxGetNumberOfDimensions(mxmatrix);
    927         ipt   = mxGetDimensions(mxmatrix);
    928         size  = xNew<int>(ndims);
    929         for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
    930 
    931         /*Ok, first check if we are dealing with a sparse or full matrix: */
    932         if (mxIsSparse(mxmatrix)){
    933 
    934                 /*Dealing with sparse matrix: recover size first: */
    935                 rows = mxGetM(mxmatrix);
    936                 cols = mxGetN(mxmatrix);
    937                 matrix=xNew<char>(rows*cols);
    938 
    939                 /*Now, get ir,jc and pm: */
    940                 ir = mxGetIr(mxmatrix);
    941                 jc = mxGetJc(mxmatrix);
    942                 pm = (char*)mxGetData(mxmatrix);
    943 
    944                 /*Now, start inserting data into char* matrix: */
    945                 count=0;
    946                 for(i=0;i<cols;i++){
    947                         for(j=0;j<(jc[i+1]-jc[i]);j++){
    948                                 matrix[rows*ir[count]+i]=(char)pm[count];
    949                                 count++;
    950                         }
    951                 }
    952         }
    953         else{
    954                 /*Dealing with dense matrix: recover pointer and size: */
    955                 mxmatrix_ptr=mxGetChars(mxmatrix);
    956 
    957                 /*Create serial matrix: */
    958                 matrix=xNew<char>(numel+1);
    959                 matrix[numel]='\0';
    960 
    961                 /*looping code adapted from Matlab example explore.c: */
    962                 int elements_per_page = size[0] * size[1];
    963                 /* total_number_of_pages = size[2] x size[3] x ... x size[N-1] */
    964                 int total_number_of_pages = 1;
    965                 for (i=2; i<ndims; i++) {
    966                         total_number_of_pages *= size[i];
    967                 }
    968 
    969                 i=0;
    970                 for (int page=0; page < total_number_of_pages; page++) {
    971                         int row;
    972                         /* On each page, walk through each row. */
    973                         for (row=0; row<size[0]; row++)  {
    974                                 int column;
    975                                 j = (page * elements_per_page) + row;
    976 
    977                                 /* Walk along each column in the current row. */
    978                                 for (column=0; column<size[1]; column++) {
    979                                         *(matrix+i++)=(char)*(mxmatrix_ptr+j);
    980                                         j += size[0];
    981                                 }
    982                         }
    983                 }
    984         }
    985 
    986         /*Assign output pointer: */
    987         *pmatrix       = matrix;
    988         *pmatrix_numel = numel;
    989         *pmatrix_ndims = ndims;
    990         *pmatrix_size  = size;
    991 
    992         return 1;
    993 }
    994 /*}}}*/
    995763/*FUNCTION mxGetAssignedField{{{*/
    996764mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number,const char* field){
  • issm/trunk-jpl/src/wrappers/matlab/io/matlabio.h

    r22000 r22076  
    7171/*Matlab to double* routines: */
    7272int MatlabMatrixToDoubleMatrix(double** pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix);
    73 int MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix);
    74 int MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix);
    75 int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix);
    7673
    7774/*Print*/
Note: See TracChangeset for help on using the changeset viewer.