MATLAB: Dgemv produces only zero vectors as results

blascMATLABmex

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.

Best Answer

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);