cblas_dgemm的正确使用
我编写了以下代码来简单地调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cblas_dgemm
不适用于指向指针的指针,我尝试使用技巧A[i * N + j]
初始化矩阵,它解决了问题。cblas_dgemm
doesn't work with pointers to pointers, I tried initializing the matrices using the trickA[i * N + j]
and it solved the problem.