ask help to update the source code from 32 bit to 64 bit compatible

2 views (last 30 days)
Hi there
I have a toolbox from a professor written in c,
now i would like to make it work on my 64 bit matlab, can anybody told me how to update the following source code?
I tried to follow the instructions but it doesn't work.
I think the code body is right, just the way it initializes seems incompatible with the 64 bit system Thanks
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* ***************** */
/* Declare variables */
/* ***************** */
mxArray *pBa;
double **B, **Bptr;
double *BLastPtr;
double *z, *c, *d, *order, *zptr, *cptr, *pptr;
double *dptr;
double *dstart, *dend, *cstart, *cend, *clast;
double tempd;
int **Bir, **Bjc, **RowPtr, *RowPtrLast, *rptr, *rend;
int **ColPtr, **ColEnd, *ColPtrLast, *ColEndLast;
int Bn, Brows, cm, cn, prodcol;
int j, tempi, zfactor;
bool *isfull, LastFull;
B=mxCalloc(Bn,sizeof(z)); /* Ptrs for B matrices */
Bptr=mxCalloc(Bn,sizeof(Bptr)); /* Ptrs for current B values */
isfull=mxCalloc(Bn,sizeof(bool));
Bir=mxCalloc(Bn,sizeof(Bir));
Bjc=mxCalloc(Bn,sizeof(Bjc));
RowPtr=mxCalloc(Bn,sizeof(RowPtr));
ColPtr=mxCalloc(Bn,sizeof(ColPtr));
ColEnd=mxCalloc(Bn,sizeof(ColEnd));
prodcol=1;
for (j=0; j<Bn; j++) /* loop over the B{j} */
{
pBa=mxGetCell(prhs[0],order[j]); /* pointer to B{j} mxArrays */
B[j]=mxGetPr(pBa); /* pointers to B{j} data */
if (mxIsSparse(pBa)) /* B{j} is sparse */
{
isfull[j]=false;
Bir[j]=mxGetIr(pBa);
Bjc[j]=mxGetJc(pBa);
tempi=mxGetN(pBa); /* # of cols in B{j} */
Bptr[j]=B[j]+*(Bjc[j]+tempi);
ColEnd[j]=Bjc[j]+tempi;
ColPtr[j]=ColEnd[j];
}
else if(mxIsDouble(pBa)) /* B{j} is full */
{
isfull[j]=true;
tempi=mxGetN(pBa); /* # of cols in B{j} */
Bptr[j]=B[j]+Brows*tempi;
ColEnd[j]=0;
ColEnd[j]+=tempi; /* use ColPtr as an integer */
ColPtr[j]=ColEnd[j];
}
else mexErrMsgTxt("B must be composed of matrices of real numbers");
if (mxGetM(pBa) != Brows)
mexErrMsgTxt("All rows in cell array must be equal");
prodcol=prodcol*tempi; /* product of cols */
}
if (prodcol != cm) mexErrMsgTxt("B and c are not conformable");
plhs[0]=mxCreateDoubleMatrix(Brows,cn,mxREAL); /* mxArray for output */
z=mxGetPr(plhs[0]); /* pointer to output data */
d=mxCalloc(Brows*Bn,sizeof(double));
/* ********************************************************************* */
/* d[i,j] contains the product of variables B(i,k), k=1 to j. */
/* Column j of d is updated everytime variable j+1 turns over. */
/* The turn over is dtermined by checking ColPtr against ColEnd. */
/* The last variable, which turns over fastest, is handled separately to */
/* increase speed. */
/* ********************************************************************* */
dend=d+Brows;
for (dptr=d;dptr<dend;dptr++) *dptr=1;
dstart=d+Brows*(Bn-1);
ColEndLast=ColEnd[Bn-1];
ColPtrLast=ColEndLast;
LastFull=isfull[Bn-1];
cend=c+cm;
clast=c+cm*cn;
zfactor=Brows*cn-1;
/* **************************** */
/* The main loop over rows of c */
/* **************************** */
for (cstart=c; cstart<cend; cstart++)
{
/* ********************************************** */
/* Check if iterated through the first matrix */
/* Multipliers only updated when an index changes */
/* A full revision occurs on the first iteration */
/* ********************************************** */
if (ColPtrLast>=ColEndLast)
{
j=Bn-1;
BLastPtr=B[j];
if (LastFull) ColPtrLast=0;
else {RowPtrLast=Bir[j]; ColPtrLast=Bjc[j];}
do
{
j--;
(ColPtr[j])++;
if (ColPtr[j]<ColEnd[j]) break;
else
{
Bptr[j]=B[j];
if (isfull[j]) ColPtr[j]=0;
else {RowPtr[j]=Bir[j]; ColPtr[j]=Bjc[j];}
}
}
while (j>0);
/* ******************************************************** */
/* Replace the multipliers (d[i,j]) for variables j to Bn-1 */
/* ******************************************************** */
dptr=d+(j+1)*Brows;
for (;j<Bn-1;j++)
{
if (isfull[j]) /* jth matrix is full */
{
pptr=Bptr[j];
dend=dptr+Brows;
for (;dptr<dend; dptr++, pptr++)
*dptr = *(dptr-Brows)**pptr;
Bptr[j] = pptr;
}
else /* jth matrix is sparse */
{
/* fill d(:,j) with zeros */
memset(dptr,'\0',Brows*sizeof(double));
pptr=Bptr[j];
rptr=RowPtr[j];
rend=rptr+*(ColPtr[j]+1)-*ColPtr[j];
for (;rptr<rend; rptr++, pptr++)
*(dptr+*rptr)=*(dptr-Brows+*rptr)**pptr;
dptr=dptr+Brows;
Bptr[j]=pptr;
RowPtr[j]=rptr;
}
}
}
/* ************************************ */
/* This is executed every time */
/* Loop over rows of B and columns of c */
/* ************************************ */
zptr=z;
if (LastFull)
{
dend=dstart+Brows;
if (cn==1)
{
tempd=*cstart;
for (dptr=dstart; dptr<dend; dptr++,BLastPtr++,zptr++)
*zptr += tempd*(*dptr**BLastPtr);
}
else
for (dptr=dstart; dptr<dend; dptr++, BLastPtr++)
{
tempd=*dptr * *BLastPtr;
for (cptr=cstart; cptr<clast; cptr+=cm, zptr+=Brows)
*zptr += *cptr*tempd;
zptr-=zfactor;
}
}
else
{
rend=RowPtrLast+*(ColPtrLast+1)-*ColPtrLast;
if (cn==1)
for (;RowPtrLast<rend; RowPtrLast++, BLastPtr++)
*(z+*RowPtrLast) += *cstart*(*(dstart+*RowPtrLast)**BLastPtr);
else
for (;RowPtrLast<rend; RowPtrLast++, BLastPtr++)
{
tempd=*(dstart+*RowPtrLast)**BLastPtr;
zptr=z+*RowPtrLast;
for (cptr=cstart; cptr<clast; cptr+=cm, zptr+=Brows)
*zptr += *cptr*tempd;
}
}
ColPtrLast ++;
}
/* ******************************************** */
/* Main Loop completed - free memory and return */
/* ******************************************** */
mxFree(d);
}
  2 Comments
Jan
Jan on 31 Jul 2011
Please do not just state "it doesn't work", but explain the problems exactly. You want us to invest time in solving your problem. Then it is a good idea to try to make it as easy as possible for all readers.
Yue Zhang
Yue Zhang on 1 Aug 2011
Hi Jan,
I tried to update the source code pointers and dimensions from
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* ***************** */
/* Declare variables */
/* ***************** */
mxArray *pBa;
double **B, **Bptr;
double *BLastPtr;
double *z, *c, *d, *order, *zptr, *cptr, *pptr;
double *dptr;
double *dstart, *dend, *cstart, *cend, *clast;
double tempd;
int **Bir, **Bjc, **RowPtr, *RowPtrLast, *rptr, *rend;
int **ColPtr, **ColEnd, *ColPtrLast, *ColEndLast;
int Bn, Brows, cm, cn, prodcol;
int j, tempi, zfactor;
bool *isfull, LastFull;
to
void mexFunction(
mwSize nlhs, mxArray *plhs[],
mwSize nrhs, const mxArray *prhs[])
{
/* ***************** */
/* Declare variables */
/* ***************** */
mxArray *pBa;
mwIndex **B, **Bptr;
mwIndex *BLastPtr;
mwIndex *z, *c, *d, *order, *zptr, *cptr, *pptr;
mwIndex *dptr;
mwIndex *dstart, *dend, *cstart, *cend, *clast;
mwIndex tempd;
mwIndex **Bir, **Bjc, **RowPtr, *RowPtrLast, *rptr, *rend;
mwIndex **ColPtr, **ColEnd, *ColPtrLast, *ColEndLast;
mwSize Bn, Brows, cm, cn, prodcol;
mwSize j, tempi, zfactor;
bool *isfull, LastFull;
after recompiled, i had the error message of
"Input arguments of improper type"
Since i am not an expert in C, which i couldn't tell the exact difference among double, pointer, pointer of pointers, etc.
Can you help me figure out how to modify this c file?
I can send you the original file if necessary.
Best

Sign in to comment.

Answers (1)

Jan
Jan on 31 Jul 2011
The general procedure is:
  • Do not use int as type of pointers.
  • Use mwSize for array dimensions (e.g. Brows)
  • Use mwIndex to index an array (I assume e.g. dptr, dend)
  • Use mwSignedIndex to calculate pointer differences when the sign matters (not used in your code, as far as I can see)
  1 Comment
Yue Zhang
Yue Zhang on 1 Aug 2011
Thanks Jan,
I should be more precise on this.
I've update the source code as you described, should i just type mex -largeArrayDims xxx.c to compile this one?
2. I tried this but while running, the matlab crash and exit, i think i need to update all the source codes in the toolbox.
I will see whether it works after doing that.
Thanks again

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!