TensorTonic
Problems
Sum of Array as a CUDA Kernel
GPU Programming
Easy

Implement a CUDA kernel that computes the scalar sum of a 1-D float vector input\texttt{input}input of length NNN:

result=∑i=0N−1input[i]\text{result} = \sum_{i=0}^{N-1} \text{input}[i]result=i=0∑N−1​input[i]

The result is a single float32\texttt{float32}float32 scalar written to the first slot of the device buffer result\texttt{result}result (a one-element buffer). Because every element contributes to the same output, the kernel must accumulate across all NNN elements using a cross-thread reduction. Do not assume a single block covers NNN: the grid may span many blocks, and partial sums from different blocks must combine into result[0]\texttt{result}[0]result[0].

Loading visualization...

Examples

Input: input = [1, 2, 3, 4, 5]

Output: 15.0

Computed as 1+2+3+4+5=151 + 2 + 3 + 4 + 5 = 151+2+3+4+5=15.

Input: input = [-1, 2, -3, 4]

Output: 2.0

Computed as −1+2+(−3)+4=2-1 + 2 + (-3) + 4 = 2−1+2+(−3)+4=2.

Hint 1

A clean pattern is a per-block tree reduction in __shared__ memory: each thread loads its element into a register (or 000 when i≥Ni \ge Ni≥N), writes the partial into shared memory, then participates in log⁡2(blockDim)\log_2(\text{blockDim})log2​(blockDim) pairwise merges under __syncthreads.

Hint 2

To merge across blocks, have thread 0 of each block call atomicAdd(result, block_sum). The launcher in the starter pre-zeros result, so accumulating into it directly is safe.

Hint 3

A pure-atomics alternative is to skip shared memory and have every thread atomicAdd(result, input[i]), but contention is much higher; prefer one atomic per block.

Requirements

  • Perform a cross-thread reduction; a single thread cannot read all of input\texttt{input}input
  • Write the final scalar into result[0]\texttt{result}[0]result[0]
  • Do not assume one block covers NNN; combine partial sums from every block
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1071 \le N \le 10^71≤N≤107
  • input\texttt{input}input and result\texttt{result}result are device pointers
  • All values are 32-bit floats
  • result\texttt{result}result refers to exactly one float\texttt{float}float slot
Try Similar Problems
Min of Array as a CUDA KernelEasyMax of Array as a CUDA KernelEasyArgmax as a CUDA KernelMediumMean and Variance as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumArgmin as a CUDA KernelMediumImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasy

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Sum of Array as a CUDA Kernel

Sum of Array as a CUDA Kernel

GPU Programming
Easy

Implement a CUDA kernel that computes the scalar sum of a 1-D float vector input\texttt{input}input of length NNN:

result=∑i=0N−1input[i]\text{result} = \sum_{i=0}^{N-1} \text{input}[i]result=i=0∑N−1​input[i]

The result is a single float32\texttt{float32}float32 scalar written to the first slot of the device buffer result\texttt{result}result (a one-element buffer). Because every element contributes to the same output, the kernel must accumulate across all NNN elements using a cross-thread reduction. Do not assume a single block covers NNN: the grid may span many blocks, and partial sums from different blocks must combine into result[0]\texttt{result}[0]result[0].

Loading visualization...

Examples

Input: input = [1, 2, 3, 4, 5]

Output: 15.0

Computed as 1+2+3+4+5=151 + 2 + 3 + 4 + 5 = 151+2+3+4+5=15.

Input: input = [-1, 2, -3, 4]

Output: 2.0

Computed as −1+2+(−3)+4=2-1 + 2 + (-3) + 4 = 2−1+2+(−3)+4=2.

Hint 1

A clean pattern is a per-block tree reduction in __shared__ memory: each thread loads its element into a register (or 000 when i≥Ni \ge Ni≥N), writes the partial into shared memory, then participates in log⁡2(blockDim)\log_2(\text{blockDim})log2​(blockDim) pairwise merges under __syncthreads.

Hint 2

To merge across blocks, have thread 0 of each block call atomicAdd(result, block_sum). The launcher in the starter pre-zeros result, so accumulating into it directly is safe.

Hint 3

A pure-atomics alternative is to skip shared memory and have every thread atomicAdd(result, input[i]), but contention is much higher; prefer one atomic per block.

Requirements

  • Perform a cross-thread reduction; a single thread cannot read all of input\texttt{input}input
  • Write the final scalar into result[0]\texttt{result}[0]result[0]
  • Do not assume one block covers NNN; combine partial sums from every block
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1071 \le N \le 10^71≤N≤107
  • input\texttt{input}input and result\texttt{result}result are device pointers
  • All values are 32-bit floats
  • result\texttt{result}result refers to exactly one float\texttt{float}float slot
Try Similar Problems
Min of Array as a CUDA KernelEasyMax of Array as a CUDA KernelEasyArgmax as a CUDA KernelMediumMean and Variance as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumArgmin as a CUDA KernelMediumImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasy

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1