Skip to content

Commit

Permalink
Add EMST example
Browse files Browse the repository at this point in the history
  • Loading branch information
aprokop authored Feb 7, 2025
1 parent f26ef0a commit c3ebac5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
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)
{
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);

return 0;
}

0 comments on commit c3ebac5

Please sign in to comment.