Thread Subject: MATLAB hangs with MEX code

Subject: MATLAB hangs with MEX code

From: Jose Antonio

Date: 10 Aug, 2009 20:12:18

Message: 1 of 8

Hi everybody


I am trying to make a MEX function to split a double class grey scale image into 2^N areas, which I had already done successfully in MATLAB.

The thing is that the compiled function works properly for N up to 8, and then MATLAB simply hangs, and I have to stop the process.

I guess I might be again messing up with memory management, but I find no clue how to get things to work. Any help will be very welcome!


Thanks in advance, and here it's the code:


#include "mex.h"
#include <math.h>

/*
* Median_Cut.c
* Used to speed up the Energy Split calculations
*
* [cVList, cHList] = Median_Cut(imageIn, N)
* imageIn is double, N is int
*
* This is a MEX-file for MATLAB.
*/


/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* variable declarations here */
int N, iRows, iCols, index, numTiles, sizeList, filledVals, row, col;
int *cVList, *cHList, *cVListTemp, *cHListTemp;
double energyT, energy;
double *imageIn, *energyP, *energyPtemp;
int n, k, i, j;

imageIn = mxGetPr(prhs[0]);
iRows = mxGetM(prhs[0]);
iCols = mxGetN(prhs[0]);

N = *(mxGetPr(prhs[1]));

numTiles = pow(2,N);
sizeList = 2*numTiles;


/* Allocate intermediate & output memory */
plhs[0] = mxCreateNumericMatrix(1, sizeList, mxINT16_CLASS, mxREAL);
cVList = (int *)mxGetData(plhs[0]);
plhs[1] = mxCreateNumericMatrix(1, sizeList, mxINT16_CLASS, mxREAL);
cHList = (int *)mxGetData(plhs[1]);

cVListTemp = mxCalloc(sizeList, sizeof(int));
cHListTemp = mxCalloc(sizeList, sizeof(int));
energyP = mxCalloc(numTiles, sizeof(int));
energyPtemp = mxCalloc(numTiles, sizeof(int));

cVList[0] = cHList[0] = 1;
cVList[1] = iRows;
cHList[1] = iCols;

filledVals = 2;
energyT = 0;


/* code here */
for (i = 0; i < iRows; i++)
{
for (j = 0; j < iCols; j++)
{
index = i * iCols + j;
energyT += imageIn[index];
}
}
energyP[0] = energyT;

for (n = 0; n < N; n++)
{
for (k = 0; k < filledVals; k+=2)
energyPtemp[k] = energyP[k];

for (k = 0; k < filledVals; k+=2)
{
energy = 0;
row = col = 0;
if ((cVList[k+1] - cVList[k]) > (cHList[k+1] - cHList[k]))
{
while (energy < energyP[k/2]/2)
{
for (j = cHList[k]-1; j < cHList[k+1]; j++)
{
index = (cVList[k] + row - 1) * iCols + j;
energy += imageIn[index];
}
row++;
}
cVListTemp[0 + 2*k] = cVList[k];
cVListTemp[1 + 2*k] = cVList[k] + row;
cVListTemp[2 + 2*k] = cVList[k] + row + 1;
cVListTemp[3 + 2*k] = cVList[k+1];
cHListTemp[0 + 2*k] = cHList[k];
cHListTemp[1 + 2*k] = cHList[k+1];
cHListTemp[2 + 2*k] = cHList[k];
cHListTemp[3 + 2*k] = cHList[k+1];
}
else
{
while (energy < energyP[k/2]/2)
{
for (i = cVList[k]-1; i < cVList[k+1]; i++)
{
index = i * iCols + (cHList[k] + col - 1);
energy += imageIn[index];
}
col++;
}
cHListTemp[0 + 2*k] = cHList[k];
cHListTemp[1 + 2*k] = cHList[k] + col;
cHListTemp[2 + 2*k] = cHList[k] + col + 1;
cHListTemp[3 + 2*k] = cHList[k+1];
cVListTemp[0 + 2*k] = cVList[k];
cVListTemp[1 + 2*k] = cVList[k+1];
cVListTemp[2 + 2*k] = cVList[k];
cVListTemp[3 + 2*k] = cVList[k+1];
}
energyPtemp[k+1] = energyP[k/2] - energy;
energyPtemp[k] = energy;
}

for (k = 0; k < filledVals; k++)
energyP[k] = energyPtemp[k];

filledVals *= 2;

for (k = 0; k < filledVals; k++)
{
cVList[k] = cVListTemp[k];
cHList[k] = cHListTemp[k];
}
}

mxFree(cVListTemp);
mxFree(cHListTemp);
mxFree(energyP);
mxFree(energyPtemp);
}

Subject: MATLAB hangs with MEX code

From: Bruno Luong

Date: 10 Aug, 2009 20:36:19

Message: 2 of 8


>
>
> /* Allocate intermediate & output memory */
> plhs[0] = mxCreateNumericMatrix(1, sizeList, mxINT16_CLASS, mxREAL);
> cVList = (int *)mxGetData(plhs[0]);
> plhs[1] = mxCreateNumericMatrix(1, sizeList, mxINT16_CLASS, mxREAL);
> cHList = (int *)mxGetData(plhs[1]);

Are you sure INT corresponds to mxINT16_CLASS? To me:

INT <-> mxINT32_CLASS
SHORT INT <-> CLASS

% Bruno

Subject: MATLAB hangs with MEX code

From: Bruno Luong

Date: 10 Aug, 2009 20:44:19

Message: 3 of 8

Sorry for the typo
>
> INT <-> mxINT32_CLASS
> SHORT INT <-> mxINT16 CLASS
>
> % Bruno

Subject: MATLAB hangs with MEX code

From: Rune Allnor

Date: 10 Aug, 2009 21:09:35

Message: 4 of 8

On 10 Aug, 22:12, "Jose Antonio " <jurig...@gmail.com> wrote:
> Hi everybody
>
> I am trying to make a MEX function to split a double class grey scale image into 2^N areas, which I had already done successfully in MATLAB.
>
> The thing is that the compiled function works properly for N up to 8, and then MATLAB simply hangs, and I have to stop the process.
>
> I guess I might be again messing up with memory management, but I find no clue how to get things to work. Any help will be very welcome!

Memory managment is almost certainly not the main
problem.

When a program 'hangs' it is usally because some
control loop does not terminate; you forget to
increase a counter, you compare to the wrong number,
or something like that.

The way to handle these things is to work in a C
development environment, either on the command line
or an IDE. You develop and debug the C business code
in this environment, and then only use the MEX
routine as an interface to the C code.

You organize the C code as a number of small functions
with clear responsibilities. Test each function
independently, then assemble all of them to the
main working program.

The general principle is to divide and conquer.

Rune

Subject: MATLAB hangs with MEX code

From: Jose Antonio

Date: 10 Aug, 2009 22:34:04

Message: 5 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h5q0r3$qcr$1@fred.mathworks.com>...
> Sorry for the typo
> >
> > INT <-> mxINT32_CLASS
> > SHORT INT <-> mxINT16 CLASS
> >
> > % Bruno

Bruno, you're absolutely right. I had changed that already, but didn't post the change, since the code was doing weird stuff... However it still hangs!

Subject: MATLAB hangs with MEX code

From: Jose Antonio

Date: 10 Aug, 2009 22:36:18

Message: 6 of 8

Rune Allnor <allnor@tele.ntnu.no> wrote in message <342d4053-e7a6-443e-9ed9-a00f427ce172@c34g2000yqi.googlegroups.com>...
> On 10 Aug, 22:12, "Jose Antonio " <jurig...@gmail.com> wrote:
> > Hi everybody
> >
> > I am trying to make a MEX function to split a double class grey scale image into 2^N areas, which I had already done successfully in MATLAB.
> >
> > The thing is that the compiled function works properly for N up to 8, and then MATLAB simply hangs, and I have to stop the process.
> >
> > I guess I might be again messing up with memory management, but I find no clue how to get things to work. Any help will be very welcome!
>
> Memory managment is almost certainly not the main
> problem.
>
> When a program 'hangs' it is usally because some
> control loop does not terminate; you forget to
> increase a counter, you compare to the wrong number,
> or something like that.
>
> The way to handle these things is to work in a C
> development environment, either on the command line
> or an IDE. You develop and debug the C business code
> in this environment, and then only use the MEX
> routine as an interface to the C code.
>
> You organize the C code as a number of small functions
> with clear responsibilities. Test each function
> independently, then assemble all of them to the
> main working program.
>
> The general principle is to divide and conquer.
>
> Rune


Hi Rune

Nice suggestion! I had not though about improper loop behavior or similar, since the code works fine for N up to 8... But I will check it again :)

Any way that I can make this work with a proper C environment? I mean, my real goal is to get better with Matlab and C as well, so any recommended software or similar would be great!

Jose

Subject: MATLAB hangs with MEX code

From: James Tursa

Date: 10 Aug, 2009 22:56:03

Message: 7 of 8

"Jose Antonio " <juriguen@gmail.com> wrote in message <h5q78s$ns2$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h5q0r3$qcr$1@fred.mathworks.com>...
> > Sorry for the typo
> > >
> > > INT <-> mxINT32_CLASS
> > > SHORT INT <-> mxINT16 CLASS
> > >
> > > % Bruno
>
> Bruno, you're absolutely right. I had changed that already, but didn't post the change, since the code was doing weird stuff... However it still hangs!

Please post your current code.

James Tursa

Subject: MATLAB hangs with MEX code

From: Jose Antonio

Date: 10 Aug, 2009 23:11:03

Message: 8 of 8

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h5q8i3$ffa$1@fred.mathworks.com>...
> "Jose Antonio " <juriguen@gmail.com> wrote in message <h5q78s$ns2$1@fred.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h5q0r3$qcr$1@fred.mathworks.com>...
> > > Sorry for the typo
> > > >
> > > > INT <-> mxINT32_CLASS
> > > > SHORT INT <-> mxINT16 CLASS
> > > >
> > > > % Bruno
> >
> > Bruno, you're absolutely right. I had changed that already, but didn't post the change, since the code was doing weird stuff... However it still hangs!
>
> Please post your current code.
>
> James Tursa


Sure! But I must say time is London is late now, so I'm going to sleep now :) No rush then for answers!

Anyway, here we go


#include "mex.h"
#include <math.h>


/*
 * Median_Cut.c
 * Used to speed up the Energy Split calculations
 *
 * [cVList, cHList] = Median_Cut(imageIn, N)
 * imageIn is double, N is int
 *
 * This is a MEX-file for MATLAB.
*/


/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
    /* variable declarations here */
    int N, iRows, iCols, index, numTiles, sizeList, filledVals, row, col;
    INT32_T *cVList, *cHList;
    int *cVListTemp, *cHListTemp;
    double energyT, energy;
    double *imageIn, *energyP, *energyPtemp;
    int n, k, i, j;

    imageIn = mxGetPr(prhs[0]);
    iRows = mxGetM(prhs[0]);
    iCols = mxGetN(prhs[0]);

    N = *(mxGetPr(prhs[1]));
    
    numTiles = pow(2,N);
    sizeList = 2*numTiles;

    /* Allocate intermediate & output memory */
    plhs[0] = mxCreateNumericMatrix(1, sizeList, mxINT32_CLASS, mxREAL);
    cVList = (INT32_T *) mxGetData(plhs[0]);
    plhs[1] = mxCreateNumericMatrix(1, sizeList, mxINT32_CLASS, mxREAL);
    cHList = (INT32_T *) mxGetData(plhs[1]);
    
    cVListTemp = mxCalloc(sizeList, sizeof(int));
    cHListTemp = mxCalloc(sizeList, sizeof(int));
    energyP = mxCalloc(numTiles, sizeof(int));
    energyPtemp = mxCalloc(numTiles, sizeof(int));
    
    cVList[0] = cHList[0] = 1;
    cVList[1] = iRows;
    cHList[1] = iCols;
    
    filledVals = 2;
    energyT = 0;
    
    
    /* code here */
    for (i = 0; i < iRows; i++)
        for (j = 0; j < iCols; j++)
        {
            index = i * iCols + j;
            energyT += imageIn[index];
        }
    energyP[0] = energyT;
    
    for (n = 0; n < N; n++)
    {
        for (k = 0; k < filledVals; k++)
            energyPtemp[k] = energyP[k];
        
        for (k = 0; k < filledVals; k+=2)
        {
            energy = 0;
            row = col = 0;
            if ((cVList[k+1] - cVList[k]) > (cHList[k+1] - cHList[k]))
            {
                while (energy < energyP[k/2]/2)
                {
                    for (j = cHList[k]-1; j < cHList[k+1]; j++)
                    {
                        index = (cVList[k] + row - 1) * iCols + j;
                        energy += imageIn[index];
                    }
                    row++;
                }
                cVListTemp[0 + 2*k] = cVList[k];
                cVListTemp[1 + 2*k] = cVList[k] + row;
                cVListTemp[2 + 2*k] = cVList[k] + row + 1;
                cVListTemp[3 + 2*k] = cVList[k+1];
                cHListTemp[0 + 2*k] = cHList[k];
                cHListTemp[1 + 2*k] = cHList[k+1];
                cHListTemp[2 + 2*k] = cHList[k];
                cHListTemp[3 + 2*k] = cHList[k+1];
            }
            else
            {
                while (energy < energyP[k/2]/2)
                {
                    for (i = cVList[k]-1; i < cVList[k+1]; i++)
                    {
                        index = i * iCols + (cHList[k] + col - 1);
                        energy += imageIn[index];
                    }
                    col++;
                }
                cHListTemp[0 + 2*k] = cHList[k];
                cHListTemp[1 + 2*k] = cHList[k] + col;
                cHListTemp[2 + 2*k] = cHList[k] + col + 1;
                cHListTemp[3 + 2*k] = cHList[k+1];
                cVListTemp[0 + 2*k] = cVList[k];
                cVListTemp[1 + 2*k] = cVList[k+1];
                cVListTemp[2 + 2*k] = cVList[k];
                cVListTemp[3 + 2*k] = cVList[k+1];
            }
            energyPtemp[k+1] = energyP[k/2] - energy;
            energyPtemp[k] = energy;
        }
        
        for (k = 0; k < filledVals; k++)
            energyP[k] = energyPtemp[k];
        
        filledVals *= 2;
        
        for (k = 0; k < filledVals; k++)
        {
            cVList[k] = cVListTemp[k];
            cHList[k] = cHListTemp[k];
        }
    }
    
    mxFree(cVListTemp);
    mxFree(cHListTemp);
    mxFree(energyP);
    mxFree(energyPtemp);
}

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com