|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import atexit |
| 6 | +import os |
| 7 | +import site |
| 8 | +import tempfile |
| 9 | +import time |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from cuda.pathfinder import find_nvidia_binary_utility, find_nvidia_header_directory, locate_static_lib |
| 13 | +from cuda.pathfinder._headers.find_nvidia_headers import locate_nvidia_header_directory |
| 14 | +from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_cached |
| 15 | +from cuda.pathfinder._utils.platform_aware import IS_WINDOWS |
| 16 | + |
| 17 | +_TEMP_DIR = tempfile.TemporaryDirectory() |
| 18 | +_ROOT = Path(_TEMP_DIR.name) |
| 19 | +_SITE_ROOTS = tuple(_ROOT / f"site-root-{index}" for index in range(3)) |
| 20 | +for root in _SITE_ROOTS: |
| 21 | + root.mkdir() |
| 22 | + |
| 23 | +_PACKAGE_ROOT = _SITE_ROOTS[-1] / "nvidia" |
| 24 | +_HEADER_DIR = _PACKAGE_ROOT / "cuda_runtime" / "include" |
| 25 | +_HEADER_DIR.mkdir(parents=True) |
| 26 | +(_HEADER_DIR / "cuda_runtime.h").touch() |
| 27 | + |
| 28 | +_BINARY_DIR = _PACKAGE_ROOT / "cuda_nvcc" / "bin" |
| 29 | +_BINARY_DIR.mkdir(parents=True) |
| 30 | +_BINARY_NAME = "nvdisasm.exe" if IS_WINDOWS else "nvdisasm" |
| 31 | +_BINARY_PATH = _BINARY_DIR / _BINARY_NAME |
| 32 | +_BINARY_PATH.touch() |
| 33 | +if not IS_WINDOWS: |
| 34 | + _BINARY_PATH.chmod(0o755) |
| 35 | + |
| 36 | +_STATIC_LIB_DIR = _PACKAGE_ROOT / "cuda_runtime" / "lib" |
| 37 | +if IS_WINDOWS: |
| 38 | + _STATIC_LIB_DIR /= "x64" |
| 39 | +_STATIC_LIB_DIR.mkdir(parents=True) |
| 40 | +_STATIC_LIB_NAME = "cudadevrt.lib" if IS_WINDOWS else "libcudadevrt.a" |
| 41 | +(_STATIC_LIB_DIR / _STATIC_LIB_NAME).touch() |
| 42 | + |
| 43 | +_ORIGINAL_GETSITEPACKAGES = site.getsitepackages |
| 44 | +_ORIGINAL_ENABLE_USER_SITE = site.ENABLE_USER_SITE |
| 45 | +_ACTIVE_SITE_ROOTS: tuple[Path, ...] = _SITE_ROOTS |
| 46 | + |
| 47 | + |
| 48 | +def _getsitepackages() -> list[str]: |
| 49 | + return [str(root) for root in _ACTIVE_SITE_ROOTS] |
| 50 | + |
| 51 | + |
| 52 | +site.getsitepackages = _getsitepackages |
| 53 | +site.ENABLE_USER_SITE = False |
| 54 | + |
| 55 | + |
| 56 | +def _clear_discovery_caches() -> None: |
| 57 | + find_sub_dirs_cached.cache_clear() |
| 58 | + find_nvidia_binary_utility.cache_clear() |
| 59 | + locate_nvidia_header_directory.cache_clear() |
| 60 | + |
| 61 | + |
| 62 | +def _cleanup() -> None: |
| 63 | + site.getsitepackages = _ORIGINAL_GETSITEPACKAGES |
| 64 | + site.ENABLE_USER_SITE = _ORIGINAL_ENABLE_USER_SITE |
| 65 | + _TEMP_DIR.cleanup() |
| 66 | + |
| 67 | + |
| 68 | +def bench_header_cold_1_root(loops: int) -> float: |
| 69 | + global _ACTIVE_SITE_ROOTS |
| 70 | + _ACTIVE_SITE_ROOTS = _SITE_ROOTS[-1:] |
| 71 | + _fn = find_nvidia_header_directory |
| 72 | + _clear = _clear_discovery_caches |
| 73 | + |
| 74 | + t0 = time.perf_counter() |
| 75 | + for _ in range(loops): |
| 76 | + _clear() |
| 77 | + _fn("cudart") |
| 78 | + return time.perf_counter() - t0 |
| 79 | + |
| 80 | + |
| 81 | +def bench_header_cold_3_roots(loops: int) -> float: |
| 82 | + global _ACTIVE_SITE_ROOTS |
| 83 | + _ACTIVE_SITE_ROOTS = _SITE_ROOTS |
| 84 | + _fn = find_nvidia_header_directory |
| 85 | + _clear = _clear_discovery_caches |
| 86 | + |
| 87 | + t0 = time.perf_counter() |
| 88 | + for _ in range(loops): |
| 89 | + _clear() |
| 90 | + _fn("cudart") |
| 91 | + return time.perf_counter() - t0 |
| 92 | + |
| 93 | + |
| 94 | +def bench_binary_cold_1_root(loops: int) -> float: |
| 95 | + global _ACTIVE_SITE_ROOTS |
| 96 | + _ACTIVE_SITE_ROOTS = _SITE_ROOTS[-1:] |
| 97 | + _fn = find_nvidia_binary_utility |
| 98 | + _clear = _clear_discovery_caches |
| 99 | + |
| 100 | + t0 = time.perf_counter() |
| 101 | + for _ in range(loops): |
| 102 | + _clear() |
| 103 | + _fn("nvdisasm") |
| 104 | + return time.perf_counter() - t0 |
| 105 | + |
| 106 | + |
| 107 | +def bench_binary_cold_3_roots(loops: int) -> float: |
| 108 | + global _ACTIVE_SITE_ROOTS |
| 109 | + _ACTIVE_SITE_ROOTS = _SITE_ROOTS |
| 110 | + _fn = find_nvidia_binary_utility |
| 111 | + _clear = _clear_discovery_caches |
| 112 | + |
| 113 | + t0 = time.perf_counter() |
| 114 | + for _ in range(loops): |
| 115 | + _clear() |
| 116 | + _fn("nvdisasm") |
| 117 | + return time.perf_counter() - t0 |
| 118 | + |
| 119 | + |
| 120 | +def bench_static_lib_cold_3_roots(loops: int) -> float: |
| 121 | + global _ACTIVE_SITE_ROOTS |
| 122 | + _ACTIVE_SITE_ROOTS = _SITE_ROOTS |
| 123 | + _fn = locate_static_lib |
| 124 | + _clear = find_sub_dirs_cached.cache_clear |
| 125 | + |
| 126 | + t0 = time.perf_counter() |
| 127 | + for _ in range(loops): |
| 128 | + _clear() |
| 129 | + _fn("cudadevrt") |
| 130 | + return time.perf_counter() - t0 |
| 131 | + |
| 132 | + |
| 133 | +_clear_discovery_caches() |
| 134 | +assert find_nvidia_header_directory("cudart") == os.path.normpath(os.path.abspath(_HEADER_DIR)) |
| 135 | +assert find_nvidia_binary_utility("nvdisasm") == os.path.abspath(_BINARY_PATH) |
| 136 | +assert locate_static_lib("cudadevrt").abs_path == os.path.join(os.path.abspath(_STATIC_LIB_DIR), _STATIC_LIB_NAME) |
| 137 | +_clear_discovery_caches() |
| 138 | + |
| 139 | +atexit.register(_cleanup) |
0 commit comments