Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: MATLAB hangs with MEX code
Date: Mon, 10 Aug 2009 23:11:03 +0000 (UTC)
Organization: Imperial College London
Lines: 156
Message-ID: <h5q9e7$cd1$1@fred.mathworks.com>
References: <h5puv2$oa2$1@fred.mathworks.com> <h5q0c3$q3r$1@fred.mathworks.com> <h5q0r3$qcr$1@fred.mathworks.com> <h5q78s$ns2$1@fred.mathworks.com> <h5q8i3$ffa$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1249945863 12705 172.30.248.38 (10 Aug 2009 23:11:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 10 Aug 2009 23:11:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1570235
Xref: news.mathworks.com comp.soft-sys.matlab:562252


"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);
}