Matrix multiplication using array. More...

Functions

AFAPI array matmul (const array &lhs, const array &rhs, const matProp optLhs=AF_MAT_NONE, const matProp optRhs=AF_MAT_NONE)
 Matrix multiply of two arrays. More...
 
AFAPI array matmulNT (const array &lhs, const array &rhs)
 Matrix multiply of two arrays. More...
 
AFAPI array matmulTN (const array &lhs, const array &rhs)
 Matrix multiply of two arrays. More...
 
AFAPI array matmulTT (const array &lhs, const array &rhs)
 Matrix multiply of two arrays. More...
 
AFAPI array matmul (const array &a, const array &b, const array &c)
 Chain 2 matrix multiplications. More...
 
AFAPI array matmul (const array &a, const array &b, const array &c, const array &d)
 Chain 3 matrix multiplications. More...
 
AFAPI af_err af_matmul (af_array *out, const af_array lhs, const af_array rhs, const af_mat_prop optLhs, const af_mat_prop optRhs)
 Matrix multiply of two af_array. More...
 

Detailed Description

Matrix multiplication using array.

Performs a matrix multiplication on the two input arrays after performing the operations specified in the options. The operations are done while reading the data from memory. This results in no additional memory being used for temporary buffers.

Batched matrix multiplications are supported. Given below are the supported types of batch operations for any given set of two matrices A and B.

Size of Input Matrix A Size of Input Matrix B Output Matrix Size
\( \{ M, K, 1, 1 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, 1, 1 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, 1, 1 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, b2, b3 \} \)

where M, K, N are dimensions of the matrix and b2, b3 indicate batch size along the respective dimension.

For the last two entries in the above table, the 2D matrix is broadcasted to match the dimensions of 3D/4D array. This broadcast doesn't involve any additional memory allocations either on host or device.

Note
Sparse support was added to ArrayFire in v3.4.0. This function can be used for Sparse-Dense matrix multiplication. See the notes of the function for usage and restrictions.

Function Documentation

◆ af_matmul()

AFAPI af_err af_matmul ( af_array out,
const af_array  lhs,
const af_array  rhs,
const af_mat_prop  optLhs,
const af_mat_prop  optRhs 
)

Matrix multiply of two af_array.

Performs a matrix multiplication on two arrays (lhs, rhs).

Parameters
[out]outPointer to the output af_array
[in]lhsA 2D matrix af_array object
[in]rhsA 2D matrix af_array object
[in]optLhsTranspose left hand side before the function is performed
[in]optRhsTranspose right hand side before the function is performed
Returns
AF_SUCCESS if the process is successful.
Note
The following applies for Sparse-Dense matrix multiplication.
This function can be used with one sparse input. The sparse input must always be the lhs and the dense matrix must be rhs.
The sparse array can only be of AF_STORAGE_CSR format.
The returned array is always dense.
optLhs an only be one of AF_MAT_NONE, AF_MAT_TRANS, AF_MAT_CTRANS.
optRhs can only be AF_MAT_NONE.

◆ matmul() [1/3]

AFAPI array af::matmul ( const array lhs,
const array rhs,
const matProp  optLhs = AF_MAT_NONE,
const matProp  optRhs = AF_MAT_NONE 
)

Matrix multiply of two arrays.

Performs a matrix multiplication on the two input arrays after performing the operations specified in the options. The operations are done while reading the data from memory. This results in no additional memory being used for temporary buffers.

Batched matrix multiplications are supported. Given below are the supported types of batch operations for any given set of two matrices A and B.

Size of Input Matrix A Size of Input Matrix B Output Matrix Size
\( \{ M, K, 1, 1 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, 1, 1 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, 1, 1 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, b2, b3 \} \)

where M, K, N are dimensions of the matrix and b2, b3 indicate batch size along the respective dimension.

For the last two entries in the above table, the 2D matrix is broadcasted to match the dimensions of 3D/4D array. This broadcast doesn't involve any additional memory allocations either on host or device.

Note
Sparse support was added to ArrayFire in v3.4.0. This function can be used for Sparse-Dense matrix multiplication. See the notes of the function for usage and restrictions.

Parameters
[in]lhsThe array object on the left hand side
[in]rhsThe array object on the right hand side
[in]optLhsTranspose left hand side before the function is performed
[in]optRhsTranspose right hand side before the function is performed
Returns
The result of the matrix multiplication of lhs, rhs
Note
optLhs and optRhs can only be one of AF_MAT_NONE, AF_MAT_TRANS, AF_MAT_CTRANS
This function is not supported in GFOR
The following applies for Sparse-Dense matrix multiplication.
This function can be used with one sparse input. The sparse input must always be the lhs and the dense matrix must be rhs.
The sparse array can only be of AF_STORAGE_CSR format.
The returned array is always dense.
optLhs an only be one of AF_MAT_NONE, AF_MAT_TRANS, AF_MAT_CTRANS.
optRhs can only be AF_MAT_NONE.
Examples:
benchmarks/blas.cpp, benchmarks/cg.cpp, financial/heston_model.cpp, getting_started/convolve.cpp, graphics/gravity_sim.cpp, lin_algebra/svd.cpp, machine_learning/deep_belief_net.cpp, machine_learning/logistic_regression.cpp, machine_learning/neural_network.cpp, machine_learning/perceptron.cpp, machine_learning/rbm.cpp, and machine_learning/softmax_regression.cpp.

◆ matmul() [2/3]

AFAPI array af::matmul ( const array a,
const array b,
const array c 
)

Chain 2 matrix multiplications.

The matrix multiplications are done in a way to reduce temporary memory

Parameters
[in]aThe first array
[in]bThe second array
[in]cThe third array
Returns
out = a x b x c
Note
This function is not supported in GFOR

◆ matmul() [3/3]

AFAPI array af::matmul ( const array a,
const array b,
const array c,
const array d 
)

Chain 3 matrix multiplications.

The matrix multiplications are done in a way to reduce temporary memory

Parameters
[in]aThe first array
[in]bThe second array
[in]cThe third array
[in]dThe fourth array
Returns
out = a x b x c x d
Note
This function is not supported in GFOR

◆ matmulNT()

AFAPI array af::matmulNT ( const array lhs,
const array rhs 
)

Matrix multiply of two arrays.

Performs a matrix multiplication on the two input arrays after performing the operations specified in the options. The operations are done while reading the data from memory. This results in no additional memory being used for temporary buffers.

Batched matrix multiplications are supported. Given below are the supported types of batch operations for any given set of two matrices A and B.

Size of Input Matrix A Size of Input Matrix B Output Matrix Size
\( \{ M, K, 1, 1 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, 1, 1 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, 1, 1 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, b2, b3 \} \)

where M, K, N are dimensions of the matrix and b2, b3 indicate batch size along the respective dimension.

For the last two entries in the above table, the 2D matrix is broadcasted to match the dimensions of 3D/4D array. This broadcast doesn't involve any additional memory allocations either on host or device.

Note
Sparse support was added to ArrayFire in v3.4.0. This function can be used for Sparse-Dense matrix multiplication. See the notes of the function for usage and restrictions.

Parameters
[in]lhsThe array object on the left hand side
[in]rhsThe array object on the right hand side
Returns
The result of the matrix multiplication of lhs, transpose(rhs)
Note
This function is not supported in GFOR
Examples:
lin_algebra/cholesky.cpp, machine_learning/deep_belief_net.cpp, and machine_learning/rbm.cpp.

◆ matmulTN()

AFAPI array af::matmulTN ( const array lhs,
const array rhs 
)

Matrix multiply of two arrays.

Performs a matrix multiplication on the two input arrays after performing the operations specified in the options. The operations are done while reading the data from memory. This results in no additional memory being used for temporary buffers.

Batched matrix multiplications are supported. Given below are the supported types of batch operations for any given set of two matrices A and B.

Size of Input Matrix A Size of Input Matrix B Output Matrix Size
\( \{ M, K, 1, 1 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, 1, 1 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, 1, 1 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, b2, b3 \} \)

where M, K, N are dimensions of the matrix and b2, b3 indicate batch size along the respective dimension.

For the last two entries in the above table, the 2D matrix is broadcasted to match the dimensions of 3D/4D array. This broadcast doesn't involve any additional memory allocations either on host or device.

Note
Sparse support was added to ArrayFire in v3.4.0. This function can be used for Sparse-Dense matrix multiplication. See the notes of the function for usage and restrictions.

Parameters
[in]lhsThe array object on the left hand side
[in]rhsThe array object on the right hand side
Returns
The result of the matrix multiplication of transpose(lhs), rhs
Note
This function is not supported in GFOR
Examples:
machine_learning/deep_belief_net.cpp, machine_learning/logistic_regression.cpp, machine_learning/perceptron.cpp, machine_learning/rbm.cpp, and machine_learning/softmax_regression.cpp.

◆ matmulTT()

AFAPI array af::matmulTT ( const array lhs,
const array rhs 
)

Matrix multiply of two arrays.

Performs a matrix multiplication on the two input arrays after performing the operations specified in the options. The operations are done while reading the data from memory. This results in no additional memory being used for temporary buffers.

Batched matrix multiplications are supported. Given below are the supported types of batch operations for any given set of two matrices A and B.

Size of Input Matrix A Size of Input Matrix B Output Matrix Size
\( \{ M, K, 1, 1 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, 1, 1 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, 1, 1 \} \) \( \{ K, N, b2, b3 \} \) \( \{ M, N, b2, b3 \} \)
\( \{ M, K, b2, b3 \} \) \( \{ K, N, 1, 1 \} \) \( \{ M, N, b2, b3 \} \)

where M, K, N are dimensions of the matrix and b2, b3 indicate batch size along the respective dimension.

For the last two entries in the above table, the 2D matrix is broadcasted to match the dimensions of 3D/4D array. This broadcast doesn't involve any additional memory allocations either on host or device.

Note
Sparse support was added to ArrayFire in v3.4.0. This function can be used for Sparse-Dense matrix multiplication. See the notes of the function for usage and restrictions.

Parameters
[in]lhsThe array object on the left hand side
[in]rhsThe array object on the right hand side
Returns
The result of the matrix multiplication of transpose(lhs), transpose(rhs)
Note
This function is not supported in GFOR
Examples:
machine_learning/deep_belief_net.cpp, and machine_learning/neural_network.cpp.