[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:
@@ -1,64 +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 Computes the Degree Centrality for each vertex within a graph
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cuda/experimental/stf.cuh>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace cuda::experimental::stf;
|
||||
|
||||
/**
|
||||
* @brief Computes the Degree Centrality for each vertex.
|
||||
*
|
||||
* @param idx The index of the vertex for which Degree Centrality is being calculated.
|
||||
* @param d_offsets Slice containing the offset vector of the CSR representation.
|
||||
* @return The degree of each vertex.
|
||||
*/
|
||||
__device__ int degree_centrality(int idx, slice<const int> loffsets)
|
||||
{
|
||||
return loffsets[idx + 1] - loffsets[idx];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
stream_ctx ctx;
|
||||
|
||||
// row offsets in CSR format
|
||||
std::vector<int> offsets = {0, 4, 11, 12, 14, 15, 16, 18, 19, 20};
|
||||
// edges in CSR format
|
||||
std::vector<int> nonzeros = {1, 2, 3, 6, 0, 3, 4, 5, 6, 7, 8, 0, 0, 1, 1, 1, 0, 1, 1, 1};
|
||||
// output degrees for each vertex
|
||||
int num_vertices = offsets.size() - 1;
|
||||
std::vector<int> degrees(num_vertices, 0);
|
||||
|
||||
auto loffsets = ctx.logical_data(&offsets[0], offsets.size());
|
||||
auto lnonzeros = ctx.logical_data(&nonzeros[0], nonzeros.size());
|
||||
auto ldegrees = ctx.logical_data(°rees[0], degrees.size());
|
||||
|
||||
ctx.parallel_for(box(num_vertices), loffsets.read(), ldegrees.rw())
|
||||
->*[] __device__(size_t idx, auto loffsets, auto ldegrees) {
|
||||
ldegrees[idx] = degree_centrality(idx, loffsets);
|
||||
};
|
||||
|
||||
ctx.finalize();
|
||||
|
||||
// for (int i = 0; i < num_vertices; ++i) {
|
||||
// printf("Vertex %d: Degree Centrality = %d\n", i, degrees[i]);
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,137 +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 Computes the Jaccard Similarity for each vertex within a graph
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cuda/experimental/stf.cuh>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace cuda::experimental::stf;
|
||||
|
||||
// Performs Binary Search on a given array with start/end bounds and a lookup element
|
||||
__device__ int binary_search(slice<const int> arr, int start, int end, int lookup)
|
||||
{
|
||||
while (start <= end)
|
||||
{
|
||||
int mid = start + (end - start) / 2;
|
||||
if (arr[mid] == lookup)
|
||||
{
|
||||
return mid;
|
||||
}
|
||||
else if (arr[mid] < lookup)
|
||||
{
|
||||
start = mid + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
end = mid - 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Computes the intersection size of neighbors of two vertices.
|
||||
*
|
||||
* @param loffsets Slice containing the offset vector of the CSR representation.
|
||||
* @param lnonzeros Slice containing the non-zero elements (neighbors) vector of the CSR representation.
|
||||
* @param u Index of the first vertex.
|
||||
* @param v Index of the second vertex.
|
||||
* @return The number of common neighbors (intersection size) of vertices u and v.
|
||||
*/
|
||||
__device__ int calculate_intersection_size(slice<const int> loffsets, slice<const int> lnonzeros, int u, int v)
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = loffsets[u]; i < loffsets[u + 1]; i++)
|
||||
{
|
||||
if (binary_search(lnonzeros, loffsets[v], loffsets[v + 1] - 1, lnonzeros[i]) != -1)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Computes the union size of neighbors of two vertices.
|
||||
*
|
||||
* @param loffsets Slice containing the offset vector of the CSR representation.
|
||||
* @param lnonzeros Slice containing the non-zero elements (neighbors) vector of the CSR representation.
|
||||
* @param u Index of the first vertex.
|
||||
* @param v Index of the second vertex.
|
||||
* @return The number of unique neighbors (union size) of vertices u and v.
|
||||
*/
|
||||
__device__ int calculate_union_size(slice<const int> loffsets, slice<const int> lnonzeros, int u, int v)
|
||||
{
|
||||
int count = (loffsets[u + 1] - loffsets[u]) + (loffsets[v + 1] - loffsets[v]);
|
||||
for (int i = loffsets[u]; i < loffsets[u + 1]; i++)
|
||||
{
|
||||
if (binary_search(lnonzeros, loffsets[v], loffsets[v + 1] - 1, lnonzeros[i]) != -1)
|
||||
{
|
||||
count--;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
stream_ctx ctx;
|
||||
|
||||
// row offsets in CSR format
|
||||
std::vector<int> offsets = {0, 4, 11, 12, 14, 15, 16, 18, 19, 20};
|
||||
// edges in CSR format
|
||||
std::vector<int> nonzeros = {1, 2, 3, 6, 0, 3, 4, 5, 6, 7, 8, 0, 0, 1, 1, 1, 0, 1, 1, 1};
|
||||
// output jaccard similarities for each vertex
|
||||
int num_vertices = offsets.size() - 1;
|
||||
std::vector<float> jaccard_similarities(num_vertices * num_vertices, 0.0f);
|
||||
|
||||
auto loffsets = ctx.logical_data(&offsets[0], offsets.size());
|
||||
auto lnonzeros = ctx.logical_data(&nonzeros[0], nonzeros.size());
|
||||
auto ljaccard_similarities = ctx.logical_data(&jaccard_similarities[0], jaccard_similarities.size());
|
||||
|
||||
ctx.parallel_for(box(num_vertices), loffsets.read(), lnonzeros.read(), ljaccard_similarities.rw())
|
||||
->*[] __device__(size_t idx, auto loffsets, auto lnonzeros, auto ljaccard_similarities) {
|
||||
for (int j = 0; j < loffsets.size() - 1; j++)
|
||||
{
|
||||
if (idx != j)
|
||||
{
|
||||
int intersection = calculate_intersection_size(loffsets, lnonzeros, idx, j);
|
||||
int uni = calculate_union_size(loffsets, lnonzeros, idx, j);
|
||||
if (uni > 0)
|
||||
{
|
||||
ljaccard_similarities[idx * (loffsets.size() - 1) + j] = static_cast<float>(intersection) / uni;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ctx.finalize();
|
||||
|
||||
for (int u = 0; u < num_vertices; u++)
|
||||
{
|
||||
for (int v = 0; v < num_vertices; v++)
|
||||
{
|
||||
if (u != v)
|
||||
{
|
||||
printf(
|
||||
"Jaccard similarity between vertex %d and vertex %d: %f\n", u, v, jaccard_similarities[u * num_vertices + v]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,121 +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 Computes the PageRank for vertices within a graph
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cuda/experimental/stf.cuh>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace cuda::experimental::stf;
|
||||
|
||||
/**
|
||||
* @brief Calculates the PageRank for a given vertex.
|
||||
*
|
||||
* @param idx The index of the vertex for which PageRank is being calculated.
|
||||
* @param loffsets Slice containing the offset vector of the CSR representation.
|
||||
* @param lnonzeros Slice containing the non-zero elements (neighbors) vector of the CSR representation.
|
||||
* @param lpage_rank Slice containing current PageRank values for each vertex.
|
||||
* @param lnew_page_rank Slice containing where new PageRank values will be stored.
|
||||
* @param init_rank The initial PageRank value to be used in the calculation.
|
||||
*/
|
||||
__device__ void calculating_pagerank(
|
||||
int idx,
|
||||
const slice<const int>& loffsets,
|
||||
const slice<const int>& lnonzeros,
|
||||
const slice<const float>& lpage_rank,
|
||||
slice<float>& lnew_page_rank,
|
||||
float init_rank)
|
||||
{
|
||||
float rank_sum = 0.0;
|
||||
for (int i = loffsets[idx]; i < loffsets[idx + 1]; i++)
|
||||
{
|
||||
int neighbor = lnonzeros[i];
|
||||
int out_degree = loffsets[neighbor + 1] - loffsets[neighbor];
|
||||
rank_sum += lpage_rank[neighbor] / out_degree;
|
||||
}
|
||||
lnew_page_rank[idx] = 0.85 * rank_sum + (1.0 - 0.85) * init_rank;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
stream_ctx ctx;
|
||||
|
||||
// row offsets in CSR format
|
||||
std::vector<int> offsets = {0, 4, 11, 12, 14, 15, 16, 18, 19, 20};
|
||||
// edges in CSR format
|
||||
std::vector<int> nonzeros = {1, 2, 3, 6, 0, 3, 4, 5, 6, 7, 8, 0, 0, 1, 1, 1, 0, 1, 1, 1};
|
||||
|
||||
int num_vertices = offsets.size() - 1;
|
||||
float init_rank = 1.0f / num_vertices;
|
||||
float tolerance = 1e-6f;
|
||||
int NITER = 100;
|
||||
|
||||
// output pageranks for each vertex
|
||||
std::vector<float> page_rank(num_vertices, init_rank);
|
||||
std::vector<float> new_page_rank(num_vertices);
|
||||
|
||||
auto loffsets = ctx.logical_data(&offsets[0], offsets.size());
|
||||
auto lnonzeros = ctx.logical_data(&nonzeros[0], nonzeros.size());
|
||||
auto lpage_rank = ctx.logical_data(&page_rank[0], page_rank.size());
|
||||
auto lnew_page_rank = ctx.logical_data(&new_page_rank[0], new_page_rank.size());
|
||||
auto lmax_diff = ctx.logical_data(shape_of<scalar_view<float>>());
|
||||
|
||||
for (int iter = 0; iter < NITER; ++iter)
|
||||
{
|
||||
// Calculate Current Iteration PageRank
|
||||
ctx.parallel_for(
|
||||
box(num_vertices),
|
||||
loffsets.read(),
|
||||
lnonzeros.read(),
|
||||
lpage_rank.rw(),
|
||||
lnew_page_rank.rw(),
|
||||
lmax_diff.reduce(reducer::maxval<float>{}))
|
||||
->*[init_rank] __device__(
|
||||
size_t idx, auto loffsets, auto lnonzeros, auto lpage_rank, auto lnew_page_rank, auto& max_diff) {
|
||||
calculating_pagerank(idx, loffsets, lnonzeros, lpage_rank, lnew_page_rank, init_rank);
|
||||
max_diff = ::std::max(max_diff, lnew_page_rank[idx] - lpage_rank[idx]);
|
||||
};
|
||||
|
||||
// Reduce Error and Check for Convergence
|
||||
bool converged = (ctx.wait(lmax_diff) < tolerance);
|
||||
if (converged)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Update New PageRank Values
|
||||
std::swap(lpage_rank, lnew_page_rank);
|
||||
}
|
||||
|
||||
ctx.finalize();
|
||||
|
||||
/* CHECKING FOR ANSWER CORRECTNESS */
|
||||
// sum of all page ranks should equal 1
|
||||
double sum_pageranks = 0.0;
|
||||
for (int64_t i = 0; i < num_vertices; i++)
|
||||
{
|
||||
sum_pageranks += page_rank[i];
|
||||
}
|
||||
printf("Page rank answer is %s.\n", abs(sum_pageranks - 1.0) < 0.001 ? "correct" : "not correct");
|
||||
|
||||
printf("PageRank Results:\n");
|
||||
for (size_t i = 0; i < page_rank.size(); ++i)
|
||||
{
|
||||
printf("Vertex %zu: %f\n", i, page_rank[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,203 +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 Computes the PageRank for vertices within a graph
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cuda/experimental/stf.cuh>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace cuda::experimental::stf;
|
||||
|
||||
#if _CCCL_CTK_AT_LEAST(12, 4)
|
||||
/**
|
||||
* @brief Calculates the PageRank for a given vertex.
|
||||
*
|
||||
* @param idx The index of the vertex for which PageRank is being calculated.
|
||||
* @param loffsets Slice containing the offset vector of the CSR representation.
|
||||
* @param lnonzeros Slice containing the non-zero elements (neighbors) vector of the CSR representation.
|
||||
* @param lpage_rank Slice containing current PageRank values for each vertex.
|
||||
* @param lnew_page_rank Slice containing where new PageRank values will be stored.
|
||||
* @param lpersonalization Slice containing the personalization vector for each vertex.
|
||||
*/
|
||||
__device__ void calculating_pagerank(
|
||||
int idx,
|
||||
const slice<const int>& loffsets,
|
||||
const slice<const int>& lnonzeros,
|
||||
const slice<const float>& lpage_rank,
|
||||
slice<float>& lnew_page_rank,
|
||||
const slice<const float>& lpersonalization)
|
||||
{
|
||||
float rank_sum = 0.0;
|
||||
for (int i = loffsets[idx]; i < loffsets[idx + 1]; i++)
|
||||
{
|
||||
int neighbor = lnonzeros[i];
|
||||
int out_degree = loffsets[neighbor + 1] - loffsets[neighbor];
|
||||
rank_sum += lpage_rank[neighbor] / out_degree;
|
||||
}
|
||||
lnew_page_rank[idx] = 0.85 * rank_sum + (1.0 - 0.85) * lpersonalization[idx];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Computes PageRank using the power iteration method
|
||||
*
|
||||
* @param ctx The CUDASTF context
|
||||
* @param loffsets Logical data for CSR offset vector
|
||||
* @param lnonzeros Logical data for CSR non-zero elements vector
|
||||
* @param lpage_rank Logical data for current PageRank values
|
||||
* @param lpersonalization Logical data for personalization vector
|
||||
* @param num_vertices Number of vertices in the graph
|
||||
* @param NITER Maximum number of iterations
|
||||
* @param tolerance Convergence tolerance
|
||||
*/
|
||||
void compute_pagerank(
|
||||
stackable_ctx& ctx,
|
||||
stackable_logical_data<slice<int>>& loffsets,
|
||||
stackable_logical_data<slice<int>>& lnonzeros,
|
||||
stackable_logical_data<slice<float>>& lpage_rank,
|
||||
stackable_logical_data<slice<float>>& lpersonalization,
|
||||
int num_vertices,
|
||||
int NITER,
|
||||
float tolerance)
|
||||
{
|
||||
// Create local temporary buffer and convergence tracking
|
||||
auto lnew_page_rank = ctx.logical_data(lpage_rank.shape());
|
||||
auto lmax_diff = ctx.logical_data(shape_of<scalar_view<float>>());
|
||||
auto liter = ctx.logical_data(shape_of<scalar_view<int>>());
|
||||
|
||||
// Initialize iteration counter
|
||||
ctx.parallel_for(box(1), liter.write())->*[] __device__(size_t, auto iter) {
|
||||
*iter = 0;
|
||||
};
|
||||
|
||||
{
|
||||
auto while_guard = ctx.while_graph_scope();
|
||||
|
||||
// Calculate Current Iteration PageRank
|
||||
ctx.parallel_for(
|
||||
box(num_vertices),
|
||||
loffsets.read(),
|
||||
lnonzeros.read(),
|
||||
lpage_rank.rw(),
|
||||
lnew_page_rank.write(),
|
||||
lpersonalization.read(),
|
||||
lmax_diff.reduce(reducer::maxval<float>{}))
|
||||
->*
|
||||
[] __device__(
|
||||
size_t idx,
|
||||
auto loffsets,
|
||||
auto lnonzeros,
|
||||
auto lpage_rank,
|
||||
auto lnew_page_rank,
|
||||
auto lpersonalization,
|
||||
auto& max_diff) {
|
||||
calculating_pagerank(idx, loffsets, lnonzeros, lpage_rank, lnew_page_rank, lpersonalization);
|
||||
max_diff = ::std::max(max_diff, lnew_page_rank[idx] - lpage_rank[idx]);
|
||||
};
|
||||
|
||||
// Update PageRank Values
|
||||
ctx.parallel_for(lpage_rank.shape(), lpage_rank.write(), lnew_page_rank.read())
|
||||
->*[] __device__(size_t i, auto page_rank, auto new_page_rank) {
|
||||
page_rank(i) = new_page_rank(i);
|
||||
};
|
||||
|
||||
while_guard.update_cond(lmax_diff.read(), liter.rw())->*[NITER, tolerance] __device__(auto max_diff, auto iter) {
|
||||
bool converged = (*max_diff < tolerance);
|
||||
bool max_reached = ((*iter)++ >= NITER); // Maximum iteration limit
|
||||
return !converged && !max_reached; // Continue if not converged and under limit
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif // _CCCL_CTK_AT_LEAST(12, 4)
|
||||
|
||||
int main()
|
||||
{
|
||||
#if _CCCL_CTK_BELOW(12, 4)
|
||||
fprintf(stderr, "Waiving example: while_graph_scope is only available since CUDA 12.4.\n");
|
||||
return 0;
|
||||
#else
|
||||
stackable_ctx ctx;
|
||||
|
||||
// row offsets in CSR format
|
||||
std::vector<int> offsets = {0, 4, 11, 12, 14, 15, 16, 18, 19, 20};
|
||||
// edges in CSR format
|
||||
std::vector<int> nonzeros = {1, 2, 3, 6, 0, 3, 4, 5, 6, 7, 8, 0, 0, 1, 1, 1, 0, 1, 1, 1};
|
||||
|
||||
int num_vertices = offsets.size() - 1;
|
||||
float init_rank = 1.0f / num_vertices;
|
||||
float tolerance = 1e-6f;
|
||||
int NITER = 100;
|
||||
int num_personalization = 4;
|
||||
|
||||
::std::vector<stackable_logical_data<slice<float>>> lpage_rank_slices;
|
||||
for (int i = 0; i < num_personalization; i++)
|
||||
{
|
||||
lpage_rank_slices.push_back(ctx.logical_data(shape_of<slice<float>>(num_vertices)));
|
||||
}
|
||||
|
||||
auto loffsets = ctx.logical_data(&offsets[0], offsets.size());
|
||||
auto lnonzeros = ctx.logical_data(&nonzeros[0], nonzeros.size());
|
||||
|
||||
loffsets.set_read_only();
|
||||
lnonzeros.set_read_only();
|
||||
|
||||
{
|
||||
auto scope = ctx.graph_scope();
|
||||
for (int p = 0; p < num_personalization; p++)
|
||||
{
|
||||
// Initialize PageRank values to uniform distribution
|
||||
ctx.parallel_for(lpage_rank_slices[p].shape(), lpage_rank_slices[p].write())
|
||||
->*[init_rank] __device__(size_t i, auto page_rank) {
|
||||
page_rank(i) = init_rank;
|
||||
};
|
||||
|
||||
// Create personalization vector (uniform for this example)
|
||||
auto lpersonalization = ctx.logical_data(shape_of<slice<float>>(num_vertices));
|
||||
ctx.parallel_for(lpersonalization.shape(), lpersonalization.write())
|
||||
->*[init_rank] __device__(size_t i, auto lpersonalization) {
|
||||
lpersonalization(i) = init_rank;
|
||||
};
|
||||
|
||||
compute_pagerank(ctx, loffsets, lnonzeros, lpage_rank_slices[p], lpersonalization, num_vertices, NITER, tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
for (int p = 0; p < num_personalization; p++)
|
||||
{
|
||||
ctx.host_launch(lpage_rank_slices[p].read())->*[p, num_vertices] __host__(slice<const float> page_rank) {
|
||||
double sum_pageranks = 0.0;
|
||||
for (int64_t i = 0; i < num_vertices; i++)
|
||||
{
|
||||
sum_pageranks += page_rank[i];
|
||||
}
|
||||
printf("Page rank answer for personalization %d is %s.\n",
|
||||
p,
|
||||
abs(sum_pageranks - 1.0) < 0.001 ? "correct" : "not correct");
|
||||
|
||||
// Print first few results for verification
|
||||
printf("Personalization %d - First 5 vertices: ", p);
|
||||
for (size_t i = 0; i < std::min(5UL, page_rank.size()); ++i)
|
||||
{
|
||||
printf("%.6f ", page_rank[i]);
|
||||
}
|
||||
printf("\n");
|
||||
};
|
||||
}
|
||||
|
||||
ctx.finalize();
|
||||
|
||||
return 0;
|
||||
#endif // !_CCCL_CTK_BELOW(12, 4)
|
||||
}
|
||||
@@ -1,135 +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 Computes the PageRank for vertices within a graph
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cuda/experimental/stf.cuh>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace cuda::experimental::stf;
|
||||
|
||||
/**
|
||||
* @brief Calculates the PageRank for a given vertex.
|
||||
*
|
||||
* @param idx The index of the vertex for which PageRank is being calculated.
|
||||
* @param loffsets Slice containing the offset vector of the CSR representation.
|
||||
* @param lnonzeros Slice containing the non-zero elements (neighbors) vector of the CSR representation.
|
||||
* @param lpage_rank Slice containing current PageRank values for each vertex.
|
||||
* @param lnew_page_rank Slice containing where new PageRank values will be stored.
|
||||
* @param init_rank The initial PageRank value to be used in the calculation.
|
||||
*/
|
||||
__device__ void calculating_pagerank(
|
||||
int idx,
|
||||
const slice<const int>& loffsets,
|
||||
const slice<const int>& lnonzeros,
|
||||
const slice<const float>& lpage_rank,
|
||||
slice<float>& lnew_page_rank,
|
||||
float init_rank)
|
||||
{
|
||||
float rank_sum = 0.0;
|
||||
for (int i = loffsets[idx]; i < loffsets[idx + 1]; i++)
|
||||
{
|
||||
int neighbor = lnonzeros[i];
|
||||
int out_degree = loffsets[neighbor + 1] - loffsets[neighbor];
|
||||
rank_sum += lpage_rank[neighbor] / out_degree;
|
||||
}
|
||||
lnew_page_rank[idx] = 0.85 * rank_sum + (1.0 - 0.85) * init_rank;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
#if _CCCL_CTK_BELOW(12, 4)
|
||||
fprintf(stderr, "Waiving example: while_graph_scope is only available since CUDA 12.4.\n");
|
||||
return 0;
|
||||
#else
|
||||
stackable_ctx ctx;
|
||||
|
||||
// row offsets in CSR format
|
||||
std::vector<int> offsets = {0, 4, 11, 12, 14, 15, 16, 18, 19, 20};
|
||||
// edges in CSR format
|
||||
std::vector<int> nonzeros = {1, 2, 3, 6, 0, 3, 4, 5, 6, 7, 8, 0, 0, 1, 1, 1, 0, 1, 1, 1};
|
||||
|
||||
int num_vertices = offsets.size() - 1;
|
||||
float init_rank = 1.0f / num_vertices;
|
||||
float tolerance = 1e-6f;
|
||||
int NITER = 100;
|
||||
|
||||
// output pageranks for each vertex
|
||||
std::vector<float> page_rank(num_vertices, init_rank);
|
||||
std::vector<float> new_page_rank(num_vertices);
|
||||
|
||||
auto loffsets = ctx.logical_data(&offsets[0], offsets.size());
|
||||
auto lnonzeros = ctx.logical_data(&nonzeros[0], nonzeros.size());
|
||||
auto lpage_rank = ctx.logical_data(&page_rank[0], page_rank.size());
|
||||
auto lnew_page_rank = ctx.logical_data(&new_page_rank[0], new_page_rank.size());
|
||||
auto lmax_diff = ctx.logical_data(shape_of<scalar_view<float>>());
|
||||
auto liter = ctx.logical_data(shape_of<scalar_view<int>>());
|
||||
|
||||
// Initialize iteration counter
|
||||
ctx.parallel_for(box(1), liter.write())->*[] __device__(size_t, auto iter) {
|
||||
*iter = 0;
|
||||
};
|
||||
|
||||
{
|
||||
auto while_guard = ctx.while_graph_scope();
|
||||
|
||||
// Calculate Current Iteration PageRank
|
||||
ctx.parallel_for(
|
||||
box(num_vertices),
|
||||
loffsets.read(),
|
||||
lnonzeros.read(),
|
||||
lpage_rank.rw(),
|
||||
lnew_page_rank.rw(),
|
||||
lmax_diff.reduce(reducer::maxval<float>{}))
|
||||
->*[init_rank] __device__(
|
||||
size_t idx, auto loffsets, auto lnonzeros, auto lpage_rank, auto lnew_page_rank, auto& max_diff) {
|
||||
calculating_pagerank(idx, loffsets, lnonzeros, lpage_rank, lnew_page_rank, init_rank);
|
||||
max_diff = ::std::max(max_diff, lnew_page_rank[idx] - lpage_rank[idx]);
|
||||
};
|
||||
|
||||
// Update PageRank Values
|
||||
ctx.parallel_for(lpage_rank.shape(), lpage_rank.write(), lnew_page_rank.read())
|
||||
->*[] __device__(size_t i, auto page_rank, auto new_page_rank) {
|
||||
page_rank(i) = new_page_rank(i);
|
||||
};
|
||||
|
||||
while_guard.update_cond(lmax_diff.read(), liter.rw())->*[NITER, tolerance] __device__(auto max_diff, auto iter) {
|
||||
bool converged = (*max_diff < tolerance);
|
||||
bool max_reached = ((*iter)++ >= NITER); // Maximum iteration limit
|
||||
return !converged && !max_reached; // Continue if not converged and under limit
|
||||
};
|
||||
}
|
||||
|
||||
ctx.finalize();
|
||||
|
||||
/* CHECKING FOR ANSWER CORRECTNESS */
|
||||
// sum of all page ranks should equal 1
|
||||
double sum_pageranks = 0.0;
|
||||
for (int64_t i = 0; i < num_vertices; i++)
|
||||
{
|
||||
sum_pageranks += page_rank[i];
|
||||
}
|
||||
printf("Page rank answer is %s.\n", abs(sum_pageranks - 1.0) < 0.001 ? "correct" : "not correct");
|
||||
|
||||
printf("PageRank Results:\n");
|
||||
for (size_t i = 0; i < page_rank.size(); ++i)
|
||||
{
|
||||
printf("Vertex %zu: %f\n", i, page_rank[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif // !_CCCL_CTK_BELOW(12, 4)
|
||||
}
|
||||
@@ -1,100 +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 Computes the total number of triangles within a graph
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cuda/experimental/stf.cuh>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace cuda::experimental::stf;
|
||||
|
||||
// Performs Binary Search on a given array with start/end bounds and a lookup element
|
||||
__device__ int binary_search(slice<const int> arr, int start, int end, int lookup)
|
||||
{
|
||||
while (start <= end)
|
||||
{
|
||||
int mid = start + (end - start) / 2;
|
||||
if (arr[mid] == lookup)
|
||||
{
|
||||
return mid;
|
||||
}
|
||||
else if (arr[mid] < lookup)
|
||||
{
|
||||
start = mid + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
end = mid - 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Computes the Triangle Counting for each vertex.
|
||||
*
|
||||
* @param idx The index of the vertex for which Triangle Counting is being calculated.
|
||||
* @param loffsets Slice containing the offset vector of the CSR representation.
|
||||
* @param lnonzeros Slice containing the non-zero elements (neighbors) vector of the CSR representation.
|
||||
* @return The local triangle count for the vertex.
|
||||
*/
|
||||
__device__ unsigned long long int triangle_count(int idx, slice<const int> loffsets, slice<const int> lnonzeros)
|
||||
{
|
||||
int lcount = 0;
|
||||
for (int i = loffsets[idx]; i < loffsets[idx + 1]; i++)
|
||||
{
|
||||
int v = lnonzeros[i];
|
||||
for (int j = loffsets[idx]; j < loffsets[idx + 1]; j++)
|
||||
{
|
||||
int w = lnonzeros[j];
|
||||
if (binary_search(lnonzeros, loffsets[v], loffsets[v + 1] - 1, w) != -1)
|
||||
{
|
||||
lcount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return lcount;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
stream_ctx ctx;
|
||||
|
||||
// row offsets in CSR format
|
||||
std::vector<int> offsets = {0, 0, 1, 2, 4, 5, 6, 8, 9, 10};
|
||||
// edges in CSR format
|
||||
std::vector<int> nonzeros = {0, 0, 0, 1, 1, 1, 0, 1, 1, 1};
|
||||
|
||||
int num_vertices = offsets.size() - 1;
|
||||
|
||||
auto loffsets = ctx.logical_data(&offsets[0], offsets.size());
|
||||
auto lnonzeros = ctx.logical_data(&nonzeros[0], nonzeros.size());
|
||||
auto ltotal_count = ctx.logical_data(shape_of<scalar_view<unsigned long long>>());
|
||||
|
||||
ctx.parallel_for(
|
||||
box(num_vertices), loffsets.read(), lnonzeros.read(), ltotal_count.reduce(reducer::sum<unsigned long long>{}))
|
||||
->*[] __device__(size_t idx, auto loffsets, auto lnonzeros, auto& total_count) {
|
||||
total_count += triangle_count(idx, loffsets, lnonzeros);
|
||||
};
|
||||
|
||||
auto total_count = ctx.wait(ltotal_count);
|
||||
|
||||
ctx.finalize();
|
||||
|
||||
printf("Number of triangles: %lld\n", total_count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user