Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
6974 Discussions

I have a problem with MKL, results are wrong, why?

Alper_Basturk
Beginner
301 Views
Hi everybody,

I am new on MKL. I try to make matrix product, but results are wrong, i can not solve the problem. Would you kindly help me, I will be pleased.

Thanks in advance.

Source Code #1:

//transa and transb are "n"

#include

#include

#include

void main()

{

char* trans="n";

double alpha=1.0;

double beta=0.0;

int lda=2;

int ldb=4;

int ldc=2;

int m=2;

int n=3;

int k=4;

int i,j;

double a[2][4];

double b[4][3];

double c[2][3];

printf("a matrisi\\n");

for(i=0;i

for(j=0;j

a=i*j+1.2;

printf("%.02f\\t",a);

}

printf("\\n\\n");

}

printf("b mmatrisi\\n");

for(i=0;i

for(j=0;j

b=i*j+0.5;

printf("%.02f\\t",b);

}

printf("\\n\\n");

}

dgemm(trans,trans,&m,&n,&k,α,(double*)a,&lda,(double*)b,&ldb,β,(double*)c,&ldc);

printf("a*b matrisi\\n");

for(i=0;i

for(j=0;j

printf("%f\\t",c);

}

printf("\\n");

}

getch();

}



----------------

Source Code #2:

//transa and transb are "t"

#include

#include

#include

void main()

{

char* trans="t";

double alpha=1.0;

double beta=0.0;

int lda=4;

int ldb=3;

int ldc=2;

int m=2;

int n=3;

int k=4;

int i,j;

double a[2][4];

double b[4][3];

double c[2][3];

printf("a matrisi\\n");

for(i=0;i

for(j=0;j

a=i*j+1.2;

printf("%.02f\\t",a);

}

printf("\\n\\n");

}

printf("b mmatrisi\\n");

for(i=0;i

for(j=0;j

b=i*j+0.5;

printf("%.02f\\t",b);

}

printf("\\n\\n");

}

dgemm(trans,trans,&m,&n,&k,α,(double*)a,&lda,(double*)b,&ldb,β,(double*)c,&ldc);

printf("a*b matrisi\\n");

for(i=0;i

for(j=0;j

printf("%f\\t",c);

}

printf("\\n");

}

getch();

}


---------------

and respected result is below

2.4 9.6 16.8
5.4 26.6 47.8

but i can notcalculate this result. Please run my source codes and then give your results, i think there is something wrong with my codes, but i can not find.

Thanks again.
0 Kudos
1 Solution
Gennady_F_Intel
Moderator
301 Views
alper,
the causeof the problem you're experiencing is that this function ( dgemm ) uses ( expects) fortran notation but you are using C-notation.
Please usecblas_dgemm(....)instead of dgemm.
Rewrite your test, for example, like this:

double a[2][4];

double b[4][3];

double c[2][3];

int lda=4;

int ldb=3;

int ldc=3;

cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m, n, k,alpha,(double*)a,lda,(double*)b,ldb,

beta,(double*)c,ldc);

and try the test again.

For example, with your data I have:

a matrisi
1.20 1.20 1.20 1.20
1.20 2.20 3.20 4.20
b mmatrisi
0.50 0.50 0.50
0.50 1.50 2.50
0.50 2.50 4.50
0.50 3.50 6.50
a*b matrisi
2.400000 9.600000 16.800000
5.400000 26.600000 47.800000
Press any key to continue . . .
a matrisi1.20 1.20 1.20 1.20
1.20 2.20 3.20 4.20
b mmatrisi0.50 0.50 0.50
0.50 1.50 2.50
0.50 2.50 4.50
0.50 3.50 6.50
a*b matrisi
2.400000 9.600000 16.800000
5.400000 26.600000 47.800000
Press any key to continue . . .
I hope it will help.
--Gennady

View solution in original post

0 Kudos
3 Replies
Gennady_F_Intel
Moderator
302 Views
alper,
the causeof the problem you're experiencing is that this function ( dgemm ) uses ( expects) fortran notation but you are using C-notation.
Please usecblas_dgemm(....)instead of dgemm.
Rewrite your test, for example, like this:

double a[2][4];

double b[4][3];

double c[2][3];

int lda=4;

int ldb=3;

int ldc=3;

cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m, n, k,alpha,(double*)a,lda,(double*)b,ldb,

beta,(double*)c,ldc);

and try the test again.

For example, with your data I have:

a matrisi
1.20 1.20 1.20 1.20
1.20 2.20 3.20 4.20
b mmatrisi
0.50 0.50 0.50
0.50 1.50 2.50
0.50 2.50 4.50
0.50 3.50 6.50
a*b matrisi
2.400000 9.600000 16.800000
5.400000 26.600000 47.800000
Press any key to continue . . .
a matrisi1.20 1.20 1.20 1.20
1.20 2.20 3.20 4.20
b mmatrisi0.50 0.50 0.50
0.50 1.50 2.50
0.50 2.50 4.50
0.50 3.50 6.50
a*b matrisi
2.400000 9.600000 16.800000
5.400000 26.600000 47.800000
Press any key to continue . . .
I hope it will help.
--Gennady
0 Kudos
Alper_Basturk
Beginner
301 Views
Thanks for your kindly interest.

Regards,

Alper Basturk
0 Kudos
petr_gamov
Beginner
301 Views
excelent, I had the same problem but you saved me.
Petr
0 Kudos
Reply