Optimized GPU implementation of strided and batched matrix multiply operation


D = gpucoder.stridedMatrixMultiply(A,B) performs strided matrix-matrix multiplication of a batch of matrices. The input matrices A and B for each instance of the batch are located at fixed address offsets from their addresses in the previous instance. gpucoder.stridedMatrixMultiply performs matrix-matrix multiplication of the form:


where α is a scalar multiplication factor, A, B, and D are matrices with dimensions m-by-k, k-by-n, and m-by-n respectively. A and B can optionally be transposed or hermitian-conjugated. By default, α is set to one and the matrices are not transposed. Use the Name,Value pair arguments to specify a different scalar multiplication factor and to specify transpose operations on the input matrices.

All the batches passed to the gpucoder.stridedMatrixMultiply function must be uniform. That is, all instances must have the same dimensions m,n,k.


___ = gpucoder.stridedMatrixMultiply(___,Name,Value) performs strided batched matrix multiply operation using the options specified by one or more Name,Value pair arguments.


collapse all

This example performs a simple batched matrix-matrix multiplication and uses the gpucoder.stridedMatrixMultiply function to generate CUDA® code that calls appropriate cublas<t>gemmStridedBatched APIs.

In one file, write an entry-point function myStridedMatMul that accepts matrix inputs A and B. The input matrices are not transposed, therefore use the 'nn' option.

function [D] = myStridedMatMul(A,B,alpha)

[D] = gpucoder.stridedMatrixMultiply(A,B,'alpha',alpha, ...


Use the coder.newtype function to create a type for a matrix of doubles for use in code generation.

A = coder.newtype('double',[15,42],[0 0]);
B = coder.newtype('double',[42,30],[0 0]);
alpha = 0.3;
inputs = {A,B,alpha};

Use the codegen function to generate a CUDA library.

cfg = coder.gpuConfig('lib');
cfg.GpuConfig.EnableCUBLAS = true;
cfg.GpuConfig.EnableCUSOLVER = true;
cfg.GenerateReport = true;
codegen -config cfg-args inputs myStridedMatMul

The generated CUDA code contains kernels: myStridedMatMul_kernelNN for initializing the input and output matrices. It also contains the cublasDgemmStridedBatched API calls to the cuBLAS library. The following is a snippet of the generated code.

// File:

void myStridedMatMul(const double A[630], const double B[1260], double alpha,
                     double D[450])
  double alpha1;

  myStridedMatMul_kernel1<<<dim3(1U, 1U, 1U), dim3(480U, 1U, 1U)>>>(*gpu_D);
  beta1 = 0.0;
  cudaMemcpy(gpu_alpha1, &alpha1, 8UL, cudaMemcpyHostToDevice);
  cudaMemcpy(gpu_A, (void *)&A[0], 5040UL, cudaMemcpyHostToDevice);
  cudaMemcpy(gpu_B, (void *)&B[0], 10080UL, cudaMemcpyHostToDevice);
  cudaMemcpy(gpu_beta1, &beta1, 8UL, cudaMemcpyHostToDevice);
  cublasDgemmStridedBatched(getCublasGlobalHandle(), CUBLAS_OP_N, CUBLAS_OP_N,
    15, 30, 42, (double *)gpu_alpha1, (double *)&(*gpu_A)[0], 15, 0, (double *)
    &(*gpu_B)[0], 42, 0, (double *)gpu_beta1, (double *)&(*gpu_D)[0], 15, 450, 1);
  cudaMemcpy(&D[0], gpu_D, 3600UL, cudaMemcpyDeviceToHost);

Input Arguments

collapse all

Operands, specified as vectors or matrices. A and B must be 2-D arrays. The number of columns in A must be equal to the number of rows in B.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Complex Number Support: Yes

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: D = gpucoder.stridedMatrixMultiply(A,B,'alpha',0.3,'transpose','CC');

Value of the scalar used for multiplication with A. Default value is one.

Character vector or string composed of two characters, indicating the operation performed on the matrices A and B prior to matrix multiplication. Possible values are normal ('N'), transposed ('T'), or complex conjugate transpose ('C').

Output Arguments

collapse all

Product, returned as a scalar, vector, or matrix. Array D has the same number of rows as input A and the same number of columns as input B.

Introduced in R2020a