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

Enhancement ghost query functions #1248

Open
wants to merge 27 commits into
base: feature-ghost_interface
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f68206d
move binary soearch to forest_private
sandro-elsweijer Sep 16, 2024
933ac88
consting
sandro-elsweijer Sep 16, 2024
1e0ef37
add ghost retrieval functions
sandro-elsweijer Sep 16, 2024
3fee901
Typos
holke Sep 18, 2024
8c171a8
Update src/t8_forest/t8_forest_ghost.cxx
holke Sep 24, 2024
5616096
Update src/t8_forest/t8_forest_ghost.h
holke Sep 24, 2024
574b415
Update src/t8_forest/t8_forest_private.cxx
holke Sep 24, 2024
5909a16
Update src/t8_forest/t8_forest_private.h
holke Sep 24, 2024
70e9f37
Merge remote-tracking branch 'origin/feature-ghost_interface' into en…
holke Oct 18, 2024
31e9d92
indent example
holke Oct 18, 2024
2ccc215
Merge remote-tracking branch 'origin/enhancement-ghost_query_function…
holke Oct 18, 2024
58d70af
Merge branch 'feature-ghost_interface' into enhancement-ghost_query_f…
holke Oct 29, 2024
e718252
Fix Makefile line wrap
holke Oct 29, 2024
8d929a8
Add new bin search to private and remove from static
holke Oct 29, 2024
2d977da
rename get ghost function to better match parameters
holke Oct 29, 2024
4581fe0
indent
holke Oct 29, 2024
49b2dc3
remove vtk file from build list
holke Oct 29, 2024
38deaa6
finish moving bin search - move includes
holke Oct 29, 2024
975e66b
improve documentation
holke Oct 29, 2024
c79445b
Add test for ghost in tree getters
holke Oct 29, 2024
a05f39a
fix assertion logic
holke Oct 29, 2024
039efd7
more const
holke Oct 29, 2024
7432241
minor test changes
holke Oct 29, 2024
183b117
temporarily remove foreach loop over ghosts
holke Oct 29, 2024
e1ee81f
Add negative result tests
holke Nov 4, 2024
3dd4d2c
fix private.c rename in Makefile
holke Nov 8, 2024
6c699d2
remove old files from Makefile
holke Nov 12, 2024
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
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ target_sources( T8 PRIVATE
t8_forest/t8_forest_adapt.cxx
t8_forest/t8_forest_partition.cxx
t8_forest/t8_forest.cxx
t8_forest/t8_forest_private.c
t8_forest/t8_forest_private.cxx
t8_forest/t8_forest_ghost.cxx
t8_forest/t8_forest_iterate.cxx
t8_forest/t8_forest_balance.cxx
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ libt8_compiled_sources = \
src/t8_geometry/t8_geometry_implementations/t8_geometry_zero.cxx \
src/t8_geometry/t8_geometry_implementations/t8_geometry_examples.cxx \
src/t8_forest/t8_forest_partition.cxx src/t8_forest/t8_forest.cxx \
src/t8_forest/t8_forest_private.c \
src/t8_forest/t8_forest_private.cxx \
src/t8_forest/t8_forest_ghost.cxx src/t8_forest/t8_forest_iterate.cxx \
src/t8_version.c \
src/t8_vtk.c src/t8_forest/t8_forest_balance.cxx \
Expand Down
39 changes: 1 addition & 38 deletions src/t8_forest/t8_forest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@
#include <t8_geometry/t8_geometry_implementations/t8_geometry_linear.h>
#include <t8_geometry/t8_geometry_implementations/t8_geometry_linear_axis_aligned.h>
#endif
#include <t8_data/t8_element_array_iterator.hxx>

#include <algorithm>

/* We want to export the whole implementation to be callable from "C" */
T8_EXTERN_C_BEGIN ();
Expand Down Expand Up @@ -1444,39 +1441,6 @@ t8_forest_copy_trees (t8_forest_t forest, t8_forest_t from, int copy_elements)
}
}

/** \brief Search for a linear element id (at forest->maxlevel) in a sorted array of
* elements. If the element does not exist, return the largest index i
* such that the element at position i has a smaller id than the given one.
* If no such i exists, return -1.
*/
static t8_locidx_t
t8_forest_bin_search_lower (const t8_element_array_t *elements, const t8_linearidx_t element_id, const int maxlevel)
{
const t8_eclass_scheme_c *ts = t8_element_array_get_scheme (elements);
/* At first, we check whether any element has smaller id than the
* given one. */
const t8_element_t *query = t8_element_array_index_int (elements, 0);
const t8_linearidx_t query_id = ts->t8_element_get_linear_id (query, maxlevel);
if (query_id > element_id) {
/* No element has id smaller than the given one. */
return -1;
}

/* We search for the first element in the array that is greater than the given element id. */
auto elem_iter = std::upper_bound (
t8_element_array_begin (elements), t8_element_array_end (elements), element_id,
[&maxlevel, &ts] (const t8_linearidx_t element_id_, const t8_element_array_iterator::value_type &elem_ptr) {
return (element_id_ < ts->t8_element_get_linear_id (elem_ptr, maxlevel));
});

/* After we found the element with an id greater than the given one, we are able to jump one index back.
* This guarantees us that the element at (index - 1) is smaller or equal to the given element id.
* In case we do not find an element that is greater than the given element_id, the binary search returns
* the end-iterator of the element array. In that case, we want to return the last index from the element
* array. */
return elem_iter.GetCurrentIndex () - 1;
}

t8_eclass_t
t8_forest_element_neighbor_eclass (t8_forest_t forest, t8_locidx_t ltreeid, const t8_element_t *elem, int face)
{
Expand Down Expand Up @@ -3680,11 +3644,10 @@ t8_forest_get_element (t8_forest_t forest, t8_locidx_t lelement_id, t8_locidx_t
const t8_element_t *
t8_forest_get_element_in_tree (t8_forest_t forest, t8_locidx_t ltreeid, t8_locidx_t leid_in_tree)
{
t8_tree_t tree;
T8_ASSERT (t8_forest_is_committed (forest));
T8_ASSERT (0 <= ltreeid && ltreeid < t8_forest_get_num_local_trees (forest));

tree = t8_forest_get_tree (forest, ltreeid);
const t8_tree_t tree = t8_forest_get_tree (forest, ltreeid);
const t8_element_t *element = t8_forest_get_tree_element (tree, leid_in_tree);
T8_ASSERT (t8_forest_element_is_leaf (forest, element, ltreeid));
return element;
Expand Down
55 changes: 53 additions & 2 deletions src/t8_forest/t8_forest_ghost.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,57 @@ t8_forest_ghost_tree_num_elements (t8_forest_t forest, t8_locidx_t lghost_tree)
return t8_element_array_get_count (&ghost_tree->elements);
}

const t8_element_t *
t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, t8_locidx_t lghost_tree, t8_linearidx_t linear_id,
int element_level, t8_locidx_t *loc_ghost_id)
{
T8_ASSERT (t8_forest_is_committed (forest));
const t8_element_array_t *ghost_elements = t8_forest_ghost_get_tree_elements (forest, lghost_tree);
T8_ASSERT (ghost_elements != NULL);
T8_ASSERT (!forest->incomplete_trees); // This function will not word properlt with incomplete trees.
/* Search for the element.
* The search returns the largest index i,
* such that the element at position i has a smaller id than the given one.
* If no such i exists, it returns -1. */
*loc_ghost_id = t8_forest_bin_search_lower (ghost_elements, linear_id, element_level);
if (*loc_ghost_id >= 0) {
/* The element was found */
return t8_element_array_index_locidx (ghost_elements, *loc_ghost_id);
}
*loc_ghost_id = -1;
return nullptr;
}

t8_locidx_t
t8_ghost_get_ghost_id_in_tree (t8_forest_t forest, t8_locidx_t lghost_tree, const t8_element_t *ghost_element)
{
T8_ASSERT (t8_forest_is_committed (forest));
holke marked this conversation as resolved.
Show resolved Hide resolved
const t8_element_array_t *ghost_elements = t8_forest_ghost_get_tree_elements (forest, lghost_tree);
T8_ASSERT (ghost_elements != NULL);
/* In order to find the element, we need to compute its linear id.
* To do so, we need the scheme and the level of the element. */
const t8_eclass_scheme_c *scheme = t8_element_array_get_scheme (ghost_elements);
const int ghost_level = scheme->t8_element_level (ghost_element);
/* Compute the linear id. */
const t8_linearidx_t element_id = scheme->t8_element_get_linear_id (ghost_element, ghost_level);
/* Search for the element */
t8_locidx_t loc_ghost_id;
const t8_element_t *found_ghost
= t8_ghost_get_ghost_in_tree_from_linear_id (forest, lghost_tree, element_id, ghost_level, &loc_ghost_id);
if (loc_ghost_id < 0) {
/* The element was not found */
return -1;
}
/* An element was found but it may not be the candidate element.
* To identify whether the element was found, we compare these two. */
if (scheme->t8_element_equal (ghost_element, found_ghost)) {
return loc_ghost_id;
}
else {
return -1;
}
}

t8_element_array_t *
t8_forest_ghost_get_tree_elements (const t8_forest_t forest, const t8_locidx_t lghost_tree)
{
Expand Down Expand Up @@ -1882,9 +1933,9 @@ t8_forest_ghost_destroy (t8_forest_ghost_t *pghost)

/**
* Implementation of the ghost-interface
* Wrapper for the abstracte base classes
* Wrapper for the abstract base class
* Implementation of the communication steps
* and implementation of the dreived class search
* and implementation of the derived class search.
*/

t8_ghost_type_t
Expand Down
25 changes: 25 additions & 0 deletions src/t8_forest/t8_forest_ghost.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@ t8_forest_ghost_get_tree_element_offset (t8_forest_t forest, t8_locidx_t lghost_
t8_locidx_t
t8_forest_ghost_tree_num_elements (t8_forest_t forest, t8_locidx_t lghost_tree);

/** Retrieves a ghost element from its ghost tree given the element's linear id.
*
* \param [in] forest The forest object.
* \param [in] lghost_tree The local index of the ghost tree.
* \param [in] linear_id The linear id of the element.
* \param [in] element_level The level of the element.
* \param [out] loc_ghost_id The local id of the ghost. -1 if no ghost was found.
holke marked this conversation as resolved.
Show resolved Hide resolved
* \return The ghost element. nullptr if no ghost was found.
*/
const t8_element_t *
t8_ghost_get_ghost_in_tree_from_linear_id (t8_forest_t forest, const t8_locidx_t lghost_tree,
const t8_linearidx_t linear_id, const int element_level,
t8_locidx_t *loc_ghost_id);

/** Retrieves the local index of a ghost element in its specific ghost tree.
*
* \param [in] forest The forest object.
* \param [in] lghost_tree The local index of the ghost tree.
* \param [in] ghost_element The ghost element.
* \return The local index of the element in the ghost element array of the ghost tree.
* -1 if no ghost element was found.
*/
t8_locidx_t
t8_ghost_get_ghost_id_in_tree (t8_forest_t forest, const t8_locidx_t lghost_tree, const t8_element_t *ghost_element);

/** Get a pointer to the ghost element array of a ghost tree.
* \param [in] forest The forest. Ghost layer must exist.
* \param [in] lghost_tree The ghost tree id of a ghost tree.
Expand Down
54 changes: 0 additions & 54 deletions src/t8_forest/t8_forest_private.c

This file was deleted.

94 changes: 94 additions & 0 deletions src/t8_forest/t8_forest_private.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
This file is part of t8code.
t8code is a C library to manage a collection (a forest) of multiple
connected adaptive space-trees of general element classes in parallel.

Copyright (C) 2015 the developers

t8code is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

t8code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with t8code; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <t8_forest/t8_forest_types.h>
#include <t8_forest/t8_forest_private.h>
#include <t8_forest/t8_forest_general.h>
#include <t8_element.hxx>
#include <t8_data/t8_element_array_iterator.hxx>
#include <algorithm>

T8_EXTERN_C_BEGIN ();

const t8_element_t *
t8_forest_get_tree_element (t8_tree_t tree, t8_locidx_t elem_in_tree)
{
T8_ASSERT (tree != NULL);
T8_ASSERT (0 <= elem_in_tree && elem_in_tree < t8_forest_get_tree_element_count (tree));
return t8_element_array_index_locidx (&tree->elements, elem_in_tree);
}

t8_element_t *
t8_forest_get_tree_element_mutable (t8_tree_t tree, t8_locidx_t elem_in_tree)
{
return (t8_element_t *) t8_forest_get_tree_element (tree, elem_in_tree);
}

const t8_element_array_t *
t8_forest_get_tree_element_array (const t8_forest_t forest, t8_locidx_t ltreeid)
{
T8_ASSERT (t8_forest_is_committed (forest));
T8_ASSERT (0 <= ltreeid && ltreeid < t8_forest_get_num_local_trees (forest));

return &t8_forest_get_tree (forest, ltreeid)->elements;
}

t8_element_array_t *
t8_forest_get_tree_element_array_mutable (const t8_forest_t forest, t8_locidx_t ltreeid)
{
return (t8_element_array_t *) t8_forest_get_tree_element_array (forest, ltreeid);
}

/** \brief Search for a linear element id (at forest->maxlevel) in a sorted array of
* elements. If the element does not exist, return the largest index i
* such that the element at position i has a smaller id than the given one.
* If no such i exists, return -1.
*/
t8_locidx_t
t8_forest_bin_search_lower (const t8_element_array_t *elements, const t8_linearidx_t element_id, const int maxlevel)
{
const t8_eclass_scheme_c *ts = t8_element_array_get_scheme (elements);
/* At first, we check whether any element has smaller id than the
* given one. */
const t8_element_t *query = t8_element_array_index_int (elements, 0);
const t8_linearidx_t query_id = ts->t8_element_get_linear_id (query, maxlevel);
if (query_id > element_id) {
/* No element has id smaller than the given one. */
return -1;
}

/* We search for the first element in the array that is greater than the given element id. */
auto elem_iter = std::upper_bound (
t8_element_array_begin (elements), t8_element_array_end (elements), element_id,
[&maxlevel, &ts] (const t8_linearidx_t element_id_, const t8_element_array_iterator::value_type &elem_ptr) {
return (element_id_ < ts->t8_element_get_linear_id (elem_ptr, maxlevel));
});

/* After we found the element with an id greater than the given one, we are able to jump one index back.
* This guarantees us that the element at (index - 1) is smaller or equal to the given element id.
* In case we do not find an element that is greater than the given element_id, the binary search returns
* the end-iterator of the element array. In that case, we want to return the last index from the element
* array. */
return elem_iter.GetCurrentIndex () - 1;
}

T8_EXTERN_C_END ();
12 changes: 12 additions & 0 deletions src/t8_forest/t8_forest_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ t8_forest_get_tree_element_array (t8_forest_t forest, t8_locidx_t ltreeid);
t8_element_array_t *
t8_forest_get_tree_element_array_mutable (const t8_forest_t forest, t8_locidx_t ltreeid);

/** Search for a linear element id (at forest->maxlevel) in a sorted array of
* elements. If the element does not exist, return the largest index i
* such that the element at position i has a smaller id than the given one.
* If no such i exists, return -1.
holke marked this conversation as resolved.
Show resolved Hide resolved
* \note This search does not work for subelements.
* \param [in] elements The array of elements.
* \param [in] element_id The linear id of the element to search for.
* \param [in] maxlevel The maximum level of the elements.
*/
t8_locidx_t
t8_forest_bin_search_lower (const t8_element_array_t *elements, const t8_linearidx_t element_id, const int maxlevel);

/** Find the owner process of a given element, deprecated version.
* Use t8_forest_element_find_owner instead.
* \param [in] forest The forest.
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ add_t8_test( NAME t8_gtest_find_owner_parallel SOURCES t8_gtest_main.cx
add_t8_test( NAME t8_gtest_user_data_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_user_data.cxx )
add_t8_test( NAME t8_gtest_transform_serial SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_transform.cxx )
add_t8_test( NAME t8_gtest_ghost_exchange_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_ghost_exchange.cxx )
add_t8_test( NAME t8_gtest_ghost_in_tree SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_ghost_in_tree.cxx )
add_t8_test( NAME t8_gtest_ghost_delete_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_ghost_delete.cxx )
add_t8_test( NAME t8_gtest_ghost_and_owner_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_ghost_and_owner.cxx )
add_t8_test( NAME t8_gtest_balance_parallel SOURCES t8_gtest_main.cxx t8_forest/t8_gtest_balance.cxx )
Expand Down
Loading
Loading