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

Compute the outer product (dyadic product) of two 1D vectors on the GPU. Given vector aaa of length MMM and vector bbb of length NNN, produce the 2D matrix C=a⊗bC = a \otimes bC=a⊗b of shape (M,N)(M, N)(M,N). For each valid index (i,j)(i, j)(i,j):

C[i,j]=a[i]⋅b[j]C[i, j] = a[i] \cdot b[j]C[i,j]=a[i]⋅b[j]

The output matrix CCC is stored as a row-major flat array 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 = 3, a = [1, 2], b = [3, 4, 5]

Output: [[3.0, 4.0, 5.0], [6.0, 8.0, 10.0]]

Input: M = 3, N = 2, a = [1, 2, 3], b = [4, 5]

Output: [[4.0, 5.0], [8.0, 10.0], [12.0, 15.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 exactly one (i,j)(i, j)(i,j) element.

Hint 2

Each output element reads only a[i]a[i]a[i] and b[j]b[j]b[j], so there is no reduction or shared state. Write C[i⋅N+j]C[i \cdot N + j]C[i⋅N+j] directly.

Hint 3

Because the grid is rounded up to cover any shape, some threads fall outside the matrix. Guard with i<Mi < Mi<M and j<Nj < Nj<N before writing.

Requirements

  • Launch one thread per output element across a 2D grid
  • Compute C[i⋅N+j]=a[i]⋅b[j]C[i \cdot N + j] = a[i] \cdot b[j]C[i⋅N+j]=a[i]⋅b[j] with row-major indexing
  • 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
Matrix Addition 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

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Outer Product as a CUDA Kernel

Outer Product as a CUDA Kernel

Linear AlgebraGPU Programming
Easy

Compute the outer product (dyadic product) of two 1D vectors on the GPU. Given vector aaa of length MMM and vector bbb of length NNN, produce the 2D matrix C=a⊗bC = a \otimes bC=a⊗b of shape (M,N)(M, N)(M,N). For each valid index (i,j)(i, j)(i,j):

C[i,j]=a[i]⋅b[j]C[i, j] = a[i] \cdot b[j]C[i,j]=a[i]⋅b[j]

The output matrix CCC is stored as a row-major flat array 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 = 3, a = [1, 2], b = [3, 4, 5]

Output: [[3.0, 4.0, 5.0], [6.0, 8.0, 10.0]]

Input: M = 3, N = 2, a = [1, 2, 3], b = [4, 5]

Output: [[4.0, 5.0], [8.0, 10.0], [12.0, 15.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 exactly one (i,j)(i, j)(i,j) element.

Hint 2

Each output element reads only a[i]a[i]a[i] and b[j]b[j]b[j], so there is no reduction or shared state. Write C[i⋅N+j]C[i \cdot N + j]C[i⋅N+j] directly.

Hint 3

Because the grid is rounded up to cover any shape, some threads fall outside the matrix. Guard with i<Mi < Mi<M and j<Nj < Nj<N before writing.

Requirements

  • Launch one thread per output element across a 2D grid
  • Compute C[i⋅N+j]=a[i]⋅b[j]C[i \cdot N + j] = a[i] \cdot b[j]C[i⋅N+j]=a[i]⋅b[j] with row-major indexing
  • 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
Matrix Addition 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

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1