/
example_cuda_access_traits.cpp
99 lines (82 loc) · 2.8 KB
/
example_cuda_access_traits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/****************************************************************************
* Copyright (c) 2017-2022 by the ArborX authors *
* All rights reserved. *
* *
* This file is part of the ArborX library. ArborX is *
* distributed under a BSD 3-clause license. For the licensing terms see *
* the LICENSE file in the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
#include <ArborX.hpp>
#include <Kokkos_Core.hpp>
#include <array>
#include <iostream>
#include <numeric>
struct PointCloud
{
float *d_x;
float *d_y;
float *d_z;
int N;
};
struct Spheres
{
float *d_x;
float *d_y;
float *d_z;
float *d_r;
int N;
};
template <>
struct ArborX::AccessTraits<PointCloud, ArborX::PrimitivesTag>
{
static KOKKOS_FUNCTION std::size_t size(PointCloud const &cloud)
{
return cloud.N;
}
static KOKKOS_FUNCTION ArborX::Point get(PointCloud const &cloud,
std::size_t i)
{
return {{cloud.d_x[i], cloud.d_y[i], cloud.d_z[i]}};
}
using memory_space = Kokkos::CudaSpace;
};
template <>
struct ArborX::AccessTraits<Spheres, ArborX::PredicatesTag>
{
static KOKKOS_FUNCTION std::size_t size(Spheres const &d) { return d.N; }
static KOKKOS_FUNCTION auto get(Spheres const &d, std::size_t i)
{
return ArborX::intersects(
ArborX::Sphere{{{d.d_x[i], d.d_y[i], d.d_z[i]}}, d.d_r[i]});
}
using memory_space = Kokkos::CudaSpace;
};
int main(int argc, char *argv[])
{
Kokkos::ScopeGuard guard(argc, argv);
constexpr std::size_t N = 10;
std::array<float, N> a;
float *d_a;
cudaMalloc(&d_a, sizeof(a));
std::iota(std::begin(a), std::end(a), 1.0);
cudaStream_t stream;
cudaStreamCreate(&stream);
Kokkos::push_finalize_hook([stream]() { cudaStreamDestroy(stream); });
cudaMemcpyAsync(d_a, a.data(), sizeof(a), cudaMemcpyHostToDevice, stream);
Kokkos::Cuda cuda{stream};
ArborX::BVH<Kokkos::CudaSpace> bvh{cuda, PointCloud{d_a, d_a, d_a, N}};
Kokkos::View<int *, Kokkos::CudaSpace> indices("Example::indices", 0);
Kokkos::View<int *, Kokkos::CudaSpace> offset("Example::offset", 0);
ArborX::query(bvh, cuda, Spheres{d_a, d_a, d_a, d_a, N}, indices, offset);
Kokkos::parallel_for(
"Example::print_indices", Kokkos::RangePolicy<Kokkos::Cuda>(cuda, 0, N),
KOKKOS_LAMBDA(int i) {
for (int j = offset(i); j < offset(i + 1); ++j)
{
printf("%i %i\n", i, indices(j));
}
});
return 0;
}