Main function demonstrating parallel reduction.
()
| 291 | |
| 292 | |
| 293 | def main() -> bool: |
| 294 | """Main function demonstrating parallel reduction.""" |
| 295 | print("=" * 70) |
| 296 | print("Parallel Reduction - Efficient GPU Array Summation") |
| 297 | print("=" * 70) |
| 298 | |
| 299 | device = Device(0) |
| 300 | device.set_current() |
| 301 | stream = device.create_stream() |
| 302 | cp_stream = cp.cuda.Stream.from_external(stream) |
| 303 | |
| 304 | print() |
| 305 | print_gpu_info(device) |
| 306 | |
| 307 | array_size = 1 << 20 # 1M elements |
| 308 | h_input = np.random.rand(array_size).astype(np.float32) |
| 309 | expected_sum = float(np.sum(h_input)) |
| 310 | |
| 311 | print(f"\nArray size: {array_size:,} elements ({array_size * 4 / 1e6:.1f} MB)") |
| 312 | print(f"Expected sum: {expected_sum:.6f}") |
| 313 | |
| 314 | print("\nCompiling custom CUDA kernel...") |
| 315 | kernel = compile_kernel(device) |
| 316 | |
| 317 | try: |
| 318 | with cp_stream: |
| 319 | d_input = cp.asarray(h_input) |
| 320 | |
| 321 | # ====================================================================== |
| 322 | # Part 1: Custom Kernel |
| 323 | # ====================================================================== |
| 324 | print("\n" + "=" * 70) |
| 325 | print("PART 1: Custom Kernel (Educational)") |
| 326 | print("=" * 70) |
| 327 | |
| 328 | result, time_ms = benchmark_custom(stream, kernel, d_input) |
| 329 | |
| 330 | print(f"\nReduction tree kernel: {result:>14.2f}") |
| 331 | print(f"Expected: {expected_sum:>14.2f}") |
| 332 | print(f"Time: {time_ms:>14.3f} ms") |
| 333 | |
| 334 | # ====================================================================== |
| 335 | # Part 2: cuda.compute (Production) |
| 336 | # ====================================================================== |
| 337 | print("\n" + "=" * 70) |
| 338 | print("PART 2: cuda.compute.reduce_into() (Production)") |
| 339 | print("=" * 70) |
| 340 | |
| 341 | result_cc, time_cc = benchmark_cuda_compute(stream, d_input) |
| 342 | |
| 343 | print(f"\ncuda.compute result: {result_cc:>14.2f}") |
| 344 | print(f"Expected: {expected_sum:>14.2f}") |
| 345 | print(f"Time: {time_cc:>14.3f} ms") |
| 346 | |
| 347 | # Verify both results using principled rtol/atol |
| 348 | with cp_stream: |
| 349 | d_expected = cp.array([expected_sum], dtype=cp.float32) |
| 350 | custom_ok = verify_array_result( |
no test coverage detected