-
Notifications
You must be signed in to change notification settings - Fork 41
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
Add EMST example #1214
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) |
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) | ||
{ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would do There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.