MCPcopy Create free account
hub / github.com/NVIDIA/cuda-samples / main

Function main

python/2_CoreConcepts/launchConfigTuning/launchConfigTuning.py:334–391  ·  view source on GitHub ↗

Complete demonstration of CUDA launch configuration tuning. This sample shows: 1. Device initialization with cuda.core.Device 2. Kernel compilation with cuda.core.Program 3. Benchmarking different thread block configurations 4. Finding optimal threads-per-block for various

()

Source from the content-addressed store, hash-verified

332
333
334def main():
335 """
336 Complete demonstration of CUDA launch configuration tuning.
337
338 This sample shows:
339 1. Device initialization with cuda.core.Device
340 2. Kernel compilation with cuda.core.Program
341 3. Benchmarking different thread block configurations
342 4. Finding optimal threads-per-block for various kernel types
343 """
344 print("=" * 60)
345 print("Launch Configuration Tuning (cuda.core)")
346 print("Finding the Best Block Size for Your Kernel")
347 print("=" * 60)
348
349 # Initialize CUDA device
350 device = Device(0)
351 device.set_current()
352
353 # This sample builds host NumPy views over managed memory. Devices without
354 # concurrent managed access keep managed allocations GPU-exclusive while
355 # the GPU is active, so those host views fault.
356 if not device.properties.concurrent_managed_access:
357 print("Concurrent managed memory access is not supported on this platform.")
358 sys.exit(2)
359
360 # Print GPU information
361 print_gpu_info(device)
362
363 # Create stream and device-preferred memory resource
364 stream = device.create_stream()
365 mr_options = ManagedMemoryResourceOptions(preferred_location=device.device_id)
366 mr = ManagedMemoryResource(mr_options)
367
368 try:
369 # Compile kernels
370 print("\nCompiling CUDA kernels with cuda.core.Program...")
371 arch = f"sm_{device.arch}"
372 print(f" Target architecture: {arch}")
373
374 vec_add_kernel = compile_kernel(device, VECTOR_ADD_KERNEL, "vector_add")
375 print(" [OK] vector_add kernel compiled")
376
377 reduction_kernel = compile_kernel(device, REDUCTION_KERNEL, "reduce_sum")
378 print(" [OK] reduce_sum kernel compiled")
379
380 # Run demonstrations
381 demo_vector_add_tuning(device, stream, mr, vec_add_kernel)
382 demo_reduction_tuning(device, stream, mr, reduction_kernel)
383
384 print("\n" + "=" * 60)
385 print("SAMPLE COMPLETE")
386 print("=" * 60)
387 print("\nKey Takeaway: The optimal thread configuration depends on your")
388 print("specific kernel characteristics. Always benchmark to find the best!")
389 print()
390 finally:
391 stream.close()

Callers 1

Calls 4

demo_vector_add_tuningFunction · 0.85
demo_reduction_tuningFunction · 0.85
print_gpu_infoFunction · 0.70
compile_kernelFunction · 0.70

Tested by

no test coverage detected