37. Matrix Power medium
Implement a GPU program that raises a square matrix of size to an integer power .
The solve function receives a flattened input matrix input (row-major order), an empty output matrix output of the same size, the dimension N, and the exponent P.
You must compute where matrix multiplication is standard dense multiplication over 32-bit floating point numbers.
Implementation Requirements
- External libraries are not permitted.
- The
solvefunction signature must remain unchanged. - The final result must be written to the
outputarray in row-major order.
Example 1:
Input:
input = [[1.0, 2.0],
[3.0, 4.0]]
N = 2
P = 3
Output:
output = [[37.0, 54.0],
[81.0, 118.0]]
Example 2:
Input:
input = [[1.0, 0.0, 2.0],
[0.0, 1.0, 0.0],
[3.0, 0.0, 0.0]]
N = 3
P = 2
Output:
output = [[7.0, 0.0, 2.0],
[0.0, 1.0, 0.0],
[3.0, 0.0, 6.0]]
Constraints
- Elements of
inputsatisfy - Performance is measured with
N= 512
Starter code (CUDA)
#include <cuda_runtime.h>
// input, output are device pointers
extern "C" void solve(const float* input, float* output, int N, int P) {}
From LeetGPU, CC BY-NC-ND 4.0.