MKL 库在 mex 文件和独立C++中的行为不同
MKL library behaving differently in mex-files and in standalone C++
我正在尝试使用mex文件让GPc(https://github.com/SheffieldML/GPc(在Matlab中工作。我让示例正常工作,我将我目前感兴趣的部分作为独立的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 标头和库(英特尔 MKL 实施(。事实上,这就是$MATLABROOTexternincludelapack.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 将另一半归零并提取上半部分(。
- 使用 MEX 编译C++文件
- 当我运行MEX文件时,MATLAB崩溃
- ASIOSDK;从hostsample.cpp生成mex文件时出错
- 为什么这段代码(在 Matlab 的 MEX 文件中使用 OpenMP)给出不同的结果?
- 索引和访问 mex 函数中单元数组和矩阵的元素
- MEX C++原始数据访问
- 使用 mex 编译库时出错 - __int 64 未命名类型
- 创建 Matlab MEX 函数时,我是否将 mexFunction 放在 c++ 头文件或源文件中
- MEX 文件可以使用 -fsanitize=address 运行吗?
- MEX文件实现特征库伪内函数崩溃
- 为什么用于阈值矩阵元素的 Matlab 逻辑索引操作在性能上优于 mex 实现?
- 从MEX函数中清除MATLAB命令窗口
- 使用带有 MEX 包装器的帮助程序 C 文件从 MATLAB 2016 调用C++代码时出现问题
- 使用 mex 读取和写入文件
- 如何将整数值作为 mex 函数的输入传递?
- MATLAB MEX:-STD = C 11使用,但MEX错误仍然是错误
- 无法生成 mex 文件
- complex_Bessel_function库的编译——与Fortran代码的链接——mex文件
- 如何使用 Boost 库构建 MEX 函数
- 如何在多线程 MEX 函数中打印到控制台?