From afb9951ed8e2550667bd2dfd0ebacb94e62857be Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk <21087696+oleksandr-pavlyk@users.noreply.github.com> Date: Mon, 28 Jul 2025 11:41:55 -0500 Subject: [PATCH] Enable building of NVBench as part of buildign extension 1. Download and include CPM.cmake, version 0.42.0 2. Use CPM.make to get Pybind11 3. Update to use pybind11=3.0.0 4. Also use CPM to configure/build nvbench --- python/CMakeLists.txt | 41 +++++++++++++++++++++++++++-------------- python/README.md | 39 ++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index b61da52..abfc59a 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -1,29 +1,42 @@ cmake_minimum_required(VERSION 3.30...4.0) -project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX) - -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_POSITION_INDEPENDENT_CODE ON) +# CUDA is transitive dependency of nvbench +project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX CUDA) find_package(Python REQUIRED COMPONENTS Development.Module) find_package(CUDAToolkit REQUIRED) -include(FetchContent) - -FetchContent_Declare( - pybind11 - URL https://github.com/pybind/pybind11/archive/refs/tags/v2.13.6.tar.gz - URL_HASH SHA256=e08cb87f4773da97fa7b5f035de8763abc656d87d5773e62f6da0587d1f0ec20 - FIND_PACKAGE_ARGS NAMES pybind11 +# Get CMake package manager +set(_cpm_download_location ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake) +file( + DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.42.0/CPM.cmake + ${_cpm_download_location} + EXPECTED_HASH SHA256=2020b4fc42dba44817983e06342e682ecfc3d2f484a581f11cc5731fbe4dce8a ) -FetchContent_MakeAvailable(pybind11) +include(${_cpm_download_location}) -find_package(nvbench CONFIG REQUIRED) +CPMAddPackage( + NAME nvbench + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/.. + OPTIONS "NVBench_INSTALL_RULES ON" + FIND_PACKAGE_ARGS CONFIG REQUIRED +) + +CPMAddPackage("gh:pybind/pybind11@3.0.0") pybind11_add_module(_nvbench MODULE src/py_nvbench.cpp) target_link_libraries(_nvbench PUBLIC nvbench::nvbench) target_link_libraries(_nvbench PRIVATE CUDA::cudart_static) + set_target_properties(_nvbench PROPERTIES INSTALL_RPATH "$ORIGIN") +set_target_properties(_nvbench PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON) +set_target_properties(_nvbench PROPERTIES POSITION_INDEPENDENT_CODE ON) +set_target_properties(_nvbench PROPERTIES CXX_STANDARD 20) install(TARGETS _nvbench DESTINATION cuda/nvbench) -install(IMPORTED_RUNTIME_ARTIFACTS nvbench::nvbench DESTINATION cuda/nvbench) + +# Determine target that nvbench::nvbench is an alias of, +# necessary because ALIAS targets cannot be installed +get_target_property(_aliased_target_name nvbench::nvbench ALIASED_TARGET) +install(IMPORTED_RUNTIME_ARTIFACTS ${_aliased_target_name} DESTINATION cuda/nvbench) diff --git a/python/README.md b/python/README.md index dffd70e..bd812f5 100644 --- a/python/README.md +++ b/python/README.md @@ -4,7 +4,7 @@ This package provides Python API to CUDA Kernel Benchmarking Library `NVBench`. ## Building -### Build `NVBench` project +### Ensure recent version of CMake Since `nvbench` requires a rather new version of CMake (>=3.30.4), either build CMake from sources, or create a conda environment with a recent version of CMake, using @@ -13,48 +13,49 @@ conda create -n build_env --yes cmake ninja conda activate build_env ``` +### Ensure CUDA compiler + +Since building `NVBench` library requires CUDA compiler, ensure that appropriate environment variables +are set. For example, assuming CUDA toolkit is installedsystem-wide, and assuming Ampere GPU architecture: + +```bash +export CUDACXX=/usr/local/cuda/bin/nvcc +export CUDAARCHS=86 +`` + +### Build Python project + Now switch to python folder, configure and install NVBench library, and install the package in editable mode: -``` +```bash cd nvbench/python -cmake -B nvbench_build --preset nvbench-ci -S $(pwd)/.. -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DNVBench_ENABLE_EXAMPLES=OFF -DCMAKE_INSTALL_PREFIX=$(pwd)/nvbench_install -cmake --build nvbench_build/ --config Release --target install +pip install -e . ``` -### Build Python extension - -Specify location local installation of `NVBench` library and perform editable `pip install`: - -``` -nvbench_DIR=$(pwd)/nvbench_install/lib/cmake CUDACXX=/usr/local/cuda/bin/nvcc pip install -e . -``` - -Note that `CUDACXX` must be set for NVBench cmake script to work, but Python extension itself only uses host compiler. - ### Verify that package works -``` +```bash python test/run_1.py ``` ### Run examples -``` +```bash # Example benchmarking numba.cuda kernel python examples/throughput.py ``` -``` +```bash # Example benchmarking kernels authored using cuda.core python examples/axes.py ``` -``` +```bash # Example benchmarking algorithms from cuda.cccl.parallel python examples/cccl_parallel_segmented_reduce.py ``` -``` +```bash # Example benchmarking CuPy function python examples/cupy_extract.py ```