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

incorrect dgemm results

ado
Beginner
341 Views

Hi,

I was using random matrix to test some code. I found dgemm gave wrong results sometimes. If I use truncated precision as copied from the console display, the results can be correct again. So I saved the generated matrix to a binary file and attached it (I changed the file extension to .cpp so I could upload it to this post). Could you take a look and let me know what went wrong?

output is incorrect:

 A=[
 7064610704830808.00000000 -2571117317297868.50000000 371407000938997.87500000
 6397257092471370.00000000 -2328238480630291.00000000 336322292936225.12500000
 -29483308061538568.00000000 10730250695401230.00000000 -1550022709306368.50000000
 -14929918819165514.00000000 5433643044981792.00000000 -784908978650044.00000000
]

 B=[
 -0.20072837 -0.01530215
 -0.57723645 -0.10924951
 -0.17790418 -0.46522968
]

 Out=[
 -2.00000000 -0.53125000
 0.27343750 1.50000000
 0.81250000 1.12500000
 -0.26562500 -2.68750000
]

 

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <mkl.h>

using namespace std;

void print_matrix( char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda );

int main()
{
		int m=4, n=3,k=2;                                
        double * A= new double [m*n];
        double * B= new double [n*k];                                                      
        double * out= new double [m*k];

		ifstream infile;
		infile.open("matrix3.cpp", ios::binary | ios::in);
        infile.read((char *)&A[0], m*n*sizeof(double));
        infile.read((char *)&B[0], n*k*sizeof(double));
        infile.close();

		cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 
                m, k, n, 1.0, A, n, B, k, 0.0, out, k);

		print_matrix("A=[",m,n,A,n);  
        print_matrix("B=[",n,k,B,k);
        print_matrix("Out=[",m,k,out,k);       

		delete[] A;
		delete[] B;
		delete[] out;
		return 0;

}

void print_matrix( char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda ) {
        MKL_INT i, j;
        printf( "\n %s\n", desc );
        for( i = 0; i < m; i++ ) {
                for( j = 0; j < n; j++ ) printf( " %6.8f", a[i*lda+j] );
                printf( "\n" );
        }
                printf( "]\n" );
}

 

0 Kudos
2 Replies
ado
Beginner
341 Views
please help, thanks!
0 Kudos
Zhen_Z_Intel
Employee
341 Views

Hi arth,

Please see our reply in https://software.intel.com/en-us/forums/intel-math-kernel-library/topic/685034, I will close this post that is same question with above link. Thank you.

Best regards,
Fiona

0 Kudos
Reply