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

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

result=min⁡i=0N−1input[i]\text{result} = \min_{i=0}^{N-1} \text{input}[i]result=i=0minN−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 must be considered, the kernel must reduce across all NNN elements using a cross-thread reduction. Do not assume one block covers NNN: the grid may span many blocks, and partial minima from different blocks must combine into result[0]\texttt{result}[0]result[0]. The launcher pre-fills result[0]\texttt{result}[0]result[0] with +FLT_MAX\texttt{+FLT\_MAX}+FLT_MAX, the identity for min.

Loading visualization...

Examples

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

Output: 1.0

The minimum of the eight elements is 111.

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

Output: -5.0

The smallest element is −5-5−5.

Hint 1

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

Hint 2

To merge across blocks, have thread 0 of each block atomically fold its block-local min into result[0]. Use the atomicCAS trick on a unsigned int view of the float to implement atomic min.

Hint 3

The identity for min is +∞+\infty+∞, available as FLT_MAX from <float.h>. The launcher pre-fills result[0] with FLT_MAX, so accumulating into it via atomic-min is safe.

Requirements

  • Perform a cross-thread reduction; a single thread cannot inspect 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 block-local minima across the grid
  • 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, pre-filled with +FLT_MAX\texttt{+FLT\_MAX}+FLT_MAX
Try Similar Problems
Sum 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.

Min of Array as a CUDA Kernel

Min of Array as a CUDA Kernel

GPU Programming
Easy

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

result=min⁡i=0N−1input[i]\text{result} = \min_{i=0}^{N-1} \text{input}[i]result=i=0minN−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 must be considered, the kernel must reduce across all NNN elements using a cross-thread reduction. Do not assume one block covers NNN: the grid may span many blocks, and partial minima from different blocks must combine into result[0]\texttt{result}[0]result[0]. The launcher pre-fills result[0]\texttt{result}[0]result[0] with +FLT_MAX\texttt{+FLT\_MAX}+FLT_MAX, the identity for min.

Loading visualization...

Examples

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

Output: 1.0

The minimum of the eight elements is 111.

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

Output: -5.0

The smallest element is −5-5−5.

Hint 1

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

Hint 2

To merge across blocks, have thread 0 of each block atomically fold its block-local min into result[0]. Use the atomicCAS trick on a unsigned int view of the float to implement atomic min.

Hint 3

The identity for min is +∞+\infty+∞, available as FLT_MAX from <float.h>. The launcher pre-fills result[0] with FLT_MAX, so accumulating into it via atomic-min is safe.

Requirements

  • Perform a cross-thread reduction; a single thread cannot inspect 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 block-local minima across the grid
  • 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, pre-filled with +FLT_MAX\texttt{+FLT\_MAX}+FLT_MAX
Try Similar Problems
Sum 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