[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,86 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
#include <iostream>
/*
* The goal of this test is to ensure that using read access modes actually
* results in concurrent tasks
*/
using namespace cuda::experimental::stf;
static __global__ void cuda_sleep_kernel(long long int clock_cnt)
{
long long int start_clock = clock64();
long long int clock_offset = 0;
while (clock_offset < clock_cnt)
{
clock_offset = clock64() - start_clock;
}
}
int main(int argc, char** argv)
{
int NTASKS = 256;
int ms = 40;
if (argc > 1)
{
NTASKS = atoi(argv[1]);
}
if (argc > 2)
{
ms = atoi(argv[2]);
}
// cudaDevAttrClockRate: Peak clock frequency in kilohertz;
int clock_rate;
cuda_safe_call(cudaDeviceGetAttribute(&clock_rate, cudaDevAttrClockRate, 0));
long long int clock_cnt = (long long int) (ms * clock_rate);
graph_ctx ctx;
int dummy[1];
auto handle = ctx.logical_data(dummy);
ctx.task(handle.rw())->*[](cudaGraph_t graph, auto /*unused*/) {
cudaGraphNode_t n;
cuda_safe_call(cudaGraphAddEmptyNode(&n, graph, nullptr, 0));
};
for (int iter = 0; iter < 10; iter++)
{
for (int k = 0; k < NTASKS; k++)
{
ctx.task(handle.read())->*[&](cudaStream_t stream, auto /*unused*/) {
cuda_sleep_kernel<<<1, 1, 0, stream>>>(clock_cnt);
};
}
ctx.task(handle.rw())->*[&](cudaGraph_t graph, auto /*unused*/) {
cudaGraphNode_t n;
cuda_safe_call(cudaGraphAddEmptyNode(&n, graph, nullptr, 0));
};
}
ctx.submit();
if (argc > 3)
{
std::cout << "Generating DOT output in " << argv[3] << '\n';
ctx.print_to_dot(argv[3]);
}
ctx.finalize();
}

View File

@@ -1,56 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief Test explicit uses of the API to change stage and create a sequence
* of CUDA graphs
*/
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
using namespace cuda::experimental::stf;
int main()
{
graph_ctx ctx;
const size_t N = 8;
const size_t NITER = 2;
double A[N];
for (size_t i = 0; i < N; i++)
{
A[i] = 1.0 * i;
}
auto lA = ctx.logical_data(A);
for (size_t k = 0; k < NITER; k++)
{
ctx.parallel_for(blocked_partition(), exec_place::current_device(), lA.shape(), lA.rw())
->*[] __host__ __device__(size_t i, slice<double> A) { A(i) = cos(A(i)); };
ctx.change_stage();
}
ctx.finalize();
for (size_t i = 0; i < N; i++)
{
double Ai_ref = 1.0 * i;
for (size_t k = 0; k < NITER; k++)
{
Ai_ref = cos(Ai_ref);
}
EXPECT(fabs(A[i] - Ai_ref) < 0.01);
}
}

View File

@@ -1,57 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//! @file
//! @brief Add tasks to a user-provided graph
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
using namespace cuda::experimental::stf;
__global__ void dummy() {}
int main()
{
cudaGraph_t graph;
cudaGraphExec_t graphExec = NULL;
cudaStream_t stream;
cuda_safe_call(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
cuda_safe_call(cudaGraphCreate(&graph, 0));
graph_ctx ctx(graph);
auto lX = ctx.token();
auto lY = ctx.token();
auto lZ = ctx.token();
ctx.task(lX.write())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task(lX.read(), lY.write())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task(lX.read(), lZ.write())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task(lY.rw(), lZ.rw())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.finalize_as_graph();
cuda_safe_call(cudaGraphInstantiate(&graphExec, graph, NULL, NULL, 0));
cuda_safe_call(cudaGraphLaunch(graphExec, stream));
cuda_safe_call(cudaStreamSynchronize(stream));
}

View File

@@ -1,57 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//! @file
//! @brief Add tasks to a user-provided graph and launch on a user-provided stream.
//! Exercises graph_ctx(cudaGraph_t, cudaStream_t): finalize() submits the
//! graph on the given stream and does not block; the caller synchronizes.
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
using namespace cuda::experimental::stf;
__global__ void dummy() {}
int main()
{
cudaGraph_t graph;
cudaStream_t stream;
cuda_safe_call(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
cuda_safe_call(cudaGraphCreate(&graph, 0));
graph_ctx ctx(graph, stream);
auto lX = ctx.token();
auto lY = ctx.token();
auto lZ = ctx.token();
ctx.task(lX.write())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task(lX.read(), lY.write())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task(lX.read(), lZ.write())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task(lY.rw(), lZ.rw())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
// Non-blocking: submits the graph on the user-provided stream
ctx.finalize();
cuda_safe_call(cudaStreamSynchronize(stream));
cuda_safe_call(cudaStreamDestroy(stream));
}

View File

@@ -1,92 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//! @file
//! @brief Add tasks to a user-provided child graph from a while loop
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
using namespace cuda::experimental::stf;
#if _CCCL_CTK_AT_LEAST(12, 4)
__global__ void dummy() {}
__global__ void setHandle(cudaGraphConditionalHandle handle)
{
static int count = 5;
cudaGraphSetConditional(handle, --count ? 1 : 0);
}
#endif // _CCCL_CTK_AT_LEAST(12, 4)
int main()
{
#if _CCCL_CTK_BELOW(12, 4)
fprintf(stderr, "Waiving test: conditional nodes are only available since CUDA 12.4.\n");
#else
cudaStream_t stream;
cuda_safe_call(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
cudaGraph_t graph;
cudaGraphNode_t conditionalNode;
cudaGraphCreate(&graph, 0);
cudaGraphConditionalHandle handle;
cudaGraphConditionalHandleCreate(&handle, graph, 1, cudaGraphCondAssignDefault);
cudaGraphNodeParams cParams = {};
cParams.type = cudaGraphNodeTypeConditional;
cParams.conditional.handle = handle;
cParams.conditional.type = cudaGraphCondTypeWhile;
cParams.conditional.size = 1;
# if _CCCL_CTK_AT_LEAST(13, 0)
cudaGraphAddNode(&conditionalNode, graph, nullptr, nullptr, 0, &cParams);
# else
cudaGraphAddNode(&conditionalNode, graph, nullptr, 0, &cParams);
# endif
cudaGraph_t bodyGraph = cParams.conditional.phGraph_out[0];
graph_ctx ctx(bodyGraph);
auto lX = ctx.token();
auto lY = ctx.token();
auto lZ = ctx.token();
ctx.cuda_kernel(lX.write())->*[]() {
return cuda_kernel_desc{dummy, 1, 1, 0};
};
ctx.cuda_kernel(lX.read(), lY.write())->*[]() {
return cuda_kernel_desc{dummy, 1, 1, 0};
};
ctx.cuda_kernel(lX.read(), lZ.write())->*[]() {
return cuda_kernel_desc{dummy, 1, 1, 0};
};
ctx.cuda_kernel(lY.rw(), lZ.rw())->*[]() {
return cuda_kernel_desc{dummy, 1, 1, 0};
};
ctx.cuda_kernel()->*[handle]() {
return cuda_kernel_desc{setHandle, 1, 1, 0, handle};
};
ctx.finalize_as_graph();
cudaGraphExec_t graphExec = NULL;
cuda_safe_call(cudaGraphInstantiate(&graphExec, graph, NULL, NULL, 0));
cuda_safe_call(cudaGraphLaunch(graphExec, stream));
cuda_safe_call(cudaStreamSynchronize(stream));
cuda_safe_call(cudaGraphDebugDotPrint(graph, "test-while.dot", cudaGraphDebugDotFlags(0)));
#endif // !_CCCL_CTK_BELOW(12, 4)
}

View File

@@ -1,92 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//! @file
//! @brief Add tasks to a user-provided child graph from a while loop
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
using namespace cuda::experimental::stf;
#if _CCCL_CTK_AT_LEAST(12, 4)
__global__ void dummy() {}
__global__ void setHandle(cudaGraphConditionalHandle handle)
{
static int count = 5;
cudaGraphSetConditional(handle, --count ? 1 : 0);
}
#endif // _CCCL_CTK_AT_LEAST(12, 4)
int main()
{
#if _CCCL_CTK_BELOW(12, 4)
fprintf(stderr, "Waiving test: conditional nodes are only available since CUDA 12.4.\n");
#else
cudaStream_t stream;
cuda_safe_call(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
cudaGraph_t graph;
cudaGraphNode_t conditionalNode;
cudaGraphCreate(&graph, 0);
cudaGraphConditionalHandle handle;
cudaGraphConditionalHandleCreate(&handle, graph, 1, cudaGraphCondAssignDefault);
cudaGraphNodeParams cParams = {};
cParams.type = cudaGraphNodeTypeConditional;
cParams.conditional.handle = handle;
cParams.conditional.type = cudaGraphCondTypeWhile;
cParams.conditional.size = 1;
# if _CCCL_CTK_AT_LEAST(13, 0)
cudaGraphAddNode(&conditionalNode, graph, nullptr, nullptr, 0, &cParams);
# else
cudaGraphAddNode(&conditionalNode, graph, nullptr, 0, &cParams);
# endif
cudaGraph_t bodyGraph = cParams.conditional.phGraph_out[0];
graph_ctx ctx(bodyGraph);
auto lX = ctx.token();
auto lY = ctx.token();
auto lZ = ctx.token();
ctx.task(lX.write())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task(lX.read(), lY.write())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task(lX.read(), lZ.write())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task(lY.rw(), lZ.rw())->*[](cudaStream_t s) {
dummy<<<1, 1, 0, s>>>();
};
ctx.task()->*[&handle](cudaStream_t s) {
setHandle<<<1, 1, 0, s>>>(handle);
};
ctx.finalize_as_graph();
cudaGraphExec_t graphExec = NULL;
cuda_safe_call(cudaGraphInstantiate(&graphExec, graph, NULL, NULL, 0));
cuda_safe_call(cudaGraphLaunch(graphExec, stream));
cuda_safe_call(cudaStreamSynchronize(stream));
cuda_safe_call(cudaGraphDebugDotPrint(graph, "test-while.dot", cudaGraphDebugDotFlags(0)));
#endif // !_CCCL_CTK_BELOW(12, 4)
}

View File

@@ -1,74 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
*
* @brief Ensure temporary data are destroyed
*
*/
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
#include <cuda/experimental/__stf/stream/stream_ctx.cuh>
using namespace cuda::experimental::stf;
int X0(int i)
{
return 17 * i + 45;
}
__global__ void dummy() {}
int main()
{
stream_ctx ctx;
const int N = 16;
int X[N];
for (int i = 0; i < N; i++)
{
X[i] = X0(i);
}
auto lX = ctx.logical_data(X);
auto fX = ctx.freeze(lX, access_mode::rw, data_place::current_device());
auto stream = ctx.pick_stream();
graph_ctx gctx(stream);
auto frozen_X = fX.get(data_place::current_device(), stream);
auto lX_alias = gctx.logical_data(frozen_X, data_place::current_device());
auto lY = gctx.logical_data(lX.shape());
gctx.parallel_for(lX.shape(), lX_alias.read(), lY.write())->*[] __device__(size_t i, auto x, auto y) {
y(i) = x(i);
};
gctx.parallel_for(lX.shape(), lX_alias.write(), lY.read())->*[] __device__(size_t i, auto x, auto y) {
x(i) = y(i) + 2;
};
gctx.finalize();
fX.unfreeze(stream);
ctx.host_launch(lX.read())->*[](auto x) {
for (int i = 0; i < static_cast<int>(x.size()); i++)
{
EXPECT(x(i) == X0(i) + 2);
}
};
ctx.finalize();
}

View File

@@ -1,96 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//! \file
//!
//! \brief Freeze a logical data in a graph to use it in a child graph
#include <cuda/experimental/stf.cuh>
#include <vector>
using namespace cuda::experimental::stf;
int X0(int i)
{
return 17 * i + 45;
}
__global__ void dummy() {}
int main()
{
const int N = 16;
int X[N];
for (int i = 0; i < N; i++)
{
X[i] = X0(i);
}
graph_ctx ctx;
auto lX = ctx.logical_data(X);
ctx.parallel_for(lX.shape(), lX.rw())->*[] __device__(size_t i, auto x) {
x(i) *= 3;
};
auto fX = ctx.freeze(lX, access_mode::rw, data_place::current_device());
// Create a graph that will later be inserted as a child graph once all input
// dependencies are known
cudaGraph_t sub_graph;
cuda_safe_call(cudaGraphCreate(&sub_graph, 0));
// Create a context based on this child graph
graph_ctx sub_ctx(sub_graph);
auto [frozen_X, fX_get_events] = fX.get(data_place::current_device());
auto lX_alias = sub_ctx.logical_data(frozen_X, data_place::current_device());
// XXX we need an adapter to allocate data from the upper context
// auto lY = sub_ctx.logical_data(lX.shape());
// sub_ctx.parallel_for(lX.shape(), lX_alias.read(), lY.write())->*[] __device__(size_t i, auto x, auto y) {
// y(i) = x(i);
// };
sub_ctx.parallel_for(lX.shape(), lX_alias.rw())->*[] __device__(size_t i, auto x) {
x(i) = x(i) + 2;
};
sub_ctx.finalize_as_graph();
// The child graph depends on the events to get the frozen data
::std::vector<cudaGraphNode_t> fX_ready_nodes = reserved::join_with_graph_nodes(ctx, fX_get_events, ctx.stage());
// Add the child graph as a node that depends on the frozen data being ready
cudaGraphNode_t child_graph_node;
cuda_safe_call(cudaGraphAddChildGraphNode(
&child_graph_node, ctx.get_graph(), fX_ready_nodes.data(), fX_ready_nodes.size(), sub_ctx.get_graph()));
// Create an event that signals when the child graph completes
event_list child_graph_event;
reserved::fork_from_graph_node(
ctx, child_graph_node, ctx.get_graph(), ctx.stage(), child_graph_event, "child graph done");
// Unfreeze the data after the child graph completes
fX.unfreeze(child_graph_event);
ctx.host_launch(lX.read())->*[](auto x) {
for (int i = 0; i < static_cast<int>(x.size()); i++)
{
EXPECT(x(i) == 3 * X0(i) + 2);
}
};
ctx.finalize();
}

View File

@@ -1,96 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//! \file
//!
//! \brief Freeze a logical data in a graph to use it in a child graph
#include <cuda/experimental/stf.cuh>
#include <vector>
using namespace cuda::experimental::stf;
int X0(int i)
{
return 17 * i + 45;
}
__global__ void dummy() {}
int main()
{
const int N = 16;
int X[N];
for (int i = 0; i < N; i++)
{
X[i] = X0(i);
}
graph_ctx ctx;
auto lX = ctx.logical_data(X);
ctx.parallel_for(lX.shape(), lX.rw())->*[] __device__(size_t i, auto x) {
x(i) *= 3;
};
auto fX = ctx.freeze(lX, access_mode::rw, data_place::current_device());
// Create a graph that will later be inserted as a child graph once all input
// dependencies are known
cudaGraph_t sub_graph;
cuda_safe_call(cudaGraphCreate(&sub_graph, 0));
// Create a context based on this child graph
graph_ctx sub_ctx(sub_graph);
auto [frozen_X, fX_get_events] = fX.get(data_place::current_device());
auto lX_alias = sub_ctx.logical_data(frozen_X, data_place::current_device());
// XXX we need an adapter to allocate data from the upper context
// auto lY = sub_ctx.logical_data(lX.shape());
// sub_ctx.parallel_for(lX.shape(), lX_alias.read(), lY.write())->*[] __device__(size_t i, auto x, auto y) {
// y(i) = x(i);
// };
sub_ctx.parallel_for(lX.shape(), lX_alias.rw())->*[] __device__(size_t i, auto x) {
x(i) = x(i) + 2;
};
sub_ctx.finalize_as_graph();
// The child graph depends on the events to get the frozen data
::std::vector<cudaGraphNode_t> fX_ready_nodes = reserved::join_with_graph_nodes(ctx, fX_get_events, ctx.stage());
// Add the child graph as a node that depends on the frozen data being ready
cudaGraphNode_t child_graph_node;
cuda_safe_call(cudaGraphAddChildGraphNode(
&child_graph_node, ctx.get_graph(), fX_ready_nodes.data(), fX_ready_nodes.size(), sub_ctx.get_graph()));
// Create an event that signals when the child graph completes
event_list child_graph_event;
reserved::fork_from_graph_node(
ctx, child_graph_node, ctx.get_graph(), ctx.stage(), child_graph_event, "child graph done");
// Unfreeze the data after the child graph completes
fX.unfreeze(child_graph_event);
ctx.host_launch(lX.read())->*[](auto x) {
for (int i = 0; i < static_cast<int>(x.size()); i++)
{
EXPECT(x(i) == 3 * X0(i) + 2);
}
};
ctx.finalize();
}

View File

@@ -1,127 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//! \file
//!
//! \brief Freeze a logical data in a graph to use it in the body of a "while" graph node
#include <cuda/experimental/stf.cuh>
#include <vector>
using namespace cuda::experimental::stf;
#if _CCCL_CTK_AT_LEAST(12, 4)
int X0(int i)
{
return 17 * i + 45;
}
__global__ void setHandle(cudaGraphConditionalHandle handle)
{
static int count = 5;
cudaGraphSetConditional(handle, --count ? 1 : 0);
}
#endif // _CCCL_CTK_AT_LEAST(12, 4)
int main()
{
#if _CCCL_CTK_BELOW(12, 4)
fprintf(stderr, "Waiving test: conditional nodes are only available since CUDA 12.4.\n");
#else
const int N = 16;
int X[N];
for (int i = 0; i < N; i++)
{
X[i] = X0(i);
}
graph_ctx ctx;
auto lX = ctx.logical_data(X);
ctx.parallel_for(lX.shape(), lX.rw())->*[] __device__(size_t i, auto x) {
x(i) *= 3;
};
auto fX = ctx.freeze(lX, access_mode::rw, data_place::current_device());
cudaGraphConditionalHandle handle;
cudaGraphConditionalHandleCreate(&handle, ctx.get_graph(), 1, cudaGraphCondAssignDefault);
// Create a graph that will later be inserted as a child graph once all input
// dependencies are known
cudaGraph_t sub_graph;
cuda_safe_call(cudaGraphCreate(&sub_graph, 0));
// Create a context based on this child graph which is the body of the
graph_ctx sub_ctx(sub_graph);
auto [frozen_X, fX_get_events] = fX.get(data_place::current_device());
auto lX_alias = sub_ctx.logical_data(frozen_X, data_place::current_device());
sub_ctx.parallel_for(lX.shape(), lX_alias.rw())->*[] __device__(size_t i, auto x) {
x(i) = x(i) + 2;
};
// We want to repeat this a fixed number of times
sub_ctx.cuda_kernel()->*[handle]() {
return cuda_kernel_desc{setHandle, 1, 1, 0, handle};
};
sub_ctx.finalize_as_graph();
// We now create a conditional graph which depends on the same dependencies
// as the inner ctx. We then insert the body of the graph as a child graph of
// the conditional node because we cannot decide what graph is the body of
// the conditional node ourselves, and we cannot add input dependencies to
// the conditional node after it was added.
// The child graph depends on the events to get the frozen data
::std::vector<cudaGraphNode_t> fX_ready_nodes = reserved::join_with_graph_nodes(ctx, fX_get_events, ctx.stage());
cudaGraphNodeParams cParams = {};
cParams.type = cudaGraphNodeTypeConditional;
cParams.conditional.handle = handle;
cParams.conditional.type = cudaGraphCondTypeWhile;
cParams.conditional.size = 1;
cudaGraphNode_t conditionalNode;
# if _CCCL_CTK_AT_LEAST(13, 0)
cudaGraphAddNode(&conditionalNode, ctx.get_graph(), fX_ready_nodes.data(), nullptr, fX_ready_nodes.size(), &cParams);
# else
cudaGraphAddNode(&conditionalNode, ctx.get_graph(), fX_ready_nodes.data(), fX_ready_nodes.size(), &cParams);
# endif
cudaGraph_t bodyGraph = cParams.conditional.phGraph_out[0];
// A child graph contains the entire body
cudaGraphNode_t child_graph_node;
cuda_safe_call(cudaGraphAddChildGraphNode(&child_graph_node, bodyGraph, nullptr, 0, sub_ctx.get_graph()));
// Create an event that depends on the conditional node, so that we unfreeze
// after the completion of the while loop
event_list child_graph_event;
reserved::fork_from_graph_node(
ctx, conditionalNode, ctx.get_graph(), ctx.stage(), child_graph_event, "child graph done");
fX.unfreeze(child_graph_event);
ctx.host_launch(lX.read())->*[](auto x) {
for (int i = 0; i < static_cast<int>(x.size()); i++)
{
EXPECT(x(i) == 3 * X0(i) + 2 * 5);
}
};
ctx.finalize();
#endif // !_CCCL_CTK_BELOW(12, 4)
}

View File

@@ -1,128 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//! \file
//!
//! \brief Freeze a logical data in a graph to use it in the body of a "while" graph node, the resulting looping graph
//! will be executed within a stream context.
#include <cuda/experimental/stf.cuh>
#include <vector>
using namespace cuda::experimental::stf;
#if _CCCL_CTK_AT_LEAST(12, 4)
int X0(int i)
{
return 17 * i + 45;
}
__global__ void setHandle(cudaGraphConditionalHandle handle)
{
static int count = 5;
cudaGraphSetConditional(handle, --count ? 1 : 0);
}
#endif // _CCCL_CTK_AT_LEAST(12, 4)
int main()
{
#if _CCCL_CTK_BELOW(12, 4)
fprintf(stderr, "Skipping test: conditional nodes are only available since CUDA 12.4.\n");
#else
const int N = 16;
int X[N];
for (int i = 0; i < N; i++)
{
X[i] = X0(i);
}
stream_ctx ctx;
auto lX = ctx.logical_data(X);
ctx.parallel_for(lX.shape(), lX.rw())->*[] __device__(size_t i, auto x) {
x(i) *= 3;
};
/* We are going to create a local context which is a graph, and we will populate it using a graph_ctx */
cudaGraph_t graph;
cuda_safe_call(cudaGraphCreate(&graph, 0));
cudaGraphConditionalHandle handle;
cudaGraphConditionalHandleCreate(&handle, graph, 1, cudaGraphCondAssignDefault);
// Create a graph that will later be inserted as a child graph once all input
// dependencies are known
cudaGraph_t sub_graph;
cuda_safe_call(cudaGraphCreate(&sub_graph, 0));
cudaGraphNodeParams cParams = {};
cParams.type = cudaGraphNodeTypeConditional;
cParams.conditional.handle = handle;
cParams.conditional.type = cudaGraphCondTypeWhile;
cParams.conditional.size = 1;
cudaGraphNode_t conditionalNode;
// There is no input dependency because they are implied by graph launch
# if _CCCL_CTK_AT_LEAST(13, 0)
cuda_safe_call(cudaGraphAddNode(&conditionalNode, graph, nullptr, nullptr, 0, &cParams));
# else
cuda_safe_call(cudaGraphAddNode(&conditionalNode, graph, nullptr, 0, &cParams));
# endif
cudaGraph_t bodyGraph = cParams.conditional.phGraph_out[0];
// Create a context based on this child graph which is the body of the
graph_ctx sub_ctx(bodyGraph);
auto fX = ctx.freeze(lX, access_mode::rw, data_place::current_device());
auto [frozen_X, fX_get_events] = fX.get(data_place::current_device());
auto lX_alias = sub_ctx.logical_data(frozen_X, data_place::current_device());
sub_ctx.parallel_for(lX.shape(), lX_alias.rw())->*[] __device__(size_t i, auto x) {
x(i) = x(i) + 2;
};
// We want to repeat this a fixed number of times
sub_ctx.cuda_kernel()->*[handle]() {
return cuda_kernel_desc{setHandle, 1, 1, 0, handle};
};
sub_ctx.finalize_as_graph();
// The sub graph depends on the events to get the frozen data, so the
// launch of the graph will depend on them
cudaGraphExec_t graph_exec = NULL;
cuda_safe_call(cudaGraphInstantiate(&graph_exec, graph, NULL, NULL, 0));
auto support_dstream = ctx.pick_dstream();
/* auto before_launch = */ reserved::join_with_stream(ctx, support_dstream, fX_get_events, "graph_launch", false);
cuda_safe_call(cudaGraphLaunch(graph_exec, support_dstream.stream));
// We stop using the frozen logical data after then graph has been launched
auto graph_launched = reserved::record_event_in_stream(support_dstream);
fX.unfreeze(event_list(mv(graph_launched)));
ctx.host_launch(lX.read())->*[](auto x) {
for (int i = 0; i < static_cast<int>(x.size()); i++)
{
EXPECT(x(i) == 3 * X0(i) + 2 * 5);
}
};
ctx.finalize();
#endif // !_CCCL_CTK_BELOW(12, 4)
}

View File

@@ -1,146 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//! \file
//!
//! \brief Freeze a logical data in a graph to use it in the body of a "while" graph node, the resulting looping graph
//! will be executed within a stream context.
#include <cuda/experimental/stf.cuh>
#include <vector>
using namespace cuda::experimental::stf;
#if _CCCL_CTK_AT_LEAST(12, 4)
/**
* @brief Insert an existing CUDA graph node into a graph context with appropriate dependencies
*
* This function is designed for graph contexts and adds the provided graph node
* to the context's graph with dependencies from the input prerequisites.
*
* @tparam ctx_t Context type (must be a graph_ctx or context using graph_ctx under the hood)
* @param ctx The execution context (must be a graph context)
* @param node The existing CUDA graph node to insert
* @param input_prereqs Input dependencies that must be satisfied (must be graph events)
* @return event_list Events representing the completion of the graph node insertion
*/
template <typename ctx_t>
event_list insert_graph_node(ctx_t& ctx, cudaGraphNode_t node, event_list& input_prereqs)
{
cudaGraph_t support_graph = ctx.graph();
size_t graph_stage = ctx.stage();
::std::vector<cudaGraphNode_t> ready_nodes = reserved::join_with_graph_nodes(ctx, input_prereqs, graph_stage);
// Add dependencies from the ready_nodes to the existing node
if (!ready_nodes.empty())
{
# if _CCCL_CTK_AT_LEAST(13, 0)
cuda_safe_call(cudaGraphAddDependencies(support_graph, ready_nodes.data(), &node, nullptr, ready_nodes.size()));
# else // _CCCL_CTK_AT_LEAST(13, 0)
cuda_safe_call(cudaGraphAddDependencies(support_graph, ready_nodes.data(), &node, ready_nodes.size()));
# endif // _CCCL_CTK_AT_LEAST(13, 0)
}
// Create an event that depends on the inserted graph node
auto node_event = reserved::graph_event(node, graph_stage, support_graph);
node_event->set_symbol(ctx, "inserted_graph_node");
// Return the event list from that single event
return event_list(mv(node_event));
}
int X0(int i)
{
return 17 * i + 45;
}
__global__ void setHandle(cudaGraphConditionalHandle handle)
{
static int count = 5;
cudaGraphSetConditional(handle, --count ? 1 : 0);
}
#endif // _CCCL_CTK_AT_LEAST(12, 4)
int main()
{
#if _CCCL_CTK_BELOW(12, 4)
fprintf(stderr, "Waiving test: conditional nodes are only available since CUDA 12.4.\n");
#else
const int N = 16;
int X[N];
for (int i = 0; i < N; i++)
{
X[i] = X0(i);
}
graph_ctx ctx;
auto lX = ctx.logical_data(X);
ctx.parallel_for(lX.shape(), lX.rw())->*[] __device__(size_t i, auto x) {
x(i) *= 3;
};
cudaGraphConditionalHandle handle;
cuda_safe_call(cudaGraphConditionalHandleCreate(&handle, ctx.graph(), 1, cudaGraphCondAssignDefault));
cudaGraphNodeParams cParams = {};
cParams.type = cudaGraphNodeTypeConditional;
cParams.conditional.handle = handle;
cParams.conditional.type = cudaGraphCondTypeWhile;
cParams.conditional.size = 1;
cudaGraphNode_t conditionalNode;
// There is no input dependencies yet, we will add them later
# if _CCCL_CTK_AT_LEAST(13, 0)
cuda_safe_call(cudaGraphAddNode(&conditionalNode, ctx.graph(), nullptr, nullptr, 0, &cParams));
# else
cuda_safe_call(cudaGraphAddNode(&conditionalNode, ctx.graph(), nullptr, 0, &cParams));
# endif
cudaGraph_t bodyGraph = cParams.conditional.phGraph_out[0];
graph_ctx sub_ctx(bodyGraph);
auto fX = ctx.freeze(lX, access_mode::rw, data_place::current_device());
auto [frozen_X, fX_get_events] = fX.get(data_place::current_device());
auto lX_alias = sub_ctx.logical_data(frozen_X, data_place::current_device());
sub_ctx.parallel_for(lX.shape(), lX_alias.rw())->*[] __device__(size_t i, auto x) {
x(i) = x(i) + 2;
};
// We want to repeat this a fixed number of times
sub_ctx.cuda_kernel()->*[handle]() {
return cuda_kernel_desc{setHandle, 1, 1, 0, handle};
};
sub_ctx.finalize_as_graph();
event_list cond_graph_launched = insert_graph_node(ctx, conditionalNode, fX_get_events);
fX.unfreeze(mv(cond_graph_launched));
ctx.host_launch(lX.read())->*[](auto x) {
for (int i = 0; i < static_cast<int>(x.size()); i++)
{
EXPECT(x(i) == 3 * X0(i) + 2 * 5);
}
};
ctx.finalize();
#endif // !_CCCL_CTK_BELOW(12, 4)
}

View File

@@ -1,51 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief An example to query statistics about graph instantiation
*/
#include <cuda/experimental/stf.cuh>
using namespace cuda::experimental::stf;
int main()
{
async_resources_handle handle;
for (size_t i = 0; i < 10; i++)
{
graph_ctx ctx(handle);
auto lA = ctx.logical_data(shape_of<slice<size_t>>(64));
ctx.launch(lA.write())->*[] _CCCL_DEVICE(auto t, slice<size_t> A) {
for (auto i : t.apply_partition(shape(A)))
{
A(i) = 2 * i;
}
};
ctx.finalize();
// Query statistics about the graph context : the first iteration needs to
// instantiate the graph, then we will reuse graphs saved in the handle.
auto* st = ctx.graph_get_cache_stat();
if (i == 0)
{
EXPECT(st->instantiate_cnt == 1);
EXPECT(st->update_cnt == 0);
}
else
{
EXPECT(st->instantiate_cnt == 0);
EXPECT(st->update_cnt == 1);
}
// fprintf(stderr, "nnodes %ld nedges %ld\n", st->nnodes, st->nedges);
}
}

View File

@@ -1,57 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief An example to query statistics about graph instantiation
*/
#include <cuda/experimental/stf.cuh>
using namespace cuda::experimental::stf;
int main()
{
async_resources_handle handle;
for (size_t i = 0; i < 10; i++)
{
graph_ctx ctx(handle);
// If i is a multiple of 3 we enable the cache, the first iteration will fill the cache
ctx.set_graph_cache_policy([i]() {
return (i % 3) == 0;
});
auto lA = ctx.logical_data(shape_of<slice<size_t>>(64));
ctx.launch(lA.write())->*[] _CCCL_DEVICE(auto t, slice<size_t> A) {
for (auto i : t.apply_partition(shape(A)))
{
A(i) = 2 * i;
}
};
ctx.finalize();
// Query statistics about the graph context : the first iteration needs to
// instantiate the graph, then we will reuse graphs saved in the handle.
auto* st = ctx.graph_get_cache_stat();
// For the first iteration, or non multiple of 3 we have to instantiate, otherwise we should have a cache hit
if (i == 0 || (i % 3) != 0)
{
EXPECT(st->instantiate_cnt == 1);
EXPECT(st->update_cnt == 0);
}
else
{
EXPECT(st->instantiate_cnt == 0);
EXPECT(st->update_cnt == 1);
}
}
}

View File

@@ -1,68 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2026 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief Verify that graph_ctx tasks whose captured child graphs contain
* memory allocation/free nodes (from cudaMallocAsync) work correctly.
*
* Before the move-ownership fix, cudaGraphAddChildGraphNode (clone semantics)
* rejected such child graphs with CUDA_ERROR_NOT_SUPPORTED. CTK 13+ exposes
* cudaGraphChildGraphOwnershipMove via cudaGraphAddNode which transfers the
* child graph to the parent instead of cloning it.
*/
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
using namespace cuda::experimental::stf;
__global__ void fill_kernel(int* ptr, int n, int val)
{
int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid < n)
{
ptr[tid] = val;
}
}
int main()
{
#if _CCCL_CTK_BELOW(13, 0)
fprintf(stderr, "Waiving test: cudaGraphChildGraphOwnershipMove requires CTK 13+.\n");
#else
constexpr int N = 256;
int host_data[N];
for (int i = 0; i < N; i++)
{
host_data[i] = 0;
}
graph_ctx ctx;
auto ldata = ctx.logical_data(host_data);
// The lambda receives cudaStream_t, so graph_ctx uses stream capture.
// cudaMallocAsync/cudaFreeAsync on that stream produce mem-alloc/free
// graph nodes inside the captured child graph.
ctx.task(ldata.rw())->*[](cudaStream_t s, auto sdata) {
int* tmp = nullptr;
cuda_safe_call(cudaMallocAsync(&tmp, N * sizeof(int), s));
fill_kernel<<<(N + 255) / 256, 256, 0, s>>>(tmp, N, 42);
cuda_safe_call(cudaMemcpyAsync(sdata.data_handle(), tmp, N * sizeof(int), cudaMemcpyDeviceToDevice, s));
cuda_safe_call(cudaFreeAsync(tmp, s));
};
ctx.finalize();
for (int i = 0; i < N; i++)
{
assert(host_data[i] == 42);
}
#endif // !_CCCL_CTK_BELOW(13, 0)
}

View File

@@ -1,117 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief Generate a library call from nested CUDA graphs generated using algorithms
*/
#include <cuda/experimental/stf.cuh>
using namespace cuda::experimental::stf;
// Some fake library doing MATH
void libMATH(graph_ctx ctx, logical_data<slice<double>> x, logical_data<slice<double>> y)
{
// We only want to have kernels with 4 CTAs to stress the system
auto spec = par<4>(par<128>());
ctx.launch(spec, exec_place::current_device(), x.read(), y.write()).set_symbol("MATH1")->*
[] __device__(auto t, auto x, auto y) {
for (auto i : t.apply_partition(shape(x)))
{
y(i) = cos(cos(x(i)));
}
};
ctx.launch(spec, exec_place::current_device(), x.write(), y.read()).set_symbol("MATH2")->*
[] __device__(auto t, auto x, auto y) {
for (auto i : t.apply_partition(shape(x)))
{
x(i) = sin(sin(y(i)));
};
};
}
template <typename context_t>
void libMATH_AS_GRAPH(context_t& ctx, logical_data<slice<double>> x, logical_data<slice<double>> y)
{
static algorithm alg;
alg.run_as_task(libMATH, ctx, x.rw(), y.write());
}
// Some fake lib doing a SWAP
template <typename context_t>
void libSWAP(context_t& ctx, logical_data<slice<double>> x, logical_data<slice<double>> y)
{
// We only want to have kernels with 4 CTAs to stress the system
auto spec = par<4>(par<128>());
ctx.launch(spec, exec_place::current_device(), x.rw(), y.rw()).set_symbol("SWAP")->*
[] __device__(auto t, auto x, auto y) {
for (auto i : t.apply_partition(shape(x)))
{
auto tmp = x(i);
x(i) = y(i);
y(i) = tmp;
}
};
}
template <typename context_t>
logical_data<slice<double>> libCOPY(context_t& ctx, logical_data<slice<double>> x)
{
logical_data<slice<double>> res = ctx.logical_data(x.shape());
// We only want to have kernels with 4 CTAs to stress the system
auto spec = par<4>(par<128>());
ctx.launch(spec, exec_place::current_device(), x.read(), res.write()).set_symbol("SWAP")->*
[] __device__(auto t, auto x, auto res) {
for (auto i : t.apply_partition(shape(x)))
{
res(i) = x(i);
}
};
return res;
}
int main()
{
nvtx_range r("run");
stream_ctx ctx;
const size_t N = 256 * 1024;
const size_t K = 8;
logical_data<slice<double>> lX[K];
logical_data<slice<double>> lY[K];
for (size_t i = 0; i < K; i++)
{
lX[i] = ctx.logical_data<double>(N);
lY[i] = ctx.logical_data<double>(N);
ctx.parallel_for(lX[i].shape(), lX[i].write(), lY[i].write()).set_symbol("INIT")->*
[] __device__(size_t i, auto x, auto y) {
x(i) = 2.0 * i + 12.0;
y(i) = -3.0 * i + 17.0;
};
}
for (size_t i = 0; i < K; i++)
{
auto tmp = libCOPY(ctx, lX[i]);
libSWAP(ctx, tmp, lY[i]);
libMATH_AS_GRAPH(ctx, lX[i], lY[i]);
libSWAP(ctx, lX[i], lY[i]);
}
ctx.finalize();
}

View File

@@ -1,56 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief Test low level API of the graph context
*/
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
#include <iostream>
using namespace cuda::experimental::stf;
int main(int argc, char** argv)
{
graph_ctx ctx;
double X[1024], Y[1024];
auto handle_X = ctx.logical_data(X);
auto handle_Y = ctx.logical_data(Y);
for (int k = 0; k < 10; k++)
{
graph_task<> t = ctx.task();
t.add_deps(handle_X.rw());
t.start();
cudaGraphNode_t n;
cuda_safe_call(cudaGraphAddEmptyNode(&n, t.get_graph(), nullptr, 0));
t.end();
}
graph_task<> t2 = ctx.task();
t2.add_deps(handle_X.read(), handle_Y.rw());
t2.start();
cudaGraphNode_t n2;
cuda_safe_call(cudaGraphAddEmptyNode(&n2, t2.get_graph(), nullptr, 0));
t2.end();
ctx.submit();
if (argc > 1)
{
std::cout << "Generating DOT output in " << argv[1] << '\n';
ctx.print_to_dot(argv[1]);
}
ctx.finalize();
}

View File

@@ -1,61 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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.
//
//===----------------------------------------------------------------------===//
//! \file
//! \brief Test resource management for graphs launched multiple times
#include <cuda/experimental/__stf/internal/context.cuh>
#include <atomic>
using namespace cuda::experimental::stf;
int main()
{
cudaStream_t stream;
cuda_safe_call(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
// Test: Create a reusable graph that can be launched multiple times
// while properly managing resources across multiple executions
context ctx = graph_ctx(); // Generic context holding a graph_ctx
std::atomic<int> callback_count{0};
// Add work that creates resources (host_launch creates host callback resources)
ctx.host_launch()->*[&callback_count]() {
callback_count.fetch_add(1);
};
// Get reusable graph without finalizing context
::std::shared_ptr<cudaGraph_t> graph = ctx.to_graph_ctx().finalize_as_graph();
// Instantiate the graph for multiple launches
cudaGraphExec_t graphExec;
cuda_safe_call(cudaGraphInstantiate(&graphExec, *graph, nullptr, nullptr, 0));
// Launch the same graph multiple times - resources should be reused properly
const int num_launches = 3;
for (int i = 0; i < num_launches; i++)
{
cuda_safe_call(cudaGraphLaunch(graphExec, stream));
}
// Clean up resources after all graph executions
ctx.release_resources(stream);
cuda_safe_call(cudaStreamSynchronize(stream));
// Verify the callback was executed once per graph launch
EXPECT(callback_count.load() == num_launches);
// Clean up
cuda_safe_call(cudaGraphExecDestroy(graphExec));
cuda_safe_call(cudaStreamDestroy(stream));
return 0;
}

View File

@@ -1,60 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
*
* @brief Ensure temporary data are destroyed
*
*/
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
#include <cuda/experimental/__stf/stream/stream_ctx.cuh>
using namespace cuda::experimental::stf;
double X0(int i)
{
return sin((double) i);
}
__global__ void dummy() {}
int main()
{
// stream_ctx ctx;
graph_ctx ctx;
const int N = 16;
double X[N];
for (int i = 0; i < N; i++)
{
X[i] = X0(i);
}
auto lX = ctx.logical_data(X);
for (int i = 0; i < 10; i++)
{
// fprintf(stderr, "START loop %ld\n", i);
auto lY = ctx.logical_data(lX.shape());
lY.set_symbol("tmp" + std::to_string(i));
// fprintf(stderr, "START pfor %ld\n", i);
ctx.parallel_for(lX.shape(), lX.rw(), lY.write())->*[] _CCCL_DEVICE(size_t ind, auto dX, auto dY) {
dY(ind) = dX(ind);
dX(ind) = dY(ind) + 1.0;
};
// fprintf(stderr, "End loop %ld\n", i);
}
// fprintf(stderr, "OVER...\n");
ctx.finalize();
}

View File

@@ -1,34 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief Ensure we can create graph contexts many times
*/
#include <cuda/experimental/stf.cuh>
using namespace cuda::experimental::stf;
int main()
{
for (size_t i = 0; i < 10240; i++)
{
graph_ctx ctx;
auto lA = ctx.logical_data(shape_of<slice<size_t>>(64));
ctx.launch(lA.write())->*[] _CCCL_DEVICE(auto t, slice<size_t> A) {
for (auto i : t.apply_partition(shape(A)))
{
A(i) = 2 * i;
}
};
ctx.finalize();
}
}

View File

@@ -1,71 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief Ensure we can use multiple graph contexts simultaneously and launch them
*/
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
#include <iostream>
using namespace cuda::experimental::stf;
__global__ void dummy() {}
int main(int argc, char** argv)
{
graph_ctx ctx;
graph_ctx ctx_2;
double X[1024], Y[1024];
auto handle_X = ctx.logical_data(make_slice(X, 1024));
auto handle_Y = ctx.logical_data(make_slice(Y, 1024));
double Z[1024];
auto handle_Z = ctx_2.logical_data(make_slice(Z, 1024));
for (int k = 0; k < 10; k++)
{
ctx.task(handle_X.rw())->*[&](cudaStream_t s, auto /*unused*/) {
dummy<<<1, 1, 0, s>>>();
};
}
ctx.task(handle_X.read(), handle_Y.rw())->*[&](cudaStream_t s, auto /*unused*/, auto /*unused*/) {
dummy<<<1, 1, 0, s>>>();
};
ctx_2.task(handle_Z.rw())->*[&](cudaStream_t s, auto /*unused*/) {
dummy<<<1, 1, 0, s>>>();
};
cudaStream_t stream;
cuda_safe_call(cudaStreamCreate(&stream));
ctx.submit(stream);
ctx_2.submit(stream);
if (argc > 1)
{
std::cout << "Generating DOT output in " << argv[1] << '\n';
ctx.print_to_dot(argv[1]);
}
if (argc > 2)
{
std::cout << "Generating DOT output in " << argv[2] << '\n';
ctx_2.print_to_dot(argv[2]);
}
ctx.finalize();
ctx_2.finalize();
}

View File

@@ -1,53 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF 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) 2022-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief Ensure a graph context can be defined as a static variable
*/
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
#include <iostream>
using namespace cuda::experimental::stf;
// Static graph ctx
graph_ctx ctx;
__global__ void dummy() {}
int main(int argc, char** argv)
{
double X[1024], Y[1024];
auto handle_X = ctx.logical_data(X);
auto handle_Y = ctx.logical_data(Y);
for (int k = 0; k < 10; k++)
{
ctx.task(handle_X.rw())->*[&](cudaStream_t s, auto /*unused*/) {
dummy<<<1, 1, 0, s>>>();
};
}
ctx.task(handle_X.read(), handle_Y.rw())->*[&](cudaStream_t s, auto /*unused*/, auto /*unused*/) {
dummy<<<1, 1, 0, s>>>();
};
ctx.submit();
if (argc > 1)
{
std::cout << "Generating DOT output in " << argv[1] << '\n';
ctx.print_to_dot(argv[1]);
}
ctx.finalize();
}