TensorTonic
Problems
Argmax as a CUDA Kernel
GPU Programming
Medium

Implement a CUDA kernel that returns the index of the maximum element of a 1-D float vector input\texttt{input}input of length NNN:

result=arg⁡max⁡i∈[0,N)input[i]\text{result} = \arg\max_{i \in [0, N)} \text{input}[i]result=argi∈[0,N)max​input[i]

The result is a single int32\texttt{int32}int32 value written to slot 000 of the one-element device buffer result\texttt{result}result. When two or more elements share the maximum value, the lowest index wins (this matches the default PyTorch torch.argmax\texttt{torch.argmax}torch.argmax tie-break).

Because the answer depends on every element, the kernel must reduce across all NNN entries. The grid can span many blocks, so per-block partial answers must combine into the single integer at result[0]\texttt{result}[0]result[0].

Loading visualization...

Examples

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

Output: 5

The maximum value is 999 at index 555.

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

Output: 1

The largest value is −1-1−1 at index 111.

Hint 1

Carry a (value,index)(\text{value}, \text{index})(value,index) pair through the reduction rather than just the value. The combine rule is: keep the pair with the larger value; if values tie, keep the pair with the smaller index.

Hint 2

Use two __shared__ arrays per block, one for values and one for indices. After the per-block tree reduce, write the block's best pair into two global scratch arrays sized by the number of blocks.

Hint 3

Launch a second kernel with one block to reduce the per-block scratch arrays into the final pair, then write only the index to result[0].

Requirements

  • Reduce (value,index)(\text{value}, \text{index})(value,index) pairs; a single thread cannot scan all of input\texttt{input}input
  • Resolve ties by keeping the lowest index among equal maxima
  • Write the final integer index into result[0]\texttt{result}[0]result[0] as int32\texttt{int32}int32
  • 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
  • input\texttt{input}input holds 32-bit floats; result\texttt{result}result holds one int32\texttt{int32}int32 slot
  • Tie-break rule: smallest index wins
Try Similar Problems
Mean and Variance as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumArgmin as a CUDA KernelMediumMin of Array as a CUDA KernelEasySum of Array as a CUDA KernelEasyMax of Array as a CUDA KernelEasyRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumSoftmax as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMedium

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Argmax as a CUDA Kernel

Argmax as a CUDA Kernel

GPU Programming
Medium

Implement a CUDA kernel that returns the index of the maximum element of a 1-D float vector input\texttt{input}input of length NNN:

result=arg⁡max⁡i∈[0,N)input[i]\text{result} = \arg\max_{i \in [0, N)} \text{input}[i]result=argi∈[0,N)max​input[i]

The result is a single int32\texttt{int32}int32 value written to slot 000 of the one-element device buffer result\texttt{result}result. When two or more elements share the maximum value, the lowest index wins (this matches the default PyTorch torch.argmax\texttt{torch.argmax}torch.argmax tie-break).

Because the answer depends on every element, the kernel must reduce across all NNN entries. The grid can span many blocks, so per-block partial answers must combine into the single integer at result[0]\texttt{result}[0]result[0].

Loading visualization...

Examples

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

Output: 5

The maximum value is 999 at index 555.

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

Output: 1

The largest value is −1-1−1 at index 111.

Hint 1

Carry a (value,index)(\text{value}, \text{index})(value,index) pair through the reduction rather than just the value. The combine rule is: keep the pair with the larger value; if values tie, keep the pair with the smaller index.

Hint 2

Use two __shared__ arrays per block, one for values and one for indices. After the per-block tree reduce, write the block's best pair into two global scratch arrays sized by the number of blocks.

Hint 3

Launch a second kernel with one block to reduce the per-block scratch arrays into the final pair, then write only the index to result[0].

Requirements

  • Reduce (value,index)(\text{value}, \text{index})(value,index) pairs; a single thread cannot scan all of input\texttt{input}input
  • Resolve ties by keeping the lowest index among equal maxima
  • Write the final integer index into result[0]\texttt{result}[0]result[0] as int32\texttt{int32}int32
  • 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
  • input\texttt{input}input holds 32-bit floats; result\texttt{result}result holds one int32\texttt{int32}int32 slot
  • Tie-break rule: smallest index wins
Try Similar Problems
Mean and Variance as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumArgmin as a CUDA KernelMediumMin of Array as a CUDA KernelEasySum of Array as a CUDA KernelEasyMax of Array as a CUDA KernelEasyRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumSoftmax as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMedium

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1