From bb50b9a6ff2272536f00ec10e5753037f9377439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com> Date: Fri, 21 Feb 2025 19:23:45 +0100 Subject: [PATCH] Begin unit tests writing for load balancing --- src/utils/Partitioner.cpp | 2 ++ src/utils/PartitionerOptions.hpp | 6 ++-- tests/test_Partitioner.cpp | 53 +++++++++++++++++++++++++------ tests/test_PartitionerOptions.cpp | 18 +++++++++++ 4 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 tests/test_PartitionerOptions.cpp diff --git a/src/utils/Partitioner.cpp b/src/utils/Partitioner.cpp index 4f53d322f..9ef628245 100644 --- a/src/utils/Partitioner.cpp +++ b/src/utils/Partitioner.cpp @@ -15,8 +15,10 @@ Partitioner::partition(const CRSGraph& graph) case PartitionerLibrary::ptscotch: { return PTScotchPartitioner::partition(graph); } + // LCOV_EXCL_START default: { throw UnexpectedError("invalid partition library"); } + // LCOV_EXCL_STOP } } diff --git a/src/utils/PartitionerOptions.hpp b/src/utils/PartitionerOptions.hpp index e4157f765..b17180e0b 100644 --- a/src/utils/PartitionerOptions.hpp +++ b/src/utils/PartitionerOptions.hpp @@ -29,7 +29,7 @@ name(const PartitionerLibrary library) case PartitionerLibrary::PT__end: { } } - throw UnexpectedError("Linear system library name is not defined!"); + throw UnexpectedError("Partitioner library name is not defined!"); } template <typename PartitionerEnumType> @@ -43,7 +43,9 @@ getPartitionerEnumFromName(const std::string& enum_name) return PartitionerEnumType{enum_value}; } } - throw NormalError(std::string{"could not find '"} + enum_name + "' associate type!"); + // LCOV_EXCL_START + throw UnexpectedError(std::string{"could not find '"} + enum_name + "' associate type!"); + // LCOV_EXCL_STOP } class PartitionerOptions diff --git a/tests/test_Partitioner.cpp b/tests/test_Partitioner.cpp index 78a427f6d..3638a3926 100644 --- a/tests/test_Partitioner.cpp +++ b/tests/test_Partitioner.cpp @@ -11,8 +11,6 @@ TEST_CASE("Partitioner", "[utils]") { SECTION("one graph split to all") { - Partitioner partitioner; - std::vector<int> entries_vector; std::vector<int> neighbors_vector; @@ -70,17 +68,54 @@ TEST_CASE("Partitioner", "[utils]") CRSGraph graph{entries, neighbors}; - Array<int> partitioned = partitioner.partition(graph); + SECTION("ParMETIS") + { + PartitionerOptions partitioner_options; + partitioner_options.library() = PartitionerLibrary::parmetis; + Partitioner partitioner{partitioner_options}; - REQUIRE((partitioned.size() + 1) == entries.size()); + Array<int> partitioned = partitioner.partition(graph); - if (parallel::rank() == 0) { - std::set<int> assigned_ranks; - for (size_t i = 0; i < partitioned.size(); ++i) { - assigned_ranks.insert(partitioned[i]); + REQUIRE((partitioned.size() + 1) == entries.size()); + +#ifdef PUGS_HAS_PARMETIS + if (parallel::rank() == 0) { + std::set<int> assigned_ranks; + for (size_t i = 0; i < partitioned.size(); ++i) { + assigned_ranks.insert(partitioned[i]); + } + + REQUIRE(assigned_ranks.size() == parallel::size()); } +#else // PUGS_HAS_PARMETIS + REQUIRE(min(partitionned == 0)); + REQUIRE(max(partitionned == 0)); +#endif // PUGS_HAS_PARMETIS + } - REQUIRE(assigned_ranks.size() == parallel::size()); + SECTION("PTScotch") + { + PartitionerOptions partitioner_options; + partitioner_options.library() = PartitionerLibrary::ptscotch; + Partitioner partitioner{partitioner_options}; + + Array<int> partitioned = partitioner.partition(graph); + + REQUIRE((partitioned.size() + 1) == entries.size()); + +#ifdef PUGS_HAS_PTSCOTCH + if (parallel::rank() == 0) { + std::set<int> assigned_ranks; + for (size_t i = 0; i < partitioned.size(); ++i) { + assigned_ranks.insert(partitioned[i]); + } + + REQUIRE(assigned_ranks.size() == parallel::size()); + } +#else // PUGS_HAS_PTSCOTCH + REQUIRE(min(partitionned == 0)); + REQUIRE(max(partitionned == 0)); +#endif // PUGS_HAS_PTSCOTCH } } } diff --git a/tests/test_PartitionerOptions.cpp b/tests/test_PartitionerOptions.cpp new file mode 100644 index 000000000..67c016221 --- /dev/null +++ b/tests/test_PartitionerOptions.cpp @@ -0,0 +1,18 @@ +#include <catch2/catch_all.hpp> +#include <catch2/catch_test_macros.hpp> + +#include <utils/PartitionerOptions.hpp> + +// clazy:excludeall=non-pod-global-static + +TEST_CASE("PartitionerOptions", "[utils]") +{ + SECTION("name") + { + REQUIRE(name(PartitionerLibrary::parmetis) == "ParMetis"); + REQUIRE(name(PartitionerLibrary::ptscotch) == "PTScotch"); + REQUIRE_THROWS_WITH(PartitionerLibrary::PT__end, "unexpected error: Partitioner library name is not defined!"); + + WARN("not finished"); + } +} -- GitLab