TensorTonic
Problems
Matrix Addition as a CUDA Kernel
Linear AlgebraGPU Programming
Easy

Add two float32 matrices elementwise on the GPU.

Cij=Aij+BijC_{ij} = A_{ij} + B_{ij}Cij​=Aij​+Bij​

Here, A and B are the input matrices, C is the output matrix, M is the number of rows, N is the number of columns, and i and j are valid row and column indices. All buffers use contiguous row-major storage, with each row's N values placed consecutively.

Write the result into the preallocated float32 device buffer C with shape (M, N), leaving A and B unchanged. The function returns no value.

Loading visualization...

Examples

Input: A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]], M = 2, N = 2

Output: [[6.0, 8.0], [10.0, 12.0]]

Explanation: Each output entry adds the two inputs at the same row and column.

Input: A = [[0, 1, 2], [3, 4, 5]], B = [[10, 10, 10], [20, 20, 20]], M = 2, N = 3

Output: [[10.0, 11.0, 12.0], [23.0, 24.0, 25.0]]

Hint 1

Use the x and y thread coordinates to distinguish columns from rows.

Hint 2

The row stride in either flat input buffer is N elements.

Hint 3

Check both coordinates before accessing a matrix element.

Requirements

  • Add matching entries from A and B using their row-major layout.
  • Write every output element without changing either input or accessing beyond the buffers.
  • Keep the supplied solve interface.

Constraints

  • M and N are each between 1 and 4,096.
  • A, B, and C are contiguous row-major device buffers with shape (M, N).
  • Input values are finite float32.
Try Similar Problems
Outer Product as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyMatrix Transpose as a CUDA KernelEasyRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumVector Addition as a CUDA KernelEasyVector Subtraction as a CUDA KernelEasyL1 Normalization as a CUDA KernelMediumCosine Similarity as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMedium

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Matrix Addition as a CUDA Kernel

Matrix Addition as a CUDA Kernel

Linear AlgebraGPU Programming
Easy

Add two float32 matrices elementwise on the GPU.

Cij=Aij+BijC_{ij} = A_{ij} + B_{ij}Cij​=Aij​+Bij​

Here, A and B are the input matrices, C is the output matrix, M is the number of rows, N is the number of columns, and i and j are valid row and column indices. All buffers use contiguous row-major storage, with each row's N values placed consecutively.

Write the result into the preallocated float32 device buffer C with shape (M, N), leaving A and B unchanged. The function returns no value.

Loading visualization...

Examples

Input: A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]], M = 2, N = 2

Output: [[6.0, 8.0], [10.0, 12.0]]

Explanation: Each output entry adds the two inputs at the same row and column.

Input: A = [[0, 1, 2], [3, 4, 5]], B = [[10, 10, 10], [20, 20, 20]], M = 2, N = 3

Output: [[10.0, 11.0, 12.0], [23.0, 24.0, 25.0]]

Hint 1

Use the x and y thread coordinates to distinguish columns from rows.

Hint 2

The row stride in either flat input buffer is N elements.

Hint 3

Check both coordinates before accessing a matrix element.

Requirements

  • Add matching entries from A and B using their row-major layout.
  • Write every output element without changing either input or accessing beyond the buffers.
  • Keep the supplied solve interface.

Constraints

  • M and N are each between 1 and 4,096.
  • A, B, and C are contiguous row-major device buffers with shape (M, N).
  • Input values are finite float32.
Try Similar Problems
Outer Product as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyMatrix Transpose as a CUDA KernelEasyRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumVector Addition as a CUDA KernelEasyVector Subtraction as a CUDA KernelEasyL1 Normalization as a CUDA KernelMediumCosine Similarity as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMedium

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1