f



Call C code from Matlab -> Matlab crash #2

Hello,

I have a problem calling C code from Matlab. The mex file compilation
is ok. When I run the program Matlab (7.3) crashes. Below is the
mexFunction code. Any idea is welcome. (I think the main part of the
code (middle part) is not useful for the crash problem, but I put it
anyway).

//###########################################################
#include "math.h"
#include "mex.h"

void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray
*prhs[]) {
  // Standard variables
  int p,r,h,i,j,f,w; // Indices for loop
  int MAX_NBM = 1000;
  int MAX_DIM = 10;
  int MAX_NB_CLUST = 20;
  double *SF, *km, *new_data;
  int size, dim, K, nbm, aux_size, count_bis, count_aux;
  double diam_max, diam, vec1_p, vec2_p, dist_min, dis;
  int combi[1000000][2];
  int dist_aux[1000000][2];

  /* Check for proper number of arguments. */
  if (nrhs != 5) {
    mexErrMsgTxt("5 input required.");
  } else if (nlhs > 1) {
    mexErrMsgTxt("Too many output arguments");
  }

  // Input parameter (MATLAB)
  new_data = mxGetPr(prhs[0]);
  size = (int)mxGetScalar(prhs[1]);
  dim = (int)mxGetScalar(prhs[2]);
  K = (int)mxGetScalar(prhs[3]);
  km = mxGetPr(prhs[4]);
  
  // Output parameter (MATLAB)
  plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
  SF = mxGetPr(plhs[0]);
  
  // Other variables
  nbm = size/dim; // Number of points
  printf("Taille: %d\n",nbm);
  printf("Dimension: %d\n",dim);
    
  //.......

  //##############################################
  // Calculate dissimilarity
  //##############################################
  dist_min = 1000000;
  // Find combination of 2 in nbm
  count_bis = 0;
  for (i=1; i<nbm; i++) {
    for (r=i+1; r<nbm+1; r++) {
      dist_aux[count_bis][0] = i;
      dist_aux[count_bis][1] = r;
      //printf("%d
%d\n",dist_aux[count_bis][0],dist_aux[count_bis][1]);
      count_bis++;
    }
  }
  
  // Dissimilarity
  dis = 0;
  vec1_p = 0;
  vec2_p = 0;
  //printf("\n");
  for (w=0; w<count_bis; w++) {
    // Cluster appartenance
    int clu1 = km[dist_aux[w][0]-1];
    int clu2 = km[dist_aux[w][1]-1];
    //printf("%d %d\n",clu1,clu2);
    
    // Calculate distance if belong to different clusters
    if (clu1 != clu2) {
      for (j=0; j<dim; j++) {
	// Vectors
	vec1_p = new_data[dist_aux[w][0]*dim+j];
	vec2_p = new_data[dist_aux[w][1]*dim+j];
        
	// Standard Euclidean distance
      	dis = dis + (vec1_p-vec2_p)*(vec1_p-vec2_p);
      }
      //printf("%f\n",dis);
      dis = sqrt(dis);
      //printf("%f\n",dis);
      
      // Look for the minimum
      if (dis < dist_min) {
	dist_min = dis;
      }
    }
  }
  printf("Dissimilarity: %f\n",dist_min);

  //###########################################
  // Dunn's Index
  //###########################################
  SF[0] = dist_min/diam_max;
}
0
9/27/2006 9:58:28 AM
comp.soft-sys.matlab 211266 articles. 24 followers. lunamoonmoon (257) is leader. Post Follow

3 Replies
773 Views

Similar Articles

[PageSpeed] 59

Sandro wrote:
> Hello,
> 
> I have a problem calling C code from Matlab. The mex file compilation
> is ok. When I run the program Matlab (7.3) crashes. Below is the
> mexFunction code. Any idea is welcome. (I think the main part of the
> code (middle part) is not useful for the crash problem, but I put it
> anyway).

I did not check your code but you should consider allocating these huge 
arrays dynamically:

   int combi[1000000][2];
   int dist_aux[1000000][2];

These arrays are approx. 16MB and you may hit the stack size limit.

Have you tried to debug your code to localize the error?

-Herbert
0
9/27/2006 10:21:31 AM
Herbert Ramoser wrote:
>
>
>
> I did not check your code but you should consider allocating these
> huge
> arrays dynamically:
>
> int combi[1000000][2];
> int dist_aux[1000000][2];
>
> These arrays are approx. 16MB and you may hit the stack size limit.

Agreed. The easy way to check this is to declare them static so they
aren't on the stack.

static int combi[1000000][2];
static int dist_aux[1000000][2];
0
9/27/2006 10:47:46 AM
Thanks for the help. The static "trick" seems to work!
Thanks a lot!
0
9/27/2006 11:28:07 AM
Reply: