Multiplying Matrices Using dgemm
Use dgemm to Multiply Matrices
* Fortran source code is found in dgemm_example.f PROGRAM MAIN IMPLICIT NONE DOUBLE PRECISION ALPHA, BETA INTEGER M, K, N, I, J PARAMETER (M=2000, K=200, N=1000) DOUBLE PRECISION A(M,K), B(K,N), C(M,N) PRINT *, "This example computes real matrix C=alpha*A*B+beta*C" PRINT *, "using Intel(R) MKL function dgemm, where A, B, and C" PRINT *, "are matrices and alpha and beta are double precision " PRINT *, "scalars" PRINT *, "" PRINT *, "Initializing data for matrix multiplication C=A*B for " PRINT 10, " matrix A(",M," x",K, ") and matrix B(", K," x", N, ")" 10 FORMAT(a,I5,a,I5,a,I5,a,I5,a) PRINT *, "" ALPHA = 1.0 BETA = 0.0 PRINT *, "Intializing matrix data" PRINT *, "" DO I = 1, M DO J = 1, K A(I,J) = (I1) * K + J END DO END DO DO I = 1, K DO J = 1, N B(I,J) = ((I1) * N + J) END DO END DO DO I = 1, M DO J = 1, N C(I,J) = 0.0 END DO END DO PRINT *, "Computing matrix product using Intel(R) MKL DGEMM " PRINT *, "subroutine" CALL DGEMM('N','N',M,N,K,ALPHA,A,M,B,K,BETA,C,M) PRINT *, "Computations completed." PRINT *, "" PRINT *, "Top left corner of matrix A:" PRINT 20, ((A(I,J), J = 1,MIN(K,6)), I = 1,MIN(M,6)) PRINT *, "" PRINT *, "Top left corner of matrix B:" PRINT 20, ((B(I,J),J = 1,MIN(N,6)), I = 1,MIN(K,6)) PRINT *, "" 20 FORMAT(6(F12.0,1x)) PRINT *, "Top left corner of matrix C:" PRINT 30, ((C(I,J), J = 1,MIN(N,6)), I = 1,MIN(M,6)) PRINT *, "" 30 FORMAT(6(ES12.4,1x)) PRINT *, "Example completed." STOP END
CALL DGEMM('N','N',M,N,K,ALPHA,A,M,B,K,BETA,C,M)
 'N'
 Characterindicating that the matricesAandBshould not be transposed or conjugate transposed before multiplication.
 M, N, K
 Integers indicating the size of the matrices:
 A:Mrows byKcolumns
 B:Krows byNcolumns
 C:Mrows byNcolumns
 ALPHA
 Real value used to scale the product of matricesAandB.
 A
 Array used to store matrixA.
 M
 Leading dimension of arrayA, or the number of elements between successivecolumns (for column major storage)in memory. In the case of this exercise the leading dimension is the same as the number ofrows.
 B
 Array used to store matrixB.
 K
 Leading dimension of arrayB, or the number of elements between successivecolumns (for column major storage)in memory. In the case of this exercise the leading dimension is the same as the number ofrows.
 BETA
 Real value used to scale matrixC.
 C
 Array used to store matrixC.
 M
 Leading dimension of arrayC, or the number of elements between successivecolumns (for column major storage)in memory. In the case of this exercise the leading dimension is the same as the number ofrows.
Compile and Link Your Code
 Windows* OS:ifort /Qmkl src\dgemm_example.f
 Linux* OS, macOS*:ifort mkl src/dgemm_example.f
 Windows* OS:build build run_dgemm_example
 Linux* OS, macOS*:make make run_dgemm_example
Example
 Executable


dgemm_example .f  run_dgemm_example 
dgemm_with_timing .f  run_dgemm_with_timing 
matrix_multiplication .f  run_matrix_multiplication 
dgemm_threading_effect_example .f  run_dgemm_threading_effect_example 
Optimization Notice


Intel's compilers may or may not optimize to the same degree
for nonIntel microprocessors for optimizations that are not unique to Intel
microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction
sets and other optimizations. Intel does not guarantee the availability,
functionality, or effectiveness of any optimization on microprocessors not
manufactured by Intel. Microprocessordependent optimizations in this product
are intended for use with Intel microprocessors. Certain optimizations not
specific to Intel microarchitecture are reserved for Intel microprocessors.
Please refer to the applicable product User and Reference Guides for more
information regarding the specific instruction sets covered by this notice.
Notice revision #20110804
