>  Docs Center  >  IDL Reference  >  Array Manipulation  >  BLAS_GEMM

BLAS_GEMM

BLAS_GEMM

The BLAS_GEMM procedure updates an existing matrix by adding a multiple of the product of two other matrices, according to the following vector operation:

C = alpha * op(A) * op(B) + beta * C

where alpha and beta are scale factors, A, B, and C are input matrices, and op(X) is one of X, XT, or XH.

BLAS_GEMM is based on the following BLAS routines:

Output Type

BLAS Routine

Float

sgemm

Double

dgemm

Complex

cgemm

Double complex

zgemm

Note: BLAS_GEMM uses MKL, so it only supports output of type float, double, complex, or dcomplex. The input arguments can be integer types, at which point they will be converted to the type of the output argument C before being multiplied.

BLAS_GEMM can be faster and use less memory than the usual IDL array notation (e.g., C = C + A * B) for updating existing arrays.

The # Operator vs. MATRIX_MULTIPLY vs. BLAS_GEMM

The following table illustrates how various operations are performed using the # operator versus the MATRIX_MULTIPLY function and BLAS_GEMM procedure:

# Operator

MATRIX_MULTIPLY Function

BLAS_GEMM Procedure

A # B

MATRIX_MULTIPLY(A, B)

BLAS_GEMM(A, B)

TRANSPOSE(A) # B

MATRIX_MULTIPLY(A, B, /ATRANSPOSE)

BLAS_GEMM(A, B, ATRANSPOSE=1)

A # TRANSPOSE(B)

MATRIX_MULTIPLY(A, B, /BTRANSPOSE)

BLAS_GEMM(A, B, BTRANSPOSE=1)

TRANSPOSE(A) # TRANSPOSE(B)

MATRIX_MULTIPLY(A, B, /ATRANSPOSE, /BTRANSPOSE)

BLAS_GEMM(A, B, ATRANSPOSE=1, BTRANSPOSE=1)
CONJ(TRANSPOSE(A)) # B MATRIX_MULTIPLY(CONJ(A), B, /ATRANSPOSE) BLAS_GEMM(A, B, ATRANSPOSE=-1)
A # CONJ(TRANSPOSE(B))

MATRIX_MULTIPLY(A, CONJ(B), /BTRANSPOSE)

BLAS_GEMM(A, B, BTRANSPOSE=-1)
CONJ(TRANSPOSE(A)) #TRANSPOSE(B) MATRIX_MULTIPLY(CONJ(A), B, /ATRANSPOSE, /BTRANSPOSE) BLAS_GEMM(A, B, ATRANSPOSE=-1, BTRANSPOSE=1)
TRANSPOSE(A) # CONJ(TRANSPOSE(B)) MATRIX_MULTIPLY(A, CONJ(B), /ATRANSPOSE, /BTRANSPOSE) BLAS_GEMM(A, B, ATRANSPOSE=1, BTRANSPOSE=-1)
CONJ(TRANSPOSE(A)) # CONJ(TRANSPOSE(B)) MATRIX_MULTIPLY(CONJ(A), CONJ(B), /ATRANSPOSE, /BTRANSPOSE) BLAS_GEMM(A, B, ATRANSPOSE=-1, BTRANSPOSE=-1)

Note: BLAS_GEMM can also be used in place of the ## operator. For example, A ## B is equivalent to BLAS_GEMM(B, A), and A ## TRANSPOSE(B) is equivalent to BLAS_GEMM(BA, /ATRANSPOSE).

Syntax


BLAS_GEMM, C, A, B [, ALPHA=value] [, BETA=value] [, ATRANSPOSE=value] [, BTRANSPOSE=value]

Arguments


C

The array to be updated. C must be of float, double, complex, or dcomplex type. BLAS_GEMM does not change the size and type of C.

A

The first matrix operand in the matrix product. A may be any array that IDL can convert to the type of C. BLAS_GEMM does not change A, but A will be internally converted to the type of C before multiplication.

B

The second matrix operand in the matrix product. B may be any array that IDL can convert to the type of C. BLAS_GEMM does not change B, but A will be internally converted to the type of C before multiplication.

NOTE: Based on the values of ATRANSPOSE and BTRANSPOSE, the arguments A and B must be compatible with each other to perform matrix multiplication, as well as the output argument C.

Keywords


ALPHA

An optional scaling factor to be multiplied with A and B. ALPHA may be any scalar that IDL can convert to the type of C. BLAS_GEMM does not change ALPHA. If ALPHA is not specified then a default value of 1.0 is used.

ATRANSPOSE

Set this keyword to a positive value to multiply using the transpose of A. Set this keyword to a negative value to multiply using the complex conjugate transpose of A.

Note: If C is of type float or double, then any non-zero value for ATRANSPOSE has the same effect of a normal transpose of A.

BETA

An optional scaling factor to be multiplied with C before adding the matrix product of A and B. BETA may be any scalar that IDL can convert to the type of C. BLAS_GEMM does not change BETA. If BETA is not specified, then a default value of 0.0 is used.

BTRANSPOSE

Set this keyword to a positive value to multiply using the transpose of B. Set this keyword to a negative value to multiply using the complex conjugate transpose of B.

Note: If C is of type float or double, then any non-zero value for BTRANSPOSE has the same effect of a normal transpose of B.

Thread Pool Keywords

This routine is written to make use of IDL’s thread pool, which can increase execution speed on systems with multiple CPUs. The values stored in the !CPU system variable control whether IDL uses the thread pool for a given computation. In addition, you can use the thread pool keywords TPOOL_MAX_ELTS, TPOOL_MIN_ELTS, and TPOOL_NOTHREAD to override the defaults established by !CPU for a single invocation of this routine. See Thread Pool Keywords for details.

Note: The thread pool keywords have no effect on Mac, MKL will always use multiple cores.

Examples


The following examples show how to use the BLAS_GEMM procedure to perform matrix multiplication, with scaling.

Create a multidimensional array:

A = RANDOMU(1, 4, 5)

Print A:

PRINT, A

IDL prints:

0.41702199      0.99718481      0.72032452      0.93255734
0.00011438108      0.12812445      0.30233258      0.99904054
0.14675589      0.23608898     0.092338592      0.39658073
0.18626021      0.38791075      0.34556073      0.66974604
0.39676747      0.93553907      0.53881675      0.84631091

Create another multidimensional array:

B = RANDOMU(2, 4, 5)

Print B

PRINT, B

IDL prints:

0.43599489      0.18508208     0.025926229      0.93154085
0.54966247      0.94773060      0.43532240      0.48474911
0.42036781      0.32053643      0.33033481      0.15442668
0.20464863      0.69886267      0.61927098      0.11995054
0.29965466      0.48517591      0.26682729      0.63273776

Create an array to store the product of A and B:

C = FLTARR(4, 4)

Print C

PRINT, C

IDL prints:

0.00000000      0.00000000      0.00000000      0.00000000
0.00000000      0.00000000      0.00000000      0.00000000
0.00000000      0.00000000      0.00000000      0.00000000
0.00000000      0.00000000      0.00000000      0.00000000

Set C equal to the product of A and B (i.e., C = TRANSPOSE(A) # B):

BLAS_GEMM, C, A, B, /ATRANSPOSE

Print C:

PRINT, C

IDL prints:

     0.40058488      0.44700462      0.28055412      0.68458325
0.96416092       1.1066610      0.64946634       1.6659660
0.75123233      0.95236725      0.53855681       1.2142065
1.5130987       2.1252096       1.2306600       2.0300713

Copy C into a temporary variable:

CPrime = C

Add the product of A and B to C (i.e., C += TRANSPOSE(A) # B):

BLAS_GEMM, C, A, B, /ATRANSPOSE, BETA=1.0

Print C:

PRINT, C

IDL prints:

0.80116975      0.89400923      0.56110823       1.3691664
1.9283218       2.2133219       1.2989327       3.3319321
1.5024647       1.9047346       1.0771137       2.4284132
3.0261972       4.2504191       2.4613199       4.0601425

Show that C is now twice CPrime:

PRINT, C / CPrime

IDL prints:

2.0000000       2.0000000       2.0000000       1.9999999
2.0000000       2.0000000       2.0000000       2.0000000
2.0000000       2.0000002       2.0000002       2.0000002
1.9999999       2.0000000       2.0000000       2.0000000

Version History


8.8

Introduced

See Also


MATRIX MULTIPLY



© 2020 Harris Geospatial Solutions, Inc. |  Legal
My Account    |    Store    |    Contact Us