Skip to content

Commit

Permalink
Regards #681: Added a fatbin builder based on NVIDIA's libnvfatbin an…
Browse files Browse the repository at this point in the history
…d bumped version to 0.8.0; but...

* not tested it yet
* need to add the nvFatbin documentation example program, rewritten to use the APIs
  • Loading branch information
eyalroz committed Sep 23, 2024
1 parent c3c8f30 commit 90299ca
Show file tree
Hide file tree
Showing 14 changed files with 755 additions and 31 deletions.
40 changes: 23 additions & 17 deletions .github/action-scripts/install-cuda-ubuntu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,10 @@
#


## -------------------
## Constants
## -------------------

# @todo - apt repos/known supported versions?

# @todo - GCC support matrix?

# List of sub-packages to install.
# @todo - pass this in from outside the script?
# @todo - check the specified subpackages exist via apt pre-install? apt-rdepends cuda-9-0 | grep "^cuda-"?

# Ideally choose from the list of meta-packages to minimise variance between cuda versions (although it does change too)
CUDA_PACKAGES_IN=(
"command-line-tools"
"nvrtc-dev"
"cudart-dev"
"nvcc"
"profiler-api"
)

## -------------------
## Bash functions
## -------------------
Expand All @@ -56,6 +39,7 @@ function version_lt() {
[ "$1" = "$2" ] && return 1 || version_le $1 $2
}


## -------------------
## Select CUDA version
## -------------------
Expand Down Expand Up @@ -93,6 +77,27 @@ if [ -z ${UBUNTU_VERSION} ]; then
exit 1
fi

## -------------------
## List of sub-packages to install
## -------------------

# @todo: Use pairs of package name and minimum version (or version range)

# Ideally choose from the list of meta-packages to minimise variance between cuda versions (although it does change too)
CUDA_PACKAGES_IN=(
"command-line-tools"
"nvrtc-dev"
"cudart-dev"
"nvcc"
"profiler-api"
)

if version_ge "$CUDA_VERSION_MAJOR_MINOR" "12.4"; then
CUDA_PACKAGES_IN+=(
"libnvfatbin"
"libnvfatbin-dev"
)
fi

## ---------------------------
## GCC studio support check?
Expand All @@ -105,6 +110,7 @@ fi
## -------------------------------

CUDA_PACKAGES=""

for package in "${CUDA_PACKAGES_IN[@]}"
do :
if [[ "${package}" == "profiler-api" ]] && version_lt "$CUDA_VERSION_MAJOR_MINOR" "11.8" ; then
Expand Down
9 changes: 3 additions & 6 deletions .github/action-scripts/install-cuda-windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,9 @@ $VISUAL_STUDIO_YEAR = $VISUAL_STUDIO.Substring($VISUAL_STUDIO.Length-4)

$CUDA_PACKAGES = ""

# for CUDA >= 11 cudart is a required package.
# if([version]$CUDA_VERSION_FULL -ge [version]"11.0") {
# if(-not $CUDA_PACKAGES_IN -contains "cudart") {
# $CUDA_PACKAGES_IN += 'cudart'
# }
# }
if([version]$CUDA_VERSION_FULL -ge [version]"12.4") {
$CUDA_PACKAGES_IN += "nvfatbin"
}

Foreach ($package in $CUDA_PACKAGES_IN) {
# Make sure the correct package name is used for nvcc.
Expand Down
30 changes: 24 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
cmake_minimum_required(VERSION 3.25 FATAL_ERROR)

PROJECT(cuda-api-wrappers
VERSION 0.7.2
VERSION 0.8.0
DESCRIPTION "Thin C++-flavored wrappers for the CUDA Runtime API"
HOMEPAGE_URL https://github.com/eyalroz/cuda-api-wrappers
)
Expand All @@ -25,8 +25,8 @@ if(libm_exists)
set(c_math_lib m)
endif()

if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.1)
foreach(tgt in nvptxcompiler nvptxcompiler_static)
if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.4)
foreach(tgt in nvfatbin nvfatbin_static)
if (NOT TARGET ${tgt})
_CUDAToolkit_find_and_add_import_lib(${tgt})
endif()
Expand All @@ -35,7 +35,7 @@ endif()
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)


set(targets runtime-and-driver nvtx rtc)
set(targets runtime-and-driver nvtx rtc fatbin)
set(prefixed-targets "")
set(caw_namespace "cuda-api-wrappers")

Expand Down Expand Up @@ -79,13 +79,29 @@ add_library("${caw_namespace}::driver-and-runtime" ALIAS caw_runtime-and-driver)
target_link_libraries(caw_rtc INTERFACE cuda-api-wrappers::runtime-and-driver CUDA::nvrtc)
if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.1)
if (TARGET CUDA::nvptxcompiler)
target_link_libraries(caw_rtc INTERFACE CUDA::nvptxcompiler)
set(ptx_compiler_target nvptxcompiler)
elseif (TARGET CUDA::nvptxcompiler_static)
target_link_libraries(caw_rtc INTERFACE CUDA::nvptxcompiler_static)
else()
message(WARNING "No valid NVIDIA PTX Compiler target is available")
endif()
endif()
if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.4)
if (TARGET CUDA::nvfatbin)
target_link_libraries(caw_fatbin INTERFACE CUDA::nvfatbin)
elseif (TARGET CUDA::nvfatbin)
target_link_libraries(caw_fatbin INTERFACE CUDA::nvfatbin_static)
elseif(EXISTS "${CUDA_nvfatbin_LIBRARY}")
target_link_libraries(caw_fatbin INTERFACE "${CUDA_nvfatbin_LIBRARY}")
elseif(EXISTS "${CUDA_nvfatbin_static_LIBRARY}")
target_link_libraries(caw_fatbin INTERFACE "${CUDA_nvfatbin_static_LIBRARY}")
else()
set(ptx_compiler_target nvptxcompiler_static)
message(WARNING "Could not locate a valid NVIDIA fatbin creator target or library file")
endif()
target_link_libraries(caw_rtc INTERFACE CUDA::${ptx_compiler_target})
endif()

target_link_libraries(caw_fatbin INTERFACE cuda-api-wrappers::runtime-and-driver)
target_link_libraries(caw_nvtx INTERFACE cuda-api-wrappers::runtime-and-driver)

if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 10.0)
Expand All @@ -99,8 +115,10 @@ endif()
# but for two settings I won't bother creating one of those
if(DEFINED CMAKE_USE_PTHREADS_INIT)
target_compile_definitions(caw_nvtx INTERFACE "" "CUDA_API_WRAPPERS_USE_PTHREADS")
target_compile_definitions(caw_fatbin INTERFACE "" "CUDA_API_WRAPPERS_USE_PTHREADS")
elseif(DEFINED CMAKE_USE_WIN32_THREADS_INIT)
target_compile_definitions(caw_nvtx INTERFACE "" "CUDA_API_WRAPPERS_USE_WIN32_THREADS")
target_compile_definitions(caw_fatbin INTERFACE "" "CUDA_API_WRAPPERS_USE_WIN32_THREADS")
endif()

# --------
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Thin C++-flavored wrappers for the CUDA APIs:<br> Runtime, Driver, NVRTC, PTX compiler and NVTX
# Thin C++-flavored wrappers for the CUDA APIs:<br> Runtime, Driver, NVRTC and more

<!--Branch Build Status: Master [![Master Build Status](https://api.travis-ci.com/eyalroz/cuda-api-wrappers.svg?branch=master)](https://travis-ci.com/eyalroz/cuda-api-wrappers) | Development: [![Development Build Status](https://api.travis-ci.com/eyalroz/cuda-api-wrappers.svg?branch=development)](https://travis-ci.com/eyalroz/cuda-api-wrappers) -->

Expand All @@ -16,6 +16,7 @@ This is a header-only library of integrated wrappers around the core parts of NV
* The slightly higher-level CUDA [Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
* NVIDIA's dynamic CUDA code compilation library, [NVRTC](http://docs.nvidia.com/cuda/nvrtc/index.html)
* NVIDIA's out-of-driver, full-featured [PTX compiler library](https://docs.nvidia.com/cuda/ptx-compiler-api/index.html) (available since CUDA 11.1)
* NVIDIA's fat binary creation library [nvFatbin](https://docs.nvidia.com/cuda/nvfatbin/index.html) (available since CUDA 12.4)
* The NVIDIA profiler in-program API, also known as [NVTX](https://docs.nvidia.com/cuda/profiler-users-guide/index.html#nvtx) (the NVIDIA Toolkit Extensions library).

It is intended for those who would otherwise use these APIs directly, to make working with them be more intuitive and consistent, making use of modern C++ language capabilities, programming idioms and best practices. In a nutshell - making CUDA API work more fun :-)
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ if( NOT MSVC )
endif()
set(tgt "cpp_${std_version}")
add_executable(${tgt} other/new_cpp_standard/main.cpp)
target_link_libraries(${tgt} cuda-api-wrappers::rtc cuda-api-wrappers::nvtx)
target_link_libraries(${tgt} cuda-api-wrappers::rtc cuda-api-wrappers::nvtx cuda-api-wrappers::fatbin)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
target_link_libraries(${tgt} stdc++fs)
endif()
Expand Down
6 changes: 6 additions & 0 deletions examples/other/new_cpp_standard/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <cuda/nvtx.hpp>
#endif
#include <cuda/rtc.hpp>
#include <cuda/fatbin.hpp>

#include <cstdlib>
#include <iostream>
Expand Down Expand Up @@ -42,6 +43,11 @@ int main()
auto nvrtc_version = cuda::version_numbers::nvrtc();
(void) nvrtc_version;

#if CUDA_VERSION >= 12040
auto fatbin_version = cuda::version_numbers::fatbin();
(void) fatbin_version;
#endif

#ifndef _MSC_VER
auto nvtx_color_yellow = cuda::profiling::color_t::from_hex(0x0FFFF00);
(void) nvtx_color_yellow;
Expand Down
5 changes: 5 additions & 0 deletions src/cuda/api/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#ifndef CUDA_API_WRAPPERS_COMMON_TYPES_HPP_
#define CUDA_API_WRAPPERS_COMMON_TYPES_HPP_

#ifdef _MSC_VER
// See @url https://stackoverflow.com/q/4913922/1593077
#define NOMINMAX
#endif

#if (__cplusplus < 201103L && (!defined(_MSVC_LANG) || _MSVC_LANG < 201103L))
#error "The CUDA API headers can only be compiled with C++11 or a later version of the C++ language standard"
#endif
Expand Down
24 changes: 24 additions & 0 deletions src/cuda/api/versions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@

#include "error.hpp"

#if CUDA_VERSION >= 12040
#include <nvFatbin.h>
#endif

#include <ostream>
#include <utility>
#include <limits>


namespace cuda {

Expand Down Expand Up @@ -161,6 +167,24 @@ inline version_t runtime() {
return version_t::from_single_number(version);
}

#if CUDA_VERSION >= 12040

inline version_t fatbin() {
unsigned int major { 0 }, minor { 0 };

auto status = nvFatbinVersion(&major, &minor);
throw_if_error_lazy(status, "Failed obtaining the nvfatbin library version");
#ifndef NDEBUG
if ((major == 0) or (major > ::std::numeric_limits<int>::max())
or (minor == 0) or (minor > ::std::numeric_limits<int>::max())) {
throw ::std::logic_error("Invalid version encountered: ("
+ ::std::to_string(major) + ", " + ::std::to_string(minor) + ')' );
}
#endif
return version_t{ static_cast<int>(major), static_cast<int>(minor) };
}
#endif // CUDA_VERSION >= 12040

} // namespace version_numbers
} // namespace cuda

Expand Down
20 changes: 20 additions & 0 deletions src/cuda/fatbin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file
*
* @brief A single file which includes, in turn, the CUDA
* fatbin creator library API wrappers and related headers.
*/
#pragma once
#ifndef CUDA_FATBIN_WRAPPERS_HPP_
#define CUDA_FATBIN_WRAPPERS_HPP_

#if CUDA_VERSION >= 12040

#include "fatbin/types.hpp"
#include "fatbin/error.hpp"
#include "fatbin/builder_options.hpp"
#include "fatbin/builder.hpp"

#endif // CUDA_VERSION >= 12040

#endif // CUDA_FATBIN_WRAPPERS_HPP_
Loading

0 comments on commit 90299ca

Please sign in to comment.