TensorTonic
Problems
Hadamard Product as a CUDA Kernel
Linear AlgebraGPU Programming
Easy

Implement a CUDA kernel that computes the elementwise (Hadamard) product of two matrices AAA and BBB of shape (M,N)(M, N)(M,N), writing the result to CCC. For each valid index (i,j)(i, j)(i,j):

C[i,j]=A[i,j]⋅B[i,j]C[i, j] = A[i, j] \cdot B[i, j]C[i,j]=A[i,j]⋅B[i,j]

Both inputs and the output share the same shape (M,N)(M, N)(M,N). All three matrices are stored as row-major flat arrays of length M⋅NM \cdot NM⋅N, so element (i,j)(i, j)(i,j) lives at linear index i⋅N+ji \cdot N + ji⋅N+j.

Loading visualization...

Examples

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

Output: [[5.0, 12.0], [21.0, 32.0]]

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

Output: [[1.0, 0.0, 3.0], [0.0, 5.0, 0.0]]

Hint 1

Map threadIdx.y\texttt{threadIdx.y}threadIdx.y and blockIdx.y\texttt{blockIdx.y}blockIdx.y to the row iii, and threadIdx.x\texttt{threadIdx.x}threadIdx.x and blockIdx.x\texttt{blockIdx.x}blockIdx.x to the column jjj, so each thread owns one (i,j)(i, j)(i,j).

Hint 2

Each output element depends only on the matching pair A[i,j]A[i, j]A[i,j] and B[i,j]B[i, j]B[i,j], so there is no reduction and no need for shared memory.

Hint 3

Because the grid is rounded up to cover any shape, some threads fall outside the matrix. An early-return bounds check on iii and jjj keeps those threads from writing past the buffer.

Requirements

  • Launch one thread per output element across a 2D grid
  • Respect row-major layout when computing the linear index
  • Guard against threads where i≥Mi \ge Mi≥M or j≥Nj \ge Nj≥N
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N≤40961 \le M, N \le 40961≤M,N≤4096
  • Input and output buffers are device pointers
  • Use 16×1616 \times 1616×16 thread blocks
  • Values fit in 32-bit float
Try Similar Problems
Outer Product as a CUDA KernelEasyMatrix Addition 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

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Hadamard Product as a CUDA Kernel

Hadamard Product as a CUDA Kernel

Linear AlgebraGPU Programming
Easy

Implement a CUDA kernel that computes the elementwise (Hadamard) product of two matrices AAA and BBB of shape (M,N)(M, N)(M,N), writing the result to CCC. For each valid index (i,j)(i, j)(i,j):

C[i,j]=A[i,j]⋅B[i,j]C[i, j] = A[i, j] \cdot B[i, j]C[i,j]=A[i,j]⋅B[i,j]

Both inputs and the output share the same shape (M,N)(M, N)(M,N). All three matrices are stored as row-major flat arrays of length M⋅NM \cdot NM⋅N, so element (i,j)(i, j)(i,j) lives at linear index i⋅N+ji \cdot N + ji⋅N+j.

Loading visualization...

Examples

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

Output: [[5.0, 12.0], [21.0, 32.0]]

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

Output: [[1.0, 0.0, 3.0], [0.0, 5.0, 0.0]]

Hint 1

Map threadIdx.y\texttt{threadIdx.y}threadIdx.y and blockIdx.y\texttt{blockIdx.y}blockIdx.y to the row iii, and threadIdx.x\texttt{threadIdx.x}threadIdx.x and blockIdx.x\texttt{blockIdx.x}blockIdx.x to the column jjj, so each thread owns one (i,j)(i, j)(i,j).

Hint 2

Each output element depends only on the matching pair A[i,j]A[i, j]A[i,j] and B[i,j]B[i, j]B[i,j], so there is no reduction and no need for shared memory.

Hint 3

Because the grid is rounded up to cover any shape, some threads fall outside the matrix. An early-return bounds check on iii and jjj keeps those threads from writing past the buffer.

Requirements

  • Launch one thread per output element across a 2D grid
  • Respect row-major layout when computing the linear index
  • Guard against threads where i≥Mi \ge Mi≥M or j≥Nj \ge Nj≥N
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N≤40961 \le M, N \le 40961≤M,N≤4096
  • Input and output buffers are device pointers
  • Use 16×1616 \times 1616×16 thread blocks
  • Values fit in 32-bit float
Try Similar Problems
Outer Product as a CUDA KernelEasyMatrix Addition 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

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1