cblas_dgemm的正确使用

发布于 2025-01-11 22:04:32 字数 2385 浏览 1 评论 0原文

我编写了以下代码来简单地调用 cblas_dgemm 来将两个矩阵相乘。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cblas.h>
#define N 2
void fill_matrices(double **first, double **second, double **result) 
{
    srand(time(NULL)); // randomize seed
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            first[i][j] = rand() % 10;
            second[i][j] = rand() % 10;
            result[i][j] = 0;
        }
    }
}
void print(double **first, double **second, double **result) 
{
    printf("First:\n");
    for (int i = 0; i <  N; i++){
        printf("[ ");
        for (int j = 0; j < N; j++){
            printf("%f ", first[i][j]);
        }
        printf("]\n");
    }
    printf("\nSecond:\n");
    for (int i = 0; i <  N; i++){
        printf("[ ");
        for (int j = 0; j < N; j++){
            printf("%f ", second[i][j]);
        }
        printf("]\n");
    }
    printf("\nResult:\n");
    for (int i = 0; i <  N; i++){
        printf("[ ");
        for (int j = 0; j < N; j++){
            printf("%f ", result[i][j]);
        }
        printf("]\n");
    }   
}
int main()
{
    double** first = (double**)malloc(N * sizeof(double*));
    for (int index=0;index<N;++index)
    {
        first[index] = (double*)malloc(N * sizeof(double));
    }
    double** second = (double**)malloc(N * sizeof(double*));
    for (int index=0;index<N;++index)
    {
        second[index] = (double*)malloc(N * sizeof(double));
    }    
    double** result = (double**)malloc(N * sizeof(double*));
    for (int index=0;index<N;++index)
    {
        result[index] = (double*)malloc(N * sizeof(double));
    }
    
    fill_matrices(first, second, result);
 
    cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 
                N,N,N, 1.0, *first, 
                N, *second, N, 0.0, 
                *result, N);

    print(first, second, result);
    return 0;
}

构建命令是

gcc -O3  -c mm_blas.c -o mm_blas.o
g++ -std=c++11 -o  mm_blas mm_blas.o -lblas

但是,程序的输出显示错误的乘法。

First:
[ 1.000000 4.000000 ]
[ 4.000000 4.000000 ]

Second:
[ 8.000000 4.000000 ]
[ 2.000000 6.000000 ]

Result:
[ 8.000000 4.000000 ]
[ 0.000000 0.000000 ]

不仅1*8+4*2写成8,而且第二行全为零。我根据示例使用该语法。我想知道传递给函数的选项是否正确。对此有什么想法吗?

I have written the following code to simply call cblas_dgemm to multiply two matrices.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cblas.h>
#define N 2
void fill_matrices(double **first, double **second, double **result) 
{
    srand(time(NULL)); // randomize seed
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            first[i][j] = rand() % 10;
            second[i][j] = rand() % 10;
            result[i][j] = 0;
        }
    }
}
void print(double **first, double **second, double **result) 
{
    printf("First:\n");
    for (int i = 0; i <  N; i++){
        printf("[ ");
        for (int j = 0; j < N; j++){
            printf("%f ", first[i][j]);
        }
        printf("]\n");
    }
    printf("\nSecond:\n");
    for (int i = 0; i <  N; i++){
        printf("[ ");
        for (int j = 0; j < N; j++){
            printf("%f ", second[i][j]);
        }
        printf("]\n");
    }
    printf("\nResult:\n");
    for (int i = 0; i <  N; i++){
        printf("[ ");
        for (int j = 0; j < N; j++){
            printf("%f ", result[i][j]);
        }
        printf("]\n");
    }   
}
int main()
{
    double** first = (double**)malloc(N * sizeof(double*));
    for (int index=0;index<N;++index)
    {
        first[index] = (double*)malloc(N * sizeof(double));
    }
    double** second = (double**)malloc(N * sizeof(double*));
    for (int index=0;index<N;++index)
    {
        second[index] = (double*)malloc(N * sizeof(double));
    }    
    double** result = (double**)malloc(N * sizeof(double*));
    for (int index=0;index<N;++index)
    {
        result[index] = (double*)malloc(N * sizeof(double));
    }
    
    fill_matrices(first, second, result);
 
    cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 
                N,N,N, 1.0, *first, 
                N, *second, N, 0.0, 
                *result, N);

    print(first, second, result);
    return 0;
}

The build commands are

gcc -O3  -c mm_blas.c -o mm_blas.o
g++ -std=c++11 -o  mm_blas mm_blas.o -lblas

However, the output of the program shows wrong multiplication.

First:
[ 1.000000 4.000000 ]
[ 4.000000 4.000000 ]

Second:
[ 8.000000 4.000000 ]
[ 2.000000 6.000000 ]

Result:
[ 8.000000 4.000000 ]
[ 0.000000 0.000000 ]

Not only 1*8+4*2 is written as 8, but also the second row is all zero. I use that syntax based on examples. I wonder if the options passed to the functions are correct. Any thoughts about that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2025-01-18 22:04:32

cblas_dgemm 不适用于指向指针的指针,我尝试使用技巧 A[i * N + j] 初始化矩阵,它解决了问题。

cblas_dgemm doesn't work with pointers to pointers, I tried initializing the matrices using the trick A[i * N + j] and it solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文