Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EMST example #1214

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_subdirectory(brute_force)
add_subdirectory(callback)
add_subdirectory(custom_distance)
add_subdirectory(dbscan)
add_subdirectory(emst)
add_subdirectory(molecular_dynamics)
add_subdirectory(simple_intersection)
add_subdirectory(triangle_intersection)
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Directory | Description
`callback` | execute search which calls user function on match
`custom_distance` | execute nearest search using a custom distance metric
`dbscan` | cluster a set of points using DBSCAN
`emst` | build an Euclidean minimum spanning tree (MST)
`molecular_dynamics` | do one step of moving particles under forces
`moving_least_squares` | interpolate data between point clouds using MLS
`raytracing` | deposit ray energy onto intersected boxes
Expand Down
3 changes: 3 additions & 0 deletions examples/emst/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(ArborX_Example_EMST.exe example_emst.cpp)
target_link_libraries(ArborX_Example_EMST.exe ArborX::ArborX)
add_test(NAME ArborX_Example_EMST COMMAND ArborX_Example_EMST.exe)
72 changes: 72 additions & 0 deletions examples/emst/example_emst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/****************************************************************************
* Copyright (c) 2025, 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_MinimumSpanningTree.hpp>

#include <Kokkos_Sort.hpp>

#include <iostream>

template <typename MemorySpace>
void printEdges(
Kokkos::View<ArborX::Experimental::WeightedEdge *, MemorySpace> edges)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be pedantic one would want to ensure that the view is contiguous here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be more correct, yes. Don't think it's worthwhile, though.

{
auto edges_host =
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, edges);
Kokkos::sort(edges_host);
for (int i = 0; i < (int)edges_host.size(); ++i)
{
auto const &edge = edges_host(i);
auto min_index = std::min(edge.source, edge.target);
auto max_index = std::max(edge.source, edge.target);
printf("#%d: (%d, %d, %.2f)\n", i, min_index, max_index, edge.weight);
}
}

int main(int argc, char *argv[])
{
Kokkos::ScopeGuard guard(argc, argv);

using ExecutionSpace = Kokkos::DefaultExecutionSpace;
using MemorySpace = ExecutionSpace::memory_space;

Kokkos::View<ArborX::Point<2> *, MemorySpace> cloud("Example::point_cloud",
6);
auto cloud_host = Kokkos::create_mirror_view(cloud);
// 4 | 5
// 3 | 2 4
// 2 | 3
// 1 | 1
// 0 | 0
// ----------
// 0 1 2 3 4
cloud_host[0] = {1, 0};
cloud_host[1] = {0, 1};
cloud_host[2] = {1, 3};
cloud_host[3] = {4, 2};
cloud_host[4] = {2, 3};
cloud_host[5] = {2, 4};
Kokkos::deep_copy(cloud, cloud_host);

ArborX::Experimental::MinimumSpanningTree<MemorySpace> emst(ExecutionSpace{},
cloud);
auto edges = emst.edges;

// Expected output:
// #0: (2, 4, 1.00)
// #1: (4, 5, 1.00)
// #2: (0, 1, 1.41)
// #3: (3, 4, 2.24)
// #4: (1, 2, 2.24)
printEdges(edges);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do printEdges(emst.edges);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that at first. But decided to explicitly show a user how to get the edges in a separate line. Felt a bit more clear.


return 0;
}