mex运行时错误:必须使用“-largeArrayDims”选项重建使用稀疏矩阵的64位mex文件

我的c代码应该将Matlab稀疏格式转换为
TAUCS格式,这也是列主要格式.

当然我在Matlab本身生成Matlab稀疏格式,然后将其传输到mex文件.

代码编译好.但是,当我尝试运行Matlab时,我收到以下运行时错误:

Error using MatSparse_2_TAUCS
Function "mxGetJc_700" is obsolete.
(64-bit mex files using sparse matrices must be rebuilt with the "-largeArrayDims" option.  See the R2006b release notes for more details.)

Error in Matlab_mex_Testing (line 18)
    z = MatSparse_2_TAUCS(x, spMat);

我想我已经在make文件中使用了“-largeArrayDims”选项.

这是我的Matlab代码:

Dir  = '/home/dkumar/Mex_Codes_DKU/MexCode_Working/Mex_CPP_N_ARMADILLO_Codes_DKU_makefile_Working';

%Create a simple sparse matrix
spMat = speye(100, 100);
%Set few more elements to be non-zero
spMat(5, 8)   = 1.500;
spMat(5, 100) = 3.500;
spMat(19, 100) = 3.500;
spMat(59, 89)   = 1.500;

% MEX
cd(Dir);
x = 5.0;
z = MatSparse_2_TAUCS(x, spMat);

这是我的C代码:

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

#include "/home/dkumar/libtsnnls-2.3.3/tsnnls/taucs_basic/taucs.h"

extern void _main();

const int numInputArgs  = 2;
const int numOutputArgs = 1;

// Function declarations.
// -----------------------------------------------------------------

int TestingLibraries() ;   // declared and defined in In Include_4_TSNNLS.c and Include_4_TSNNLS.h


// Function definitions.
// -----------------------------------------------------------------
void mexFunction (int nlhs, mxArray *plhs[],
          int nrhs, const mxArray *prhs[]) {

    double *y, *z, x;

    /* Declare variable */
    mwSize mrows,ncols;
    int status;
    mwSize nzmax;
    mwIndex *irs,*jcs,j,k;

    /*  Check for proper number of arguments. */
    if (nrhs != 2) 
        mexErrMsgTxt("Two inputs required.");
    if (nlhs != 1) 
        mexErrMsgTxt("One output required.");

    /* Check to make sure the first input argument is a scalar. */
    if (!mxIsDouble(prhs[0]) ||     mxIsComplex(prhs[0]) || mxGetN(prhs[0])*mxGetM(prhs[0]) != 1) {
        mexErrMsgTxt("Input x must be a scalar.");
    }

    /* Get the scalar input x. */
    x = mxGetScalar(prhs[0]);

    /* Create a pointer to the input matrix y. */
    y = mxGetPr(prhs[1]);


     /* Check data type dimensions of the matrix input y. */
    if (!(mxIsDouble(prhs[1]))){
        mexErrMsgIdAndTxt( "MATLAB:DKU", "Input argument must be of type double.");
    }

    if (mxGetNumberOfDimensions(prhs[1]) != 2){
        mexErrMsgIdAndTxt( "MATLAB:DKU", "Input argument must be two dimensional\n");
    }

     /* Get the size and pointers to input data */
    mrows =mxGetM(prhs[1]);
    ncols = mxGetN(prhs[1]);

    // Declaring the matrix in TAUCS
    taucs_ccs_matrix *A;

    // Verify the matrix y is infact sparse
    if (!(mxIsSparse(prhs[1]))){
        mexErrMsgIdAndTxt( "MATLAB: DKU", "Input matrix is not sparse\n");
    }else{
        //Work with irs and jcs directly instead of row-column pairs that are less compact.  
        double* sr      = mxGetPr(prhs[1]);   
        jcs         = mxGetJc(prhs[1]);     
        irs         = mxGetIr(prhs[1]);

    // Now converting sparse matrix to TAUCS format
        A       = (taucs_ccs_matrix*)malloc(sizeof(taucs_ccs_matrix));
        A->n        = ncols;
        A->m        = mrows;   
        A->flags    = TAUCS_DOUBLE;

        // Allocating spaces
        int nnz     = jcs[ncols]; 
        A->colptr       = (int*)mxMalloc(sizeof(int)*(A->n+1));
        A->rowind       = (int*)mxMalloc(sizeof(int)*nnz);
        A->values.d     = (double*)mxMalloc(sizeof(taucs_double)*nnz);  

        int icolOffset = 0;  // both matlab "SPARSE" indices and TAUCS  "SPARSE" indices start with 0 

    A->colptr   = jcs-icolOffset;
        A->rowind   = irs-icolOffset;
        A->values.d     = sr;
    }


    /* Create a C pointer to a copy of the output matrix. */
     //memcpy(mxGetPr(plhs[0]), &A, sizeof(A)); 

    //Freeing spaces
    mxFree(A->colptr);
    mxFree(A->rowind);
    mxFree(A->values.d);  

    //Creating plhs: just some fake value
   mwSize sz[2];
   sz[0] = mrows  ; // Matlab is row first
   sz[1] = ncols  ;
   plhs[0] = mxCreateNumericArray(2, sz, mxDOUBLE_CLASS, mxREAL);
   //Get a pointer to pOUT
   double* p2 = (double*)mxGetData(plhs[0]);
   // just copying input matrix
   int i1;

   for (i1 =0; i1 < mrows*ncols; i1++)
   {
    *p2 = *y;
         p2++; y++;
   }

}

这是我的makefile:

  MEXSUFFIX  = mexa64
  MATLABHOME = /usr/local/MATLAB/R2011b
  MEX        = /usr/local/MATLAB/R2011b/bin/mex
  CXX        = gcc
  CFLAGS     = -fPIC -pthread -DMX_COMPAT_32 \
               -DMATLAB_MEX_FILE  -largeArrayDims


  LIBS      = -lm
  INCLUDE   = -I$(MATLABHOME)/extern/include
  MEXFLAGS  = -cxx CC='$(CXX)' CXX='$(CXX)' LD='$(CXX)' -largeArrayDims

  REBUILDABLES = *.o *.mexa64     ## Anything with these extension
  TARGET = MatSparse_2_TAUCS.$(MEXSUFFIX)

  all : $(TARGET)
    echo All done

  clean : 
    rm -f $(REBUILDABLES)   
    echo Clean done

  $(TARGET): MatSparse_2_TAUCS.o
    $(MEX) $(MEXFLAGS) $(LIBS) -output MatSparse_2_TAUCS $^

  MatSparse_2_TAUCS.o: Include_4_TSNNLS.c MatSparse_2_TAUCS.c
    $(CXX) $(CFLAGS) $(INCLUDE) -c $^

另外,当我运行makefile时,我收到一些关于memcpy的警告:

make的输出:

$make
gcc -fPIC -pthread -DMX_COMPAT_32 -DMATLAB_MEX_FILE  -largeArrayDims -I/usr/local/MATLAB/R2011b/extern/include -c Include_4_TSNNLS.c MatSparse_2_TAUCS.c
MatSparse_2_TAUCS.c: In function ‘mexFunction’:
MatSparse_2_TAUCS.c:98:6: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
      memcpy(mxGetPr(plhs[0]), &A, sizeof(A)); 
      ^
/usr/local/MATLAB/R2011b/bin/mex -cxx CC='gcc' CXX='gcc' LD='gcc' -largeArrayDims -lm -output MatSparse_2_TAUCS MatSparse_2_TAUCS.o

Warning: You are using gcc version "4.8.2-19ubuntu1)".  The version
         currently supported with MEX is "4.3.4".
         For a list of currently supported compilers see: 
         http://www.mathworks.com/support/compilers/current_release/

echo All done
All done

一些帮助将不胜感激.

更新:

有关TAUCS或taucs_ccs_matrix page 12 of the link的信息

根据@chappjc的建议更新了c代码

最佳答案 在CFLAGS中定义MX_COMPAT_32与mex命令的-largeArrayDims选项不兼容.事实上,它恰恰相反.我记下了这些选项如何在
this post底部附近工作.实际上是-compatibleArrayDims选项设置MX_COMPAT_32,这是你不想要的.

另请注意,-largeArrayDims是mex命令的一个选项,因此它依赖于MEXFLAGS而不是CFLAGS.我很确定gcc不知道如何处理该开关.只是不要定义MX_COMPAT_32.

如果您想坚持使用32位索引,但看起来您可以尝试设置MX_INTERNAL_730并保留MX_COMPAT_32,但不要这样做.

点赞