Compute the outer product (dyadic product) of two 1D vectors on the GPU. Given vector a of length M and vector b of length N, produce the 2D matrix C=a⊗b of shape (M,N). For each valid index (i,j):
C[i,j]=a[i]⋅b[j]The output matrix C is stored as a row-major flat array of length M⋅N, so element (i,j) lives at linear index i⋅N+j.
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]]
Map threadIdx.y and blockIdx.y to the row i, and threadIdx.x and blockIdx.x to the column j, so each thread owns exactly one (i,j) element.
Each output element reads only a[i] and b[j], so there is no reduction or shared state. Write C[i⋅N+j] directly.
Because the grid is rounded up to cover any shape, some threads fall outside the matrix. Guard with i<M and j<N before writing.
Accepts: array
Accepts: array
Submit a correct solution to see your kernel performance.
Compute the outer product (dyadic product) of two 1D vectors on the GPU. Given vector a of length M and vector b of length N, produce the 2D matrix C=a⊗b of shape (M,N). For each valid index (i,j):
C[i,j]=a[i]⋅b[j]The output matrix C is stored as a row-major flat array of length M⋅N, so element (i,j) lives at linear index i⋅N+j.
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]]
Map threadIdx.y and blockIdx.y to the row i, and threadIdx.x and blockIdx.x to the column j, so each thread owns exactly one (i,j) element.
Each output element reads only a[i] and b[j], so there is no reduction or shared state. Write C[i⋅N+j] directly.
Because the grid is rounded up to cover any shape, some threads fall outside the matrix. Guard with i<M and j<N before writing.
Accepts: array
Accepts: array
Submit a correct solution to see your kernel performance.