意想不到的结果与cblas_dgemv [英] Unexpected result with cblas_dgemv
问题描述
我有一个关于cblas_dgemv的问题。我想了解它是如何工作的。什么我可能做错了。我有一个数组矩阵,然后我尝试读取矩阵RowMajor和ColumnMajor。
我收到了RowMajor案例预期的结果; [6,2,4,6]
不过,对于ColMajor,我得到[-7,3,0,5]当答案应该是[6,3,2,3]
下面是我的code。我使用英特尔MKL。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&mkl.h GT;
#定义NCOLS 5
#定义NROWS 4双A [] = {8,4,7,3,5,1,1,3,2,1,2,3,2,0,1,1,2,3,4,1};双X [] = {-1,2,-1,1,2};双Y [NROWS]
双阿尔法= 1.0,β= 0.0;
烧焦TBUF [1024];
诠释主(){
INT I,J; //打印原始矩阵 // Y =斧
cblas_dgemv(CblasRowMajor,CblasNoTrans,NROWS,NCOLS,α,A,NCOLS,X,1,β,Y,1);
//打印结果向量
为(J = 0; J< NROWS; J ++){
的printf(%F \\ N,Y [J]);
} cblas_dgemv(CblasColMajor,CblasNoTrans,NROWS,NCOLS,α,A,NCOLS,X,1,β,Y,1);
//打印结果向量
为(J = 0; J< NROWS; J ++){
的printf(%F \\ N,Y [J]);
} 返回0;
}
这个问题是关于 LDA
。从参考我们得到了
LDA:矩阵A的第一个维度的尺寸
块引用>的
CblasRowMajor
和CblasColMajor
描述了一个二维矩阵的记忆存储序列。矩阵
A(nrow,NCOL)
的CblasRowMajor
存储单元,首先存储在NCOL
矩阵A
,那么NCOL
值的第一行的值第二排的A
等。矩阵
A(nrow,NCOL)
的CblasColMajor
存储单元,首先存储在nrow
矩阵A
,那么nrow
值的第一列中的值第二列的A
等。因此,在
CblasRowMajor
存储LDA的(矩阵A的第一个维度)的是NCOL
,而在CblasColMajor
的nrow
。在您的例子中,你只需要改变
LDA
第二cblas_dgemv
cblas_dgemv(CblasColMajor,CblasNoTrans,NROWS,NCOLS,α,A,NROWS,X,1,β,Y,1);
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; }
解决方案The issue is on the
lda
. From the reference we get thatlda: The size of the first dimension of matrix A
The
CblasRowMajor
andCblasColMajor
describe the memory storage sequence of a two dimensional matrix.The
CblasRowMajor
storage of a matrixA(nrow,ncol)
means that first are stored thencol
values of the first row of matrixA
, then thencol
values of second row ofA
and so on.The
CblasColMajor
storage of a matrixA(nrow,ncol)
means that first are stored thenrow
values of the first column of matrixA
, then thenrow
values of second column ofA
and so on.So in
CblasRowMajor
storage the LDA (first dimension of matrix A) is thencol
while inCblasColMajor
thenrow
.In your example you just have to change
lda
of the secondcblas_dgemv
cblas_dgemv(CblasColMajor, CblasNoTrans, Nrows, NCols, alpha, A, Nrows, x, 1, beta, y, 1);
这篇关于意想不到的结果与cblas_dgemv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!