[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:
75
cccl_upstream/examples/cudax/CMakeLists.txt
Normal file
75
cccl_upstream/examples/cudax/CMakeLists.txt
Normal file
@@ -0,0 +1,75 @@
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2023 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.
|
||||
|
||||
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
|
||||
|
||||
project(CUDAX_SAMPLES CUDA 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
|
||||
"Git 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
|
||||
CPMAddPackage(
|
||||
NAME CCCL
|
||||
GIT_REPOSITORY "${CCCL_REPOSITORY}"
|
||||
GIT_TAG ${CCCL_TAG}
|
||||
GIT_SHALLOW ON
|
||||
# The following is required to make the `CCCL::cudax` target available:
|
||||
OPTIONS "CCCL_ENABLE_UNSTABLE ON"
|
||||
)
|
||||
|
||||
# Default to building for the GPU on the current system
|
||||
if (NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
|
||||
set(CMAKE_CUDA_ARCHITECTURES native)
|
||||
endif()
|
||||
|
||||
add_library(cudax_samples_interface INTERFACE)
|
||||
|
||||
target_link_libraries(cudax_samples_interface INTERFACE CCCL::CCCL CCCL::cudax)
|
||||
|
||||
if ("MSVC" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
|
||||
# mdspan on windows only works in C++20 mode
|
||||
target_compile_features(cudax_samples_interface INTERFACE cxx_std_20)
|
||||
|
||||
if (MSVC_TOOLSET_VERSION LESS 143)
|
||||
# winbase.h(9572): warning C5105: macro expansion producing 'defined' has undefined behavior
|
||||
target_compile_options(
|
||||
cudax_samples_interface
|
||||
INTERFACE
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/wd5105>
|
||||
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=/wd5105>
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# The vector_add sample demonstrates a simple CUDA kernel that adds two vectors
|
||||
add_executable(vector_add vector_add/vector_add.cu)
|
||||
target_link_libraries(vector_add PUBLIC cudax_samples_interface)
|
||||
|
||||
# This is only relevant for internal testing and not needed by end users.
|
||||
include(CTest)
|
||||
enable_testing()
|
||||
add_test(NAME vector_add COMMAND vector_add)
|
||||
5
cccl_upstream/examples/cudax/README.md
Normal file
5
cccl_upstream/examples/cudax/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
# Overview
|
||||
|
||||
This example extends the `example_project` by additionally enabling the CUDA Experimental library, `cudax`.
|
||||
See the `example_project`'s README for additional information.
|
||||
1297
cccl_upstream/examples/cudax/cmake/CPM.cmake
Normal file
1297
cccl_upstream/examples/cudax/cmake/CPM.cmake
Normal file
File diff suppressed because it is too large
Load Diff
145
cccl_upstream/examples/cudax/vector_add/vector.cuh
Normal file
145
cccl_upstream/examples/cudax/vector_add/vector.cuh
Normal file
@@ -0,0 +1,145 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of CUDA Experimental in CUDA C++ Core Libraries,
|
||||
// under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef _CUDAX__CONTAINER_VECTOR
|
||||
#define _CUDAX__CONTAINER_VECTOR
|
||||
|
||||
#include <cuda/__cccl_config>
|
||||
|
||||
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
|
||||
# pragma GCC system_header
|
||||
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
|
||||
# pragma clang system_header
|
||||
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
|
||||
# pragma system_header
|
||||
#endif // no system header
|
||||
|
||||
#include <thrust/device_vector.h>
|
||||
#include <thrust/host_vector.h>
|
||||
|
||||
#include <cuda/std/__type_traits/maybe_const.h>
|
||||
#include <cuda/std/span>
|
||||
#include <cuda/stream>
|
||||
|
||||
#include <cuda/experimental/__detail/utility.cuh>
|
||||
#include <cuda/experimental/__launch/param_kind.cuh>
|
||||
|
||||
namespace cuda::experimental
|
||||
{
|
||||
using ::cuda::std::span;
|
||||
using ::thrust::device_vector;
|
||||
using ::thrust::host_vector;
|
||||
|
||||
template <typename _Ty>
|
||||
class vector
|
||||
{
|
||||
public:
|
||||
vector() = default;
|
||||
explicit vector(size_t __n)
|
||||
: __h_(__n)
|
||||
{}
|
||||
|
||||
_Ty& operator[](size_t __i) noexcept
|
||||
{
|
||||
__dirty_ = true;
|
||||
return __h_[__i];
|
||||
}
|
||||
|
||||
const _Ty& operator[](size_t __i) const noexcept
|
||||
{
|
||||
return __h_[__i];
|
||||
}
|
||||
|
||||
private:
|
||||
void sync_host_to_device([[maybe_unused]] ::cuda::stream_ref __str, __detail::__param_kind __p) const
|
||||
{
|
||||
if (__dirty_)
|
||||
{
|
||||
if (__p == __detail::__param_kind::_out)
|
||||
{
|
||||
// There's no need to copy the data from host to device if the data is
|
||||
// only going to be written to. We can just allocate the device memory.
|
||||
__d_.resize(__h_.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: use a memcpy async here
|
||||
__d_ = __h_;
|
||||
}
|
||||
__dirty_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void sync_device_to_host(::cuda::stream_ref __str, __detail::__param_kind __p) const
|
||||
{
|
||||
if (__p != __detail::__param_kind::_in)
|
||||
{
|
||||
// TODO: use a memcpy async here
|
||||
__str.sync(); // wait for the kernel to finish executing
|
||||
__h_ = __d_;
|
||||
}
|
||||
}
|
||||
|
||||
template <__detail::__param_kind _Kind>
|
||||
class __action //: private __detail::__immovable
|
||||
{
|
||||
using __cv_vector = ::cuda::std::__maybe_const<_Kind == __detail::__param_kind::_in, vector>;
|
||||
|
||||
public:
|
||||
explicit __action(::cuda::stream_ref __str, __cv_vector& __v) noexcept
|
||||
: __str_(__str)
|
||||
, __v_(__v)
|
||||
{
|
||||
__v_.sync_host_to_device(__str_, _Kind);
|
||||
}
|
||||
|
||||
__action(__action&&) = delete;
|
||||
|
||||
~__action()
|
||||
{
|
||||
__v_.sync_device_to_host(__str_, _Kind);
|
||||
}
|
||||
|
||||
::cuda::std::span<_Ty> transformed_argument()
|
||||
{
|
||||
return {__v_.__d_.data().get(), __v_.__d_.size()};
|
||||
}
|
||||
|
||||
private:
|
||||
::cuda::stream_ref __str_;
|
||||
__cv_vector& __v_;
|
||||
};
|
||||
|
||||
[[nodiscard]] friend __action<__detail::__param_kind::_inout>
|
||||
transform_launch_argument(::cuda::stream_ref __str, vector& __v) noexcept
|
||||
{
|
||||
return __action<__detail::__param_kind::_inout>{__str, __v};
|
||||
}
|
||||
|
||||
[[nodiscard]] friend __action<__detail::__param_kind::_in>
|
||||
transform_launch_argument(::cuda::stream_ref __str, const vector& __v) noexcept
|
||||
{
|
||||
return __action<__detail::__param_kind::_in>{__str, __v};
|
||||
}
|
||||
|
||||
template <__detail::__param_kind _Kind>
|
||||
[[nodiscard]] friend __action<_Kind>
|
||||
transform_launch_argument(::cuda::stream_ref __str, __detail::__box<vector, _Kind> __b) noexcept
|
||||
{
|
||||
return __action<_Kind>{__str, __b.__val};
|
||||
}
|
||||
|
||||
mutable host_vector<_Ty> __h_;
|
||||
mutable device_vector<_Ty> __d_{};
|
||||
mutable bool __dirty_ = true;
|
||||
};
|
||||
} // namespace cuda::experimental
|
||||
|
||||
#endif
|
||||
127
cccl_upstream/examples/cudax/vector_add/vector_add.cu
Normal file
127
cccl_upstream/examples/cudax/vector_add/vector_add.cu
Normal file
@@ -0,0 +1,127 @@
|
||||
/* Copyright (c) 2022, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Vector addition: C = A + B.
|
||||
*
|
||||
* This sample is a very basic sample that implements element by element
|
||||
* vector addition. It is the same as the sample illustrating Chapter 2
|
||||
* of the programming guide with some additions like error checking.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
// For the CUDA runtime routines (prefixed with "cuda_")
|
||||
#include <cuda/std/span>
|
||||
|
||||
#include <cuda/experimental/launch.cuh>
|
||||
#include <cuda/experimental/stream.cuh>
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include "vector.cuh"
|
||||
|
||||
namespace cudax = cuda::experimental;
|
||||
using cudax::in;
|
||||
using cudax::out;
|
||||
|
||||
/**
|
||||
* CUDA Kernel Device code
|
||||
*
|
||||
* Computes the vector addition of A and B into C. The 3 vectors have the same
|
||||
* number of elements numElements.
|
||||
*/
|
||||
__global__ void vectorAdd(cudax::span<const float> A, cudax::span<const float> B, cudax::span<float> C)
|
||||
{
|
||||
int i = blockDim.x * blockIdx.x + threadIdx.x;
|
||||
|
||||
if (i < A.size())
|
||||
{
|
||||
C[i] = A[i] + B[i] + 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Host main routine
|
||||
*/
|
||||
int main(void)
|
||||
try
|
||||
{
|
||||
// A CUDA stream on which to execute the vector addition kernel
|
||||
cudax::stream stream(cuda::devices[0]);
|
||||
|
||||
// Print the vector length to be used, and compute its size
|
||||
int numElements = 50000;
|
||||
printf("[Vector addition of %d elements]\n", numElements);
|
||||
|
||||
// Allocate the host vectors
|
||||
cudax::vector<float> A(numElements); // input
|
||||
cudax::vector<float> B(numElements); // input
|
||||
cudax::vector<float> C(numElements); // output
|
||||
|
||||
// Initialize the host input vectors
|
||||
for (int i = 0; i < numElements; ++i)
|
||||
{
|
||||
A[i] = rand() / (float) RAND_MAX;
|
||||
B[i] = rand() / (float) RAND_MAX;
|
||||
}
|
||||
|
||||
// Define the kernel launch parameters
|
||||
constexpr int threadsPerBlock = 256;
|
||||
auto config = cuda::distribute<threadsPerBlock>(numElements);
|
||||
|
||||
// Launch the vectorAdd kernel
|
||||
printf("CUDA kernel launch with %zu blocks of %d threads\n", cuda::block.count(cuda::grid, config), threadsPerBlock);
|
||||
cudax::launch(stream, config, vectorAdd, in(A), in(B), out(C));
|
||||
|
||||
printf("waiting for the stream to finish\n");
|
||||
stream.sync();
|
||||
|
||||
printf("verifying the results\n");
|
||||
// Verify that the result vector is correct
|
||||
for (int i = 0; i < numElements; ++i)
|
||||
{
|
||||
if (fabs(A[i] + B[i] - C[i]) > 1e-5)
|
||||
{
|
||||
fprintf(stderr, "Result verification failed at element %d!\n", i);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
printf("Test PASSED\n");
|
||||
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("caught an exception: \"%s\"\n", e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
printf("caught an unknown exception\n");
|
||||
}
|
||||
Reference in New Issue
Block a user