Changeset 12060


Ignore:
Timestamp:
04/19/12 15:55:42 (13 years ago)
Author:
Mathieu Morlighem
Message:

do not use Matlab's memory management for internal variable (big mistake)

Location:
issm/trunk-jpl/src/modules
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified issm/trunk-jpl/src/modules/Chaco/Chaco.cpp

    r12050 r12060  
    1818
    1919        /*Inputs: */
    20         int       nvtxs;                /* number of vertices in graph */
    21     int      *start;            /* start of edge list for each vertex */
    22     int      *adjacency;        /* edge list data */
    23     int      *vwgts=NULL;       /* weights for all vertices */
    24         int       nedges;
    25     float    *ewgts=NULL;       /* weights for all edges */
    26     float    *x=NULL;
    27     float    *y=NULL;
    28     float    *z=NULL;           /* coordinates for inertial method */
    29     double    options[10]={1,1,0,0,1,1,50,0,.001,7654321}; /* architecture and partitioning options */
    30     double*   in_options=NULL;
    31     int      *nparts=NULL;      /* number of parts options */
    32         int       npart;
    33     double   *goal=NULL;        /* desired set sizes */
     20        int     nvtxs;               /* number of vertices in graph          */
     21        int    *start;               /* start of edge list for each vertex    */
     22        int    *adjacency;           /* edge list data                        */
     23        int    *vwgts       = NULL;  /* weights for all vertices              */
     24        int     nedges;
     25        float  *ewgts       = NULL;  /* weights for all edges                */
     26        float  *x           = NULL;
     27        float  *y           = NULL;
     28        float  *z           = NULL;  /* coordinates for inertial method      */
     29        double  options[10] = {1,1,0,0,1,1,50,0,.001,7654321}; /* architecture and partitioning options */
     30        double *in_options  = NULL;
     31        int    *nparts      = NULL;   /* number of parts options              */
     32        int     npart;
     33        double *goal        = NULL;   /* desired set sizes                    */
    3434
    3535        /*intermediary pointers: */
     
    3838
    3939        /*output: */
    40     short    *assignment=NULL;  /* set number of each vtx (length nvtxs+1) */
    41         double   *doubleassignment=NULL;        /*holds assignment, in double format, to return to matlab*/
    42 
     40   short  *assignment       = NULL; /* set number of each vtx (length nvtxs+1)                */
     41   double *doubleassignment = NULL; /*holds assignment, in double format, to return to matlab */
    4342
    4443        #ifndef _HAVE_CHACO_ //only works if dakota library has been compiled in.
    4544        _error_(" Chaco not available! Cannot carry out Chaco partitioning!");
    4645        #endif
    47 
    4846
    4947        /*Boot module: */
     
    5351        CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,&ChacoUsage);
    5452
    55 
    5653        /*Fetch adjacency matrix: */
    5754        nvtxs = mxGetN(A_IN);
    58 
    5955        mwstart = mxGetJc(A_IN);
    60         start=(int*)mxMalloc(nvtxs*sizeof(int));
     56        start=(int*)xmalloc(nvtxs*sizeof(int));
    6157        for (i=0; i<nvtxs+1;i++)start[i]=(int)mwstart[i];
    6258
    6359        mwadjacency = mxGetIr(A_IN);
    64         adjacency = (int*)mxMalloc(mxGetNzmax(A_IN)*sizeof(int));
     60        adjacency = (int*)xmalloc(mxGetNzmax(A_IN)*sizeof(int));
    6561        for (i=0; i<mxGetNzmax(A_IN); i++) adjacency[i]= (int)mwadjacency[i];
    6662
    6763        nedges = start[nvtxs];
    6864        if(!mxIsEmpty(EWGTS_IN)){
    69                 ewgts = (float *) mxCalloc(nedges, sizeof(float));
     65                ewgts = (float*)xcalloc(nedges, sizeof(float));
    7066                doublepointer=mxGetPr(A_IN);
    7167                for (i = 0; i < nedges; i++)ewgts[i] = (float)doublepointer[i];
     
    7571        /*Fetch rest of data: */
    7672        FetchData(&vwgts,&nterms,VWGTS_IN);
    77 
    7873        FetchData(&x,&nterms,X_IN);
    7974        FetchData(&y,&nterms,Y_IN);
    8075        FetchData(&z,&nterms,Z_IN);
    81        
    8276        FetchData(&in_options,&nterms,OPTNS_IN);
    8377        for (i=0;i<(nterms<10?nterms:10);i++) options[i]=in_options[i]; //copy in_options into default options
    84        
    8578        FetchData(&npart,NPARTS_IN);
    86         nparts=(int*)mxMalloc(sizeof(int)); nparts[0]=npart; //weird Chacox interface ain't it?
    87 
     79        nparts=(int*)xmalloc(sizeof(int)); nparts[0]=npart; //weird Chacox interface ain't it?
    8880        FetchData(&goal,&nterms,GOAL_IN);
    8981       
     
    112104       
    113105        /*Allocate output: */
    114         assignment = (short *) mxCalloc(nvtxs, sizeof(short));
     106        assignment = (short*)xcalloc(nvtxs, sizeof(short));
    115107       
    116108    /*Call core: */
     
    118110
    119111    /*Output data: */
    120         doubleassignment=(double*)mxMalloc(nvtxs*sizeof(double));
    121         for (i=0;i<nvtxs;i++) doubleassignment[i]=(double)assignment[i];
     112        doubleassignment=(double*)xmalloc(nvtxs*sizeof(double));
     113        for(i=0;i<nvtxs;i++) doubleassignment[i]=(double)assignment[i];
    122114        WriteData(ASSGN_OUT,doubleassignment,nvtxs);
    123115
     
    133125        xfree((void**)&adjacency);
    134126        xfree((void**)&start);
     127        xfree((void**)&doubleassignment);
    135128
    136129        /*end module: */
     
    138131}
    139132
    140 void ChacoUsage( void )
    141 {
     133void ChacoUsage(void){
    142134        _printf_(true,"\n");
    143135        _printf_(true,"Usage: [assgn] = Chaco(A,vwgts,ewgts,x,y,z,options,nparts,goal);\n");
    144136        _printf_(true,"\n");
    145137}
    146 
  • TabularUnified issm/trunk-jpl/src/modules/ContourToMesh/ContourToMesh.cpp

    r12050 r12060  
    2626
    2727        /* required input: */
    28         double* index=NULL;
    29         double* x=NULL;
    30         double* y=NULL;
    3128        int     edgevalue;
    32         char*   interptype=NULL;
     29        double *index      = NULL;
     30        double *x          = NULL;
     31        double *y          = NULL;
     32        char   *interptype = NULL;
    3333
    3434        /* output: */
    35         Vector*  in_nod=NULL;
    36         int  nods;
    37         Vector*  in_elem=NULL;
    38         int  nel;
     35        Vector *in_nod  = NULL;
     36        int     nods;
     37        Vector *in_elem = NULL;
     38        int     nel;
    3939
    4040        //contours
    41         mxArray*  matlabstructure=NULL;
    42         int numcontours;
    43         Contour** contours=NULL;
    44         Contour*  contouri=NULL;
     41        mxArray  *matlabstructure = NULL;
     42        int       numcontours;
     43        Contour **contours        = NULL;
     44        Contour  *contouri        = NULL;
    4545
    4646        /*Boot module: */
     
    6565        //Fetch contours
    6666        numcontours=mxGetNumberOfElements(matlabstructure);
    67         contours=(Contour**)mxMalloc(numcontours*sizeof(Contour*));
     67        contours=(Contour**)xmalloc(numcontours*sizeof(Contour*));
    6868        for(i=0;i<numcontours;i++){
    6969                //allocate
    70                 contouri=(Contour*)mxMalloc(sizeof(Contour));
     70                contouri=(Contour*)xmalloc(sizeof(Contour));
    7171                //retrieve dimension of this contour.
    7272                contouri->nods=(int)mxGetScalar(mxGetField(matlabstructure,i,"nods"));
  • TabularUnified issm/trunk-jpl/src/modules/ContourToNodes/ContourToNodes.cpp

    r12050 r12060  
    6868
    6969        numcontours=mxGetNumberOfElements(matlabstructure);
    70         contours=(Contour**)mxMalloc(numcontours*sizeof(Contour*));
     70        contours=(Contour**)xmalloc(numcontours*sizeof(Contour*));
    7171        for(i=0;i<numcontours;i++){
    7272                //allocate
    73                 contouri=(Contour*)mxMalloc(sizeof(Contour));
     73                contouri=(Contour*)xmalloc(sizeof(Contour));
    7474                //retrieve dimension of this contour.
    7575                contouri->nods=(int)mxGetScalar(mxGetField(matlabstructure,i,"nods"));
  • TabularUnified issm/trunk-jpl/src/modules/InternalFront/InternalFront.cpp

    r12050 r12060  
    3636        /*Allocate and initialize all variables*/
    3737        numberofsegments=0;
    38         front=(int*)mxMalloc(3*numberofelements*4*sizeof(int));
     38        front=(int*)xmalloc(3*numberofelements*4*sizeof(int));
    3939
    4040        /*Loop over all elements on water*/
     
    8181        /*Now that we know how many segments there is we can allocate the final matrix*/
    8282        if(numberofsegments){
    83                 front2=(double*)mxMalloc(4*numberofsegments*sizeof(double));
     83                front2=(double*)xmalloc(4*numberofsegments*sizeof(double));
    8484                for(i=0;i<4*numberofsegments;i++) front2[i]=(double)front[i];
    8585        }
  • TabularUnified issm/trunk-jpl/src/modules/InterpFromMesh2d/InterpFromMesh2d.cpp

    r12050 r12060  
    100100                /*contours: */
    101101                numcontours=mxGetNumberOfElements(matlabstructure);
    102                 contours=(Contour**)mxMalloc(numcontours*sizeof(Contour*));
     102                contours=(Contour**)xmalloc(numcontours*sizeof(Contour*));
    103103                for(i=0;i<numcontours;i++){
    104104                        //allocate
    105                         contouri=(Contour*)mxMalloc(sizeof(Contour));
     105                        contouri=(Contour*)xmalloc(sizeof(Contour));
    106106                        //retrieve dimension of this contour.
    107107                        contouri->nods=(int)mxGetScalar(mxGetField(matlabstructure,i,"nods"));
  • TabularUnified issm/trunk-jpl/src/modules/InterpFromMeshToMesh2d/InterpFromMeshToMesh2d.cpp

    r12050 r12060  
    7878                /*contours: */
    7979                numcontours=mxGetNumberOfElements(matlabstructure);
    80                 contours=(Contour**)mxMalloc(numcontours*sizeof(Contour*));
     80                contours=(Contour**)xmalloc(numcontours*sizeof(Contour*));
    8181                for(i=0;i<numcontours;i++){
    8282                        //allocate
    83                         contouri=(Contour*)mxMalloc(sizeof(Contour));
     83                        contouri=(Contour*)xmalloc(sizeof(Contour));
    8484                        //retrieve dimension of this contour.
    8585                        contouri->nods=(int)mxGetScalar(mxGetField(matlabstructure,i,"nods"));
  • TabularUnified issm/trunk-jpl/src/modules/KMLMeshWrite/KMLMeshWrite.cpp

    r12050 r12060  
    5555                        if (notesi && mxIsChar(notesi) && mxGetNumberOfElements(notesi)) {
    5656                                if (!notes) {
    57                                         notes=(char *) mxMalloc((mxGetNumberOfElements(notesi)+1)*sizeof(char));
     57                                        notes=(char *) xmalloc((mxGetNumberOfElements(notesi)+1)*sizeof(char));
    5858                                        mxGetString(notesi,notes,mxGetNumberOfElements(notesi)+1);
    5959                                }
    6060                                else {
    6161/*  note that strlen does not include trailing null  */
    62                                         notes=(char *) mxRealloc(notes,(strlen(notes)+1+mxGetNumberOfElements(notesi)+1)*sizeof(char));
     62                                        notes=(char*)xrealloc(notes,(strlen(notes)+1+mxGetNumberOfElements(notesi)+1)*sizeof(char));
    6363                                        strcat(notes,"\n");
    6464                                        mxGetString(notesi,&notes[strlen(notes)],mxGetNumberOfElements(notesi)+1);
  • TabularUnified issm/trunk-jpl/src/modules/MeshPartition/MeshPartition.cpp

    r12050 r12060  
    7474
    7575        /*Post process node_partitioning and element_partitioning to be in double format. Metis needed them in int* format: */
    76         element_partitioning=(double*)mxMalloc(numberofelements*sizeof(double));
     76        element_partitioning=(double*)xmalloc(numberofelements*sizeof(double));
    7777        for (i=0;i<numberofelements;i++){
    7878                element_partitioning[i]=(double)int_element_partitioning[i]+1; //Metis indexing from 0, matlab from 1.
    7979        }
    8080       
    81         node_partitioning=(double*)mxMalloc(numberofvertices*sizeof(double));
     81        node_partitioning=(double*)xmalloc(numberofvertices*sizeof(double));
    8282        for (i=0;i<numberofvertices;i++){
    8383                node_partitioning[i]=(double)int_node_partitioning[i]+1; //Metis indexing from 0, matlab from 1.
  • TabularUnified issm/trunk-jpl/src/modules/MeshProfileIntersection/MeshProfileIntersection.cpp

    r12050 r12060  
    5555        FetchData(&double_index,&nel,&dummy,INDEX);
    5656        if(dummy!=3 && dummy!=6)_error_(" element triangulation should be of 3 or 6 column width!");
    57         index=(int*)mxMalloc(nel*3*sizeof(int));
     57        index=(int*)xmalloc(nel*3*sizeof(int));
    5858        for(i=0;i<nel;i++){
    5959                for(j=0;j<3;j++){
     
    6767        //contours
    6868        numcontours=mxGetNumberOfElements(matlabstructure);
    69         contours=(Contour**)mxMalloc(numcontours*sizeof(Contour*));
     69        contours=(Contour**)xmalloc(numcontours*sizeof(Contour*));
    7070        for(i=0;i<numcontours;i++){
    7171                //allocate
    72                 contouri=(Contour*)mxMalloc(sizeof(Contour));
     72                contouri=(Contour*)xmalloc(sizeof(Contour));
    7373                //retrieve dimension of this contour.
    7474                contouri->nods=(int)mxGetScalar(mxGetField(matlabstructure,i,"nods"));
  • TabularUnified issm/trunk-jpl/src/modules/TriMeshProcessRifts/TriMeshProcessRifts.cpp

    r12050 r12060  
    7474                nel=mxGetM(prhs[0]);
    7575                tindex_in=mxGetPr(prhs[0]);
    76                 index_in=(double*)mxMalloc(nel*3*sizeof(double));
     76                index_in=(double*)xmalloc(nel*3*sizeof(double));
    7777                for (i=0;i<nel;i++){
    7878                        for (j=0;j<3;j++){
     
    9090                nods=mxGetM(prhs[1]);
    9191                x_inm=mxGetPr(prhs[1]);
    92                 x_in=(double*)mxMalloc(nods*sizeof(double));
     92                x_in=(double*)xmalloc(nods*sizeof(double));
    9393                for (i=0;i<nods;i++){
    9494                        x_in[i]=x_inm[i];
     
    103103        if(mxIsDouble(prhs[2])){
    104104                y_inm=mxGetPr(prhs[2]);
    105                 y_in=(double*)mxMalloc(nods*sizeof(double));
     105                y_in=(double*)xmalloc(nods*sizeof(double));
    106106                for (i=0;i<nods;i++){
    107107                        y_in[i]=y_inm[i];
     
    117117                num_seg=mxGetM(prhs[3]);
    118118                tsegments_in=mxGetPr(prhs[3]);
    119                 segments_in=(double*)mxMalloc(num_seg*3*sizeof(double));
     119                segments_in=(double*)xmalloc(num_seg*3*sizeof(double));
    120120                for (i=0;i<num_seg;i++){
    121121                        for (j=0;j<3;j++){
     
    132132        if(mxIsDouble(prhs[4])){
    133133                tsegmentmarkers_in=mxGetPr(prhs[4]);
    134                 segmentmarkers_in=(double*)mxMalloc(num_seg*sizeof(double));
     134                segmentmarkers_in=(double*)xmalloc(num_seg*sizeof(double));
    135135                for (i=0;i<num_seg;i++){
    136136                        segmentmarkers_in[i]=tsegmentmarkers_in[i];
     
    270270                        pmxa_array2= mxCreateDoubleMatrix(0,0,mxREAL);
    271271                        mxSetM(pmxa_array2,1);
    272                         pair=(double*)mxMalloc(2*sizeof(double));
     272                        pair=(double*)xmalloc(2*sizeof(double));
    273273                        pair[0]=*(out_riftstips+2*i+0);
    274274                        pair[1]=*(out_riftstips+2*i+1);
     
    293293
    294294                        /*State: */
    295                         state=(double*)mxMalloc(out_riftsnumpenaltypairs[i]*sizeof(double));
     295                        state=(double*)xmalloc(out_riftsnumpenaltypairs[i]*sizeof(double));
    296296                        for(j=0;j<out_riftsnumpenaltypairs[i];j++)state[j]=FreeEnum;
    297297                        pmxa_array2= mxCreateDoubleMatrix(0,0,mxREAL);
     
    306306        else{
    307307                /*output NaN :*/
    308                 pNaN=(double*)mxMalloc(sizeof(double));
     308                pNaN=(double*)xmalloc(sizeof(double));
    309309                *pNaN=NAN;
    310310                pmxa_array= mxCreateDoubleMatrix(0,0,mxREAL);
  • TabularUnified issm/trunk-jpl/src/modules/TriMeshRefine/TriMeshRefine.cpp

    r12050 r12060  
    6565                nel=mxGetM(prhs[0]);
    6666                tindex_in=mxGetPr(prhs[0]);
    67                 index_in=(double*)mxMalloc(nel*3*sizeof(double));
     67                index_in=(double*)xmalloc(nel*3*sizeof(double));
    6868                for (i=0;i<nel;i++){
    6969                        for (j=0;j<3;j++){
     
    8181                nods=mxGetM(prhs[1]);
    8282                x_inm=mxGetPr(prhs[1]);
    83                 x_in=(double*)mxMalloc(nods*sizeof(double));
     83                x_in=(double*)xmalloc(nods*sizeof(double));
    8484                for (i=0;i<nods;i++){
    8585                        x_in[i]=x_inm[i];
     
    9494        if(mxIsDouble(prhs[2])){
    9595                y_inm=mxGetPr(prhs[2]);
    96                 y_in=(double*)mxMalloc(nods*sizeof(double));
     96                y_in=(double*)xmalloc(nods*sizeof(double));
    9797                for (i=0;i<nods;i++){
    9898                        y_in[i]=y_inm[i];
     
    108108                num_seg=mxGetM(prhs[3]);
    109109                tsegments_in=mxGetPr(prhs[3]);
    110                 segments_in=(double*)mxMalloc(num_seg*3*sizeof(double));
     110                segments_in=(double*)xmalloc(num_seg*3*sizeof(double));
    111111                for (i=0;i<num_seg;i++){
    112112                        for (j=0;j<3;j++){
     
    123123        if(mxIsDouble(prhs[4])){
    124124                tsegmentmarkers_in=mxGetPr(prhs[4]);
    125                 segmentmarkers_in=(double*)mxMalloc(num_seg*sizeof(double));
     125                segmentmarkers_in=(double*)xmalloc(num_seg*sizeof(double));
    126126                for (i=0;i<num_seg;i++){
    127127                        segmentmarkers_in[i]=tsegmentmarkers_in[i];
     
    152152                        mexErrMsgTxt(" ");
    153153                }
    154                 order = (char *) mxMalloc((mxGetN(prhs[6])+1)*sizeof(char));
     154                order = (char *) xmalloc((mxGetN(prhs[6])+1)*sizeof(char));
    155155                mxGetString(prhs[6],order,mxGetN(prhs[6])+1);
    156156        }
     
    186186        in.numberofcorners=3;
    187187       
    188         in.trianglelist = (int *) mxMalloc(3*in.numberoftriangles * sizeof(int));
     188        in.trianglelist = (int *) xmalloc(3*in.numberoftriangles * sizeof(int));
    189189        for(i=0;i<in.numberoftriangles;i++){
    190190                for(j=0;j<3;j++){
     
    192192                }
    193193        }
    194         in.triangleattributelist = (REAL *) mxMalloc(in.numberoftriangles * in.numberoftriangleattributes * sizeof(REAL));
     194        in.triangleattributelist = (REAL *) xmalloc(in.numberoftriangles * in.numberoftriangleattributes * sizeof(REAL));
    195195        for(i=0;i<in.numberoftriangles;i++){
    196196                in.triangleattributelist[i]=0.0;
    197197        }
    198         in.trianglearealist = (REAL *) mxMalloc(in.numberoftriangles * sizeof(REAL));
     198        in.trianglearealist = (REAL *) xmalloc(in.numberoftriangles * sizeof(REAL));
    199199        for(i=0;i<in.numberoftriangles;i++){
    200200                in.trianglearealist[i]=area[i];
     
    203203        in.numberofpoints=nods;
    204204        in.numberofpointattributes=1;
    205         in.pointlist = (REAL *) mxMalloc(in.numberofpoints * 2 * sizeof(REAL));
     205        in.pointlist = (REAL *) xmalloc(in.numberofpoints * 2 * sizeof(REAL));
    206206        for (i=0;i<nods;i++){
    207207                in.pointlist[2*i+0]=x_in[i];
    208208                in.pointlist[2*i+1]=y_in[i];
    209209        }
    210         in.pointattributelist = (REAL *) mxMalloc(in.numberofpoints * in.numberofpointattributes * sizeof(REAL));
     210        in.pointattributelist = (REAL *) xmalloc(in.numberofpoints * in.numberofpointattributes * sizeof(REAL));
    211211        for (i=0;i<nods;i++){
    212212                in.pointattributelist[i] = 0.0;
     
    214214
    215215        in.numberofsegments = num_seg;
    216         in.segmentlist = (int *) mxMalloc(in.numberofsegments * 2 * sizeof(REAL));
     216        in.segmentlist = (int *) xmalloc(in.numberofsegments * 2 * sizeof(REAL));
    217217        in.segmentmarkerlist = (int *) mxCalloc(in.numberofsegments,sizeof(int));
    218218        for (i=0;i<num_seg;i++){
     
    250250       
    251251        /*Allocate index, x and y: */
    252         index=(double*)mxMalloc(3*out.numberoftriangles*sizeof(double));
    253         x=(double*)mxMalloc(out.numberofpoints*sizeof(double));
    254         y=(double*)mxMalloc(out.numberofpoints*sizeof(double));
    255         segments=(double*)mxMalloc(3*out.numberofsegments*sizeof(double));
    256         segmentmarkerlist=(double*)mxMalloc(out.numberofsegments*sizeof(double));
     252        index=(double*)xmalloc(3*out.numberoftriangles*sizeof(double));
     253        x=(double*)xmalloc(out.numberofpoints*sizeof(double));
     254        y=(double*)xmalloc(out.numberofpoints*sizeof(double));
     255        segments=(double*)xmalloc(3*out.numberofsegments*sizeof(double));
     256        segmentmarkerlist=(double*)xmalloc(out.numberofsegments*sizeof(double));
    257257
    258258        for (i = 0; i < out.numberoftriangles; i++) {
Note: See TracChangeset for help on using the changeset viewer.