MKL文件在MEX文件和独立C ++中的行为不同

虫族

我正在尝试使用mex文件在Matlab中使GPc(https://github.com/SheffieldML/GPc)工作。我得到了可以工作的示例,我认为我目前对作为独立的C ++程序感兴趣,这很好用。但是,当我尝试在mex中执行相同操作并通过Matlab运行它时,出现了一些错误,尤其是:

MKL ERROR: Parameter 4 was incorrect on entry to DPOTRF.

或者

** On entry to DPOTRF parameter number  4 had an illegal value

取决于我使用的是MKL的系统版本还是Matlab附带的版本。对dpotrf的调用是:

dpotrf_(type, nrows, vals, nrows, info);

所有变量均有效(type =“ U”,nrows = 40,vals = double [40 * 40]),并且具有以下接口:

extern "C" void dpotrf_(
        const char* t,  // whether upper or lower triangluar 'U' or 'L'
        const int &n,   // (input)
        double *a,  // a[n][lda] (input/output)
        const int &lda, // (input)
        int &info   // (output)
        );

(均取自GPc)。LDA最初以ncols的形式提供(我认为这是不正确的,但是我还没有向图书馆作者查询过),但是它不应该有所作为,因为这是在平方矩阵上调用的。

我担心引用可能存在问题,所以我将接口头更改为接受int *(例如http://www.netlib.org/clapack/clapack-3.2.1-CMAKE/SRC/dpotrf.c) ,但是那开始给了我段错误,所以让我认为那里的引用是正确的。

有人知道什么地方可能出问题吗?

安姆罗

我尝试通过最后的示例进行重现,但是没有看到任何错误。实际上,结果与MATLAB的结果相同。

mex_chol.cpp

#include "mex.h"
#include "lapack.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    // verify arguments
    if (nrhs != 1 || nlhs > 1) {
        mexErrMsgTxt("Wrong number of arguments.");
    }
    if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0])) {
        mexErrMsgTxt("Input must be a real double matrix.");
    }
    if (mxGetM(prhs[0]) != mxGetN(prhs[0])) {
        mexErrMsgTxt("Input must be a symmetric positive-definite matrix.");
    }

    // copy input matrix to output (its contents will be overwritten)
    plhs[0] = mxDuplicateArray(prhs[0]);

    // pointer to data
    double *A = mxGetPr(plhs[0]);
    mwSignedIndex n = mxGetN(plhs[0]);

    // perform matrix factorization
    mwSignedIndex info = 0;
    dpotrf("U", &n, A, &n, &info);

    // check if call was successful
    if (info < 0) {
        mexErrMsgTxt("Parameters had an illegal value.");
    } else if (info > 0) {
        mexErrMsgTxt("Matrix is not positive-definite.");
    }
}

请注意,MATLAB已经附带了BLAS / LAPCK标头和库(Intel MKL实现)。其实这是$MATLABROOT\extern\include\lapack.h有作为的函数原型dpotrf

#define dpotrf FORTRAN_WRAPPER(dpotrf)
extern void dpotrf(
    char   *uplo,
    ptrdiff_t *n,
    double *a,
    ptrdiff_t *lda,
    ptrdiff_t *info
);

这是上面的C ++代码的编译方式:

>> mex -largeArrayDims mex_chol.cpp libmwblas.lib libmwlapack.lib

最后,让我们测试一下MEX函数:

% some random symmetric positive semidefinite matrix
A = gallery('randcorr',10);

% our MEX-version of Cholesky decomposition
chol2 = @(A) triu(mex_chol(A));

% compare
norm(chol(A) - chol2(A))   % I get 0

(请注意,MEX代码按原样返回工作矩阵,而LAPACK例程仅覆盖矩阵的一半。因此,我使用TRIU将另一半清零,并提取了上半部分)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

getDeclaredMethods()在Java 7和Java 8中的行为不同

Java和Android中Calendar类的不同行为

事务中的游标在go和psql之间的行为不同

枚举常量在C和C ++中的行为不同

为什么$ _在.ps1和.psm1文件中的行为不同?

python脚本和python空闲中的行为不同?

Haskell中Applicative在元组和列表上的不同行为

*和+在正则表达式中的行为不同

命令在终端或程序(Python和C ++)中给出不同的行为

setjmp / longjmp在发行和调试中的不同行为

C ++中字符和整数数组的不同行为

为什么在assertEquals()中stdClass和匿名类的行为不同?

在GCC和MSVC中,consteval的不同行为

在Windows XP和Windows 7中启动.vbs文件的不同行为

`rails new`:Linux和Windows中的不同行为

$(...)在zsh和bash函数中的不同行为

用C读取文件:“ r”和“ a +”标志的不同行为

用jarbundler从jar中创建的文件的行为与源jar不同

!(* example)文件的glob模式在bash脚本中的行为与在bash shell中的行为不同

Grok调试器和Logstash grok中的行为不同

在Python Shell和* .py文件中运行时,Python有不同的内存管理行为

红宝石中“ do .. end”和“ {..}”块的不同行为

pylab:shell和脚本中的不同行为

Libreoffice在Debian Jessie和Ubuntu中具有不同的行为

程序在 FPC 和 ObjFPC 中的行为不同

Flex 在 chrome 和 Firefox 中的行为不同

bash 源文件中的多个变量定义的行为不同

awk 在我的本地系统和 RedHat 6.5 中的行为不同

Chromium 和 Safari 中不同的 `String` 函数行为