| 1 |
|
| 2 |
# Example Project Using CCCL From GitHub |
| 3 |
|
| 4 |
Many CUDA C++ users are accustomed to using CCCL headers (Thrust, CUB, libcu++) provided with the [NVIDIA CUDA Toolkit](https://developer.nvidia.com/cuda-toolkit) or [NVIDIA HPC SDK](https://developer.nvidia.com/hpc-sdk). |
| 5 |
|
| 6 |
In addition, we also support using CCCL directly from GitHub. |
| 7 |
The primary benefit is that this allows users to use the latest version of CCCL without having to wait for a new release of the CUDA Toolkit or HPC SDK. |
| 8 |
|
| 9 |
This example demonstrates how to use CCCL from GitHub in a CMake project. |
| 10 |
|
| 11 |
## Overview |
| 12 |
|
| 13 |
This is a standalone example of how to use [CCCL](https://github.com/nvidia/cccl) in a CMake project. |
| 14 |
|
| 15 |
This example demonstrates fetching CCCL from GitHub and linking it with a simple example CUDA program ([`example.cu`](example.cu)) that utilizes the headers from CCCL. |
| 16 |
|
| 17 |
This is intended to be a starting point for users who want to use CCCL in their own projects. |
| 18 |
|
| 19 |
## How to Adapt This Example to Your Project |
| 20 |
|
| 21 |
This example is intended to be a starting point for users who want to use CCCL in their own projects. |
| 22 |
In order to adapt this example to your project, you will need to do the following: |
| 23 |
1. Download `CPM.cmake` into your project's `cmake/` directory ([see below for instructions](#downloading-cpm)). |
| 24 |
2. Add the following lines to your project's `CMakeLists.txt` file: |
| 25 |
```cmake |
| 26 |
include(cmake/CPM.cmake) |
| 27 |
|
| 28 |
# This will automatically clone CCCL from GitHub and make the exported cmake targets available |
| 29 |
CPMAddPackage( |
| 30 |
NAME CCCL |
| 31 |
GITHUB_REPOSITORY nvidia/cccl |
| 32 |
GIT_TAG main # Fetches the latest commit on the main branch |
| 33 |
) |
| 34 |
|
| 35 |
# If you're building an executable |
| 36 |
add_executable(your_executable your_file.cu) |
| 37 |
target_link_libraries(your_executable PRIVATE CCCL::CCCL) |
| 38 |
|
| 39 |
# Alternatively, if you're building a library |
| 40 |
add_library(your_library your_file.cu) |
| 41 |
target_link_libraries(your_library PRIVATE CCCL::CCCL) |
| 42 |
``` |
| 43 |
See the [CMakeLists.txt](CMakeLists.txt) file in this directory for a complete example. |
| 44 |
3. Configure and build your project as normal and verify that it builds successfully. |
| 45 |
|
| 46 |
For more information on using CPM, see [below](#using-cmake-package-manager). |
| 47 |
|
| 48 |
## Using CMake Package Manager |
| 49 |
|
| 50 |
This example uses the CMake Package Manager (CPM) to fetch CCCL from GitHub. |
| 51 |
|
| 52 |
See the [CMakeLists.txt](CMakeLists.txt) file in this directory for the complete example. |
| 53 |
|
| 54 |
If you are not familiar with CPM, you can find more information [here](https://github.com/cpm-cmake/CPM.cmake). |
| 55 |
In short, CPM is a CMake module that simplifies dependency management for CMake projects. |
| 56 |
It automatically downloads and integrates dependencies into your CMake project. |
| 57 |
|
| 58 |
### Downloading CPM |
| 59 |
|
| 60 |
In order to get the latest version of CPM.cmake, you can run the following command in the root directory of your project: |
| 61 |
|
| 62 |
```bash |
| 63 |
mkdir -p cmake |
| 64 |
wget -O cmake/CPM.cmake https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/get_cpm.cmake |
| 65 |
``` |
| 66 |
|
| 67 |
This will download and create the file `cmake/CPM.cmake` in your project directory. |
| 68 |
Most projects will want to commit this file to their source control system. |
| 69 |
You can then use `include(cmake/CPM.cmake)` in your project's `CMakeLists.txt` file to include CPM in your project. |
| 70 |
|
| 71 |
Alternatively, you can add the following logic to your `CMakeLists.txt` to download CPM if it is not already present in your project directory. |
| 72 |
|
| 73 |
```cmake |
| 74 |
set(CPM_DOWNLOAD_VERSION 0.34.0) |
| 75 |
|
| 76 |
if(CPM_SOURCE_CACHE) |
| 77 |
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") |
| 78 |
elseif(DEFINED ENV{CPM_SOURCE_CACHE}) |
| 79 |
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") |
| 80 |
else() |
| 81 |
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") |
| 82 |
endif() |
| 83 |
|
| 84 |
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) |
| 85 |
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") |
| 86 |
file(DOWNLOAD |
| 87 |
https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake |
| 88 |
${CPM_DOWNLOAD_LOCATION} |
| 89 |
) |
| 90 |
endif() |
| 91 |
|
| 92 |
include(${CPM_DOWNLOAD_LOCATION}) |
| 93 |
``` |
| 94 |
|
| 95 |
## Building and Running the Example |
| 96 |
|
| 97 |
Most people will want to adapt this example to their own project as described [above](#how-to-adapt-this-example-to-your-project). If you would like to build and run this example as-is, you will need to follow the instructions below. |
| 98 |
|
| 99 |
### Prerequisites |
| 100 |
|
| 101 |
If you would like to build and run this example as-is, you will need: |
| 102 |
|
| 103 |
- A CUDA-capable GPU |
| 104 |
- NVIDIA CUDA Toolkit (11.1 or later) |
| 105 |
- CMake (3.14 or later) |
| 106 |
- A C++14 standard-compliant compiler |
| 107 |
- git |
| 108 |
|
| 109 |
### Instructions |
| 110 |
|
| 111 |
1. Clone this repository to your local machine. |
| 112 |
```bash |
| 113 |
git clone https://github.com/NVIDIA/cccl.git |
| 114 |
``` |
| 115 |
|
| 116 |
2. Enter the directory of the cloned repository. |
| 117 |
```bash |
| 118 |
cd cccl/examples/example_project |
| 119 |
``` |
| 120 |
|
| 121 |
3. Run the CMake configure step |
| 122 |
```bash |
| 123 |
cmake -S . -B build |
| 124 |
``` |
| 125 |
Alternatively, |
| 126 |
```bash |
| 127 |
mkdir -p build |
| 128 |
cd build |
| 129 |
cmake .. |
| 130 |
``` |
| 131 |
4. Run the CMake build step. |
| 132 |
```bash |
| 133 |
cmake --build . |
| 134 |
``` |
| 135 |
|
| 136 |
6. Run the executable. |
| 137 |
```bash |
| 138 |
./build/example_project |
| 139 |
``` |
| 140 |
|
| 141 |
If everything is configured correctly, the program will execute and print the sum of an array of integers, demonstrating the use of cccl. |