[CCCL] 瘦身 + 补全: 移除 cudax/python/libcudacxx-tests 冗余文件, 新增 c2h 测试助手 + cmake 构建系统 + 8 个 CUDA thrust examples

变更摘要:
- 删除: cudax/ (783 files, 7.2M) — 实验性组件,竞赛不需要
- 删除: python/ (226 files, 2.0M) — Python 绑定,竞赛不需要
- 删除: libcudacxx/{test,benchmarks,codegen,cmake,share} (4432 files, 31M)
  保留: libcudacxx/include/ (1463 headers, cuda::std 编译依赖)
- 新增: c2h/ (27 files) — CUB Catch2 测试辅助头文件,编译 243 个测试必需
- 新增: cmake/ (29 files) — CCCL 原生 CMake 构建系统
- 新增: thrust/examples/cuda/ (7 files) + cpp_integration/ (1 file)
  async_reduce, custom_temporary_allocation, explicit_cuda_stream,
  global_device_vector, range_view, unwrap_pointer, wrap_pointer, device

结果: cccl_upstream 从 74M→35M (瘦身 53%), 核心内容 100% 保留:
  27/27 tuning headers, 78 benchmarks, 243 tests,
  60 thrust examples, 18 CUB examples, 全部编译头文件
This commit is contained in:
muh-bot
2026-08-03 12:39:26 +00:00
parent a2a5dd8f00
commit 24ef6a91b5
5439 changed files with 0 additions and 719516 deletions

View File

@@ -1,331 +0,0 @@
//===----------------------------------------------------------------------===//
//
// 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) 2026 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
#include <cuda/std/__cccl/cuda_toolkit.h>
#if _CCCL_CTK_AT_LEAST(12, 2)
# include <cuda/memory_resource>
# include <cuda/experimental/container.cuh>
# include <cuda/experimental/graph.cuh>
# include <cuda/experimental/launch.cuh>
# include <cuda/experimental/stream.cuh>
# include <testing.cuh>
# include <utility.cuh>
namespace
{
namespace test
{
// RAII wrapper around a pinned-memory allocation.
template <typename T>
struct pinned_array
{
cuda::mr::legacy_pinned_memory_resource __mr{};
T* __ptr;
std::size_t __n;
explicit pinned_array(std::size_t __count, T __init = T{})
: __ptr(static_cast<T*>(__mr.allocate_sync(__count * sizeof(T))))
, __n(__count)
{
for (std::size_t i = 0; i < __n; ++i)
{
__ptr[i] = __init;
}
}
~pinned_array()
{
__mr.deallocate_sync(__ptr, __n * sizeof(T));
}
pinned_array(const pinned_array&) = delete;
pinned_array& operator=(const pinned_array&) = delete;
T* get() const noexcept
{
return __ptr;
}
T& operator[](std::size_t i) const noexcept
{
return __ptr[i];
}
};
} // namespace test
struct write_iota
{
__device__ void operator()(cuda::std::span<int> buf) const noexcept
{
for (int i = 0; i < static_cast<int>(buf.size()); ++i)
{
buf[i] = i;
}
}
};
struct verify_iota
{
__device__ void operator()(cuda::std::span<const int> buf) const noexcept
{
for (int i = 0; i < static_cast<int>(buf.size()); ++i)
{
REQUIRE(buf[i] == i);
}
}
};
struct verify_all_zero
{
__device__ void operator()(cuda::std::span<const int> buf) const noexcept
{
for (const auto& val : buf)
{
REQUIRE(val == 0);
}
}
};
struct sum_to_ptr
{
__device__ void operator()(cuda::std::span<const int> buf, int* out) const noexcept
{
int s = 0;
for (const auto& val : buf)
{
s += val;
}
*out = s;
}
};
} // namespace
C2H_TEST("graph_buffer with no_init allocates and can be written/read", "[graph][graph_buffer]")
{
cudax::stream s{cuda::device_ref{0}};
constexpr int N = 10;
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::graph_memory_resource mr{cuda::device_ref{0}};
cudax::graph_buffer<int> buf(pb, mr, N, cuda::no_init);
REQUIRE(buf.size() == N);
REQUIRE(buf.data() != nullptr);
STATIC_CHECK(decltype(buf)::properties_list::has_property(cuda::mr::device_accessible{}));
// Write iota pattern to buffer
cudax::launch(pb, test::one_thread_dims, write_iota{}, buf);
// Read back and verify
cudax::launch(pb, test::one_thread_dims, verify_iota{}, buf);
// Free the buffer
buf.destroy(pb);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
}
C2H_TEST("graph_buffer with zero-fill initializes to zero", "[graph][graph_buffer]")
{
cudax::stream s{cuda::device_ref{0}};
constexpr int N = 16;
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::graph_memory_resource mr{cuda::device_ref{0}};
int zero = 0;
cudax::graph_buffer<int> buf(pb, mr, N, zero);
// Verify all zeros
cudax::launch(pb, test::one_thread_dims, verify_all_zero{}, buf);
buf.destroy(pb);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
}
C2H_TEST("graph_buffer from span", "[graph][graph_buffer]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> host_data{6};
for (int i = 0; i < 6; ++i)
{
host_data[i] = i + 1;
}
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::graph_memory_resource mr{cuda::device_ref{0}};
cudax::graph_buffer<int> buf(pb, mr, cuda::std::span<const int>{host_data.get(), 6});
REQUIRE(buf.size() == 6);
test::pinned_array<int> result{6};
cudax::copy_bytes(pb, buf, cuda::std::span<int>{result.get(), 6});
buf.destroy(pb);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
for (int i = 0; i < 6; ++i)
{
REQUIRE(result[i] == i + 1);
}
}
C2H_TEST("graph_buffer from initializer_list", "[graph][graph_buffer]")
{
cudax::stream s{cuda::device_ref{0}};
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::graph_memory_resource mr{cuda::device_ref{0}};
cudax::graph_buffer<int> buf(pb, mr, {10, 20, 30, 40});
REQUIRE(buf.size() == 4);
test::pinned_array<int> result{4};
cudax::copy_bytes(pb, buf, cuda::std::span<int>{result.get(), 4});
buf.destroy(pb);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(result[0] == 10);
REQUIRE(result[1] == 20);
REQUIRE(result[2] == 30);
REQUIRE(result[3] == 40);
}
C2H_TEST("make_buffer factory with no_init", "[graph][graph_buffer]")
{
cudax::stream s{cuda::device_ref{0}};
constexpr int N = 8;
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::graph_memory_resource mr{cuda::device_ref{0}};
auto buf = cudax::make_buffer<int>(pb, mr, N, cuda::no_init);
REQUIRE(buf.size() == N);
cudax::launch(pb, test::one_thread_dims, write_iota{}, buf);
cudax::launch(pb, test::one_thread_dims, verify_iota{}, buf);
buf.destroy(pb);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
}
C2H_TEST("graph_buffer on forked paths", "[graph][graph_buffer]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> result_mem{1};
int* result = result_mem.get();
constexpr int N = 10;
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::graph_memory_resource mr{cuda::device_ref{0}};
cudax::graph_buffer<int> buf(pb, mr, N, cuda::no_init);
// Write on one path
cudax::launch(pb, test::one_thread_dims, write_iota{}, buf);
// Fork: read from the buffer on a second path
auto read_path = cudax::start_path(g, pb);
cudax::launch(read_path, test::one_thread_dims, sum_to_ptr{}, buf, result);
// Join and free
pb.wait(read_path);
buf.destroy(pb);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
// sum of 0..9 = 45
REQUIRE(*result == 45);
}
C2H_TEST("graph_buffer move semantics", "[graph][graph_buffer]")
{
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::stream s{cuda::device_ref{0}};
cudax::graph_memory_resource mr{cuda::device_ref{0}};
cudax::graph_buffer<int> buf1(pb, mr, 4, cuda::no_init);
auto* original_data = buf1.data();
auto original_size = buf1.size();
// Move construct
cudax::graph_buffer<int> buf2(::cuda::std::move(buf1));
REQUIRE(buf2.data() == original_data);
REQUIRE(buf2.size() == original_size);
REQUIRE(buf1.data() == nullptr);
REQUIRE(buf1.size() == 0);
// Move assign — create a second buffer and move-assign over it
cudax::graph_buffer<int> buf3(pb, mr, 1, cuda::no_init);
buf3.destroy(pb); // free the dummy allocation before overwriting
buf3 = ::cuda::std::move(buf2);
REQUIRE(buf3.data() == original_data);
REQUIRE(buf3.size() == original_size);
buf3.destroy(pb);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
}
C2H_TEST("graph_buffer empty buffer", "[graph][graph_buffer]")
{
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::stream s{cuda::device_ref{0}};
cudax::graph_memory_resource mr{cuda::device_ref{0}};
cudax::graph_buffer<int> buf(pb, mr, 0, cuda::no_init);
REQUIRE(buf.data() == nullptr);
REQUIRE(buf.size() == 0);
REQUIRE(buf.empty());
// destroy on empty buffer should be a no-op
buf.destroy(pb);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
}
#endif // _CCCL_CTK_AT_LEAST(12, 2)

View File

@@ -1,555 +0,0 @@
//===----------------------------------------------------------------------===//
//
// 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) 2026 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
#include <cuda/std/__cccl/cuda_toolkit.h>
#if _CCCL_CTK_AT_LEAST(12, 2)
# include <cuda/experimental/graph.cuh>
# include <cuda/experimental/launch.cuh>
# include <cuda/experimental/stream.cuh>
# include <testing.cuh>
# include <utility.cuh>
namespace
{
namespace test
{
// ─── helpers ───────────────────────────────────────────────────────────────
// RAII wrapper around a pinned-memory allocation of N elements of type T.
template <typename T>
struct pinned_array
{
_malloc_pinned mem;
std::size_t n;
explicit pinned_array(std::size_t __n, T __init = T{})
: mem(__n * sizeof(T))
, n(__n)
{
for (std::size_t i = 0; i < n; ++i)
{
get()[i] = __init;
}
}
pinned_array(const pinned_array&) = delete;
pinned_array& operator=(const pinned_array&) = delete;
T* get() const noexcept
{
return mem.get_as<T>();
}
T& operator[](std::size_t i) const noexcept
{
return get()[i];
}
};
// ─── kernels used in conditional tests ─────────────────────────────────────
# if _CCCL_CTK_AT_LEAST(12, 4)
// Body kernel for while-loop conditional test: decrements a counter and
// stops the loop when it reaches zero.
struct count_down_and_stop
{
__device__ void operator()(cudax::conditional_handle handle, int* counter) const noexcept
{
--(*counter);
if (*counter <= 0)
{
handle.disable();
}
}
};
# endif // _CCCL_CTK_AT_LEAST(12, 4)
} // namespace test
} // namespace
// ────────────────────────────────────────────────────────────────────────────
// fill_bytes
// ────────────────────────────────────────────────────────────────────────────
C2H_TEST("graph fill_bytes sets every byte to the requested value", "[graph][fill_bytes]")
{
cudax::stream s{cuda::device_ref{0}};
constexpr std::size_t N = 64;
test::pinned_array<int> mem{N, static_cast<int>(0xDEADBEEF)};
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// Zero-fill via a graph memset node.
cudax::fill_bytes(pb, ::cuda::std::span{mem.get(), N}, ::cuda::std::uint8_t{0});
auto exec = g.instantiate();
exec.launch(s);
s.sync();
for (std::size_t i = 0; i < N; ++i)
{
REQUIRE(mem[i] == 0);
}
}
C2H_TEST("graph fill_bytes with non-zero value", "[graph][fill_bytes]")
{
cudax::stream s{cuda::device_ref{0}};
constexpr std::size_t N = 8;
test::pinned_array<unsigned char> mem{N};
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::fill_bytes(pb, ::cuda::std::span{mem.get(), N}, ::cuda::std::uint8_t{0xAB});
auto exec = g.instantiate();
exec.launch(s);
s.sync();
for (std::size_t i = 0; i < N; ++i)
{
REQUIRE(mem[i] == static_cast<unsigned char>(0xAB));
}
}
// ────────────────────────────────────────────────────────────────────────────
// copy_bytes
// ────────────────────────────────────────────────────────────────────────────
C2H_TEST("graph copy_bytes copies data from source to destination", "[graph][copy_bytes]")
{
cudax::stream s{cuda::device_ref{0}};
constexpr std::size_t N = 32;
test::pinned_array<int> src{N};
test::pinned_array<int> dst{N, -1};
for (std::size_t i = 0; i < N; ++i)
{
src[i] = static_cast<int>(i * 7);
}
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::copy_bytes(pb, ::cuda::std::span{src.get(), N}, ::cuda::std::span{dst.get(), N});
auto exec = g.instantiate();
exec.launch(s);
s.sync();
for (std::size_t i = 0; i < N; ++i)
{
REQUIRE(dst[i] == src[i]);
}
}
C2H_TEST("graph copy_bytes can be chained after fill_bytes", "[graph][fill_bytes][copy_bytes]")
{
cudax::stream s{cuda::device_ref{0}};
constexpr std::size_t N = 16;
test::pinned_array<unsigned char> src{N};
test::pinned_array<unsigned char> dst{N};
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// Fill source with 0xFF, then copy to destination.
cudax::fill_bytes(pb, ::cuda::std::span{src.get(), N}, ::cuda::std::uint8_t{0xFF});
cudax::copy_bytes(pb, ::cuda::std::span{src.get(), N}, ::cuda::std::span{dst.get(), N});
auto exec = g.instantiate();
exec.launch(s);
s.sync();
for (std::size_t i = 0; i < N; ++i)
{
REQUIRE(dst[i] == static_cast<unsigned char>(0xFF));
}
}
// ────────────────────────────────────────────────────────────────────────────
// host_launch
// ────────────────────────────────────────────────────────────────────────────
C2H_TEST("graph host_launch executes a lambda callback", "[graph][host_launch]")
{
cudax::stream s{cuda::device_ref{0}};
// pinned so the host-side increment is visible immediately after sync
test::pinned<int> counter{0};
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// Capture the pointer by value so the callback remains valid after graph build.
int* ptr = counter.get();
cudax::host_launch(pb, [ptr]() {
*ptr = 42;
});
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(*counter == 42);
}
C2H_TEST("graph host_launch with arguments", "[graph][host_launch]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned<int> a{10};
test::pinned<int> b{20};
test::pinned<int> result{0};
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
int* pa = a.get();
int* pb2 = b.get();
int* pr = result.get();
cudax::host_launch(
pb,
[](int* x, int* y, int* r) {
*r = *x + *y;
},
pa,
pb2,
pr);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(*result == 30);
}
C2H_TEST("graph host_launch can be chained with kernel nodes", "[graph][host_launch]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> mem{1};
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// Kernel sets value to 42.
int* ptr = mem.get();
cudax::launch(pb, test::one_thread_dims, test::assign_42{}, ptr);
// Host callback increments it.
cudax::host_launch(pb, [ptr]() {
*ptr += 1;
});
// Kernel verifies the final value is 43.
cudax::launch(pb, test::one_thread_dims, test::verify_n<43>{}, ptr);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(mem[0] == 43);
}
C2H_TEST("graph host_launch can be launched multiple times", "[graph][host_launch]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> mem{1};
int* ptr = mem.get();
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// Host callback increments the value each time.
cudax::host_launch(pb, [ptr]() {
*ptr += 1;
});
auto exec = g.instantiate();
// Launch 5 times — each launch should increment by 1.
for (int i = 0; i < 5; ++i)
{
exec.launch(s);
s.sync();
REQUIRE(mem[0] == i + 1);
}
}
C2H_TEST("graph host_launch data is cleaned up when graph is destroyed", "[graph][host_launch]")
{
// Use a shared_ptr as a witness: the weak_ptr expires when all copies are gone.
auto witness = ::std::make_shared<int>(42);
::std::weak_ptr<int> weak = witness;
{
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// The lambda captures a copy of the shared_ptr, which gets stored in the graph's user object.
cudax::host_launch(pb, [witness]() {
(void) witness;
});
// Release our copy — the graph's user object should keep the shared_ptr alive.
witness.reset();
REQUIRE(!weak.expired());
}
// graph_builder destroyed — user object destructor should have deleted the callback data,
// releasing the last shared_ptr copy.
REQUIRE(weak.expired());
}
// ────────────────────────────────────────────────────────────────────────────
// event record / wait
// ────────────────────────────────────────────────────────────────────────────
C2H_TEST("graph record_event and wait(event_ref) impose ordering across independent paths",
"[graph][event_record][event_wait]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> mem{1};
cuda::event ev{cuda::device_ref{0}};
cudax::graph_builder g;
// Path A: assign 42, then record the event.
int* val = mem.get();
cudax::path_builder path_a = cudax::start_path(g);
cudax::launch(path_a, test::one_thread_dims, test::assign_42{}, val);
path_a.record_event(ev);
// Path B (independent start): wait on the event, then verify value is 42.
cudax::path_builder path_b = cudax::start_path(g); // no deps from path_a
path_b.wait(ev);
cudax::launch(path_b, test::one_thread_dims, test::verify_42{}, val);
// Drain both paths.
path_a.wait(path_b);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(mem[0] == 42);
}
C2H_TEST("graph record_event node has the correct node type", "[graph][event_record]")
{
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cuda::event ev{cuda::device_ref{0}};
auto node = pb.record_event(ev);
REQUIRE(node.type() == cudax::graph_node_type::event_record);
}
C2H_TEST("graph wait(event_ref) node has the correct node type", "[graph][event_wait]")
{
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cuda::event ev{cuda::device_ref{0}};
auto node = pb.wait(ev);
REQUIRE(node.type() == cudax::graph_node_type::wait_event);
}
// ────────────────────────────────────────────────────────────────────────────
// child graph
// ────────────────────────────────────────────────────────────────────────────
C2H_TEST("graph insert_child_graph embeds a subgraph", "[graph][child_graph]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> mem{1};
int* val = mem.get();
// Build the child graph: kernel that assigns 42.
cudax::graph_builder child_g;
{
cudax::path_builder child_pb = cudax::start_path(child_g);
cudax::launch(child_pb, test::one_thread_dims, test::assign_42{}, val);
}
// Build the parent graph: embed the child, then verify.
cudax::graph_builder parent_g;
cudax::path_builder pb = cudax::start_path(parent_g);
cudax::insert_child_graph(pb, child_g);
cudax::launch(pb, test::one_thread_dims, test::verify_42{}, val);
auto exec = parent_g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(mem[0] == 42);
}
# if _CCCL_CTK_AT_LEAST(12, 9)
C2H_TEST("graph insert_child_graph with ownership transfer", "[graph][child_graph]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> mem{1};
int* val = mem.get();
cudax::graph_builder child_g;
{
cudax::path_builder child_pb = cudax::start_path(child_g);
cudax::launch(child_pb, test::one_thread_dims, test::assign_42{}, val);
}
cudax::graph_builder parent_g;
cudax::path_builder pb = cudax::start_path(parent_g);
// Move the child graph into the parent — child_g is null afterwards.
cudax::insert_child_graph(pb, std::move(child_g));
REQUIRE(child_g.get() == nullptr); // NOLINT(bugprone-use-after-move)
cudax::launch(pb, test::one_thread_dims, test::verify_42{}, val);
auto exec = parent_g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(mem[0] == 42);
}
# endif // _CCCL_CTK_AT_LEAST(12, 9)
C2H_TEST("graph insert_child_graph node has the correct node type", "[graph][child_graph]")
{
cudax::graph_builder child_g;
{
cudax::path_builder child_pb = cudax::start_path(child_g);
cudax::launch(child_pb, test::one_thread_dims, test::empty_kernel{});
}
cudax::graph_builder parent_g;
cudax::path_builder pb = cudax::start_path(parent_g);
auto node = cudax::insert_child_graph(pb, child_g);
REQUIRE(node.type() == cudax::graph_node_type::graph);
}
// ────────────────────────────────────────────────────────────────────────────
// conditional nodes (if / while)
// ────────────────────────────────────────────────────────────────────────────
# if _CCCL_CTK_AT_LEAST(12, 4)
C2H_TEST("graph make_if_node body executes when handle is non-zero", "[graph][conditional][if_node]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> mem{1};
int* val = mem.get();
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// Default value 1 → body executes.
auto [cond_node, body_graph, handle] = cudax::make_if_node(pb, /*__default_val=*/true);
// Populate the body graph: assign 42 to val.
{
cudax::path_builder body_pb = cudax::start_path(body_graph);
cudax::launch(body_pb, test::one_thread_dims, test::assign_42{}, val);
}
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(mem[0] == 42);
}
C2H_TEST("graph make_if_node body is skipped when handle is zero", "[graph][conditional][if_node]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> mem{1};
int* val = mem.get();
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// Default value 0 → body is skipped.
auto [cond_node, body_graph, handle] = cudax::make_if_node(pb, /*__default_val=*/false);
{
cudax::path_builder body_pb = cudax::start_path(body_graph);
cudax::launch(body_pb, test::one_thread_dims, test::assign_42{}, val);
}
auto exec = g.instantiate();
exec.launch(s);
s.sync();
// val should remain 0 because the body was skipped.
REQUIRE(mem[0] == 0);
}
C2H_TEST("graph make_while_node body executes the expected number of times", "[graph][conditional][while_node]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> mem{1, 5}; // will be decremented to 0
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// Default value 1 → loop runs as long as counter > 0.
auto [while_node, body_graph, handle] = cudax::make_while_node(pb);
// Body: decrement counter and stop when done.
{
cudax::path_builder body_pb = cudax::start_path(body_graph);
cudax::launch(body_pb, test::one_thread_dims, test::count_down_and_stop{}, handle, mem.get());
}
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(mem[0] == 0);
}
C2H_TEST("graph make_if_node with pre-constructed handle", "[graph][conditional][if_node]")
{
cudax::stream s{cuda::device_ref{0}};
test::pinned_array<int> mem{1};
int* val = mem.get();
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// User constructs handle directly.
cudax::conditional_handle my_handle{g, true};
auto [cond_node, body_graph, handle] = cudax::make_if_node(pb, my_handle);
{
cudax::path_builder body_pb = cudax::start_path(body_graph);
cudax::launch(body_pb, test::one_thread_dims, test::assign_42{}, val);
}
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(mem[0] == 42);
}
# endif // _CCCL_CTK_AT_LEAST(12, 4)
#endif // _CCCL_CTK_AT_LEAST(12, 2)

View File

@@ -1,309 +0,0 @@
//===----------------------------------------------------------------------===//
//
// 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) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
#include <cuda/experimental/graph.cuh>
#include <cuda/experimental/launch.cuh>
#include <cuda/experimental/memory_resource.cuh>
#include <cuda/experimental/stream.cuh>
#include <testing.cuh>
#include <utility.cuh>
namespace
{
// Empty node descriptor for testing
struct empty_node_descriptor
{
cuda::experimental::graph_node_ref __add_to_graph(cudaGraph_t graph, ::cuda::std::span<cudaGraphNode_t> deps) const
{
cudaGraphNode_t node;
_CCCL_TRY_CUDA_API(cudaGraphAddEmptyNode, "cudaGraphAddEmptyNode failed", &node, graph, deps.data(), deps.size());
return cuda::experimental::graph_node_ref{node, graph};
}
};
} // namespace
C2H_TEST("can default construct a graph and destroy it", "[graph]")
{
cuda::experimental::graph_builder g;
REQUIRE(g.get() != nullptr);
}
C2H_TEST("can create an empty node in a graph", "[graph]")
{
cuda::experimental::graph_builder g;
auto node = g.add(empty_node_descriptor{});
REQUIRE(node.get() != nullptr);
REQUIRE(node.type() == cuda::experimental::graph_node_type::empty);
}
C2H_TEST("can create multiple nodes and establish dependencies", "[graph]")
{
cuda::experimental::graph_builder g;
// Create three empty nodes
auto node1 = g.add(empty_node_descriptor{});
auto node2 = g.add(empty_node_descriptor{});
auto node3 = g.add(empty_node_descriptor{});
// Set up dependencies: node3 depends on node1 and node2
node3.depends_on(node1, node2);
// Verify the nodes exist
REQUIRE(node1.get() != nullptr);
REQUIRE(node2.get() != nullptr);
REQUIRE(node3.get() != nullptr);
// Verify node types
REQUIRE(node1.type() == cuda::experimental::graph_node_type::empty);
REQUIRE(node2.type() == cuda::experimental::graph_node_type::empty);
REQUIRE(node3.type() == cuda::experimental::graph_node_type::empty);
}
C2H_TEST("can instantiate and launch a graph", "[graph]")
{
cuda::experimental::graph_builder g;
// Create a simple graph with two nodes
auto node1 = g.add(empty_node_descriptor{});
auto node2 = g.add(empty_node_descriptor{});
node2.depends_on(node1);
// Instantiate the graph
auto exec = g.instantiate();
REQUIRE(exec.get() != nullptr);
// Create a stream and launch the graph
cuda::experimental::stream s{cuda::device_ref{0}};
exec.launch(s);
// Wait for completion
s.sync();
}
C2H_TEST("graph_node_ref comparison operators work correctly", "[graph]")
{
cuda::experimental::graph_builder g;
// Create two nodes
auto node1 = g.add(empty_node_descriptor{});
auto node2 = g.add(empty_node_descriptor{}, cuda::experimental::depends_on(node1.get()));
// Test equality operators
REQUIRE(node1 == node1);
REQUIRE(node1 != node2);
REQUIRE_FALSE(node1 == node2);
REQUIRE_FALSE(node1 != node1);
// Test null comparison
cuda::experimental::graph_node_ref null_ref;
REQUIRE_FALSE(node1 == null_ref);
REQUIRE(node1 != null_ref);
REQUIRE_FALSE(null_ref == node1);
REQUIRE(null_ref != node1);
}
C2H_TEST("graph_node_ref can be swapped", "[graph]")
{
cuda::experimental::graph_builder g;
// Create two nodes
auto node1 = g.add(empty_node_descriptor{});
auto node2 = g.add(empty_node_descriptor{}, cuda::experimental::depends_on(node1));
// Store original handles
auto node1_handle = node1.get();
auto node2_handle = node2.get();
// Swap the nodes
node1.swap(node2);
// Verify the handles were swapped
REQUIRE(node1.get() == node2_handle);
REQUIRE(node2.get() == node1_handle);
}
C2H_TEST("graph_node_ref can be copied", "[graph]")
{
cuda::experimental::graph_builder g;
// Create a node
auto node1 = g.add(empty_node_descriptor{});
auto node1_handle = node1.get();
// Move construct a new node
auto node2 = node1;
// Verify the handle was moved
REQUIRE(node2.get() == node1_handle);
REQUIRE(node1.get() == node1_handle);
// Test move assignment
auto node3 = g.add(empty_node_descriptor{});
node3 = std::move(node2);
// Verify the source node is still valid (moving a node ref does not zero out the source)
REQUIRE(node3.get() == node1_handle);
REQUIRE(node2.get() == node1_handle); // NOLINT(bugprone-use-after-move)
}
C2H_TEST("Path builder with kernel nodes", "[graph]")
{
cudax::stream s{cuda::device_ref{0}};
cuda::mr::legacy_managed_memory_resource mr{};
int* ptr = static_cast<int*>(mr.allocate_sync(sizeof(int)));
*ptr = 0;
SECTION("simple graph with kernel node")
{
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
// Create a kernel node
[[maybe_unused]] auto node = cudax::launch(pb, test::one_thread_dims, test::empty_kernel{});
auto exec = g.instantiate();
exec.launch(s);
s.sync();
}
SECTION("graph with a branching path")
{
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
[[maybe_unused]] auto node = cudax::launch(pb, test::one_thread_dims, test::assign_42{}, ptr);
auto node2 = cudax::launch(pb, test::one_thread_dims, test::verify_42{}, ptr);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(*ptr == 42);
*ptr = 0;
cudax::path_builder path1 = cudax::start_path(g, node2);
cudax::path_builder path2 = cudax::start_path(g, node2);
for (int i = 0; i < 10; ++i)
{
cudax::launch(path1, test::one_thread_dims, test::atomic_add_one{}, ptr);
}
for (int i = 0; i < 9; ++i)
{
cudax::launch(path2, test::one_thread_dims, test::atomic_sub_one{}, ptr);
}
REQUIRE(path1.get_dependencies()[0] != path2.get_dependencies()[0]);
path1.wait(path2);
cudax::launch(path1, test::one_thread_dims, test::verify_n<43>{}, ptr);
auto exec2 = g.instantiate();
exec2.launch(s);
s.sync();
REQUIRE(*ptr == 43);
}
SECTION("many branching paths joining")
{
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::launch(pb, test::one_thread_dims, test::assign_42{}, ptr);
auto node = cudax::launch(start_path(g, pb), test::one_thread_dims, test::atomic_add_one{}, ptr);
auto node2 = cudax::launch(start_path(g, pb), test::one_thread_dims, test::atomic_add_one{}, ptr);
auto node3 = cudax::launch(start_path(g, pb), test::one_thread_dims, test::atomic_add_one{}, ptr);
auto node4 = cudax::launch(start_path(g, pb), test::one_thread_dims, test::atomic_add_one{}, ptr);
auto node5 = cudax::launch(start_path(g, pb), test::one_thread_dims, test::atomic_add_one{}, ptr);
auto node6 = cudax::launch(start_path(g, pb), test::one_thread_dims, test::atomic_add_one{}, ptr);
auto another_path_builder = cudax::start_path(g, pb);
cudax::launch(another_path_builder, test::one_thread_dims, test::atomic_add_one{}, ptr);
cudax::launch(another_path_builder, test::one_thread_dims, test::atomic_add_one{}, ptr);
cudax::launch(another_path_builder, test::one_thread_dims, test::atomic_add_one{}, ptr);
cudax::launch(another_path_builder, test::one_thread_dims, test::atomic_add_one{}, ptr);
cudax::launch(another_path_builder, test::one_thread_dims, test::atomic_add_one{}, ptr);
cudax::launch(another_path_builder, test::one_thread_dims, test::atomic_add_one{}, ptr);
auto join_path_builder = cudax::start_path(g, node, node2, node3, another_path_builder);
join_path_builder.depends_on(node4, node5, node6);
REQUIRE(join_path_builder.get_dependencies().size() == 7);
cudax::launch(join_path_builder, test::one_thread_dims, test::verify_n<54>{}, ptr);
REQUIRE(g.node_count() == 14);
REQUIRE(join_path_builder.get_dependencies().size() == 1);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(*ptr == 54);
}
#if _CCCL_CTK_AT_LEAST(12, 3)
SECTION("legacy stream capture")
{
cudax::graph_builder g;
cudax::path_builder pb = cudax::start_path(g);
cudax::launch(pb, test::one_thread_dims, test::assign_42{}, ptr);
pb.legacy_stream_capture(s, [ptr](cudaStream_t stream) {
cudax::launch(stream, test::one_thread_dims, test::verify_42{}, ptr);
cudax::launch(stream, test::one_thread_dims, test::atomic_add_one{}, ptr);
});
s.sync();
REQUIRE(*ptr == 0);
cudax::launch(pb, test::one_thread_dims, test::atomic_add_one{}, ptr);
cudax::launch(pb, test::one_thread_dims, test::verify_n<44>{}, ptr);
REQUIRE(g.node_count() == 5);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(*ptr == 44);
}
#endif // _CCCL_CTK_AT_LEAST(12, 3)
if (cuda::devices.size() > 1)
{
SECTION("Multi-device graph")
{
cuda::device_memory_pool_ref dev0_mr = cuda::device_default_memory_pool(cuda::devices[0]);
int* dev0_ptr = static_cast<int*>(dev0_mr.allocate_sync(sizeof(int)));
cuda::device_memory_pool_ref dev1_mr = cuda::device_default_memory_pool(cuda::devices[1]);
int* dev1_ptr = static_cast<int*>(dev1_mr.allocate_sync(sizeof(int)));
cudax::graph_builder g(cuda::devices[0]);
cudax::path_builder dev0_pb = cudax::start_path(g);
cudax::launch(dev0_pb, test::one_thread_dims, test::assign_42{}, dev0_ptr);
cudax::launch(dev0_pb, test::one_thread_dims, test::assign_42{}, ptr);
cudax::path_builder dev1_pb = cudax::start_path(cuda::devices[1], dev0_pb);
cudax::launch(dev1_pb, test::one_thread_dims, test::assign_42{}, dev1_ptr);
cudax::launch(dev1_pb, test::one_thread_dims, test::verify_42{}, ptr);
cudax::launch(dev1_pb, test::one_thread_dims, test::atomic_add_one{}, ptr);
cudax::path_builder back_to_dev0 = cudax::start_path(cuda::devices[0], dev1_pb);
cudax::launch(back_to_dev0, test::one_thread_dims, test::verify_n<43>{}, ptr);
cudax::launch(back_to_dev0, test::one_thread_dims, test::verify_42{}, dev0_ptr);
REQUIRE(g.node_count() == 7);
auto exec = g.instantiate();
exec.launch(s);
s.sync();
REQUIRE(*ptr == 43);
dev0_mr.deallocate_sync(dev0_ptr, sizeof(int));
dev1_mr.deallocate_sync(dev1_ptr, sizeof(int));
}
}
mr.deallocate_sync(ptr, sizeof(int));
}