Parse CLI, call ``run()``, and exit with validation status.
()
| 223 | |
| 224 | |
| 225 | def main(): |
| 226 | """Parse CLI, call ``run()``, and exit with validation status.""" |
| 227 | parser = argparse.ArgumentParser( |
| 228 | description="Demonstrate zero-copy memory access with CUDA", |
| 229 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 230 | epilog=""" |
| 231 | Examples: |
| 232 | python simpleZeroCopy.py |
| 233 | python simpleZeroCopy.py --num_elements 2097152 |
| 234 | What is Zero-Copy Memory? |
| 235 | Zero-copy allows the GPU to directly access host (CPU) memory without |
| 236 | explicit memory transfers. This is useful for: |
| 237 | - Small data that doesn't benefit from explicit transfers |
| 238 | - Data that is accessed infrequently |
| 239 | - Integrated GPUs that share memory with CPU |
| 240 | |
| 241 | Trade-offs: |
| 242 | - Slower than device memory (PCIe bandwidth limited) |
| 243 | - No explicit transfers needed (simpler code) |
| 244 | - Good for discrete GPUs with small data |
| 245 | - Excellent for integrated GPUs (e.g., Tegra) |
| 246 | """, |
| 247 | ) |
| 248 | |
| 249 | parser.add_argument( |
| 250 | "--num_elements", |
| 251 | type=int, |
| 252 | default=1048576, |
| 253 | help="Number of elements in vectors (default: 1048576)", |
| 254 | ) |
| 255 | |
| 256 | args = parser.parse_args() |
| 257 | |
| 258 | if args.num_elements <= 0: |
| 259 | print("Error: num_elements must be positive") |
| 260 | sys.exit(1) |
| 261 | |
| 262 | try: |
| 263 | exit_code = run(num_elements=args.num_elements) |
| 264 | except Exception as e: |
| 265 | print(f"\nError: {e}") |
| 266 | import traceback |
| 267 | |
| 268 | traceback.print_exc() |
| 269 | exit_code = 1 |
| 270 | |
| 271 | sys.exit(exit_code) |
| 272 | |
| 273 | |
| 274 | if __name__ == "__main__": |