CUDA Elementwise Maximum
Problem Statement
Compute the elementwise maximum of two 1D arrays: out[i] = max(a[i], b[i]).
Background
A two-input elementwise kernel: load one value from each array and keep the larger. This is the pattern behind np.maximum and elementwise gating.
Your Task
Implement emax_kernel and run(n=1024) comparing to np.maximum(a, b).
How it is tested
Your solution must define a top-level function run(...) that allocates the inputs, copies them to the GPU, launches your @cuda.jit kernel, and returns a Python bool from np.allclose(gpu_result, reference). The grader prints run(...); the expected output is True.
Example:
n = 1024
True
- The input
n = 1024determines the size of the 1D arraysaandb. - The
emax_kernelfunction is launched, computing the elementwise maximum ofaandbusing the formula out[i]=max(a[i],b[i]). - The result from the GPU is compared to the reference result from
np.maximum(a, b)usingnp.allclose. - The comparison returns
Trueif the two results are close enough, indicating that the GPU computation was correct, resulting in the outputTrue.
Constraints:
- out[i] = a[i] if a[i] > b[i] else b[i]
- Two input arrays, one output
- Bounds-check the global index
Background Knowledge
The problem involves computing the elementwise maximum of two 1D arrays using CUDA, a parallel computing platform developed by NVIDIA. To understand this problem, it's essential to have a basic knowledge of parallel computing and GPU architecture. In traditional CPU-based computing, tasks are executed sequentially, which can lead to performance bottlenecks for large datasets. In contrast, GPUs have thousands of cores that can perform tasks concurrently, making them ideal for data-parallel computations.
The concept of elementwise operations is crucial in this problem. Elementwise operations involve performing a specific operation on corresponding elements of two or more arrays. In this case, we need to compute the maximum of corresponding elements from two input arrays a and b. This operation is a fundamental building block in many scientific computing and machine learning applications. The NumPy library provides an efficient implementation of elementwise operations, including np.maximum, which serves as a reference solution for this problem.
To implement the emax_kernel function, we need to understand the basics of CUDA kernel programming. A CUDA kernel is a small program that runs on the GPU, executing a specific task in parallel across multiple threads. The kernel function will be launched with a specified number of threads, each responsible for computing a subset of the output array. The CUDA jit decorator (@cuda.jit) is used to compile the kernel function just-in-time, allowing for efficient execution on the GPU.
Algorithm/Approach
The general approach to solving this problem involves:
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.