'Correct usage of cblas_dgemm
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
