Skip to content

Commit

Permalink
Make sure that CUDA instances are destroyed prior to destroying streams
Browse files Browse the repository at this point in the history
  • Loading branch information
aprokop committed Apr 1, 2024
1 parent b4f6a16 commit b19cb1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class InstanceManager<Kokkos::Cuda>

~InstanceManager()
{
// Destroy CUDA instances prior to destroying streams
_instances.clear();

for (auto &stream : _streams)
cudaStreamDestroy(stream);
}
Expand Down
34 changes: 19 additions & 15 deletions examples/access_traits/example_cuda_access_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,25 @@ int main(int argc, char *argv[])
cudaStreamCreate(&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));
}
});
{
// Place the code using CUDA instance in a block to make sure the instance
// is destroyed prior to destroying the 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));
}
});
}

cudaStreamDestroy(stream);

Expand Down

0 comments on commit b19cb1c

Please sign in to comment.