gpt4 book ai didi

c - mex 运行时错误 : 64-bit mex files using sparse matrices must be rebuilt with the "-largeArrayDims" option

转载 作者:太空狗 更新时间:2023-10-29 15:38:13 28 4
gpt4 key购买 nike

我的 C 代码应该将 Matlab 稀疏格式转换为 TAUCS format 也是列的主要格式。

当然,我是在 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++;
}

}

这是我的生成文件:

  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 代码

最佳答案

定义 MX_COMPAT_32CFLAGS-largeArrayDims 不兼容mex 的选项命令。事实上,它恰恰相反。我在 this post 底部记录了这些选项的工作原理。 .它实际上是 -compatibleArrayDims设置 MX_COMPAT_32 的选项,这是你不想要的。

另请注意 -largeArrayDimsmex 的一个选项命令,所以它继续MEXFLAGS而不是 CFLAGS .我很确定 gcc 不知道如何处理该开关。只是不要定义 MX_COMPAT_32 .

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

关于c - mex 运行时错误 : 64-bit mex files using sparse matrices must be rebuilt with the "-largeArrayDims" option,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29601620/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com