Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,26 @@ if(MSVC)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Available configuration types to select")
endif()

option(GPU "GPU" ON)
if(GPU)
option(GPU "Build with CUDA GPU support" ON)
option(HIP "Build with HIP GPU support (AMD ROCm or NVIDIA via HIP)" OFF)

if(GPU AND HIP)
message(FATAL_ERROR "GPU (CUDA) and HIP options are mutually exclusive. Choose one backend.")
endif()

if(HIP)
if(NOT DEFINED CMAKE_HIP_ARCHITECTURES)
set(CMAKE_HIP_ARCHITECTURES native CACHE STRING "Targeted HIP architectures")
endif()
project(cuda-battery
HOMEPAGE_URL "https://github.com/lattice-land/cuda-battery"
LANGUAGES HIP CXX)
# find_package(hip) provides hip::host which handles HIP runtime linking on both
# AMD (amdhip64) and NVIDIA (wraps cudart). CUDA::cuda_driver is needed on NVIDIA
# because hipDeviceGetAttribute routes through the CUDA driver API (libcuda.so).
find_package(hip REQUIRED)
find_package(CUDAToolkit QUIET)
elseif(GPU)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES native CACHE STRING "Targeted CUDA architectures")
endif()
Expand Down Expand Up @@ -41,12 +59,28 @@ target_compile_options(cuda_battery INTERFACE
"$<$<CXX_COMPILER_ID:GNU,Clang>:-frounding-math>"
$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/fp:strict>
"$<$<AND:$<COMPILE_LANGUAGE:CUDA>,$<CUDA_COMPILER_ID:NVIDIA>,$<CXX_COMPILER_ID:MSVC>>:SHELL:--compiler-options /fp:strict>"
# HIP compile options (hipcc on AMD ROCm or NVIDIA via HIP_PLATFORM=nvidia)
"$<$<COMPILE_LANGUAGE:HIP>:--expt-relaxed-constexpr>"
"$<$<AND:$<COMPILE_LANGUAGE:HIP>,$<CONFIG:Debug>>:-ggdb>"
)

if(REDUCE_PTX_SIZE)
target_compile_definitions(cuda_battery INTERFACE "$<$<COMPILE_LANGUAGE:CUDA>:REDUCE_PTX_SIZE>")
endif()

# When building with HIP=ON, link hip::host so both AMD and NVIDIA HIP builds get
# the correct HIP runtime libraries through a single CMake target.
if(HIP)
target_link_libraries(cuda_battery INTERFACE hip::host)
endif()

# When building with HIP=ON (either AMD or NVIDIA via HIP), expose BATTERY_HIP_BUILD
# so that utility.hpp makes HIPE/HIPEX available even when nvcc is the compiler
# (hip-nvidia-* presets: __CUDACC__ is defined but HIP headers are in scope).
if(HIP)
target_compile_definitions(cuda_battery INTERFACE BATTERY_HIP_BUILD)
endif()

if(CUDA_BATTERY_BUILD_TESTS)

# Google Test dependency
Expand Down Expand Up @@ -88,6 +122,26 @@ if(GPU)
endforeach()
endif()

# III. HIP GPU Tests (ending with "_hip.cpp")
if(HIP)
# HIP runtime on NVIDIA routes device-attribute queries through the CUDA Driver
# API (cuDeviceGetAttribute in libcuda.so). CMake's implicit HIP link only adds
# libcudart_static; link CUDA::cuda_driver explicitly so hipDeviceGetAttribute works.
find_package(CUDAToolkit REQUIRED)

file(GLOB hip_test_files tests/*_hip.cpp)
foreach(file ${hip_test_files})
cmake_path(GET file STEM test_name)
set_source_files_properties(${file} PROPERTIES LANGUAGE HIP)
add_executable(${test_name} ${file})
target_link_libraries(${test_name} cuda_battery)
if(CUDAToolkit_FOUND)
target_link_libraries(${test_name} CUDA::cuda_driver)
endif()
add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>)
endforeach()
endif()

endif()

# Documentation
Expand Down
123 changes: 111 additions & 12 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,50 @@
"displayName": "CPU Release configuration",
"description": "Build the project with a CPU compiler such as GCC.",
"inherits": ["default-cpu","release"]
},
{
"name": "default-hip",
"hidden": true,
"inherits": "default",
"cacheVariables": {
"GPU": { "type": "BOOL", "value": "OFF" },
"HIP": { "type": "BOOL", "value": "ON" }
}
},
{
"name": "default-hip-nvidia",
"hidden": true,
"inherits": "default-hip",
"environment": {
"HIP_PLATFORM": "nvidia"
},
"cacheVariables": {
"CMAKE_HIP_ARCHITECTURES": "native"
}
},
{
"name": "hip-debug",
"displayName": "HIP Debug (AMD ROCm)",
"description": "Build using hipcc targeting AMD GPUs.",
"inherits": ["default-hip", "debug", "verbose"]
},
{
"name": "hip-release",
"displayName": "HIP Release (AMD ROCm)",
"description": "Build using hipcc targeting AMD GPUs.",
"inherits": ["default-hip", "release"]
},
{
"name": "hip-nvidia-debug",
"displayName": "HIP Debug - NVIDIA backend [temporary]",
"description": "Build using hipcc with HIP_PLATFORM=nvidia. Used to develop and validate HIP support on NVIDIA hardware before testing on AMD. Remove once AMD testing is established.",
"inherits": ["default-hip-nvidia", "debug", "verbose"]
},
{
"name": "hip-nvidia-release",
"displayName": "HIP Release - NVIDIA backend [temporary]",
"description": "See hip-nvidia-debug. Remove once AMD testing is established.",
"inherits": ["default-hip-nvidia", "release"]
}
],
"buildPresets": [
Expand All @@ -125,6 +169,22 @@
{
"name": "cpu-release",
"configurePreset": "cpu-release"
},
{
"name": "hip-debug",
"configurePreset": "hip-debug"
},
{
"name": "hip-release",
"configurePreset": "hip-release"
},
{
"name": "hip-nvidia-debug",
"configurePreset": "hip-nvidia-debug"
},
{
"name": "hip-nvidia-release",
"configurePreset": "hip-nvidia-release"
}
],
"testPresets": [
Expand All @@ -143,6 +203,22 @@
{
"name": "cpu-release",
"configurePreset": "cpu-release"
},
{
"name": "hip-debug",
"configurePreset": "hip-debug"
},
{
"name": "hip-release",
"configurePreset": "hip-release"
},
{
"name": "hip-nvidia-debug",
"configurePreset": "hip-nvidia-debug"
},
{
"name": "hip-nvidia-release",
"configurePreset": "hip-nvidia-release"
}
],
"workflowPresets": [
Expand Down Expand Up @@ -200,18 +276,41 @@
{
"name": "gpu-release",
"steps": [
{
"type": "configure",
"name": "gpu-release"
},
{
"type": "build",
"name": "gpu-release"
},
{
"type": "test",
"name": "gpu-release"
}
{ "type": "configure", "name": "gpu-release" },
{ "type": "build", "name": "gpu-release" },
{ "type": "test", "name": "gpu-release" }
]
},
{
"name": "hip-debug",
"steps": [
{ "type": "configure", "name": "hip-debug" },
{ "type": "build", "name": "hip-debug" },
{ "type": "test", "name": "hip-debug" }
]
},
{
"name": "hip-release",
"steps": [
{ "type": "configure", "name": "hip-release" },
{ "type": "build", "name": "hip-release" },
{ "type": "test", "name": "hip-release" }
]
},
{
"name": "hip-nvidia-debug",
"steps": [
{ "type": "configure", "name": "hip-nvidia-debug" },
{ "type": "build", "name": "hip-nvidia-debug" },
{ "type": "test", "name": "hip-nvidia-debug" }
]
},
{
"name": "hip-nvidia-release",
"steps": [
{ "type": "configure", "name": "hip-nvidia-release" },
{ "type": "build", "name": "hip-nvidia-release" },
{ "type": "test", "name": "hip-nvidia-release" }
]
}
]
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
# Standard Library for CUDA Programming
# Standard Library for CUDA/HIP Programming

[![Build Status](https://travis-ci.com/lattice-land/cuda-battery.svg?branch=main)](https://travis-ci.com/lattice-land/cuda-battery)

This library provides data structures to ease programming in CUDA (version 12 or higher).
This library provides data structures to ease GPU programming in CUDA (version 12 or higher) and HIP (ROCm 6+).
For a tutorial and further information, please read [this manual](https://lattice-land.github.io/1-cuda-battery.html).

## Supported Backends

| Backend | Compiler | CMake option | Active macro |
|---|---|---|---|
| CPU only | GCC / Clang / MSVC | _(default)_ | — |
| CUDA (NVIDIA) | nvcc 12+ | `GPU=ON` | `BATTERY_CUDA_BACKEND` |
| HIP (AMD ROCm) | amdclang++ / hipcc, ROCm 6+ | `HIP=ON GPU=OFF` | `BATTERY_HIP_BACKEND` |

The three backends are mutually exclusive at compile time.

## Example

The example below works unchanged on both CUDA and HIP — just compile with the appropriate backend option.
For HIP, replace `CUDAEX` with `HIPEX` and `cudaDeviceSynchronize` with `hipDeviceSynchronize`.

Quick example on how to transfer a `std::vector` on CPU to a `battery::vector` on GPU (notice you don't need to do any manual memory allocation or deallocation):

```cpp
Expand Down Expand Up @@ -60,6 +73,7 @@ int main(int argc, char** argv) {
| Containers | [`vector`](https://lattice-land.github.io/cuda-battery/vector_8hpp.html) ([`std`](https://en.cppreference.com/w/cpp/container/vector)) | [`string`](https://lattice-land.github.io/cuda-battery/string_8hpp.html) ([`std`](https://en.cppreference.com/w/cpp/string/basic_string)) | [`dynamic_bitset`](https://lattice-land.github.io/cuda-battery/dynamic__bitset_8hpp.html) | |
| | [`tuple`](https://en.cppreference.com/w/cpp/utility/tuple) | [`variant`](https://lattice-land.github.io/cuda-battery/variant_8hpp.html) ([`std`](https://en.cppreference.com/w/cpp/utility/variant)) | [`bitset`](https://lattice-land.github.io/cuda-battery/bitset_8hpp.html) ([`std`](https://en.cppreference.com/w/cpp/utility/bitset)) | |
| Utility | [`CUDA`](https://lattice-land.github.io/cuda-battery/utility_8hpp.html#a7b01e29f669d6beed251f1b2a547ca93) | [`INLINE`](https://lattice-land.github.io/cuda-battery/utility_8hpp.html#a2eb6f9e0395b47b8d5e3eeae4fe0c116) | [`CUDAE`](https://lattice-land.github.io/cuda-battery/utility_8hpp.html#a289596c1db721f82251de6902f9699db) | [`CUDAEX`](https://lattice-land.github.io/cuda-battery/utility_8hpp.html#af35c92d967acfadd086658422f631100) |
| | | | [`HIPE`](https://lattice-land.github.io/cuda-battery/utility_8hpp.html) _(HIP)_ | [`HIPEX`](https://lattice-land.github.io/cuda-battery/utility_8hpp.html) _(HIP)_ |
| | [`limits`](https://lattice-land.github.io/cuda-battery/structbattery_1_1limits.html) | [`ru_cast`](https://lattice-land.github.io/cuda-battery/namespacebattery.html#a7fdea425c76eab201a009ea09b8cbac0) | [`rd_cast`](https://lattice-land.github.io/cuda-battery/namespacebattery.html#aa2296c962277e71780bccf1ba9708f59) | |
| | [`popcount`](https://lattice-land.github.io/cuda-battery/namespacebattery.html#a2821ae67e8ea81b375f3fd6d70909fef) ([`std`](https://en.cppreference.com/w/cpp/numeric/popcount)) | [`countl_zero`](https://lattice-land.github.io/cuda-battery/namespacebattery.html#aa18a34122dc3e8b7e96c4a54eeffa9aa) ([`std`](https://en.cppreference.com/w/cpp/numeric/countl_zero)) | [`countl_one`](https://lattice-land.github.io/cuda-battery/namespacebattery.html#ae252a50e577d7b092eb368b7e0289772) ([`std`](https://en.cppreference.com/w/cpp/numeric/countl_one)) | [`countr_zero`](https://lattice-land.github.io/cuda-battery/namespacebattery.html#a7338f90fab224e49c3716c5eace58bee) ([`std`](https://en.cppreference.com/w/cpp/numeric/countr_zero)) |
| | [`countr_one`](https://lattice-land.github.io/cuda-battery/namespacebattery.html#a974d0a682d546e1185ae7dca29c272d6) ([`std`](https://en.cppreference.com/w/cpp/numeric/countr_one)) | [`signum`](https://lattice-land.github.io/cuda-battery/namespacebattery.html#a31b3f5ee3799a73d29c153ebd222cdea) | [`ipow`](https://lattice-land.github.io/cuda-battery/namespacebattery.html#a93472d80842253624e2436eef7b900b6) | |
Expand Down
Loading