[INFRA] Import NVIDIA/CCCL upstream as optimization reference library
CCCL (CUDA C++ Core Libraries) provides: - CUB: device/block/warp-level GPU primitives (reduce, scan, sort, topk) - Thrust: high-level parallel algorithms (transform_reduce, sort, scan) - libcudacxx: CUDA C++ standard library (atomics, barriers, memory) - cudax: experimental features (memory resources, allocators) - Tuning policies: per-SM hardware-specific algorithm parameters Competition optimization vectors mapped to CCCL: - Output TPS (83% weight): warp_reduce, block_reduce, device_topk - Input TPS (14% weight): device_scan, block_load, prefetch - Cache TPS (3% weight): prefix caching strategy patterns - Memory (0.9 util): pooled/cached/buddy allocators Source: https://github.com/NVIDIA/cccl (shallow clone, HEAD only) License: Apache-2.0
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# This demo provides an example of how to configure a project to use Thrust while selecting
|
||||
# the device system as a configuration option. The device system is selected by setting the
|
||||
# CMake option `CCCL_THRUST_DEVICE_SYSTEM={CUDA, OMP, TBB, CPP}` for CUDA, OpenMP, Intel Threading
|
||||
# Building Blocks (TBB), and serial C++, respectively. If no option is provided, the default is `CUDA`.
|
||||
#
|
||||
# See the accompanying README.md for more information and build instructions.
|
||||
|
||||
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
|
||||
|
||||
project(ThrustFlexibleDeviceSystemDemo CXX)
|
||||
|
||||
# This example uses the CMake Package Manager (CPM) to simplify fetching CCCL from GitHub
|
||||
# For more information, see https://github.com/cpm-cmake/CPM.cmake
|
||||
include(cmake/CPM.cmake)
|
||||
|
||||
# We define these as variables so they can be overridden in CI to pull from a PR instead of CCCL `main`
|
||||
# In your project, these variables are unnecessary and you can just use the values directly
|
||||
set(
|
||||
CCCL_REPOSITORY
|
||||
"https://github.com/NVIDIA/cccl"
|
||||
CACHE STRING
|
||||
"GitHub repository to fetch CCCL from"
|
||||
)
|
||||
set(CCCL_TAG "main" CACHE STRING "Git tag/branch to fetch from CCCL repository")
|
||||
|
||||
# This will automatically clone CCCL from GitHub and make the exported cmake targets available.
|
||||
# The default `CCCL::Thrust` target will be configured to use the system device defined by
|
||||
# `CCCL_THRUST_DEVICE_SYSTEM`.
|
||||
CPMAddPackage(NAME CCCL GIT_REPOSITORY "${CCCL_REPOSITORY}" GIT_TAG ${CCCL_TAG})
|
||||
|
||||
# CUDA specific setup
|
||||
if (CCCL_THRUST_DEVICE_SYSTEM STREQUAL "CUDA")
|
||||
# Need to explicitly enable the CUDA language for the project.
|
||||
# Note that the project(...) command earlier only enables CXX by default.
|
||||
enable_language(CUDA)
|
||||
|
||||
# Optional: Call find_package(CCCL) again after enabling new languages
|
||||
# to update compatibility flags for newly detected compilers.
|
||||
find_package(CCCL CONFIG REQUIRED)
|
||||
|
||||
# Compile for the native CUDA arch if not specified:
|
||||
if (NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
|
||||
set(CMAKE_CUDA_ARCHITECTURES native)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Creates a cmake executable target for the main program
|
||||
add_executable(example_program example.cpp)
|
||||
|
||||
# Thrust requires at least C++17:
|
||||
target_compile_features(example_program PUBLIC cxx_std_17)
|
||||
if (CCCL_THRUST_DEVICE_SYSTEM STREQUAL "CUDA")
|
||||
target_compile_features(example_program PUBLIC cuda_std_17)
|
||||
endif()
|
||||
|
||||
# By default, CMake inspects the source file extension to determine whether to use C++ or CUDA
|
||||
# compilers. We can override this behavior by using source file properties. Here, we tell CMake
|
||||
# to compile this C++ (.cpp) file with the CUDA compiler when using the CUDA device system:
|
||||
if (CCCL_THRUST_DEVICE_SYSTEM STREQUAL "CUDA")
|
||||
set_source_files_properties(example.cpp PROPERTIES LANGUAGE CUDA)
|
||||
endif()
|
||||
|
||||
# "Links" the CCCL Cmake target to the `example_program` executable. This configures everything needed to use
|
||||
# CCCL headers, including setting up include paths, compiler flags, Thrust host/device configuration, etc.
|
||||
target_link_libraries(example_program PRIVATE CCCL::CCCL)
|
||||
|
||||
# This is only relevant for internal testing and not needed by end users.
|
||||
include(CTest)
|
||||
enable_testing()
|
||||
add_test(NAME example_program COMMAND example_program)
|
||||
set_tests_properties(
|
||||
example_program
|
||||
PROPERTIES
|
||||
PASS_REGULAR_EXPRESSION
|
||||
"Detected device system: ${CCCL_THRUST_DEVICE_SYSTEM}"
|
||||
)
|
||||
@@ -0,0 +1,45 @@
|
||||
# Thrust Flexible Device System Example
|
||||
|
||||
This example illustrates best practices for writing generic CMake that supports user-configuration of the Thrust device system via the `CCCL_THRUST_DEVICE_SYSTEM` CMake option.
|
||||
|
||||
Valid values for this option are:
|
||||
|
||||
- `CUDA`
|
||||
- `OMP` (OpenMP)
|
||||
- `TBB` (Intel Thread Building Blocks)
|
||||
- `CPP` (Serial C++ backend)
|
||||
|
||||
The CMakeLists.txt file for this example is annotated to show how to achieve a generic build system that supports any of device system.
|
||||
|
||||
## How To Use This Example
|
||||
|
||||
Configure and build this example as follows:
|
||||
|
||||
```
|
||||
# Checkout example and prepare build directory:
|
||||
git clone https://github.com/NVIDIA/cccl.git
|
||||
cd cccl/thrust_flexible_device_system
|
||||
mkdir build
|
||||
cd build
|
||||
|
||||
# Configure:
|
||||
cmake .. -DCCCL_THRUST_DEVICE_SYSTEM=CUDA # or TBB, OMP, CPP
|
||||
|
||||
# Build:
|
||||
cmake --build .
|
||||
|
||||
# Run:
|
||||
ctest -V
|
||||
```
|
||||
|
||||
## Advanced Thrust Usecases
|
||||
|
||||
For more control over the Thrust configuration, see the Thrust CMake package's [README.md](../../lib/cmake/thrust/README.md).
|
||||
This details how to use the `thrust_create_target` function to generate Thrust interface targets in CMake.
|
||||
|
||||
If using `thrust_create_target` directly, you may also want to set the CMake option `CCCL_ENABLE_DEFAULT_THRUST_TARGET=OFF` to prevent the default `CCCL::Thrust` target from being initialized.
|
||||
This will avoid checking for any dependencies required for the default target that may be unnecessary for your project.
|
||||
|
||||
## Further Reading About CCCL + CMake
|
||||
|
||||
The `basic` example's [README.md](../basic/README.md) has additional information that you may find useful for using CCCL with CPM and CMake.
|
||||
1297
cccl_upstream/examples/thrust_flexible_device_system/cmake/CPM.cmake
Normal file
1297
cccl_upstream/examples/thrust_flexible_device_system/cmake/CPM.cmake
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <thrust/device_vector.h>
|
||||
#include <thrust/reduce.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
constexpr std::size_t N = 1000;
|
||||
thrust::device_vector<int> data(N, 1);
|
||||
|
||||
const auto result = thrust::reduce(data.cbegin(), data.cend());
|
||||
|
||||
std::cout << "Sum: " << result << '\n';
|
||||
|
||||
if (result != N)
|
||||
{
|
||||
std::cerr << "Error: Expected sum of " << N << ", but got " << result << '\n';
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Detected device system: ";
|
||||
#if THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_CUDA
|
||||
std::cout << "CUDA" << '\n';
|
||||
#elif THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_TBB
|
||||
std::cout << "TBB" << '\n';
|
||||
#elif THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_OMP
|
||||
std::cout << "OMP" << '\n';
|
||||
#elif THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_CPP
|
||||
std::cout << "CPP" << '\n';
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user