diff --git a/tests/splines/CMakeLists.txt b/tests/splines/CMakeLists.txt index de6476f24..f8959ffc2 100644 --- a/tests/splines/CMakeLists.txt +++ b/tests/splines/CMakeLists.txt @@ -33,17 +33,17 @@ target_link_libraries(bsplines_tests DDC::DDC ) -add_executable(matrix_tests +add_executable(splines_linear_problem_tests ../main.cpp - matrix.cpp + splines_linear_problem.cpp ) -target_compile_features(matrix_tests PUBLIC cxx_std_17) -target_link_libraries(matrix_tests +target_compile_features(splines_linear_problem_tests PUBLIC cxx_std_17) +target_link_libraries(splines_linear_problem_tests PUBLIC GTest::gtest DDC::DDC ) -gtest_discover_tests(matrix_tests DISCOVERY_MODE PRE_TEST) +gtest_discover_tests(splines_linear_problem_tests DISCOVERY_MODE PRE_TEST) foreach(DEGREE_X RANGE "${SPLINES_TEST_DEGREE_MIN}" "${SPLINES_TEST_DEGREE_MAX}") foreach(BSPLINES_TYPE "BSPLINES_TYPE_UNIFORM" "BSPLINES_TYPE_NON_UNIFORM") diff --git a/tests/splines/matrix.cpp b/tests/splines/splines_linear_problem.cpp similarity index 65% rename from tests/splines/matrix.cpp rename to tests/splines/splines_linear_problem.cpp index 19f5d6ae0..5b1f139a1 100644 --- a/tests/splines/matrix.cpp +++ b/tests/splines/splines_linear_problem.cpp @@ -80,17 +80,18 @@ namespace DDC_HIP_5_7_ANONYMOUS_NAMESPACE_WORKAROUND(MATRIX_CPP) } void solve_and_validate( - ddc::detail::SplinesLinearProblem & matrix) + ddc::detail::SplinesLinearProblem< + Kokkos::DefaultExecutionSpace> & splines_linear_problem) { - const std::size_t N = matrix.size(); + const std::size_t N = splines_linear_problem.size(); std::vector val_ptr(N * N); ddc::detail::SplinesLinearProblem::MultiRHS val(val_ptr.data(), N, N); - copy_matrix(val, matrix); + copy_matrix(val, splines_linear_problem); - matrix.setup_solver(); + splines_linear_problem.setup_solver(); Kokkos::DualView inv_ptr("inv_ptr", N * N); ddc::detail::SplinesLinearProblem::MultiRHS @@ -98,8 +99,9 @@ namespace DDC_HIP_5_7_ANONYMOUS_NAMESPACE_WORKAROUND(MATRIX_CPP) fill_identity(inv); inv_ptr.modify_host(); inv_ptr.sync_device(); - matrix.solve(ddc::detail::SplinesLinearProblem< - Kokkos::DefaultExecutionSpace>::MultiRHS(inv_ptr.d_view.data(), N, N)); + splines_linear_problem.solve( + ddc::detail::SplinesLinearProblem< + Kokkos::DefaultExecutionSpace>::MultiRHS(inv_ptr.d_view.data(), N, N)); inv_ptr.modify_device(); inv_ptr.sync_host(); @@ -109,7 +111,7 @@ namespace DDC_HIP_5_7_ANONYMOUS_NAMESPACE_WORKAROUND(MATRIX_CPP) fill_identity(inv_tr); inv_tr_ptr.modify_host(); inv_tr_ptr.sync_device(); - matrix + splines_linear_problem .solve(ddc::detail::SplinesLinearProblem:: MultiRHS(inv_tr_ptr.d_view.data(), N, N), true); @@ -122,276 +124,281 @@ namespace DDC_HIP_5_7_ANONYMOUS_NAMESPACE_WORKAROUND(MATRIX_CPP) } // namespace ) -TEST(MatrixSparse, Formatting) +TEST(SplinesLinearProblemSparse, Formatting) { - ddc::detail::SplinesLinearProblemSparse matrix(2); - matrix.set_element(0, 0, 1); - matrix.set_element(0, 1, 2); - matrix.set_element(1, 0, 3); - matrix.set_element(1, 1, 4); + ddc::detail::SplinesLinearProblemSparse splines_linear_problem( + 2); + splines_linear_problem.set_element(0, 0, 1); + splines_linear_problem.set_element(0, 1, 2); + splines_linear_problem.set_element(1, 0, 3); + splines_linear_problem.set_element(1, 1, 4); std::stringstream ss; - ss << matrix; + ss << splines_linear_problem; EXPECT_EQ(ss.str(), " 1.000 2.000\n 3.000 4.000\n"); } -TEST(Matrix, Dense) +TEST(SplinesLinearProblem, Dense) { std::size_t const N = 10; std::size_t const k = 10; - std::unique_ptr> matrix - = std::make_unique< + std::unique_ptr> + splines_linear_problem = std::make_unique< ddc::detail::SplinesLinearProblemDense>(N); // Build a non-symmetric full-rank matrix (without zero) for (std::size_t i(0); i < N; ++i) { - matrix->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); + splines_linear_problem->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST(Matrix, Band) +TEST(SplinesLinearProblem, Band) { std::size_t const N = 10; std::size_t const k = 3; - std::unique_ptr> matrix - = std::make_unique< + std::unique_ptr> + splines_linear_problem = std::make_unique< ddc::detail::SplinesLinearProblemBand>(N, k, k); // Build a non-symmetric full-rank band matrix for (std::size_t i(0); i < N; ++i) { - matrix->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); + splines_linear_problem->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST(Matrix, PDSBand) +TEST(SplinesLinearProblem, PDSBand) { std::size_t const N = 10; std::size_t const k = 3; - std::unique_ptr> matrix - = std::make_unique< + std::unique_ptr> + splines_linear_problem = std::make_unique< ddc::detail::SplinesLinearProblemPDSBand>(N, k); // Build a positive-definite symmetric full-rank band matrix for (std::size_t i(0); i < N; ++i) { - matrix->set_element(i, i, 2.0 * k + 1); + splines_linear_problem->set_element(i, i, 2.0 * k + 1); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -1.0); + splines_linear_problem->set_element(i, j, -1.0); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -1.0); + splines_linear_problem->set_element(i, j, -1.0); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST(Matrix, PDSTridiag) +TEST(SplinesLinearProblem, PDSTridiag) { std::size_t const N = 10; std::size_t const k = 1; - std::unique_ptr> matrix - = std::make_unique< + std::unique_ptr> + splines_linear_problem = std::make_unique< ddc::detail::SplinesLinearProblemPDSTridiag>(N); // Build a positive-definite symmetric full-rank tridiagonal matrix for (std::size_t i(0); i < N; ++i) { - matrix->set_element(i, i, 2.0 * k + 1); + splines_linear_problem->set_element(i, i, 2.0 * k + 1); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -1.0); + splines_linear_problem->set_element(i, j, -1.0); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -1.0); + splines_linear_problem->set_element(i, j, -1.0); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST(Matrix, 2x2Blocks) +TEST(SplinesLinearProblem, 2x2Blocks) { std::size_t const N = 10; std::size_t const k = 10; std::unique_ptr> top_left_block = std::make_unique< ddc::detail::SplinesLinearProblemDense>(7); - std::unique_ptr> matrix - = std::make_unique> + splines_linear_problem = std::make_unique>(N, std::move(top_left_block)); // Build a non-symmetric full-rank matrix (without zero) for (std::size_t i(0); i < N; ++i) { - matrix->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); + splines_linear_problem->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST(Matrix, 3x3Blocks) +TEST(SplinesLinearProblem, 3x3Blocks) { std::size_t const N = 10; std::size_t const k = 10; std::unique_ptr> center_block = std::make_unique< ddc::detail::SplinesLinearProblemDense>(N - 5); - std::unique_ptr> matrix - = std::make_unique> + splines_linear_problem = std::make_unique>(N, 2, std::move(center_block)); // Build a non-symmetric full-rank matrix (without zero) for (std::size_t i(0); i < N; ++i) { std::cout << i; - matrix->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); + splines_linear_problem->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -class MatrixSizesFixture : public testing::TestWithParam> +class SplinesLinearProblemSizesFixture + : public testing::TestWithParam> { }; -TEST_P(MatrixSizesFixture, NonSymmetric) +TEST_P(SplinesLinearProblemSizesFixture, NonSymmetric) { auto const [N, k] = GetParam(); - std::unique_ptr> matrix - = ddc::detail::SplinesLinearProblemMaker::make_new_band< + std::unique_ptr> + splines_linear_problem = ddc::detail::SplinesLinearProblemMaker::make_new_band< Kokkos::DefaultExecutionSpace>(N, k, k, false); // Build a non-symmetric full-rank band matrix for (std::size_t i(0); i < N; ++i) { - matrix->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); + splines_linear_problem->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST_P(MatrixSizesFixture, PositiveDefiniteSymmetric) +TEST_P(SplinesLinearProblemSizesFixture, PositiveDefiniteSymmetric) { auto const [N, k] = GetParam(); - std::unique_ptr> matrix - = ddc::detail::SplinesLinearProblemMaker::make_new_band< + std::unique_ptr> + splines_linear_problem = ddc::detail::SplinesLinearProblemMaker::make_new_band< Kokkos::DefaultExecutionSpace>(N, k, k, true); // Build a positive-definite symmetric full-rank band matrix for (std::size_t i(0); i < N; ++i) { - matrix->set_element(i, i, 2.0 * k + 1); + splines_linear_problem->set_element(i, i, 2.0 * k + 1); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -1.0); + splines_linear_problem->set_element(i, j, -1.0); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -1.0); + splines_linear_problem->set_element(i, j, -1.0); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST_P(MatrixSizesFixture, OffsetBanded) +TEST_P(SplinesLinearProblemSizesFixture, OffsetBanded) { auto const [N, k] = GetParam(); - std::unique_ptr> matrix - = ddc::detail::SplinesLinearProblemMaker::make_new_band< + std::unique_ptr> + splines_linear_problem = ddc::detail::SplinesLinearProblemMaker::make_new_band< Kokkos::DefaultExecutionSpace>(N, 0, 2 * k, true); // Build a positive-definite symmetric full-rank band matrix permuted in such a way the band is shifted for (std::size_t i(0); i < N; ++i) { for (std::size_t j(i); j < std::min(N, i + k); ++j) { - matrix->set_element(i, i, -1.0); + splines_linear_problem->set_element(i, i, -1.0); } if (i + k < N) { - matrix->set_element(i, i + k, 2.0 * k + 1); + splines_linear_problem->set_element(i, i + k, 2.0 * k + 1); } for (std::size_t j(i + k + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -1.0); + splines_linear_problem->set_element(i, j, -1.0); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST_P(MatrixSizesFixture, 2x2Blocks) +TEST_P(SplinesLinearProblemSizesFixture, 2x2Blocks) { auto const [N, k] = GetParam(); - std::unique_ptr> matrix + std::unique_ptr> + splines_linear_problem = ddc::detail::SplinesLinearProblemMaker::make_new_block_matrix_with_band_main_block< Kokkos::DefaultExecutionSpace>(N, k, k, false, 3); // Build a non-symmetric full-rank band matrix for (std::size_t i(0); i < N; ++i) { - matrix->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); + splines_linear_problem->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST_P(MatrixSizesFixture, 3x3Blocks) +TEST_P(SplinesLinearProblemSizesFixture, 3x3Blocks) { auto const [N, k] = GetParam(); - std::unique_ptr> matrix + std::unique_ptr> + splines_linear_problem = ddc::detail::SplinesLinearProblemMaker::make_new_block_matrix_with_band_main_block< Kokkos::DefaultExecutionSpace>(N, k, k, false, 3, 2); // Build a non-symmetric full-rank band matrix for (std::size_t i(0); i < N; ++i) { - matrix->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); + splines_linear_problem->set_element(i, i, 3. / 4 * ((N + 1) * i + 1)); for (std::size_t j(std::max(0, int(i) - int(k))); j < i; ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } for (std::size_t j(i + 1); j < std::min(N, i + k + 1); ++j) { - matrix->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); + splines_linear_problem->set_element(i, j, -(1. / 4) / k * (N * i + j + 1)); } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } -TEST_P(MatrixSizesFixture, PeriodicBand) +TEST_P(SplinesLinearProblemSizesFixture, PeriodicBand) { auto const [N, k] = GetParam(); // Build a full-rank periodic band matrix permuted in such a way the band is shifted for (std::ptrdiff_t s(-k + k / 2 + 1); s < static_cast(k - k / 2); ++s) { - std::unique_ptr> matrix + std::unique_ptr> + splines_linear_problem = ddc::detail::SplinesLinearProblemMaker::make_new_periodic_band_matrix< Kokkos::DefaultExecutionSpace>( N, @@ -403,41 +410,41 @@ TEST_P(MatrixSizesFixture, PeriodicBand) std::ptrdiff_t diag = ddc::detail:: modulo(static_cast(j - i), static_cast(N)); if (diag == s || diag == N + s) { - matrix->set_element(i, j, 2.0 * k + 1); + splines_linear_problem->set_element(i, j, 2.0 * k + 1); } else if (diag <= s + k || diag >= N + s - k) { - matrix->set_element(i, j, -1.); + splines_linear_problem->set_element(i, j, -1.); } } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } } -TEST_P(MatrixSizesFixture, Sparse) +TEST_P(SplinesLinearProblemSizesFixture, Sparse) { auto const [N, k] = GetParam(); - std::unique_ptr> matrix - = ddc::detail::SplinesLinearProblemMaker::make_new_sparse< + std::unique_ptr> + splines_linear_problem = ddc::detail::SplinesLinearProblemMaker::make_new_sparse< Kokkos::DefaultExecutionSpace>(N); // Build a positive-definite symmetric diagonal-dominant band matrix (stored as a sparse matrix) for (std::size_t i(0); i < N; ++i) { for (std::size_t j(0); j < N; ++j) { if (i == j) { - matrix->set_element(i, j, 3. / 4); + splines_linear_problem->set_element(i, j, 3. / 4); } else if ( std::abs(static_cast(j - i)) <= static_cast(k)) { - matrix->set_element(i, j, -(1. / 4) / k); + splines_linear_problem->set_element(i, j, -(1. / 4) / k); } } } - solve_and_validate(*matrix); + solve_and_validate(*splines_linear_problem); } INSTANTIATE_TEST_SUITE_P( MyGroup, - MatrixSizesFixture, + SplinesLinearProblemSizesFixture, testing::Combine(testing::Values(10, 20), testing::Range(1, 7)));