Thread Subject: mex: Compile error with mxArray

Subject: mex: Compile error with mxArray

From: David Hanau

Date: 13 Aug, 2009 13:40:19

Message: 1 of 7

Hi,

Compiling C functions with Lcc in Matlab is have the following (classic) error:

Error sparsesvd_partialeig_matlab.c: 86 operands of = have illegal types `pointer to incomplete struct mxArray_tag defined at C:\MATLAB6p1\extern\include\matrix.h 154' and `int'

for the last line of this code:

mxArray *output[3],*input[4];

mexEvalString("options.disp=0;"); // for use in calling Matlab function eigs
mexEvalString("options.maxit=500;"); // for use in calling Matlab function eigs
input[0] = mxCreateDoubleMatrix(n,n,mxREAL); // for use in calling Matlab function eigs
input[1] = mxCreateDoubleMatrix(1,1,mxREAL);
input[2]= mxCreateString("la");
input[3]= mexGetVariable("caller","options");

of course I have included mex.h in the header file.

Any idea?

Thanks a lot,

David

Subject: mex: Compile error with mxArray

From: James Tursa

Date: 13 Aug, 2009 14:58:02

Message: 2 of 7

"David Hanau" <hanaud@gmail.com.REMTHIS> wrote in message <h61543$anv$1@fred.mathworks.com>...
>
> Compiling C functions with Lcc in Matlab is have the following (classic) error:
>
> Error sparsesvd_partialeig_matlab.c: 86 operands of = have illegal types `pointer to incomplete struct mxArray_tag defined at C:\MATLAB6p1\extern\include\matrix.h 154' and `int'
>
> for the last line of this code:
>
> mxArray *output[3],*input[4];
>
> mexEvalString("options.disp=0;"); // for use in calling Matlab function eigs
> mexEvalString("options.maxit=500;"); // for use in calling Matlab function eigs
> input[0] = mxCreateDoubleMatrix(n,n,mxREAL); // for use in calling Matlab function eigs
> input[1] = mxCreateDoubleMatrix(1,1,mxREAL);
> input[2]= mxCreateString("la");
> input[3]= mexGetVariable("caller","options");
>

I think you will have to post your complete routine, if it isn't too long.

James Tursa

Subject: mex: Compile error with mxArray

From: David Hanau

Date: 13 Aug, 2009 15:10:11

Message: 3 of 7

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h619lq$hka$1@fred.mathworks.com>...
> "David Hanau" <hanaud@gmail.com.REMTHIS> wrote in message <h61543$anv$1@fred.mathworks.com>...
> >
> > Compiling C functions with Lcc in Matlab is have the following (classic) error:
> >
> > Error sparsesvd_partialeig_matlab.c: 86 operands of = have illegal types `pointer to incomplete struct mxArray_tag defined at C:\MATLAB6p1\extern\include\matrix.h 154' and `int'
> >
> > for the last line of this code:
> >
> > mxArray *output[3],*input[4];
> >
> > mexEvalString("options.disp=0;"); // for use in calling Matlab function eigs
> > mexEvalString("options.maxit=500;"); // for use in calling Matlab function eigs
> > input[0] = mxCreateDoubleMatrix(n,n,mxREAL); // for use in calling Matlab function eigs
> > input[1] = mxCreateDoubleMatrix(1,1,mxREAL);
> > input[2]= mxCreateString("la");
> > input[3]= mexGetVariable("caller","options");
> >
>
> I think you will have to post your complete routine, if it isn't too long.
>
> James Tursa

This code works and the last releave of MatLab bur not on my V6.1
I post only part of my files. They are really big and I don't think the rest of the code would be relevant here.
the sparseesvd.h:

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <mex.h>
#include <matrix.h>
#include <string.h>

#ifdef WIN32
#include <malloc.h>
extern void dsaupd();
extern void dseupd();
#endif

the sparse_rank_one_partialeig_matlab.c
#include "sparsesvd.h"

void sparse_rank_one_partialeig_matlab(double *Amat, int n, double rho, double gapchange, int MaxIter, double *Xmat, double *Umat, double *uvec, double *Fmat, double *iters, int info, int numeigs, int addeigs, int checkgap, double perceigs, int check_for_more_eigs, double *dualitygap_alliter, double *cputime_alliter, double *perceigs_alliter)
{
// Hard parameters
int Nperiod=imaxf(1,info),dspca_finished=0;
int work_size=3*n+n*n,changedmu=0;
// Working variables
double d1,sig1,d2,sig2,norma12,mu,Ntheo,L;
double alpha,gapk;
double dmax=0.0,fmu,lambda;
int n2=n*n,incx=1,precision_flag=0,iteration_flag=0,error_flag=0;
int lwork,inflapack,indmax,k=0,i,j;
double cputime,last_time=(double)clock();double start_time=(double)clock();int left_h=0,left_m=0,left_s=0;
char jobz[1],uplo[1];
double *Vmat=(double *) calloc(n*n,sizeof(double));
double *bufmata=(double *) calloc(n*n,sizeof(double));
double *bufmatb=(double *) calloc(n*n,sizeof(double));
double *workvec=(double *) calloc(work_size,sizeof(double));
double *hvec=(double *) calloc(n,sizeof(double));
int work_size3=8*n;
double *workvec2=(double *) calloc(work_size3,sizeof(double));
int *iwork=(int *) calloc(5*n,sizeof(int));
double *numeigs_matlab=(double *) calloc(1,sizeof(double));
double *evector_temp=(double *) calloc(n*n,sizeof(double));
double *evalue=(double *) calloc(n*n,sizeof(double));
double *evector_store=(double *) calloc(n*n,sizeof(double));
double eigcut,tolweight=.75,tol=.01;
int *count=(int *) calloc(n,sizeof(int));
mxArray *output[3],*input[4];
double *Fmattemp=(double *) calloc(n*n,sizeof(double));
double *Xmattemp=(double *) calloc(n*n,sizeof(double));
int checkgap_count=0; // added for test variables

// Start...
if (info>=1)
{
mexPrintf("DSPCA starting ... \n");
mexEvalString("drawnow;");
}
// Test malloc results
if ((Fmat==NULL) || (Vmat==NULL) || (bufmata==NULL) || (bufmatb==NULL)|| (workvec==NULL) || (hvec==NULL) ||(evector_temp==NULL)||(evector_store==NULL)||(evalue==NULL)||(iwork==NULL)||(workvec2==NULL)||(numeigs_matlab==NULL)||(Fmattemp==NULL)||(Xmattemp==NULL))
{
mexPrintf("DSPCA: memory allocation failed ... \n");
mexEvalString("drawnow;");return;
}
eigcut=(1-tolweight)*(tol/10)/(rho*pow(n,1.5)); // scale delta (tol/10) to get eig threshold
tol=tolweight*tol; // scale of .5 for partial eig precision
mexEvalString("options.disp=0;"); // for use in calling Matlab function eigs
mexEvalString("options.maxit=500;"); // for use in calling Matlab function eigs
input[0] = mxCreateDoubleMatrix(n,n,mxREAL); // for use in calling Matlab function eigs
input[1] = mxCreateDoubleMatrix(1,1,mxREAL);
input[2]= mxCreateString("la");
input[3]= mexGetVariable("caller","options");

// First, compute some local params
d1=rho*rho*n*n/2.0;sig1=1.0;d2=log(n);sig2=0.5;norma12=1.0;mu=tol/(2.0*d2);
Ntheo=(4.0*norma12*sqrt(d1*d2/(sig1*sig2)))/tol;Ntheo=ceil(Ntheo);
L=(d2*norma12*norma12)/(2.0*sig2*tol);
alpha=0.0;cblas_dscal(n2,alpha,Xmat,incx);

cputime=start_time;

*count=0;
// eigenvalue decomposition of A+X
cblas_dcopy(n2,Xmat,incx,Vmat,incx);
alpha=1.0;
cblas_daxpy(n2,alpha,Amat,incx,Vmat,incx);
symmetrize(Vmat,bufmata,n); // symmetrize A+X so no precision problems
// do partial eigenvalue approximation to exp(A+X)
cblas_dcopy(n2,bufmata,incx,Vmat,incx);
*numeigs_matlab=1.0*numeigs;
fmu=partial_eig_matlab(n,k,mu,eigcut,bufmata,bufmatb,numeigs_matlab,
evector_temp,evector_store,evalue,input,output,
hvec,Vmat,Umat,workvec,count,dmax,addeigs,perceigs,check_for_more_eigs);
numeigs=(int)(*numeigs_matlab);
}

Subject: mex: Compile error with mxArray

From: James Tursa

Date: 13 Aug, 2009 17:26:02

Message: 4 of 7

"David Hanau" <hanaud@gmail.com.REMTHIS> wrote in message <h61acj$43k$1@fred.mathworks.com>...
>
> This code works and the last releave of MatLab bur not on my V6.1
> I post only part of my files. They are really big and I don't think the rest of the code would be relevant here.
               :
> mexEvalString("options.disp=0;"); // for use in calling Matlab function eigs
> mexEvalString("options.maxit=500;"); // for use in calling Matlab function eigs
> input[0] = mxCreateDoubleMatrix(n,n,mxREAL); // for use in calling Matlab function eigs
> input[1] = mxCreateDoubleMatrix(1,1,mxREAL);
> input[2]= mxCreateString("la");
> input[3]= mexGetVariable("caller","options");
                    :

I don't have a v6.1 available, but my guess is that one or more of your mx___ or mex___ functions is not available in the older MATLAB version. If it is not available, then there will be no prototype in the mex.h or matrix.h file. So when the compiler gets to the function, absent a prototype, it makes the default assumption that the function returns an int, and of course you are assigning it to a mxArray *. Thus you get the "... invalid assignment int to mxArray * ..." message from the compiler.

My advice is to get a list of all the mx___ and mex___ functions you use and check to see which ones are not available in the earlier version of MATLAB, and then write workarounds for these functions if you can.

James Tursa

Subject: mex: Compile error with mxArray

From: James Tursa

Date: 13 Aug, 2009 17:48:01

Message: 5 of 7

"David Hanau" <hanaud@gmail.com.REMTHIS> wrote in message <h61acj$43k$1@fred.mathworks.com>...
>
> I post only part of my files. They are really big and I don't think the rest of the code would be relevant here.

I notice in the code you post there are several opportunities for memory leaks. I assume you have free() calls somewhere to clean up all of your calloc calls and you snipped that out and didn't post that code??? Also, you might think of switching your calloc (and free) calls to mxCalloc (and mxFree) calls.

James Tursa

Subject: mex: Compile error with mxArray

From: James Tursa

Date: 14 Aug, 2009 06:39:01

Message: 6 of 7

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h61iba$hft$1@fred.mathworks.com>...
> "David Hanau" <hanaud@gmail.com.REMTHIS> wrote in message <h61acj$43k$1@fred.mathworks.com>...
> >
> > This code works and the last releave of MatLab bur not on my V6.1
> > I post only part of my files. They are really big and I don't think the rest of the code would be relevant here.
> :
> > mexEvalString("options.disp=0;"); // for use in calling Matlab function eigs
> > mexEvalString("options.maxit=500;"); // for use in calling Matlab function eigs
> > input[0] = mxCreateDoubleMatrix(n,n,mxREAL); // for use in calling Matlab function eigs
> > input[1] = mxCreateDoubleMatrix(1,1,mxREAL);
> > input[2]= mxCreateString("la");
> > input[3]= mexGetVariable("caller","options");
> :
>
> I don't have a v6.1 available, but my guess is that one or more of your mx___ or mex___ functions is not available in the older MATLAB version. If it is not available, then there will be no prototype in the mex.h or matrix.h file. So when the compiler gets to the function, absent a prototype, it makes the default assumption that the function returns an int, and of course you are assigning it to a mxArray *. Thus you get the "... invalid assignment int to mxArray * ..." message from the compiler.
>
> My advice is to get a list of all the mx___ and mex___ functions you use and check to see which ones are not available in the earlier version of MATLAB, and then write workarounds for these functions if you can.
>

Looks like the culprit is indeed mexGetVariable. The older MATLAB versions have a different function called mexGetArray. The comparison prototypes are:

Older versions:
mxArray *mexGetArray(const char *name, const char *workspace);

Newer versions:
mxArray *mexGetVariable(const char *workspace, const char *varname);

So the name is different, and the argument order has switched. So include this at the top of your source code when compiling using the older version of MATLAB:

mxArray *mexGetVariable(const char *workspace, const char *varname)
{
    return mexGetArray(varname, workspace);
}

James Tursa

Subject: mex: Compile error with mxArray

From: David Hanau

Date: 17 Aug, 2009 12:45:18

Message: 7 of 7

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h630q5$o5b$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h61iba$hft$1@fred.mathworks.com>...
> > "David Hanau" <hanaud@gmail.com.REMTHIS> wrote in message <h61acj$43k$1@fred.mathworks.com>...
> > >
> > > This code works and the last releave of MatLab bur not on my V6.1
> > > I post only part of my files. They are really big and I don't think the rest of the code would be relevant here.
> > :
> > > mexEvalString("options.disp=0;"); // for use in calling Matlab function eigs
> > > mexEvalString("options.maxit=500;"); // for use in calling Matlab function eigs
> > > input[0] = mxCreateDoubleMatrix(n,n,mxREAL); // for use in calling Matlab function eigs
> > > input[1] = mxCreateDoubleMatrix(1,1,mxREAL);
> > > input[2]= mxCreateString("la");
> > > input[3]= mexGetVariable("caller","options");
> > :
> >
> > I don't have a v6.1 available, but my guess is that one or more of your mx___ or mex___ functions is not available in the older MATLAB version. If it is not available, then there will be no prototype in the mex.h or matrix.h file. So when the compiler gets to the function, absent a prototype, it makes the default assumption that the function returns an int, and of course you are assigning it to a mxArray *. Thus you get the "... invalid assignment int to mxArray * ..." message from the compiler.
> >
> > My advice is to get a list of all the mx___ and mex___ functions you use and check to see which ones are not available in the earlier version of MATLAB, and then write workarounds for these functions if you can.
> >
>
> Looks like the culprit is indeed mexGetVariable. The older MATLAB versions have a different function called mexGetArray. The comparison prototypes are:
>
> Older versions:
> mxArray *mexGetArray(const char *name, const char *workspace);
>
> Newer versions:
> mxArray *mexGetVariable(const char *workspace, const char *varname);
>
> So the name is different, and the argument order has switched. So include this at the top of your source code when compiling using the older version of MATLAB:
>
> mxArray *mexGetVariable(const char *workspace, const char *varname)
> {
> return mexGetArray(varname, workspace);
> }
>
> James Tursa

Thank you for your help. Indeed the mexGetArray do the job perfectly.

No I tacke a new issue, but I think it comes from the memory allocation.
I am going to look into it and try to debug the mex file!

------------------------------------------------------------------------
       Segmentation violation detected at Mon Aug 17 14:39:10 2009
------------------------------------------------------------------------

Configuration:
  MATLAB Version: 6.1.0.450 (R12.1)
  Operating System: Microsoft Windows 2000
  Window System: Version 5.1 (Build 2600: Service Pack 2)
  Processor ID: x86 Family 15 Model 4 Stepping 9, GenuineIntel
  Virtual Machine: Java 1.1.8 from Sun Microsystems Inc.

Register State:
  EAX = 00000000 EBX = 00074330
  ECX = 00074970 EDX = 00000640
  ESI = 000000c8 EDI = 0000002c
  EBP = 00000160 ESP = 0149a1c8
  EIP = 02210f92 FLG = 00010287

Stack Trace:

This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
mexgetarray James Tursa 14 Aug, 2009 02:59:52
mexgetvariable James Tursa 14 Aug, 2009 02:59:52
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