2. Matrix Multiplication easy
Write a program that multiplies two matrices of 32-bit floating point numbers on a GPU. Given matrix of dimensions and matrix of dimensions , compute the product matrix , which will have dimensions . All matrices are stored in row-major format.
Implementation Requirements
- Use only native features (external libraries are not permitted)
- The
solvefunction signature must remain unchanged - The final result must be stored in matrix
C
Example 1:
Input:
Matrix ():
Matrix ():
Output:
Matrix ():
Example 2:
Input:
Matrix ():
Matrix ():
Output:
Matrix ():
Constraints
- 1 ≤
M,N,K≤ 8192 - Performance is measured with
M= 8192,N= 6144,K= 4096
Starter code (CUDA)
#include <cuda_runtime.h>
__global__ void matrix_multiplication_kernel(const float* A, const float* B, float* C, int M, int N,
int K) {}
// A, B, C are device pointers (i.e. pointers to memory on the GPU)
extern "C" void solve(const float* A, const float* B, float* C, int M, int N, int K) {
dim3 threadsPerBlock(16, 16);
dim3 blocksPerGrid((K + threadsPerBlock.x - 1) / threadsPerBlock.x,
(M + threadsPerBlock.y - 1) / threadsPerBlock.y);
matrix_multiplication_kernel<<<blocksPerGrid, threadsPerBlock>>>(A, B, C, M, N, K);
cudaDeviceSynchronize();
}
From LeetGPU, CC BY-NC-ND 4.0.