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

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

result=max⁡i=0N−1input[i]\text{result} = \max_{i=0}^{N-1} \text{input}[i]result=i=0maxN−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). The kernel must perform a cross-thread reduction with the max⁡\maxmax combiner: a single thread cannot scan all of input\texttt{input}input. Because the grid may span many blocks, partial maxima from different blocks must combine into result[0]\texttt{result}[0]result[0].

Loading visualization...

Examples

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

Output: 9.0

The maximum element is 999 at index 555.

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

Output: -1.0

All values are negative; the largest (least-negative) is −1-1−1.

Hint 1

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

Hint 2

For the cross-block combine, write each block's partial maximum to a scratch buffer of size numBlocks, then launch a second kernel that reduces that scratch buffer to result[0].

Hint 3

Pre-fill result[0] (and any scratch slots that may not be written) with -FLT_MAX, since it is the identity for fmaxf. Include <float.h> for the constant.

Requirements

  • Perform a cross-thread reduction with the max⁡\maxmax combiner; 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 maxima 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 KernelEasySum 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.

Max of Array as a CUDA Kernel

Max of Array as a CUDA Kernel

GPU Programming
Easy

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

result=max⁡i=0N−1input[i]\text{result} = \max_{i=0}^{N-1} \text{input}[i]result=i=0maxN−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). The kernel must perform a cross-thread reduction with the max⁡\maxmax combiner: a single thread cannot scan all of input\texttt{input}input. Because the grid may span many blocks, partial maxima from different blocks must combine into result[0]\texttt{result}[0]result[0].

Loading visualization...

Examples

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

Output: 9.0

The maximum element is 999 at index 555.

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

Output: -1.0

All values are negative; the largest (least-negative) is −1-1−1.

Hint 1

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

Hint 2

For the cross-block combine, write each block's partial maximum to a scratch buffer of size numBlocks, then launch a second kernel that reduces that scratch buffer to result[0].

Hint 3

Pre-fill result[0] (and any scratch slots that may not be written) with -FLT_MAX, since it is the identity for fmaxf. Include <float.h> for the constant.

Requirements

  • Perform a cross-thread reduction with the max⁡\maxmax combiner; 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 maxima 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 KernelEasySum 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