dgemv produces only zero vectors as results
1 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to link this simple matlab script
A = [2 3; -1 4];
B = [5; 3];
C = zeros(2, 1);
alpha = 1;
beta = 1;
C = mv_mult(A, B, alpha, beta)
with the following mexFunction
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "blas.h"
#include "mex.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
/* pointers to input & output matrices*/
double *A, *v, *Y, *a, *b;
ptrdiff_t rows_A, cols_A;
ptrdiff_t rows_v, cols_v;
// create input matrix (A)
A = mxGetDoubles(prhs[0]);
rows_A = mxGetM(prhs[0]);
cols_A = mxGetN(prhs[0]);
// create input vector (v)
v = mxGetDoubles(prhs[1]);
rows_v = mxGetM(prhs[1]);
cols_v = 1;
// parse scalars (a, b)
a = mxGetDoubles(prhs[2]);
b = mxGetDoubles(prhs[3]);
// create output matrix (Y)
plhs[0] = mxCreateDoubleMatrix(rows_A, cols_v, mxREAL);
Y = mxGetPr(plhs[0]);
printf("A[0]: %lf\n", A[0]);
printf("A[1]: %lf\n", A[1]);
// compute mm multiplication using blas2 operations
char chn = 'N';
const long int i_one = 1;
dgemv_(&chn, &rows_A, &cols_A,
a, A, &cols_v,
v, &i_one,
b, Y, &i_one);
}
The function compiles without errors nor warnings but matlab replies with [0 0]' instead of [19 7]'. I can't understand why though. Note that I am trying to use blas2 operations only.
1 个评论
Walter Roberson
2020-1-19
https://www.mathworks.com/matlabcentral/answers/390546-wrong-result-when-calling-cblas-dgemv-function-in-a-mex-file might help
采纳的回答
James Tursa
2020-1-20
编辑:James Tursa
2020-1-20
Two things:
1) All of the integers that you are passing into BLAS/LAPACK functions should be the same. Why are you using ptrdiff_t and long int? Make them the same. What that needs to be depends on the version of MATLAB you are running, but based on the fact you are calling mxGetDoubles it indicates a later version, so use ptrdiff_t for that i_one. Or use the supplied mwSignedIndex macro.
2) You have an incorrect argument for the leading dimension of A, LDA. You have &cols_v when it should be &rows_A:
dgemv_(&chn, &rows_A, &cols_A,
a, A, &rows_A, // <-- changed
v, &i_one,
b, Y, &i_one);
2 个评论
James Tursa
2020-1-20
You can find several online links that describe the interface to the BLAS/LAPACK routines. You can generally trust them as far as the order and meaning of the arguments, and where the floating point doubles or singles are. But you cannot trust them for what integer size to use in the arguments. The integer size needed will entirely depend on your particular setup and which BLAS/LAPACK libraries you are linking to. So you need to know that detail about the libraries to know what is correct. For the MATLAB supplied BLAS/LAPACK libraries, I think you are always safe to use their supplied mwSignedIndex macro, which would be translated into 32-bit integers or 64-bit integers appropriate for the system. In any event, regardless of the particular libraries involved, all of the integer arguments should always be defined the same way.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!