问题
I have a question regarding cblas_dgemv. I am trying to understand how it works. And what I am possibly doing wrong. I have an array Matrix and then I try to read that matrix RowMajor and ColumnMajor.
I am getting the expected result in the RowMajor Case; [6, 2, 4, 6]'.
However for the ColMajor, I am getting [-7, 3, 0, 5]' when the answer should be [6, 3, 2, 3]'
Here is my code. I am using Intel MKL.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mkl.h>
#define NCols 5
#define Nrows 4
double A[] = { 8, 4, 7, 3, 5, 1, 1, 3, 2, 1, 2, 3, 2, 0, 1, 1 , 2, 3, 4, 1};
double x[] = { -1, 2, -1, 1, 2 };
double y[Nrows];
double alpha = 1.0, beta = 0.0;
char tbuf[1024];
int main() {
int i, j;
// Print original matrix
// y = Ax
cblas_dgemv(CblasRowMajor, CblasNoTrans, Nrows, NCols, alpha, A, NCols, x, 1, beta, y, 1);
// Print resulting vector
for (j = 0; j < Nrows; j++) {
printf(" %f\n", y[j]);
}
cblas_dgemv(CblasColMajor, CblasNoTrans, Nrows, NCols, alpha, A, NCols, x, 1, beta, y, 1);
// Print resulting vector
for (j = 0; j < Nrows; j++) {
printf(" %f\n", y[j]);
}
return 0;
}
回答1:
The issue is on the lda
. From the reference we get that
lda: The size of the first dimension of matrix A
The CblasRowMajor
and CblasColMajor
describe the memory storage sequence of a two dimensional matrix.
The CblasRowMajor
storage of a matrix A(nrow,ncol)
means that first are stored the ncol
values of the first row of matrix A
, then the ncol
values of second row of A
and so on.
The CblasColMajor
storage of a matrix A(nrow,ncol)
means that first are stored the nrow
values of the first column of matrix A
, then the nrow
values of second column of A
and so on.
So in CblasRowMajor
storage the LDA (first dimension of matrix A) is the ncol
while in CblasColMajor
the nrow
.
In your example you just have to change lda
of the second cblas_dgemv
cblas_dgemv(CblasColMajor, CblasNoTrans, Nrows, NCols, alpha, A, Nrows, x, 1, beta, y, 1);
来源:https://stackoverflow.com/questions/30195779/unexpected-result-with-cblas-dgemv