意想不到的结果与cblas_dgemv [英] Unexpected result with cblas_dgemv

查看:612
本文介绍了意想不到的结果与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 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);

这篇关于意想不到的结果与cblas_dgemv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆