Main function demonstrating parallel reduction.
()
| 291 | |
| 292 | |
| 293 | def main() -> int: |
| 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 | |
| 302 | # cuda.compute allocates temporary storage from the device's default memory |
| 303 | # pool, which requires CUDA memory-pool support. This is not available on |
| 304 | # every platform (for example, Windows in TCC mode). |
| 305 | if not device.properties.memory_pools_supported: |
| 306 | print("CUDA memory pools are not supported on this platform.") |
| 307 | return 2 |
| 308 | |
| 309 | stream = device.create_stream() |
| 310 | cp_stream = cp.cuda.Stream.from_external(stream) |
| 311 | |
| 312 | print() |
| 313 | print_gpu_info(device) |
| 314 | |
| 315 | array_size = 1 << 20 # 1M elements |
| 316 | h_input = np.random.rand(array_size).astype(np.float32) |
| 317 | expected_sum = float(np.sum(h_input)) |
| 318 | |
| 319 | print(f"\nArray size: {array_size:,} elements ({array_size * 4 / 1e6:.1f} MB)") |
| 320 | print(f"Expected sum: {expected_sum:.6f}") |
| 321 | |
| 322 | print("\nCompiling custom CUDA kernel...") |
| 323 | kernel = compile_kernel(device) |
| 324 | |
| 325 | try: |
| 326 | with cp_stream: |
| 327 | d_input = cp.asarray(h_input) |
| 328 | |
| 329 | # ====================================================================== |
| 330 | # Part 1: Custom Kernel |
| 331 | # ====================================================================== |
| 332 | print("\n" + "=" * 70) |
| 333 | print("PART 1: Custom Kernel (Educational)") |
| 334 | print("=" * 70) |
| 335 | |
| 336 | result, time_ms = benchmark_custom(stream, kernel, d_input) |
| 337 | |
| 338 | print(f"\nReduction tree kernel: {result:>14.2f}") |
| 339 | print(f"Expected: {expected_sum:>14.2f}") |
| 340 | print(f"Time: {time_ms:>14.3f} ms") |
| 341 | |
| 342 | # ====================================================================== |
| 343 | # Part 2: cuda.compute (Production) |
| 344 | # ====================================================================== |
| 345 | print("\n" + "=" * 70) |
| 346 | print("PART 2: cuda.compute.reduce_into() (Production)") |
| 347 | print("=" * 70) |
| 348 | |
| 349 | result_cc, time_cc = benchmark_cuda_compute(stream, d_input) |
| 350 |
no test coverage detected