我利用CBLA执行外部产品遇到了麻烦。我的代码如下:

//===SET UP===//
double x1[] = {1,2,3,4};
double x2[] = {1,2,3};
int dx1 = 4;
int dx2 = 3;
double X[dx1 * dx2];
for (int i = 0; i < (dx1*dx2); i++) {X[i] = 0.0;}

//===DO THE OUTER PRODUCT===//
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasTrans, dx1, dx2, 1, 1.0, x1, dx1, x2, 1, 0.0, X, dx1);

//===PRINT THE RESULTS===//
printf("\nMatrix X (%d x %d) = x1 (*) x2 is:\n", dx1, dx2);
for (i=0; i<4; i++) {
    for (j=0; j<3; j++) {
        printf ("%lf ", X[j+i*3]);
    }
    printf ("\n");
}

我得到:

Matrix X (4 x 3) = x1 (*) x2 is:
1.000000 2.000000 3.000000 
0.000000 -1.000000 -2.000000 
-3.000000 0.000000 7.000000 
14.000000 21.000000 0.000000 

但是在这里找到正确的答案:https://www.sharcnet.ca/help/index.php/blas_and_cblas_usage_and_examples

我见过: 在C中有效计算KRONECKER产品

但是,这无济于事,因为他们实际上并没有说如何利用DGEMM实际做到这一点...

有帮助吗?我在这里做错了什么?

有帮助吗?

解决方案

您可以使用DGEMM进行操作,但是使用DGER在风格上是正确的,这是专用的外部产品实现。因此,它更容易正确使用:

cblas_dger(CblasRowMajor, /* you’re using row-major storage */
           dx1,           /* the matrix X has dx1 rows ...  */
           dx2,           /*  ... and dx2 columns.          */
           1.0,           /* scale factor to apply to x1x2' */
           x1,
           1,             /* stride between elements of x1. */
           x2,
           1,             /* stride between elements of x2. */
           X,
           dx2);          /* leading dimension of matrix X. */

DGEMM 拥有一个不错的功能 \beta = 0 为您初始化结果矩阵,从而使您免于需要在通话之前明确归零。 @Artem Shinkarov的答案提供了有关如何使用DGEMM的很好描述。

其他提示

但是,在Blas中,接口并不是很方便,但是,让我们尝试弄清楚它。首先,假设我们所有的矩阵都在Rowmajor中。现在我们有以下设置

     row  col
x1:  dx1   1   (A)
x2:   1   dx2  (B)
 X:  dx1  dx2  (C)

现在,我们只需要根据文档填写电话,该文档是根据

C = \alpha A*B + \beta C

所以我们得到:

cblas_dgemm (CblasRowMajor, CblasNoTrans, CblasNoTrans,
             (int)dx1, /* rows in A         */
             (int)dx2, /* columns in B      */
             (int)1,   /* columns in A      */
             1.0, x1,  /* \alpha, A itself  */
             (int)1,   /* Colums in A       */
             x2,       /* B itself          */
             (int)dx2, /* Columns in B      */
             0.0, X,   /* \beta, C itself   */
             (int)dx2  /* Columns in C  */);

所以我希望这要完成这项工作。这是DGEMM参数的描述: 关联

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top