Skip to content

Commit

Permalink
add reference euler path computation kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
upsj committed Jan 10, 2025
1 parent 56e16e6 commit 64800aa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
31 changes: 22 additions & 9 deletions reference/factorization/elimination_forest_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void compute_postorder(
GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(
GKO_DECLARE_ELIMINATION_FOREST_COMPUTE_POSTORDER);

/*

template <typename IndexType>
IndexType traverse_euler_path(const IndexType* child_ptrs,
const IndexType* children, IndexType node,
Expand All @@ -254,14 +254,26 @@ IndexType traverse_euler_path(const IndexType* child_ptrs,
{
const auto child_begin = child_ptrs[node];
const auto child_end = child_ptrs[node + 1];
if (child_begin == child_end) {
euler_path[index] = node;
euler_first[node] = index;
euler_level[index] = level;
return index + 1;
}
euler_path[index] = node;
euler_first[node] = index;
euler_level[index] = level;
index++;
for (const auto child_idx : irange{child_begin, child_end}) {
const auto child = children[child_idx];
index = traverse_postorder(child_ptrs, children, child, index,
postorder, inv_postorder);
index =
traverse_euler_path(child_ptrs, children, child, index, level + 1,
euler_path, euler_first, euler_level);
euler_path[index] = node;
euler_level[index] = level;
index++;
}
postorder[index] = node;
inv_postorder[node] = index;
return index + 1;
return index;
}


Expand All @@ -280,13 +292,14 @@ void compute_euler_path(
IndexType index{};
for (const auto root_idx : irange{root_begin, root_end}) {
const auto root = children[root_idx];
index = traverse_euler_path(child_ptrs, children, root, index,
postorder, inv_postorder);
index =
traverse_euler_path(child_ptrs, children, root, index, IndexType{0},
euler_path, first_visit, euler_levels);
}
}

GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(
GKO_DECLARE_ELIMINATION_FOREST_COMPUTE_EULER_PATH);*/
GKO_DECLARE_ELIMINATION_FOREST_COMPUTE_EULER_PATH);


} // namespace elimination_forest
Expand Down
43 changes: 35 additions & 8 deletions reference/test/factorization/cholesky_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "core/matrix/csr_lookup.hpp"
#include "core/test/utils.hpp"
#include "core/test/utils/assertions.hpp"
#include "ginkgo/core/base/types.hpp"
#include "matrices/config.hpp"


Expand Down Expand Up @@ -434,16 +435,19 @@ TYPED_TEST(Cholesky, KernelComputeLevels)
template <typename IndexType>
void reference_euler_path(const IndexType* child_ptrs,
const IndexType* children, IndexType node,
IndexType level,
std::vector<std::pair<IndexType, IndexType>>& path)
IndexType level, std::vector<IndexType>& path,
std::vector<IndexType>& levels)
{
path.emplace_back(node, level);
path.emplace_back(node);
levels.emplace_back(level);
const auto child_begin = child_ptrs[node];
const auto child_end = child_ptrs[node + 1];
for (const auto child_idx : gko::irange{child_begin, child_end}) {
const auto child = children[child_idx];
reference_euler_path(child_ptrs, children, child, level + 1, path);
path.emplace_back(node, level);
reference_euler_path(child_ptrs, children, child, level + 1, path,
levels);
path.emplace_back(node);
levels.emplace_back(level);
}
}

Expand All @@ -460,20 +464,43 @@ TYPED_TEST(Cholesky, KernelComputeEulerPath)
gko::factorization::compute_elimination_forest(this->mtx.get(),
forest);
gko::array<index_type> levels{this->ref, size};
gko::array<index_type> euler_path{this->ref, size};
gko::array<index_type> euler_first{this->ref, size};
gko::array<index_type> euler_levels{this->ref, size};
gko::array<index_type> euler_path{this->ref, 2 * size};
gko::array<index_type> euler_first{this->ref, 2 * size};
gko::array<index_type> euler_levels{this->ref, 2 * size};
gko::array<index_type> subtree_sizes{this->ref, size};
euler_path.fill(gko::invalid_index<index_type>());
euler_levels.fill(gko::invalid_index<index_type>());
gko::kernels::reference::elimination_forest::
compute_subtree_euler_path_sizes(this->ref, *forest,
subtree_sizes.get_data());
gko::kernels::reference::elimination_forest::compute_levels(
this->ref, *forest, levels.get_data());
std::vector<index_type> ref_path;
std::vector<index_type> ref_levels;
const auto child_ptrs = forest->child_ptrs.get_const_data();
const auto children = forest->children.get_const_data();
for (auto root_idx :
gko::irange{child_ptrs[size], child_ptrs[size + 1]}) {
const auto root = forest->children.get_const_data()[root_idx];
reference_euler_path(child_ptrs, children, root, index_type{},
ref_path, ref_levels);
}
ASSERT_LT(ref_path.size(), 2 * size);
ASSERT_LT(ref_levels.size(), 2 * size);
ref_path.resize(2 * size, gko::invalid_index<index_type>());
ref_levels.resize(2 * size, gko::invalid_index<index_type>());
const gko::array<index_type> ref_path_array{
this->ref, ref_path.begin(), ref_path.end()};
const gko::array<index_type> ref_levels_array{
this->ref, ref_levels.begin(), ref_levels.end()};

gko::kernels::reference::elimination_forest::compute_euler_path(
this->ref, *forest, subtree_sizes.get_const_data(),
levels.get_const_data(), euler_path.get_data(),
euler_first.get_data(), euler_levels.get_data());

GKO_ASSERT_ARRAY_EQ(ref_path_array, euler_path);
GKO_ASSERT_ARRAY_EQ(ref_levels_array, euler_levels);
},
true);
}
Expand Down

0 comments on commit 64800aa

Please sign in to comment.