我正在尝试使用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的结果相同。
#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] 删除。
我来说两句