TensorTonic
Problems
1D Convolution as a CUDA Kernel
Computer VisionGPU Programming
Easy

Implement a CUDA kernel that performs a 1D cross-correlation of a single-channel input signal of length NNN with a single-channel kernel of length kNkNkN. This is the operation deep learning frameworks call "convolution" (no kernel flip). Stride is 111 and there is no padding (valid).

For each output position iii:

output[i]=∑a=0kN−1input[i+a]⋅kernel[a]\text{output}[i] = \sum_{a=0}^{kN-1} \text{input}[i + a] \cdot \text{kernel}[a]output[i]=a=0∑kN−1​input[i+a]⋅kernel[a]

The output has length N−kN+1N - kN + 1N−kN+1. All three buffers are flat float32 device arrays.

Loading visualization...

Examples

Input: N = 5, kN = 3, input = [1, 2, 3, 4, 5], kernel = [1, 0, -1]

Output: [-2.0, -2.0, -2.0]

Each length-333 window contributes input[i]−input[i+2]\text{input}[i] - \text{input}[i+2]input[i]−input[i+2], which is −2-2−2 everywhere on this ramp.

Input: N = 4, kN = 2, input = [1, 2, 3, 4], kernel = [0.5, 0.5]

Output: [1.5, 2.5, 3.5]

A simple two-tap box average across each adjacent pair.

Hint 1

Each output position owns an independent length-kNkNkN window of input reads, so threads do not need to synchronize or share state.

Hint 2

Map blockIdx.x, threadIdx.x\texttt{blockIdx.x, threadIdx.x}blockIdx.x, threadIdx.x to the output index iii and remember the output length is outN=N−kN+1\texttt{outN} = N - kN + 1outN=N−kN+1, not NNN.

Hint 3

For small kNkNkN the inner loop is tight enough that the compiler unrolls it; accumulating into a local float\texttt{float}float avoids repeated global memory writes.

Requirements

  • Launch one thread per output element using a 1D grid with 256256256 threads per block
  • Accumulate the inner sum over the kNkNkN kernel positions into a register before the single store
  • The solve\texttt{solve}solve signature must not change
  • Use cross-correlation indexing (no kernel flip)

Constraints

  • 1≤N≤1071 \le N \le 10^71≤N≤107
  • 1≤kN≤N1 \le kN \le N1≤kN≤N
  • All buffers are device pointers
  • Use 256256256 threads per block
  • Values are 32-bit float, cross-correlation only (no kernel flip)
Try Similar Problems
2D Convolution as a CUDA KernelMediumMax Pool 2D as a CUDA KernelMedium3D Convolution as a CUDA KernelHardImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasyMin of Array as a CUDA KernelEasyImplement Leaky ReLU as a CUDA KernelEasySum of Array as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasy

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

1D Convolution as a CUDA Kernel

1D Convolution as a CUDA Kernel

Computer VisionGPU Programming
Easy

Implement a CUDA kernel that performs a 1D cross-correlation of a single-channel input signal of length NNN with a single-channel kernel of length kNkNkN. This is the operation deep learning frameworks call "convolution" (no kernel flip). Stride is 111 and there is no padding (valid).

For each output position iii:

output[i]=∑a=0kN−1input[i+a]⋅kernel[a]\text{output}[i] = \sum_{a=0}^{kN-1} \text{input}[i + a] \cdot \text{kernel}[a]output[i]=a=0∑kN−1​input[i+a]⋅kernel[a]

The output has length N−kN+1N - kN + 1N−kN+1. All three buffers are flat float32 device arrays.

Loading visualization...

Examples

Input: N = 5, kN = 3, input = [1, 2, 3, 4, 5], kernel = [1, 0, -1]

Output: [-2.0, -2.0, -2.0]

Each length-333 window contributes input[i]−input[i+2]\text{input}[i] - \text{input}[i+2]input[i]−input[i+2], which is −2-2−2 everywhere on this ramp.

Input: N = 4, kN = 2, input = [1, 2, 3, 4], kernel = [0.5, 0.5]

Output: [1.5, 2.5, 3.5]

A simple two-tap box average across each adjacent pair.

Hint 1

Each output position owns an independent length-kNkNkN window of input reads, so threads do not need to synchronize or share state.

Hint 2

Map blockIdx.x, threadIdx.x\texttt{blockIdx.x, threadIdx.x}blockIdx.x, threadIdx.x to the output index iii and remember the output length is outN=N−kN+1\texttt{outN} = N - kN + 1outN=N−kN+1, not NNN.

Hint 3

For small kNkNkN the inner loop is tight enough that the compiler unrolls it; accumulating into a local float\texttt{float}float avoids repeated global memory writes.

Requirements

  • Launch one thread per output element using a 1D grid with 256256256 threads per block
  • Accumulate the inner sum over the kNkNkN kernel positions into a register before the single store
  • The solve\texttt{solve}solve signature must not change
  • Use cross-correlation indexing (no kernel flip)

Constraints

  • 1≤N≤1071 \le N \le 10^71≤N≤107
  • 1≤kN≤N1 \le kN \le N1≤kN≤N
  • All buffers are device pointers
  • Use 256256256 threads per block
  • Values are 32-bit float, cross-correlation only (no kernel flip)
Try Similar Problems
2D Convolution as a CUDA KernelMediumMax Pool 2D as a CUDA KernelMedium3D Convolution as a CUDA KernelHardImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasyMin of Array as a CUDA KernelEasyImplement Leaky ReLU as a CUDA KernelEasySum of Array as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasy

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