mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-13 00:08:26 +08:00
Release v13.3 of the CUDA samples with CUDA 13.3 Toolkit (#435)
This is the release of the CUDA 13.3 samples, which include additions for CUDA Tile C++, and updated CCCL and Python samples.
This commit is contained in:
63
cpp/4_CUDA_Libraries/cubDeviceSegmentedScan/CMakeLists.txt
Normal file
63
cpp/4_CUDA_Libraries/cubDeviceSegmentedScan/CMakeLists.txt
Normal file
@@ -0,0 +1,63 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
project(cubDeviceSegmentedScan LANGUAGES C CXX CUDA)
|
||||
|
||||
# Disable response file for libraries on QNX as qcc does not support lib paths with double quotes
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
|
||||
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_LIBRARIES OFF)
|
||||
endif()
|
||||
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 89 90 100 110 120)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||
if(ENABLE_CUDA_DEBUG)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (may significantly affect performance on some targets)
|
||||
else()
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo") # add line information to all builds for debug tools (exclusive to -G option)
|
||||
endif()
|
||||
|
||||
# Fetch CCCL via CPM.
|
||||
# Override with -DCCCL_SOURCE_DIR=/path/to/cccl to use a local checkout
|
||||
# instead of fetching from GitHub.
|
||||
set(CCCL_SAMPLES_CCCL_TAG "v3.3.3" CACHE STRING
|
||||
"Tag/branch of NVIDIA/cccl to fetch for the CCCL samples")
|
||||
|
||||
if(NOT TARGET CCCL::CCCL)
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/CPM.cmake")
|
||||
if(DEFINED CCCL_SOURCE_DIR AND NOT CCCL_SOURCE_DIR STREQUAL "")
|
||||
CPMAddPackage(NAME CCCL SOURCE_DIR "${CCCL_SOURCE_DIR}")
|
||||
else()
|
||||
CPMAddPackage(
|
||||
NAME CCCL
|
||||
GIT_REPOSITORY "https://github.com/NVIDIA/cccl"
|
||||
GIT_TAG "${CCCL_SAMPLES_CCCL_TAG}"
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Include directories and libraries
|
||||
include_directories(../../../Common)
|
||||
|
||||
# Source file
|
||||
# Add target for cubDeviceSegmentedScan
|
||||
add_executable(cubDeviceSegmentedScan cubDeviceSegmentedScan.cu)
|
||||
|
||||
target_compile_options(cubDeviceSegmentedScan PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(cubDeviceSegmentedScan PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(cubDeviceSegmentedScan PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
target_link_libraries(cubDeviceSegmentedScan PRIVATE
|
||||
CUDA::cudart
|
||||
CCCL::CCCL
|
||||
)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
48
cpp/4_CUDA_Libraries/cubDeviceSegmentedScan/README.md
Normal file
48
cpp/4_CUDA_Libraries/cubDeviceSegmentedScan/README.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# cubDeviceSegmentedScan - CUB DeviceSegmentedScan
|
||||
|
||||
## Description
|
||||
|
||||
This sample demonstrates `cub::DeviceSegmentedScan`. A segmented scan computes an independent scan over each of many contiguous segments in a single device-wide call. Two operations are shown: `ExclusiveSegmentedSum` across three independent segments, and `InclusiveSegmentedScan` with a custom binary operator (running maximum via `cuda::maximum<>`).
|
||||
|
||||
## Key Concepts
|
||||
|
||||
CUB Device Algorithms, Segmented Scan, Prefix Sum
|
||||
|
||||
## Supported SM Architectures
|
||||
|
||||
[SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus) [SM 10.0 ](https://developer.nvidia.com/cuda-gpus) [SM 11.0 ](https://developer.nvidia.com/cuda-gpus) [SM 12.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||
|
||||
## Supported OSes
|
||||
|
||||
Linux, Windows
|
||||
|
||||
## Supported CPU Architecture
|
||||
|
||||
x86_64, aarch64
|
||||
|
||||
## CUDA APIs involved
|
||||
|
||||
### [CCCL CUB](https://nvidia.github.io/cccl/cub/)
|
||||
|
||||
cub::DeviceSegmentedScan::ExclusiveSegmentedSum, cub::DeviceSegmentedScan::InclusiveSegmentedScan
|
||||
|
||||
### [CCCL libcu++](https://nvidia.github.io/cccl/libcudacxx/)
|
||||
|
||||
cuda::maximum
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
|
||||
cudaDeviceSynchronize, cudaGetDeviceProperties
|
||||
|
||||
## Dependencies needed to build/run
|
||||
|
||||
[CCCL 3.3+](https://github.com/NVIDIA/cccl). Fetched automatically via CPM at configure time (pinned to `v3.3.3`). Override with `-DCCCL_SOURCE_DIR=/path/to/cccl` to use a local checkout.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||
|
||||
## References (for more details)
|
||||
|
||||
[CCCL 3.3 release notes](https://github.com/NVIDIA/cccl/releases), [cub::DeviceSegmentedScan header](https://github.com/NVIDIA/cccl/blob/main/cub/cub/device/device_segmented_scan.cuh)
|
||||
@@ -0,0 +1,188 @@
|
||||
/* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* This sample demonstrates cub::DeviceSegmentedScan, added in CCCL 3.3.
|
||||
* Two operations are shown: ExclusiveSegmentedSum across three independent
|
||||
* segments, and InclusiveSegmentedScan with a custom binary operator
|
||||
* (running maximum via cuda::maximum<>). Each is verified against a
|
||||
* host reference implementation.
|
||||
*/
|
||||
|
||||
/* Includes, system */
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
|
||||
/* Includes, cuda */
|
||||
#include <cuda_runtime.h>
|
||||
#include <helper_cuda.h>
|
||||
|
||||
/* Includes, cccl */
|
||||
#include <cub/device/device_segmented_scan.cuh>
|
||||
#include <cuda/functional>
|
||||
#include <thrust/device_vector.h>
|
||||
#include <thrust/host_vector.h>
|
||||
|
||||
template <typename T>
|
||||
static void print_vec(const char *label, const std::vector<T> &v)
|
||||
{
|
||||
printf(" %-24s{", label);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
printf(" %d", static_cast<int>(v[i]));
|
||||
printf(" }\n");
|
||||
}
|
||||
|
||||
static std::vector<int> host_exclusive_segmented_sum(const std::vector<int> &input, const std::vector<size_t> &offsets)
|
||||
{
|
||||
std::vector<int> out(input.size(), 0);
|
||||
for (size_t s = 0; s + 1 < offsets.size(); ++s) {
|
||||
int running = 0;
|
||||
for (size_t i = offsets[s]; i < offsets[s + 1]; ++i) {
|
||||
out[i] = running;
|
||||
running += input[i];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static std::vector<int> host_inclusive_segmented_max(const std::vector<int> &input, const std::vector<size_t> &offsets)
|
||||
{
|
||||
std::vector<int> out(input.size(), 0);
|
||||
for (size_t s = 0; s + 1 < offsets.size(); ++s) {
|
||||
int running = std::numeric_limits<int>::min();
|
||||
for (size_t i = offsets[s]; i < offsets[s + 1]; ++i) {
|
||||
running = std::max(running, input[i]);
|
||||
out[i] = running;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static bool run_exclusive_segmented_sum()
|
||||
{
|
||||
/* 3 segments: [1,2,3] [4,5] [6,7,8] -> [0,1,3] [0,4] [0,6,13]. */
|
||||
thrust::device_vector<int> d_in = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||
thrust::device_vector<size_t> d_offsets = {0, 3, 5, 8};
|
||||
thrust::device_vector<int> d_out(d_in.size());
|
||||
|
||||
const auto num_segments = d_offsets.size() - 1;
|
||||
auto begin_offsets = d_offsets.begin();
|
||||
auto end_offsets = d_offsets.begin() + 1;
|
||||
|
||||
size_t temp_bytes = 0;
|
||||
checkCudaErrors(cub::DeviceSegmentedScan::ExclusiveSegmentedSum(
|
||||
nullptr, temp_bytes, d_in.begin(), d_out.begin(), begin_offsets, end_offsets, num_segments));
|
||||
thrust::device_vector<char> temp(temp_bytes);
|
||||
checkCudaErrors(cub::DeviceSegmentedScan::ExclusiveSegmentedSum(thrust::raw_pointer_cast(temp.data()),
|
||||
temp_bytes,
|
||||
d_in.begin(),
|
||||
d_out.begin(),
|
||||
begin_offsets,
|
||||
end_offsets,
|
||||
num_segments));
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
std::vector<int> h_in(d_in.begin(), d_in.end());
|
||||
std::vector<size_t> h_off(d_offsets.begin(), d_offsets.end());
|
||||
std::vector<int> got(d_out.begin(), d_out.end());
|
||||
std::vector<int> expected = host_exclusive_segmented_sum(h_in, h_off);
|
||||
|
||||
printf("cub::DeviceSegmentedScan::ExclusiveSegmentedSum\n");
|
||||
print_vec("input:", h_in);
|
||||
printf(" %-24s{", "offsets:");
|
||||
for (auto o : h_off)
|
||||
printf(" %zu", o);
|
||||
printf(" }\n");
|
||||
print_vec("got:", got);
|
||||
print_vec("expected:", expected);
|
||||
const bool ok = got == expected;
|
||||
printf(" %s\n", ok ? "OK" : "FAIL");
|
||||
return ok;
|
||||
}
|
||||
|
||||
static bool run_inclusive_segmented_max()
|
||||
{
|
||||
/* Same three segments, but compute running max per segment. */
|
||||
thrust::device_vector<int> d_in = {3, 1, 4, 5, 2, 9, 7, 8};
|
||||
thrust::device_vector<size_t> d_offsets = {0, 3, 5, 8};
|
||||
thrust::device_vector<int> d_out(d_in.size());
|
||||
|
||||
const auto num_segments = d_offsets.size() - 1;
|
||||
auto begin_offsets = d_offsets.begin();
|
||||
auto end_offsets = d_offsets.begin() + 1;
|
||||
|
||||
auto max_op = [] __host__ __device__(int a, int b) -> int { return cuda::maximum<>{}(a, b); };
|
||||
|
||||
size_t temp_bytes = 0;
|
||||
checkCudaErrors(cub::DeviceSegmentedScan::InclusiveSegmentedScan(
|
||||
nullptr, temp_bytes, d_in.begin(), d_out.begin(), begin_offsets, end_offsets, num_segments, max_op));
|
||||
thrust::device_vector<char> temp(temp_bytes);
|
||||
checkCudaErrors(cub::DeviceSegmentedScan::InclusiveSegmentedScan(thrust::raw_pointer_cast(temp.data()),
|
||||
temp_bytes,
|
||||
d_in.begin(),
|
||||
d_out.begin(),
|
||||
begin_offsets,
|
||||
end_offsets,
|
||||
num_segments,
|
||||
max_op));
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
std::vector<int> h_in(d_in.begin(), d_in.end());
|
||||
std::vector<size_t> h_off(d_offsets.begin(), d_offsets.end());
|
||||
std::vector<int> got(d_out.begin(), d_out.end());
|
||||
std::vector<int> expected = host_inclusive_segmented_max(h_in, h_off);
|
||||
|
||||
printf("cub::DeviceSegmentedScan::InclusiveSegmentedScan (running max)\n");
|
||||
print_vec("input:", h_in);
|
||||
printf(" %-24s{", "offsets:");
|
||||
for (auto o : h_off)
|
||||
printf(" %zu", o);
|
||||
printf(" }\n");
|
||||
print_vec("got:", got);
|
||||
print_vec("expected:", expected);
|
||||
const bool ok = got == expected;
|
||||
printf(" %s\n", ok ? "OK" : "FAIL");
|
||||
return ok;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
cudaDeviceProp props;
|
||||
checkCudaErrors(cudaGetDeviceProperties(&props, devID));
|
||||
printf("Device: %s (Compute Capability %d.%d)\n\n", props.name, props.major, props.minor);
|
||||
|
||||
bool ok = true;
|
||||
ok &= run_exclusive_segmented_sum();
|
||||
printf("\n");
|
||||
ok &= run_inclusive_segmented_max();
|
||||
|
||||
printf("\n%s\n", ok ? "Done" : "FAILED");
|
||||
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
Reference in New Issue
Block a user