diff --git a/core/test/matrix/fbcsr_sample.hpp b/core/test/matrix/fbcsr_sample.hpp index dc38f5d4954..feba93f3b6c 100644 --- a/core/test/matrix/fbcsr_sample.hpp +++ b/core/test/matrix/fbcsr_sample.hpp @@ -462,17 +462,16 @@ class FbcsrSampleComplex { v[i] = 0.15 + fbcsr_test_offset; } - using namespace std::complex_literals; - v[0] = 1.0 + 1.15i; - v[2] = 2.0 + 2.15i; - v[1] = 3.0 - 3.15i; - v[3] = 0.0 - 0.15i; + v[0] = value_type{1.0, 1.15}; + v[2] = value_type{2.0, 2.15}; + v[1] = value_type{3.0, -3.15}; + v[3] = value_type{0.0, -0.15}; v[9] = 0.0; v[11] = 0.0; - v[12] = -12.0 + 12.15i; - v[14] = -1.0 + 1.15i; - v[13] = -2.0 - 2.15i; - v[15] = -11.0 - 11.15i; + v[12] = -value_type{12.0, 12.15}; + v[14] = -value_type{1.0, 1.15}; + v[13] = -value_type{2.0, -2.15}; + v[15] = -value_type{11.0, -11.15}; return Fbcsr::create(exec, gko::dim<2>{static_cast(nrows), @@ -491,17 +490,16 @@ class FbcsrSampleComplex { v[i] = 0.15 + fbcsr_test_offset; } - using namespace std::complex_literals; - v[0] = 1.0 + 1.15i; - v[1] = 2.0 + 2.15i; - v[2] = 3.0 - 3.15i; - v[3] = 0.0 - 0.15i; + v[0] = value_type{1.0, 1.15}; + v[1] = value_type{2.0, 2.15}; + v[2] = value_type{3.0, -3.15}; + v[3] = value_type{0.0, -0.15}; v[10] = 0.0; v[11] = 0.0; - v[12] = -12.0 + 12.15i; - v[13] = -1.0 + 1.15i; - v[14] = -2.0 - 2.15i; - v[15] = -11.0 - 11.15i; + v[12] = -value_type{12.0, 12.15}; + v[13] = -value_type{1.0, 1.15}; + v[14] = -value_type{2.0, -2.15}; + v[15] = -value_type{11.0, -11.15}; return Csr::create(exec, gko::dim<2>{static_cast(nrows), diff --git a/core/test/utils.hpp b/core/test/utils.hpp index b37f6981e7c..c99b323108f 100644 --- a/core/test/utils.hpp +++ b/core/test/utils.hpp @@ -34,6 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define GKO_CORE_TEST_UTILS_HPP_ +#include #include #include #include @@ -189,7 +190,8 @@ struct reduction_factor { using nc_output = remove_complex; using nc_precision = remove_complex; static constexpr nc_output value{ - std::numeric_limits::epsilon() * nc_output{10}}; + std::numeric_limits::epsilon() * nc_output{10} * + (gko::is_complex() ? nc_output{1.4142} : one())}; }; diff --git a/core/test/utils/fb_matrix_generator.hpp b/core/test/utils/fb_matrix_generator.hpp index 99c78f53439..80db76aeda4 100644 --- a/core/test/utils/fb_matrix_generator.hpp +++ b/core/test/utils/fb_matrix_generator.hpp @@ -160,7 +160,6 @@ std::unique_ptr> generate_fbcsr_from_csr( const IndexType* const row_ptrs = fmtx->get_const_row_ptrs(); const IndexType* const col_idxs = fmtx->get_const_col_idxs(); - const IndexType nnz = nbnz * bs2; ValueType* const vals = fmtx->get_values(); std::uniform_real_distribution> off_diag_dist(-1.0, 1.0); diff --git a/cuda/test/CMakeLists.txt b/cuda/test/CMakeLists.txt index 83d7b39f35e..36a876b1377 100644 --- a/cuda/test/CMakeLists.txt +++ b/cuda/test/CMakeLists.txt @@ -2,10 +2,6 @@ include(${PROJECT_SOURCE_DIR}/cmake/create_test.cmake) add_subdirectory(base) add_subdirectory(components) -add_subdirectory(factorization) -add_subdirectory(matrix) -add_subdirectory(preconditioner) add_subdirectory(reorder) add_subdirectory(solver) -add_subdirectory(stop) add_subdirectory(utils) diff --git a/cuda/test/components/CMakeLists.txt b/cuda/test/components/CMakeLists.txt index d2f8152a787..89e374431c4 100644 --- a/cuda/test/components/CMakeLists.txt +++ b/cuda/test/components/CMakeLists.txt @@ -2,4 +2,3 @@ ginkgo_create_cuda_test(cooperative_groups) ginkgo_create_cuda_test(merging) ginkgo_create_cuda_test(searching) ginkgo_create_cuda_test(sorting) -ginkgo_create_test(prefix_sum_kernels) diff --git a/cuda/test/components/prefix_sum_kernels.cpp b/cuda/test/components/prefix_sum_kernels.cpp deleted file mode 100644 index 67004a2ec90..00000000000 --- a/cuda/test/components/prefix_sum_kernels.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/components/prefix_sum_kernels.hpp" - - -#include -#include -#include - - -#include - - -#include - - -#include "cuda/test/utils.hpp" - - -namespace { - - -class PrefixSum : public ::testing::Test { -protected: - using index_type = gko::int32; - PrefixSum() - : ref(gko::ReferenceExecutor::create()), - exec(gko::CudaExecutor::create(0, ref)), - rand(293), - total_size(42793), - vals(ref, total_size), - dvals(exec) - { - std::uniform_int_distribution dist(0, 1000); - for (gko::size_type i = 0; i < total_size; ++i) { - vals.get_data()[i] = dist(rand); - } - dvals = vals; - } - - void test(gko::size_type size) - { - gko::kernels::reference::components::prefix_sum(ref, vals.get_data(), - size); - gko::kernels::cuda::components::prefix_sum(exec, dvals.get_data(), - size); - - GKO_ASSERT_ARRAY_EQ(vals, dvals); - } - - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand; - gko::size_type total_size; - gko::array vals; - gko::array dvals; -}; - - -TEST_F(PrefixSum, SmallEqualsReference) { test(100); } - - -TEST_F(PrefixSum, BigEqualsReference) { test(total_size); } - - -} // namespace diff --git a/cuda/test/factorization/CMakeLists.txt b/cuda/test/factorization/CMakeLists.txt deleted file mode 100644 index 6fb7f5fea43..00000000000 --- a/cuda/test/factorization/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -ginkgo_create_test(ic_kernels) -ginkgo_create_test(ilu_kernels) -ginkgo_create_test(par_ic_kernels) -ginkgo_create_test(par_ict_kernels) -ginkgo_create_test(par_ilu_kernels) -ginkgo_create_test(par_ilut_kernels) diff --git a/cuda/test/factorization/ic_kernels.cpp b/cuda/test/factorization/ic_kernels.cpp deleted file mode 100644 index d79edbf58dd..00000000000 --- a/cuda/test/factorization/ic_kernels.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include -#include -#include - - -#include - - -#include -#include - - -#include "core/test/utils/unsort_matrix.hpp" -#include "cuda/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class Ic : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Csr = gko::matrix::Csr; - - std::shared_ptr ref; - std::shared_ptr cuda; - std::default_random_engine rand_engine; - std::shared_ptr csr_ref; - std::shared_ptr csr_cuda; - - Ic() - : ref(gko::ReferenceExecutor::create()), - cuda(gko::CudaExecutor::create(0, ref)), - rand_engine(6794) - {} - - void SetUp() override - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - csr_ref = gko::read(input_file, ref); - csr_cuda = gko::clone(cuda, csr_ref); - } -}; - - -TEST_F(Ic, ComputeICIsEquivalentToRefSorted) -{ - auto ref_fact = gko::factorization::ParIc<>::build() - .with_skip_sorting(true) - .on(ref) - ->generate(csr_ref); - auto cuda_fact = gko::factorization::Ic<>::build() - .with_skip_sorting(true) - .on(cuda) - ->generate(csr_cuda); - - GKO_ASSERT_MTX_NEAR(ref_fact->get_l_factor(), cuda_fact->get_l_factor(), - 1e-14); - GKO_ASSERT_MTX_NEAR(ref_fact->get_lt_factor(), cuda_fact->get_lt_factor(), - 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_l_factor(), - cuda_fact->get_l_factor()); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_lt_factor(), - cuda_fact->get_lt_factor()); -} - - -TEST_F(Ic, ComputeICIsEquivalentToRefUnsorted) -{ - gko::test::unsort_matrix(gko::lend(csr_ref), rand_engine); - csr_cuda->copy_from(gko::lend(csr_ref)); - - auto ref_fact = - gko::factorization::ParIc<>::build().on(ref)->generate(csr_ref); - auto cuda_fact = - gko::factorization::Ic<>::build().on(cuda)->generate(csr_cuda); - - GKO_ASSERT_MTX_NEAR(ref_fact->get_l_factor(), cuda_fact->get_l_factor(), - 1e-14); - GKO_ASSERT_MTX_NEAR(ref_fact->get_lt_factor(), cuda_fact->get_lt_factor(), - 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_l_factor(), - cuda_fact->get_l_factor()); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_lt_factor(), - cuda_fact->get_lt_factor()); -} - - -TEST_F(Ic, SetsCorrectStrategy) -{ - auto cuda_fact = gko::factorization::Ic<>::build() - .with_l_strategy(std::make_shared()) - .on(cuda) - ->generate(csr_cuda); - - ASSERT_EQ(cuda_fact->get_l_factor()->get_strategy()->get_name(), - "merge_path"); - ASSERT_EQ(cuda_fact->get_lt_factor()->get_strategy()->get_name(), - "merge_path"); -} - - -} // namespace diff --git a/cuda/test/factorization/ilu_kernels.cpp b/cuda/test/factorization/ilu_kernels.cpp deleted file mode 100644 index cb40e21cf45..00000000000 --- a/cuda/test/factorization/ilu_kernels.cpp +++ /dev/null @@ -1,148 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include -#include -#include - - -#include - - -#include -#include - - -#include "core/test/utils/unsort_matrix.hpp" -#include "cuda/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class Ilu : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Csr = gko::matrix::Csr; - - std::shared_ptr ref; - std::shared_ptr cuda; - std::default_random_engine rand_engine; - std::shared_ptr csr_ref; - std::shared_ptr csr_cuda; - - Ilu() - : ref(gko::ReferenceExecutor::create()), - cuda(gko::CudaExecutor::create(0, ref)), - rand_engine(1337) - {} - - void SetUp() override - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - csr_ref = gko::read(input_file, ref); - csr_cuda = gko::clone(cuda, csr_ref); - } -}; - - -TEST_F(Ilu, ComputeILUIsEquivalentToRefSorted) -{ - auto ref_fact = gko::factorization::ParIlu<>::build() - .with_skip_sorting(true) - .on(ref) - ->generate(csr_ref); - auto cuda_fact = gko::factorization::Ilu<>::build() - .with_skip_sorting(true) - .on(cuda) - ->generate(csr_cuda); - - GKO_ASSERT_MTX_NEAR(ref_fact->get_l_factor(), cuda_fact->get_l_factor(), - 1e-14); - GKO_ASSERT_MTX_NEAR(ref_fact->get_u_factor(), cuda_fact->get_u_factor(), - 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_l_factor(), - cuda_fact->get_l_factor()); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_u_factor(), - cuda_fact->get_u_factor()); -} - - -TEST_F(Ilu, ComputeILUIsEquivalentToRefUnsorted) -{ - gko::test::unsort_matrix(gko::lend(csr_ref), rand_engine); - csr_cuda->copy_from(gko::lend(csr_ref)); - - auto ref_fact = - gko::factorization::ParIlu<>::build().on(ref)->generate(csr_ref); - auto cuda_fact = - gko::factorization::Ilu<>::build().on(cuda)->generate(csr_cuda); - - GKO_ASSERT_MTX_NEAR(ref_fact->get_l_factor(), cuda_fact->get_l_factor(), - 1e-14); - GKO_ASSERT_MTX_NEAR(ref_fact->get_u_factor(), cuda_fact->get_u_factor(), - 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_l_factor(), - cuda_fact->get_l_factor()); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_u_factor(), - cuda_fact->get_u_factor()); -} - - -TEST_F(Ilu, SetsCorrectStrategy) -{ - auto hip_fact = - gko::factorization::Ilu<>::build() - .with_l_strategy(std::make_shared()) - .with_u_strategy(std::make_shared(cuda)) - .on(cuda) - ->generate(csr_cuda); - - ASSERT_EQ(hip_fact->get_l_factor()->get_strategy()->get_name(), - "merge_path"); - ASSERT_EQ(hip_fact->get_u_factor()->get_strategy()->get_name(), - "load_balance"); -} - - -} // namespace diff --git a/cuda/test/factorization/par_ic_kernels.cpp b/cuda/test/factorization/par_ic_kernels.cpp deleted file mode 100644 index adcff582775..00000000000 --- a/cuda/test/factorization/par_ic_kernels.cpp +++ /dev/null @@ -1,164 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ic_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/matrix/csr_builder.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "cuda/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIc : public ::testing::Test { -protected: - using value_type = double; - using index_type = gko::int32; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - ParIc() - : mtx_size(624, 624), - rand_engine(43456), - ref(gko::ReferenceExecutor::create()), - cuda(gko::CudaExecutor::create(0, gko::ReferenceExecutor::create())) - { - mtx_l = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution(10, mtx_size[0]), - std::normal_distribution>(0, 10.0), - rand_engine, ref); - - dmtx_ani = Csr::create(cuda); - dmtx_l_ani = Csr::create(cuda); - dmtx_l_ani_init = Csr::create(cuda); - dmtx_l = gko::clone(cuda, mtx_l); - } - - void SetUp() - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - mtx_ani = gko::read(input_file, ref); - mtx_ani->sort_by_column_index(); - - { - mtx_l_ani = Csr::create(ref, mtx_ani->get_size()); - gko::matrix::CsrBuilder l_builder( - lend(mtx_l_ani)); - gko::kernels::reference::factorization::initialize_row_ptrs_l( - ref, lend(mtx_ani), mtx_l_ani->get_row_ptrs()); - auto l_nnz = - mtx_l_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - l_builder.get_col_idx_array().resize_and_reset(l_nnz); - l_builder.get_value_array().resize_and_reset(l_nnz); - gko::kernels::reference::factorization::initialize_l( - ref, lend(mtx_ani), lend(mtx_l_ani), false); - mtx_l_ani_init = gko::clone(ref, mtx_l_ani); - gko::kernels::reference::par_ic_factorization::init_factor( - ref, lend(mtx_l_ani_init)); - } - dmtx_ani->copy_from(lend(mtx_ani)); - dmtx_l_ani->copy_from(lend(mtx_l_ani)); - dmtx_l_ani_init->copy_from(lend(mtx_l_ani_init)); - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx_l; - std::unique_ptr mtx_ani; - std::unique_ptr mtx_l_ani; - std::unique_ptr mtx_l_ani_init; - - std::unique_ptr dmtx_l; - std::unique_ptr dmtx_ani; - std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx_l_ani_init; -}; - - -TEST_F(ParIc, KernelInitFactorIsEquivalentToRef) -{ - gko::kernels::reference::par_ic_factorization::init_factor(ref, - lend(mtx_l)); - gko::kernels::cuda::par_ic_factorization::init_factor(cuda, lend(dmtx_l)); - - GKO_ASSERT_MTX_NEAR(mtx_l, dmtx_l, r::value); -} - - -TEST_F(ParIc, KernelComputeFactorIsEquivalentToRef) -{ - auto square_size = mtx_ani->get_size(); - auto mtx_l_coo = Coo::create(ref, square_size); - mtx_l_ani->convert_to(lend(mtx_l_coo)); - auto dmtx_l_coo = gko::clone(cuda, mtx_l_coo); - - gko::kernels::reference::par_ic_factorization::compute_factor( - ref, 1, lend(mtx_l_coo), lend(mtx_l_ani_init)); - gko::kernels::cuda::par_ic_factorization::compute_factor( - cuda, 100, lend(dmtx_l_coo), lend(dmtx_l_ani_init)); - - GKO_ASSERT_MTX_NEAR(mtx_l_ani_init, dmtx_l_ani_init, 1e-4); -} - - -} // namespace diff --git a/cuda/test/factorization/par_ict_kernels.cpp b/cuda/test/factorization/par_ict_kernels.cpp deleted file mode 100644 index 8fb336777c0..00000000000 --- a/cuda/test/factorization/par_ict_kernels.cpp +++ /dev/null @@ -1,174 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ict_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/matrix/csr_builder.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "cuda/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIct : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - ParIct() - : mtx_size(436, 436), - rand_engine(45856), - ref(gko::ReferenceExecutor::create()), - cuda(gko::CudaExecutor::create(0, ref)) - { - mtx = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[1], - std::uniform_int_distribution<>(10, mtx_size[1]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - - dmtx_ani = Csr::create(cuda); - dmtx_l_ani = Csr::create(cuda); - dmtx = gko::clone(cuda, mtx); - dmtx_l = gko::clone(cuda, mtx_l); - } - - void SetUp() - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - mtx_ani = gko::read(input_file, ref); - mtx_ani->sort_by_column_index(); - - { - mtx_l_ani = Csr::create(ref, mtx_ani->get_size()); - gko::matrix::CsrBuilder l_builder( - lend(mtx_l_ani)); - gko::kernels::reference::factorization::initialize_row_ptrs_l( - ref, lend(mtx_ani), mtx_l_ani->get_row_ptrs()); - auto l_nnz = - mtx_l_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - l_builder.get_col_idx_array().resize_and_reset(l_nnz); - l_builder.get_value_array().resize_and_reset(l_nnz); - gko::kernels::reference::factorization::initialize_l( - ref, lend(mtx_ani), lend(mtx_l_ani), true); - } - dmtx_ani->copy_from(lend(mtx_ani)); - dmtx_l_ani->copy_from(lend(mtx_l_ani)); - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr mtx_ani; - std::unique_ptr mtx_l_ani; - std::unique_ptr mtx_l; - - std::unique_ptr dmtx; - std::unique_ptr dmtx_ani; - std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx_l; -}; - - -TEST_F(ParIct, KernelAddCandidatesIsEquivalentToRef) -{ - auto mtx_llh = Csr::create(ref, mtx_size); - mtx_l->apply(lend(mtx_l->conj_transpose()), lend(mtx_llh)); - auto dmtx_llh = Csr::create(cuda, mtx_size); - dmtx_llh->copy_from(lend(mtx_llh)); - auto res_mtx_l = Csr::create(ref, mtx_size); - auto dres_mtx_l = Csr::create(cuda, mtx_size); - - gko::kernels::reference::par_ict_factorization::add_candidates( - ref, lend(mtx_llh), lend(mtx), lend(mtx_l), lend(res_mtx_l)); - gko::kernels::cuda::par_ict_factorization::add_candidates( - cuda, lend(dmtx_llh), lend(dmtx), lend(dmtx_l), lend(dres_mtx_l)); - - GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_l, dres_mtx_l); - GKO_ASSERT_MTX_NEAR(res_mtx_l, dres_mtx_l, 1e-14); -} - - -TEST_F(ParIct, KernelComputeFactorIsEquivalentToRef) -{ - auto square_size = mtx_ani->get_size(); - auto mtx_l_coo = Coo::create(ref, square_size); - mtx_l_ani->convert_to(lend(mtx_l_coo)); - auto dmtx_l_coo = Coo::create(cuda, square_size); - dmtx_l_coo->copy_from(lend(mtx_l_coo)); - - gko::kernels::reference::par_ict_factorization::compute_factor( - ref, lend(mtx_ani), lend(mtx_l_ani), lend(mtx_l_coo)); - for (int i = 0; i < 20; ++i) { - gko::kernels::cuda::par_ict_factorization::compute_factor( - cuda, lend(dmtx_ani), lend(dmtx_l_ani), lend(dmtx_l_coo)); - } - - GKO_ASSERT_MTX_NEAR(mtx_l_ani, dmtx_l_ani, 1e-2); -} - - -} // namespace diff --git a/cuda/test/factorization/par_ilu_kernels.cpp b/cuda/test/factorization/par_ilu_kernels.cpp deleted file mode 100644 index f27e753e1ae..00000000000 --- a/cuda/test/factorization/par_ilu_kernels.cpp +++ /dev/null @@ -1,343 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ilu_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "cuda/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIlu : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Dense = gko::matrix::Dense; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - std::default_random_engine rand_engine; - std::shared_ptr ref; - std::shared_ptr cuda; - std::shared_ptr csr_ref; - std::shared_ptr csr_cuda; - - ParIlu() - : rand_engine(18), - ref(gko::ReferenceExecutor::create()), - cuda(gko::CudaExecutor::create(0, ref)), - csr_ref(nullptr), - csr_cuda(nullptr) - {} - - void SetUp() override - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - auto csr_ref_temp = gko::read(input_file, ref); - auto csr_cuda_temp = gko::clone(cuda, csr_ref_temp); - // Make sure there are diagonal elements present - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(csr_ref_temp), false); - gko::kernels::cuda::factorization::add_diagonal_elements( - cuda, gko::lend(csr_cuda_temp), false); - csr_ref = gko::give(csr_ref_temp); - csr_cuda = gko::give(csr_cuda_temp); - } - - template - std::unique_ptr gen_mtx(index_type num_rows, index_type num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(0, num_cols - 1), - std::normal_distribution(0.0, 1.0), rand_engine, ref); - } - - std::unique_ptr gen_unsorted_mtx(index_type num_rows, - index_type num_cols) - { - using std::swap; - auto mtx = gen_mtx(num_rows, num_cols); - auto values = mtx->get_values(); - auto col_idxs = mtx->get_col_idxs(); - const auto row_ptrs = mtx->get_const_row_ptrs(); - for (int row = 0; row < num_rows; ++row) { - const auto row_start = row_ptrs[row]; - const auto row_end = row_ptrs[row + 1]; - const int num_row_elements = row_end - row_start; - auto idx_dist = std::uniform_int_distribution( - row_start, row_end - 1); - for (int i = 0; i < num_row_elements / 2; ++i) { - auto idx1 = idx_dist(rand_engine); - auto idx2 = idx_dist(rand_engine); - if (idx1 != idx2) { - swap(values[idx1], values[idx2]); - swap(col_idxs[idx1], col_idxs[idx2]); - } - } - } - return mtx; - } - - void initialize_row_ptrs(index_type* l_row_ptrs_ref, - index_type* u_row_ptrs_ref, - index_type* l_row_ptrs_cuda, - index_type* u_row_ptrs_cuda) - { - gko::kernels::reference::factorization::initialize_row_ptrs_l_u( - ref, gko::lend(csr_ref), l_row_ptrs_ref, u_row_ptrs_ref); - gko::kernels::cuda::factorization::initialize_row_ptrs_l_u( - cuda, gko::lend(csr_cuda), l_row_ptrs_cuda, u_row_ptrs_cuda); - } - - void initialize_lu(std::unique_ptr* l_ref, std::unique_ptr* u_ref, - std::unique_ptr* l_cuda, - std::unique_ptr* u_cuda) - { - auto num_row_ptrs = csr_ref->get_size()[0] + 1; - gko::array l_row_ptrs_ref{ref, num_row_ptrs}; - gko::array u_row_ptrs_ref{ref, num_row_ptrs}; - gko::array l_row_ptrs_cuda{cuda, num_row_ptrs}; - gko::array u_row_ptrs_cuda{cuda, num_row_ptrs}; - - initialize_row_ptrs( - l_row_ptrs_ref.get_data(), u_row_ptrs_ref.get_data(), - l_row_ptrs_cuda.get_data(), u_row_ptrs_cuda.get_data()); - // Since `initialize_row_ptrs` was already tested, it is expected that - // `*_ref` and `*_cuda` contain identical values - auto l_nnz = l_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; - auto u_nnz = u_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; - - *l_ref = Csr::create(ref, csr_ref->get_size(), l_nnz); - *u_ref = Csr::create(ref, csr_ref->get_size(), u_nnz); - *l_cuda = Csr::create(cuda, csr_cuda->get_size(), l_nnz); - *u_cuda = Csr::create(cuda, csr_cuda->get_size(), u_nnz); - // Copy the already initialized `row_ptrs` to the new matrices - ref->copy(num_row_ptrs, l_row_ptrs_ref.get_data(), - (*l_ref)->get_row_ptrs()); - ref->copy(num_row_ptrs, u_row_ptrs_ref.get_data(), - (*u_ref)->get_row_ptrs()); - cuda->copy(num_row_ptrs, l_row_ptrs_cuda.get_data(), - (*l_cuda)->get_row_ptrs()); - cuda->copy(num_row_ptrs, u_row_ptrs_cuda.get_data(), - (*u_cuda)->get_row_ptrs()); - - gko::kernels::reference::factorization::initialize_l_u( - ref, gko::lend(csr_ref), gko::lend(*l_ref), gko::lend(*u_ref)); - gko::kernels::cuda::factorization::initialize_l_u( - cuda, gko::lend(csr_cuda), gko::lend(*l_cuda), gko::lend(*u_cuda)); - } - - template - static std::unique_ptr static_unique_ptr_cast( - std::unique_ptr&& from) - { - return std::unique_ptr{static_cast(from.release())}; - } - - void compute_lu(std::unique_ptr* l_ref, std::unique_ptr* u_ref, - std::unique_ptr* l_cuda, std::unique_ptr* u_cuda, - gko::size_type iterations = 0) - { - auto coo_ref = Coo::create(ref); - csr_ref->convert_to(gko::lend(coo_ref)); - auto coo_cuda = Coo::create(cuda); - csr_cuda->convert_to(gko::lend(coo_cuda)); - initialize_lu(l_ref, u_ref, l_cuda, u_cuda); - auto u_transpose_lin_op_ref = (*u_ref)->transpose(); - auto u_transpose_csr_ref = - static_unique_ptr_cast(std::move(u_transpose_lin_op_ref)); - auto u_transpose_lin_op_cuda = (*u_cuda)->transpose(); - auto u_transpose_csr_cuda = - static_unique_ptr_cast(std::move(u_transpose_lin_op_cuda)); - - gko::kernels::reference::par_ilu_factorization::compute_l_u_factors( - ref, iterations, gko::lend(coo_ref), gko::lend(*l_ref), - gko::lend(u_transpose_csr_ref)); - gko::kernels::cuda::par_ilu_factorization::compute_l_u_factors( - cuda, iterations, gko::lend(coo_cuda), gko::lend(*l_cuda), - gko::lend(u_transpose_csr_cuda)); - auto u_lin_op_ref = u_transpose_csr_ref->transpose(); - *u_ref = static_unique_ptr_cast(std::move(u_lin_op_ref)); - auto u_lin_op_cuda = u_transpose_csr_cuda->transpose(); - *u_cuda = static_unique_ptr_cast(std::move(u_lin_op_cuda)); - } -}; - - -TEST_F(ParIlu, CudaKernelAddDiagonalElementsSortedEquivalentToRef) -{ - index_type num_rows{600}; - index_type num_cols{600}; - auto mtx_ref = gen_mtx(num_rows, num_cols); - auto mtx_cuda = gko::clone(cuda, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(mtx_ref), true); - gko::kernels::cuda::factorization::add_diagonal_elements( - cuda, gko::lend(mtx_cuda), true); - - ASSERT_TRUE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_cuda, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_cuda); -} - - -TEST_F(ParIlu, CudaKernelAddDiagonalElementsUnsortedEquivalentToRef) -{ - index_type num_rows{600}; - index_type num_cols{600}; - auto mtx_ref = gen_unsorted_mtx(num_rows, num_cols); - auto mtx_cuda = gko::clone(cuda, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(mtx_ref), false); - gko::kernels::cuda::factorization::add_diagonal_elements( - cuda, gko::lend(mtx_cuda), false); - - ASSERT_FALSE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_cuda, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_cuda); -} - - -TEST_F(ParIlu, CudaKernelAddDiagonalElementsNonSquareEquivalentToRef) -{ - index_type num_rows{600}; - index_type num_cols{500}; - auto mtx_ref = gen_mtx(num_rows, num_cols); - auto mtx_cuda = gko::clone(cuda, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(mtx_ref), true); - gko::kernels::cuda::factorization::add_diagonal_elements( - cuda, gko::lend(mtx_cuda), true); - - ASSERT_TRUE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_cuda, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_cuda); -} - - -TEST_F(ParIlu, KernelInitializeRowPtrsLUEquivalentToRef) -{ - auto num_row_ptrs = csr_ref->get_size()[0] + 1; - gko::array l_row_ptrs_array_ref(ref, num_row_ptrs); - gko::array u_row_ptrs_array_ref(ref, num_row_ptrs); - gko::array l_row_ptrs_array_cuda(cuda, num_row_ptrs); - gko::array u_row_ptrs_array_cuda(cuda, num_row_ptrs); - - initialize_row_ptrs( - l_row_ptrs_array_ref.get_data(), u_row_ptrs_array_ref.get_data(), - l_row_ptrs_array_cuda.get_data(), u_row_ptrs_array_cuda.get_data()); - - GKO_ASSERT_ARRAY_EQ(l_row_ptrs_array_ref, l_row_ptrs_array_cuda); - GKO_ASSERT_ARRAY_EQ(u_row_ptrs_array_ref, u_row_ptrs_array_cuda); -} - - -TEST_F(ParIlu, KernelInitializeParILUIsEquivalentToRef) -{ - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_cuda{}; - std::unique_ptr u_cuda{}; - - initialize_lu(&l_ref, &u_ref, &l_cuda, &u_cuda); - - GKO_ASSERT_MTX_NEAR(l_ref, l_cuda, 1e-14); - GKO_ASSERT_MTX_NEAR(u_ref, u_cuda, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_cuda); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_cuda); -} - - -TEST_F(ParIlu, KernelComputeParILUIsEquivalentToRef) -{ - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_cuda{}; - std::unique_ptr u_cuda{}; - - compute_lu(&l_ref, &u_ref, &l_cuda, &u_cuda); - - GKO_ASSERT_MTX_NEAR(l_ref, l_cuda, 5e-2); - GKO_ASSERT_MTX_NEAR(u_ref, u_cuda, 5e-2); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_cuda); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_cuda); -} - - -TEST_F(ParIlu, KernelComputeParILUWithMoreIterationsIsEquivalentToRef) -{ - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_cuda{}; - std::unique_ptr u_cuda{}; - gko::size_type iterations{200}; - - compute_lu(&l_ref, &u_ref, &l_cuda, &u_cuda, iterations); - - GKO_ASSERT_MTX_NEAR(l_ref, l_cuda, 1e-14); - GKO_ASSERT_MTX_NEAR(u_ref, u_cuda, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_cuda); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_cuda); -} - - -} // namespace diff --git a/cuda/test/factorization/par_ilut_kernels.cpp b/cuda/test/factorization/par_ilut_kernels.cpp deleted file mode 100644 index 3fc63fb553c..00000000000 --- a/cuda/test/factorization/par_ilut_kernels.cpp +++ /dev/null @@ -1,536 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ilut_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/matrix/csr_builder.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "cuda/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIlut : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Dense = gko::matrix::Dense; - using ComplexDense = gko::matrix::Dense>; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - using ComplexCsr = gko::matrix::Csr, index_type>; - - ParIlut() -#ifdef GINKGO_FAST_TESTS - : mtx_size(152, 231), -#else - : mtx_size(532, 423), -#endif - rand_engine(1337), - ref(gko::ReferenceExecutor::create()), - cuda(gko::CudaExecutor::create(0, ref)) - { - mtx1 = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[1], - std::uniform_int_distribution<>(10, mtx_size[1]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx2 = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[1], - std::uniform_int_distribution<>(0, mtx_size[1]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_square = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[0], - std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l2 = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], true, std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l_complex = - gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_u = gko::test::generate_random_upper_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_u_complex = - gko::test::generate_random_upper_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - - dmtx1 = gko::clone(cuda, mtx1); - dmtx2 = gko::clone(cuda, mtx2); - dmtx_square = gko::clone(cuda, mtx_square); - dmtx_ani = Csr::create(cuda); - dmtx_l_ani = Csr::create(cuda); - dmtx_u_ani = Csr::create(cuda); - dmtx_ut_ani = Csr::create(cuda); - dmtx_l = gko::clone(cuda, mtx_l); - dmtx_l2 = gko::clone(cuda, mtx_l2); - dmtx_u = gko::clone(cuda, mtx_u); - dmtx_l_complex = gko::clone(cuda, mtx_l_complex); - dmtx_u_complex = gko::clone(cuda, mtx_u_complex); - } - - void SetUp() - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - mtx_ani = gko::read(input_file, ref); - mtx_ani->sort_by_column_index(); - - { - mtx_l_ani = Csr::create(ref, mtx_ani->get_size()); - mtx_u_ani = Csr::create(ref, mtx_ani->get_size()); - gko::matrix::CsrBuilder l_builder( - mtx_l_ani.get()); - gko::matrix::CsrBuilder u_builder( - mtx_u_ani.get()); - gko::kernels::reference::factorization::initialize_row_ptrs_l_u( - ref, mtx_ani.get(), mtx_l_ani->get_row_ptrs(), - mtx_u_ani->get_row_ptrs()); - auto l_nnz = - mtx_l_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - auto u_nnz = - mtx_u_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - l_builder.get_col_idx_array().resize_and_reset(l_nnz); - l_builder.get_value_array().resize_and_reset(l_nnz); - u_builder.get_col_idx_array().resize_and_reset(u_nnz); - u_builder.get_value_array().resize_and_reset(u_nnz); - gko::kernels::reference::factorization::initialize_l_u( - ref, mtx_ani.get(), mtx_l_ani.get(), mtx_u_ani.get()); - mtx_ut_ani = Csr::create(ref, mtx_ani->get_size(), - mtx_u_ani->get_num_stored_elements()); - gko::kernels::reference::csr::transpose(ref, mtx_u_ani.get(), - mtx_ut_ani.get()); - } - dmtx_ani->copy_from(mtx_ani.get()); - dmtx_l_ani->copy_from(mtx_l_ani.get()); - dmtx_u_ani->copy_from(mtx_u_ani.get()); - dmtx_ut_ani->copy_from(mtx_ut_ani.get()); - } - - template - void test_select(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, index_type rank, - value_type tolerance = 0.0) - { - auto size = index_type(mtx->get_num_stored_elements()); - using ValueType = typename Mtx::value_type; - - gko::remove_complex res{}; - gko::remove_complex dres{}; - gko::array tmp(ref); - gko::array> tmp2(ref); - gko::array dtmp(cuda); - gko::array> dtmp2(cuda); - - gko::kernels::reference::par_ilut_factorization::threshold_select( - ref, mtx.get(), rank, tmp, tmp2, res); - gko::kernels::cuda::par_ilut_factorization::threshold_select( - cuda, dmtx.get(), rank, dtmp, dtmp2, dres); - - ASSERT_NEAR(res, dres, tolerance); - } - - template > - void test_filter(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, value_type threshold, - bool lower) - { - auto res = Mtx::create(ref, mtx_size); - auto dres = Mtx::create(cuda, mtx_size); - auto res_coo = Coo::create(ref, mtx_size); - auto dres_coo = Coo::create(cuda, mtx_size); - auto local_mtx = gko::as(lower ? mtx->clone() : mtx->transpose()); - auto local_dmtx = - gko::as(lower ? dmtx->clone() : dmtx->transpose()); - - gko::kernels::reference::par_ilut_factorization::threshold_filter( - ref, local_mtx.get(), threshold, res.get(), res_coo.get(), lower); - gko::kernels::cuda::par_ilut_factorization::threshold_filter( - cuda, local_dmtx.get(), threshold, dres.get(), dres_coo.get(), - lower); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); - GKO_ASSERT_MTX_NEAR(res, res_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, res_coo); - GKO_ASSERT_MTX_NEAR(dres, dres_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(dres, dres_coo); - } - - template > - void test_filter_approx(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, index_type rank, - value_type tolerance = 0.0) - { - auto res = Mtx::create(ref, mtx_size); - auto dres = Mtx::create(cuda, mtx_size); - auto res_coo = Coo::create(ref, mtx_size); - auto dres_coo = Coo::create(cuda, mtx_size); - using ValueType = typename Mtx::value_type; - - gko::array tmp(ref); - gko::array dtmp(cuda); - gko::remove_complex threshold{}; - gko::remove_complex dthreshold{}; - - gko::kernels::reference::par_ilut_factorization:: - threshold_filter_approx(ref, mtx.get(), rank, tmp, threshold, - res.get(), res_coo.get()); - gko::kernels::cuda::par_ilut_factorization::threshold_filter_approx( - cuda, dmtx.get(), rank, dtmp, dthreshold, dres.get(), - dres_coo.get()); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); - GKO_ASSERT_MTX_NEAR(res, res_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, res_coo); - GKO_ASSERT_MTX_NEAR(dres, dres_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(dres, dres_coo); - ASSERT_NEAR(threshold, dthreshold, tolerance); - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx1; - std::unique_ptr mtx2; - std::unique_ptr mtx_square; - std::unique_ptr mtx_ani; - std::unique_ptr mtx_l_ani; - std::unique_ptr mtx_u_ani; - std::unique_ptr mtx_ut_ani; - std::unique_ptr mtx_l; - std::unique_ptr mtx_l2; - std::unique_ptr mtx_l_complex; - std::unique_ptr mtx_u; - std::unique_ptr mtx_u_complex; - - std::unique_ptr dmtx1; - std::unique_ptr dmtx2; - std::unique_ptr dmtx_square; - std::unique_ptr dmtx_ani; - std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx_u_ani; - std::unique_ptr dmtx_ut_ani; - std::unique_ptr dmtx_l; - std::unique_ptr dmtx_l2; - std::unique_ptr dmtx_l_complex; - std::unique_ptr dmtx_u; - std::unique_ptr dmtx_u_complex; -}; - - -TEST_F(ParIlut, KernelThresholdSelectIsEquivalentToRef) -{ - test_select(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() / 3); -} - - -TEST_F(ParIlut, KernelThresholdSelectMinIsEquivalentToRef) -{ - test_select(mtx_l, dmtx_l, 0); -} - - -TEST_F(ParIlut, KernelThresholdSelectMaxIsEquivalentToRef) -{ - test_select(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() - 1); -} - - -TEST_F(ParIlut, KernelComplexThresholdSelectIsEquivalentToRef) -{ - test_select(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() / 3, 1e-14); -} - - -TEST_F(ParIlut, KernelComplexThresholdSelectMinIsEquivalentToRef) -{ - test_select(mtx_l_complex, dmtx_l_complex, 0, 1e-14); -} - - -TEST_F(ParIlut, KernelComplexThresholdSelectMaxLowerIsEquivalentToRef) -{ - test_select(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() - 1, 1e-14); -} - - -TEST_F(ParIlut, KernelThresholdFilterNullptrCooIsEquivalentToRef) -{ - auto res = Csr::create(ref, mtx_size); - auto dres = Csr::create(cuda, mtx_size); - Coo* null_coo = nullptr; - - gko::kernels::reference::par_ilut_factorization::threshold_filter( - ref, mtx_l.get(), 0.5, res.get(), null_coo, true); - gko::kernels::cuda::par_ilut_factorization::threshold_filter( - cuda, dmtx_l.get(), 0.5, dres.get(), null_coo, true); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); -} - - -TEST_F(ParIlut, KernelThresholdFilterLowerIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0.5, true); -} - - -TEST_F(ParIlut, KernelThresholdFilterUpperIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0.5, false); -} - - -TEST_F(ParIlut, KernelThresholdFilterNoneLowerIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0, true); -} - - -TEST_F(ParIlut, KernelThresholdFilterNoneUpperIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0, false); -} - - -TEST_F(ParIlut, KernelThresholdFilterAllLowerIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 1e6, true); -} - - -TEST_F(ParIlut, KernelThresholdFilterAllUpperIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 1e6, false); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterLowerIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0.5, true); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterUpperIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0.5, false); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterNoneLowerIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0, true); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterNoneUpperIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0, false); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterAllLowerIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 1e6, true); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterAllUppererIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 1e6, false); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxNullptrCooIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0.5, true); - auto res = Csr::create(ref, mtx_size); - auto dres = Csr::create(cuda, mtx_size); - Coo* null_coo = nullptr; - gko::array tmp(ref); - gko::array dtmp(cuda); - gko::remove_complex threshold{}; - gko::remove_complex dthreshold{}; - index_type rank{}; - - gko::kernels::reference::par_ilut_factorization::threshold_filter_approx( - ref, mtx_l.get(), rank, tmp, threshold, res.get(), null_coo); - gko::kernels::cuda::par_ilut_factorization::threshold_filter_approx( - cuda, dmtx_l.get(), rank, dtmp, dthreshold, dres.get(), null_coo); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); - ASSERT_EQ(threshold, dthreshold); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() / 2); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxNoneLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l, dmtx_l, 0); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxAllLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() - 1); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterApproxLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() / 2, - r::value); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterApproxNoneLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l_complex, dmtx_l_complex, 0, r::value); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterApproxAllLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() - 1, - r::value); -} - - -TEST_F(ParIlut, KernelAddCandidatesIsEquivalentToRef) -{ - auto square_size = mtx_square->get_size(); - auto mtx_lu = Csr::create(ref, square_size); - mtx_l2->apply(mtx_u.get(), mtx_lu.get()); - auto dmtx_lu = Csr::create(cuda, square_size); - dmtx_lu->copy_from(mtx_lu.get()); - auto res_mtx_l = Csr::create(ref, square_size); - auto res_mtx_u = Csr::create(ref, square_size); - auto dres_mtx_l = Csr::create(cuda, square_size); - auto dres_mtx_u = Csr::create(cuda, square_size); - - gko::kernels::reference::par_ilut_factorization::add_candidates( - ref, mtx_lu.get(), mtx_square.get(), mtx_l2.get(), mtx_u.get(), - res_mtx_l.get(), res_mtx_u.get()); - gko::kernels::cuda::par_ilut_factorization::add_candidates( - cuda, dmtx_lu.get(), dmtx_square.get(), dmtx_l2.get(), dmtx_u.get(), - dres_mtx_l.get(), dres_mtx_u.get()); - - GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_l, dres_mtx_l); - GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_u, dres_mtx_u); - GKO_ASSERT_MTX_NEAR(res_mtx_l, dres_mtx_l, 1e-14); - GKO_ASSERT_MTX_NEAR(res_mtx_u, dres_mtx_u, 1e-14); -} - - -TEST_F(ParIlut, KernelComputeLUIsEquivalentToRef) -{ - auto square_size = mtx_ani->get_size(); - auto mtx_l_coo = Coo::create(ref, square_size); - auto mtx_u_coo = Coo::create(ref, square_size); - mtx_l_ani->convert_to(mtx_l_coo.get()); - mtx_u_ani->convert_to(mtx_u_coo.get()); - auto dmtx_l_coo = Coo::create(cuda, square_size); - auto dmtx_u_coo = Coo::create(cuda, square_size); - dmtx_l_coo->copy_from(mtx_l_coo.get()); - dmtx_u_coo->copy_from(mtx_u_coo.get()); - - gko::kernels::reference::par_ilut_factorization::compute_l_u_factors( - ref, mtx_ani.get(), mtx_l_ani.get(), mtx_l_coo.get(), mtx_u_ani.get(), - mtx_u_coo.get(), mtx_ut_ani.get()); - for (int i = 0; i < 20; ++i) { - gko::kernels::cuda::par_ilut_factorization::compute_l_u_factors( - cuda, dmtx_ani.get(), dmtx_l_ani.get(), dmtx_l_coo.get(), - dmtx_u_ani.get(), dmtx_u_coo.get(), dmtx_ut_ani.get()); - } - auto dmtx_utt_ani = gko::as(dmtx_ut_ani->transpose()); - - GKO_ASSERT_MTX_NEAR(mtx_l_ani, dmtx_l_ani, 1e-2); - GKO_ASSERT_MTX_NEAR(mtx_u_ani, dmtx_u_ani, 1e-2); - GKO_ASSERT_MTX_NEAR(dmtx_u_ani, dmtx_utt_ani, 0); -} - - -} // namespace diff --git a/cuda/test/matrix/CMakeLists.txt b/cuda/test/matrix/CMakeLists.txt deleted file mode 100644 index 0c0442f61e0..00000000000 --- a/cuda/test/matrix/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -ginkgo_create_test(coo_kernels) -ginkgo_create_test(csr_kernels) -ginkgo_create_test(dense_kernels) -ginkgo_create_test(diagonal_kernels) -ginkgo_create_test(ell_kernels) -ginkgo_create_test(fbcsr_kernels) -ginkgo_create_test(fft_kernels) -ginkgo_create_test(hybrid_kernels) -ginkgo_create_test(sellp_kernels) diff --git a/cuda/test/matrix/dense_kernels.cpp b/cuda/test/matrix/dense_kernels.cpp deleted file mode 100644 index 73e68a01067..00000000000 --- a/cuda/test/matrix/dense_kernels.cpp +++ /dev/null @@ -1,644 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include -#include - - -#include "core/components/fill_array_kernels.hpp" -#include "core/matrix/dense_kernels.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -class Dense : public ::testing::Test { -protected: - using itype = int; - using vtype = double; - using Mtx = gko::matrix::Dense; - using MixedMtx = gko::matrix::Dense>; - using NormVector = gko::matrix::Dense>; - using Arr = gko::array; - using ComplexMtx = gko::matrix::Dense>; - using Diagonal = gko::matrix::Diagonal; - using MixedComplexMtx = - gko::matrix::Dense>>; - - Dense() : rand_engine(15) {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); - } - - void set_up_vector_data(gko::size_type num_vecs, - bool different_alpha = false) - { - x = gen_mtx(1000, num_vecs); - y = gen_mtx(1000, num_vecs); - if (different_alpha) { - alpha = gen_mtx(1, num_vecs); - } else { - alpha = gko::initialize({2.0}, ref); - } - dx = gko::clone(cuda, x); - dy = gko::clone(cuda, y); - dalpha = gko::clone(cuda, alpha); - expected = Mtx::create(ref, gko::dim<2>{1, num_vecs}); - dresult = Mtx::create(cuda, gko::dim<2>{1, num_vecs}); - } - - void set_up_apply_data() - { - x = gen_mtx(65, 25); - c_x = gen_mtx(65, 25); - y = gen_mtx(25, 35); - expected = gen_mtx(65, 35); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - square = gen_mtx(x->get_size()[0], x->get_size()[0]); - dx = gko::clone(cuda, x); - dc_x = gko::clone(cuda, c_x); - dy = gko::clone(cuda, y); - dresult = gko::clone(cuda, expected); - dalpha = gko::clone(cuda, alpha); - dbeta = gko::clone(cuda, beta); - dsquare = gko::clone(cuda, square); - - std::vector tmp(x->get_size()[0], 0); - auto rng = std::default_random_engine{}; - std::iota(tmp.begin(), tmp.end(), 0); - std::shuffle(tmp.begin(), tmp.end(), rng); - std::vector tmp2(x->get_size()[1], 0); - std::iota(tmp2.begin(), tmp2.end(), 0); - std::shuffle(tmp2.begin(), tmp2.end(), rng); - std::vector tmp3(x->get_size()[0] / 10); - std::uniform_int_distribution row_dist(0, x->get_size()[0] - 1); - for (auto& i : tmp3) { - i = row_dist(rng); - } - rpermute_idxs = - std::unique_ptr(new Arr{ref, tmp.begin(), tmp.end()}); - cpermute_idxs = - std::unique_ptr(new Arr{ref, tmp2.begin(), tmp2.end()}); - rgather_idxs = - std::unique_ptr(new Arr{ref, tmp3.begin(), tmp3.end()}); - } - - template - std::unique_ptr convert(InputType&& input) - { - auto result = ConvertedType::create(input->get_executor()); - input->convert_to(result.get()); - return result; - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::default_random_engine rand_engine; - - std::unique_ptr x; - std::unique_ptr c_x; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - std::unique_ptr expected; - std::unique_ptr square; - std::unique_ptr dresult; - std::unique_ptr dx; - std::unique_ptr dc_x; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; - std::unique_ptr dsquare; - std::unique_ptr rpermute_idxs; - std::unique_ptr cpermute_idxs; - std::unique_ptr rgather_idxs; -}; - - -TEST_F(Dense, SingleVectorCudaComputeDotIsEquivalentToRef) -{ - set_up_vector_data(1); - - x->compute_dot(y.get(), expected.get()); - dx->compute_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, MultipleVectorCudaComputeDotIsEquivalentToRef) -{ - set_up_vector_data(20); - - x->compute_dot(y.get(), expected.get()); - dx->compute_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, SingleVectorCudaComputeConjDotIsEquivalentToRef) -{ - set_up_vector_data(1); - - x->compute_conj_dot(y.get(), expected.get()); - dx->compute_conj_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, MultipleVectorCudaComputeConjDotIsEquivalentToRef) -{ - set_up_vector_data(20); - - x->compute_conj_dot(y.get(), expected.get()); - dx->compute_conj_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, SingleVectorCudaComputeNorm2IsEquivalentToRef) -{ - set_up_vector_data(1); - auto norm_size = gko::dim<2>{1, x->get_size()[1]}; - auto norm_expected = NormVector::create(this->ref, norm_size); - auto dnorm = NormVector::create(this->cuda, norm_size); - - x->compute_norm2(norm_expected.get()); - dx->compute_norm2(dnorm.get()); - - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, 1e-14); -} - - -TEST_F(Dense, MultipleVectorCudaComputeNorm2IsEquivalentToRef) -{ - set_up_vector_data(20); - auto norm_size = gko::dim<2>{1, x->get_size()[1]}; - auto norm_expected = NormVector::create(this->ref, norm_size); - auto dnorm = NormVector::create(this->cuda, norm_size); - - x->compute_norm2(norm_expected.get()); - dx->compute_norm2(dnorm.get()); - - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, 1e-14); -} - - -TEST_F(Dense, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(y.get(), expected.get()); - dx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, SimpleApplyMixedIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(convert(y).get(), convert(expected).get()); - dx->apply(convert(dy).get(), convert(dresult).get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7); -} - - -TEST_F(Dense, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, AdvancedApplyMixedIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(convert(alpha).get(), convert(y).get(), - convert(beta).get(), convert(expected).get()); - dx->apply(convert(dalpha).get(), convert(dy).get(), - convert(dbeta).get(), convert(dresult).get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7); -} - - -TEST_F(Dense, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(cuda, complex_x); - - x->apply(complex_b.get(), complex_x.get()); - dx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Dense, ApplyToMixedComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(cuda, complex_x); - - x->apply(complex_b.get(), complex_x.get()); - dx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7); -} - - -TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(cuda, complex_x); - - x->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Dense, AdvancedApplyToMixedComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(cuda, complex_x); - - x->apply(convert(alpha).get(), complex_b.get(), - convert(beta).get(), complex_x.get()); - dx->apply(convert(dalpha).get(), dcomplex_b.get(), - convert(dbeta).get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7); -} - - -TEST_F(Dense, ComputeDotComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(1234, 2); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(1234, 2); - auto dcomplex_x = gko::clone(cuda, complex_x); - auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); - auto dresult = ComplexMtx::create(cuda, gko::dim<2>{1, 2}); - - complex_b->compute_dot(complex_x.get(), result.get()); - dcomplex_b->compute_dot(dcomplex_x.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(result, dresult, 1e-14); -} - - -TEST_F(Dense, ComputeConjDotComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(1234, 2); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(1234, 2); - auto dcomplex_x = gko::clone(cuda, complex_x); - auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); - auto dresult = ComplexMtx::create(cuda, gko::dim<2>{1, 2}); - - complex_b->compute_conj_dot(complex_x.get(), result.get()); - dcomplex_b->compute_conj_dot(dcomplex_x.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(result, dresult, 1e-14); -} - - -TEST_F(Dense, ConvertToCooIsEquivalentToRef) -{ - set_up_apply_data(); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(cuda); - - x->convert_to(coo_mtx.get()); - dx->convert_to(dcoo_mtx.get()); - - ASSERT_EQ(dcoo_mtx->get_num_stored_elements(), - coo_mtx->get_num_stored_elements()); - GKO_ASSERT_MTX_NEAR(dcoo_mtx.get(), coo_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToCooIsEquivalentToRef) -{ - set_up_apply_data(); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(cuda); - - x->move_to(coo_mtx.get()); - dx->move_to(dcoo_mtx.get()); - - ASSERT_EQ(dcoo_mtx->get_num_stored_elements(), - coo_mtx->get_num_stored_elements()); - GKO_ASSERT_MTX_NEAR(dcoo_mtx.get(), coo_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(cuda); - - x->convert_to(csr_mtx.get()); - dx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dcsr_mtx.get(), csr_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(cuda); - - x->move_to(csr_mtx.get()); - dx->move_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dcsr_mtx.get(), csr_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(cuda); - - x->convert_to(sparsity_mtx.get()); - dx->convert_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(cuda); - - x->move_to(sparsity_mtx.get()); - dx->move_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToEllIsEquivalentToRef) -{ - set_up_apply_data(); - auto ell_mtx = gko::matrix::Ell<>::create(ref); - auto dell_mtx = gko::matrix::Ell<>::create(cuda); - - x->convert_to(ell_mtx.get()); - dx->convert_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dell_mtx.get(), ell_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToEllIsEquivalentToRef) -{ - set_up_apply_data(); - auto ell_mtx = gko::matrix::Ell<>::create(ref); - auto dell_mtx = gko::matrix::Ell<>::create(cuda); - - x->move_to(ell_mtx.get()); - dx->move_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dell_mtx.get(), ell_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToSellpIsEquivalentToRef) -{ - set_up_apply_data(); - auto sellp_mtx = gko::matrix::Sellp<>::create(ref); - auto dsellp_mtx = gko::matrix::Sellp<>::create(cuda); - - x->convert_to(sellp_mtx.get()); - dx->convert_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx, dsellp_mtx, 0); -} - - -TEST_F(Dense, MoveToSellpIsEquivalentToRef) -{ - set_up_apply_data(); - auto sellp_mtx = gko::matrix::Sellp<>::create(ref); - auto dsellp_mtx = gko::matrix::Sellp<>::create(cuda); - - x->move_to(sellp_mtx.get()); - dx->move_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx, dsellp_mtx, 0); -} - - -TEST_F(Dense, ConvertsEmptyToSellp) -{ - auto dempty_mtx = Mtx::create(cuda); - auto dsellp_mtx = gko::matrix::Sellp<>::create(cuda); - - dempty_mtx->convert_to(dsellp_mtx.get()); - - ASSERT_EQ(cuda->copy_val_to_host(dsellp_mtx->get_const_slice_sets()), 0); - ASSERT_FALSE(dsellp_mtx->get_size()); -} - - -TEST_F(Dense, CalculateNNZPerRowIsEquivalentToRef) -{ - set_up_apply_data(); - gko::array nnz_per_row(ref); - nnz_per_row.resize_and_reset(x->get_size()[0]); - gko::array dnnz_per_row(cuda); - dnnz_per_row.resize_and_reset(dx->get_size()[0]); - - gko::kernels::reference::dense::count_nonzeros_per_row( - ref, x.get(), nnz_per_row.get_data()); - gko::kernels::cuda::dense::count_nonzeros_per_row(cuda, dx.get(), - dnnz_per_row.get_data()); - - auto tmp = gko::array(ref, dnnz_per_row); - for (gko::size_type i = 0; i < nnz_per_row.get_num_elems(); i++) { - ASSERT_EQ(nnz_per_row.get_const_data()[i], tmp.get_const_data()[i]); - } -} - - -TEST_F(Dense, ComputeMaxNNZPerRowIsEquivalentToRef) -{ - set_up_apply_data(); - gko::size_type max_nnz; - gko::size_type dmax_nnz; - - gko::kernels::reference::dense::compute_max_nnz_per_row(ref, x.get(), - max_nnz); - gko::kernels::cuda::dense::compute_max_nnz_per_row(cuda, dx.get(), - dmax_nnz); - - ASSERT_EQ(max_nnz, dmax_nnz); -} - - -TEST_F(Dense, IsTransposable) -{ - set_up_apply_data(); - - auto trans = x->transpose(); - auto dtrans = dx->transpose(); - - GKO_ASSERT_MTX_NEAR(static_cast(dtrans.get()), - static_cast(trans.get()), 0); -} - - -TEST_F(Dense, IsTransposableIntoDenseCrossExecutor) -{ - set_up_apply_data(); - auto row_span = gko::span{0, x->get_size()[0] - 2}; - auto col_span = gko::span{0, x->get_size()[1] - 2}; - auto sub_x = x->create_submatrix(row_span, col_span); - auto sub_dx = dx->create_submatrix(row_span, col_span); - // create the target matrices on another executor to - // force temporary clone - auto trans = Mtx::create(ref, gko::transpose(sub_x->get_size())); - auto dtrans = Mtx::create(ref, gko::transpose(sub_x->get_size()), - sub_x->get_size()[0] + 4); - - sub_x->transpose(trans.get()); - sub_dx->transpose(dtrans.get()); - - GKO_ASSERT_MTX_NEAR(dtrans, trans, 0); -} - - -TEST_F(Dense, IsConjugateTransposable) -{ - set_up_apply_data(); - - auto trans = c_x->conj_transpose(); - auto dtrans = dc_x->conj_transpose(); - - GKO_ASSERT_MTX_NEAR(static_cast(dtrans.get()), - static_cast(trans.get()), 0); -} - - -TEST_F(Dense, IsConjugateTransposableIntoDenseCrossExecutor) -{ - set_up_apply_data(); - auto row_span = gko::span{0, c_x->get_size()[0] - 2}; - auto col_span = gko::span{0, c_x->get_size()[1] - 2}; - auto sub_x = c_x->create_submatrix(row_span, col_span); - auto sub_dx = dc_x->create_submatrix(row_span, col_span); - // create the target matrices on another executor to - // force temporary clone - auto trans = ComplexMtx::create(ref, gko::transpose(sub_x->get_size())); - auto dtrans = ComplexMtx::create(ref, gko::transpose(sub_x->get_size()), - sub_x->get_size()[0] + 4); - - sub_x->conj_transpose(trans.get()); - sub_dx->conj_transpose(dtrans.get()); - - GKO_ASSERT_MTX_NEAR(dtrans, trans, 0); -} - - -} // namespace diff --git a/cuda/test/matrix/diagonal_kernels.cpp b/cuda/test/matrix/diagonal_kernels.cpp deleted file mode 100644 index 4f22ce40399..00000000000 --- a/cuda/test/matrix/diagonal_kernels.cpp +++ /dev/null @@ -1,271 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include -#include -#include - - -#include - - -#include -#include - - -#include "core/matrix/diagonal_kernels.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -class Diagonal : public ::testing::Test { -protected: - using ValueType = double; - using ComplexValueType = std::complex; - using Csr = gko::matrix::Csr; - using Diag = gko::matrix::Diagonal; - using Dense = gko::matrix::Dense; - using ComplexDiag = gko::matrix::Diagonal; - - Diagonal() -#ifdef GINKGO_FAST_TESTS - : mtx_size(152, 231), -#else - : mtx_size(532, 231), -#endif - rand_engine(42) - {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); - } - - std::unique_ptr gen_diag(int size) - { - auto diag = Diag::create(ref, size); - auto vals = diag->get_values(); - auto value_dist = std::normal_distribution<>(0.0, 1.0); - for (int i = 0; i < size; i++) { - vals[i] = gko::test::detail::get_rand_value(value_dist, - rand_engine); - } - return diag; - } - - std::unique_ptr gen_cdiag(int size) - { - auto cdiag = ComplexDiag::create(ref, size); - auto vals = cdiag->get_values(); - auto value_dist = std::normal_distribution<>(0.0, 1.0); - for (int i = 0; i < size; i++) { - vals[i] = ComplexValueType{ - gko::test::detail::get_rand_value( - value_dist, rand_engine)}; - } - return cdiag; - } - - void set_up_apply_data() - { - diag = gen_diag(mtx_size[0]); - ddiag = gko::clone(cuda, diag); - dense1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[1]); - dense2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[0]); - denseexpected1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[1]); - denseexpected2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[0]); - ddense1 = gko::clone(cuda, dense1); - ddense2 = gko::clone(cuda, dense2); - denseresult1 = gko::clone(cuda, denseexpected1); - denseresult2 = gko::clone(cuda, denseexpected2); - csr1 = gen_mtx(mtx_size[0], mtx_size[1], 1); - csr2 = gen_mtx(mtx_size[1], mtx_size[0], 1); - csrexpected1 = gen_mtx(mtx_size[0], mtx_size[1], 1); - csrexpected2 = gen_mtx(mtx_size[1], mtx_size[0], 1); - dcsr1 = gko::clone(cuda, csr1); - dcsr2 = gko::clone(cuda, csr2); - csrresult1 = gko::clone(cuda, csrexpected1); - csrresult2 = gko::clone(cuda, csrexpected2); - } - - void set_up_complex_data() - { - cdiag = gen_cdiag(mtx_size[0]); - dcdiag = gko::clone(cuda, cdiag); - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr diag; - std::unique_ptr ddiag; - std::unique_ptr cdiag; - std::unique_ptr dcdiag; - - std::unique_ptr dense1; - std::unique_ptr dense2; - std::unique_ptr denseexpected1; - std::unique_ptr denseexpected2; - std::unique_ptr denseresult1; - std::unique_ptr denseresult2; - std::unique_ptr ddense1; - std::unique_ptr ddense2; - std::unique_ptr csr1; - std::unique_ptr csr2; - std::unique_ptr dcsr1; - std::unique_ptr dcsr2; - std::unique_ptr csrexpected1; - std::unique_ptr csrexpected2; - std::unique_ptr csrresult1; - std::unique_ptr csrresult2; -}; - - -TEST_F(Diagonal, ApplyToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->apply(dense1.get(), denseexpected1.get()); - ddiag->apply(ddense1.get(), denseresult1.get()); - - GKO_ASSERT_MTX_NEAR(denseexpected1, denseresult1, 1e-14); -} - - -TEST_F(Diagonal, RightApplyToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->rapply(dense2.get(), denseexpected2.get()); - ddiag->rapply(ddense2.get(), denseresult2.get()); - - GKO_ASSERT_MTX_NEAR(denseexpected2, denseresult2, 1e-14); -} - - -TEST_F(Diagonal, ApplyToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->apply(csr1.get(), csrexpected1.get()); - ddiag->apply(dcsr1.get(), csrresult1.get()); - - GKO_ASSERT_MTX_NEAR(csrexpected1, csrresult1, 1e-14); -} - - -TEST_F(Diagonal, RightApplyToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->rapply(csr2.get(), csrexpected2.get()); - ddiag->rapply(dcsr2.get(), csrresult2.get()); - - GKO_ASSERT_MTX_NEAR(csrexpected2, csrresult2, 1e-14); -} - - -TEST_F(Diagonal, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->convert_to(csr1.get()); - ddiag->convert_to(dcsr1.get()); - - GKO_ASSERT_MTX_NEAR(csr1, dcsr1, 0); -} - - -TEST_F(Diagonal, ConjTransposeIsEquivalentToRef) -{ - set_up_complex_data(); - - auto trans = cdiag->conj_transpose(); - auto trans_diag = static_cast(trans.get()); - auto dtrans = dcdiag->conj_transpose(); - auto dtrans_diag = static_cast(dtrans.get()); - - GKO_ASSERT_MTX_NEAR(trans_diag, dtrans_diag, 0); -} - - -TEST_F(Diagonal, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->compute_absolute_inplace(); - ddiag->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(diag, ddiag, 1e-14); -} - - -TEST_F(Diagonal, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_diag = diag->compute_absolute(); - auto dabs_diag = ddiag->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_diag, dabs_diag, 1e-14); -} - - -} // namespace diff --git a/cuda/test/matrix/ell_kernels.cpp b/cuda/test/matrix/ell_kernels.cpp deleted file mode 100644 index 290441b4700..00000000000 --- a/cuda/test/matrix/ell_kernels.cpp +++ /dev/null @@ -1,605 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/matrix/ell_kernels.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -class Ell : public ::testing::Test { -protected: - using Mtx = gko::matrix::Ell<>; - using Vec = gko::matrix::Dense<>; - using Vec2 = gko::matrix::Dense; - using ComplexVec = gko::matrix::Dense>; - - Ell() - : rand_engine(42), size{532, 231}, num_els_rowwise{300}, ell_stride{600} - {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data(int num_rows = 532, int num_cols = 231, - int num_vectors = 1, - int num_stored_elements_per_row = 0, int stride = 0) - { - mtx = Mtx::create(ref, gko::dim<2>{}, num_stored_elements_per_row, - stride); - mtx->copy_from(gen_mtx(num_rows, num_cols)); - expected = gen_mtx(num_rows, num_vectors); - expected2 = Vec2::create(ref); - expected2->copy_from(expected.get()); - y = gen_mtx(num_cols, num_vectors); - y2 = Vec2::create(ref); - y2->copy_from(y.get()); - alpha = gko::initialize({2.0}, ref); - alpha2 = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - beta2 = gko::initialize({-1.0}, ref); - dmtx = gko::clone(cuda, mtx); - dresult = gko::clone(cuda, expected); - dresult2 = gko::clone(cuda, expected2); - dy = gko::clone(cuda, y); - dy2 = gko::clone(cuda, y2); - dalpha = gko::clone(cuda, alpha); - dalpha2 = gko::clone(cuda, alpha2); - dbeta = gko::clone(cuda, beta); - dbeta2 = gko::clone(cuda, beta2); - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::default_random_engine rand_engine; - gko::dim<2> size; - gko::size_type num_els_rowwise; - gko::size_type ell_stride; - - std::unique_ptr mtx; - std::unique_ptr expected; - std::unique_ptr expected2; - std::unique_ptr y; - std::unique_ptr y2; - std::unique_ptr alpha; - std::unique_ptr alpha2; - std::unique_ptr beta; - std::unique_ptr beta2; - - std::unique_ptr dmtx; - std::unique_ptr dresult; - std::unique_ptr dresult2; - std::unique_ptr dy; - std::unique_ptr dy2; - std::unique_ptr dalpha; - std::unique_ptr dalpha2; - std::unique_ptr dbeta; - std::unique_ptr dbeta2; -}; - - -TEST_F(Ell, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyIsEquivalentToRef1) -{ - set_up_apply_data(); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedSimpleApplyIsEquivalentToRef2) -{ - set_up_apply_data(); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyIsEquivalentToRef3) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyIsEquivalentToRef1) -{ - set_up_apply_data(); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedAdvancedApplyIsEquivalentToRef2) -{ - set_up_apply_data(); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyIsEquivalentToRef3) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, SimpleApplyWithStrideIsEquivalentToRef) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideIsEquivalentToRef1) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideIsEquivalentToRef2) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideIsEquivalentToRef3) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, AdvancedApplyWithStrideIsEquivalentToRef) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideIsEquivalentToRef1) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideIsEquivalentToRef2) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideIsEquivalentToRef3) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, SimpleApplyWithStrideToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef1) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef2) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef3) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, AdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef1) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef2) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef3) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, SimpleApplyByAtomicIsEquivalentToRef) -{ - set_up_apply_data(10, 10000); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, AdvancedByAtomicApplyIsEquivalentToRef) -{ - set_up_apply_data(10, 10000); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, SimpleApplyByAtomicToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(10, 10000, 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, AdvancedByAtomicToDenseMatrixApplyIsEquivalentToRef) -{ - set_up_apply_data(10, 10000, 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, SimpleApplyOnSmallMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, 10); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyOnSmallMatrixToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, 10, 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, SimpleApplyOnSmallMatrixToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, 10, 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyOnSmallMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, 10); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(size[1], 3); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(size[0], 3); - auto dcomplex_x = gko::clone(cuda, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(size[1], 3); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(size[0], 3); - auto dcomplex_x = gko::clone(cuda, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Ell, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(cuda); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Ell, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(cuda); - - mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Ell, CalculateNNZPerRowIsEquivalentToRef) -{ - set_up_apply_data(); - gko::array nnz_per_row{ref, mtx->get_size()[0]}; - gko::array dnnz_per_row{cuda, dmtx->get_size()[0]}; - - gko::kernels::reference::ell::count_nonzeros_per_row( - ref, mtx.get(), nnz_per_row.get_data()); - gko::kernels::cuda::ell::count_nonzeros_per_row(cuda, dmtx.get(), - dnnz_per_row.get_data()); - - GKO_ASSERT_ARRAY_EQ(nnz_per_row, dnnz_per_row); -} - - -TEST_F(Ell, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Ell, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Ell, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -} // namespace diff --git a/cuda/test/matrix/fbcsr_kernels.cpp b/cuda/test/matrix/fbcsr_kernels.cpp deleted file mode 100644 index 7919af5437a..00000000000 --- a/cuda/test/matrix/fbcsr_kernels.cpp +++ /dev/null @@ -1,377 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include - - -#include "core/matrix/fbcsr_kernels.hpp" -#include "core/test/matrix/fbcsr_sample.hpp" -#include "core/test/utils.hpp" -#include "core/test/utils/fb_matrix_generator.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -template -class Fbcsr : public ::testing::Test { -protected: - using value_type = T; - using index_type = int; - using real_type = gko::remove_complex; - using Mtx = gko::matrix::Fbcsr; - using Dense = gko::matrix::Dense; - - Fbcsr() : distb(), engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - const index_type rand_brows = 100; - const index_type rand_bcols = 70; - const int block_size = 3; - rsorted_ref = gko::test::generate_random_fbcsr( - ref, rand_brows, rand_bcols, block_size, false, false, - std::default_random_engine(43)); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::unique_ptr rsorted_ref; - - std::normal_distribution> distb; - std::default_random_engine engine; - - value_type get_random_value() - { - return gko::test::detail::get_rand_value(distb, engine); - } - - void generate_sin(Dense* const x) - { - value_type* const xarr = x->get_values(); - for (index_type i = 0; i < x->get_size()[0] * x->get_size()[1]; i++) { - xarr[i] = - static_cast(2.0) * - std::sin(static_cast(i / 2.0) + get_random_value()); - } - } -}; - -TYPED_TEST_SUITE(Fbcsr, gko::test::ValueTypes, TypenameNameGenerator); - - -TYPED_TEST(Fbcsr, CanWriteFromMatrixOnDevice) -{ - using Mtx = typename TestFixture::Mtx; - using value_type = typename Mtx::value_type; - using index_type = typename Mtx::index_type; - using MatData = gko::matrix_data; - gko::testing::FbcsrSample sample(this->ref); - auto refmat = sample.generate_fbcsr(); - auto cudamat = gko::clone(this->cuda, refmat); - MatData refdata; - MatData cudadata; - - refmat->write(refdata); - cudamat->write(cudadata); - - ASSERT_TRUE(refdata.nonzeros == cudadata.nonzeros); -} - - -TYPED_TEST(Fbcsr, TransposeIsEquivalentToRefSortedBS3) -{ - using Mtx = typename TestFixture::Mtx; - using value_type = typename Mtx::value_type; - using index_type = typename Mtx::index_type; - auto rand_cuda = Mtx::create(this->cuda); - rand_cuda->copy_from(gko::lend(this->rsorted_ref)); - auto trans_ref_linop = this->rsorted_ref->transpose(); - std::unique_ptr trans_ref = - gko::as(std::move(trans_ref_linop)); - - auto trans_cuda_linop = rand_cuda->transpose(); - std::unique_ptr trans_cuda = - gko::as(std::move(trans_cuda_linop)); - - GKO_ASSERT_MTX_EQ_SPARSITY(trans_ref, trans_cuda); - GKO_ASSERT_MTX_NEAR(trans_ref, trans_cuda, 0.0); -} - - -TYPED_TEST(Fbcsr, TransposeIsEquivalentToRefSortedBS7) -{ - using Mtx = typename TestFixture::Mtx; - using value_type = typename Mtx::value_type; - using index_type = typename Mtx::index_type; - auto rand_cuda = Mtx::create(this->cuda); - const index_type rand_brows = 50; - const index_type rand_bcols = 40; - const int block_size = 7; - auto rsorted_ref2 = - gko::test::generate_random_fbcsr( - this->ref, rand_brows, rand_bcols, block_size, false, false, - std::default_random_engine(43)); - rand_cuda->copy_from(gko::lend(rsorted_ref2)); - - auto trans_ref_linop = rsorted_ref2->transpose(); - std::unique_ptr trans_ref = - gko::as(std::move(trans_ref_linop)); - auto trans_cuda_linop = rand_cuda->transpose(); - std::unique_ptr trans_cuda = - gko::as(std::move(trans_cuda_linop)); - - GKO_ASSERT_MTX_EQ_SPARSITY(trans_ref, trans_cuda); - GKO_ASSERT_MTX_NEAR(trans_ref, trans_cuda, 0.0); -} - - -TYPED_TEST(Fbcsr, SpmvIsEquivalentToRefSorted) -{ - using Mtx = typename TestFixture::Mtx; - using Dense = typename TestFixture::Dense; - using value_type = typename Mtx::value_type; - auto rand_cuda = Mtx::create(this->cuda); - rand_cuda->copy_from(gko::lend(this->rsorted_ref)); - auto x_ref = Dense::create( - this->ref, gko::dim<2>(this->rsorted_ref->get_size()[1], 1)); - this->generate_sin(x_ref.get()); - auto x_cuda = Dense::create(this->cuda); - x_cuda->copy_from(x_ref.get()); - auto prod_ref = Dense::create( - this->ref, gko::dim<2>(this->rsorted_ref->get_size()[0], 1)); - auto prod_cuda = Dense::create(this->cuda, prod_ref->get_size()); - - rand_cuda->apply(x_cuda.get(), prod_cuda.get()); - this->rsorted_ref->apply(x_ref.get(), prod_ref.get()); - - const double tol = r::value; - GKO_ASSERT_MTX_NEAR(prod_ref, prod_cuda, 5 * tol); -} - - -TYPED_TEST(Fbcsr, SpmvMultiIsEquivalentToRefSorted) -{ - using Mtx = typename TestFixture::Mtx; - using Dense = typename TestFixture::Dense; - using value_type = typename Mtx::value_type; - auto rand_cuda = Mtx::create(this->cuda); - rand_cuda->copy_from(gko::lend(this->rsorted_ref)); - auto x_ref = Dense::create( - this->ref, gko::dim<2>(this->rsorted_ref->get_size()[1], 3)); - this->generate_sin(x_ref.get()); - auto x_cuda = Dense::create(this->cuda); - x_cuda->copy_from(x_ref.get()); - auto prod_ref = Dense::create( - this->ref, gko::dim<2>(this->rsorted_ref->get_size()[0], 3)); - auto prod_cuda = Dense::create(this->cuda, prod_ref->get_size()); - - rand_cuda->apply(x_cuda.get(), prod_cuda.get()); - this->rsorted_ref->apply(x_ref.get(), prod_ref.get()); - - const double tol = r::value; - GKO_ASSERT_MTX_NEAR(prod_ref, prod_cuda, 5 * tol); -} - - -TYPED_TEST(Fbcsr, AdvancedSpmvIsEquivalentToRefSorted) -{ - using Mtx = typename TestFixture::Mtx; - using Dense = typename TestFixture::Dense; - using value_type = typename TestFixture::value_type; - using real_type = typename TestFixture::real_type; - auto rand_cuda = Mtx::create(this->cuda); - rand_cuda->copy_from(gko::lend(this->rsorted_ref)); - auto x_ref = Dense::create( - this->ref, gko::dim<2>(this->rsorted_ref->get_size()[1], 1)); - this->generate_sin(x_ref.get()); - auto x_cuda = Dense::create(this->cuda); - x_cuda->copy_from(x_ref.get()); - auto prod_ref = Dense::create( - this->ref, gko::dim<2>(this->rsorted_ref->get_size()[0], 1)); - this->generate_sin(prod_ref.get()); - auto prod_cuda = Dense::create(this->cuda); - prod_cuda->copy_from(prod_ref.get()); - auto alpha_ref = Dense::create(this->ref, gko::dim<2>(1, 1)); - alpha_ref->get_values()[0] = - static_cast(2.4) + this->get_random_value(); - auto beta_ref = Dense::create(this->ref, gko::dim<2>(1, 1)); - beta_ref->get_values()[0] = -1.2; - auto alpha = Dense::create(this->cuda); - alpha->copy_from(alpha_ref.get()); - auto beta = Dense::create(this->cuda); - beta->copy_from(beta_ref.get()); - - rand_cuda->apply(alpha.get(), x_cuda.get(), beta.get(), prod_cuda.get()); - this->rsorted_ref->apply(alpha_ref.get(), x_ref.get(), beta_ref.get(), - prod_ref.get()); - - const double tol = r::value; - GKO_ASSERT_MTX_NEAR(prod_ref, prod_cuda, 5 * tol); -} - - -TYPED_TEST(Fbcsr, AdvancedSpmvMultiIsEquivalentToRefSorted) -{ - using Mtx = typename TestFixture::Mtx; - using Dense = typename TestFixture::Dense; - using value_type = typename TestFixture::value_type; - using real_type = typename TestFixture::real_type; - auto rand_cuda = Mtx::create(this->cuda); - rand_cuda->copy_from(gko::lend(this->rsorted_ref)); - auto x_ref = Dense::create( - this->ref, gko::dim<2>(this->rsorted_ref->get_size()[1], 3)); - this->generate_sin(x_ref.get()); - auto x_cuda = Dense::create(this->cuda); - x_cuda->copy_from(x_ref.get()); - auto prod_ref = Dense::create( - this->ref, gko::dim<2>(this->rsorted_ref->get_size()[0], 3)); - this->generate_sin(prod_ref.get()); - auto prod_cuda = Dense::create(this->cuda); - prod_cuda->copy_from(prod_ref.get()); - auto alpha_ref = Dense::create(this->ref, gko::dim<2>(1, 1)); - alpha_ref->get_values()[0] = - static_cast(2.4) + this->get_random_value(); - auto beta_ref = Dense::create(this->ref, gko::dim<2>(1, 1)); - beta_ref->get_values()[0] = -1.2; - auto alpha = Dense::create(this->cuda); - alpha->copy_from(alpha_ref.get()); - auto beta = Dense::create(this->cuda); - beta->copy_from(beta_ref.get()); - - rand_cuda->apply(alpha.get(), x_cuda.get(), beta.get(), prod_cuda.get()); - this->rsorted_ref->apply(alpha_ref.get(), x_ref.get(), beta_ref.get(), - prod_ref.get()); - - const double tol = r::value; - GKO_ASSERT_MTX_NEAR(prod_ref, prod_cuda, 5 * tol); -} - - -TYPED_TEST(Fbcsr, ConjTransposeIsEquivalentToRefSortedBS3) -{ - using Mtx = typename TestFixture::Mtx; - using value_type = typename Mtx::value_type; - using index_type = typename Mtx::index_type; - auto rand_cuda = Mtx::create(this->cuda); - rand_cuda->copy_from(gko::lend(this->rsorted_ref)); - auto trans_ref_linop = this->rsorted_ref->conj_transpose(); - std::unique_ptr trans_ref = - gko::as(std::move(trans_ref_linop)); - - auto trans_cuda_linop = rand_cuda->conj_transpose(); - std::unique_ptr trans_cuda = - gko::as(std::move(trans_cuda_linop)); - - GKO_ASSERT_MTX_EQ_SPARSITY(trans_ref, trans_cuda); - GKO_ASSERT_MTX_NEAR(trans_ref, trans_cuda, 0.0); -} - - -TYPED_TEST(Fbcsr, RecognizeSortedMatrix) -{ - using Mtx = typename TestFixture::Mtx; - auto rand_cuda = Mtx::create(this->cuda); - rand_cuda->copy_from(gko::lend(this->rsorted_ref)); - - ASSERT_TRUE(rand_cuda->is_sorted_by_column_index()); -} - - -TYPED_TEST(Fbcsr, RecognizeUnsortedMatrix) -{ - using Mtx = typename TestFixture::Mtx; - using index_type = typename TestFixture::index_type; - auto mat = this->rsorted_ref->clone(); - index_type* const colinds = mat->get_col_idxs(); - std::swap(colinds[0], colinds[1]); - auto unsrt_cuda = Mtx::create(this->cuda); - unsrt_cuda->copy_from(gko::lend(mat)); - - ASSERT_FALSE(unsrt_cuda->is_sorted_by_column_index()); -} - - -TYPED_TEST(Fbcsr, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - using Mtx = typename TestFixture::Mtx; - using value_type = typename Mtx::value_type; - auto rand_ref = Mtx::create(this->ref); - rand_ref->copy_from(this->rsorted_ref.get()); - auto rand_cuda = Mtx::create(this->cuda); - rand_cuda->copy_from(gko::lend(this->rsorted_ref)); - - rand_ref->compute_absolute_inplace(); - rand_cuda->compute_absolute_inplace(); - - const double tol = r::value; - GKO_ASSERT_MTX_NEAR(rand_ref, rand_cuda, tol); -} - - -TYPED_TEST(Fbcsr, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - using Mtx = typename TestFixture::Mtx; - using value_type = typename Mtx::value_type; - auto rand_cuda = Mtx::create(this->cuda); - rand_cuda->copy_from(gko::lend(this->rsorted_ref)); - - auto abs_mtx = this->rsorted_ref->compute_absolute(); - auto dabs_mtx = rand_cuda->compute_absolute(); - - const double tol = r::value; - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, tol); -} - - -} // namespace diff --git a/cuda/test/matrix/fft_kernels.cpp b/cuda/test/matrix/fft_kernels.cpp deleted file mode 100644 index 46e0493cafc..00000000000 --- a/cuda/test/matrix/fft_kernels.cpp +++ /dev/null @@ -1,280 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include - - -#include "cuda/test/utils.hpp" - - -namespace { - - -template -class Fft : public ::testing::Test { -protected: - using value_type = ValueType; - using Vec = gko::matrix::Dense; - using Mtx = gko::matrix::Fft; - using Mtx2 = gko::matrix::Fft2; - using Mtx3 = gko::matrix::Fft3; - - Fft() - : rand_engine(1364245), - n1{16}, - n2{32}, - n3{64}, - n{n1 * n2 * n3}, - cols{3}, - subcols{2}, - out_stride{6} - {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - - data = gko::test::generate_random_matrix( - n, cols, std::uniform_int_distribution<>(1, cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - ddata = Vec::create(cuda); - ddata->copy_from(this->data.get()); - data_strided = data->create_submatrix({0, n}, {0, subcols}); - ddata_strided = ddata->create_submatrix({0, n}, {0, subcols}); - out = data->clone(); - dout = data->clone(); - out_strided = Vec::create(ref, data_strided->get_size(), out_stride); - dout_strided = Vec::create(cuda, data_strided->get_size(), out_stride); - fft = Mtx::create(ref, n); - dfft = Mtx::create(cuda, n); - ifft = Mtx::create(ref, n, true); - difft = Mtx::create(cuda, n, true); - fft2 = Mtx2::create(ref, n1 * n2, n3); - dfft2 = Mtx2::create(cuda, n1 * n2, n3); - ifft2 = Mtx2::create(ref, n1 * n2, n3, true); - difft2 = Mtx2::create(cuda, n1 * n2, n3, true); - fft3 = Mtx3::create(ref, n1, n2, n3); - dfft3 = Mtx3::create(cuda, n1, n2, n3); - ifft3 = Mtx3::create(ref, n1, n2, n3, true); - difft3 = Mtx3::create(cuda, n1, n2, n3, true); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - std::shared_ptr ref; - std::shared_ptr cuda; - std::default_random_engine rand_engine; - size_t n1; - size_t n2; - size_t n3; - size_t n; - size_t cols; - size_t subcols; - size_t out_stride; - std::unique_ptr data; - std::unique_ptr ddata; - std::unique_ptr data_strided; - std::unique_ptr ddata_strided; - std::unique_ptr out; - std::unique_ptr dout; - std::unique_ptr out_strided; - std::unique_ptr dout_strided; - std::unique_ptr fft; - std::unique_ptr dfft; - std::unique_ptr ifft; - std::unique_ptr difft; - std::unique_ptr fft2; - std::unique_ptr dfft2; - std::unique_ptr ifft2; - std::unique_ptr difft2; - std::unique_ptr fft3; - std::unique_ptr dfft3; - std::unique_ptr ifft3; - std::unique_ptr difft3; -}; - - -TYPED_TEST_SUITE(Fft, gko::test::ComplexValueTypes, TypenameNameGenerator); - - -TYPED_TEST(Fft, Apply1DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft->apply(this->data.get(), this->out.get()); - this->dfft->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided1DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft->apply(this->data_strided.get(), this->out_strided.get()); - this->dfft->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply1DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft->apply(this->data.get(), this->out.get()); - this->difft->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided1DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft->apply(this->data_strided.get(), this->out_strided.get()); - this->difft->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply2DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft2->apply(this->data.get(), this->out.get()); - this->dfft2->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided2DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft2->apply(this->data_strided.get(), this->out_strided.get()); - this->dfft2->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply2DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft2->apply(this->data.get(), this->out.get()); - this->difft2->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided2DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft2->apply(this->data_strided.get(), this->out_strided.get()); - this->difft2->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply3DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft3->apply(this->data.get(), this->out.get()); - this->dfft3->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided3DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft3->apply(this->data_strided.get(), this->out_strided.get()); - this->dfft3->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply3DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft3->apply(this->data.get(), this->out.get()); - this->difft3->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided3DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft3->apply(this->data_strided.get(), this->out_strided.get()); - this->difft3->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -} // namespace diff --git a/cuda/test/matrix/hybrid_kernels.cpp b/cuda/test/matrix/hybrid_kernels.cpp deleted file mode 100644 index e560dbc0bfc..00000000000 --- a/cuda/test/matrix/hybrid_kernels.cpp +++ /dev/null @@ -1,279 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/matrix/hybrid_kernels.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -class Hybrid : public ::testing::Test { -protected: - using Mtx = gko::matrix::Hybrid<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; - - Hybrid() : rand_engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data(int num_vectors = 1, - std::shared_ptr strategy = - std::make_shared()) - { - mtx = Mtx::create(ref, strategy); - mtx->copy_from(gen_mtx(532, 231, 1)); - expected = gen_mtx(532, num_vectors, 1); - y = gen_mtx(231, num_vectors, 1); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = Mtx::create(cuda, strategy); - dmtx->copy_from(mtx.get()); - dresult = gko::clone(cuda, expected); - dy = gko::clone(cuda, y); - dalpha = gko::clone(cuda, alpha); - dbeta = gko::clone(cuda, beta); - } - - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; -}; - - -TEST_F(Hybrid, SubMatrixExecutorAfterCopyIsEquivalentToExcutor) -{ - set_up_apply_data(); - - auto coo_mtx = dmtx->get_coo(); - auto ell_mtx = dmtx->get_ell(); - - ASSERT_EQ(coo_mtx->get_executor(), cuda); - ASSERT_EQ(ell_mtx->get_executor(), cuda); - ASSERT_EQ(dmtx->get_executor(), cuda); -} - - -TEST_F(Hybrid, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Hybrid, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Hybrid, SimpleApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Hybrid, AdvancedApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Hybrid, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(cuda, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Hybrid, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(cuda, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Hybrid, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(1, std::make_shared(2)); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(cuda); - - mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Hybrid, MoveToCsrIsEquivalentToRef) -{ - set_up_apply_data(1, std::make_shared(2)); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(cuda); - - mtx->move_to(csr_mtx.get()); - dmtx->move_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 1e-14); -} - - -TEST_F(Hybrid, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Hybrid, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Hybrid, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, std::make_shared(2)); - using AbsMtx = gko::remove_complex; - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - auto abs_strategy = gko::as(abs_mtx->get_strategy()); - auto dabs_strategy = - gko::as(dabs_mtx->get_strategy()); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); - GKO_ASSERT_EQ(abs_strategy->get_num_columns(), - dabs_strategy->get_num_columns()); - GKO_ASSERT_EQ(abs_strategy->get_num_columns(), 2); -} - - -} // namespace diff --git a/cuda/test/matrix/sellp_kernels.cpp b/cuda/test/matrix/sellp_kernels.cpp deleted file mode 100644 index c09ec410e00..00000000000 --- a/cuda/test/matrix/sellp_kernels.cpp +++ /dev/null @@ -1,342 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/matrix/sellp_kernels.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -class Sellp : public ::testing::Test { -protected: - using Mtx = gko::matrix::Sellp<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; - - Sellp() : rand_engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_matrix( - int total_cols = 1, int slice_size = gko::matrix::default_slice_size, - int stride_factor = gko::matrix::default_stride_factor) - { - mtx = gen_mtx(532, 231); - empty = Mtx::create(ref); - expected = gen_mtx(532, total_cols); - y = gen_mtx(231, total_cols); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = gko::clone(cuda, mtx); - dempty = Mtx::create(cuda); - dresult = gko::clone(cuda, expected); - dy = gko::clone(cuda, y); - dalpha = gko::clone(cuda, alpha); - dbeta = gko::clone(cuda, beta); - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr empty; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dempty; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; -}; - - -TEST_F(Sellp, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_matrix(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_matrix(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, SimpleApplyWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(1, 32, 2); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, AdvancedApplyWithSliceSizeAndStrideFActorIsEquivalentToRef) -{ - set_up_apply_matrix(1, 32, 2); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, SimpleApplyMultipleRHSIsEquivalentToRef) -{ - set_up_apply_matrix(64); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, AdvancedApplyMultipleRHSIsEquivalentToRef) -{ - set_up_apply_matrix(64); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, - SimpleApplyMultipleRHSWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(32, 2); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, - AdvancedApplyMultipleRHSWithSliceSizeAndStrideFActorIsEquivalentToRef) -{ - set_up_apply_matrix(32, 2); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(cuda, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Sellp, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(cuda, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(cuda, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Sellp, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(cuda); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Sellp, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(cuda); - - mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Sellp, ConvertEmptyToDenseIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(cuda); - - empty->convert_to(dense_mtx.get()); - dempty->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Sellp, ConvertEmptyToCsrIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(cuda); - - empty->convert_to(csr_mtx.get()); - dempty->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Sellp, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_matrix(64); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Sellp, ExtractDiagonalWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(64, 32, 2); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Sellp, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_matrix(64, 32, 2); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Sellp, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_matrix(64, 32, 2); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -} // namespace diff --git a/cuda/test/preconditioner/CMakeLists.txt b/cuda/test/preconditioner/CMakeLists.txt deleted file mode 100644 index 575384a4c84..00000000000 --- a/cuda/test/preconditioner/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -ginkgo_create_test(jacobi_kernels) -ginkgo_create_test(isai_kernels) diff --git a/cuda/test/preconditioner/isai_kernels.cpp b/cuda/test/preconditioner/isai_kernels.cpp deleted file mode 100644 index 721e9b9eb0c..00000000000 --- a/cuda/test/preconditioner/isai_kernels.cpp +++ /dev/null @@ -1,674 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/preconditioner/isai_kernels.hpp" -#include "cuda/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -enum struct matrix_type { lower, upper, general, spd }; - - -class Isai : public ::testing::Test { -protected: - using value_type = double; - using index_type = gko::int32; - using Csr = gko::matrix::Csr; - using Dense = gko::matrix::Dense; - Isai() : rand_engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - std::unique_ptr clone_allocations(const Csr* csr_mtx) - { - if (csr_mtx->get_executor() != ref) { - return {nullptr}; - } - const auto num_elems = csr_mtx->get_num_stored_elements(); - auto sparsity = csr_mtx->clone(); - - // values are now filled with invalid data to catch potential errors - auto begin_values = sparsity->get_values(); - auto end_values = begin_values + num_elems; - std::fill(begin_values, end_values, -gko::one()); - return sparsity; - } - - void initialize_data(matrix_type type, gko::size_type n, - gko::size_type row_limit) - { - const bool for_lower_tm = type == matrix_type::lower; - auto nz_dist = std::uniform_int_distribution(1, row_limit); - auto val_dist = std::uniform_real_distribution(-1., 1.); - mtx = Csr::create(ref); - if (type == matrix_type::general) { - auto dense_mtx = gko::test::generate_random_matrix( - n, n, nz_dist, val_dist, rand_engine, ref, gko::dim<2>{n, n}); - ensure_diagonal(dense_mtx.get()); - mtx->copy_from(dense_mtx.get()); - } else if (type == matrix_type::spd) { - auto dense_mtx = gko::test::generate_random_band_matrix( - n, row_limit / 4, row_limit / 4, val_dist, rand_engine, ref, - gko::dim<2>{n, n}); - auto transp = gko::as(dense_mtx->transpose()); - auto spd_mtx = Dense::create(ref, gko::dim<2>{n, n}); - dense_mtx->apply(transp.get(), spd_mtx.get()); - mtx->copy_from(spd_mtx.get()); - } else { - mtx = gko::test::generate_random_triangular_matrix( - n, true, for_lower_tm, nz_dist, val_dist, rand_engine, ref, - gko::dim<2>{n, n}); - } - inverse = clone_allocations(mtx.get()); - - d_mtx = gko::clone(cuda, mtx); - d_inverse = gko::clone(cuda, inverse); - } - - template - std::unique_ptr read(const char* name) - { - std::ifstream mtxstream{std::string{gko::matrices::location_isai_mtxs} + - name}; - auto result = gko::read(mtxstream, ref); - // to avoid removing 0s, the matrices store 12345 instead - for (gko::size_type i = 0; i < result->get_num_stored_elements(); ++i) { - auto& val = result->get_values()[i]; - if (val == static_cast(12345.0)) { - val = 0; - } - } - return std::move(result); - } - - void ensure_diagonal(Dense* mtx) - { - for (int i = 0; i < mtx->get_size()[0]; ++i) { - mtx->at(i, i) = gko::one(); - } - } - - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr inverse; - - std::unique_ptr d_mtx; - std::unique_ptr d_inverse; -}; - - -TEST_F(Isai, CudaIsaiGenerateLinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 536, 31); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(cuda, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::cuda::isai::generate_tri_inverse( - cuda, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, CudaIsaiGenerateUinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 615, 31); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(cuda, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::cuda::isai::generate_tri_inverse( - cuda, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, CudaIsaiGenerateAinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 615, 15); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(cuda, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::cuda::isai::generate_general_inverse( - cuda, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, CudaIsaiGenerateSpdinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 15); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(cuda, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::cuda::isai::generate_general_inverse( - cuda, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 15 * r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, CudaIsaiGenerateLinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 554, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(cuda, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::cuda::isai::generate_tri_inverse( - cuda, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, CudaIsaiGenerateUinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 695, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(cuda, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::cuda::isai::generate_tri_inverse( - cuda, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, CudaIsaiGenerateAinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 695, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(cuda, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::cuda::isai::generate_general_inverse( - cuda, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 10 * r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, CudaIsaiGenerateSpdinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(cuda, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::cuda::isai::generate_general_inverse( - cuda, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 10 * r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, CudaIsaiGenerateExcessLinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 518, 40); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(cuda, a1); - gko::array da2(cuda, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(cuda, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(cuda, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::cuda::isai::generate_excess_system( - cuda, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, CudaIsaiGenerateExcessUinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 673, 51); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(cuda, a1); - gko::array da2(cuda, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(cuda, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(cuda, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::cuda::isai::generate_excess_system( - cuda, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, CudaIsaiGenerateExcessAinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 100, 51); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(cuda, a1); - gko::array da2(cuda, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(cuda, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(cuda, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::cuda::isai::generate_excess_system( - cuda, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, CudaIsaiGenerateExcessSpdinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(cuda, a1); - gko::array da2(cuda, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(cuda, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(cuda, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::cuda::isai::generate_excess_system( - cuda, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, CudaIsaiGeneratePartialExcessIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(cuda, a1); - gko::array da2(cuda, a2); - auto e_dim = a1.get_data()[10] - a1.get_data()[5]; - auto e_nnz = a2.get_data()[10] - a2.get_data()[5]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(cuda, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(cuda, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 5u, 10u); - gko::kernels::cuda::isai::generate_excess_system( - cuda, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 5u, 10u); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, CudaIsaiScaleExcessSolutionIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(cuda, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(cuda, e_rhs); - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scale_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), 0, num_rows); - gko::kernels::cuda::isai::scale_excess_solution(cuda, da1.get_const_data(), - de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); -} - - -TEST_F(Isai, CudaIsaiScalePartialExcessSolutionIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(cuda, a1); - auto e_dim = a1.get_data()[10] - a1.get_data()[5]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(cuda, e_rhs); - - gko::kernels::reference::isai::scale_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), 5u, 10u); - gko::kernels::cuda::isai::scale_excess_solution(cuda, da1.get_const_data(), - de_rhs.get(), 5u, 10u); - - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); -} - - -TEST_F(Isai, CudaIsaiScatterExcessSolutionLIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 572, 52); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(cuda, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(cuda, e_rhs); - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::cuda::isai::scatter_excess_solution( - cuda, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, CudaIsaiScatterExcessSolutionUIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 702, 45); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(cuda, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(cuda, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::cuda::isai::scatter_excess_solution( - cuda, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, CudaIsaiScatterExcessSolutionAIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 702, 45); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(cuda, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(cuda, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::cuda::isai::scatter_excess_solution( - cuda, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, CudaIsaiScatterExcessSolutionSpdIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(cuda, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(cuda, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::cuda::isai::scatter_excess_solution( - cuda, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, CudaIsaiScatterPartialExcessSolutionIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(cuda, a1); - auto e_dim = a1.get_data()[10] - a1.get_data()[5]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(cuda, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 5u, 10u); - gko::kernels::cuda::isai::scatter_excess_solution( - cuda, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 5u, 10u); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -} // namespace diff --git a/cuda/test/preconditioner/jacobi_kernels.cpp b/cuda/test/preconditioner/jacobi_kernels.cpp deleted file mode 100644 index bd6d1ee2915..00000000000 --- a/cuda/test/preconditioner/jacobi_kernels.cpp +++ /dev/null @@ -1,908 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include - - -#include "core/test/utils/unsort_matrix.hpp" -#include "core/utils/matrix_utils.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -class Jacobi : public ::testing::Test { -protected: - using Bj = gko::preconditioner::Jacobi<>; - using Mtx = gko::matrix::Csr<>; - using Vec = gko::matrix::Dense<>; - using mtx_data = gko::matrix_data<>; - using value_type = typename Mtx::value_type; - using index_type = typename Mtx::index_type; - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - void initialize_data( - std::initializer_list block_pointers, - std::initializer_list block_precisions, - std::initializer_list condition_numbers, - gko::uint32 max_block_size, int min_nnz, int max_nnz, int num_rhs = 1, - double accuracy = 0.1, bool skip_sorting = true) - { - std::default_random_engine engine(42); - const auto dim = *(end(block_pointers) - 1); - if (condition_numbers.size() == 0) { - mtx = gko::test::generate_random_matrix( - dim, dim, std::uniform_int_distribution<>(min_nnz, max_nnz), - std::normal_distribution<>(0.0, 1.0), engine, ref); - } else { - std::vector blocks; - for (gko::size_type i = 0; i < block_pointers.size() - 1; ++i) { - const auto size = - begin(block_pointers)[i + 1] - begin(block_pointers)[i]; - const auto cond = begin(condition_numbers)[i]; - blocks.push_back(mtx_data::cond( - size, cond, std::normal_distribution<>(-1, 1), engine)); - } - mtx = Mtx::create(ref); - mtx->read(mtx_data::diag(begin(blocks), end(blocks))); - } - gko::array block_ptrs(ref, block_pointers); - gko::array block_prec(ref, block_precisions); - if (block_prec.get_num_elems() == 0) { - bj_factory = Bj::build() - .with_max_block_size(max_block_size) - .with_block_pointers(block_ptrs) - .with_skip_sorting(skip_sorting) - .on(ref); - d_bj_factory = Bj::build() - .with_max_block_size(max_block_size) - .with_block_pointers(block_ptrs) - .with_skip_sorting(skip_sorting) - .on(cuda); - } else { - bj_factory = Bj::build() - .with_max_block_size(max_block_size) - .with_block_pointers(block_ptrs) - .with_storage_optimization(block_prec) - .with_accuracy(accuracy) - .with_skip_sorting(skip_sorting) - .on(ref); - d_bj_factory = Bj::build() - .with_max_block_size(max_block_size) - .with_block_pointers(block_ptrs) - .with_storage_optimization(block_prec) - .with_accuracy(accuracy) - .with_skip_sorting(skip_sorting) - .on(cuda); - } - b = gko::test::generate_random_matrix( - dim, num_rhs, std::uniform_int_distribution<>(num_rhs, num_rhs), - std::normal_distribution<>(0.0, 1.0), engine, ref); - d_b = gko::clone(cuda, b); - x = gko::test::generate_random_matrix( - dim, num_rhs, std::uniform_int_distribution<>(num_rhs, num_rhs), - std::normal_distribution<>(0.0, 1.0), engine, ref); - d_x = gko::clone(cuda, x); - } - - const gko::precision_reduction dp{}; - const gko::precision_reduction sp{0, 1}; - const gko::precision_reduction hp{0, 2}; - const gko::precision_reduction tp{1, 0}; - const gko::precision_reduction qp{2, 0}; - const gko::precision_reduction up{1, 1}; - const gko::precision_reduction ap{gko::precision_reduction::autodetect()}; - - std::shared_ptr ref; - std::shared_ptr cuda; - std::shared_ptr mtx; - std::unique_ptr x; - std::unique_ptr b; - std::unique_ptr d_x; - std::unique_ptr d_b; - - std::unique_ptr bj_factory; - std::unique_ptr d_bj_factory; -}; - - -TEST_F(Jacobi, CudaFindNaturalBlocksEquivalentToRef) -{ - /* example matrix: - 1 1 - 1 1 - 1 1 - 1 1 - */ - auto mtx = share(Mtx::create(ref)); - mtx->read({{4, 4}, - {{0, 0, 1.0}, - {0, 1, 1.0}, - {1, 0, 1.0}, - {1, 1, 1.0}, - {2, 0, 1.0}, - {2, 2, 1.0}, - {3, 0, 1.0}, - {3, 2, 1.0}}}); - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(cuda)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, CudaExecutesSupervariableAgglomerationEquivalentToRef) -{ - /* example matrix: - 1 1 - 1 1 - 1 1 - 1 1 - 1 - */ - auto mtx = share(Mtx::create(ref)); - mtx->read({{5, 5}, - {{0, 0, 1.0}, - {0, 1, 1.0}, - {1, 0, 1.0}, - {1, 1, 1.0}, - {2, 2, 1.0}, - {2, 3, 1.0}, - {3, 2, 1.0}, - {3, 3, 1.0}, - {4, 4, 1.0}}}); - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(cuda)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, CudaFindNaturalBlocksInLargeMatrixEquivalentToRef) -{ - /* example matrix: - 1 1 - 1 1 - 1 1 - 1 1 - 1 1 - 1 1 - */ - using data = gko::matrix_data; - auto mtx = share(Mtx::create(ref)); - mtx->read(data::diag({550, 550}, {{1.0, 1.0, 0.0, 0.0, 0.0, 0.0}, - {1.0, 1.0, 0.0, 0.0, 0.0, 0.0}, - {1.0, 0.0, 1.0, 0.0, 0.0, 0.0}, - {1.0, 0.0, 1.0, 0.0, 0.0, 0.0}, - {1.0, 0.0, 1.0, 0.0, 0.0, 0.0}, - {1.0, 0.0, 1.0, 0.0, 0.0, 0.0}})); - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(cuda)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, - CudaExecutesSupervariableAgglomerationInLargeMatrixEquivalentToRef) -{ - /* example matrix: - 1 1 - 1 1 - 1 1 - 1 1 - 1 - */ - using data = gko::matrix_data; - auto mtx = share(Mtx::create(ref)); - mtx->read(data::diag({550, 550}, {{1.0, 1.0, 0.0, 0.0, 0.0}, - {1.0, 1.0, 0.0, 0.0, 0.0}, - {0.0, 0.0, 1.0, 1.0, 0.0}, - {0.0, 0.0, 1.0, 1.0, 0.0}, - {0.0, 0.0, 0.0, 0.0, 1.0}})); - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(cuda)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, - CudaExecutesSupervarAgglomerationEquivalentToRefFor150NonzerowsPerRow) -{ - /* example matrix duplicated 50 times: - 1 1 1 - 1 1 1 - 1 1 1 - 1 1 1 - 1 1 - */ - using data = gko::matrix_data; - auto mtx = share(Mtx::create(ref)); - mtx->read({{50, 50}, - {{1.0, 1.0, 0.0, 1.0, 0.0}, - {1.0, 1.0, 0.0, 1.0, 0.0}, - {1.0, 0.0, 1.0, 1.0, 0.0}, - {1.0, 0.0, 1.0, 1.0, 0.0}, - {0.0, 0.0, 1.0, 0.0, 1.0}}}); - - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(cuda)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithBlockSize32Sorted) -{ - initialize_data({0, 32, 64, 96, 128}, {}, {}, 32, 100, 110); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj.get()), gko::as(bj.get()), 1e-13); -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithBlockSize32Unsorted) -{ - std::default_random_engine engine(42); - initialize_data({0, 32, 64, 96, 128}, {}, {}, 32, 100, 110, 1, 0.1, false); - gko::test::unsort_matrix(mtx.get(), engine); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj.get()), gko::as(bj.get()), 1e-13); -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithDifferentBlockSize) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 32, - 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj.get()), gko::as(bj.get()), 1e-13); -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithMPW) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj.get()), gko::as(bj.get()), 1e-13); -} - - -TEST_F(Jacobi, CudaTransposedPreconditionerEquivalentToRefWithMPW) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - d_bj->copy_from(bj.get()); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj->transpose()), - gko::as(bj->transpose()), 1e-14); -} - - -TEST_F(Jacobi, CudaConjTransposedPreconditionerEquivalentToRefWithMPW) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - d_bj->copy_from(bj.get()); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj->conj_transpose()), - gko::as(bj->conj_transpose()), 1e-14); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRefWithBlockSize32) -{ - initialize_data({0, 32, 64, 96, 128}, {}, {}, 32, 100, 111); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRefWithDifferentBlockSize) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 32, - 97, 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, CudaScalarApplyEquivalentToRef) -{ - gko::size_type dim = 313; - std::default_random_engine engine(42); - auto dense_data = - gko::test::generate_random_matrix_data( - dim, dim, std::uniform_int_distribution<>(1, dim), - std::normal_distribution<>(1.0, 2.0), engine); - gko::utils::make_diag_dominant(dense_data); - auto dense_smtx = gko::share(Vec::create(ref)); - dense_smtx->read(dense_data); - auto smtx = gko::share(Mtx::create(ref)); - smtx->copy_from(dense_smtx.get()); - auto sb = gko::share(gko::test::generate_random_matrix( - dim, 3, std::uniform_int_distribution<>(1, 1), - std::normal_distribution<>(0.0, 1.0), engine, ref)); - auto sx = Vec::create(ref, sb->get_size()); - - auto d_smtx = gko::share(Mtx::create(cuda)); - auto d_sb = gko::share(Vec::create(cuda)); - auto d_sx = gko::share(Vec::create(cuda, sb->get_size())); - d_smtx->copy_from(smtx.get()); - d_sb->copy_from(sb.get()); - - auto sj = Bj::build().with_max_block_size(1u).on(ref)->generate(smtx); - auto d_sj = Bj::build().with_max_block_size(1u).on(cuda)->generate(d_smtx); - - sj->apply(sb.get(), sx.get()); - d_sj->apply(d_sb.get(), d_sx.get()); - - GKO_ASSERT_MTX_NEAR(sx.get(), d_sx.get(), 1e-12); -} - - -TEST_F(Jacobi, CudaLinearCombinationApplyEquivalentToRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, cuda); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, cuda); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(alpha.get(), b.get(), beta.get(), x.get()); - d_bj->apply(d_alpha.get(), d_b.get(), d_beta.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, CudaScalarLinearCombinationApplyEquivalentToRef) -{ - gko::size_type dim = 313; - std::default_random_engine engine(42); - auto dense_data = - gko::test::generate_random_matrix_data( - dim, dim, std::uniform_int_distribution<>(1, dim), - std::normal_distribution<>(1.0, 2.0), engine); - gko::utils::make_diag_dominant(dense_data); - auto dense_smtx = gko::share(Vec::create(ref)); - dense_smtx->read(dense_data); - auto smtx = gko::share(Mtx::create(ref)); - smtx->copy_from(dense_smtx.get()); - auto sb = gko::share(gko::test::generate_random_matrix( - dim, 3, std::uniform_int_distribution<>(1, 1), - std::normal_distribution<>(0.0, 1.0), engine, ref, gko::dim<2>(dim, 3), - 4)); - auto sx = gko::share(gko::test::generate_random_matrix( - dim, 3, std::uniform_int_distribution<>(1, 1), - std::normal_distribution<>(0.0, 1.0), engine, ref, gko::dim<2>(dim, 3), - 4)); - - auto d_smtx = gko::share(gko::clone(cuda, smtx)); - auto d_sb = gko::share(gko::clone(cuda, sb)); - auto d_sx = gko::share(gko::clone(cuda, sx)); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, cuda); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, cuda); - - auto sj = Bj::build().with_max_block_size(1u).on(ref)->generate(smtx); - auto d_sj = Bj::build().with_max_block_size(1u).on(cuda)->generate(d_smtx); - - sj->apply(alpha.get(), sb.get(), beta.get(), sx.get()); - d_sj->apply(d_alpha.get(), d_sb.get(), d_beta.get(), d_sx.get()); - - GKO_ASSERT_MTX_NEAR(sx.get(), d_sx.get(), 1e-12); -} - - -TEST_F(Jacobi, CudaApplyToMultipleVectorsEquivalentToRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99, 5); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, CudaLinearCombinationApplyToMultipleVectorsEquivalentToRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99, 5); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, cuda); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, cuda); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(alpha.get(), b.get(), beta.get(), x.get()); - d_bj->apply(d_alpha.get(), d_b.get(), d_beta.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, ComputesTheSameConditionNumberAsRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = clone(ref, d_bj_factory->generate(mtx)); - - for (int i = 0; i < gko::as(bj.get())->get_num_blocks(); ++i) { - EXPECT_NEAR(bj->get_conditioning()[i], d_bj->get_conditioning()[i], - 1e-9); - } -} - - -TEST_F(Jacobi, SelectsTheSamePrecisionsAsRef) -{ - initialize_data( - {0, 2, 14, 27, 40, 51, 61, 70, 80, 92, 100}, - {ap, ap, ap, ap, ap, ap, ap, ap, ap, ap}, - {1e+0, 1e+0, 1e+2, 1e+3, 1e+4, 1e+4, 1e+6, 1e+7, 1e+8, 1e+9}, 13, 97, - 99, 1, 0.2); - - auto bj = bj_factory->generate(mtx); - auto d_bj = gko::clone(ref, d_bj_factory->generate(mtx)); - - auto bj_prec = - bj->get_parameters().storage_optimization.block_wise.get_const_data(); - auto d_bj_prec = - d_bj->get_parameters().storage_optimization.block_wise.get_const_data(); - for (int i = 0; i < gko::as(bj.get())->get_num_blocks(); ++i) { - EXPECT_EQ(bj_prec[i], d_bj_prec[i]); - } -} - - -TEST_F(Jacobi, AvoidsPrecisionsThatOverflow) -{ - auto mtx = gko::matrix::Csr<>::create(cuda); - // clang-format off - mtx->read(mtx_data::diag({ - // perfectly conditioned block, small value difference, - // can use fp16 (5, 10) - {{2.0, 1.0}, - {1.0, 2.0}}, - // perfectly conditioned block (scaled orthogonal), - // with large value difference, need fp16 (7, 8) - {{1e-8, -1e-16}, - {1e-16, 1e-8}} - })); - // clang-format on - - auto bj = - Bj::build() - .with_max_block_size(13u) - .with_block_pointers(gko::array(cuda, {0, 2, 4})) - .with_storage_optimization(gko::precision_reduction::autodetect()) - .with_accuracy(0.1) - .on(cuda) - ->generate(give(mtx)); - - // both blocks are in the same group, both need (7, 8) - auto h_bj = clone(ref, bj); - auto prec = - h_bj->get_parameters().storage_optimization.block_wise.get_const_data(); - EXPECT_EQ(prec[0], gko::precision_reduction(1, 1)); - ASSERT_EQ(prec[1], gko::precision_reduction(1, 1)); -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithFullPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-13); -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, sp, sp, sp, sp, sp, sp, sp, sp, sp}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-7); -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithCustomReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {tp, tp, tp, tp, tp, tp, tp, tp, tp, tp, tp}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-6); -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithQuarteredPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {hp, hp, hp, hp, hp, hp, hp, hp, hp, hp, hp}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-3); -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithCustomQuarteredPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {qp, qp, qp, qp, qp, qp, qp, qp, qp, qp, qp}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-1); -} - - -TEST_F(Jacobi, CudaPreconditionerEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-1); -} - - -TEST_F(Jacobi, CudaTransposedPreconditionerEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - d_bj->copy_from(bj.get()); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj->transpose()), - gko::as(bj->transpose()), 1e-14); -} - - -TEST_F(Jacobi, - CudaConjTransposedPreconditionerEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - d_bj->copy_from(bj.get()); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj->conj_transpose()), - gko::as(bj->conj_transpose()), 1e-14); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRefWithFullPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRefWithReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, sp, sp, sp, sp, sp, sp, sp, sp, sp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRefWithCustomReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {tp, tp, tp, tp, tp, tp, tp, tp, tp, tp, tp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-5); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRefWithQuarteredPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {hp, hp, hp, hp, hp, hp, hp, hp, hp, hp, hp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-2); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRefWithCustomReducedAndReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {up, up, up, up, up, up, up, up, up, up, up}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-2); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRefWithCustomQuarteredPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {qp, qp, qp, qp, qp, qp, qp, qp, qp, qp, qp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -TEST_F(Jacobi, CudaApplyEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-1); -} - - -TEST_F(Jacobi, CudaLinearCombinationApplyEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, dp, dp, sp, sp, sp, dp, dp, sp, dp, sp}, {}, 13, 97, - 99); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, cuda); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, cuda); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -TEST_F(Jacobi, CudaApplyToMultipleVectorsEquivalentToRefWithFullPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, - 99, 5); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, CudaApplyToMultipleVectorsEquivalentToRefWithReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, sp, sp, sp, sp, sp, sp, sp, sp, sp}, {}, 13, 97, - 99, 5); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -TEST_F(Jacobi, CudaApplyToMultipleVectorsEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99, 5); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-1); -} - - -TEST_F( - Jacobi, - CudaLinearCombinationApplyToMultipleVectorsEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, dp, dp, sp, sp, sp, dp, dp, sp, dp, sp}, {}, 13, 97, - 99, 5); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, cuda); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, cuda); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -} // namespace diff --git a/cuda/test/solver/CMakeLists.txt b/cuda/test/solver/CMakeLists.txt index efaa399c496..0220d94c8d9 100644 --- a/cuda/test/solver/CMakeLists.txt +++ b/cuda/test/solver/CMakeLists.txt @@ -1,6 +1,2 @@ -ginkgo_create_test(gmres_kernels) -ginkgo_create_test(cb_gmres_kernels) -ginkgo_create_test(idr_kernels) -ginkgo_create_test(multigrid_kernels) ginkgo_create_test(lower_trs_kernels ADDITIONAL_INCLUDES ${CUDA_INCLUDE_DIRS}) ginkgo_create_test(upper_trs_kernels ADDITIONAL_INCLUDES ${CUDA_INCLUDE_DIRS}) diff --git a/cuda/test/solver/cb_gmres_kernels.cpp b/cuda/test/solver/cb_gmres_kernels.cpp deleted file mode 100644 index a587a16545d..00000000000 --- a/cuda/test/solver/cb_gmres_kernels.cpp +++ /dev/null @@ -1,347 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/solver/cb_gmres_accessor.hpp" -#include "core/solver/cb_gmres_kernels.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -class CbGmres : public ::testing::Test { -protected: - using value_type = double; - using storage_type = float; - using index_type = int; - using size_type = gko::size_type; - using Range3dHelper = - gko::cb_gmres::Range3dHelper; - using Range3d = typename Range3dHelper::Range; - using Dense = gko::matrix::Dense; - using Mtx = Dense; - static constexpr unsigned int default_krylov_dim_mixed{100}; - - CbGmres() : rand_engine(30) {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(num_cols, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - Range3dHelper generate_krylov_helper(gko::dim<3> size) - { - auto helper = Range3dHelper{ref, size}; - auto& bases = helper.get_bases(); - const auto num_rows = size[0] * size[1]; - const auto num_cols = size[2]; - auto temp_krylov_bases = gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(num_cols, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, - ref); - std::copy_n(temp_krylov_bases->get_const_values(), - bases.get_num_elems(), bases.get_data()); - // Only useful when the Accessor actually has a scale - auto range = helper.get_range(); - auto dist = std::normal_distribution(-1, 1); - for (size_type k = 0; k < size[0]; ++k) { - for (size_type i = 0; i < size[2]; ++i) { - gko::cb_gmres::helper_functions_accessor::write_scalar( - range, k, i, dist(rand_engine)); - } - } - return helper; - } - - void initialize_data() - { -#ifdef GINKGO_FAST_TESTS - int m = 123; -#else - int m = 597; -#endif - int n = 43; - x = gen_mtx(m, n); - y = gen_mtx(default_krylov_dim_mixed, n); - before_preconditioner = Mtx::create_with_config_of(x.get()); - b = gen_mtx(m, n); - arnoldi_norm = gen_mtx(3, n); - gko::dim<3> krylov_bases_dim(default_krylov_dim_mixed + 1, m, n); - range_helper = generate_krylov_helper(krylov_bases_dim); - - next_krylov_basis = gen_mtx(m, n); - hessenberg = - gen_mtx(default_krylov_dim_mixed + 1, default_krylov_dim_mixed * n); - hessenberg_iter = gen_mtx(default_krylov_dim_mixed + 1, n); - buffer_iter = gen_mtx(default_krylov_dim_mixed + 1, n); - residual = gen_mtx(m, n); - residual_norm = gen_mtx(1, n); - residual_norm_collection = gen_mtx(default_krylov_dim_mixed + 1, n); - givens_sin = gen_mtx(default_krylov_dim_mixed, n); - givens_cos = gen_mtx(default_krylov_dim_mixed, n); - stop_status = - std::make_unique>(ref, n); - for (size_t i = 0; i < stop_status->get_num_elems(); ++i) { - stop_status->get_data()[i].reset(); - } - reorth_status = - std::make_unique>(ref, n); - for (size_t i = 0; i < reorth_status->get_num_elems(); ++i) { - reorth_status->get_data()[i].reset(); - } - final_iter_nums = std::make_unique>(ref, n); - for (size_t i = 0; i < final_iter_nums->get_num_elems(); ++i) { - final_iter_nums->get_data()[i] = 5; - } - num_reorth = std::make_unique>(ref, n); - for (size_t i = 0; i < num_reorth->get_num_elems(); ++i) { - num_reorth->get_data()[i] = 5; - } - - d_x = gko::clone(cuda, x); - d_before_preconditioner = Mtx::create_with_config_of(d_x.get()); - d_y = gko::clone(cuda, y); - d_b = gko::clone(cuda, b); - d_arnoldi_norm = gko::clone(cuda, arnoldi_norm); - d_range_helper = Range3dHelper{cuda, {}}; - d_range_helper = range_helper; - d_next_krylov_basis = gko::clone(cuda, next_krylov_basis); - d_hessenberg = gko::clone(cuda, hessenberg); - d_hessenberg_iter = gko::clone(cuda, hessenberg_iter); - d_buffer_iter = gko::clone(cuda, buffer_iter); - d_residual = gko::clone(cuda, residual); - d_residual_norm = gko::clone(cuda, residual_norm); - d_residual_norm_collection = gko::clone(cuda, residual_norm_collection); - d_givens_sin = gko::clone(cuda, givens_sin); - d_givens_cos = gko::clone(cuda, givens_cos); - d_stop_status = std::make_unique>( - cuda, *stop_status); - d_reorth_status = std::make_unique>( - cuda, *reorth_status); - d_final_iter_nums = std::make_unique>( - cuda, *final_iter_nums); - d_num_reorth = - std::make_unique>(cuda, *num_reorth); - } - - void assert_krylov_bases_near() - { - gko::array d_to_host{ref}; - auto& krylov_bases = range_helper.get_bases(); - d_to_host = d_range_helper.get_bases(); - const auto tolerance = r::value; - using std::abs; - for (gko::size_type i = 0; i < krylov_bases.get_num_elems(); ++i) { - const auto ref_value = krylov_bases.get_const_data()[i]; - const auto dev_value = d_to_host.get_const_data()[i]; - ASSERT_LE(abs(dev_value - ref_value), tolerance); - } - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::default_random_engine rand_engine; - - std::unique_ptr before_preconditioner; - std::unique_ptr x; - std::unique_ptr y; - std::unique_ptr b; - std::unique_ptr arnoldi_norm; - Range3dHelper range_helper; - std::unique_ptr next_krylov_basis; - std::unique_ptr hessenberg; - std::unique_ptr hessenberg_iter; - std::unique_ptr buffer_iter; - std::unique_ptr residual; - std::unique_ptr residual_norm; - std::unique_ptr residual_norm_collection; - std::unique_ptr givens_sin; - std::unique_ptr givens_cos; - std::unique_ptr> stop_status; - std::unique_ptr> reorth_status; - std::unique_ptr> final_iter_nums; - std::unique_ptr> num_reorth; - - std::unique_ptr d_x; - std::unique_ptr d_before_preconditioner; - std::unique_ptr d_y; - std::unique_ptr d_b; - std::unique_ptr d_arnoldi_norm; - Range3dHelper d_range_helper; - std::unique_ptr d_next_krylov_basis; - std::unique_ptr d_hessenberg; - std::unique_ptr d_hessenberg_iter; - std::unique_ptr d_buffer_iter; - std::unique_ptr d_residual; - std::unique_ptr d_residual_norm; - std::unique_ptr d_residual_norm_collection; - std::unique_ptr d_givens_sin; - std::unique_ptr d_givens_cos; - std::unique_ptr> d_stop_status; - std::unique_ptr> d_reorth_status; - std::unique_ptr> d_final_iter_nums; - std::unique_ptr> d_num_reorth; -}; - - -TEST_F(CbGmres, CudaCbGmresInitialize1IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::cb_gmres::initialize_1( - ref, b.get(), residual.get(), givens_sin.get(), givens_cos.get(), - stop_status.get(), default_krylov_dim_mixed); - gko::kernels::cuda::cb_gmres::initialize_1( - cuda, d_b.get(), d_residual.get(), d_givens_sin.get(), - d_givens_cos.get(), d_stop_status.get(), default_krylov_dim_mixed); - - GKO_ASSERT_MTX_NEAR(d_residual, residual, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_stop_status, *stop_status); -} - - -TEST_F(CbGmres, CudaCbGmresInitialize2IsEquivalentToRef) -{ - initialize_data(); - gko::array tmp{ref}; - gko::array dtmp{cuda}; - - gko::kernels::reference::cb_gmres::initialize_2( - ref, residual.get(), residual_norm.get(), - residual_norm_collection.get(), arnoldi_norm.get(), - range_helper.get_range(), next_krylov_basis.get(), - final_iter_nums.get(), tmp, default_krylov_dim_mixed); - gko::kernels::cuda::cb_gmres::initialize_2( - cuda, d_residual.get(), d_residual_norm.get(), - d_residual_norm_collection.get(), d_arnoldi_norm.get(), - d_range_helper.get_range(), d_next_krylov_basis.get(), - d_final_iter_nums.get(), dtmp, default_krylov_dim_mixed); - - GKO_ASSERT_MTX_NEAR(d_arnoldi_norm, arnoldi_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - assert_krylov_bases_near(); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(CbGmres, CudaCbGmresStep1IsEquivalentToRef) -{ - initialize_data(); - int iter = 5; - - gko::kernels::reference::cb_gmres::step_1( - ref, next_krylov_basis.get(), givens_sin.get(), givens_cos.get(), - residual_norm.get(), residual_norm_collection.get(), - range_helper.get_range(), hessenberg_iter.get(), buffer_iter.get(), - arnoldi_norm.get(), iter, final_iter_nums.get(), stop_status.get(), - reorth_status.get(), num_reorth.get()); - gko::kernels::cuda::cb_gmres::step_1( - cuda, d_next_krylov_basis.get(), d_givens_sin.get(), d_givens_cos.get(), - d_residual_norm.get(), d_residual_norm_collection.get(), - d_range_helper.get_range(), d_hessenberg_iter.get(), - d_buffer_iter.get(), d_arnoldi_norm.get(), iter, - d_final_iter_nums.get(), d_stop_status.get(), d_reorth_status.get(), - d_num_reorth.get()); - - GKO_ASSERT_MTX_NEAR(d_arnoldi_norm, arnoldi_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_next_krylov_basis, next_krylov_basis, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_hessenberg_iter, hessenberg_iter, 1e-14); - assert_krylov_bases_near(); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(CbGmres, CudaCbGmresStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::cb_gmres::step_2( - ref, residual_norm_collection.get(), - range_helper.get_range().get_accessor().to_const(), hessenberg.get(), - y.get(), before_preconditioner.get(), final_iter_nums.get()); - gko::kernels::cuda::cb_gmres::step_2( - cuda, d_residual_norm_collection.get(), - d_range_helper.get_range().get_accessor().to_const(), - d_hessenberg.get(), d_y.get(), d_before_preconditioner.get(), - d_final_iter_nums.get()); - - GKO_ASSERT_MTX_NEAR(d_y, y, 1e-14); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-14); -} - - -} // namespace diff --git a/cuda/test/solver/gmres_kernels.cpp b/cuda/test/solver/gmres_kernels.cpp deleted file mode 100644 index 4eac27b7912..00000000000 --- a/cuda/test/solver/gmres_kernels.cpp +++ /dev/null @@ -1,292 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include - - -#include "core/solver/gmres_kernels.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -class Gmres : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Mtx = gko::matrix::Dense; - using norm_type = gko::remove_complex; - using NormVector = gko::matrix::Dense; - template - using Dense = typename gko::matrix::Dense; - - Gmres() : rand_engine(30) {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - template - std::unique_ptr> gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix>( - num_rows, num_cols, - std::uniform_int_distribution(num_cols, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - void initialize_data(int nrhs = 43) - { -#ifdef GINKGO_FAST_TESTS - int m = 123; -#else - int m = 597; -#endif - x = gen_mtx(m, nrhs); - y = gen_mtx(gko::solver::default_krylov_dim, nrhs); - before_preconditioner = Mtx::create_with_config_of(x.get()); - b = gen_mtx(m, nrhs); - krylov_bases = gen_mtx(m * (gko::solver::default_krylov_dim + 1), nrhs); - hessenberg = gen_mtx(gko::solver::default_krylov_dim + 1, - gko::solver::default_krylov_dim * nrhs); - hessenberg_iter = gen_mtx(gko::solver::default_krylov_dim + 1, nrhs); - residual = gen_mtx(m, nrhs); - residual_norm = gen_mtx(1, nrhs); - residual_norm_collection = - gen_mtx(gko::solver::default_krylov_dim + 1, nrhs); - givens_sin = gen_mtx(gko::solver::default_krylov_dim, nrhs); - givens_cos = gen_mtx(gko::solver::default_krylov_dim, nrhs); - stop_status = - std::make_unique>(ref, nrhs); - for (size_t i = 0; i < stop_status->get_num_elems(); ++i) { - stop_status->get_data()[i].reset(); - } - final_iter_nums = - std::make_unique>(ref, nrhs); - for (size_t i = 0; i < final_iter_nums->get_num_elems(); ++i) { - final_iter_nums->get_data()[i] = 5; - } - - d_x = gko::clone(cuda, x); - d_before_preconditioner = Mtx::create_with_config_of(d_x.get()); - d_y = gko::clone(cuda, y); - d_b = gko::clone(cuda, b); - d_krylov_bases = gko::clone(cuda, krylov_bases); - d_hessenberg = gko::clone(cuda, hessenberg); - d_hessenberg_iter = gko::clone(cuda, hessenberg_iter); - d_residual = gko::clone(cuda, residual); - d_residual_norm = gko::clone(cuda, residual_norm); - d_residual_norm_collection = gko::clone(cuda, residual_norm_collection); - d_givens_sin = gko::clone(cuda, givens_sin); - d_givens_cos = gko::clone(cuda, givens_cos); - d_stop_status = std::make_unique>( - cuda, *stop_status); - d_final_iter_nums = std::make_unique>( - cuda, *final_iter_nums); - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::default_random_engine rand_engine; - - std::unique_ptr before_preconditioner; - std::unique_ptr x; - std::unique_ptr y; - std::unique_ptr b; - std::unique_ptr krylov_bases; - std::unique_ptr hessenberg; - std::unique_ptr hessenberg_iter; - std::unique_ptr residual; - std::unique_ptr residual_norm; - std::unique_ptr residual_norm_collection; - std::unique_ptr givens_sin; - std::unique_ptr givens_cos; - std::unique_ptr> stop_status; - std::unique_ptr> final_iter_nums; - - std::unique_ptr d_x; - std::unique_ptr d_before_preconditioner; - std::unique_ptr d_y; - std::unique_ptr d_b; - std::unique_ptr d_krylov_bases; - std::unique_ptr d_hessenberg; - std::unique_ptr d_hessenberg_iter; - std::unique_ptr d_residual; - std::unique_ptr d_residual_norm; - std::unique_ptr d_residual_norm_collection; - std::unique_ptr d_givens_sin; - std::unique_ptr d_givens_cos; - std::unique_ptr> d_stop_status; - std::unique_ptr> d_final_iter_nums; -}; - - -TEST_F(Gmres, CudaGmresInitialize1IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::gmres::initialize_1( - ref, b.get(), residual.get(), givens_sin.get(), givens_cos.get(), - stop_status.get(), gko::solver::default_krylov_dim); - gko::kernels::cuda::gmres::initialize_1( - cuda, d_b.get(), d_residual.get(), d_givens_sin.get(), - d_givens_cos.get(), d_stop_status.get(), - gko::solver::default_krylov_dim); - - GKO_ASSERT_MTX_NEAR(d_residual, residual, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_stop_status, *stop_status); -} - - -TEST_F(Gmres, CudaGmresInitialize2IsEquivalentToRef) -{ - initialize_data(); - gko::array tmp{ref}; - gko::array dtmp{cuda}; - - gko::kernels::reference::gmres::initialize_2( - ref, residual.get(), residual_norm.get(), - residual_norm_collection.get(), krylov_bases.get(), - final_iter_nums.get(), tmp, gko::solver::default_krylov_dim); - gko::kernels::cuda::gmres::initialize_2( - cuda, d_residual.get(), d_residual_norm.get(), - d_residual_norm_collection.get(), d_krylov_bases.get(), - d_final_iter_nums.get(), dtmp, gko::solver::default_krylov_dim); - - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_krylov_bases, krylov_bases, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(Gmres, CudaGmresStep1IsEquivalentToRef) -{ - initialize_data(); - int iter = 5; - - gko::kernels::reference::gmres::step_1( - ref, x->get_size()[0], givens_sin.get(), givens_cos.get(), - residual_norm.get(), residual_norm_collection.get(), krylov_bases.get(), - hessenberg_iter.get(), iter, final_iter_nums.get(), stop_status.get()); - gko::kernels::cuda::gmres::step_1( - cuda, d_x->get_size()[0], d_givens_sin.get(), d_givens_cos.get(), - d_residual_norm.get(), d_residual_norm_collection.get(), - d_krylov_bases.get(), d_hessenberg_iter.get(), iter, - d_final_iter_nums.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_hessenberg_iter, hessenberg_iter, 1e-14); - GKO_ASSERT_MTX_NEAR(d_krylov_bases, krylov_bases, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(Gmres, CudaGmresStep1OnSingleRHSIsEquivalentToRef) -{ - initialize_data(1); - int iter = 5; - - gko::kernels::reference::gmres::step_1( - ref, x->get_size()[0], givens_sin.get(), givens_cos.get(), - residual_norm.get(), residual_norm_collection.get(), krylov_bases.get(), - hessenberg_iter.get(), iter, final_iter_nums.get(), stop_status.get()); - gko::kernels::cuda::gmres::step_1( - cuda, d_x->get_size()[0], d_givens_sin.get(), d_givens_cos.get(), - d_residual_norm.get(), d_residual_norm_collection.get(), - d_krylov_bases.get(), d_hessenberg_iter.get(), iter, - d_final_iter_nums.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_hessenberg_iter, hessenberg_iter, 1e-14); - GKO_ASSERT_MTX_NEAR(d_krylov_bases, krylov_bases, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(Gmres, CudaGmresStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::gmres::step_2(ref, residual_norm_collection.get(), - krylov_bases.get(), hessenberg.get(), - y.get(), before_preconditioner.get(), - final_iter_nums.get()); - gko::kernels::cuda::gmres::step_2(cuda, d_residual_norm_collection.get(), - d_krylov_bases.get(), d_hessenberg.get(), - d_y.get(), d_before_preconditioner.get(), - d_final_iter_nums.get()); - - GKO_ASSERT_MTX_NEAR(d_y, y, 1e-14); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-14); -} - - -} // namespace diff --git a/cuda/test/solver/idr_kernels.cpp b/cuda/test/solver/idr_kernels.cpp deleted file mode 100644 index fe90843c6d5..00000000000 --- a/cuda/test/solver/idr_kernels.cpp +++ /dev/null @@ -1,357 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include - - -#include "core/solver/idr_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Idr : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using Solver = gko::solver::Idr<>; - - Idr() : rand_engine(30) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - - cuda_idr_factory = - Solver::build() - .with_deterministic(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(cuda)) - .on(cuda); - - ref_idr_factory = - Solver::build() - .with_deterministic(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(ref)) - .on(ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); - } - - void initialize_data(int size = 597, int input_nrhs = 17) - { - nrhs = input_nrhs; - int s = 4; - mtx = gen_mtx(size, size); - x = gen_mtx(size, nrhs); - b = gen_mtx(size, nrhs); - r = gen_mtx(size, nrhs); - m = gen_mtx(s, nrhs * s); - f = gen_mtx(s, nrhs); - g = gen_mtx(size, nrhs * s); - u = gen_mtx(size, nrhs * s); - c = gen_mtx(s, nrhs); - v = gen_mtx(size, nrhs); - p = gen_mtx(s, size); - alpha = gen_mtx(1, nrhs); - omega = gen_mtx(1, nrhs); - tht = gen_mtx(1, nrhs); - residual_norm = gen_mtx(1, nrhs); - stop_status = - std::make_unique>(ref, nrhs); - for (size_t i = 0; i < nrhs; ++i) { - stop_status->get_data()[i].reset(); - } - - d_mtx = gko::clone(cuda, mtx); - d_x = gko::clone(cuda, x); - d_b = gko::clone(cuda, b); - d_r = gko::clone(cuda, r); - d_m = gko::clone(cuda, m); - d_f = gko::clone(cuda, f); - d_g = gko::clone(cuda, g); - d_u = gko::clone(cuda, u); - d_c = gko::clone(cuda, c); - d_v = gko::clone(cuda, v); - d_p = gko::clone(cuda, p); - d_alpha = gko::clone(cuda, alpha); - d_omega = gko::clone(cuda, omega); - d_tht = gko::clone(cuda, tht); - d_residual_norm = gko::clone(cuda, residual_norm); - d_stop_status = std::make_unique>( - cuda, *stop_status); - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::default_random_engine rand_engine; - - std::shared_ptr mtx; - std::shared_ptr d_mtx; - std::unique_ptr cuda_idr_factory; - std::unique_ptr ref_idr_factory; - - gko::size_type nrhs; - - std::unique_ptr x; - std::unique_ptr b; - std::unique_ptr r; - std::unique_ptr m; - std::unique_ptr f; - std::unique_ptr g; - std::unique_ptr u; - std::unique_ptr c; - std::unique_ptr v; - std::unique_ptr p; - std::unique_ptr alpha; - std::unique_ptr omega; - std::unique_ptr tht; - std::unique_ptr residual_norm; - std::unique_ptr> stop_status; - - std::unique_ptr d_x; - std::unique_ptr d_b; - std::unique_ptr d_r; - std::unique_ptr d_m; - std::unique_ptr d_f; - std::unique_ptr d_g; - std::unique_ptr d_u; - std::unique_ptr d_c; - std::unique_ptr d_v; - std::unique_ptr d_p; - std::unique_ptr d_alpha; - std::unique_ptr d_omega; - std::unique_ptr d_tht; - std::unique_ptr d_residual_norm; - std::unique_ptr> d_stop_status; -}; - - -TEST_F(Idr, IdrInitializeIsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::idr::initialize(ref, nrhs, m.get(), p.get(), true, - stop_status.get()); - gko::kernels::cuda::idr::initialize(cuda, nrhs, d_m.get(), d_p.get(), true, - d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(m, d_m, 1e-14); - GKO_ASSERT_MTX_NEAR(p, d_p, 1e-14); -} - - -TEST_F(Idr, IdrStep1IsEquivalentToRef) -{ - initialize_data(); - - gko::size_type k = 2; - gko::kernels::reference::idr::step_1(ref, nrhs, k, m.get(), f.get(), - r.get(), g.get(), c.get(), v.get(), - stop_status.get()); - gko::kernels::cuda::idr::step_1(cuda, nrhs, k, d_m.get(), d_f.get(), - d_r.get(), d_g.get(), d_c.get(), d_v.get(), - d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(c, d_c, 1e-14); - GKO_ASSERT_MTX_NEAR(v, d_v, 1e-14); -} - - -TEST_F(Idr, IdrStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::size_type k = 2; - gko::kernels::reference::idr::step_2(ref, nrhs, k, omega.get(), v.get(), - c.get(), u.get(), stop_status.get()); - gko::kernels::cuda::idr::step_2(cuda, nrhs, k, d_omega.get(), d_v.get(), - d_c.get(), d_u.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(u, d_u, 1e-14); -} - - -TEST_F(Idr, IdrStep3IsEquivalentToRef) -{ - initialize_data(); - - gko::size_type k = 2; - gko::kernels::reference::idr::step_3( - ref, nrhs, k, p.get(), g.get(), v.get(), u.get(), m.get(), f.get(), - alpha.get(), r.get(), x.get(), stop_status.get()); - gko::kernels::cuda::idr::step_3( - cuda, nrhs, k, d_p.get(), d_g.get(), d_v.get(), d_u.get(), d_m.get(), - d_f.get(), d_alpha.get(), d_r.get(), d_x.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(g, d_g, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(v, d_v, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(u, d_u, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(m, d_m, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(f, d_f, 150 * 1e-14); - GKO_ASSERT_MTX_NEAR(r, d_r, 50 * 1e-14); - GKO_ASSERT_MTX_NEAR(x, d_x, 50 * 1e-14); -} - - -TEST_F(Idr, IdrComputeOmegaIsEquivalentToRef) -{ - initialize_data(); - - double kappa = 0.7; - gko::kernels::reference::idr::compute_omega(ref, nrhs, kappa, tht.get(), - residual_norm.get(), - omega.get(), stop_status.get()); - gko::kernels::cuda::idr::compute_omega(cuda, nrhs, kappa, d_tht.get(), - d_residual_norm.get(), d_omega.get(), - d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(omega, d_omega, 1e-14); -} - - -TEST_F(Idr, IdrIterationOneRHSIsEquivalentToRef) -{ - initialize_data(123, 1); - auto ref_solver = ref_idr_factory->generate(mtx); - auto cuda_solver = cuda_idr_factory->generate(d_mtx); - - ref_solver->apply(b.get(), x.get()); - cuda_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-13); -} - - -TEST_F(Idr, IdrIterationWithComplexSubspaceOneRHSIsEquivalentToRef) -{ - initialize_data(123, 1); - cuda_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(cuda)) - .on(cuda); - ref_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(ref)) - .on(ref); - auto ref_solver = ref_idr_factory->generate(mtx); - auto cuda_solver = cuda_idr_factory->generate(d_mtx); - - ref_solver->apply(b.get(), x.get()); - cuda_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-13); -} - - -TEST_F(Idr, IdrIterationMultipleRHSIsEquivalentToRef) -{ - initialize_data(123, 16); - auto cuda_solver = cuda_idr_factory->generate(d_mtx); - auto ref_solver = ref_idr_factory->generate(mtx); - - ref_solver->apply(b.get(), x.get()); - cuda_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-12); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Idr, IdrIterationWithComplexSubspaceMultipleRHSIsEquivalentToRef) -{ - initialize_data(123, 16); - cuda_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(cuda)) - .on(cuda); - ref_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(ref)) - .on(ref); - auto cuda_solver = cuda_idr_factory->generate(d_mtx); - auto ref_solver = ref_idr_factory->generate(mtx); - - ref_solver->apply(b.get(), x.get()); - cuda_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-13); -} - - -} // namespace diff --git a/cuda/test/solver/multigrid_kernels.cpp b/cuda/test/solver/multigrid_kernels.cpp deleted file mode 100644 index 080a48ba94f..00000000000 --- a/cuda/test/solver/multigrid_kernels.cpp +++ /dev/null @@ -1,256 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/solver/multigrid_kernels.hpp" -#include "cuda/test/utils.hpp" - - -namespace { - - -class Multigrid : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - Multigrid() : rand_engine(30) {} - - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void initialize_data() - { - int m = 597; - int n = 43; - v = gen_mtx(m, n); - d = gen_mtx(m, n); - g = gen_mtx(m, n); - e = gen_mtx(m, n); - alpha = gen_mtx(1, n); - rho = gen_mtx(1, n); - beta = gen_mtx(1, n); - gamma = gen_mtx(1, n); - zeta = gen_mtx(1, n); - old_norm = gen_mtx(1, n); - new_norm = Mtx::create(ref, gko::dim<2>{1, n}); - this->modify_norm(old_norm, new_norm); - this->modify_scalar(alpha, rho, beta, gamma, zeta); - - d_v = Mtx::create(cuda); - d_v->copy_from(v.get()); - d_d = Mtx::create(cuda); - d_d->copy_from(d.get()); - d_g = Mtx::create(cuda); - d_g->copy_from(g.get()); - d_e = Mtx::create(cuda); - d_e->copy_from(e.get()); - d_alpha = Mtx::create(cuda); - d_alpha->copy_from(alpha.get()); - d_rho = Mtx::create(cuda); - d_rho->copy_from(rho.get()); - d_beta = Mtx::create(cuda); - d_beta->copy_from(beta.get()); - d_gamma = Mtx::create(cuda); - d_gamma->copy_from(gamma.get()); - d_zeta = Mtx::create(cuda); - d_zeta->copy_from(zeta.get()); - d_old_norm = Mtx::create(cuda); - d_old_norm->copy_from(old_norm.get()); - d_new_norm = Mtx::create(cuda); - d_new_norm->copy_from(new_norm.get()); - } - - void modify_norm(std::unique_ptr& old_norm, - std::unique_ptr& new_norm) - { - double ratio = 0.7; - for (gko::size_type i = 0; i < old_norm->get_size()[1]; i++) { - old_norm->at(0, i) = gko::abs(old_norm->at(0, i)); - new_norm->at(0, i) = ratio * old_norm->at(0, i); - } - } - - void modify_scalar(std::unique_ptr& alpha, std::unique_ptr& rho, - std::unique_ptr& beta, std::unique_ptr& gamma, - std::unique_ptr& zeta) - { - // modify the first three element such that the isfinite condition can - // be reached, which are checked in the last three group in reference - // test. - // scalar_d = zeta/(beta - gamma * gamma / rho) - // scalar_e = one() - gamma / alpha * scalar_d - // temp = alpha/rho - - // scalar_d, scalar_e are not finite - alpha->at(0, 0) = 3.0; - rho->at(0, 0) = 2.0; - beta->at(0, 0) = 2.0; - gamma->at(0, 0) = 2.0; - zeta->at(0, 0) = -1.0; - - // temp, scalar_d, scalar_e are not finite - alpha->at(0, 1) = 0.0; - rho->at(0, 1) = 0.0; - beta->at(0, 1) = -1.0; - gamma->at(0, 1) = 0.0; - zeta->at(0, 1) = 3.0; - - // scalar_e is not finite - alpha->at(0, 2) = 0.0; - rho->at(0, 2) = 1.0; - beta->at(0, 2) = 2.0; - gamma->at(0, 2) = 1.0; - zeta->at(0, 2) = 2.0; - } - - std::shared_ptr ref; - std::shared_ptr cuda; - - std::default_random_engine rand_engine; - - std::unique_ptr v; - std::unique_ptr d; - std::unique_ptr g; - std::unique_ptr e; - std::unique_ptr alpha; - std::unique_ptr rho; - std::unique_ptr beta; - std::unique_ptr gamma; - std::unique_ptr zeta; - std::unique_ptr old_norm; - std::unique_ptr new_norm; - - std::unique_ptr d_v; - std::unique_ptr d_d; - std::unique_ptr d_g; - std::unique_ptr d_e; - std::unique_ptr d_alpha; - std::unique_ptr d_rho; - std::unique_ptr d_beta; - std::unique_ptr d_gamma; - std::unique_ptr d_zeta; - std::unique_ptr d_old_norm; - std::unique_ptr d_new_norm; -}; - - -TEST_F(Multigrid, CudaMultigridKCycleStep1IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::multigrid::kcycle_step_1( - ref, gko::lend(alpha), gko::lend(rho), gko::lend(v), gko::lend(g), - gko::lend(d), gko::lend(e)); - gko::kernels::cuda::multigrid::kcycle_step_1( - cuda, gko::lend(d_alpha), gko::lend(d_rho), gko::lend(d_v), - gko::lend(d_g), gko::lend(d_d), gko::lend(d_e)); - - GKO_ASSERT_MTX_NEAR(d_g, g, 1e-14); - GKO_ASSERT_MTX_NEAR(d_d, d, 1e-14); - GKO_ASSERT_MTX_NEAR(d_e, e, 1e-14); -} - - -TEST_F(Multigrid, CudaMultigridKCycleStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::multigrid::kcycle_step_2( - ref, gko::lend(alpha), gko::lend(rho), gko::lend(gamma), - gko::lend(beta), gko::lend(zeta), gko::lend(d), gko::lend(e)); - gko::kernels::cuda::multigrid::kcycle_step_2( - cuda, gko::lend(d_alpha), gko::lend(d_rho), gko::lend(d_gamma), - gko::lend(d_beta), gko::lend(d_zeta), gko::lend(d_d), gko::lend(d_e)); - - GKO_ASSERT_MTX_NEAR(d_e, e, 1e-14); -} - - -TEST_F(Multigrid, CudaMultigridKCycleCheckStopIsEquivalentToRef) -{ - initialize_data(); - bool is_stop_10; - bool d_is_stop_10; - bool is_stop_5; - bool d_is_stop_5; - - gko::kernels::reference::multigrid::kcycle_check_stop( - ref, gko::lend(old_norm), gko::lend(new_norm), 1.0, is_stop_10); - gko::kernels::cuda::multigrid::kcycle_check_stop( - cuda, gko::lend(d_old_norm), gko::lend(d_new_norm), 1.0, d_is_stop_10); - gko::kernels::reference::multigrid::kcycle_check_stop( - ref, gko::lend(old_norm), gko::lend(new_norm), 0.5, is_stop_5); - gko::kernels::cuda::multigrid::kcycle_check_stop( - cuda, gko::lend(d_old_norm), gko::lend(d_new_norm), 0.5, d_is_stop_5); - - GKO_ASSERT_EQ(d_is_stop_10, is_stop_10); - GKO_ASSERT_EQ(d_is_stop_10, true); - GKO_ASSERT_EQ(d_is_stop_5, is_stop_5); - GKO_ASSERT_EQ(d_is_stop_5, false); -} - - -} // namespace diff --git a/cuda/test/stop/CMakeLists.txt b/cuda/test/stop/CMakeLists.txt deleted file mode 100644 index 0ba0781e077..00000000000 --- a/cuda/test/stop/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -ginkgo_create_test(criterion_kernels) -ginkgo_create_test(residual_norm_kernels) diff --git a/cuda/test/stop/criterion_kernels.cpp b/cuda/test/stop/criterion_kernels.cpp deleted file mode 100644 index f78e27dc20b..00000000000 --- a/cuda/test/stop/criterion_kernels.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include "cuda/test/utils.hpp" - - -namespace { - - -constexpr gko::size_type test_iterations = 10; - - -class Criterion : public ::testing::Test { -protected: - Criterion() - { - ref_ = gko::ReferenceExecutor::create(); - cuda_ = gko::CudaExecutor::create(0, ref_); - // Actually use an iteration stopping criterion because Criterion is an - // abstract class - factory_ = gko::stop::Iteration::build() - .with_max_iters(test_iterations) - .on(cuda_); - } - - std::unique_ptr factory_; - std::shared_ptr ref_; - std::shared_ptr cuda_; -}; - - -TEST_F(Criterion, SetsOneStopStatus) -{ - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - auto criterion = factory_->generate(nullptr, nullptr, nullptr); - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - - stop_status.set_executor(cuda_); - criterion->update() - .num_iterations(test_iterations) - .check(RelativeStoppingId, true, &stop_status, &one_changed); - stop_status.set_executor(ref_); - - ASSERT_EQ(stop_status.get_data()[0].has_stopped(), true); -} - - -TEST_F(Criterion, SetsMultipleStopStatuses) -{ - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - auto criterion = factory_->generate(nullptr, nullptr, nullptr); - gko::array stop_status(ref_, 3); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.get_data()[2].reset(); - - stop_status.set_executor(cuda_); - criterion->update() - .num_iterations(test_iterations) - .check(RelativeStoppingId, true, &stop_status, &one_changed); - stop_status.set_executor(ref_); - - ASSERT_EQ(stop_status.get_data()[0].has_stopped(), true); - ASSERT_EQ(stop_status.get_data()[1].has_stopped(), true); - ASSERT_EQ(stop_status.get_data()[2].has_stopped(), true); -} - - -} // namespace diff --git a/cuda/test/stop/residual_norm_kernels.cpp b/cuda/test/stop/residual_norm_kernels.cpp deleted file mode 100644 index 73a84ea121f..00000000000 --- a/cuda/test/stop/residual_norm_kernels.cpp +++ /dev/null @@ -1,781 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include "cuda/test/utils.hpp" - - -namespace { - - -constexpr double tol = 1.0e-14; - - -class ResidualNorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using NormVector = gko::matrix::Dense>; - - ResidualNorm() - { - ref_ = gko::ReferenceExecutor::create(); - cuda_ = gko::CudaExecutor::create(0, ref_); - factory_ = - gko::stop::ResidualNorm<>::build().with_reduction_factor(tol).on( - cuda_); - rel_factory_ = gko::stop::ResidualNorm<>::build() - .with_reduction_factor(tol) - .with_baseline(gko::stop::mode::initial_resnorm) - .on(cuda_); - abs_factory_ = gko::stop::ResidualNorm<>::build() - .with_reduction_factor(tol) - .with_baseline(gko::stop::mode::absolute) - .on(cuda_); - } - - std::unique_ptr::Factory> factory_; - std::unique_ptr::Factory> rel_factory_; - std::unique_ptr::Factory> abs_factory_; - std::shared_ptr cuda_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNorm, WaitsTillResidualGoalForRhsResNorm) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - auto rhs_norm = gko::initialize({0.0}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalMultipleRHSForRhsResNorm) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - auto rhs_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * rhs_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * rhs_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalForRelResNorm) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = - rel_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalMultipleRHSForRelResNorm) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = - rel_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * res_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * res_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalForAbsResNorm) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = - abs_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalMultipleRHSForAbsResNorm) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = - abs_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ResidualNormWithInitialResnorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using NormVector = gko::matrix::Dense>; - - ResidualNormWithInitialResnorm() - { - ref_ = gko::ReferenceExecutor::create(); - cuda_ = gko::CudaExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm<>::build() - .with_baseline(gko::stop::mode::initial_resnorm) - .with_reduction_factor(tol) - .on(cuda_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr cuda_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNormWithInitialResnorm, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNormWithInitialResnorm, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * res_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * res_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ResidualNormWithRhsNorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using NormVector = gko::matrix::Dense>; - - ResidualNormWithRhsNorm() - { - ref_ = gko::ReferenceExecutor::create(); - cuda_ = gko::CudaExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm<>::build() - .with_baseline(gko::stop::mode::rhs_norm) - .with_reduction_factor(tol) - .on(cuda_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr cuda_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNormWithRhsNorm, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - auto rhs_norm = gko::initialize({0.0}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNormWithRhsNorm, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - auto rhs_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * rhs_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * rhs_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ImplicitResidualNorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using NormVector = gko::matrix::Dense>; - - ImplicitResidualNorm() - { - ref_ = gko::ReferenceExecutor::create(); - cuda_ = gko::CudaExecutor::create(0, ref_); - factory_ = gko::stop::ImplicitResidualNorm<>::build() - .with_reduction_factor(tol) - .on(cuda_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr cuda_; - std::shared_ptr ref_; -}; - - -TEST_F(ImplicitResidualNorm, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - auto rhs_norm = gko::initialize({0.0}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = std::pow(tol * 1.1 * rhs_norm->at(0), 2); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_FALSE(one_changed); - - res->at(0) = std::pow(tol * 0.9 * rhs_norm->at(0), 2); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ImplicitResidualNorm, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - auto rhs_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = std::pow(tol * 0.9 * rhs_norm->at(0, 0), 2); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = std::pow(tol * 0.9 * rhs_norm->at(0, 1), 2); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ResidualNormWithAbsolute : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using NormVector = gko::matrix::Dense>; - - ResidualNormWithAbsolute() - { - ref_ = gko::ReferenceExecutor::create(); - cuda_ = gko::CudaExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm<>::build() - .with_baseline(gko::stop::mode::absolute) - .with_reduction_factor(tol) - .on(cuda_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr cuda_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNormWithAbsolute, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNormWithAbsolute, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto d_res = gko::clone(cuda_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(cuda_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(cuda_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(cuda_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -} // namespace diff --git a/dev_tools/scripts/config b/dev_tools/scripts/config index 24e33c15fd5..0ebaa462bc4 100644 --- a/dev_tools/scripts/config +++ b/dev_tools/scripts/config @@ -39,6 +39,11 @@ - PathPrefix: "ginkgo/core" - PathIgnore: "0" - RemoveTest: "true" +- "^test/matrix/csr_kernels2.cpp" + - CoreSuffix: "_kernels2" + - PathPrefix: "ginkgo/core" + - PathIgnore: "0" + - RemoveTest: "true" - "elimination_forest\.cpp" - FixInclude: "core/factorization/elimination_forest.hpp" - "common/unified/.*.cpp" diff --git a/dpcpp/matrix/dense_kernels.dp.cpp b/dpcpp/matrix/dense_kernels.dp.cpp index d9f6a46d1d0..8eec93219c6 100644 --- a/dpcpp/matrix/dense_kernels.dp.cpp +++ b/dpcpp/matrix/dense_kernels.dp.cpp @@ -126,7 +126,6 @@ void transpose(sycl::queue* queue, const matrix::Dense* orig, { auto size = orig->get_size(); constexpr auto sg_size = DeviceConfig::subgroup_size; - std::cout << sg_size << std::endl; dim3 grid(ceildiv(size[1], sg_size), ceildiv(size[0], sg_size)); dim3 block(sg_size, sg_size); diff --git a/dpcpp/test/CMakeLists.txt b/dpcpp/test/CMakeLists.txt index 2597520bd75..00be1e71e35 100644 --- a/dpcpp/test/CMakeLists.txt +++ b/dpcpp/test/CMakeLists.txt @@ -3,8 +3,4 @@ set(GINKGO_COMPILING_DPCPP_TEST ON) add_subdirectory(base) add_subdirectory(components) -add_subdirectory(factorization) add_subdirectory(matrix) -add_subdirectory(preconditioner) -add_subdirectory(solver) -add_subdirectory(stop) diff --git a/dpcpp/test/components/CMakeLists.txt b/dpcpp/test/components/CMakeLists.txt index 1d8614c7ca9..9b44f670fcc 100644 --- a/dpcpp/test/components/CMakeLists.txt +++ b/dpcpp/test/components/CMakeLists.txt @@ -1,2 +1 @@ ginkgo_create_dpcpp_test(cooperative_groups) -ginkgo_create_test(prefix_sum_kernels) diff --git a/dpcpp/test/components/prefix_sum_kernels.cpp b/dpcpp/test/components/prefix_sum_kernels.cpp deleted file mode 100644 index df4c125cd7c..00000000000 --- a/dpcpp/test/components/prefix_sum_kernels.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/components/prefix_sum_kernels.hpp" - - -#include -#include -#include - - -#include - - -#include - - -#include "core/test/utils.hpp" - - -namespace { - - -class PrefixSum : public ::testing::Test { -protected: - using index_type = gko::int32; - PrefixSum() - : ref(gko::ReferenceExecutor::create()), - exec(gko::DpcppExecutor::create(0, ref)), - rand(293), - total_size(42793), - vals(ref, total_size), - dvals(exec) - { - std::uniform_int_distribution dist(0, 1000); - for (gko::size_type i = 0; i < total_size; ++i) { - vals.get_data()[i] = dist(rand); - } - dvals = vals; - } - - void test(gko::size_type size) - { - gko::kernels::reference::components::prefix_sum(ref, vals.get_data(), - size); - gko::kernels::dpcpp::components::prefix_sum(exec, dvals.get_data(), - size); - - GKO_ASSERT_ARRAY_EQ(vals, dvals); - } - - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand; - gko::size_type total_size; - gko::array vals; - gko::array dvals; -}; - - -TEST_F(PrefixSum, SmallEqualsReference) { test(100); } - - -TEST_F(PrefixSum, BigEqualsReference) { test(total_size); } - - -} // namespace diff --git a/dpcpp/test/factorization/CMakeLists.txt b/dpcpp/test/factorization/CMakeLists.txt deleted file mode 100644 index dc0845513a8..00000000000 --- a/dpcpp/test/factorization/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -ginkgo_create_test(par_ic_kernels) -ginkgo_create_test(par_ict_kernels) -ginkgo_create_test(par_ilu_kernels) -ginkgo_create_test(par_ilut_kernels) \ No newline at end of file diff --git a/dpcpp/test/factorization/par_ic_kernels.cpp b/dpcpp/test/factorization/par_ic_kernels.cpp deleted file mode 100644 index 2bc9c11a32d..00000000000 --- a/dpcpp/test/factorization/par_ic_kernels.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ic_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/matrix/csr_builder.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "core/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIc : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = gko::int32; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - ParIc() - : mtx_size(624, 624), - rand_engine(43456), - ref(gko::ReferenceExecutor::create()), - dpcpp(gko::DpcppExecutor::create(0, gko::ReferenceExecutor::create())) - { - mtx_l = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution(10, mtx_size[0]), - std::normal_distribution>(0, 10.0), - rand_engine, ref); - - dmtx_ani = Csr::create(dpcpp); - dmtx_l_ani = Csr::create(dpcpp); - dmtx_l_ani_init = Csr::create(dpcpp); - dmtx_l = gko::clone(dpcpp, mtx_l); - } - - void SetUp() - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - mtx_ani = gko::read(input_file, ref); - mtx_ani->sort_by_column_index(); - - { - mtx_l_ani = Csr::create(ref, mtx_ani->get_size()); - gko::matrix::CsrBuilder l_builder( - lend(mtx_l_ani)); - gko::kernels::reference::factorization::initialize_row_ptrs_l( - ref, lend(mtx_ani), mtx_l_ani->get_row_ptrs()); - auto l_nnz = - mtx_l_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - l_builder.get_col_idx_array().resize_and_reset(l_nnz); - l_builder.get_value_array().resize_and_reset(l_nnz); - gko::kernels::reference::factorization::initialize_l( - ref, lend(mtx_ani), lend(mtx_l_ani), false); - mtx_l_ani_init = gko::clone(ref, mtx_l_ani); - gko::kernels::reference::par_ic_factorization::init_factor( - ref, lend(mtx_l_ani_init)); - } - dmtx_ani->copy_from(lend(mtx_ani)); - dmtx_l_ani->copy_from(lend(mtx_l_ani)); - dmtx_l_ani_init->copy_from(lend(mtx_l_ani_init)); - } - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx_l; - std::unique_ptr mtx_ani; - std::unique_ptr mtx_l_ani; - std::unique_ptr mtx_l_ani_init; - - std::unique_ptr dmtx_l; - std::unique_ptr dmtx_ani; - std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx_l_ani_init; -}; - - -TEST_F(ParIc, KernelInitFactorIsEquivalentToRef) -{ - gko::kernels::reference::par_ic_factorization::init_factor(ref, - lend(mtx_l)); - gko::kernels::dpcpp::par_ic_factorization::init_factor(dpcpp, lend(dmtx_l)); - - GKO_ASSERT_MTX_NEAR(mtx_l, dmtx_l, r::value); -} - - -TEST_F(ParIc, KernelComputeFactorIsEquivalentToRef) -{ - auto square_size = mtx_ani->get_size(); - auto mtx_l_coo = Coo::create(ref, square_size); - mtx_l_ani->convert_to(lend(mtx_l_coo)); - auto dmtx_l_coo = gko::clone(dpcpp, mtx_l_coo); - - gko::kernels::reference::par_ic_factorization::compute_factor( - ref, 1, lend(mtx_l_coo), lend(mtx_l_ani_init)); - gko::kernels::dpcpp::par_ic_factorization::compute_factor( - dpcpp, 100, lend(dmtx_l_coo), lend(dmtx_l_ani_init)); - - GKO_ASSERT_MTX_NEAR(mtx_l_ani_init, dmtx_l_ani_init, 1e-4); -} - - -} // namespace diff --git a/dpcpp/test/factorization/par_ict_kernels.cpp b/dpcpp/test/factorization/par_ict_kernels.cpp deleted file mode 100644 index a7e0d702a39..00000000000 --- a/dpcpp/test/factorization/par_ict_kernels.cpp +++ /dev/null @@ -1,178 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ict_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/matrix/csr_builder.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "core/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIct : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = gko::int32; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - ParIct() - : mtx_size(436, 436), - rand_engine(45856), - ref(gko::ReferenceExecutor::create()), - dpcpp(gko::DpcppExecutor::create(0, ref)) - { - mtx = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[1], - std::uniform_int_distribution<>(10, mtx_size[1]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - - dmtx_ani = Csr::create(dpcpp); - dmtx_l_ani = Csr::create(dpcpp); - dmtx = gko::clone(dpcpp, mtx); - dmtx_l = gko::clone(dpcpp, mtx_l); - } - - void SetUp() - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - mtx_ani = gko::read(input_file, ref); - mtx_ani->sort_by_column_index(); - - { - mtx_l_ani = Csr::create(ref, mtx_ani->get_size()); - gko::matrix::CsrBuilder l_builder( - lend(mtx_l_ani)); - gko::kernels::reference::factorization::initialize_row_ptrs_l( - ref, lend(mtx_ani), mtx_l_ani->get_row_ptrs()); - auto l_nnz = - mtx_l_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - l_builder.get_col_idx_array().resize_and_reset(l_nnz); - l_builder.get_value_array().resize_and_reset(l_nnz); - gko::kernels::reference::factorization::initialize_l( - ref, lend(mtx_ani), lend(mtx_l_ani), true); - } - dmtx_ani->copy_from(lend(mtx_ani)); - dmtx_l_ani->copy_from(lend(mtx_l_ani)); - } - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr mtx_ani; - std::unique_ptr mtx_l_ani; - std::unique_ptr mtx_l; - - std::unique_ptr dmtx; - std::unique_ptr dmtx_ani; - std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx_l; -}; - - -TEST_F(ParIct, KernelAddCandidatesIsEquivalentToRef) -{ - auto mtx_llh = Csr::create(ref, mtx_size); - mtx_l->apply(lend(mtx_l->conj_transpose()), lend(mtx_llh)); - auto dmtx_llh = Csr::create(dpcpp, mtx_size); - dmtx_llh->copy_from(lend(mtx_llh)); - auto res_mtx_l = Csr::create(ref, mtx_size); - auto dres_mtx_l = Csr::create(dpcpp, mtx_size); - - gko::kernels::reference::par_ict_factorization::add_candidates( - ref, lend(mtx_llh), lend(mtx), lend(mtx_l), lend(res_mtx_l)); - gko::kernels::dpcpp::par_ict_factorization::add_candidates( - dpcpp, lend(dmtx_llh), lend(dmtx), lend(dmtx_l), lend(dres_mtx_l)); - - GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_l, dres_mtx_l); - GKO_ASSERT_MTX_NEAR(res_mtx_l, dres_mtx_l, r::value); -} - - -TEST_F(ParIct, KernelComputeFactorIsEquivalentToRef) -{ - auto square_size = mtx_ani->get_size(); - auto mtx_l_coo = Coo::create(ref, square_size); - mtx_l_ani->convert_to(lend(mtx_l_coo)); - auto dmtx_l_coo = Coo::create(dpcpp, square_size); - dmtx_l_coo->copy_from(lend(mtx_l_coo)); - - gko::kernels::reference::par_ict_factorization::compute_factor( - ref, lend(mtx_ani), lend(mtx_l_ani), lend(mtx_l_coo)); - for (int i = 0; i < 20; ++i) { - gko::kernels::dpcpp::par_ict_factorization::compute_factor( - dpcpp, lend(dmtx_ani), lend(dmtx_l_ani), lend(dmtx_l_coo)); - } - - GKO_ASSERT_MTX_NEAR(mtx_l_ani, dmtx_l_ani, 1e-2); -} - - -} // namespace diff --git a/dpcpp/test/factorization/par_ilu_kernels.cpp b/dpcpp/test/factorization/par_ilu_kernels.cpp deleted file mode 100644 index d33d6f4c625..00000000000 --- a/dpcpp/test/factorization/par_ilu_kernels.cpp +++ /dev/null @@ -1,349 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ilu_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIlu : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = gko::int32; - using Dense = gko::matrix::Dense; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - std::ranlux48 rand_engine; - std::shared_ptr ref; - std::shared_ptr dpcpp; - std::shared_ptr csr_ref; - std::shared_ptr csr_dpcpp; - - ParIlu() - : rand_engine(18), - ref(gko::ReferenceExecutor::create()), - dpcpp(gko::DpcppExecutor::create(0, ref)), - csr_ref(nullptr), - csr_dpcpp(nullptr) - {} - - void SetUp() override - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - auto csr_ref_temp = gko::read(input_file, ref); - auto csr_dpcpp_temp = gko::clone(dpcpp, csr_ref_temp); - // Make sure there are diagonal elements present - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(csr_ref_temp), false); - gko::kernels::dpcpp::factorization::add_diagonal_elements( - dpcpp, gko::lend(csr_dpcpp_temp), false); - csr_ref = gko::give(csr_ref_temp); - csr_dpcpp = gko::give(csr_dpcpp_temp); - } - - template - std::unique_ptr gen_mtx(index_type num_rows, index_type num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(0, num_cols - 1), - std::normal_distribution(0.0, 1.0), rand_engine, ref); - } - - std::unique_ptr gen_unsorted_mtx(index_type num_rows, - index_type num_cols) - { - using std::swap; - auto mtx = gen_mtx(num_rows, num_cols); - auto values = mtx->get_values(); - auto col_idxs = mtx->get_col_idxs(); - const auto row_ptrs = mtx->get_const_row_ptrs(); - for (int row = 0; row < num_rows; ++row) { - const auto row_start = row_ptrs[row]; - const auto row_end = row_ptrs[row + 1]; - const int num_row_elements = row_end - row_start; - auto idx_dist = std::uniform_int_distribution( - row_start, row_end - 1); - for (int i = 0; i < num_row_elements / 2; ++i) { - auto idx1 = idx_dist(rand_engine); - auto idx2 = idx_dist(rand_engine); - if (idx1 != idx2) { - swap(values[idx1], values[idx2]); - swap(col_idxs[idx1], col_idxs[idx2]); - } - } - } - return mtx; - } - - void initialize_row_ptrs(index_type* l_row_ptrs_ref, - index_type* u_row_ptrs_ref, - index_type* l_row_ptrs_dpcpp, - index_type* u_row_ptrs_dpcpp) - { - gko::kernels::reference::factorization::initialize_row_ptrs_l_u( - ref, gko::lend(csr_ref), l_row_ptrs_ref, u_row_ptrs_ref); - gko::kernels::dpcpp::factorization::initialize_row_ptrs_l_u( - dpcpp, gko::lend(csr_dpcpp), l_row_ptrs_dpcpp, u_row_ptrs_dpcpp); - } - - void initialize_lu(std::unique_ptr* l_ref, std::unique_ptr* u_ref, - std::unique_ptr* l_dpcpp, - std::unique_ptr* u_dpcpp) - { - auto num_row_ptrs = csr_ref->get_size()[0] + 1; - gko::array l_row_ptrs_ref{ref, num_row_ptrs}; - gko::array u_row_ptrs_ref{ref, num_row_ptrs}; - gko::array l_row_ptrs_dpcpp{dpcpp, num_row_ptrs}; - gko::array u_row_ptrs_dpcpp{dpcpp, num_row_ptrs}; - - initialize_row_ptrs( - l_row_ptrs_ref.get_data(), u_row_ptrs_ref.get_data(), - l_row_ptrs_dpcpp.get_data(), u_row_ptrs_dpcpp.get_data()); - // Since `initialize_row_ptrs` was already tested, it is expected that - // `*_ref` and `*_dpcpp` contain identical values - auto l_nnz = l_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; - auto u_nnz = u_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; - - *l_ref = Csr::create(ref, csr_ref->get_size(), l_nnz); - *u_ref = Csr::create(ref, csr_ref->get_size(), u_nnz); - *l_dpcpp = Csr::create(dpcpp, csr_dpcpp->get_size(), l_nnz); - *u_dpcpp = Csr::create(dpcpp, csr_dpcpp->get_size(), u_nnz); - // Copy the already initialized `row_ptrs` to the new matrices - ref->copy(num_row_ptrs, l_row_ptrs_ref.get_data(), - (*l_ref)->get_row_ptrs()); - ref->copy(num_row_ptrs, u_row_ptrs_ref.get_data(), - (*u_ref)->get_row_ptrs()); - dpcpp->copy(num_row_ptrs, l_row_ptrs_dpcpp.get_data(), - (*l_dpcpp)->get_row_ptrs()); - dpcpp->copy(num_row_ptrs, u_row_ptrs_dpcpp.get_data(), - (*u_dpcpp)->get_row_ptrs()); - - gko::kernels::reference::factorization::initialize_l_u( - ref, gko::lend(csr_ref), gko::lend(*l_ref), gko::lend(*u_ref)); - gko::kernels::dpcpp::factorization::initialize_l_u( - dpcpp, gko::lend(csr_dpcpp), gko::lend(*l_dpcpp), - gko::lend(*u_dpcpp)); - } - - template - static std::unique_ptr static_unique_ptr_cast( - std::unique_ptr&& from) - { - return std::unique_ptr{static_cast(from.release())}; - } - - void compute_lu(std::unique_ptr* l_ref, std::unique_ptr* u_ref, - std::unique_ptr* l_dpcpp, - std::unique_ptr* u_dpcpp, - gko::size_type iterations = 0) - { - auto coo_ref = Coo::create(ref); - csr_ref->convert_to(gko::lend(coo_ref)); - auto coo_dpcpp = Coo::create(dpcpp); - csr_dpcpp->convert_to(gko::lend(coo_dpcpp)); - initialize_lu(l_ref, u_ref, l_dpcpp, u_dpcpp); - auto u_transpose_lin_op_ref = (*u_ref)->transpose(); - auto u_transpose_csr_ref = - static_unique_ptr_cast(std::move(u_transpose_lin_op_ref)); - auto u_transpose_lin_op_dpcpp = (*u_dpcpp)->transpose(); - auto u_transpose_csr_dpcpp = - static_unique_ptr_cast(std::move(u_transpose_lin_op_dpcpp)); - - gko::kernels::reference::par_ilu_factorization::compute_l_u_factors( - ref, iterations, gko::lend(coo_ref), gko::lend(*l_ref), - gko::lend(u_transpose_csr_ref)); - gko::kernels::dpcpp::par_ilu_factorization::compute_l_u_factors( - dpcpp, iterations, gko::lend(coo_dpcpp), gko::lend(*l_dpcpp), - gko::lend(u_transpose_csr_dpcpp)); - auto u_lin_op_ref = u_transpose_csr_ref->transpose(); - *u_ref = static_unique_ptr_cast(std::move(u_lin_op_ref)); - auto u_lin_op_dpcpp = u_transpose_csr_dpcpp->transpose(); - *u_dpcpp = static_unique_ptr_cast(std::move(u_lin_op_dpcpp)); - } -}; - - -TEST_F(ParIlu, DpcppKernelAddDiagonalElementsSortedEquivalentToRef) -{ - index_type num_rows{600}; - index_type num_cols{600}; - auto mtx_ref = gen_mtx(num_rows, num_cols); - auto mtx_dpcpp = gko::clone(dpcpp, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(mtx_ref), true); - gko::kernels::dpcpp::factorization::add_diagonal_elements( - dpcpp, gko::lend(mtx_dpcpp), true); - - ASSERT_TRUE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_dpcpp, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_dpcpp); -} - - -TEST_F(ParIlu, DpcppKernelAddDiagonalElementsUnsortedEquivalentToRef) -{ - index_type num_rows{600}; - index_type num_cols{600}; - auto mtx_ref = gen_unsorted_mtx(num_rows, num_cols); - auto mtx_dpcpp = gko::clone(dpcpp, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(mtx_ref), false); - gko::kernels::dpcpp::factorization::add_diagonal_elements( - dpcpp, gko::lend(mtx_dpcpp), false); - - ASSERT_FALSE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_dpcpp, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_dpcpp); -} - - -TEST_F(ParIlu, DpcppKernelAddDiagonalElementsNonSquareEquivalentToRef) -{ - index_type num_rows{600}; - index_type num_cols{500}; - auto mtx_ref = gen_mtx(num_rows, num_cols); - auto mtx_dpcpp = gko::clone(dpcpp, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(mtx_ref), true); - gko::kernels::dpcpp::factorization::add_diagonal_elements( - dpcpp, gko::lend(mtx_dpcpp), true); - - ASSERT_TRUE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_dpcpp, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_dpcpp); -} - - -TEST_F(ParIlu, KernelInitializeRowPtrsLUEquivalentToRef) -{ - auto num_row_ptrs = csr_ref->get_size()[0] + 1; - gko::array l_row_ptrs_array_ref(ref, num_row_ptrs); - gko::array u_row_ptrs_array_ref(ref, num_row_ptrs); - gko::array l_row_ptrs_array_dpcpp(dpcpp, num_row_ptrs); - gko::array u_row_ptrs_array_dpcpp(dpcpp, num_row_ptrs); - - initialize_row_ptrs( - l_row_ptrs_array_ref.get_data(), u_row_ptrs_array_ref.get_data(), - l_row_ptrs_array_dpcpp.get_data(), u_row_ptrs_array_dpcpp.get_data()); - - GKO_ASSERT_ARRAY_EQ(l_row_ptrs_array_ref, l_row_ptrs_array_dpcpp); - GKO_ASSERT_ARRAY_EQ(u_row_ptrs_array_ref, u_row_ptrs_array_dpcpp); -} - - -TEST_F(ParIlu, KernelInitializeParILUIsEquivalentToRef) -{ - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_dpcpp{}; - std::unique_ptr u_dpcpp{}; - - initialize_lu(&l_ref, &u_ref, &l_dpcpp, &u_dpcpp); - - GKO_ASSERT_MTX_NEAR(l_ref, l_dpcpp, r::value); - GKO_ASSERT_MTX_NEAR(u_ref, u_dpcpp, r::value); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_dpcpp); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_dpcpp); -} - - -TEST_F(ParIlu, KernelComputeParILUIsEquivalentToRef) -{ - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_dpcpp{}; - std::unique_ptr u_dpcpp{}; - - compute_lu(&l_ref, &u_ref, &l_dpcpp, &u_dpcpp); - - GKO_ASSERT_MTX_NEAR(l_ref, l_dpcpp, 5e-2); - GKO_ASSERT_MTX_NEAR(u_ref, u_dpcpp, 5e-2); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_dpcpp); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_dpcpp); -} - - -TEST_F(ParIlu, KernelComputeParILUWithMoreIterationsIsEquivalentToRef) -{ - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_dpcpp{}; - std::unique_ptr u_dpcpp{}; - gko::size_type iterations{200}; - - compute_lu(&l_ref, &u_ref, &l_dpcpp, &u_dpcpp, iterations); - - GKO_ASSERT_MTX_NEAR(l_ref, l_dpcpp, r::value); - GKO_ASSERT_MTX_NEAR(u_ref, u_dpcpp, r::value); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_dpcpp); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_dpcpp); -} - - -} // namespace diff --git a/dpcpp/test/factorization/par_ilut_kernels.cpp b/dpcpp/test/factorization/par_ilut_kernels.cpp deleted file mode 100644 index 99207dd76e8..00000000000 --- a/dpcpp/test/factorization/par_ilut_kernels.cpp +++ /dev/null @@ -1,571 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ilut_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/matrix/csr_builder.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "core/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIlut : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = gko::int32; - using Dense = gko::matrix::Dense; - using ComplexDense = gko::matrix::Dense>; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - using ComplexCsr = gko::matrix::Csr, index_type>; - - ParIlut() -#ifdef GINKGO_FAST_TESTS - : mtx_size(152, 231), -#else - : mtx_size(532, 423), -#endif - rand_engine(1337), - ref(gko::ReferenceExecutor::create()), - dpcpp(gko::DpcppExecutor::create(0, ref)) - { - mtx1 = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[1], - std::uniform_int_distribution<>(10, mtx_size[1]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx2 = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[1], - std::uniform_int_distribution<>(0, mtx_size[1]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_square = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[0], - std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l2 = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], true, std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l_complex = - gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_u = gko::test::generate_random_upper_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_u_complex = - gko::test::generate_random_upper_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - - dmtx1 = gko::clone(dpcpp, mtx1); - dmtx2 = gko::clone(dpcpp, mtx2); - dmtx_square = gko::clone(dpcpp, mtx_square); - dmtx_ani = Csr::create(dpcpp); - dmtx_l_ani = Csr::create(dpcpp); - dmtx_u_ani = Csr::create(dpcpp); - dmtx_ut_ani = Csr::create(dpcpp); - dmtx_l = gko::clone(dpcpp, mtx_l); - dmtx_l2 = gko::clone(dpcpp, mtx_l2); - dmtx_u = gko::clone(dpcpp, mtx_u); - dmtx_l_complex = gko::clone(dpcpp, mtx_l_complex); - dmtx_u_complex = gko::clone(dpcpp, mtx_u_complex); - } - - void SetUp() - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - mtx_ani = gko::read(input_file, ref); - mtx_ani->sort_by_column_index(); - - { - mtx_l_ani = Csr::create(ref, mtx_ani->get_size()); - mtx_u_ani = Csr::create(ref, mtx_ani->get_size()); - gko::matrix::CsrBuilder l_builder( - mtx_l_ani.get()); - gko::matrix::CsrBuilder u_builder( - mtx_u_ani.get()); - gko::kernels::reference::factorization::initialize_row_ptrs_l_u( - ref, mtx_ani.get(), mtx_l_ani->get_row_ptrs(), - mtx_u_ani->get_row_ptrs()); - auto l_nnz = - mtx_l_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - auto u_nnz = - mtx_u_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - l_builder.get_col_idx_array().resize_and_reset(l_nnz); - l_builder.get_value_array().resize_and_reset(l_nnz); - u_builder.get_col_idx_array().resize_and_reset(u_nnz); - u_builder.get_value_array().resize_and_reset(u_nnz); - gko::kernels::reference::factorization::initialize_l_u( - ref, mtx_ani.get(), mtx_l_ani.get(), mtx_u_ani.get()); - mtx_ut_ani = Csr::create(ref, mtx_ani->get_size(), - mtx_u_ani->get_num_stored_elements()); - gko::kernels::reference::csr::transpose(ref, mtx_u_ani.get(), - mtx_ut_ani.get()); - } - dmtx_ani->copy_from(mtx_ani.get()); - dmtx_l_ani->copy_from(mtx_l_ani.get()); - dmtx_u_ani->copy_from(mtx_u_ani.get()); - dmtx_ut_ani->copy_from(mtx_ut_ani.get()); - } - - template - void test_select(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, index_type rank, - value_type tolerance = 0.0) - { - auto size = index_type(mtx->get_num_stored_elements()); - using ValueType = typename Mtx::value_type; - - gko::remove_complex res{}; - gko::remove_complex dres{}; - gko::array tmp(ref); - gko::array> tmp2(ref); - gko::array dtmp(dpcpp); - gko::array> dtmp2(dpcpp); - - gko::kernels::reference::par_ilut_factorization::threshold_select( - ref, mtx.get(), rank, tmp, tmp2, res); - gko::kernels::dpcpp::par_ilut_factorization::threshold_select( - dpcpp, dmtx.get(), rank, dtmp, dtmp2, dres); - - ASSERT_NEAR(res, dres, tolerance); - } - - template > - void test_filter(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, value_type threshold, - bool lower) - { - auto res = Mtx::create(ref, mtx_size); - auto dres = Mtx::create(dpcpp, mtx_size); - auto res_coo = Coo::create(ref, mtx_size); - auto dres_coo = Coo::create(dpcpp, mtx_size); - auto local_mtx = gko::as(lower ? mtx->clone() : mtx->transpose()); - auto local_dmtx = - gko::as(lower ? dmtx->clone() : dmtx->transpose()); - - gko::kernels::reference::par_ilut_factorization::threshold_filter( - ref, local_mtx.get(), threshold, res.get(), res_coo.get(), lower); - gko::kernels::dpcpp::par_ilut_factorization::threshold_filter( - dpcpp, local_dmtx.get(), threshold, dres.get(), dres_coo.get(), - lower); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); - GKO_ASSERT_MTX_NEAR(res, res_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, res_coo); - GKO_ASSERT_MTX_NEAR(dres, dres_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(dres, dres_coo); - } - - template > - void test_filter_approx(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, index_type rank, - value_type tolerance = 0.0) - { - auto res = Mtx::create(ref, mtx_size); - auto dres = Mtx::create(dpcpp, mtx_size); - auto res_coo = Coo::create(ref, mtx_size); - auto dres_coo = Coo::create(dpcpp, mtx_size); - using ValueType = typename Mtx::value_type; - - gko::array tmp(ref); - gko::array dtmp(dpcpp); - gko::remove_complex threshold{}; - gko::remove_complex dthreshold{}; - - gko::kernels::reference::par_ilut_factorization:: - threshold_filter_approx(ref, mtx.get(), rank, tmp, threshold, - res.get(), res_coo.get()); - gko::kernels::dpcpp::par_ilut_factorization::threshold_filter_approx( - dpcpp, dmtx.get(), rank, dtmp, dthreshold, dres.get(), - dres_coo.get()); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); - GKO_ASSERT_MTX_NEAR(res, res_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, res_coo); - GKO_ASSERT_MTX_NEAR(dres, dres_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(dres, dres_coo); - ASSERT_NEAR(threshold, dthreshold, tolerance); - } - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx1; - std::unique_ptr mtx2; - std::unique_ptr mtx_square; - std::unique_ptr mtx_ani; - std::unique_ptr mtx_l_ani; - std::unique_ptr mtx_u_ani; - std::unique_ptr mtx_ut_ani; - std::unique_ptr mtx_l; - std::unique_ptr mtx_l2; - std::unique_ptr mtx_l_complex; - std::unique_ptr mtx_u; - std::unique_ptr mtx_u_complex; - - std::unique_ptr dmtx1; - std::unique_ptr dmtx2; - std::unique_ptr dmtx_square; - std::unique_ptr dmtx_ani; - std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx_u_ani; - std::unique_ptr dmtx_ut_ani; - std::unique_ptr dmtx_l; - std::unique_ptr dmtx_l2; - std::unique_ptr dmtx_l_complex; - std::unique_ptr dmtx_u; - std::unique_ptr dmtx_u_complex; -}; - - -TEST_F(ParIlut, KernelThresholdSelectIsEquivalentToRef) -{ - test_select(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() / 3); -} - - -TEST_F(ParIlut, KernelThresholdSelectMinIsEquivalentToRef) -{ - test_select(mtx_l, dmtx_l, 0); -} - - -TEST_F(ParIlut, KernelThresholdSelectMaxIsEquivalentToRef) -{ - test_select(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() - 1); -} - - -TEST_F(ParIlut, KernelComplexThresholdSelectIsEquivalentToRef) -{ - test_select(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() / 3, - 50 * r::value); -} - - -TEST_F(ParIlut, KernelComplexThresholdSelectMinIsEquivalentToRef) -{ - // can change the threshold? - test_select(mtx_l_complex, dmtx_l_complex, 0, 50 * r::value); -} - - -TEST_F(ParIlut, KernelComplexThresholdSelectMaxLowerIsEquivalentToRef) -{ - test_select(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() - 1, - 50 * r::value); -} - - -TEST_F(ParIlut, KernelThresholdFilterNullptrCooIsEquivalentToRef) -{ - auto res = Csr::create(ref, mtx_size); - auto dres = Csr::create(dpcpp, mtx_size); - Coo* null_coo = nullptr; - - gko::kernels::reference::par_ilut_factorization::threshold_filter( - ref, mtx_l.get(), 0.5, res.get(), null_coo, true); - gko::kernels::dpcpp::par_ilut_factorization::threshold_filter( - dpcpp, dmtx_l.get(), 0.5, dres.get(), null_coo, true); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); -} - - -TEST_F(ParIlut, KernelThresholdFilterLowerIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0.5, true); -} - - -TEST_F(ParIlut, KernelThresholdFilterUpperIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0.5, false); -} - - -TEST_F(ParIlut, KernelThresholdFilterNoneLowerIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0, true); -} - - -TEST_F(ParIlut, KernelThresholdFilterNoneUpperIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0, false); -} - - -TEST_F(ParIlut, KernelThresholdFilterAllLowerIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 1e6, true); -} - - -TEST_F(ParIlut, KernelThresholdFilterAllUpperIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 1e6, false); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterLowerIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0.5, true); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterUpperIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0.5, false); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterNoneLowerIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0, true); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterNoneUpperIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0, false); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterAllLowerIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 1e6, true); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterAllUppererIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 1e6, false); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxNullptrCooIsEquivalentToRef) -{ -#if GINKGO_DPCPP_SINGLE_MODE - // Need bitwise equivalence to CPU - GTEST_SKIP(); -#endif - test_filter(mtx_l, dmtx_l, 0.5, true); - auto res = Csr::create(ref, mtx_size); - auto dres = Csr::create(dpcpp, mtx_size); - Coo* null_coo = nullptr; - gko::array tmp(ref); - gko::array dtmp(dpcpp); - gko::remove_complex threshold{}; - gko::remove_complex dthreshold{}; - index_type rank{}; - - gko::kernels::reference::par_ilut_factorization::threshold_filter_approx( - ref, mtx_l.get(), rank, tmp, threshold, res.get(), null_coo); - gko::kernels::dpcpp::par_ilut_factorization::threshold_filter_approx( - dpcpp, dmtx_l.get(), rank, dtmp, dthreshold, dres.get(), null_coo); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); - ASSERT_EQ(threshold, dthreshold); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxLowerIsEquivalentToRef) -{ -#if GINKGO_DPCPP_SINGLE_MODE - // Need bitwise equivalence to CPU - GTEST_SKIP(); -#endif - test_filter_approx(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() / 2); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxNoneLowerIsEquivalentToRef) -{ -#if GINKGO_DPCPP_SINGLE_MODE - // Need bitwise equivalence to CPU - GTEST_SKIP(); -#endif - test_filter_approx(mtx_l, dmtx_l, 0); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxAllLowerIsEquivalentToRef) -{ -#if GINKGO_DPCPP_SINGLE_MODE - // Need bitwise equivalence to CPU - GTEST_SKIP(); -#endif - test_filter_approx(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() - 1); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterApproxLowerIsEquivalentToRef) -{ -#if GINKGO_DPCPP_SINGLE_MODE - // Need bitwise equivalence to CPU - GTEST_SKIP(); -#endif - test_filter_approx(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() / 2, - r::value); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterApproxNoneLowerIsEquivalentToRef) -{ -#if GINKGO_DPCPP_SINGLE_MODE - // Need bitwise equivalence to CPU - GTEST_SKIP(); -#endif - test_filter_approx(mtx_l_complex, dmtx_l_complex, 0, r::value); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterApproxAllLowerIsEquivalentToRef) -{ -#if GINKGO_DPCPP_SINGLE_MODE - // Need bitwise equivalence to CPU - GTEST_SKIP(); -#endif - test_filter_approx(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() - 1, - r::value); -} - - -TEST_F(ParIlut, KernelAddCandidatesIsEquivalentToRef) -{ - auto square_size = mtx_square->get_size(); - auto mtx_lu = Csr::create(ref, square_size); - mtx_l2->apply(mtx_u.get(), mtx_lu.get()); - auto dmtx_lu = Csr::create(dpcpp, square_size); - dmtx_lu->copy_from(mtx_lu.get()); - auto res_mtx_l = Csr::create(ref, square_size); - auto res_mtx_u = Csr::create(ref, square_size); - auto dres_mtx_l = Csr::create(dpcpp, square_size); - auto dres_mtx_u = Csr::create(dpcpp, square_size); - - gko::kernels::reference::par_ilut_factorization::add_candidates( - ref, mtx_lu.get(), mtx_square.get(), mtx_l2.get(), mtx_u.get(), - res_mtx_l.get(), res_mtx_u.get()); - gko::kernels::dpcpp::par_ilut_factorization::add_candidates( - dpcpp, dmtx_lu.get(), dmtx_square.get(), dmtx_l2.get(), dmtx_u.get(), - dres_mtx_l.get(), dres_mtx_u.get()); - - GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_l, dres_mtx_l); - GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_u, dres_mtx_u); - GKO_ASSERT_MTX_NEAR(res_mtx_l, dres_mtx_l, r::value); - GKO_ASSERT_MTX_NEAR(res_mtx_u, dres_mtx_u, r::value); -} - - -TEST_F(ParIlut, KernelComputeLUIsEquivalentToRef) -{ - auto square_size = mtx_ani->get_size(); - auto mtx_l_coo = Coo::create(ref, square_size); - auto mtx_u_coo = Coo::create(ref, square_size); - mtx_l_ani->convert_to(mtx_l_coo.get()); - mtx_u_ani->convert_to(mtx_u_coo.get()); - auto dmtx_l_coo = Coo::create(dpcpp, square_size); - auto dmtx_u_coo = Coo::create(dpcpp, square_size); - dmtx_l_coo->copy_from(mtx_l_coo.get()); - dmtx_u_coo->copy_from(mtx_u_coo.get()); - - gko::kernels::reference::par_ilut_factorization::compute_l_u_factors( - ref, mtx_ani.get(), mtx_l_ani.get(), mtx_l_coo.get(), mtx_u_ani.get(), - mtx_u_coo.get(), mtx_ut_ani.get()); - for (int i = 0; i < 20; ++i) { - gko::kernels::dpcpp::par_ilut_factorization::compute_l_u_factors( - dpcpp, dmtx_ani.get(), dmtx_l_ani.get(), dmtx_l_coo.get(), - dmtx_u_ani.get(), dmtx_u_coo.get(), dmtx_ut_ani.get()); - } - auto dmtx_utt_ani = gko::as(dmtx_ut_ani->transpose()); - - GKO_ASSERT_MTX_NEAR(mtx_l_ani, dmtx_l_ani, 1e-2); - GKO_ASSERT_MTX_NEAR(mtx_u_ani, dmtx_u_ani, 1e-2); - GKO_ASSERT_MTX_NEAR(dmtx_u_ani, dmtx_utt_ani, 0); -} - - -} // namespace diff --git a/dpcpp/test/matrix/CMakeLists.txt b/dpcpp/test/matrix/CMakeLists.txt index 5be841b3d00..88ab52e9c3f 100644 --- a/dpcpp/test/matrix/CMakeLists.txt +++ b/dpcpp/test/matrix/CMakeLists.txt @@ -1,8 +1 @@ -ginkgo_create_test(coo_kernels) -ginkgo_create_test(csr_kernels) -ginkgo_create_test(dense_kernels) -ginkgo_create_test(diagonal_kernels) -ginkgo_create_test(ell_kernels) ginkgo_create_test(fbcsr_kernels) -ginkgo_create_test(hybrid_kernels) -ginkgo_create_test(sellp_kernels) diff --git a/dpcpp/test/matrix/coo_kernels.cpp b/dpcpp/test/matrix/coo_kernels.cpp deleted file mode 100644 index da5607063bd..00000000000 --- a/dpcpp/test/matrix/coo_kernels.cpp +++ /dev/null @@ -1,398 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/matrix/coo_kernels.hpp" -#include "core/test/utils.hpp" -#include "core/test/utils/unsort_matrix.hpp" - - -namespace { - - -class Coo : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using vtype = float; -#else - using vtype = double; -#endif // GINKGO_DPCPP_SINGLE_MODE - using Mtx = gko::matrix::Coo; - using Vec = gko::matrix::Dense; - using ComplexVec = gko::matrix::Dense>; - - Coo() : rand_engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data(int num_vectors = 1) - { - mtx = gen_mtx(532, 231); - expected = gen_mtx(532, num_vectors); - y = gen_mtx(231, num_vectors); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = gko::clone(dpcpp, mtx); - dresult = gko::clone(dpcpp, expected); - dy = gko::clone(dpcpp, y); - dalpha = gko::clone(dpcpp, alpha); - dbeta = gko::clone(dpcpp, beta); - } - - void unsort_mtx() - { - gko::test::unsort_matrix(mtx.get(), rand_engine); - dmtx->copy_from(mtx.get()); - } - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; -}; - - -TEST_F(Coo, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, SimpleApplyDoesntOverwritePadding) -{ - set_up_apply_data(); - auto dresult_padded = - Vec::create(dpcpp, dresult->get_size(), dresult->get_stride() + 1); - dresult_padded->copy_from(dresult.get()); - vtype padding_val{1234.0}; - dpcpp->copy_from(dpcpp->get_master().get(), 1, &padding_val, - dresult_padded->get_values() + 1); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult_padded.get()); - - GKO_ASSERT_MTX_NEAR(dresult_padded, expected, r::value); - ASSERT_EQ(dpcpp->copy_val_to_host(dresult_padded->get_values() + 1), - 1234.0); -} - - -TEST_F(Coo, SimpleApplyIsEquivalentToRefUnsorted) -{ - set_up_apply_data(); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, AdvancedApplyDoesntOverwritePadding) -{ - set_up_apply_data(); - auto dresult_padded = - Vec::create(dpcpp, dresult->get_size(), dresult->get_stride() + 1); - dresult_padded->copy_from(dresult.get()); - vtype padding_val{1234.0}; - dpcpp->copy_from(dpcpp->get_master().get(), 1, &padding_val, - dresult_padded->get_values() + 1); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult_padded.get()); - - GKO_ASSERT_MTX_NEAR(dresult_padded, expected, r::value); - ASSERT_EQ(dpcpp->copy_val_to_host(dresult_padded->get_values() + 1), - 1234.0); -} - - -TEST_F(Coo, SimpleApplyAddIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, AdvancedApplyAddIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply2(alpha.get(), y.get(), expected.get()); - dmtx->apply2(dalpha.get(), dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, SimpleApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, AdvancedApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, SimpleApplyAddToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, SimpleApplyAddToLargeDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(33); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, AdvancedApplyAddToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply2(alpha.get(), y.get(), expected.get()); - dmtx->apply2(dalpha.get(), dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, AdvancedApplyAddToLargeDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(33); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Coo, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Coo, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Coo, ApplyAddToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - mtx->apply2(alpha.get(), complex_b.get(), complex_x.get()); - dmtx->apply2(dalpha.get(), dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Coo, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - auto dense_mtx = gko::matrix::Dense::create(ref); - auto ddense_mtx = gko::matrix::Dense::create(dpcpp); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Coo, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto dense_mtx = gko::matrix::Dense::create(ref); - auto csr_mtx = gko::matrix::Csr::create(ref); - auto dcsr_mtx = gko::matrix::Csr::create(dpcpp); - - mtx->convert_to(dense_mtx.get()); - dense_mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Coo, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Coo, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); -} - - -TEST_F(Coo, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); -} - - -} // namespace diff --git a/dpcpp/test/matrix/csr_kernels.cpp b/dpcpp/test/matrix/csr_kernels.cpp deleted file mode 100644 index 375f7d93a93..00000000000 --- a/dpcpp/test/matrix/csr_kernels.cpp +++ /dev/null @@ -1,967 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#include "core/components/prefix_sum_kernels.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "core/test/utils.hpp" -#include "core/test/utils/unsort_matrix.hpp" - - -namespace { - - -class Csr : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using vtype = float; -#else - using vtype = double; -#endif // GINKGO_DPCPP_SINGLE_MODE - using Arr = gko::array; - using Vec = gko::matrix::Dense; - using Mtx = gko::matrix::Csr; - using ComplexVec = gko::matrix::Dense>; - using ComplexMtx = gko::matrix::Csr>; - - Csr() -#ifdef GINKGO_FAST_TESTS - : mtx_size(152, 231), -#else - : mtx_size(532, 231), -#endif - rand_engine(42) - {} - - void SetUp() - { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data() - { - mtx2 = Mtx::create(ref); - mtx2->copy_from(gen_mtx(mtx_size[0], mtx_size[1], 5)); - dmtx2 = Mtx::create(dpcpp); - dmtx2->copy_from(mtx2.get()); - } - - void set_up_apply_data(std::shared_ptr strategy, - int num_vectors = 1) - { - mtx = Mtx::create(ref, strategy); - mtx->copy_from(gen_mtx(mtx_size[0], mtx_size[1], 1)); - square_mtx = Mtx::create(ref, strategy); - square_mtx->copy_from(gen_mtx(mtx_size[0], mtx_size[0], 1)); - expected = gen_mtx(mtx_size[0], num_vectors, 1); - y = gen_mtx(mtx_size[1], num_vectors, 1); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = Mtx::create(dpcpp, strategy); - dmtx->copy_from(mtx.get()); - square_dmtx = Mtx::create(dpcpp, strategy); - square_dmtx->copy_from(square_mtx.get()); - dresult = gko::clone(dpcpp, expected); - dy = gko::clone(dpcpp, y); - dalpha = gko::clone(dpcpp, alpha); - dbeta = gko::clone(dpcpp, beta); - - std::vector tmp(mtx->get_size()[0], 0); - auto rng = std::default_random_engine{}; - std::iota(tmp.begin(), tmp.end(), 0); - std::shuffle(tmp.begin(), tmp.end(), rng); - std::vector tmp2(mtx->get_size()[1], 0); - std::iota(tmp2.begin(), tmp2.end(), 0); - std::shuffle(tmp2.begin(), tmp2.end(), rng); - rpermute_idxs = std::make_unique(ref, tmp.begin(), tmp.end()); - cpermute_idxs = std::make_unique(ref, tmp2.begin(), tmp2.end()); - } - - void set_up_apply_complex_data( - std::shared_ptr strategy) - { - complex_mtx = ComplexMtx::create(ref, strategy); - complex_mtx->copy_from( - gen_mtx(mtx_size[0], mtx_size[1], 1)); - complex_dmtx = ComplexMtx::create(dpcpp, strategy); - complex_dmtx->copy_from(complex_mtx.get()); - } - - void unsort_mtx() - { - gko::test::unsort_matrix(mtx.get(), rand_engine); - dmtx->copy_from(mtx.get()); - } - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr mtx2; - std::unique_ptr complex_mtx; - std::unique_ptr square_mtx; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dmtx2; - std::unique_ptr complex_dmtx; - std::unique_ptr square_dmtx; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; - std::unique_ptr rpermute_idxs; - std::unique_ptr cpermute_idxs; -}; - - -TEST_F(Csr, StrategyAfterCopyIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - - ASSERT_EQ(mtx->get_strategy()->get_name(), - dmtx->get_strategy()->get_name()); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithLoadBalance) -{ - set_up_apply_data(std::make_shared(dpcpp)); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithLoadBalanceUnsorted) -{ - set_up_apply_data(std::make_shared(dpcpp)); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithLoadBalance) -{ - set_up_apply_data(std::make_shared(dpcpp)); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithOnemkl) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithOnemklUnsorted) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithOnemkl) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithMergePath) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithMergePathUnsorted) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithMergePath) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithClassical) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithClassicalUnsorted) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithClassical) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithAutomatical) -{ - set_up_apply_data(std::make_shared(dpcpp)); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithLoadBalance) -{ - set_up_apply_data(std::make_shared(dpcpp), 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithLoadBalance) -{ - set_up_apply_data(std::make_shared(dpcpp), 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithClassical) -{ - set_up_apply_data(std::make_shared(), 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithClassical) -{ - set_up_apply_data(std::make_shared(), 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithMergePath) -{ - set_up_apply_data(std::make_shared(), 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithMergePath) -{ - set_up_apply_data(std::make_shared(), 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Csr, AdvancedApplyToCsrMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - auto trans = mtx->transpose(); - auto d_trans = dmtx->transpose(); - - mtx->apply(alpha.get(), trans.get(), beta.get(), square_mtx.get()); - dmtx->apply(dalpha.get(), d_trans.get(), dbeta.get(), square_dmtx.get()); - - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, r::value); - GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); - ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); -} - - -TEST_F(Csr, SimpleApplyToCsrMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - auto trans = mtx->transpose(); - auto d_trans = dmtx->transpose(); - - mtx->apply(trans.get(), square_mtx.get()); - dmtx->apply(d_trans.get(), square_dmtx.get()); - - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, r::value); - GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); - ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); -} - - -TEST_F(Csr, AdvancedApplyToIdentityMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - auto a = gen_mtx(mtx_size[0], mtx_size[1], 0); - auto b = gen_mtx(mtx_size[0], mtx_size[1], 0); - auto da = gko::clone(dpcpp, a); - auto db = gko::clone(dpcpp, b); - auto id = gko::matrix::Identity::create(ref, mtx_size[1]); - auto did = - gko::matrix::Identity::create(dpcpp, mtx_size[1]); - - a->apply(alpha.get(), id.get(), beta.get(), b.get()); - da->apply(dalpha.get(), did.get(), dbeta.get(), db.get()); - - GKO_ASSERT_MTX_NEAR(b, db, r::value); - GKO_ASSERT_MTX_EQ_SPARSITY(b, db); - ASSERT_TRUE(db->is_sorted_by_column_index()); -} - - -TEST_F(Csr, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - auto complex_b = gen_mtx(this->mtx_size[1], 3, 1); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(this->mtx_size[0], 3, 1); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Csr, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - auto complex_b = gen_mtx(this->mtx_size[1], 3, 1); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(this->mtx_size[0], 3, 1); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Csr, TransposeIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - - auto trans = gko::as(mtx->transpose()); - auto d_trans = gko::as(dmtx->transpose()); - - GKO_ASSERT_MTX_NEAR(d_trans, trans, 0.0); - ASSERT_TRUE(d_trans->is_sorted_by_column_index()); -} - - -TEST_F(Csr, ConjugateTransposeIsEquivalentToRef) -{ - set_up_apply_complex_data(std::make_shared(dpcpp)); - - auto trans = gko::as(complex_mtx->conj_transpose()); - auto d_trans = gko::as(complex_dmtx->conj_transpose()); - - GKO_ASSERT_MTX_NEAR(d_trans, trans, 0.0); - ASSERT_TRUE(d_trans->is_sorted_by_column_index()); -} - - -TEST_F(Csr, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto dense_mtx = gko::matrix::Dense::create(ref); - auto ddense_mtx = gko::matrix::Dense::create(dpcpp); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToDenseIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto dense_mtx = gko::matrix::Dense::create(ref); - auto ddense_mtx = gko::matrix::Dense::create(dpcpp); - - mtx->move_to(dense_mtx.get()); - dmtx->move_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToEllIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto ell_mtx = gko::matrix::Ell::create(ref); - auto dell_mtx = gko::matrix::Ell::create(dpcpp); - - mtx->convert_to(ell_mtx.get()); - dmtx->convert_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(ell_mtx.get(), dell_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToEllIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto ell_mtx = gko::matrix::Ell::create(ref); - auto dell_mtx = gko::matrix::Ell::create(dpcpp); - - mtx->move_to(ell_mtx.get()); - dmtx->move_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(ell_mtx.get(), dell_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto sparsity_mtx = gko::matrix::SparsityCsr::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr::create(dpcpp); - - mtx->convert_to(sparsity_mtx.get()); - dmtx->convert_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sparsity_mtx.get(), d_sparsity_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto sparsity_mtx = gko::matrix::SparsityCsr::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr::create(dpcpp); - - mtx->move_to(sparsity_mtx.get()); - dmtx->move_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sparsity_mtx.get(), d_sparsity_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToCooIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto coo_mtx = gko::matrix::Coo::create(ref); - auto dcoo_mtx = gko::matrix::Coo::create(dpcpp); - - mtx->convert_to(coo_mtx.get()); - dmtx->convert_to(dcoo_mtx.get()); - - GKO_ASSERT_MTX_NEAR(coo_mtx.get(), dcoo_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToCooIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto coo_mtx = gko::matrix::Coo::create(ref); - auto dcoo_mtx = gko::matrix::Coo::create(dpcpp); - - mtx->move_to(coo_mtx.get()); - dmtx->move_to(dcoo_mtx.get()); - - GKO_ASSERT_MTX_NEAR(coo_mtx.get(), dcoo_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToSellpIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto sellp_mtx = gko::matrix::Sellp::create(ref); - auto dsellp_mtx = gko::matrix::Sellp::create(dpcpp); - - mtx->convert_to(sellp_mtx.get()); - dmtx->convert_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx.get(), dsellp_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToSellpIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto sellp_mtx = gko::matrix::Sellp::create(ref); - auto dsellp_mtx = gko::matrix::Sellp::create(dpcpp); - - mtx->move_to(sellp_mtx.get()); - dmtx->move_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx.get(), dsellp_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertsEmptyToSellp) -{ - auto dempty_mtx = Mtx::create(dpcpp); - auto dsellp_mtx = gko::matrix::Sellp::create(dpcpp); - - dempty_mtx->convert_to(dsellp_mtx.get()); - - ASSERT_EQ(dpcpp->copy_val_to_host(dsellp_mtx->get_const_slice_sets()), 0); - ASSERT_FALSE(dsellp_mtx->get_size()); -} - - -TEST_F(Csr, ConvertToHybridIsEquivalentToRef) -{ - using Hybrid_type = gko::matrix::Hybrid; - set_up_apply_data(std::make_shared()); - auto hybrid_mtx = Hybrid_type::create( - ref, std::make_shared(2)); - auto dhybrid_mtx = Hybrid_type::create( - dpcpp, std::make_shared(2)); - - mtx->convert_to(hybrid_mtx.get()); - dmtx->convert_to(dhybrid_mtx.get()); - - GKO_ASSERT_MTX_NEAR(hybrid_mtx.get(), dhybrid_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToHybridIsEquivalentToRef) -{ - using Hybrid_type = gko::matrix::Hybrid; - set_up_apply_data(std::make_shared()); - auto hybrid_mtx = Hybrid_type::create( - ref, std::make_shared(2)); - auto dhybrid_mtx = Hybrid_type::create( - dpcpp, std::make_shared(2)); - - mtx->move_to(hybrid_mtx.get()); - dmtx->move_to(dhybrid_mtx.get()); - - GKO_ASSERT_MTX_NEAR(hybrid_mtx.get(), dhybrid_mtx.get(), 0); -} - - -TEST_F(Csr, IsPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto permuted = gko::as(square_mtx->permute(rpermute_idxs.get())); - auto dpermuted = gko::as(square_dmtx->permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(permuted, dpermuted); - GKO_ASSERT_MTX_NEAR(permuted, dpermuted, 0); -} - - -TEST_F(Csr, IsInversePermutable) -{ - set_up_apply_data(std::make_shared()); - - auto permuted = - gko::as(square_mtx->inverse_permute(rpermute_idxs.get())); - auto dpermuted = - gko::as(square_dmtx->inverse_permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(permuted, dpermuted); - GKO_ASSERT_MTX_NEAR(permuted, dpermuted, 0); -} - - -TEST_F(Csr, IsRowPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto r_permute = gko::as(mtx->row_permute(rpermute_idxs.get())); - auto dr_permute = gko::as(dmtx->row_permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(r_permute, dr_permute); - GKO_ASSERT_MTX_NEAR(r_permute, dr_permute, 0); -} - - -TEST_F(Csr, IsColPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto c_permute = gko::as(mtx->column_permute(cpermute_idxs.get())); - auto dc_permute = gko::as(dmtx->column_permute(cpermute_idxs.get())); - - ASSERT_TRUE(dc_permute->is_sorted_by_column_index()); - GKO_ASSERT_MTX_EQ_SPARSITY(c_permute, dc_permute); - GKO_ASSERT_MTX_NEAR(c_permute, dc_permute, 0); -} - - -TEST_F(Csr, IsInverseRowPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto inverse_r_permute = - gko::as(mtx->inverse_row_permute(rpermute_idxs.get())); - auto d_inverse_r_permute = - gko::as(dmtx->inverse_row_permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse_r_permute, d_inverse_r_permute); - GKO_ASSERT_MTX_NEAR(inverse_r_permute, d_inverse_r_permute, 0); -} - - -TEST_F(Csr, IsInverseColPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto inverse_c_permute = - gko::as(mtx->inverse_column_permute(cpermute_idxs.get())); - auto d_inverse_c_permute = - gko::as(dmtx->inverse_column_permute(cpermute_idxs.get())); - - ASSERT_TRUE(d_inverse_c_permute->is_sorted_by_column_index()); - GKO_ASSERT_MTX_EQ_SPARSITY(inverse_c_permute, d_inverse_c_permute); - GKO_ASSERT_MTX_NEAR(inverse_c_permute, d_inverse_c_permute, 0); -} - - -TEST_F(Csr, RecognizeSortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - bool is_sorted_dpcpp{}; - bool is_sorted_ref{}; - - is_sorted_ref = mtx->is_sorted_by_column_index(); - is_sorted_dpcpp = dmtx->is_sorted_by_column_index(); - - ASSERT_EQ(is_sorted_ref, is_sorted_dpcpp); -} - - -TEST_F(Csr, RecognizeUnsortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - bool is_sorted_dpcpp{}; - bool is_sorted_ref{}; - - is_sorted_ref = mtx->is_sorted_by_column_index(); - is_sorted_dpcpp = dmtx->is_sorted_by_column_index(); - - ASSERT_EQ(is_sorted_ref, is_sorted_dpcpp); -} - - -TEST_F(Csr, SortSortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - - mtx->sort_by_column_index(); - dmtx->sort_by_column_index(); - - // Values must be unchanged, therefore, tolerance is `0` - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 0); -} - - -TEST_F(Csr, SortUnsortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - - mtx->sort_by_column_index(); - dmtx->sort_by_column_index(); - - // Values must be unchanged, therefore, tolerance is `0` - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 0); -} - - -TEST_F(Csr, OneAutomaticalWorksWithDifferentMatrices) -{ - auto automatical = std::make_shared(dpcpp); - auto row_len_limit = automatical->intel_row_len_limit; - auto load_balance_mtx = - gen_mtx(1, row_len_limit + 1000, row_len_limit + 1); - auto classical_mtx = gen_mtx(50, 50, 1); - auto load_balance_mtx_d = gko::clone(dpcpp, load_balance_mtx); - auto classical_mtx_d = gko::clone(dpcpp, classical_mtx); - - load_balance_mtx_d->set_strategy(automatical); - classical_mtx_d->set_strategy(automatical); - - EXPECT_EQ("load_balance", load_balance_mtx_d->get_strategy()->get_name()); - EXPECT_EQ("classical", classical_mtx_d->get_strategy()->get_name()); - ASSERT_NE(load_balance_mtx_d->get_strategy().get(), - classical_mtx_d->get_strategy().get()); -} - - -TEST_F(Csr, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Csr, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); -} - - -TEST_F(Csr, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(dpcpp)); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); -} - - -TEST_F(Csr, InplaceAbsoluteComplexMatrixIsEquivalentToRef) -{ - set_up_apply_complex_data(std::make_shared(dpcpp)); - - complex_mtx->compute_absolute_inplace(); - complex_dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(complex_mtx, complex_dmtx, r::value); -} - - -TEST_F(Csr, OutplaceAbsoluteComplexMatrixIsEquivalentToRef) -{ - set_up_apply_complex_data(std::make_shared(dpcpp)); - - auto abs_mtx = complex_mtx->compute_absolute(); - auto dabs_mtx = complex_dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); -} - - -TEST_F(Csr, CalculateNnzPerRowInSpanIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr; - set_up_apply_data(); - gko::span rspan{7, 51}; - gko::span cspan{22, 88}; - auto size = this->mtx2->get_size(); - auto row_nnz = gko::array(this->ref, rspan.length() + 1); - auto drow_nnz = gko::array(this->dpcpp, row_nnz); - - gko::kernels::reference::csr::calculate_nonzeros_per_row_in_span( - this->ref, this->mtx2.get(), rspan, cspan, &row_nnz); - gko::kernels::dpcpp::csr::calculate_nonzeros_per_row_in_span( - this->dpcpp, this->dmtx2.get(), rspan, cspan, &drow_nnz); - - GKO_ASSERT_ARRAY_EQ(row_nnz, drow_nnz); -} - - -TEST_F(Csr, ComputeSubmatrixIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr; - using IndexType = int; - using ValueType = vtype; - set_up_apply_data(); - gko::span rspan{7, 51}; - gko::span cspan{22, 88}; - auto size = this->mtx2->get_size(); - auto row_nnz = gko::array(this->ref, rspan.length() + 1); - row_nnz.fill(gko::zero()); - gko::kernels::reference::csr::calculate_nonzeros_per_row_in_span( - this->ref, this->mtx2.get(), rspan, cspan, &row_nnz); - gko::kernels::reference::components::prefix_sum( - this->ref, row_nnz.get_data(), row_nnz.get_num_elems()); - auto num_nnz = row_nnz.get_data()[rspan.length()]; - auto drow_nnz = gko::array(this->dpcpp, row_nnz); - auto smat1 = - Mtx::create(this->ref, gko::dim<2>(rspan.length(), cspan.length()), - std::move(gko::array(this->ref, num_nnz)), - std::move(gko::array(this->ref, num_nnz)), - std::move(row_nnz)); - auto sdmat1 = - Mtx::create(this->dpcpp, gko::dim<2>(rspan.length(), cspan.length()), - std::move(gko::array(this->dpcpp, num_nnz)), - std::move(gko::array(this->dpcpp, num_nnz)), - std::move(drow_nnz)); - - - gko::kernels::reference::csr::compute_submatrix(this->ref, this->mtx2.get(), - rspan, cspan, smat1.get()); - gko::kernels::dpcpp::csr::compute_submatrix(this->dpcpp, this->dmtx2.get(), - rspan, cspan, sdmat1.get()); - - GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); -} - - -TEST_F(Csr, CreateSubMatrixIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr; - set_up_apply_data(); - gko::span rspan{47, 81}; - gko::span cspan{2, 31}; - - auto smat1 = this->mtx2->create_submatrix(rspan, cspan); - auto sdmat1 = this->dmtx2->create_submatrix(rspan, cspan); - - GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); -} - - -} // namespace diff --git a/dpcpp/test/matrix/dense_kernels.cpp b/dpcpp/test/matrix/dense_kernels.cpp deleted file mode 100644 index b9dff6e2a78..00000000000 --- a/dpcpp/test/matrix/dense_kernels.cpp +++ /dev/null @@ -1,613 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include -#include - - -#include "core/components/fill_array_kernels.hpp" -#include "core/matrix/dense_kernels.hpp" -#include "core/test/utils.hpp" -#include "dpcpp/test/utils.hpp" - - -namespace { - - -class Dense : public ::testing::Test { -protected: - using itype = int; -#if GINKGO_DPCPP_SINGLE_MODE - using vtype = float; -#else - using vtype = double; -#endif // GINKGO_DPCPP_SINGLE_MODE - using Mtx = gko::matrix::Dense; - using MixedMtx = gko::matrix::Dense>; - using NormVector = gko::matrix::Dense>; - using Arr = gko::array; - using ComplexMtx = gko::matrix::Dense>; - using MixedComplexMtx = - gko::matrix::Dense>>; - - Dense() : rand_engine(15) {} - - void SetUp() - { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); - } - - void set_up_vector_data(gko::size_type num_vecs, - bool different_alpha = false) - { - x = gen_mtx(1000, num_vecs); - y = gen_mtx(1000, num_vecs); - if (different_alpha) { - alpha = gen_mtx(1, num_vecs); - } else { - alpha = gko::initialize({2.0}, ref); - } - dx = gko::clone(dpcpp, x); - dy = gko::clone(dpcpp, y); - dalpha = gko::clone(dpcpp, alpha); - expected = Mtx::create(ref, gko::dim<2>{1, num_vecs}); - dresult = Mtx::create(dpcpp, gko::dim<2>{1, num_vecs}); - } - - void set_up_apply_data() - { - x = gen_mtx(65, 25); - c_x = gen_mtx(65, 25); - y = gen_mtx(25, 35); - expected = gen_mtx(65, 35); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - square = gen_mtx(x->get_size()[0], x->get_size()[0]); - dx = gko::clone(dpcpp, x); - dc_x = gko::clone(dpcpp, c_x); - dy = gko::clone(dpcpp, y); - dresult = gko::clone(dpcpp, expected); - dalpha = gko::clone(dpcpp, alpha); - dbeta = gko::clone(dpcpp, beta); - dsquare = gko::clone(dpcpp, square); - - std::vector tmp(x->get_size()[0], 0); - auto rng = std::default_random_engine{}; - std::iota(tmp.begin(), tmp.end(), 0); - std::shuffle(tmp.begin(), tmp.end(), rng); - std::vector tmp2(x->get_size()[1], 0); - std::iota(tmp2.begin(), tmp2.end(), 0); - std::shuffle(tmp2.begin(), tmp2.end(), rng); - std::vector tmp3(x->get_size()[0] / 10); - std::uniform_int_distribution row_dist(0, x->get_size()[0] - 1); - for (auto& i : tmp3) { - i = row_dist(rng); - } - rpermute_idxs = - std::unique_ptr(new Arr{ref, tmp.begin(), tmp.end()}); - cpermute_idxs = - std::unique_ptr(new Arr{ref, tmp2.begin(), tmp2.end()}); - rgather_idxs = - std::unique_ptr(new Arr{ref, tmp3.begin(), tmp3.end()}); - } - - template - std::unique_ptr convert(InputType&& input) - { - auto result = ConvertedType::create(input->get_executor()); - input->convert_to(result.get()); - return result; - } - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - std::default_random_engine rand_engine; - - std::unique_ptr x; - std::unique_ptr c_x; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - std::unique_ptr expected; - std::unique_ptr square; - std::unique_ptr dresult; - std::unique_ptr dx; - std::unique_ptr dc_x; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; - std::unique_ptr dsquare; - std::unique_ptr rpermute_idxs; - std::unique_ptr cpermute_idxs; - std::unique_ptr rgather_idxs; -}; - - -TEST_F(Dense, SingleVectorDpcppComputeDotIsEquivalentToRef) -{ - set_up_vector_data(1); - - x->compute_dot(y.get(), expected.get()); - dx->compute_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Dense, MultipleVectorDpcppComputeDotIsEquivalentToRef) -{ - set_up_vector_data(20); - - x->compute_dot(y.get(), expected.get()); - dx->compute_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Dense, SingleVectorDpcppComputeConjDotIsEquivalentToRef) -{ - set_up_vector_data(1); - - x->compute_conj_dot(y.get(), expected.get()); - dx->compute_conj_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Dense, MultipleVectorDpcppComputeConjDotIsEquivalentToRef) -{ - set_up_vector_data(20); - - x->compute_conj_dot(y.get(), expected.get()); - dx->compute_conj_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Dense, SingleVectorDpcppComputeNorm2IsEquivalentToRef) -{ - set_up_vector_data(1); - auto norm_size = gko::dim<2>{1, x->get_size()[1]}; - auto norm_expected = NormVector::create(this->ref, norm_size); - auto dnorm = NormVector::create(this->dpcpp, norm_size); - - x->compute_norm2(norm_expected.get()); - dx->compute_norm2(dnorm.get()); - - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); -} - - -TEST_F(Dense, MultipleVectorDpcppComputeNorm2IsEquivalentToRef) -{ - set_up_vector_data(20); - auto norm_size = gko::dim<2>{1, x->get_size()[1]}; - auto norm_expected = NormVector::create(this->ref, norm_size); - auto dnorm = NormVector::create(this->dpcpp, norm_size); - - x->compute_norm2(norm_expected.get()); - dx->compute_norm2(dnorm.get()); - - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); -} - - -TEST_F(Dense, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(y.get(), expected.get()); - dx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Dense, SimpleApplyMixedIsEquivalentToRef) -{ - SKIP_IF_SINGLE_MODE; - set_up_apply_data(); - - x->apply(convert(y).get(), convert(expected).get()); - dx->apply(convert(dy).get(), convert(dresult).get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7); -} - - -TEST_F(Dense, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Dense, AdvancedApplyMixedIsEquivalentToRef) -{ - SKIP_IF_SINGLE_MODE; - set_up_apply_data(); - - x->apply(convert(alpha).get(), convert(y).get(), - convert(beta).get(), convert(expected).get()); - dx->apply(convert(dalpha).get(), convert(dy).get(), - convert(dbeta).get(), convert(dresult).get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7); -} - - -TEST_F(Dense, ApplyToComplexIsEquivalentToRef) -{ - SKIP_IF_SINGLE_MODE; - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - x->apply(complex_b.get(), complex_x.get()); - dx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Dense, ApplyToMixedComplexIsEquivalentToRef) -{ - SKIP_IF_SINGLE_MODE; - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - x->apply(complex_b.get(), complex_x.get()); - dx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7); -} - - -TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - x->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Dense, AdvancedApplyToMixedComplexIsEquivalentToRef) -{ - SKIP_IF_SINGLE_MODE; - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - x->apply(convert(alpha).get(), complex_b.get(), - convert(beta).get(), complex_x.get()); - dx->apply(convert(dalpha).get(), dcomplex_b.get(), - convert(dbeta).get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7); -} - - -TEST_F(Dense, ComputeDotComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(1234, 2); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(1234, 2); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); - auto dresult = ComplexMtx::create(dpcpp, gko::dim<2>{1, 2}); - - complex_b->compute_dot(complex_x.get(), result.get()); - dcomplex_b->compute_dot(dcomplex_x.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(result, dresult, r::value); -} - - -TEST_F(Dense, ComputeConjDotComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(1234, 2); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(1234, 2); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); - auto dresult = ComplexMtx::create(dpcpp, gko::dim<2>{1, 2}); - - complex_b->compute_conj_dot(complex_x.get(), result.get()); - dcomplex_b->compute_conj_dot(dcomplex_x.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(result, dresult, r::value); -} - - -TEST_F(Dense, IsTransposable) -{ - set_up_apply_data(); - - auto trans = x->transpose(); - auto dtrans = dx->transpose(); - - GKO_ASSERT_MTX_NEAR(static_cast(dtrans.get()), - static_cast(trans.get()), 0); -} - - -TEST_F(Dense, IsConjugateTransposable) -{ - set_up_apply_data(); - - auto trans = c_x->conj_transpose(); - auto dtrans = dc_x->conj_transpose(); - - GKO_ASSERT_MTX_NEAR(static_cast(dtrans.get()), - static_cast(trans.get()), 0); -} - - -TEST_F(Dense, ConvertToCooIsEquivalentToRef) -{ - set_up_apply_data(); - auto coo_mtx = gko::matrix::Coo::create(ref); - auto dcoo_mtx = gko::matrix::Coo::create(dpcpp); - - x->convert_to(coo_mtx.get()); - dx->convert_to(dcoo_mtx.get()); - - ASSERT_EQ(dcoo_mtx->get_num_stored_elements(), - coo_mtx->get_num_stored_elements()); - GKO_ASSERT_MTX_NEAR(dcoo_mtx.get(), coo_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToCooIsEquivalentToRef) -{ - set_up_apply_data(); - auto coo_mtx = gko::matrix::Coo::create(ref); - auto dcoo_mtx = gko::matrix::Coo::create(dpcpp); - - x->move_to(coo_mtx.get()); - dx->move_to(dcoo_mtx.get()); - - ASSERT_EQ(dcoo_mtx->get_num_stored_elements(), - coo_mtx->get_num_stored_elements()); - GKO_ASSERT_MTX_NEAR(dcoo_mtx.get(), coo_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto csr_mtx = gko::matrix::Csr::create(ref); - auto dcsr_mtx = gko::matrix::Csr::create(dpcpp); - - x->convert_to(csr_mtx.get()); - dx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dcsr_mtx.get(), csr_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto csr_mtx = gko::matrix::Csr::create(ref); - auto dcsr_mtx = gko::matrix::Csr::create(dpcpp); - - x->move_to(csr_mtx.get()); - dx->move_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dcsr_mtx.get(), csr_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto sparsity_mtx = gko::matrix::SparsityCsr::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr::create(dpcpp); - - x->convert_to(sparsity_mtx.get()); - dx->convert_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto sparsity_mtx = gko::matrix::SparsityCsr::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr::create(dpcpp); - - x->move_to(sparsity_mtx.get()); - dx->move_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToEllIsEquivalentToRef) -{ - set_up_apply_data(); - auto ell_mtx = gko::matrix::Ell::create(ref); - auto dell_mtx = gko::matrix::Ell::create(dpcpp); - - x->convert_to(ell_mtx.get()); - dx->convert_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dell_mtx.get(), ell_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToEllIsEquivalentToRef) -{ - set_up_apply_data(); - auto ell_mtx = gko::matrix::Ell::create(ref); - auto dell_mtx = gko::matrix::Ell::create(dpcpp); - - x->move_to(ell_mtx.get()); - dx->move_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dell_mtx.get(), ell_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToSellpIsEquivalentToRef) -{ - set_up_apply_data(); - auto sellp_mtx = gko::matrix::Sellp::create(ref); - auto dsellp_mtx = gko::matrix::Sellp::create(dpcpp); - - x->convert_to(sellp_mtx.get()); - dx->convert_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx, dsellp_mtx, 0); -} - - -TEST_F(Dense, MoveToSellpIsEquivalentToRef) -{ - set_up_apply_data(); - auto sellp_mtx = gko::matrix::Sellp::create(ref); - auto dsellp_mtx = gko::matrix::Sellp::create(dpcpp); - - x->move_to(sellp_mtx.get()); - dx->move_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx, dsellp_mtx, 0); -} - - -TEST_F(Dense, ConvertsEmptyToSellp) -{ - auto dempty_mtx = Mtx::create(dpcpp); - auto dsellp_mtx = gko::matrix::Sellp::create(dpcpp); - - dempty_mtx->convert_to(dsellp_mtx.get()); - - ASSERT_EQ(dpcpp->copy_val_to_host(dsellp_mtx->get_const_slice_sets()), 0); - ASSERT_FALSE(dsellp_mtx->get_size()); -} - - -TEST_F(Dense, CalculateNNZPerRowIsEquivalentToRef) -{ - set_up_apply_data(); - gko::array nnz_per_row(ref); - nnz_per_row.resize_and_reset(x->get_size()[0]); - gko::array dnnz_per_row(dpcpp); - dnnz_per_row.resize_and_reset(dx->get_size()[0]); - - gko::kernels::reference::dense::count_nonzeros_per_row( - ref, x.get(), nnz_per_row.get_data()); - gko::kernels::dpcpp::dense::count_nonzeros_per_row(dpcpp, dx.get(), - dnnz_per_row.get_data()); - - auto tmp = gko::array(ref, dnnz_per_row); - for (gko::size_type i = 0; i < nnz_per_row.get_num_elems(); i++) { - ASSERT_EQ(nnz_per_row.get_const_data()[i], tmp.get_const_data()[i]); - } -} - - -TEST_F(Dense, ComputeMaxNNZPerRowIsEquivalentToRef) -{ - set_up_apply_data(); - gko::size_type max_nnz; - gko::size_type dmax_nnz; - - gko::kernels::reference::dense::compute_max_nnz_per_row(ref, x.get(), - max_nnz); - gko::kernels::dpcpp::dense::compute_max_nnz_per_row(dpcpp, dx.get(), - dmax_nnz); - - ASSERT_EQ(max_nnz, dmax_nnz); -} - - -} // namespace diff --git a/dpcpp/test/matrix/diagonal_kernels.cpp b/dpcpp/test/matrix/diagonal_kernels.cpp deleted file mode 100644 index 37a482dad31..00000000000 --- a/dpcpp/test/matrix/diagonal_kernels.cpp +++ /dev/null @@ -1,273 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include -#include -#include -#include - - -#include - - -#include -#include - - -#include "core/matrix/diagonal_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Diagonal : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using vtype = float; -#else - using vtype = double; -#endif // GINKGO_DPCPP_SINGLE_MODE - using ValueType = vtype; - using ComplexValueType = std::complex; - using Csr = gko::matrix::Csr; - using Diag = gko::matrix::Diagonal; - using Dense = gko::matrix::Dense; - using ComplexDiag = gko::matrix::Diagonal; - - Diagonal() -#ifdef GINKGO_FAST_TESTS - : mtx_size(152, 231), -#else - : mtx_size(532, 231), -#endif - rand_engine(42) - {} - - void SetUp() - { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution(0.0, 1.0), rand_engine, ref); - } - - std::unique_ptr gen_diag(int size) - { - auto diag = Diag::create(ref, size); - auto vals = diag->get_values(); - auto value_dist = std::normal_distribution(0.0, 1.0); - for (int i = 0; i < size; i++) { - vals[i] = gko::test::detail::get_rand_value(value_dist, - rand_engine); - } - return diag; - } - - std::unique_ptr gen_cdiag(int size) - { - auto cdiag = ComplexDiag::create(ref, size); - auto vals = cdiag->get_values(); - auto value_dist = std::normal_distribution(0.0, 1.0); - for (int i = 0; i < size; i++) { - vals[i] = ComplexValueType{ - gko::test::detail::get_rand_value( - value_dist, rand_engine)}; - } - return cdiag; - } - - void set_up_apply_data() - { - diag = gen_diag(mtx_size[0]); - ddiag = gko::clone(dpcpp, diag); - dense1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[1]); - dense2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[0]); - denseexpected1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[1]); - denseexpected2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[0]); - ddense1 = gko::clone(dpcpp, dense1); - ddense2 = gko::clone(dpcpp, dense2); - denseresult1 = gko::clone(dpcpp, denseexpected1); - denseresult2 = gko::clone(dpcpp, denseexpected2); - csr1 = gen_mtx(mtx_size[0], mtx_size[1], 1); - csr2 = gen_mtx(mtx_size[1], mtx_size[0], 1); - csrexpected1 = gen_mtx(mtx_size[0], mtx_size[1], 1); - csrexpected2 = gen_mtx(mtx_size[1], mtx_size[0], 1); - dcsr1 = gko::clone(dpcpp, csr1); - dcsr2 = gko::clone(dpcpp, csr2); - csrresult1 = gko::clone(dpcpp, csrexpected1); - csrresult2 = gko::clone(dpcpp, csrexpected2); - } - - void set_up_complex_data() - { - cdiag = gen_cdiag(mtx_size[0]); - dcdiag = gko::clone(dpcpp, cdiag); - } - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr diag; - std::unique_ptr ddiag; - std::unique_ptr cdiag; - std::unique_ptr dcdiag; - - std::unique_ptr dense1; - std::unique_ptr dense2; - std::unique_ptr denseexpected1; - std::unique_ptr denseexpected2; - std::unique_ptr denseresult1; - std::unique_ptr denseresult2; - std::unique_ptr ddense1; - std::unique_ptr ddense2; - std::unique_ptr csr1; - std::unique_ptr csr2; - std::unique_ptr dcsr1; - std::unique_ptr dcsr2; - std::unique_ptr csrexpected1; - std::unique_ptr csrexpected2; - std::unique_ptr csrresult1; - std::unique_ptr csrresult2; -}; - - -TEST_F(Diagonal, ApplyToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->apply(dense1.get(), denseexpected1.get()); - ddiag->apply(ddense1.get(), denseresult1.get()); - - GKO_ASSERT_MTX_NEAR(denseexpected1, denseresult1, r::value); -} - - -TEST_F(Diagonal, RightApplyToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->rapply(dense2.get(), denseexpected2.get()); - ddiag->rapply(ddense2.get(), denseresult2.get()); - - GKO_ASSERT_MTX_NEAR(denseexpected2, denseresult2, r::value); -} - - -TEST_F(Diagonal, ApplyToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->apply(csr1.get(), csrexpected1.get()); - ddiag->apply(dcsr1.get(), csrresult1.get()); - - GKO_ASSERT_MTX_NEAR(csrexpected1, csrresult1, r::value); -} - - -TEST_F(Diagonal, RightApplyToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->rapply(csr2.get(), csrexpected2.get()); - ddiag->rapply(dcsr2.get(), csrresult2.get()); - - GKO_ASSERT_MTX_NEAR(csrexpected2, csrresult2, r::value); -} - - -TEST_F(Diagonal, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->convert_to(csr1.get()); - ddiag->convert_to(dcsr1.get()); - - GKO_ASSERT_MTX_NEAR(csr1, dcsr1, 0); -} - - -TEST_F(Diagonal, ConjTransposeIsEquivalentToRef) -{ - set_up_complex_data(); - - auto trans = cdiag->conj_transpose(); - auto trans_diag = static_cast(trans.get()); - auto dtrans = dcdiag->conj_transpose(); - auto dtrans_diag = static_cast(dtrans.get()); - - GKO_ASSERT_MTX_NEAR(trans_diag, dtrans_diag, 0); -} - - -TEST_F(Diagonal, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->compute_absolute_inplace(); - ddiag->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(diag, ddiag, r::value); -} - - -TEST_F(Diagonal, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_diag = diag->compute_absolute(); - auto dabs_diag = ddiag->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_diag, dabs_diag, r::value); -} - - -} // namespace diff --git a/dpcpp/test/matrix/hybrid_kernels.cpp b/dpcpp/test/matrix/hybrid_kernels.cpp deleted file mode 100644 index cb9b883da47..00000000000 --- a/dpcpp/test/matrix/hybrid_kernels.cpp +++ /dev/null @@ -1,281 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/matrix/hybrid_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Hybrid : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using vtype = float; -#else - using vtype = double; -#endif // GINKGO_DPCPP_SINGLE_MODE - using Mtx = gko::matrix::Hybrid; - using Vec = gko::matrix::Dense; - using ComplexVec = gko::matrix::Dense>; - - Hybrid() : rand_engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data(int num_vectors = 1, - std::shared_ptr strategy = - std::make_shared()) - { - mtx = Mtx::create(ref, strategy); - mtx->copy_from(gen_mtx(532, 231, 1)); - expected = gen_mtx(532, num_vectors, 1); - y = gen_mtx(231, num_vectors, 1); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = Mtx::create(dpcpp, strategy); - dmtx->copy_from(mtx.get()); - dresult = gko::clone(dpcpp, expected); - dy = gko::clone(dpcpp, y); - dalpha = gko::clone(dpcpp, alpha); - dbeta = gko::clone(dpcpp, beta); - } - - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; -}; - - -TEST_F(Hybrid, SubMatrixExecutorAfterCopyIsEquivalentToExcutor) -{ - set_up_apply_data(); - - auto coo_mtx = dmtx->get_coo(); - auto ell_mtx = dmtx->get_ell(); - - ASSERT_EQ(coo_mtx->get_executor(), dpcpp); - ASSERT_EQ(ell_mtx->get_executor(), dpcpp); - ASSERT_EQ(dmtx->get_executor(), dpcpp); -} - - -TEST_F(Hybrid, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Hybrid, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Hybrid, SimpleApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Hybrid, AdvancedApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Hybrid, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Hybrid, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Hybrid, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(1, std::make_shared(2)); - auto csr_mtx = gko::matrix::Csr::create(ref); - auto dcsr_mtx = gko::matrix::Csr::create(dpcpp); - - mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Hybrid, MoveToCsrIsEquivalentToRef) -{ - set_up_apply_data(1, std::make_shared(2)); - auto csr_mtx = gko::matrix::Csr::create(ref); - auto dcsr_mtx = gko::matrix::Csr::create(dpcpp); - - mtx->move_to(csr_mtx.get()); - dmtx->move_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Hybrid, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Hybrid, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); -} - - -TEST_F(Hybrid, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, std::make_shared(2)); - using AbsMtx = gko::remove_complex; - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - auto abs_strategy = gko::as(abs_mtx->get_strategy()); - auto dabs_strategy = - gko::as(dabs_mtx->get_strategy()); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); - GKO_ASSERT_EQ(abs_strategy->get_num_columns(), - dabs_strategy->get_num_columns()); - GKO_ASSERT_EQ(abs_strategy->get_num_columns(), 2); -} - - -} // namespace diff --git a/dpcpp/test/matrix/sellp_kernels.cpp b/dpcpp/test/matrix/sellp_kernels.cpp deleted file mode 100644 index 927346b3798..00000000000 --- a/dpcpp/test/matrix/sellp_kernels.cpp +++ /dev/null @@ -1,344 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/matrix/sellp_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Sellp : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using vtype = float; -#else - using vtype = double; -#endif // GINKGO_DPCPP_SINGLE_MODE - using Mtx = gko::matrix::Sellp; - using Vec = gko::matrix::Dense; - using ComplexVec = gko::matrix::Dense>; - - Sellp() : rand_engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_matrix( - int total_cols = 1, int slice_size = gko::matrix::default_slice_size, - int stride_factor = gko::matrix::default_stride_factor) - { - mtx = gen_mtx(532, 231); - empty = Mtx::create(ref); - expected = gen_mtx(532, total_cols); - y = gen_mtx(231, total_cols); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = gko::clone(dpcpp, mtx); - dempty = Mtx::create(dpcpp); - dresult = gko::clone(dpcpp, expected); - dy = gko::clone(dpcpp, y); - dalpha = gko::clone(dpcpp, alpha); - dbeta = gko::clone(dpcpp, beta); - } - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr empty; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dempty; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; -}; - - -TEST_F(Sellp, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_matrix(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Sellp, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_matrix(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Sellp, SimpleApplyWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(1, 32, 2); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Sellp, AdvancedApplyWithSliceSizeAndStrideFActorIsEquivalentToRef) -{ - set_up_apply_matrix(1, 32, 2); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Sellp, SimpleApplyMultipleRHSIsEquivalentToRef) -{ - set_up_apply_matrix(64); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Sellp, AdvancedApplyMultipleRHSIsEquivalentToRef) -{ - set_up_apply_matrix(64); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Sellp, - SimpleApplyMultipleRHSWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(32, 2); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Sellp, - AdvancedApplyMultipleRHSWithSliceSizeAndStrideFActorIsEquivalentToRef) -{ - set_up_apply_matrix(32, 2); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); -} - - -TEST_F(Sellp, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Sellp, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(dpcpp, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(dpcpp, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); -} - - -TEST_F(Sellp, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto dense_mtx = gko::matrix::Dense::create(ref); - auto ddense_mtx = gko::matrix::Dense::create(dpcpp); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Sellp, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto csr_mtx = gko::matrix::Csr::create(ref); - auto dcsr_mtx = gko::matrix::Csr::create(dpcpp); - - mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Sellp, ConvertEmptyToDenseIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto dense_mtx = gko::matrix::Dense::create(ref); - auto ddense_mtx = gko::matrix::Dense::create(dpcpp); - - empty->convert_to(dense_mtx.get()); - dempty->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Sellp, ConvertEmptyToCsrIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto csr_mtx = gko::matrix::Csr::create(ref); - auto dcsr_mtx = gko::matrix::Csr::create(dpcpp); - - empty->convert_to(csr_mtx.get()); - dempty->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Sellp, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_matrix(64); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Sellp, ExtractDiagonalWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(64, 32, 2); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Sellp, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_matrix(64, 32, 2); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); -} - - -TEST_F(Sellp, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_matrix(64, 32, 2); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); -} - - -} // namespace diff --git a/dpcpp/test/preconditioner/CMakeLists.txt b/dpcpp/test/preconditioner/CMakeLists.txt deleted file mode 100644 index 5f5c09e3129..00000000000 --- a/dpcpp/test/preconditioner/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -ginkgo_create_test(isai_kernels) \ No newline at end of file diff --git a/dpcpp/test/preconditioner/isai_kernels.cpp b/dpcpp/test/preconditioner/isai_kernels.cpp deleted file mode 100644 index a063f0bf334..00000000000 --- a/dpcpp/test/preconditioner/isai_kernels.cpp +++ /dev/null @@ -1,683 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/preconditioner/isai_kernels.hpp" -#include "core/test/utils.hpp" -#include "dpcpp/base/config.hpp" -#include "matrices/config.hpp" - - -namespace { - - -enum struct matrix_type { lower, upper, general, spd }; - - -class Isai : public ::testing::Test { -protected: -#if GINKGO_DPCPP_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = gko::int32; - using Csr = gko::matrix::Csr; - using Dense = gko::matrix::Dense; - Isai() : rand_engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - std::unique_ptr clone_allocations(const Csr* csr_mtx) - { - if (csr_mtx->get_executor() != ref) { - return {nullptr}; - } - const auto num_elems = csr_mtx->get_num_stored_elements(); - auto sparsity = csr_mtx->clone(); - - // values are now filled with invalid data to catch potential errors - auto begin_values = sparsity->get_values(); - auto end_values = begin_values + num_elems; - std::fill(begin_values, end_values, -gko::one()); - return sparsity; - } - - void initialize_data(matrix_type type, gko::size_type n, - gko::size_type row_limit) - { - const bool for_lower_tm = type == matrix_type::lower; - auto nz_dist = std::uniform_int_distribution(1, row_limit); - auto val_dist = std::uniform_real_distribution(-1., 1.); - mtx = Csr::create(ref); - if (type == matrix_type::general) { - auto dense_mtx = gko::test::generate_random_matrix( - n, n, nz_dist, val_dist, rand_engine, ref, gko::dim<2>{n, n}); - ensure_diagonal(dense_mtx.get()); - mtx->copy_from(dense_mtx.get()); - } else if (type == matrix_type::spd) { - auto dense_mtx = gko::test::generate_random_band_matrix( - n, row_limit / 4, row_limit / 4, val_dist, rand_engine, ref, - gko::dim<2>{n, n}); - auto transp = gko::as(dense_mtx->transpose()); - auto spd_mtx = Dense::create(ref, gko::dim<2>{n, n}); - dense_mtx->apply(transp.get(), spd_mtx.get()); - mtx->copy_from(spd_mtx.get()); - } else { - mtx = gko::test::generate_random_triangular_matrix( - n, true, for_lower_tm, nz_dist, val_dist, rand_engine, ref, - gko::dim<2>{n, n}); - } - inverse = clone_allocations(mtx.get()); - - d_mtx = gko::clone(dpcpp, mtx); - d_inverse = gko::clone(dpcpp, inverse); - } - - template - std::unique_ptr read(const char* name) - { - std::ifstream mtxstream{std::string{gko::matrices::location_isai_mtxs} + - name}; - auto result = gko::read(mtxstream, ref); - // to avoid removing 0s, the matrices store 12345 instead - for (gko::size_type i = 0; i < result->get_num_stored_elements(); ++i) { - auto& val = result->get_values()[i]; - if (val == static_cast(12345.0)) { - val = 0; - } - } - return std::move(result); - } - - void ensure_diagonal(Dense* mtx) - { - for (int i = 0; i < mtx->get_size()[0]; ++i) { - mtx->at(i, i) = gko::one(); - } - } - - - std::shared_ptr ref; - std::shared_ptr dpcpp; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr inverse; - - std::unique_ptr d_mtx; - std::unique_ptr d_inverse; -}; - - -TEST_F(Isai, DpcppIsaiGenerateLinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 536, 31); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(dpcpp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::dpcpp::isai::generate_tri_inverse( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateUinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 615, 31); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(dpcpp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::dpcpp::isai::generate_tri_inverse( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateAinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 615, 15); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(dpcpp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::dpcpp::isai::generate_general_inverse( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateSpdinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 15); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(dpcpp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::dpcpp::isai::generate_general_inverse( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 15 * r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateLinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 554, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(dpcpp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::dpcpp::isai::generate_tri_inverse( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateUinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 695, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(dpcpp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::dpcpp::isai::generate_tri_inverse( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateAinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 695, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(dpcpp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::dpcpp::isai::generate_general_inverse( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 30 * r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateSpdinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(dpcpp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::dpcpp::isai::generate_general_inverse( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 10 * r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateExcessLinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 518, 40); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(dpcpp, a1); - gko::array da2(dpcpp, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(dpcpp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(dpcpp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::dpcpp::isai::generate_excess_system( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateExcessUinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 673, 51); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(dpcpp, a1); - gko::array da2(dpcpp, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(dpcpp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(dpcpp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::dpcpp::isai::generate_excess_system( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateExcessAinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 100, 51); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(dpcpp, a1); - gko::array da2(dpcpp, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(dpcpp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(dpcpp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::dpcpp::isai::generate_excess_system( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, DpcppIsaiGenerateExcessSpdinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(dpcpp, a1); - gko::array da2(dpcpp, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(dpcpp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(dpcpp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::dpcpp::isai::generate_excess_system( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, DpcppIsaiGeneratePartialExcessIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(dpcpp, a1); - gko::array da2(dpcpp, a2); - auto e_dim = a1.get_data()[10] - a1.get_data()[5]; - auto e_nnz = a2.get_data()[10] - a2.get_data()[5]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(dpcpp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(dpcpp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 5u, 10u); - gko::kernels::dpcpp::isai::generate_excess_system( - dpcpp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 5u, 10u); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, DpcppIsaiScaleExcessSolutionIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(dpcpp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(dpcpp, e_rhs); - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scale_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), 0, num_rows); - gko::kernels::dpcpp::isai::scale_excess_solution( - dpcpp, da1.get_const_data(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); -} - - -TEST_F(Isai, DpcppIsaiScalePartialExcessSolutionIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(dpcpp, a1); - auto e_dim = a1.get_data()[10] - a1.get_data()[5]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(dpcpp, e_rhs); - - gko::kernels::reference::isai::scale_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), 5u, 10u); - gko::kernels::dpcpp::isai::scale_excess_solution( - dpcpp, da1.get_const_data(), de_rhs.get(), 5u, 10u); - - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); -} - - -TEST_F(Isai, DpcppIsaiScatterExcessSolutionLIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 572, 52); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(dpcpp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(dpcpp, e_rhs); - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::dpcpp::isai::scatter_excess_solution( - dpcpp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, - num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, DpcppIsaiScatterExcessSolutionUIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 702, 45); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(dpcpp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(dpcpp, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::dpcpp::isai::scatter_excess_solution( - dpcpp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, - num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, DpcppIsaiScatterExcessSolutionAIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 702, 45); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(dpcpp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(dpcpp, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::dpcpp::isai::scatter_excess_solution( - dpcpp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, - num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, DpcppIsaiScatterExcessSolutionSpdIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(dpcpp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(dpcpp, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::dpcpp::isai::scatter_excess_solution( - dpcpp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, - num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, DpcppIsaiScatterPartialExcessSolutionIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(dpcpp, a1); - auto e_dim = a1.get_data()[10] - a1.get_data()[5]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(dpcpp, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 5u, 10u); - gko::kernels::dpcpp::isai::scatter_excess_solution( - dpcpp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 5u, 10u); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -} // namespace diff --git a/dpcpp/test/solver/CMakeLists.txt b/dpcpp/test/solver/CMakeLists.txt deleted file mode 100644 index b4f0e4059ca..00000000000 --- a/dpcpp/test/solver/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -ginkgo_create_test(gmres_kernels) -ginkgo_create_test(cb_gmres_kernels) -ginkgo_create_test(idr_kernels) diff --git a/dpcpp/test/stop/CMakeLists.txt b/dpcpp/test/stop/CMakeLists.txt deleted file mode 100644 index 0ba0781e077..00000000000 --- a/dpcpp/test/stop/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -ginkgo_create_test(criterion_kernels) -ginkgo_create_test(residual_norm_kernels) diff --git a/dpcpp/test/stop/criterion_kernels.cpp b/dpcpp/test/stop/criterion_kernels.cpp deleted file mode 100644 index fb298bcebaa..00000000000 --- a/dpcpp/test/stop/criterion_kernels.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -namespace { - - -constexpr gko::size_type test_iterations = 10; - - -class Criterion : public ::testing::Test { -protected: - Criterion() - { - ref_ = gko::ReferenceExecutor::create(); - dpcpp_ = gko::DpcppExecutor::create(0, ref_); - // Actually use an iteration stopping criterion because Criterion is an - // abstract class - factory_ = gko::stop::Iteration::build() - .with_max_iters(test_iterations) - .on(dpcpp_); - } - - std::unique_ptr factory_; - std::shared_ptr ref_; - std::shared_ptr dpcpp_; -}; - - -TEST_F(Criterion, SetsOneStopStatus) -{ - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - auto criterion = factory_->generate(nullptr, nullptr, nullptr); - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - - stop_status.set_executor(dpcpp_); - criterion->update() - .num_iterations(test_iterations) - .check(RelativeStoppingId, true, &stop_status, &one_changed); - stop_status.set_executor(ref_); - - ASSERT_EQ(stop_status.get_data()[0].has_stopped(), true); -} - - -TEST_F(Criterion, SetsMultipleStopStatuses) -{ - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - auto criterion = factory_->generate(nullptr, nullptr, nullptr); - gko::array stop_status(ref_, 3); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.get_data()[2].reset(); - - stop_status.set_executor(dpcpp_); - criterion->update() - .num_iterations(test_iterations) - .check(RelativeStoppingId, true, &stop_status, &one_changed); - stop_status.set_executor(ref_); - - ASSERT_EQ(stop_status.get_data()[0].has_stopped(), true); - ASSERT_EQ(stop_status.get_data()[1].has_stopped(), true); - ASSERT_EQ(stop_status.get_data()[2].has_stopped(), true); -} - - -} // namespace diff --git a/dpcpp/test/stop/residual_norm_kernels.cpp b/dpcpp/test/stop/residual_norm_kernels.cpp deleted file mode 100644 index be4bb979022..00000000000 --- a/dpcpp/test/stop/residual_norm_kernels.cpp +++ /dev/null @@ -1,788 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include "core/test/utils.hpp" - - -namespace { - - -#ifdef GINKGO_DPCPP_SINGLE_MODE -using value_type = float; -constexpr auto tol = r::value; -#else -using value_type = double; -constexpr value_type tol = r::value; -#endif - - -class ResidualNorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense; - using NormVector = gko::remove_complex; - - ResidualNorm() - { - ref_ = gko::ReferenceExecutor::create(); - dpcpp_ = gko::DpcppExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm::build() - .with_reduction_factor(tol) - .on(dpcpp_); - rel_factory_ = gko::stop::ResidualNorm::build() - .with_reduction_factor(tol) - .with_baseline(gko::stop::mode::initial_resnorm) - .on(dpcpp_); - abs_factory_ = gko::stop::ResidualNorm::build() - .with_reduction_factor(tol) - .with_baseline(gko::stop::mode::absolute) - .on(dpcpp_); - } - - std::unique_ptr::Factory> factory_; - std::unique_ptr::Factory> rel_factory_; - std::unique_ptr::Factory> abs_factory_; - std::shared_ptr dpcpp_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNorm, WaitsTillResidualGoalForRhsResNorm) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - auto rhs_norm = gko::initialize({0.0}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalMultipleRHSForRhsResNorm) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - auto rhs_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * rhs_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * rhs_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalForRelResNorm) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = - rel_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalMultipleRHSForRelResNorm) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = - rel_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * res_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * res_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalForAbsResNorm) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = - abs_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalMultipleRHSForAbsResNorm) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = - abs_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ResidualNormWithInitialResnorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense; - using NormVector = gko::matrix::Dense>; - - ResidualNormWithInitialResnorm() - { - ref_ = gko::ReferenceExecutor::create(); - dpcpp_ = gko::DpcppExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm::build() - .with_baseline(gko::stop::mode::initial_resnorm) - .with_reduction_factor(tol) - .on(dpcpp_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr dpcpp_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNormWithInitialResnorm, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNormWithInitialResnorm, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * res_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * res_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ResidualNormWithRhsNorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense; - using NormVector = gko::matrix::Dense>; - - ResidualNormWithRhsNorm() - { - ref_ = gko::ReferenceExecutor::create(); - dpcpp_ = gko::DpcppExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm::build() - .with_baseline(gko::stop::mode::rhs_norm) - .with_reduction_factor(tol) - .on(dpcpp_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr dpcpp_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNormWithRhsNorm, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - auto rhs_norm = gko::initialize({0.0}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNormWithRhsNorm, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - auto rhs_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * rhs_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * rhs_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ImplicitResidualNorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense; - using NormVector = gko::matrix::Dense>; - - ImplicitResidualNorm() - { - ref_ = gko::ReferenceExecutor::create(); - dpcpp_ = gko::DpcppExecutor::create(0, ref_); - factory_ = gko::stop::ImplicitResidualNorm::build() - .with_reduction_factor(tol) - .on(dpcpp_); - } - - std::unique_ptr::Factory> - factory_; - std::shared_ptr dpcpp_; - std::shared_ptr ref_; -}; - - -TEST_F(ImplicitResidualNorm, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - auto rhs_norm = gko::initialize({0.0}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = std::pow(tol * 1.1 * rhs_norm->at(0), 2); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_FALSE(one_changed); - - res->at(0) = std::pow(tol * 0.9 * rhs_norm->at(0), 2); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ImplicitResidualNorm, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - auto rhs_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = std::pow(tol * 0.9 * rhs_norm->at(0, 0), 2); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = std::pow(tol * 0.9 * rhs_norm->at(0, 1), 2); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ResidualNormWithAbsolute : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense; - using NormVector = gko::matrix::Dense>; - - ResidualNormWithAbsolute() - { - ref_ = gko::ReferenceExecutor::create(); - dpcpp_ = gko::DpcppExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm::build() - .with_baseline(gko::stop::mode::absolute) - .with_reduction_factor(tol) - .on(dpcpp_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr dpcpp_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNormWithAbsolute, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNormWithAbsolute, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto d_res = gko::clone(dpcpp_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(dpcpp_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(dpcpp_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(dpcpp_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -} // namespace diff --git a/hip/test/CMakeLists.txt b/hip/test/CMakeLists.txt index 9c8e39ca4ab..f046f9b86ca 100644 --- a/hip/test/CMakeLists.txt +++ b/hip/test/CMakeLists.txt @@ -2,9 +2,6 @@ include(${PROJECT_SOURCE_DIR}/cmake/create_test.cmake) add_subdirectory(base) add_subdirectory(components) -add_subdirectory(factorization) add_subdirectory(matrix) add_subdirectory(solver) -add_subdirectory(preconditioner) -add_subdirectory(stop) add_subdirectory(utils) diff --git a/hip/test/components/CMakeLists.txt b/hip/test/components/CMakeLists.txt index fc723bf9e87..9bbd49608a7 100644 --- a/hip/test/components/CMakeLists.txt +++ b/hip/test/components/CMakeLists.txt @@ -1,5 +1,4 @@ ginkgo_create_hip_test(cooperative_groups) ginkgo_create_hip_test(merging) -ginkgo_create_hip_test(prefix_sum_kernels) ginkgo_create_hip_test(searching) ginkgo_create_hip_test(sorting) diff --git a/hip/test/components/prefix_sum_kernels.hip.cpp b/hip/test/components/prefix_sum_kernels.hip.cpp deleted file mode 100644 index 2fc1fab377f..00000000000 --- a/hip/test/components/prefix_sum_kernels.hip.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/components/prefix_sum_kernels.hpp" - - -#include -#include -#include - - -#include - - -#include - - -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class PrefixSum : public ::testing::Test { -protected: - using index_type = gko::int32; - PrefixSum() - : ref(gko::ReferenceExecutor::create()), - exec(gko::HipExecutor::create(0, ref)), - rand(293), - total_size(42793), - vals(ref, total_size), - dvals(exec) - { - std::uniform_int_distribution dist(0, 1000); - for (gko::size_type i = 0; i < total_size; ++i) { - vals.get_data()[i] = dist(rand); - } - dvals = vals; - } - - void test(gko::size_type size) - { - gko::kernels::reference::components::prefix_sum(ref, vals.get_data(), - size); - gko::kernels::hip::components::prefix_sum(exec, dvals.get_data(), size); - - GKO_ASSERT_ARRAY_EQ(vals, dvals); - } - - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand; - gko::size_type total_size; - gko::array vals; - gko::array dvals; -}; - - -TEST_F(PrefixSum, SmallEqualsReference) { test(100); } - - -TEST_F(PrefixSum, BigEqualsReference) { test(total_size); } - - -} // namespace diff --git a/hip/test/factorization/CMakeLists.txt b/hip/test/factorization/CMakeLists.txt deleted file mode 100644 index 998601d6b64..00000000000 --- a/hip/test/factorization/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -ginkgo_create_test(ic_kernels) -ginkgo_create_test(ilu_kernels) -ginkgo_create_hip_test(par_ic_kernels) -ginkgo_create_hip_test(par_ict_kernels) -ginkgo_create_hip_test(par_ilu_kernels) -ginkgo_create_hip_test(par_ilut_kernels) diff --git a/hip/test/factorization/ic_kernels.cpp b/hip/test/factorization/ic_kernels.cpp deleted file mode 100644 index 2d2ab42f0e2..00000000000 --- a/hip/test/factorization/ic_kernels.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include -#include -#include - - -#include - - -#include -#include - - -#include "core/test/utils/unsort_matrix.hpp" -#include "hip/test/utils.hip.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class Ic : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Csr = gko::matrix::Csr; - - std::shared_ptr ref; - std::shared_ptr hip; - std::default_random_engine rand_engine; - std::shared_ptr csr_ref; - std::shared_ptr csr_hip; - - Ic() - : ref(gko::ReferenceExecutor::create()), - hip(gko::HipExecutor::create(0, ref)), - rand_engine(6794) - {} - - void SetUp() override - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - csr_ref = gko::read(input_file, ref); - csr_hip = gko::clone(hip, csr_ref); - } -}; - - -TEST_F(Ic, ComputeICIsEquivalentToRefSorted) -{ - auto ref_fact = gko::factorization::ParIc<>::build() - .with_skip_sorting(true) - .on(ref) - ->generate(csr_ref); - auto hip_fact = gko::factorization::Ic<>::build() - .with_skip_sorting(true) - .on(hip) - ->generate(csr_hip); - - GKO_ASSERT_MTX_NEAR(ref_fact->get_l_factor(), hip_fact->get_l_factor(), - 1e-14); - GKO_ASSERT_MTX_NEAR(ref_fact->get_lt_factor(), hip_fact->get_lt_factor(), - 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_l_factor(), - hip_fact->get_l_factor()); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_lt_factor(), - hip_fact->get_lt_factor()); -} - - -TEST_F(Ic, ComputeICIsEquivalentToRefUnsorted) -{ - gko::test::unsort_matrix(gko::lend(csr_ref), rand_engine); - csr_hip->copy_from(gko::lend(csr_ref)); - - auto ref_fact = - gko::factorization::ParIc<>::build().on(ref)->generate(csr_ref); - auto hip_fact = - gko::factorization::Ic<>::build().on(hip)->generate(csr_hip); - - GKO_ASSERT_MTX_NEAR(ref_fact->get_l_factor(), hip_fact->get_l_factor(), - 1e-14); - GKO_ASSERT_MTX_NEAR(ref_fact->get_lt_factor(), hip_fact->get_lt_factor(), - 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_l_factor(), - hip_fact->get_l_factor()); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_lt_factor(), - hip_fact->get_lt_factor()); -} - - -TEST_F(Ic, SetsCorrectStrategy) -{ - auto hip_fact = gko::factorization::Ic<>::build() - .with_l_strategy(std::make_shared()) - .on(hip) - ->generate(csr_hip); - - ASSERT_EQ(hip_fact->get_l_factor()->get_strategy()->get_name(), - "merge_path"); - ASSERT_EQ(hip_fact->get_lt_factor()->get_strategy()->get_name(), - "merge_path"); -} - - -} // namespace diff --git a/hip/test/factorization/ilu_kernels.cpp b/hip/test/factorization/ilu_kernels.cpp deleted file mode 100644 index 567dbb508ad..00000000000 --- a/hip/test/factorization/ilu_kernels.cpp +++ /dev/null @@ -1,148 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include -#include -#include - - -#include - - -#include -#include - - -#include "core/test/utils/unsort_matrix.hpp" -#include "hip/test/utils.hip.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class Ilu : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Csr = gko::matrix::Csr; - - std::shared_ptr ref; - std::shared_ptr hip; - std::default_random_engine rand_engine; - std::shared_ptr csr_ref; - std::shared_ptr csr_hip; - - Ilu() - : ref(gko::ReferenceExecutor::create()), - hip(gko::HipExecutor::create(0, ref)), - rand_engine(1337) - {} - - void SetUp() override - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - csr_ref = gko::read(input_file, ref); - csr_hip = gko::clone(hip, csr_ref); - } -}; - - -TEST_F(Ilu, ComputeILUIsEquivalentToRefSorted) -{ - auto ref_fact = gko::factorization::ParIlu<>::build() - .with_skip_sorting(true) - .on(ref) - ->generate(csr_ref); - auto hip_fact = gko::factorization::Ilu<>::build() - .with_skip_sorting(true) - .on(hip) - ->generate(csr_hip); - - GKO_ASSERT_MTX_NEAR(ref_fact->get_l_factor(), hip_fact->get_l_factor(), - 1e-14); - GKO_ASSERT_MTX_NEAR(ref_fact->get_u_factor(), hip_fact->get_u_factor(), - 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_l_factor(), - hip_fact->get_l_factor()); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_u_factor(), - hip_fact->get_u_factor()); -} - - -TEST_F(Ilu, ComputeILUIsEquivalentToRefUnsorted) -{ - gko::test::unsort_matrix(gko::lend(csr_ref), rand_engine); - csr_hip->copy_from(gko::lend(csr_ref)); - - auto ref_fact = - gko::factorization::ParIlu<>::build().on(ref)->generate(csr_ref); - auto hip_fact = - gko::factorization::Ilu<>::build().on(hip)->generate(csr_hip); - - GKO_ASSERT_MTX_NEAR(ref_fact->get_l_factor(), hip_fact->get_l_factor(), - 1e-14); - GKO_ASSERT_MTX_NEAR(ref_fact->get_u_factor(), hip_fact->get_u_factor(), - 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_l_factor(), - hip_fact->get_l_factor()); - GKO_ASSERT_MTX_EQ_SPARSITY(ref_fact->get_u_factor(), - hip_fact->get_u_factor()); -} - - -TEST_F(Ilu, SetsCorrectStrategy) -{ - auto hip_fact = - gko::factorization::Ilu<>::build() - .with_l_strategy(std::make_shared()) - .with_u_strategy(std::make_shared(hip)) - .on(hip) - ->generate(csr_hip); - - ASSERT_EQ(hip_fact->get_l_factor()->get_strategy()->get_name(), - "merge_path"); - ASSERT_EQ(hip_fact->get_u_factor()->get_strategy()->get_name(), - "load_balance"); -} - - -} // namespace diff --git a/hip/test/factorization/par_ic_kernels.hip.cpp b/hip/test/factorization/par_ic_kernels.hip.cpp deleted file mode 100644 index 205dd4d6ebf..00000000000 --- a/hip/test/factorization/par_ic_kernels.hip.cpp +++ /dev/null @@ -1,165 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ic_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/matrix/csr_builder.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "hip/test/utils.hip.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIc : public ::testing::Test { -protected: - using value_type = double; - using index_type = gko::int32; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - ParIc() - : mtx_size(585, 585), - rand_engine(10667), - ref(gko::ReferenceExecutor::create()), - hip(gko::HipExecutor::create(0, gko::ReferenceExecutor::create())) - { - mtx_l = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution(10, mtx_size[0]), - std::normal_distribution>(0, 10.0), - rand_engine, ref); - - dmtx_ani = Csr::create(hip); - dmtx_l_ani = Csr::create(hip); - dmtx_l_ani_init = Csr::create(hip); - dmtx_l = gko::clone(hip, mtx_l); - } - - void SetUp() - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - mtx_ani = gko::read(input_file, ref); - mtx_ani->sort_by_column_index(); - - { - mtx_l_ani = Csr::create(ref, mtx_ani->get_size()); - gko::matrix::CsrBuilder l_builder( - lend(mtx_l_ani)); - gko::kernels::reference::factorization::initialize_row_ptrs_l( - ref, lend(mtx_ani), mtx_l_ani->get_row_ptrs()); - auto l_nnz = - mtx_l_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - l_builder.get_col_idx_array().resize_and_reset(l_nnz); - l_builder.get_value_array().resize_and_reset(l_nnz); - gko::kernels::reference::factorization::initialize_l( - ref, lend(mtx_ani), lend(mtx_l_ani), false); - mtx_l_ani_init = gko::clone(ref, mtx_l_ani); - gko::kernels::reference::par_ic_factorization::init_factor( - ref, lend(mtx_l_ani_init)); - } - dmtx_ani->copy_from(lend(mtx_ani)); - dmtx_l_ani->copy_from(lend(mtx_l_ani)); - dmtx_l_ani_init->copy_from(lend(mtx_l_ani_init)); - } - - std::shared_ptr ref; - std::shared_ptr hip; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx_l; - std::unique_ptr mtx_ani; - std::unique_ptr mtx_l_ani; - std::unique_ptr mtx_l_ani_init; - - std::unique_ptr dmtx_l; - std::unique_ptr dmtx_ani; - std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx_l_ani_init; -}; - - -TEST_F(ParIc, KernelInitFactorIsEquivalentToRef) -{ - gko::kernels::reference::par_ic_factorization::init_factor(ref, - lend(mtx_l)); - gko::kernels::hip::par_ic_factorization::init_factor(hip, lend(dmtx_l)); - - GKO_ASSERT_MTX_NEAR(mtx_l, dmtx_l, r::value); -} - - -TEST_F(ParIc, KernelComputeFactorIsEquivalentToRef) -{ - auto square_size = mtx_ani->get_size(); - auto mtx_l_coo = Coo::create(ref, square_size); - mtx_l_ani->convert_to(lend(mtx_l_coo)); - auto dmtx_l_coo = Coo::create(hip, square_size); - dmtx_l_coo->copy_from(lend(mtx_l_coo)); - - gko::kernels::reference::par_ic_factorization::compute_factor( - ref, 1, lend(mtx_l_coo), lend(mtx_l_ani_init)); - gko::kernels::hip::par_ic_factorization::compute_factor( - hip, 100, lend(dmtx_l_coo), lend(dmtx_l_ani_init)); - - GKO_ASSERT_MTX_NEAR(mtx_l_ani_init, dmtx_l_ani_init, 1e-4); -} - - -} // namespace diff --git a/hip/test/factorization/par_ict_kernels.hip.cpp b/hip/test/factorization/par_ict_kernels.hip.cpp deleted file mode 100644 index e1e73a3c936..00000000000 --- a/hip/test/factorization/par_ict_kernels.hip.cpp +++ /dev/null @@ -1,174 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ict_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/matrix/csr_builder.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "hip/test/utils.hip.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIct : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - ParIct() - : mtx_size(500, 500), - rand_engine(6780), - ref(gko::ReferenceExecutor::create()), - hip(gko::HipExecutor::create(0, ref)) - { - mtx = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[1], - std::uniform_int_distribution<>(10, mtx_size[1]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - - dmtx_ani = Csr::create(hip); - dmtx_l_ani = Csr::create(hip); - dmtx = gko::clone(hip, mtx); - dmtx_l = gko::clone(hip, mtx_l); - } - - void SetUp() - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - mtx_ani = gko::read(input_file, ref); - mtx_ani->sort_by_column_index(); - - { - mtx_l_ani = Csr::create(ref, mtx_ani->get_size()); - gko::matrix::CsrBuilder l_builder( - lend(mtx_l_ani)); - gko::kernels::reference::factorization::initialize_row_ptrs_l( - ref, lend(mtx_ani), mtx_l_ani->get_row_ptrs()); - auto l_nnz = - mtx_l_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - l_builder.get_col_idx_array().resize_and_reset(l_nnz); - l_builder.get_value_array().resize_and_reset(l_nnz); - gko::kernels::reference::factorization::initialize_l( - ref, lend(mtx_ani), lend(mtx_l_ani), true); - } - dmtx_ani->copy_from(lend(mtx_ani)); - dmtx_l_ani->copy_from(lend(mtx_l_ani)); - } - - std::shared_ptr ref; - std::shared_ptr hip; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr mtx_ani; - std::unique_ptr mtx_l_ani; - std::unique_ptr mtx_l; - - std::unique_ptr dmtx; - std::unique_ptr dmtx_ani; - std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx_l; -}; - - -TEST_F(ParIct, KernelAddCandidatesIsEquivalentToRef) -{ - auto mtx_llh = Csr::create(ref, mtx_size); - mtx_l->apply(lend(mtx_l->transpose()), lend(mtx_llh)); - auto dmtx_llh = Csr::create(hip, mtx_size); - dmtx_llh->copy_from(lend(mtx_llh)); - auto res_mtx_l = Csr::create(ref, mtx_size); - auto dres_mtx_l = Csr::create(hip, mtx_size); - - gko::kernels::reference::par_ict_factorization::add_candidates( - ref, lend(mtx_llh), lend(mtx), lend(mtx_l), lend(res_mtx_l)); - gko::kernels::hip::par_ict_factorization::add_candidates( - hip, lend(dmtx_llh), lend(dmtx), lend(dmtx_l), lend(dres_mtx_l)); - - GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_l, dres_mtx_l); - GKO_ASSERT_MTX_NEAR(res_mtx_l, dres_mtx_l, 1e-14); -} - - -TEST_F(ParIct, KernelComputeFactorIsEquivalentToRef) -{ - auto square_size = mtx_ani->get_size(); - auto mtx_l_coo = Coo::create(ref, square_size); - mtx_l_ani->convert_to(lend(mtx_l_coo)); - auto dmtx_l_coo = Coo::create(hip, square_size); - dmtx_l_coo->copy_from(lend(mtx_l_coo)); - - gko::kernels::reference::par_ict_factorization::compute_factor( - ref, lend(mtx_ani), lend(mtx_l_ani), lend(mtx_l_coo)); - for (int i = 0; i < 20; ++i) { - gko::kernels::hip::par_ict_factorization::compute_factor( - hip, lend(dmtx_ani), lend(dmtx_l_ani), lend(dmtx_l_coo)); - } - - GKO_ASSERT_MTX_NEAR(mtx_l_ani, dmtx_l_ani, 1e-2); -} - - -} // namespace diff --git a/hip/test/factorization/par_ilu_kernels.hip.cpp b/hip/test/factorization/par_ilu_kernels.hip.cpp deleted file mode 100644 index b56895bccdd..00000000000 --- a/hip/test/factorization/par_ilu_kernels.hip.cpp +++ /dev/null @@ -1,345 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ilu_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "hip/test/utils.hip.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIlu : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Dense = gko::matrix::Dense; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - std::default_random_engine rand_engine; - std::shared_ptr ref; - std::shared_ptr hip; - std::shared_ptr csr_ref; - std::shared_ptr csr_hip; - - ParIlu() - : rand_engine(19), - ref(gko::ReferenceExecutor::create()), - hip(gko::HipExecutor::create(0, ref)), - csr_ref(nullptr), - csr_hip(nullptr) - {} - - void SetUp() override - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - auto csr_ref_temp = gko::read(input_file, ref); - auto csr_hip_temp = gko::clone(hip, csr_ref_temp); - // Make sure there are diagonal elements present - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(csr_ref_temp), false); - gko::kernels::hip::factorization::add_diagonal_elements( - hip, gko::lend(csr_hip_temp), false); - csr_ref = gko::give(csr_ref_temp); - csr_hip = gko::give(csr_hip_temp); - } - - template - std::unique_ptr gen_mtx(index_type num_rows, index_type num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(0, num_cols - 1), - std::normal_distribution(0.0, 1.0), rand_engine, ref); - } - - std::unique_ptr gen_unsorted_mtx(index_type num_rows, - index_type num_cols) - { - using std::swap; - auto mtx = gen_mtx(num_rows, num_cols); - auto values = mtx->get_values(); - auto col_idxs = mtx->get_col_idxs(); - const auto row_ptrs = mtx->get_const_row_ptrs(); - for (int row = 0; row < num_rows; ++row) { - const auto row_start = row_ptrs[row]; - const auto row_end = row_ptrs[row + 1]; - const int num_row_elements = row_end - row_start; - auto idx_dist = std::uniform_int_distribution( - row_start, row_end - 1); - for (int i = 0; i < num_row_elements / 2; ++i) { - auto idx1 = idx_dist(rand_engine); - auto idx2 = idx_dist(rand_engine); - if (idx1 != idx2) { - swap(values[idx1], values[idx2]); - swap(col_idxs[idx1], col_idxs[idx2]); - } - } - } - return mtx; - } - - void initialize_row_ptrs(index_type* l_row_ptrs_ref, - index_type* u_row_ptrs_ref, - index_type* l_row_ptrs_hip, - index_type* u_row_ptrs_hip) - { - gko::kernels::reference::factorization::initialize_row_ptrs_l_u( - ref, gko::lend(csr_ref), l_row_ptrs_ref, u_row_ptrs_ref); - gko::kernels::hip::factorization::initialize_row_ptrs_l_u( - hip, gko::lend(csr_hip), l_row_ptrs_hip, u_row_ptrs_hip); - } - - void initialize_lu(std::unique_ptr* l_ref, std::unique_ptr* u_ref, - std::unique_ptr* l_hip, std::unique_ptr* u_hip) - { - auto num_row_ptrs = csr_ref->get_size()[0] + 1; - gko::array l_row_ptrs_ref{ref, num_row_ptrs}; - gko::array u_row_ptrs_ref{ref, num_row_ptrs}; - gko::array l_row_ptrs_hip{hip, num_row_ptrs}; - gko::array u_row_ptrs_hip{hip, num_row_ptrs}; - - initialize_row_ptrs( - l_row_ptrs_ref.get_data(), u_row_ptrs_ref.get_data(), - l_row_ptrs_hip.get_data(), u_row_ptrs_hip.get_data()); - // Since `initialize_row_ptrs` was already tested, it is expected that - // `*_ref` and `*_hip` contain identical values - auto l_nnz = l_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; - auto u_nnz = u_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; - - *l_ref = Csr::create(ref, csr_ref->get_size(), l_nnz); - *u_ref = Csr::create(ref, csr_ref->get_size(), u_nnz); - *l_hip = Csr::create(hip, csr_hip->get_size(), l_nnz); - *u_hip = Csr::create(hip, csr_hip->get_size(), u_nnz); - // Copy the already initialized `row_ptrs` to the new matrices - ref->copy(num_row_ptrs, l_row_ptrs_ref.get_data(), - (*l_ref)->get_row_ptrs()); - ref->copy(num_row_ptrs, u_row_ptrs_ref.get_data(), - (*u_ref)->get_row_ptrs()); - hip->copy(num_row_ptrs, l_row_ptrs_hip.get_data(), - (*l_hip)->get_row_ptrs()); - hip->copy(num_row_ptrs, u_row_ptrs_hip.get_data(), - (*u_hip)->get_row_ptrs()); - - gko::kernels::reference::factorization::initialize_l_u( - ref, gko::lend(csr_ref), gko::lend(*l_ref), gko::lend(*u_ref)); - gko::kernels::hip::factorization::initialize_l_u( - hip, gko::lend(csr_hip), gko::lend(*l_hip), gko::lend(*u_hip)); - } - - template - static std::unique_ptr static_unique_ptr_cast( - std::unique_ptr&& from) - { - return std::unique_ptr{static_cast(from.release())}; - } - - void compute_lu(std::unique_ptr* l_ref, std::unique_ptr* u_ref, - std::unique_ptr* l_hip, std::unique_ptr* u_hip, - gko::size_type iterations = 0) - { - auto coo_ref = Coo::create(ref); - csr_ref->convert_to(gko::lend(coo_ref)); - auto coo_hip = Coo::create(hip); - csr_hip->convert_to(gko::lend(coo_hip)); - initialize_lu(l_ref, u_ref, l_hip, u_hip); - auto u_transpose_lin_op_ref = (*u_ref)->transpose(); - auto u_transpose_csr_ref = - static_unique_ptr_cast(std::move(u_transpose_lin_op_ref)); - auto u_transpose_lin_op_hip = (*u_hip)->transpose(); - auto u_transpose_csr_hip = - static_unique_ptr_cast(std::move(u_transpose_lin_op_hip)); - - gko::kernels::reference::par_ilu_factorization::compute_l_u_factors( - ref, iterations, gko::lend(coo_ref), gko::lend(*l_ref), - gko::lend(u_transpose_csr_ref)); - gko::kernels::hip::par_ilu_factorization::compute_l_u_factors( - hip, iterations, gko::lend(coo_hip), gko::lend(*l_hip), - gko::lend(u_transpose_csr_hip)); - auto u_lin_op_ref = u_transpose_csr_ref->transpose(); - *u_ref = static_unique_ptr_cast(std::move(u_lin_op_ref)); - auto u_lin_op_hip = u_transpose_csr_hip->transpose(); - *u_hip = static_unique_ptr_cast(std::move(u_lin_op_hip)); - } -}; - - -TEST_F(ParIlu, HipKernelAddDiagonalElementsSortedEquivalentToRef) -{ - index_type num_rows{600}; - index_type num_cols{600}; - auto mtx_ref = gen_mtx(num_rows, num_cols); - auto mtx_hip = gko::clone(hip, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(mtx_ref), true); - gko::kernels::hip::factorization::add_diagonal_elements( - hip, gko::lend(mtx_hip), true); - hip->synchronize(); - - ASSERT_TRUE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_hip, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_hip); -} - - -TEST_F(ParIlu, HipKernelAddDiagonalElementsUnsortedEquivalentToRef) -{ - index_type num_rows{600}; - index_type num_cols{600}; - auto mtx_ref = gen_unsorted_mtx(num_rows, num_cols); - auto mtx_hip = gko::clone(hip, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(mtx_ref), false); - gko::kernels::hip::factorization::add_diagonal_elements( - hip, gko::lend(mtx_hip), false); - hip->synchronize(); - - ASSERT_FALSE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_hip, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_hip); -} - - -TEST_F(ParIlu, HipKernelAddDiagonalElementsNonSquareEquivalentToRef) -{ - index_type num_rows{600}; - index_type num_cols{500}; - auto mtx_ref = gen_mtx(num_rows, num_cols); - auto mtx_hip = gko::clone(hip, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(mtx_ref), true); - gko::kernels::hip::factorization::add_diagonal_elements( - hip, gko::lend(mtx_hip), true); - hip->synchronize(); - - ASSERT_TRUE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_hip, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_hip); -} - - -TEST_F(ParIlu, KernelInitializeRowPtrsLUEquivalentToRef) -{ - auto num_row_ptrs = csr_ref->get_size()[0] + 1; - gko::array l_row_ptrs_array_ref(ref, num_row_ptrs); - gko::array u_row_ptrs_array_ref(ref, num_row_ptrs); - gko::array l_row_ptrs_array_hip(hip, num_row_ptrs); - gko::array u_row_ptrs_array_hip(hip, num_row_ptrs); - - initialize_row_ptrs( - l_row_ptrs_array_ref.get_data(), u_row_ptrs_array_ref.get_data(), - l_row_ptrs_array_hip.get_data(), u_row_ptrs_array_hip.get_data()); - - GKO_ASSERT_ARRAY_EQ(l_row_ptrs_array_ref, l_row_ptrs_array_hip); - GKO_ASSERT_ARRAY_EQ(u_row_ptrs_array_ref, u_row_ptrs_array_hip); -} - - -TEST_F(ParIlu, KernelInitializeParILUIsEquivalentToRef) -{ - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_hip{}; - std::unique_ptr u_hip{}; - - initialize_lu(&l_ref, &u_ref, &l_hip, &u_hip); - - GKO_ASSERT_MTX_NEAR(l_ref, l_hip, 1e-14); - GKO_ASSERT_MTX_NEAR(u_ref, u_hip, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_hip); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_hip); -} - - -TEST_F(ParIlu, KernelComputeParILUIsEquivalentToRef) -{ - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_hip{}; - std::unique_ptr u_hip{}; - - compute_lu(&l_ref, &u_ref, &l_hip, &u_hip); - - GKO_ASSERT_MTX_NEAR(l_ref, l_hip, 5e-2); - GKO_ASSERT_MTX_NEAR(u_ref, u_hip, 5e-2); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_hip); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_hip); -} - - -TEST_F(ParIlu, KernelComputeParILUWithMoreIterationsIsEquivalentToRef) -{ - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_hip{}; - std::unique_ptr u_hip{}; - gko::size_type iterations{200}; - - compute_lu(&l_ref, &u_ref, &l_hip, &u_hip, iterations); - - GKO_ASSERT_MTX_NEAR(l_ref, l_hip, 1e-14); - GKO_ASSERT_MTX_NEAR(u_ref, u_hip, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_hip); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_hip); -} - - -} // namespace diff --git a/hip/test/factorization/par_ilut_kernels.hip.cpp b/hip/test/factorization/par_ilut_kernels.hip.cpp deleted file mode 100644 index 76dc726b0cc..00000000000 --- a/hip/test/factorization/par_ilut_kernels.hip.cpp +++ /dev/null @@ -1,541 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ilut_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/factorization/par_ilu_kernels.hpp" -#include "core/matrix/csr_builder.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "hip/test/utils.hip.hpp" -#include "matrices/config.hpp" - - -namespace { - - -class ParIlut : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Dense = gko::matrix::Dense; - using ComplexDense = gko::matrix::Dense>; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - using ComplexCsr = gko::matrix::Csr, index_type>; - - ParIlut() -#ifdef GINKGO_FAST_TESTS - : mtx_size(152, 231), -#else - : mtx_size(500, 700), -#endif - rand_engine(1337), - ref(gko::ReferenceExecutor::create()), - hip(gko::HipExecutor::create(0, ref)) - { - mtx1 = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[1], - std::uniform_int_distribution<>(10, mtx_size[1]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx2 = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[1], - std::uniform_int_distribution<>(0, mtx_size[1]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_square = gko::test::generate_random_matrix( - mtx_size[0], mtx_size[0], - std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l2 = gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], true, std::uniform_int_distribution<>(1, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_l_complex = - gko::test::generate_random_lower_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_u = gko::test::generate_random_upper_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - mtx_u_complex = - gko::test::generate_random_upper_triangular_matrix( - mtx_size[0], false, - std::uniform_int_distribution<>(10, mtx_size[0]), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - - - dmtx1 = gko::clone(hip, mtx1); - dmtx2 = gko::clone(hip, mtx2); - dmtx_square = gko::clone(hip, mtx_square); - dmtx_ani = Csr::create(hip); - dmtx_l_ani = Csr::create(hip); - dmtx_u_ani = Csr::create(hip); - dmtx_ut_ani = Csr::create(hip); - dmtx_l = gko::clone(hip, mtx_l); - dmtx_l2 = gko::clone(hip, mtx_l2); - dmtx_u = gko::clone(hip, mtx_u); - dmtx_l_complex = gko::clone(hip, mtx_l_complex); - dmtx_u_complex = gko::clone(hip, mtx_u_complex); - } - - void SetUp() - { - std::string file_name(gko::matrices::location_ani4_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - mtx_ani = gko::read(input_file, ref); - mtx_ani->sort_by_column_index(); - - { - mtx_l_ani = Csr::create(ref, mtx_ani->get_size()); - mtx_u_ani = Csr::create(ref, mtx_ani->get_size()); - gko::matrix::CsrBuilder l_builder( - mtx_l_ani.get()); - gko::matrix::CsrBuilder u_builder( - mtx_u_ani.get()); - gko::kernels::reference::factorization::initialize_row_ptrs_l_u( - ref, mtx_ani.get(), mtx_l_ani->get_row_ptrs(), - mtx_u_ani->get_row_ptrs()); - auto l_nnz = - mtx_l_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - auto u_nnz = - mtx_u_ani->get_const_row_ptrs()[mtx_ani->get_size()[0]]; - l_builder.get_col_idx_array().resize_and_reset(l_nnz); - l_builder.get_value_array().resize_and_reset(l_nnz); - u_builder.get_col_idx_array().resize_and_reset(u_nnz); - u_builder.get_value_array().resize_and_reset(u_nnz); - gko::kernels::reference::factorization::initialize_l_u( - ref, mtx_ani.get(), mtx_l_ani.get(), mtx_u_ani.get()); - mtx_ut_ani = Csr::create(ref, mtx_ani->get_size(), - mtx_u_ani->get_num_stored_elements()); - gko::kernels::reference::csr::transpose(ref, mtx_u_ani.get(), - mtx_ut_ani.get()); - } - dmtx_ani->copy_from(mtx_ani.get()); - dmtx_l_ani->copy_from(mtx_l_ani.get()); - dmtx_u_ani->copy_from(mtx_u_ani.get()); - dmtx_ut_ani->copy_from(mtx_ut_ani.get()); - } - - template - void test_select(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, index_type rank, - value_type tolerance = 0.0) - { - auto size = index_type(mtx->get_num_stored_elements()); - using ValueType = typename Mtx::value_type; - - gko::remove_complex res{}; - gko::remove_complex dres{}; - gko::array tmp(ref); - gko::array> tmp2(ref); - gko::array dtmp(hip); - gko::array> dtmp2(hip); - - gko::kernels::reference::par_ilut_factorization::threshold_select( - ref, mtx.get(), rank, tmp, tmp2, res); - gko::kernels::hip::par_ilut_factorization::threshold_select( - hip, dmtx.get(), rank, dtmp, dtmp2, dres); - - ASSERT_NEAR(res, dres, tolerance); - } - - template > - void test_filter(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, value_type threshold, - bool lower) - { - auto res = Mtx::create(ref, mtx_size); - auto dres = Mtx::create(hip, mtx_size); - auto res_coo = Coo::create(ref, mtx_size); - auto dres_coo = Coo::create(hip, mtx_size); - auto local_mtx = gko::as(lower ? mtx->clone() : mtx->transpose()); - auto local_dmtx = - gko::as(lower ? dmtx->clone() : dmtx->transpose()); - - gko::kernels::reference::par_ilut_factorization::threshold_filter( - ref, local_mtx.get(), threshold, res.get(), res_coo.get(), lower); - gko::kernels::hip::par_ilut_factorization::threshold_filter( - hip, local_dmtx.get(), threshold, dres.get(), dres_coo.get(), - lower); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); - GKO_ASSERT_MTX_NEAR(res, res_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, res_coo); - GKO_ASSERT_MTX_NEAR(dres, dres_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(dres, dres_coo); - } - - template > - void test_filter_approx(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, index_type rank, - value_type tolerance = 0.0) - { - auto res = Mtx::create(ref, mtx_size); - auto dres = Mtx::create(hip, mtx_size); - auto res_coo = Coo::create(ref, mtx_size); - auto dres_coo = Coo::create(hip, mtx_size); - using ValueType = typename Mtx::value_type; - - gko::array tmp(ref); - gko::array dtmp(hip); - gko::remove_complex threshold{}; - gko::remove_complex dthreshold{}; - - gko::kernels::reference::par_ilut_factorization:: - threshold_filter_approx(ref, mtx.get(), rank, tmp, threshold, - res.get(), res_coo.get()); - gko::kernels::hip::par_ilut_factorization::threshold_filter_approx( - hip, dmtx.get(), rank, dtmp, dthreshold, dres.get(), - dres_coo.get()); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); - GKO_ASSERT_MTX_NEAR(res, res_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, res_coo); - GKO_ASSERT_MTX_NEAR(dres, dres_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(dres, dres_coo); - ASSERT_NEAR(threshold, dthreshold, tolerance); - } - - std::shared_ptr ref; - std::shared_ptr hip; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx1; - std::unique_ptr mtx2; - std::unique_ptr mtx_square; - std::unique_ptr mtx_ani; - std::unique_ptr mtx_l_ani; - std::unique_ptr mtx_u_ani; - std::unique_ptr mtx_ut_ani; - std::unique_ptr mtx_l; - std::unique_ptr mtx_l2; - std::unique_ptr mtx_l_complex; - std::unique_ptr mtx_u; - std::unique_ptr mtx_u_complex; - - std::unique_ptr dmtx1; - std::unique_ptr dmtx2; - std::unique_ptr dmtx_square; - std::unique_ptr dmtx_ani; - std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx_u_ani; - std::unique_ptr dmtx_ut_ani; - std::unique_ptr dmtx_l; - std::unique_ptr dmtx_l2; - std::unique_ptr dmtx_l_complex; - std::unique_ptr dmtx_u; - std::unique_ptr dmtx_u_complex; -}; - - -TEST_F(ParIlut, KernelThresholdSelectIsEquivalentToRef) -{ - test_select(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() / 3); -} - - -TEST_F(ParIlut, KernelThresholdSelectMinIsEquivalentToRef) -{ - test_select(mtx_l, dmtx_l, 0); -} - - -TEST_F(ParIlut, KernelThresholdSelectMaxIsEquivalentToRef) -{ - test_select(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() - 1); -} - - -TEST_F(ParIlut, KernelComplexThresholdSelectIsEquivalentToRef) -{ - test_select(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() / 3, 1e-14); -} - - -TEST_F(ParIlut, KernelComplexThresholdSelectMinIsEquivalentToRef) -{ - test_select(mtx_l_complex, dmtx_l_complex, 0, 1e-14); -} - - -TEST_F(ParIlut, KernelComplexThresholdSelectMaxLowerIsEquivalentToRef) -{ - test_select(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() - 1, 1e-14); -} - - -TEST_F(ParIlut, KernelThresholdFilterNullptrCooIsEquivalentToRef) -{ - auto res = Csr::create(ref, mtx_size); - auto dres = Csr::create(hip, mtx_size); - Coo* null_coo = nullptr; - - gko::kernels::reference::par_ilut_factorization::threshold_filter( - ref, mtx_l.get(), 0.5, res.get(), null_coo, true); - gko::kernels::hip::par_ilut_factorization::threshold_filter( - hip, dmtx_l.get(), 0.5, dres.get(), null_coo, true); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); -} - - -TEST_F(ParIlut, KernelThresholdFilterLowerIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0.5, true); -} - - -TEST_F(ParIlut, KernelThresholdFilterUpperIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0.5, false); -} - - -TEST_F(ParIlut, KernelThresholdFilterNoneLowerIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0, true); -} - - -TEST_F(ParIlut, KernelThresholdFilterNoneUpperIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0, false); -} - - -TEST_F(ParIlut, KernelThresholdFilterAllLowerIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 1e6, true); -} - - -TEST_F(ParIlut, KernelThresholdFilterAllUpperIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 1e6, false); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterLowerIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0.5, true); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterNoneLowerIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0, true); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterAllLowerIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 1e6, true); -} - - -#if defined(hipsparseVersionMajor) && defined(hipsparseVersionMinor) && \ - ((hipsparseVersionMajor > 1) || \ - (hipsparseVersionMajor == 1 && hipsparseVersionMinor >= 4)) -TEST_F(ParIlut, KernelComplexThresholdFilterUpperIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0.5, false); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterNoneUpperIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 0, false); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterAllUppererIsEquivalentToRef) -{ - test_filter(mtx_l_complex, dmtx_l_complex, 1e6, false); -} -#endif // hipsparse version >= 1.4 - - -TEST_F(ParIlut, KernelThresholdFilterApproxNullptrCooIsEquivalentToRef) -{ - test_filter(mtx_l, dmtx_l, 0.5, true); - auto res = Csr::create(ref, mtx_size); - auto dres = Csr::create(hip, mtx_size); - Coo* null_coo = nullptr; - gko::array tmp(ref); - gko::array dtmp(hip); - gko::remove_complex threshold{}; - gko::remove_complex dthreshold{}; - index_type rank{}; - - gko::kernels::reference::par_ilut_factorization::threshold_filter_approx( - ref, mtx_l.get(), rank, tmp, threshold, res.get(), null_coo); - gko::kernels::hip::par_ilut_factorization::threshold_filter_approx( - hip, dmtx_l.get(), rank, dtmp, dthreshold, dres.get(), null_coo); - - GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); - ASSERT_EQ(threshold, dthreshold); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() / 2); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxNoneLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l, dmtx_l, 0); -} - - -TEST_F(ParIlut, KernelThresholdFilterApproxAllLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l, dmtx_l, mtx_l->get_num_stored_elements() - 1); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterApproxLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() / 2, - r::value); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterApproxNoneLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l_complex, dmtx_l_complex, 0, r::value); -} - - -TEST_F(ParIlut, KernelComplexThresholdFilterApproxAllLowerIsEquivalentToRef) -{ - test_filter_approx(mtx_l_complex, dmtx_l_complex, - mtx_l_complex->get_num_stored_elements() - 1, - r::value); -} - - -TEST_F(ParIlut, KernelAddCandidatesIsEquivalentToRef) -{ - auto square_size = mtx_square->get_size(); - auto mtx_lu = Csr::create(ref, square_size); - mtx_l2->apply(mtx_u.get(), mtx_lu.get()); - auto dmtx_lu = Csr::create(hip, square_size); - dmtx_lu->copy_from(mtx_lu.get()); - auto res_mtx_l = Csr::create(ref, square_size); - auto res_mtx_u = Csr::create(ref, square_size); - auto dres_mtx_l = Csr::create(hip, square_size); - auto dres_mtx_u = Csr::create(hip, square_size); - - gko::kernels::reference::par_ilut_factorization::add_candidates( - ref, mtx_lu.get(), mtx_square.get(), mtx_l2.get(), mtx_u.get(), - res_mtx_l.get(), res_mtx_u.get()); - gko::kernels::hip::par_ilut_factorization::add_candidates( - hip, dmtx_lu.get(), dmtx_square.get(), dmtx_l2.get(), dmtx_u.get(), - dres_mtx_l.get(), dres_mtx_u.get()); - - GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_l, dres_mtx_l); - GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_u, dres_mtx_u); - GKO_ASSERT_MTX_NEAR(res_mtx_l, dres_mtx_l, 1e-14); - GKO_ASSERT_MTX_NEAR(res_mtx_u, dres_mtx_u, 1e-14); -} - - -TEST_F(ParIlut, KernelComputeLUIsEquivalentToRef) -{ - auto square_size = mtx_ani->get_size(); - auto mtx_l_coo = Coo::create(ref, square_size); - auto mtx_u_coo = Coo::create(ref, square_size); - mtx_l_ani->convert_to(mtx_l_coo.get()); - mtx_u_ani->convert_to(mtx_u_coo.get()); - auto dmtx_l_coo = Coo::create(hip, square_size); - auto dmtx_u_coo = Coo::create(hip, square_size); - dmtx_l_coo->copy_from(mtx_l_coo.get()); - dmtx_u_coo->copy_from(mtx_u_coo.get()); - - gko::kernels::reference::par_ilut_factorization::compute_l_u_factors( - ref, mtx_ani.get(), mtx_l_ani.get(), mtx_l_coo.get(), mtx_u_ani.get(), - mtx_u_coo.get(), mtx_ut_ani.get()); - for (int i = 0; i < 20; ++i) { - gko::kernels::hip::par_ilut_factorization::compute_l_u_factors( - hip, dmtx_ani.get(), dmtx_l_ani.get(), dmtx_l_coo.get(), - dmtx_u_ani.get(), dmtx_u_coo.get(), dmtx_ut_ani.get()); - } - auto dmtx_utt_ani = gko::as(dmtx_ut_ani->transpose()); - - GKO_ASSERT_MTX_NEAR(mtx_l_ani, dmtx_l_ani, 1e-2); - GKO_ASSERT_MTX_NEAR(mtx_u_ani, dmtx_u_ani, 1e-2); - GKO_ASSERT_MTX_NEAR(dmtx_u_ani, dmtx_utt_ani, 0); -} - - -} // namespace diff --git a/hip/test/matrix/CMakeLists.txt b/hip/test/matrix/CMakeLists.txt index 1759cd1ed28..82db4b8b376 100644 --- a/hip/test/matrix/CMakeLists.txt +++ b/hip/test/matrix/CMakeLists.txt @@ -1,11 +1,4 @@ -ginkgo_create_hip_test(coo_kernels) -ginkgo_create_hip_test(csr_kernels) -ginkgo_create_hip_test(dense_kernels) -ginkgo_create_hip_test(diagonal_kernels) -ginkgo_create_hip_test(ell_kernels) ginkgo_create_hip_test(fbcsr_kernels) if (hipfft_FOUND) ginkgo_create_hip_test(fft_kernels) endif() -ginkgo_create_hip_test(hybrid_kernels) -ginkgo_create_hip_test(sellp_kernels) diff --git a/hip/test/matrix/coo_kernels.hip.cpp b/hip/test/matrix/coo_kernels.hip.cpp deleted file mode 100644 index 808a6010c02..00000000000 --- a/hip/test/matrix/coo_kernels.hip.cpp +++ /dev/null @@ -1,392 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/matrix/coo_kernels.hpp" -#include "core/test/utils/unsort_matrix.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class Coo : public ::testing::Test { -protected: - using Mtx = gko::matrix::Coo<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; - - Coo() : rand_engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data(int num_vectors = 1) - { - mtx = gen_mtx(532, 231); - expected = gen_mtx(532, num_vectors); - y = gen_mtx(231, num_vectors); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = gko::clone(hip, mtx); - dresult = gko::clone(hip, expected); - dy = gko::clone(hip, y); - dalpha = gko::clone(hip, alpha); - dbeta = gko::clone(hip, beta); - } - - void unsort_mtx() - { - gko::test::unsort_matrix(mtx.get(), rand_engine); - dmtx->copy_from(mtx.get()); - } - - - std::shared_ptr ref; - std::shared_ptr hip; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; -}; - - -TEST_F(Coo, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, SimpleApplyDoesntOverwritePadding) -{ - set_up_apply_data(); - auto dresult_padded = - Vec::create(hip, dresult->get_size(), dresult->get_stride() + 1); - dresult_padded->copy_from(dresult.get()); - double padding_val{1234.0}; - hip->copy_from(hip->get_master().get(), 1, &padding_val, - dresult_padded->get_values() + 1); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult_padded.get()); - - GKO_ASSERT_MTX_NEAR(dresult_padded, expected, 1e-14); - ASSERT_EQ(hip->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0); -} - - -TEST_F(Coo, SimpleApplyIsEquivalentToRefUnsorted) -{ - set_up_apply_data(); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyDoesntOverwritePadding) -{ - set_up_apply_data(); - auto dresult_padded = - Vec::create(hip, dresult->get_size(), dresult->get_stride() + 1); - dresult_padded->copy_from(dresult.get()); - double padding_val{1234.0}; - hip->copy_from(hip->get_master().get(), 1, &padding_val, - dresult_padded->get_values() + 1); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult_padded.get()); - - GKO_ASSERT_MTX_NEAR(dresult_padded, expected, 1e-14); - ASSERT_EQ(hip->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0); -} - - -TEST_F(Coo, SimpleApplyAddIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyAddIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply2(alpha.get(), y.get(), expected.get()); - dmtx->apply2(dalpha.get(), dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, SimpleApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, SimpleApplyAddToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, SimpleApplyAddToLargeDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(33); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyAddToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply2(alpha.get(), y.get(), expected.get()); - dmtx->apply2(dalpha.get(), dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyAddToLargeDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(33); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(hip, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(hip, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Coo, ApplyAddToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(hip, complex_x); - - mtx->apply2(alpha.get(), complex_b.get(), complex_x.get()); - dmtx->apply2(dalpha.get(), dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Coo, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(hip); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Coo, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(hip); - - mtx->convert_to(dense_mtx.get()); - dense_mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Coo, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Coo, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Coo, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -} // namespace diff --git a/hip/test/matrix/csr_kernels.hip.cpp b/hip/test/matrix/csr_kernels.hip.cpp deleted file mode 100644 index f4cd6b53254..00000000000 --- a/hip/test/matrix/csr_kernels.hip.cpp +++ /dev/null @@ -1,1053 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#include "core/components/prefix_sum_kernels.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "core/test/utils/unsort_matrix.hpp" -#include "core/utils/matrix_utils.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class Csr : public ::testing::Test { -protected: - using Arr = gko::array; - using Vec = gko::matrix::Dense<>; - using Mtx = gko::matrix::Csr<>; - using ComplexVec = gko::matrix::Dense>; - using ComplexMtx = gko::matrix::Csr>; - - Csr() -#ifdef GINKGO_FAST_TESTS - : mtx_size(152, 231), -#else - : mtx_size(532, 231), -#endif - rand_engine(42) - {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data() - { - mtx2 = Mtx::create(ref); - mtx2->copy_from(gen_mtx(mtx_size[0], mtx_size[1], 5)); - dmtx2 = Mtx::create(hip); - dmtx2->copy_from(mtx2.get()); - } - - void set_up_apply_data(std::shared_ptr strategy, - int num_vectors = 1) - { - mtx = Mtx::create(ref, strategy); - mtx->copy_from(gen_mtx(mtx_size[0], mtx_size[1], 1)); - square_mtx = Mtx::create(ref, strategy); - square_mtx->copy_from(gen_mtx(mtx_size[0], mtx_size[0], 1)); - expected = gen_mtx(mtx_size[0], num_vectors, 1); - y = gen_mtx(mtx_size[1], num_vectors, 1); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = Mtx::create(hip, strategy); - dmtx->copy_from(mtx.get()); - square_dmtx = Mtx::create(hip, strategy); - square_dmtx->copy_from(square_mtx.get()); - dresult = gko::clone(hip, expected); - dy = gko::clone(hip, y); - dalpha = gko::clone(hip, alpha); - dbeta = gko::clone(hip, beta); - - std::vector tmp(mtx->get_size()[0], 0); - auto rng = std::default_random_engine{}; - std::iota(tmp.begin(), tmp.end(), 0); - std::shuffle(tmp.begin(), tmp.end(), rng); - std::vector tmp2(mtx->get_size()[1], 0); - std::iota(tmp2.begin(), tmp2.end(), 0); - std::shuffle(tmp2.begin(), tmp2.end(), rng); - rpermute_idxs = std::make_unique(ref, tmp.begin(), tmp.end()); - cpermute_idxs = std::make_unique(ref, tmp2.begin(), tmp2.end()); - } - - void set_up_apply_complex_data( - std::shared_ptr strategy) - { - complex_mtx = ComplexMtx::create(ref, strategy); - complex_mtx->copy_from( - gen_mtx(mtx_size[0], mtx_size[1], 1)); - complex_dmtx = ComplexMtx::create(hip, strategy); - complex_dmtx->copy_from(complex_mtx.get()); - } - - void unsort_mtx() - { - gko::test::unsort_matrix(mtx.get(), rand_engine); - dmtx->copy_from(mtx.get()); - } - - std::shared_ptr ref; - std::shared_ptr hip; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr mtx2; - std::unique_ptr complex_mtx; - std::unique_ptr square_mtx; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dmtx2; - std::unique_ptr complex_dmtx; - std::unique_ptr square_dmtx; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; - std::unique_ptr rpermute_idxs; - std::unique_ptr cpermute_idxs; -}; - - -TEST_F(Csr, StrategyAfterCopyIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - - ASSERT_EQ(mtx->get_strategy()->get_name(), - dmtx->get_strategy()->get_name()); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithLoadBalance) -{ - set_up_apply_data(std::make_shared(hip)); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithLoadBalanceUnsorted) -{ - set_up_apply_data(std::make_shared(hip)); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithLoadBalance) -{ - set_up_apply_data(std::make_shared(hip)); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithHipsparse) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithHipsparseUnsorted) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithHipsparse) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithMergePath) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithMergePathUnsorted) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithMergePath) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithClassical) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithClassicalUnsorted) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithClassical) -{ - set_up_apply_data(std::make_shared()); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithAutomatical) -{ - set_up_apply_data(std::make_shared(hip)); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithAutomaticalUnsorted) -{ - set_up_apply_data(std::make_shared(hip)); - unsort_mtx(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithLoadBalance) -{ - set_up_apply_data(std::make_shared(hip), 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithLoadBalance) -{ - set_up_apply_data(std::make_shared(hip), 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithClassical) -{ - set_up_apply_data(std::make_shared(), 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithClassical) -{ - set_up_apply_data(std::make_shared(), 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithMergePath) -{ - set_up_apply_data(std::make_shared(), 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithMergePath) -{ - set_up_apply_data(std::make_shared(), 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyToCsrMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - auto trans = mtx->transpose(); - auto d_trans = dmtx->transpose(); - - mtx->apply(alpha.get(), trans.get(), beta.get(), square_mtx.get()); - dmtx->apply(dalpha.get(), d_trans.get(), dbeta.get(), square_dmtx.get()); - - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); - ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); -} - - -TEST_F(Csr, SimpleApplyToCsrMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - auto trans = mtx->transpose(); - auto d_trans = dmtx->transpose(); - - mtx->apply(trans.get(), square_mtx.get()); - dmtx->apply(d_trans.get(), square_dmtx.get()); - - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); - ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); -} - - -TEST_F(Csr, AdvancedApplyToIdentityMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - auto a = gen_mtx(mtx_size[0], mtx_size[1], 0); - auto b = gen_mtx(mtx_size[0], mtx_size[1], 0); - auto da = gko::clone(hip, a); - auto db = gko::clone(hip, b); - auto id = gko::matrix::Identity::create(ref, mtx_size[1]); - auto did = gko::matrix::Identity::create(hip, mtx_size[1]); - - a->apply(alpha.get(), id.get(), beta.get(), b.get()); - da->apply(dalpha.get(), did.get(), dbeta.get(), db.get()); - - GKO_ASSERT_MTX_NEAR(b, db, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(b, db); - ASSERT_TRUE(db->is_sorted_by_column_index()); -} - - -TEST_F(Csr, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - auto complex_b = gen_mtx(this->mtx_size[1], 3, 1); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(this->mtx_size[0], 3, 1); - auto dcomplex_x = gko::clone(hip, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - auto complex_b = gen_mtx(this->mtx_size[1], 3, 1); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(this->mtx_size[0], 3, 1); - auto dcomplex_x = gko::clone(hip, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Csr, TransposeIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - - auto trans = gko::as(mtx->transpose()); - auto d_trans = gko::as(dmtx->transpose()); - - GKO_ASSERT_MTX_NEAR(d_trans, trans, 0.0); - ASSERT_TRUE(d_trans->is_sorted_by_column_index()); -} - - -TEST_F(Csr, Transpose64IsEquivalentToRef) -{ - using Mtx64 = gko::matrix::Csr; - auto mtx = gen_mtx(123, 234, 0); - auto dmtx = gko::clone(hip, mtx); - - auto trans = gko::as(mtx->transpose()); - auto d_trans = gko::as(dmtx->transpose()); - - GKO_ASSERT_MTX_NEAR(d_trans, trans, 0.0); - ASSERT_TRUE(d_trans->is_sorted_by_column_index()); -} - - -TEST_F(Csr, ConjugateTransposeIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - - auto trans = gko::as(mtx->conj_transpose()); - auto d_trans = gko::as(dmtx->conj_transpose()); - - GKO_ASSERT_MTX_NEAR(d_trans, trans, 0.0); - ASSERT_TRUE(d_trans->is_sorted_by_column_index()); -} - - -TEST_F(Csr, ConjugateTranspose64IsEquivalentToRef) -{ - using Mtx64 = gko::matrix::Csr; - auto mtx = gen_mtx(123, 234, 0); - auto dmtx = gko::clone(hip, mtx); - - auto trans = gko::as(mtx->transpose()); - auto d_trans = gko::as(dmtx->transpose()); - - GKO_ASSERT_MTX_NEAR(d_trans, trans, 0.0); - ASSERT_TRUE(d_trans->is_sorted_by_column_index()); -} - - -TEST_F(Csr, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(hip); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToDenseIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(hip); - - mtx->move_to(dense_mtx.get()); - dmtx->move_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToEllIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto ell_mtx = gko::matrix::Ell<>::create(ref); - auto dell_mtx = gko::matrix::Ell<>::create(hip); - - mtx->convert_to(ell_mtx.get()); - dmtx->convert_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(ell_mtx.get(), dell_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToEllIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto ell_mtx = gko::matrix::Ell<>::create(ref); - auto dell_mtx = gko::matrix::Ell<>::create(hip); - - mtx->move_to(ell_mtx.get()); - dmtx->move_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(ell_mtx.get(), dell_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(hip); - - mtx->convert_to(sparsity_mtx.get()); - dmtx->convert_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sparsity_mtx.get(), d_sparsity_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(hip); - - mtx->move_to(sparsity_mtx.get()); - dmtx->move_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sparsity_mtx.get(), d_sparsity_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToCooIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(hip); - - mtx->convert_to(coo_mtx.get()); - dmtx->convert_to(dcoo_mtx.get()); - - GKO_ASSERT_MTX_NEAR(coo_mtx.get(), dcoo_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToCooIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(hip); - - mtx->move_to(coo_mtx.get()); - dmtx->move_to(dcoo_mtx.get()); - - GKO_ASSERT_MTX_NEAR(coo_mtx.get(), dcoo_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToSellpIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto sellp_mtx = gko::matrix::Sellp<>::create(ref); - auto dsellp_mtx = gko::matrix::Sellp<>::create(hip); - - mtx->convert_to(sellp_mtx.get()); - dmtx->convert_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx.get(), dsellp_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToSellpIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - auto sellp_mtx = gko::matrix::Sellp<>::create(ref); - auto dsellp_mtx = gko::matrix::Sellp<>::create(hip); - - mtx->move_to(sellp_mtx.get()); - dmtx->move_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx.get(), dsellp_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertsEmptyToSellp) -{ - auto dempty_mtx = Mtx::create(hip); - auto dsellp_mtx = gko::matrix::Sellp<>::create(hip); - - dempty_mtx->convert_to(dsellp_mtx.get()); - - ASSERT_EQ(hip->copy_val_to_host(dsellp_mtx->get_const_slice_sets()), 0); - ASSERT_FALSE(dsellp_mtx->get_size()); -} - - -TEST_F(Csr, ConvertToHybridIsEquivalentToRef) -{ - using Hybrid_type = gko::matrix::Hybrid<>; - set_up_apply_data(std::make_shared()); - auto hybrid_mtx = Hybrid_type::create( - ref, std::make_shared(2)); - auto dhybrid_mtx = Hybrid_type::create( - hip, std::make_shared(2)); - - mtx->convert_to(hybrid_mtx.get()); - dmtx->convert_to(dhybrid_mtx.get()); - - GKO_ASSERT_MTX_NEAR(hybrid_mtx.get(), dhybrid_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToHybridIsEquivalentToRef) -{ - using Hybrid_type = gko::matrix::Hybrid<>; - set_up_apply_data(std::make_shared()); - auto hybrid_mtx = Hybrid_type::create( - ref, std::make_shared(2)); - auto dhybrid_mtx = Hybrid_type::create( - hip, std::make_shared(2)); - - mtx->move_to(hybrid_mtx.get()); - dmtx->move_to(dhybrid_mtx.get()); - - GKO_ASSERT_MTX_NEAR(hybrid_mtx.get(), dhybrid_mtx.get(), 0); -} - - -TEST_F(Csr, IsPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto permuted = gko::as(square_mtx->permute(rpermute_idxs.get())); - auto dpermuted = gko::as(square_dmtx->permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(permuted, dpermuted); - GKO_ASSERT_MTX_NEAR(permuted, dpermuted, 0); -} - - -TEST_F(Csr, IsInversePermutable) -{ - set_up_apply_data(std::make_shared()); - - auto permuted = - gko::as(square_mtx->inverse_permute(rpermute_idxs.get())); - auto dpermuted = - gko::as(square_dmtx->inverse_permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(permuted, dpermuted); - GKO_ASSERT_MTX_NEAR(permuted, dpermuted, 0); -} - - -TEST_F(Csr, IsRowPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto r_permute = gko::as(mtx->row_permute(rpermute_idxs.get())); - auto dr_permute = gko::as(dmtx->row_permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(r_permute, dr_permute); - GKO_ASSERT_MTX_NEAR(r_permute, dr_permute, 0); -} - - -TEST_F(Csr, IsColPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto c_permute = gko::as(mtx->column_permute(cpermute_idxs.get())); - auto dc_permute = gko::as(dmtx->column_permute(cpermute_idxs.get())); - - ASSERT_TRUE(dc_permute->is_sorted_by_column_index()); - GKO_ASSERT_MTX_EQ_SPARSITY(c_permute, dc_permute); - GKO_ASSERT_MTX_NEAR(c_permute, dc_permute, 0); -} - - -TEST_F(Csr, IsInverseRowPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto inverse_r_permute = - gko::as(mtx->inverse_row_permute(rpermute_idxs.get())); - auto d_inverse_r_permute = - gko::as(dmtx->inverse_row_permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse_r_permute, d_inverse_r_permute); - GKO_ASSERT_MTX_NEAR(inverse_r_permute, d_inverse_r_permute, 0); -} - - -TEST_F(Csr, IsInverseColPermutable) -{ - set_up_apply_data(std::make_shared()); - - auto inverse_c_permute = - gko::as(mtx->inverse_column_permute(cpermute_idxs.get())); - auto d_inverse_c_permute = - gko::as(dmtx->inverse_column_permute(cpermute_idxs.get())); - - ASSERT_TRUE(d_inverse_c_permute->is_sorted_by_column_index()); - GKO_ASSERT_MTX_EQ_SPARSITY(inverse_c_permute, d_inverse_c_permute); - GKO_ASSERT_MTX_NEAR(inverse_c_permute, d_inverse_c_permute, 0); -} - - -TEST_F(Csr, RecognizeSortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - bool is_sorted_hip{}; - bool is_sorted_ref{}; - - is_sorted_ref = mtx->is_sorted_by_column_index(); - is_sorted_hip = dmtx->is_sorted_by_column_index(); - - ASSERT_EQ(is_sorted_ref, is_sorted_hip); -} - - -TEST_F(Csr, RecognizeUnsortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - bool is_sorted_hip{}; - bool is_sorted_ref{}; - - is_sorted_ref = mtx->is_sorted_by_column_index(); - is_sorted_hip = dmtx->is_sorted_by_column_index(); - - ASSERT_EQ(is_sorted_ref, is_sorted_hip); -} - - -TEST_F(Csr, SortSortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - - mtx->sort_by_column_index(); - dmtx->sort_by_column_index(); - - // Values must be unchanged, therefore, tolerance is `0` - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 0); -} - - -TEST_F(Csr, SortUnsortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared()); - unsort_mtx(); - - mtx->sort_by_column_index(); - dmtx->sort_by_column_index(); - - // Values must be unchanged, therefore, tolerance is `0` - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 0); -} - - -TEST_F(Csr, OneAutomaticalWorksWithDifferentMatrices) -{ - auto automatical = std::make_shared(hip); - auto row_len_limit = std::max(automatical->nvidia_row_len_limit, - automatical->amd_row_len_limit); - auto load_balance_mtx = - gen_mtx(1, row_len_limit + 1000, row_len_limit + 1); - auto classical_mtx = gen_mtx(50, 50, 1); - auto load_balance_mtx_d = gko::clone(hip, load_balance_mtx); - auto classical_mtx_d = gko::clone(hip, classical_mtx); - - load_balance_mtx_d->set_strategy(automatical); - classical_mtx_d->set_strategy(automatical); - - EXPECT_EQ("load_balance", load_balance_mtx_d->get_strategy()->get_name()); - EXPECT_EQ("classical", classical_mtx_d->get_strategy()->get_name()); - ASSERT_NE(load_balance_mtx_d->get_strategy().get(), - classical_mtx_d->get_strategy().get()); -} - - -TEST_F(Csr, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Csr, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Csr, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(std::make_shared(hip)); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -TEST_F(Csr, InplaceAbsoluteComplexMatrixIsEquivalentToRef) -{ - set_up_apply_complex_data(std::make_shared(hip)); - - complex_mtx->compute_absolute_inplace(); - complex_dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(complex_mtx, complex_dmtx, 1e-14); -} - - -TEST_F(Csr, OutplaceAbsoluteComplexMatrixIsEquivalentToRef) -{ - set_up_apply_complex_data(std::make_shared(hip)); - - auto abs_mtx = complex_mtx->compute_absolute(); - auto dabs_mtx = complex_dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -TEST_F(Csr, CalculateNnzPerRowInSpanIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr<>; - set_up_apply_data(); - gko::span rspan{7, 51}; - gko::span cspan{22, 88}; - auto size = this->mtx2->get_size(); - auto row_nnz = gko::array(this->ref, rspan.length() + 1); - auto drow_nnz = gko::array(this->hip, row_nnz); - - gko::kernels::reference::csr::calculate_nonzeros_per_row_in_span( - this->ref, this->mtx2.get(), rspan, cspan, &row_nnz); - gko::kernels::hip::csr::calculate_nonzeros_per_row_in_span( - this->hip, this->dmtx2.get(), rspan, cspan, &drow_nnz); - - GKO_ASSERT_ARRAY_EQ(row_nnz, drow_nnz); -} - - -TEST_F(Csr, ComputeSubmatrixIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr<>; - using IndexType = int; - using ValueType = double; - set_up_apply_data(); - gko::span rspan{7, 51}; - gko::span cspan{22, 88}; - auto size = this->mtx2->get_size(); - auto row_nnz = gko::array(this->ref, rspan.length() + 1); - gko::kernels::reference::csr::calculate_nonzeros_per_row_in_span( - this->ref, this->mtx2.get(), rspan, cspan, &row_nnz); - gko::kernels::reference::components::prefix_sum( - this->ref, row_nnz.get_data(), row_nnz.get_num_elems()); - auto num_nnz = row_nnz.get_data()[rspan.length()]; - auto drow_nnz = gko::array(this->hip, row_nnz); - auto smat1 = - Mtx::create(this->ref, gko::dim<2>(rspan.length(), cspan.length()), - std::move(gko::array(this->ref, num_nnz)), - std::move(gko::array(this->ref, num_nnz)), - std::move(row_nnz)); - auto sdmat1 = - Mtx::create(this->hip, gko::dim<2>(rspan.length(), cspan.length()), - std::move(gko::array(this->hip, num_nnz)), - std::move(gko::array(this->hip, num_nnz)), - std::move(drow_nnz)); - - - gko::kernels::reference::csr::compute_submatrix(this->ref, this->mtx2.get(), - rspan, cspan, smat1.get()); - gko::kernels::hip::csr::compute_submatrix(this->hip, this->dmtx2.get(), - rspan, cspan, sdmat1.get()); - - GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); -} - - -TEST_F(Csr, CreateSubMatrixIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr<>; - set_up_apply_data(); - gko::span rspan{47, 81}; - gko::span cspan{2, 31}; - - auto smat1 = this->mtx2->create_submatrix(rspan, cspan); - auto sdmat1 = this->dmtx2->create_submatrix(rspan, cspan); - - GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); -} - - -TEST_F(Csr, CanDetectMissingDiagonalEntry) -{ - using T = double; - using Csr = Mtx; - auto ref_mtx = gen_mtx(103, 98, 10); - const auto rowptrs = ref_mtx->get_row_ptrs(); - const auto colidxs = ref_mtx->get_col_idxs(); - const int testrow = 15; - gko::utils::remove_diagonal_entry_from_row(ref_mtx.get(), testrow); - auto mtx = gko::clone(hip, ref_mtx); - bool has_diags = true; - - gko::kernels::hip::csr::check_diagonal_entries_exist(hip, mtx.get(), - has_diags); - - ASSERT_FALSE(has_diags); -} - - -TEST_F(Csr, CanDetectWhenAllDiagonalEntriesArePresent) -{ - using T = double; - using Csr = Mtx; - auto ref_mtx = gen_mtx(103, 98, 10); - gko::utils::ensure_all_diagonal_entries(ref_mtx.get()); - auto mtx = gko::clone(hip, ref_mtx); - bool has_diags = false; - - gko::kernels::hip::csr::check_diagonal_entries_exist(hip, mtx.get(), - has_diags); - - ASSERT_TRUE(has_diags); -} - - -TEST_F(Csr, AddScaledIdentityToNonSquare) -{ - set_up_apply_data(std::make_shared()); - gko::utils::ensure_all_diagonal_entries(mtx.get()); - dmtx->copy_from(mtx.get()); - - mtx->add_scaled_identity(alpha.get(), beta.get()); - dmtx->add_scaled_identity(dalpha.get(), dbeta.get()); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); -} - - -} // namespace diff --git a/hip/test/matrix/dense_kernels.hip.cpp b/hip/test/matrix/dense_kernels.hip.cpp deleted file mode 100644 index 7077590e078..00000000000 --- a/hip/test/matrix/dense_kernels.hip.cpp +++ /dev/null @@ -1,607 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include -#include - - -#include "core/components/fill_array_kernels.hpp" -#include "core/matrix/dense_kernels.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class Dense : public ::testing::Test { -protected: - using itype = int; - using vtype = double; - using Mtx = gko::matrix::Dense; - using MixedMtx = gko::matrix::Dense>; - using NormVector = gko::matrix::Dense>; - using Arr = gko::array; - using ComplexMtx = gko::matrix::Dense>; - using Diagonal = gko::matrix::Diagonal; - using MixedComplexMtx = - gko::matrix::Dense>>; - - Dense() : rand_engine(15) {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); - } - - void set_up_vector_data(gko::size_type num_vecs, - bool different_alpha = false) - { - x = gen_mtx(1000, num_vecs); - y = gen_mtx(1000, num_vecs); - if (different_alpha) { - alpha = gen_mtx(1, num_vecs); - } else { - alpha = gko::initialize({2.0}, ref); - } - dx = gko::clone(hip, x); - dy = gko::clone(hip, y); - dalpha = gko::clone(hip, alpha); - expected = Mtx::create(ref, gko::dim<2>{1, num_vecs}); - dresult = Mtx::create(hip, gko::dim<2>{1, num_vecs}); - } - - void set_up_apply_data() - { - x = gen_mtx(65, 25); - y = gen_mtx(25, 35); - expected = gen_mtx(65, 35); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - square = gen_mtx(x->get_size()[0], x->get_size()[0]); - dx = gko::clone(hip, x); - dy = gko::clone(hip, y); - dresult = gko::clone(hip, expected); - dalpha = gko::clone(hip, alpha); - dbeta = gko::clone(hip, beta); - dsquare = gko::clone(hip, square); - - std::vector tmp(x->get_size()[0], 0); - auto rng = std::default_random_engine{}; - std::iota(tmp.begin(), tmp.end(), 0); - std::shuffle(tmp.begin(), tmp.end(), rng); - std::vector tmp2(x->get_size()[1], 0); - std::iota(tmp2.begin(), tmp2.end(), 0); - std::shuffle(tmp2.begin(), tmp2.end(), rng); - std::vector tmp3(x->get_size()[0] / 10); - std::uniform_int_distribution row_dist(0, x->get_size()[0] - 1); - for (auto& i : tmp3) { - i = row_dist(rng); - } - rpermute_idxs = - std::unique_ptr(new Arr{ref, tmp.begin(), tmp.end()}); - cpermute_idxs = - std::unique_ptr(new Arr{ref, tmp2.begin(), tmp2.end()}); - rgather_idxs = - std::unique_ptr(new Arr{ref, tmp3.begin(), tmp3.end()}); - } - - template - std::unique_ptr convert(InputType&& input) - { - auto result = ConvertedType::create(input->get_executor()); - input->convert_to(result.get()); - return result; - } - - std::shared_ptr ref; - std::shared_ptr hip; - - std::default_random_engine rand_engine; - - std::unique_ptr x; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - std::unique_ptr expected; - std::unique_ptr square; - std::unique_ptr dresult; - std::unique_ptr dx; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; - std::unique_ptr dsquare; - std::unique_ptr rpermute_idxs; - std::unique_ptr cpermute_idxs; - std::unique_ptr rgather_idxs; -}; - - -TEST_F(Dense, SingleVectorHipComputeDotIsEquivalentToRef) -{ - set_up_vector_data(1); - - x->compute_dot(y.get(), expected.get()); - dx->compute_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, MultipleVectorHipComputeDotIsEquivalentToRef) -{ - set_up_vector_data(20); - - x->compute_dot(y.get(), expected.get()); - dx->compute_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, SingleVectorHipComputeConjDotIsEquivalentToRef) -{ - set_up_vector_data(1); - - x->compute_conj_dot(y.get(), expected.get()); - dx->compute_conj_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, MultipleVectorHipComputeConjDotIsEquivalentToRef) -{ - set_up_vector_data(20); - - x->compute_conj_dot(y.get(), expected.get()); - dx->compute_conj_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, SingleHipComputeNorm2IsEquivalentToRef) -{ - set_up_vector_data(1); - auto norm_size = gko::dim<2>{1, x->get_size()[1]}; - auto norm_expected = NormVector::create(this->ref, norm_size); - auto dnorm = NormVector::create(this->hip, norm_size); - - x->compute_norm2(norm_expected.get()); - dx->compute_norm2(dnorm.get()); - - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, 1e-14); -} - - -TEST_F(Dense, MultipleHipComputeNorm2IsEquivalentToRef) -{ - set_up_vector_data(20); - auto norm_size = gko::dim<2>{1, x->get_size()[1]}; - auto norm_expected = NormVector::create(this->ref, norm_size); - auto dnorm = NormVector::create(this->hip, norm_size); - - x->compute_norm2(norm_expected.get()); - dx->compute_norm2(dnorm.get()); - - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, 1e-14); -} - - -TEST_F(Dense, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(y.get(), expected.get()); - dx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, SimpleApplyMixedIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(convert(y).get(), convert(expected).get()); - dx->apply(convert(dy).get(), convert(dresult).get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7); -} - - -TEST_F(Dense, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, AdvancedApplyMixedIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(convert(alpha).get(), convert(y).get(), - convert(beta).get(), convert(expected).get()); - dx->apply(convert(dalpha).get(), convert(dy).get(), - convert(dbeta).get(), convert(dresult).get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7); -} - - -TEST_F(Dense, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(hip, complex_x); - - x->apply(complex_b.get(), complex_x.get()); - dx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Dense, ApplyToMixedComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(hip, complex_x); - - x->apply(complex_b.get(), complex_x.get()); - dx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7); -} - - -TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(hip, complex_x); - - x->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Dense, AdvancedApplyToMixedComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(65, 1); - auto dcomplex_x = gko::clone(hip, complex_x); - - x->apply(convert(alpha).get(), complex_b.get(), - convert(beta).get(), complex_x.get()); - dx->apply(convert(dalpha).get(), dcomplex_b.get(), - convert(dbeta).get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7); -} - - -TEST_F(Dense, ComputeDotComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(1234, 2); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(1234, 2); - auto dcomplex_x = gko::clone(hip, complex_x); - auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); - auto dresult = ComplexMtx::create(hip, gko::dim<2>{1, 2}); - - complex_b->compute_dot(complex_x.get(), result.get()); - dcomplex_b->compute_dot(dcomplex_x.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(result, dresult, 1e-14); -} - - -TEST_F(Dense, ComputeConjDotComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(1234, 2); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(1234, 2); - auto dcomplex_x = gko::clone(hip, complex_x); - auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); - auto dresult = ComplexMtx::create(hip, gko::dim<2>{1, 2}); - - complex_b->compute_conj_dot(complex_x.get(), result.get()); - dcomplex_b->compute_conj_dot(dcomplex_x.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(result, dresult, 1e-14); -} - - -TEST_F(Dense, ConvertToCooIsEquivalentToRef) -{ - set_up_apply_data(); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(hip); - - x->convert_to(coo_mtx.get()); - dx->convert_to(dcoo_mtx.get()); - - ASSERT_EQ(dcoo_mtx->get_num_stored_elements(), - coo_mtx->get_num_stored_elements()); - GKO_ASSERT_MTX_NEAR(dcoo_mtx.get(), coo_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToCooIsEquivalentToRef) -{ - set_up_apply_data(); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(hip); - - x->move_to(coo_mtx.get()); - dx->move_to(dcoo_mtx.get()); - - ASSERT_EQ(dcoo_mtx->get_num_stored_elements(), - coo_mtx->get_num_stored_elements()); - GKO_ASSERT_MTX_NEAR(dcoo_mtx.get(), coo_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(hip); - - x->convert_to(csr_mtx.get()); - dx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dcsr_mtx.get(), csr_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(hip); - - x->move_to(csr_mtx.get()); - dx->move_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dcsr_mtx.get(), csr_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(hip); - - x->convert_to(sparsity_mtx.get()); - dx->convert_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(hip); - - x->move_to(sparsity_mtx.get()); - dx->move_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToEllIsEquivalentToRef) -{ - set_up_apply_data(); - auto ell_mtx = gko::matrix::Ell<>::create(ref); - auto dell_mtx = gko::matrix::Ell<>::create(hip); - - x->convert_to(ell_mtx.get()); - dx->convert_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dell_mtx.get(), ell_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToEllIsEquivalentToRef) -{ - set_up_apply_data(); - auto ell_mtx = gko::matrix::Ell<>::create(ref); - auto dell_mtx = gko::matrix::Ell<>::create(hip); - - x->move_to(ell_mtx.get()); - dx->move_to(dell_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dell_mtx.get(), ell_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToSellpIsEquivalentToRef) -{ - set_up_apply_data(); - auto sellp_mtx = gko::matrix::Sellp<>::create(ref); - auto dsellp_mtx = gko::matrix::Sellp<>::create(hip); - - x->convert_to(sellp_mtx.get()); - dx->convert_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx, dsellp_mtx, 0); -} - - -TEST_F(Dense, MoveToSellpIsEquivalentToRef) -{ - set_up_apply_data(); - auto sellp_mtx = gko::matrix::Sellp<>::create(ref); - auto dsellp_mtx = gko::matrix::Sellp<>::create(hip); - - x->move_to(sellp_mtx.get()); - dx->move_to(dsellp_mtx.get()); - - GKO_ASSERT_MTX_NEAR(sellp_mtx, dsellp_mtx, 0); -} - - -TEST_F(Dense, ConvertsEmptyToSellp) -{ - auto dempty_mtx = Mtx::create(hip); - auto dsellp_mtx = gko::matrix::Sellp<>::create(hip); - - dempty_mtx->convert_to(dsellp_mtx.get()); - - ASSERT_EQ(hip->copy_val_to_host(dsellp_mtx->get_const_slice_sets()), 0); - ASSERT_FALSE(dsellp_mtx->get_size()); -} - - -TEST_F(Dense, CalculateNNZPerRowIsEquivalentToRef) -{ - set_up_apply_data(); - gko::array nnz_per_row(ref); - nnz_per_row.resize_and_reset(x->get_size()[0]); - gko::array dnnz_per_row(hip); - dnnz_per_row.resize_and_reset(dx->get_size()[0]); - - gko::kernels::reference::dense::count_nonzeros_per_row( - ref, x.get(), nnz_per_row.get_data()); - gko::kernels::hip::dense::count_nonzeros_per_row(hip, dx.get(), - dnnz_per_row.get_data()); - - auto tmp = gko::array(ref, dnnz_per_row); - for (auto i = 0; i < nnz_per_row.get_num_elems(); i++) { - ASSERT_EQ(nnz_per_row.get_const_data()[i], tmp.get_const_data()[i]); - } -} - - -TEST_F(Dense, ComputeMaxNNZPerRowIsEquivalentToRef) -{ - set_up_apply_data(); - gko::size_type max_nnz; - gko::size_type dmax_nnz; - - gko::kernels::reference::dense::compute_max_nnz_per_row(ref, x.get(), - max_nnz); - gko::kernels::hip::dense::compute_max_nnz_per_row(hip, dx.get(), dmax_nnz); - - ASSERT_EQ(max_nnz, dmax_nnz); -} - - -TEST_F(Dense, IsTransposable) -{ - set_up_apply_data(); - - auto trans = x->transpose(); - auto dtrans = dx->transpose(); - - GKO_ASSERT_MTX_NEAR(static_cast(dtrans.get()), - static_cast(trans.get()), 0); -} - - -TEST_F(Dense, IsTransposableIntoDenseCrossExecutor) -{ - set_up_apply_data(); - auto row_span = gko::span{0, x->get_size()[0] - 2}; - auto col_span = gko::span{0, x->get_size()[1] - 2}; - auto sub_x = x->create_submatrix(row_span, col_span); - auto sub_dx = dx->create_submatrix(row_span, col_span); - // create the target matrices on another executor to - // force temporary clone - auto trans = Mtx::create(ref, gko::transpose(sub_x->get_size())); - auto dtrans = Mtx::create(ref, gko::transpose(sub_x->get_size()), - sub_x->get_size()[0] + 4); - - sub_x->transpose(trans.get()); - sub_dx->transpose(dtrans.get()); - - GKO_ASSERT_MTX_NEAR(dtrans, trans, 0); -} - - -} // namespace diff --git a/hip/test/matrix/diagonal_kernels.hip.cpp b/hip/test/matrix/diagonal_kernels.hip.cpp deleted file mode 100644 index 9dc3a714d2d..00000000000 --- a/hip/test/matrix/diagonal_kernels.hip.cpp +++ /dev/null @@ -1,271 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include -#include -#include - - -#include - - -#include -#include - - -#include "core/matrix/diagonal_kernels.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class Diagonal : public ::testing::Test { -protected: - using ValueType = double; - using ComplexValueType = std::complex; - using Csr = gko::matrix::Csr; - using Diag = gko::matrix::Diagonal; - using Dense = gko::matrix::Dense; - using ComplexDiag = gko::matrix::Diagonal; - - Diagonal() -#ifdef GINKGO_FAST_TESTS - : mtx_size(152, 231), -#else - : mtx_size(532, 231), -#endif - rand_engine(42) - {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); - } - - std::unique_ptr gen_diag(int size) - { - auto diag = Diag::create(ref, size); - auto vals = diag->get_values(); - auto value_dist = std::normal_distribution<>(0.0, 1.0); - for (int i = 0; i < size; i++) { - vals[i] = gko::test::detail::get_rand_value(value_dist, - rand_engine); - } - return diag; - } - - std::unique_ptr gen_cdiag(int size) - { - auto cdiag = ComplexDiag::create(ref, size); - auto vals = cdiag->get_values(); - auto value_dist = std::normal_distribution<>(0.0, 1.0); - for (int i = 0; i < size; i++) { - vals[i] = ComplexValueType{ - gko::test::detail::get_rand_value( - value_dist, rand_engine)}; - } - return cdiag; - } - - void set_up_apply_data() - { - diag = gen_diag(mtx_size[0]); - ddiag = gko::clone(hip, diag); - dense1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[1]); - dense2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[0]); - denseexpected1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[1]); - denseexpected2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[0]); - ddense1 = gko::clone(hip, dense1); - ddense2 = gko::clone(hip, dense2); - denseresult1 = gko::clone(hip, denseexpected1); - denseresult2 = gko::clone(hip, denseexpected2); - csr1 = gen_mtx(mtx_size[0], mtx_size[1], 1); - csr2 = gen_mtx(mtx_size[1], mtx_size[0], 1); - csrexpected1 = gen_mtx(mtx_size[0], mtx_size[1], 1); - csrexpected2 = gen_mtx(mtx_size[1], mtx_size[0], 1); - dcsr1 = gko::clone(hip, csr1); - dcsr2 = gko::clone(hip, csr2); - csrresult1 = gko::clone(hip, csrexpected1); - csrresult2 = gko::clone(hip, csrexpected2); - } - - void set_up_complex_data() - { - cdiag = gen_cdiag(mtx_size[0]); - dcdiag = gko::clone(hip, cdiag); - } - - std::shared_ptr ref; - std::shared_ptr hip; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr diag; - std::unique_ptr ddiag; - std::unique_ptr cdiag; - std::unique_ptr dcdiag; - - std::unique_ptr dense1; - std::unique_ptr dense2; - std::unique_ptr denseexpected1; - std::unique_ptr denseexpected2; - std::unique_ptr denseresult1; - std::unique_ptr denseresult2; - std::unique_ptr ddense1; - std::unique_ptr ddense2; - std::unique_ptr csr1; - std::unique_ptr csr2; - std::unique_ptr dcsr1; - std::unique_ptr dcsr2; - std::unique_ptr csrexpected1; - std::unique_ptr csrexpected2; - std::unique_ptr csrresult1; - std::unique_ptr csrresult2; -}; - - -TEST_F(Diagonal, ApplyToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->apply(dense1.get(), denseexpected1.get()); - ddiag->apply(ddense1.get(), denseresult1.get()); - - GKO_ASSERT_MTX_NEAR(denseexpected1, denseresult1, 1e-14); -} - - -TEST_F(Diagonal, RightApplyToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->rapply(dense2.get(), denseexpected2.get()); - ddiag->rapply(ddense2.get(), denseresult2.get()); - - GKO_ASSERT_MTX_NEAR(denseexpected2, denseresult2, 1e-14); -} - - -TEST_F(Diagonal, ApplyToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->apply(csr1.get(), csrexpected1.get()); - ddiag->apply(dcsr1.get(), csrresult1.get()); - - GKO_ASSERT_MTX_NEAR(csrexpected1, csrresult1, 1e-14); -} - - -TEST_F(Diagonal, RightApplyToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->rapply(csr2.get(), csrexpected2.get()); - ddiag->rapply(dcsr2.get(), csrresult2.get()); - - GKO_ASSERT_MTX_NEAR(csrexpected2, csrresult2, 1e-14); -} - - -TEST_F(Diagonal, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->convert_to(csr1.get()); - ddiag->convert_to(dcsr1.get()); - - GKO_ASSERT_MTX_NEAR(csr1, dcsr1, 0); -} - - -TEST_F(Diagonal, ConjTransposeIsEquivalentToRef) -{ - set_up_complex_data(); - - auto trans = cdiag->conj_transpose(); - auto trans_diag = static_cast(trans.get()); - auto dtrans = dcdiag->conj_transpose(); - auto dtrans_diag = static_cast(dtrans.get()); - - GKO_ASSERT_MTX_NEAR(trans_diag, dtrans_diag, 0); -} - - -TEST_F(Diagonal, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - diag->compute_absolute_inplace(); - ddiag->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(diag, ddiag, 1e-14); -} - - -TEST_F(Diagonal, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_diag = diag->compute_absolute(); - auto dabs_diag = ddiag->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_diag, dabs_diag, 1e-14); -} - - -} // namespace diff --git a/hip/test/matrix/ell_kernels.hip.cpp b/hip/test/matrix/ell_kernels.hip.cpp deleted file mode 100644 index a5ce8c95843..00000000000 --- a/hip/test/matrix/ell_kernels.hip.cpp +++ /dev/null @@ -1,607 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/matrix/ell_kernels.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class Ell : public ::testing::Test { -protected: - using Mtx = gko::matrix::Ell<>; - using Vec = gko::matrix::Dense<>; - using Vec2 = gko::matrix::Dense; - using ComplexVec = gko::matrix::Dense>; - - Ell() - : rand_engine(42), size{532, 231}, num_els_rowwise{300}, ell_stride{600} - {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data(int num_rows = 532, int num_cols = 231, - int num_vectors = 1, - int num_stored_elements_per_row = 0, int stride = 0) - { - mtx = Mtx::create(ref, gko::dim<2>{}, num_stored_elements_per_row, - stride); - mtx->copy_from(gen_mtx(num_rows, num_cols)); - expected = gen_mtx(num_rows, num_vectors); - expected2 = Vec2::create(ref); - expected2->copy_from(expected.get()); - y = gen_mtx(num_cols, num_vectors); - y2 = Vec2::create(ref); - y2->copy_from(y.get()); - alpha = gko::initialize({2.0}, ref); - alpha2 = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - beta2 = gko::initialize({-1.0}, ref); - dmtx = gko::clone(hip, mtx); - dresult = gko::clone(hip, expected); - dresult2 = gko::clone(hip, expected2); - dy = gko::clone(hip, y); - dy2 = gko::clone(hip, y2); - dalpha = gko::clone(hip, alpha); - dalpha2 = gko::clone(hip, alpha2); - dbeta = gko::clone(hip, beta); - dbeta2 = gko::clone(hip, beta2); - } - - - std::shared_ptr ref; - std::shared_ptr hip; - - std::default_random_engine rand_engine; - gko::dim<2> size; - gko::size_type num_els_rowwise; - gko::size_type ell_stride; - - std::unique_ptr mtx; - std::unique_ptr expected; - std::unique_ptr expected2; - std::unique_ptr y; - std::unique_ptr y2; - std::unique_ptr alpha; - std::unique_ptr alpha2; - std::unique_ptr beta; - std::unique_ptr beta2; - - std::unique_ptr dmtx; - std::unique_ptr dresult; - std::unique_ptr dresult2; - std::unique_ptr dy; - std::unique_ptr dy2; - std::unique_ptr dalpha; - std::unique_ptr dalpha2; - std::unique_ptr dbeta; - std::unique_ptr dbeta2; -}; - - -TEST_F(Ell, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyIsEquivalentToRef1) -{ - set_up_apply_data(); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedSimpleApplyIsEquivalentToRef2) -{ - set_up_apply_data(); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyIsEquivalentToRef3) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyIsEquivalentToRef1) -{ - set_up_apply_data(); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedAdvancedApplyIsEquivalentToRef2) -{ - set_up_apply_data(); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyIsEquivalentToRef3) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, SimpleApplyWithStrideIsEquivalentToRef) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideIsEquivalentToRef1) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideIsEquivalentToRef2) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideIsEquivalentToRef3) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, AdvancedApplyWithStrideIsEquivalentToRef) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideIsEquivalentToRef1) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideIsEquivalentToRef2) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideIsEquivalentToRef3) -{ - set_up_apply_data(size[0], size[1], 1, num_els_rowwise, ell_stride); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, SimpleApplyWithStrideToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef1) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef2) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef3) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, AdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef1) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef2) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef3) -{ - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-6); -} - - -TEST_F(Ell, SimpleApplyByAtomicIsEquivalentToRef) -{ - set_up_apply_data(10, 10000); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, AdvancedByAtomicApplyIsEquivalentToRef) -{ - set_up_apply_data(10, 10000); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, SimpleApplyByAtomicToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(10, 10000, 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, AdvancedByAtomicToDenseMatrixApplyIsEquivalentToRef) -{ - set_up_apply_data(10, 10000, 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, SimpleApplyOnSmallMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, 10); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyOnSmallMatrixToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, 10, 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, SimpleApplyOnSmallMatrixToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, 10, 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyOnSmallMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, 10); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(size[1], 3); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(size[0], 3); - auto dcomplex_x = gko::clone(hip, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(size[1], 3); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(size[0], 3); - auto dcomplex_x = gko::clone(hip, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Ell, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(hip); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Ell, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(hip); - - mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Ell, CalculateNNZPerRowIsEquivalentToRef) -{ - set_up_apply_data(); - - gko::array nnz_per_row{ref, mtx->get_size()[0]}; - gko::array dnnz_per_row{hip, dmtx->get_size()[0]}; - - gko::kernels::reference::ell::count_nonzeros_per_row( - ref, mtx.get(), nnz_per_row.get_data()); - gko::kernels::hip::ell::count_nonzeros_per_row(hip, dmtx.get(), - dnnz_per_row.get_data()); - - GKO_ASSERT_ARRAY_EQ(nnz_per_row, dnnz_per_row); -} - - -TEST_F(Ell, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Ell, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Ell, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -} // namespace diff --git a/hip/test/matrix/fft_kernels.hip.cpp b/hip/test/matrix/fft_kernels.hip.cpp index 6b2f4a7661e..9b2403e93a7 100644 --- a/hip/test/matrix/fft_kernels.hip.cpp +++ b/hip/test/matrix/fft_kernels.hip.cpp @@ -33,9 +33,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include - - #include @@ -44,240 +41,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#include -#include - - -#include "hip/test/utils.hip.hpp" - - -namespace { - - -template -class Fft : public ::testing::Test { -protected: - using value_type = ValueType; - using Vec = gko::matrix::Dense; - using Mtx = gko::matrix::Fft; - using Mtx2 = gko::matrix::Fft2; - using Mtx3 = gko::matrix::Fft3; - - Fft() - : rand_engine(1364245), - n1{16}, - n2{32}, - n3{64}, - n{n1 * n2 * n3}, - cols{3}, - subcols{2}, - out_stride{6} - {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - - data = gko::test::generate_random_matrix( - n, cols, std::uniform_int_distribution<>(1, cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - ddata = Vec::create(hip); - ddata->copy_from(this->data.get()); - data_strided = data->create_submatrix({0, n}, {0, subcols}); - ddata_strided = ddata->create_submatrix({0, n}, {0, subcols}); - out = data->clone(); - dout = data->clone(); - out_strided = Vec::create(ref, data_strided->get_size(), out_stride); - dout_strided = Vec::create(hip, data_strided->get_size(), out_stride); - fft = Mtx::create(ref, n); - dfft = Mtx::create(hip, n); - ifft = Mtx::create(ref, n, true); - difft = Mtx::create(hip, n, true); - fft2 = Mtx2::create(ref, n1 * n2, n3); - dfft2 = Mtx2::create(hip, n1 * n2, n3); - ifft2 = Mtx2::create(ref, n1 * n2, n3, true); - difft2 = Mtx2::create(hip, n1 * n2, n3, true); - fft3 = Mtx3::create(ref, n1, n2, n3); - dfft3 = Mtx3::create(hip, n1, n2, n3); - ifft3 = Mtx3::create(ref, n1, n2, n3, true); - difft3 = Mtx3::create(hip, n1, n2, n3, true); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - std::shared_ptr ref; - std::shared_ptr hip; - std::default_random_engine rand_engine; - size_t n1; - size_t n2; - size_t n3; - size_t n; - size_t cols; - size_t subcols; - size_t out_stride; - std::unique_ptr data; - std::unique_ptr ddata; - std::unique_ptr data_strided; - std::unique_ptr ddata_strided; - std::unique_ptr out; - std::unique_ptr dout; - std::unique_ptr out_strided; - std::unique_ptr dout_strided; - std::unique_ptr fft; - std::unique_ptr dfft; - std::unique_ptr ifft; - std::unique_ptr difft; - std::unique_ptr fft2; - std::unique_ptr dfft2; - std::unique_ptr ifft2; - std::unique_ptr difft2; - std::unique_ptr fft3; - std::unique_ptr dfft3; - std::unique_ptr ifft3; - std::unique_ptr difft3; -}; - - -TYPED_TEST_SUITE(Fft, gko::test::ComplexValueTypes, TypenameNameGenerator); - - -TYPED_TEST(Fft, Apply1DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft->apply(this->data.get(), this->out.get()); - this->dfft->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided1DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft->apply(this->data_strided.get(), this->out_strided.get()); - this->dfft->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply1DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft->apply(this->data.get(), this->out.get()); - this->difft->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided1DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft->apply(this->data_strided.get(), this->out_strided.get()); - this->difft->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply2DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft2->apply(this->data.get(), this->out.get()); - this->dfft2->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided2DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft2->apply(this->data_strided.get(), this->out_strided.get()); - this->dfft2->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply2DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft2->apply(this->data.get(), this->out.get()); - this->difft2->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided2DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft2->apply(this->data_strided.get(), this->out_strided.get()); - this->difft2->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply3DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft3->apply(this->data.get(), this->out.get()); - this->dfft3->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided3DIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->fft3->apply(this->data_strided.get(), this->out_strided.get()); - this->dfft3->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} - - -TYPED_TEST(Fft, Apply3DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft3->apply(this->data.get(), this->out.get()); - this->difft3->apply(this->ddata.get(), this->dout.get()); - - GKO_ASSERT_MTX_NEAR(this->out, this->dout, r::value); -} - - -TYPED_TEST(Fft, ApplyStrided3DInverseIsEqualToReference) -{ - using T = typename TestFixture::value_type; - - this->ifft3->apply(this->data_strided.get(), this->out_strided.get()); - this->difft3->apply(this->ddata_strided.get(), this->dout_strided.get()); - - GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); -} // since hipFFT is optional, we test the exception behavior here @@ -291,6 +54,3 @@ TEST(AssertNoHipfftErrors, DoesNotThrowOnSuccess) { ASSERT_NO_THROW(GKO_ASSERT_NO_HIPFFT_ERRORS(HIPFFT_SUCCESS)); } - - -} // namespace diff --git a/hip/test/matrix/hybrid_kernels.hip.cpp b/hip/test/matrix/hybrid_kernels.hip.cpp deleted file mode 100644 index c75370819b9..00000000000 --- a/hip/test/matrix/hybrid_kernels.hip.cpp +++ /dev/null @@ -1,279 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/matrix/hybrid_kernels.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class Hybrid : public ::testing::Test { -protected: - using Mtx = gko::matrix::Hybrid<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; - - Hybrid() : rand_engine(42) {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data(int num_vectors = 1, - std::shared_ptr strategy = - std::make_shared()) - { - mtx = Mtx::create(ref, strategy); - mtx->copy_from(gen_mtx(532, 231, 1)); - expected = gen_mtx(532, num_vectors, 1); - y = gen_mtx(231, num_vectors, 1); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = Mtx::create(hip, strategy); - dmtx->copy_from(mtx.get()); - dresult = gko::clone(hip, expected); - dy = gko::clone(hip, y); - dalpha = gko::clone(hip, alpha); - dbeta = gko::clone(hip, beta); - } - - - std::shared_ptr ref; - std::shared_ptr hip; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; -}; - - -TEST_F(Hybrid, SubMatrixExecutorAfterCopyIsEquivalentToExcutor) -{ - set_up_apply_data(); - - auto coo_mtx = dmtx->get_coo(); - auto ell_mtx = dmtx->get_ell(); - - ASSERT_EQ(coo_mtx->get_executor(), hip); - ASSERT_EQ(ell_mtx->get_executor(), hip); - ASSERT_EQ(dmtx->get_executor(), hip); -} - - -TEST_F(Hybrid, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Hybrid, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Hybrid, SimpleApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Hybrid, AdvancedApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Hybrid, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(hip, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Hybrid, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(hip, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(hip, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Hybrid, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(1, std::make_shared(2)); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(hip); - - mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Hybrid, MoveToCsrIsEquivalentToRef) -{ - set_up_apply_data(1, std::make_shared(2)); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(hip); - - mtx->move_to(csr_mtx.get()); - dmtx->move_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Hybrid, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Hybrid, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Hybrid, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(1, std::make_shared(2)); - using AbsMtx = gko::remove_complex; - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - auto abs_strategy = gko::as(abs_mtx->get_strategy()); - auto dabs_strategy = - gko::as(dabs_mtx->get_strategy()); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); - GKO_ASSERT_EQ(abs_strategy->get_num_columns(), - dabs_strategy->get_num_columns()); - GKO_ASSERT_EQ(abs_strategy->get_num_columns(), 2); -} - - -} // namespace diff --git a/hip/test/preconditioner/CMakeLists.txt b/hip/test/preconditioner/CMakeLists.txt deleted file mode 100644 index 6769092fde5..00000000000 --- a/hip/test/preconditioner/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -ginkgo_create_test(jacobi_kernels) -ginkgo_create_hip_test(isai_kernels) diff --git a/hip/test/solver/CMakeLists.txt b/hip/test/solver/CMakeLists.txt index bcc6e71b9c4..a3b86589410 100644 --- a/hip/test/solver/CMakeLists.txt +++ b/hip/test/solver/CMakeLists.txt @@ -1,6 +1,2 @@ -ginkgo_create_test(gmres_kernels) -ginkgo_create_test(cb_gmres_kernels) -ginkgo_create_test(idr_kernels) ginkgo_create_test(lower_trs_kernels) -ginkgo_create_test(multigrid_kernels) ginkgo_create_test(upper_trs_kernels) diff --git a/hip/test/solver/cb_gmres_kernels.cpp b/hip/test/solver/cb_gmres_kernels.cpp deleted file mode 100644 index e266ccc1f3f..00000000000 --- a/hip/test/solver/cb_gmres_kernels.cpp +++ /dev/null @@ -1,347 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/solver/cb_gmres_accessor.hpp" -#include "core/solver/cb_gmres_kernels.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class CbGmres : public ::testing::Test { -protected: - using value_type = double; - using storage_type = float; - using index_type = int; - using size_type = gko::size_type; - using Range3dHelper = - gko::cb_gmres::Range3dHelper; - using Range3d = typename Range3dHelper::Range; - using Dense = gko::matrix::Dense; - using Mtx = Dense; - static constexpr unsigned int default_krylov_dim_mixed{100}; - - CbGmres() : rand_engine(30) {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(num_cols, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - Range3dHelper generate_krylov_helper(gko::dim<3> size) - { - auto helper = Range3dHelper{ref, size}; - auto& bases = helper.get_bases(); - const auto num_rows = size[0] * size[1]; - const auto num_cols = size[2]; - auto temp_krylov_bases = gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(num_cols, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, - ref); - std::copy_n(temp_krylov_bases->get_const_values(), - bases.get_num_elems(), bases.get_data()); - // Only useful when the Accessor actually has a scale - auto range = helper.get_range(); - auto dist = std::normal_distribution(-1, 1); - for (size_type k = 0; k < size[0]; ++k) { - for (size_type i = 0; i < size[2]; ++i) { - gko::cb_gmres::helper_functions_accessor::write_scalar( - range, k, i, dist(rand_engine)); - } - } - return helper; - } - - void initialize_data() - { -#ifdef GINKGO_FAST_TESTS - int m = 123; -#else - int m = 597; -#endif - int n = 43; - x = gen_mtx(m, n); - y = gen_mtx(default_krylov_dim_mixed, n); - before_preconditioner = Mtx::create_with_config_of(x.get()); - b = gen_mtx(m, n); - arnoldi_norm = gen_mtx(3, n); - gko::dim<3> krylov_bases_dim(default_krylov_dim_mixed + 1, m, n); - range_helper = generate_krylov_helper(krylov_bases_dim); - - next_krylov_basis = gen_mtx(m, n); - hessenberg = - gen_mtx(default_krylov_dim_mixed + 1, default_krylov_dim_mixed * n); - hessenberg_iter = gen_mtx(default_krylov_dim_mixed + 1, n); - buffer_iter = gen_mtx(default_krylov_dim_mixed + 1, n); - residual = gen_mtx(m, n); - residual_norm = gen_mtx(1, n); - residual_norm_collection = gen_mtx(default_krylov_dim_mixed + 1, n); - givens_sin = gen_mtx(default_krylov_dim_mixed, n); - givens_cos = gen_mtx(default_krylov_dim_mixed, n); - stop_status = - std::make_unique>(ref, n); - for (size_t i = 0; i < stop_status->get_num_elems(); ++i) { - stop_status->get_data()[i].reset(); - } - reorth_status = - std::make_unique>(ref, n); - for (size_t i = 0; i < reorth_status->get_num_elems(); ++i) { - reorth_status->get_data()[i].reset(); - } - final_iter_nums = std::make_unique>(ref, n); - for (size_t i = 0; i < final_iter_nums->get_num_elems(); ++i) { - final_iter_nums->get_data()[i] = 5; - } - num_reorth = std::make_unique>(ref, n); - for (size_t i = 0; i < num_reorth->get_num_elems(); ++i) { - num_reorth->get_data()[i] = 5; - } - - d_x = gko::clone(hip, x); - d_before_preconditioner = Mtx::create_with_config_of(d_x.get()); - d_y = gko::clone(hip, y); - d_b = gko::clone(hip, b); - d_arnoldi_norm = gko::clone(hip, arnoldi_norm); - d_range_helper = Range3dHelper{hip, {}}; - d_range_helper = range_helper; - d_next_krylov_basis = gko::clone(hip, next_krylov_basis); - d_hessenberg = gko::clone(hip, hessenberg); - d_hessenberg_iter = gko::clone(hip, hessenberg_iter); - d_buffer_iter = gko::clone(hip, buffer_iter); - d_residual = gko::clone(hip, residual); - d_residual_norm = gko::clone(hip, residual_norm); - d_residual_norm_collection = gko::clone(hip, residual_norm_collection); - d_givens_sin = gko::clone(hip, givens_sin); - d_givens_cos = gko::clone(hip, givens_cos); - d_stop_status = std::make_unique>( - hip, *stop_status); - d_reorth_status = std::make_unique>( - hip, *reorth_status); - d_final_iter_nums = - std::make_unique>(hip, *final_iter_nums); - d_num_reorth = - std::make_unique>(hip, *num_reorth); - } - - void assert_krylov_bases_near() - { - gko::array d_to_host{ref}; - auto& krylov_bases = range_helper.get_bases(); - d_to_host = d_range_helper.get_bases(); - const auto tolerance = r::value; - using std::abs; - for (gko::size_type i = 0; i < krylov_bases.get_num_elems(); ++i) { - const auto ref_value = krylov_bases.get_const_data()[i]; - const auto dev_value = d_to_host.get_const_data()[i]; - ASSERT_LE(abs(dev_value - ref_value), tolerance); - } - } - - std::shared_ptr ref; - std::shared_ptr hip; - - std::default_random_engine rand_engine; - - std::unique_ptr before_preconditioner; - std::unique_ptr x; - std::unique_ptr y; - std::unique_ptr b; - std::unique_ptr arnoldi_norm; - Range3dHelper range_helper; - std::unique_ptr next_krylov_basis; - std::unique_ptr hessenberg; - std::unique_ptr hessenberg_iter; - std::unique_ptr buffer_iter; - std::unique_ptr residual; - std::unique_ptr residual_norm; - std::unique_ptr residual_norm_collection; - std::unique_ptr givens_sin; - std::unique_ptr givens_cos; - std::unique_ptr> stop_status; - std::unique_ptr> reorth_status; - std::unique_ptr> final_iter_nums; - std::unique_ptr> num_reorth; - - std::unique_ptr d_x; - std::unique_ptr d_before_preconditioner; - std::unique_ptr d_y; - std::unique_ptr d_b; - std::unique_ptr d_arnoldi_norm; - Range3dHelper d_range_helper; - std::unique_ptr d_next_krylov_basis; - std::unique_ptr d_hessenberg; - std::unique_ptr d_hessenberg_iter; - std::unique_ptr d_buffer_iter; - std::unique_ptr d_residual; - std::unique_ptr d_residual_norm; - std::unique_ptr d_residual_norm_collection; - std::unique_ptr d_givens_sin; - std::unique_ptr d_givens_cos; - std::unique_ptr> d_stop_status; - std::unique_ptr> d_reorth_status; - std::unique_ptr> d_final_iter_nums; - std::unique_ptr> d_num_reorth; -}; - - -TEST_F(CbGmres, HipCbGmresInitialize1IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::cb_gmres::initialize_1( - ref, b.get(), residual.get(), givens_sin.get(), givens_cos.get(), - stop_status.get(), default_krylov_dim_mixed); - gko::kernels::hip::cb_gmres::initialize_1( - hip, d_b.get(), d_residual.get(), d_givens_sin.get(), - d_givens_cos.get(), d_stop_status.get(), default_krylov_dim_mixed); - - GKO_ASSERT_MTX_NEAR(d_residual, residual, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_stop_status, *stop_status); -} - - -TEST_F(CbGmres, HipCbGmresInitialize2IsEquivalentToRef) -{ - initialize_data(); - gko::array tmp{ref}; - gko::array dtmp{hip}; - - gko::kernels::reference::cb_gmres::initialize_2( - ref, residual.get(), residual_norm.get(), - residual_norm_collection.get(), arnoldi_norm.get(), - range_helper.get_range(), next_krylov_basis.get(), - final_iter_nums.get(), tmp, default_krylov_dim_mixed); - gko::kernels::hip::cb_gmres::initialize_2( - hip, d_residual.get(), d_residual_norm.get(), - d_residual_norm_collection.get(), d_arnoldi_norm.get(), - d_range_helper.get_range(), d_next_krylov_basis.get(), - d_final_iter_nums.get(), dtmp, default_krylov_dim_mixed); - - GKO_ASSERT_MTX_NEAR(d_arnoldi_norm, arnoldi_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - assert_krylov_bases_near(); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(CbGmres, HipCbGmresStep1IsEquivalentToRef) -{ - initialize_data(); - int iter = 5; - - gko::kernels::reference::cb_gmres::step_1( - ref, next_krylov_basis.get(), givens_sin.get(), givens_cos.get(), - residual_norm.get(), residual_norm_collection.get(), - range_helper.get_range(), hessenberg_iter.get(), buffer_iter.get(), - arnoldi_norm.get(), iter, final_iter_nums.get(), stop_status.get(), - reorth_status.get(), num_reorth.get()); - gko::kernels::hip::cb_gmres::step_1( - hip, d_next_krylov_basis.get(), d_givens_sin.get(), d_givens_cos.get(), - d_residual_norm.get(), d_residual_norm_collection.get(), - d_range_helper.get_range(), d_hessenberg_iter.get(), - d_buffer_iter.get(), d_arnoldi_norm.get(), iter, - d_final_iter_nums.get(), d_stop_status.get(), d_reorth_status.get(), - d_num_reorth.get()); - - GKO_ASSERT_MTX_NEAR(d_arnoldi_norm, arnoldi_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_next_krylov_basis, next_krylov_basis, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_hessenberg_iter, hessenberg_iter, 1e-14); - assert_krylov_bases_near(); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(CbGmres, HipCbGmresStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::cb_gmres::step_2( - ref, residual_norm_collection.get(), - range_helper.get_range().get_accessor().to_const(), hessenberg.get(), - y.get(), before_preconditioner.get(), final_iter_nums.get()); - gko::kernels::hip::cb_gmres::step_2( - hip, d_residual_norm_collection.get(), - d_range_helper.get_range().get_accessor().to_const(), - d_hessenberg.get(), d_y.get(), d_before_preconditioner.get(), - d_final_iter_nums.get()); - - GKO_ASSERT_MTX_NEAR(d_y, y, 1e-14); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-14); -} - - -} // namespace diff --git a/hip/test/solver/gmres_kernels.cpp b/hip/test/solver/gmres_kernels.cpp deleted file mode 100644 index a6d7158e9ee..00000000000 --- a/hip/test/solver/gmres_kernels.cpp +++ /dev/null @@ -1,293 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include - - -#include "core/solver/gmres_kernels.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class Gmres : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Mtx = gko::matrix::Dense; - using norm_type = gko::remove_complex; - using NormVector = gko::matrix::Dense; - template - using Dense = typename gko::matrix::Dense; - - Gmres() : rand_engine(30) {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - template - std::unique_ptr> gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix>( - num_rows, num_cols, - std::uniform_int_distribution(num_cols, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - - void initialize_data(int nrhs = 43) - { -#ifdef GINKGO_FAST_TESTS - int m = 123; -#else - int m = 597; -#endif - x = gen_mtx(m, nrhs); - y = gen_mtx(gko::solver::default_krylov_dim, nrhs); - before_preconditioner = Mtx::create_with_config_of(x.get()); - b = gen_mtx(m, nrhs); - krylov_bases = gen_mtx(m * (gko::solver::default_krylov_dim + 1), nrhs); - hessenberg = gen_mtx(gko::solver::default_krylov_dim + 1, - gko::solver::default_krylov_dim * nrhs); - hessenberg_iter = gen_mtx(gko::solver::default_krylov_dim + 1, nrhs); - residual = gen_mtx(m, nrhs); - residual_norm = gen_mtx(1, nrhs); - residual_norm_collection = - gen_mtx(gko::solver::default_krylov_dim + 1, nrhs); - givens_sin = gen_mtx(gko::solver::default_krylov_dim, nrhs); - givens_cos = gen_mtx(gko::solver::default_krylov_dim, nrhs); - stop_status = - std::make_unique>(ref, nrhs); - for (size_t i = 0; i < stop_status->get_num_elems(); ++i) { - stop_status->get_data()[i].reset(); - } - final_iter_nums = - std::make_unique>(ref, nrhs); - for (size_t i = 0; i < final_iter_nums->get_num_elems(); ++i) { - final_iter_nums->get_data()[i] = 5; - } - - d_x = gko::clone(hip, x); - d_before_preconditioner = Mtx::create_with_config_of(d_x.get()); - d_y = gko::clone(hip, y); - d_b = gko::clone(hip, b); - d_krylov_bases = gko::clone(hip, krylov_bases); - d_hessenberg = gko::clone(hip, hessenberg); - d_hessenberg_iter = gko::clone(hip, hessenberg_iter); - d_residual = gko::clone(hip, residual); - d_residual_norm = gko::clone(hip, residual_norm); - d_residual_norm_collection = gko::clone(hip, residual_norm_collection); - d_givens_sin = gko::clone(hip, givens_sin); - d_givens_cos = gko::clone(hip, givens_cos); - d_stop_status = std::make_unique>( - hip, *stop_status); - d_final_iter_nums = - std::make_unique>(hip, *final_iter_nums); - } - - std::shared_ptr ref; - std::shared_ptr hip; - - std::default_random_engine rand_engine; - - std::unique_ptr before_preconditioner; - std::unique_ptr x; - std::unique_ptr y; - std::unique_ptr b; - std::unique_ptr krylov_bases; - std::unique_ptr hessenberg; - std::unique_ptr hessenberg_iter; - std::unique_ptr residual; - std::unique_ptr residual_norm; - std::unique_ptr residual_norm_collection; - std::unique_ptr givens_sin; - std::unique_ptr givens_cos; - std::unique_ptr> stop_status; - std::unique_ptr> final_iter_nums; - - std::unique_ptr d_x; - std::unique_ptr d_before_preconditioner; - std::unique_ptr d_y; - std::unique_ptr d_b; - std::unique_ptr d_krylov_bases; - std::unique_ptr d_hessenberg; - std::unique_ptr d_hessenberg_iter; - std::unique_ptr d_residual; - std::unique_ptr d_residual_norm; - std::unique_ptr d_residual_norm_collection; - std::unique_ptr d_givens_sin; - std::unique_ptr d_givens_cos; - std::unique_ptr> d_stop_status; - std::unique_ptr> d_final_iter_nums; -}; - - -TEST_F(Gmres, HipGmresInitialize1IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::gmres::initialize_1( - ref, b.get(), residual.get(), givens_sin.get(), givens_cos.get(), - stop_status.get(), gko::solver::default_krylov_dim); - gko::kernels::hip::gmres::initialize_1( - hip, d_b.get(), d_residual.get(), d_givens_sin.get(), - d_givens_cos.get(), d_stop_status.get(), - gko::solver::default_krylov_dim); - - GKO_ASSERT_MTX_NEAR(d_residual, residual, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_stop_status, *stop_status); -} - - -TEST_F(Gmres, HipGmresInitialize2IsEquivalentToRef) -{ - initialize_data(); - gko::array tmp{ref}; - gko::array dtmp{hip}; - - gko::kernels::reference::gmres::initialize_2( - ref, residual.get(), residual_norm.get(), - residual_norm_collection.get(), krylov_bases.get(), - final_iter_nums.get(), tmp, gko::solver::default_krylov_dim); - gko::kernels::hip::gmres::initialize_2( - hip, d_residual.get(), d_residual_norm.get(), - d_residual_norm_collection.get(), d_krylov_bases.get(), - d_final_iter_nums.get(), dtmp, gko::solver::default_krylov_dim); - - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_krylov_bases, krylov_bases, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(Gmres, HipGmresStep1IsEquivalentToRef) -{ - initialize_data(); - int iter = 5; - - gko::kernels::reference::gmres::step_1( - ref, x->get_size()[0], givens_sin.get(), givens_cos.get(), - residual_norm.get(), residual_norm_collection.get(), krylov_bases.get(), - hessenberg_iter.get(), iter, final_iter_nums.get(), stop_status.get()); - gko::kernels::hip::gmres::step_1( - hip, d_x->get_size()[0], d_givens_sin.get(), d_givens_cos.get(), - d_residual_norm.get(), d_residual_norm_collection.get(), - d_krylov_bases.get(), d_hessenberg_iter.get(), iter, - d_final_iter_nums.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_hessenberg_iter, hessenberg_iter, 1e-14); - GKO_ASSERT_MTX_NEAR(d_krylov_bases, krylov_bases, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(Gmres, HipGmresStep1OnSingleRHSIsEquivalentToRef) -{ - initialize_data(1); - int iter = 5; - - gko::kernels::reference::gmres::step_1( - ref, x->get_size()[0], givens_sin.get(), givens_cos.get(), - residual_norm.get(), residual_norm_collection.get(), krylov_bases.get(), - hessenberg_iter.get(), iter, final_iter_nums.get(), stop_status.get()); - gko::kernels::hip::gmres::step_1( - hip, d_x->get_size()[0], d_givens_sin.get(), d_givens_cos.get(), - d_residual_norm.get(), d_residual_norm_collection.get(), - d_krylov_bases.get(), d_hessenberg_iter.get(), iter, - d_final_iter_nums.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_hessenberg_iter, hessenberg_iter, 1e-14); - GKO_ASSERT_MTX_NEAR(d_krylov_bases, krylov_bases, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(Gmres, HipGmresStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::gmres::step_2(ref, residual_norm_collection.get(), - krylov_bases.get(), hessenberg.get(), - y.get(), before_preconditioner.get(), - final_iter_nums.get()); - gko::kernels::hip::gmres::step_2(hip, d_residual_norm_collection.get(), - d_krylov_bases.get(), d_hessenberg.get(), - d_y.get(), d_before_preconditioner.get(), - d_final_iter_nums.get()); - - GKO_ASSERT_MTX_NEAR(d_y, y, 1e-14); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-14); -} - - -} // namespace diff --git a/hip/test/solver/idr_kernels.cpp b/hip/test/solver/idr_kernels.cpp deleted file mode 100644 index 66a61bebfe6..00000000000 --- a/hip/test/solver/idr_kernels.cpp +++ /dev/null @@ -1,357 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include - - -#include "core/solver/idr_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Idr : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using Solver = gko::solver::Idr<>; - - Idr() : rand_engine(30) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - - hip_idr_factory = - Solver::build() - .with_deterministic(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(hip)) - .on(hip); - - ref_idr_factory = - Solver::build() - .with_deterministic(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(ref)) - .on(ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); - } - - void initialize_data(int size = 597, int input_nrhs = 17) - { - nrhs = input_nrhs; - int s = 4; - mtx = gen_mtx(size, size); - x = gen_mtx(size, nrhs); - b = gen_mtx(size, nrhs); - r = gen_mtx(size, nrhs); - m = gen_mtx(s, nrhs * s); - f = gen_mtx(s, nrhs); - g = gen_mtx(size, nrhs * s); - u = gen_mtx(size, nrhs * s); - c = gen_mtx(s, nrhs); - v = gen_mtx(size, nrhs); - p = gen_mtx(s, size); - alpha = gen_mtx(1, nrhs); - omega = gen_mtx(1, nrhs); - tht = gen_mtx(1, nrhs); - residual_norm = gen_mtx(1, nrhs); - stop_status = - std::make_unique>(ref, nrhs); - for (size_t i = 0; i < nrhs; ++i) { - stop_status->get_data()[i].reset(); - } - - d_mtx = gko::clone(hip, mtx); - d_x = gko::clone(hip, x); - d_b = gko::clone(hip, b); - d_r = gko::clone(hip, r); - d_m = gko::clone(hip, m); - d_f = gko::clone(hip, f); - d_g = gko::clone(hip, g); - d_u = gko::clone(hip, u); - d_c = gko::clone(hip, c); - d_v = gko::clone(hip, v); - d_p = gko::clone(hip, p); - d_alpha = gko::clone(hip, alpha); - d_omega = gko::clone(hip, omega); - d_tht = gko::clone(hip, tht); - d_residual_norm = gko::clone(hip, residual_norm); - d_stop_status = std::make_unique>( - hip, *stop_status); - } - - std::shared_ptr ref; - std::shared_ptr hip; - - std::default_random_engine rand_engine; - - std::shared_ptr mtx; - std::shared_ptr d_mtx; - std::unique_ptr hip_idr_factory; - std::unique_ptr ref_idr_factory; - - gko::size_type nrhs; - - std::unique_ptr x; - std::unique_ptr b; - std::unique_ptr r; - std::unique_ptr m; - std::unique_ptr f; - std::unique_ptr g; - std::unique_ptr u; - std::unique_ptr c; - std::unique_ptr v; - std::unique_ptr p; - std::unique_ptr alpha; - std::unique_ptr omega; - std::unique_ptr tht; - std::unique_ptr residual_norm; - std::unique_ptr> stop_status; - - std::unique_ptr d_x; - std::unique_ptr d_b; - std::unique_ptr d_r; - std::unique_ptr d_m; - std::unique_ptr d_f; - std::unique_ptr d_g; - std::unique_ptr d_u; - std::unique_ptr d_c; - std::unique_ptr d_v; - std::unique_ptr d_p; - std::unique_ptr d_alpha; - std::unique_ptr d_omega; - std::unique_ptr d_tht; - std::unique_ptr d_residual_norm; - std::unique_ptr> d_stop_status; -}; - - -TEST_F(Idr, IdrInitializeIsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::idr::initialize(ref, nrhs, m.get(), p.get(), true, - stop_status.get()); - gko::kernels::hip::idr::initialize(hip, nrhs, d_m.get(), d_p.get(), true, - d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(m, d_m, 1e-14); - GKO_ASSERT_MTX_NEAR(p, d_p, 1e-14); -} - - -TEST_F(Idr, IdrStep1IsEquivalentToRef) -{ - initialize_data(); - - gko::size_type k = 2; - gko::kernels::reference::idr::step_1(ref, nrhs, k, m.get(), f.get(), - r.get(), g.get(), c.get(), v.get(), - stop_status.get()); - gko::kernels::hip::idr::step_1(hip, nrhs, k, d_m.get(), d_f.get(), - d_r.get(), d_g.get(), d_c.get(), d_v.get(), - d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(c, d_c, 1e-14); - GKO_ASSERT_MTX_NEAR(v, d_v, 1e-14); -} - - -TEST_F(Idr, IdrStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::size_type k = 2; - gko::kernels::reference::idr::step_2(ref, nrhs, k, omega.get(), v.get(), - c.get(), u.get(), stop_status.get()); - gko::kernels::hip::idr::step_2(hip, nrhs, k, d_omega.get(), d_v.get(), - d_c.get(), d_u.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(u, d_u, 1e-14); -} - - -TEST_F(Idr, IdrStep3IsEquivalentToRef) -{ - initialize_data(); - - gko::size_type k = 2; - gko::kernels::reference::idr::step_3( - ref, nrhs, k, p.get(), g.get(), v.get(), u.get(), m.get(), f.get(), - alpha.get(), r.get(), x.get(), stop_status.get()); - gko::kernels::hip::idr::step_3( - hip, nrhs, k, d_p.get(), d_g.get(), d_v.get(), d_u.get(), d_m.get(), - d_f.get(), d_alpha.get(), d_r.get(), d_x.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(g, d_g, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(v, d_v, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(u, d_u, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(m, d_m, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(f, d_f, 150 * 1e-14); - GKO_ASSERT_MTX_NEAR(r, d_r, 50 * 1e-14); - GKO_ASSERT_MTX_NEAR(x, d_x, 50 * 1e-14); -} - - -TEST_F(Idr, IdrComputeOmegaIsEquivalentToRef) -{ - initialize_data(); - - double kappa = 0.7; - gko::kernels::reference::idr::compute_omega(ref, nrhs, kappa, tht.get(), - residual_norm.get(), - omega.get(), stop_status.get()); - gko::kernels::hip::idr::compute_omega(hip, nrhs, kappa, d_tht.get(), - d_residual_norm.get(), d_omega.get(), - d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(omega, d_omega, 1e-14); -} - - -TEST_F(Idr, IdrIterationOneRHSIsEquivalentToRef) -{ - initialize_data(123, 1); - auto ref_solver = ref_idr_factory->generate(mtx); - auto hip_solver = hip_idr_factory->generate(d_mtx); - - ref_solver->apply(b.get(), x.get()); - hip_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-13); -} - - -TEST_F(Idr, IdrIterationWithComplexSubspaceOneRHSIsEquivalentToRef) -{ - initialize_data(123, 1); - hip_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(hip)) - .on(hip); - ref_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(ref)) - .on(ref); - auto ref_solver = ref_idr_factory->generate(mtx); - auto hip_solver = hip_idr_factory->generate(d_mtx); - - ref_solver->apply(b.get(), x.get()); - hip_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-13); -} - - -TEST_F(Idr, IdrIterationMultipleRHSIsEquivalentToRef) -{ - initialize_data(123, 16); - auto hip_solver = hip_idr_factory->generate(d_mtx); - auto ref_solver = ref_idr_factory->generate(mtx); - - ref_solver->apply(b.get(), x.get()); - hip_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-12); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Idr, IdrIterationWithComplexSubspaceMultipleRHSIsEquivalentToRef) -{ - initialize_data(123, 16); - hip_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(hip)) - .on(hip); - ref_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(ref)) - .on(ref); - auto hip_solver = hip_idr_factory->generate(d_mtx); - auto ref_solver = ref_idr_factory->generate(mtx); - - ref_solver->apply(b.get(), x.get()); - hip_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-13); -} - - -} // namespace diff --git a/hip/test/solver/multigrid_kernels.cpp b/hip/test/solver/multigrid_kernels.cpp deleted file mode 100644 index 9a264019b5f..00000000000 --- a/hip/test/solver/multigrid_kernels.cpp +++ /dev/null @@ -1,256 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/solver/multigrid_kernels.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { - - -class Multigrid : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - Multigrid() : rand_engine(30) {} - - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void initialize_data() - { - int m = 597; - int n = 43; - v = gen_mtx(m, n); - d = gen_mtx(m, n); - g = gen_mtx(m, n); - e = gen_mtx(m, n); - alpha = gen_mtx(1, n); - rho = gen_mtx(1, n); - beta = gen_mtx(1, n); - gamma = gen_mtx(1, n); - zeta = gen_mtx(1, n); - old_norm = gen_mtx(1, n); - new_norm = Mtx::create(ref, gko::dim<2>{1, n}); - this->modify_norm(old_norm, new_norm); - this->modify_scalar(alpha, rho, beta, gamma, zeta); - - d_v = Mtx::create(hip); - d_v->copy_from(v.get()); - d_d = Mtx::create(hip); - d_d->copy_from(d.get()); - d_g = Mtx::create(hip); - d_g->copy_from(g.get()); - d_e = Mtx::create(hip); - d_e->copy_from(e.get()); - d_alpha = Mtx::create(hip); - d_alpha->copy_from(alpha.get()); - d_rho = Mtx::create(hip); - d_rho->copy_from(rho.get()); - d_beta = Mtx::create(hip); - d_beta->copy_from(beta.get()); - d_gamma = Mtx::create(hip); - d_gamma->copy_from(gamma.get()); - d_zeta = Mtx::create(hip); - d_zeta->copy_from(zeta.get()); - d_old_norm = Mtx::create(hip); - d_old_norm->copy_from(old_norm.get()); - d_new_norm = Mtx::create(hip); - d_new_norm->copy_from(new_norm.get()); - } - - void modify_norm(std::unique_ptr& old_norm, - std::unique_ptr& new_norm) - { - double ratio = 0.7; - for (gko::size_type i = 0; i < old_norm->get_size()[1]; i++) { - old_norm->at(0, i) = gko::abs(old_norm->at(0, i)); - new_norm->at(0, i) = ratio * old_norm->at(0, i); - } - } - - void modify_scalar(std::unique_ptr& alpha, std::unique_ptr& rho, - std::unique_ptr& beta, std::unique_ptr& gamma, - std::unique_ptr& zeta) - { - // modify the first three element such that the isfinite condition can - // be reached, which are checked in the last three group in reference - // test. - // scalar_d = zeta/(beta - gamma * gamma / rho) - // scalar_e = one() - gamma / alpha * scalar_d - // temp = alpha/rho - - // scalar_d, scalar_e are not finite - alpha->at(0, 0) = 3.0; - rho->at(0, 0) = 2.0; - beta->at(0, 0) = 2.0; - gamma->at(0, 0) = 2.0; - zeta->at(0, 0) = -1.0; - - // temp, scalar_d, scalar_e are not finite - alpha->at(0, 1) = 0.0; - rho->at(0, 1) = 0.0; - beta->at(0, 1) = -1.0; - gamma->at(0, 1) = 0.0; - zeta->at(0, 1) = 3.0; - - // scalar_e is not finite - alpha->at(0, 2) = 0.0; - rho->at(0, 2) = 1.0; - beta->at(0, 2) = 2.0; - gamma->at(0, 2) = 1.0; - zeta->at(0, 2) = 2.0; - } - - std::shared_ptr ref; - std::shared_ptr hip; - - std::default_random_engine rand_engine; - - std::unique_ptr v; - std::unique_ptr d; - std::unique_ptr g; - std::unique_ptr e; - std::unique_ptr alpha; - std::unique_ptr rho; - std::unique_ptr beta; - std::unique_ptr gamma; - std::unique_ptr zeta; - std::unique_ptr old_norm; - std::unique_ptr new_norm; - - std::unique_ptr d_v; - std::unique_ptr d_d; - std::unique_ptr d_g; - std::unique_ptr d_e; - std::unique_ptr d_alpha; - std::unique_ptr d_rho; - std::unique_ptr d_beta; - std::unique_ptr d_gamma; - std::unique_ptr d_zeta; - std::unique_ptr d_old_norm; - std::unique_ptr d_new_norm; -}; - - -TEST_F(Multigrid, HipMultigridKCycleStep1IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::multigrid::kcycle_step_1( - ref, gko::lend(alpha), gko::lend(rho), gko::lend(v), gko::lend(g), - gko::lend(d), gko::lend(e)); - gko::kernels::hip::multigrid::kcycle_step_1( - hip, gko::lend(d_alpha), gko::lend(d_rho), gko::lend(d_v), - gko::lend(d_g), gko::lend(d_d), gko::lend(d_e)); - - GKO_ASSERT_MTX_NEAR(d_g, g, 1e-14); - GKO_ASSERT_MTX_NEAR(d_d, d, 1e-14); - GKO_ASSERT_MTX_NEAR(d_e, e, 1e-14); -} - - -TEST_F(Multigrid, HipMultigridKCycleStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::multigrid::kcycle_step_2( - ref, gko::lend(alpha), gko::lend(rho), gko::lend(gamma), - gko::lend(beta), gko::lend(zeta), gko::lend(d), gko::lend(e)); - gko::kernels::hip::multigrid::kcycle_step_2( - hip, gko::lend(d_alpha), gko::lend(d_rho), gko::lend(d_gamma), - gko::lend(d_beta), gko::lend(d_zeta), gko::lend(d_d), gko::lend(d_e)); - - GKO_ASSERT_MTX_NEAR(d_e, e, 1e-14); -} - - -TEST_F(Multigrid, HipMultigridKCycleCheckStopIsEquivalentToRef) -{ - initialize_data(); - bool is_stop_10; - bool d_is_stop_10; - bool is_stop_5; - bool d_is_stop_5; - - gko::kernels::reference::multigrid::kcycle_check_stop( - ref, gko::lend(old_norm), gko::lend(new_norm), 1.0, is_stop_10); - gko::kernels::hip::multigrid::kcycle_check_stop( - hip, gko::lend(d_old_norm), gko::lend(d_new_norm), 1.0, d_is_stop_10); - gko::kernels::reference::multigrid::kcycle_check_stop( - ref, gko::lend(old_norm), gko::lend(new_norm), 0.5, is_stop_5); - gko::kernels::hip::multigrid::kcycle_check_stop( - hip, gko::lend(d_old_norm), gko::lend(d_new_norm), 0.5, d_is_stop_5); - - GKO_ASSERT_EQ(d_is_stop_10, is_stop_10); - GKO_ASSERT_EQ(d_is_stop_10, true); - GKO_ASSERT_EQ(d_is_stop_5, is_stop_5); - GKO_ASSERT_EQ(d_is_stop_5, false); -} - - -} // namespace diff --git a/hip/test/stop/CMakeLists.txt b/hip/test/stop/CMakeLists.txt deleted file mode 100644 index eb3fa564d29..00000000000 --- a/hip/test/stop/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -ginkgo_create_hip_test(criterion_kernels) -ginkgo_create_test(residual_norm_kernels) diff --git a/hip/test/stop/criterion_kernels.hip.cpp b/hip/test/stop/criterion_kernels.hip.cpp deleted file mode 100644 index a6add579f92..00000000000 --- a/hip/test/stop/criterion_kernels.hip.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include -#include - - -#include "hip/test/utils.hip.hpp" - - -namespace { - - -constexpr gko::size_type test_iterations = 10; - - -class Criterion : public ::testing::Test { -protected: - Criterion() - { - ref_ = gko::ReferenceExecutor::create(); - hip_ = gko::HipExecutor::create(0, ref_); - // Actually use an iteration stopping criterion because Criterion is an - // abstract class - factory_ = gko::stop::Iteration::build() - .with_max_iters(test_iterations) - .on(hip_); - } - - std::unique_ptr factory_; - std::shared_ptr ref_; - std::shared_ptr hip_; -}; - - -TEST_F(Criterion, SetsOneStopStatus) -{ - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - auto criterion = factory_->generate(nullptr, nullptr, nullptr); - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - - stop_status.set_executor(hip_); - criterion->update() - .num_iterations(test_iterations) - .check(RelativeStoppingId, true, &stop_status, &one_changed); - stop_status.set_executor(ref_); - - ASSERT_EQ(stop_status.get_data()[0].has_stopped(), true); -} - - -TEST_F(Criterion, SetsMultipleStopStatuses) -{ - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - auto criterion = factory_->generate(nullptr, nullptr, nullptr); - gko::array stop_status(ref_, 3); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.get_data()[2].reset(); - - stop_status.set_executor(hip_); - criterion->update() - .num_iterations(test_iterations) - .check(RelativeStoppingId, true, &stop_status, &one_changed); - stop_status.set_executor(ref_); - - ASSERT_EQ(stop_status.get_data()[0].has_stopped(), true); - ASSERT_EQ(stop_status.get_data()[1].has_stopped(), true); - ASSERT_EQ(stop_status.get_data()[2].has_stopped(), true); -} - - -} // namespace diff --git a/hip/test/stop/residual_norm_kernels.cpp b/hip/test/stop/residual_norm_kernels.cpp deleted file mode 100644 index 02fafd77197..00000000000 --- a/hip/test/stop/residual_norm_kernels.cpp +++ /dev/null @@ -1,779 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include "hip/test/utils.hip.hpp" - - -namespace { - - -constexpr double tol = 1.0e-14; - - -class ResidualNorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using NormVector = gko::matrix::Dense>; - - ResidualNorm() - { - ref_ = gko::ReferenceExecutor::create(); - hip_ = gko::HipExecutor::create(0, ref_); - factory_ = - gko::stop::ResidualNorm<>::build().with_reduction_factor(tol).on( - hip_); - rel_factory_ = gko::stop::ResidualNorm<>::build() - .with_reduction_factor(tol) - .with_baseline(gko::stop::mode::initial_resnorm) - .on(hip_); - abs_factory_ = gko::stop::ResidualNorm<>::build() - .with_reduction_factor(tol) - .with_baseline(gko::stop::mode::absolute) - .on(hip_); - } - - std::unique_ptr::Factory> factory_; - std::unique_ptr::Factory> rel_factory_; - std::unique_ptr::Factory> abs_factory_; - std::shared_ptr hip_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNorm, WaitsTillResidualGoalForRhsResNorm) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - auto rhs_norm = gko::initialize({0.0}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalMultipleRHSForRhsResNorm) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - auto rhs_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * rhs_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * rhs_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalForRelResNorm) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = - rel_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalMultipleRHSForRelResNorm) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = - rel_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * res_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * res_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalForAbsResNorm) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = - abs_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNorm, WaitsTillResidualGoalMultipleRHSForAbsResNorm) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = - abs_factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - -class ResidualNormWithInitialResnorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using NormVector = gko::matrix::Dense>; - - ResidualNormWithInitialResnorm() - { - ref_ = gko::ReferenceExecutor::create(); - hip_ = gko::HipExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm<>::build() - .with_baseline(gko::stop::mode::initial_resnorm) - .with_reduction_factor(tol) - .on(hip_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr hip_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNormWithInitialResnorm, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto res_norm = gko::initialize({0.0}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * res_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNormWithInitialResnorm, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto res_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - res->compute_norm2(res_norm.get()); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * res_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * res_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ResidualNormWithRhsNorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using NormVector = gko::matrix::Dense>; - - ResidualNormWithRhsNorm() - { - ref_ = gko::ReferenceExecutor::create(); - hip_ = gko::HipExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm<>::build() - .with_baseline(gko::stop::mode::rhs_norm) - .with_reduction_factor(tol) - .on(hip_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr hip_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNormWithRhsNorm, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - auto rhs_norm = gko::initialize({0.0}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9 * rhs_norm->at(0); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNormWithRhsNorm, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - auto rhs_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9 * rhs_norm->at(0, 0); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9 * rhs_norm->at(0, 1); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ImplicitResidualNorm : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using NormVector = gko::matrix::Dense>; - - ImplicitResidualNorm() - { - ref_ = gko::ReferenceExecutor::create(); - hip_ = gko::HipExecutor::create(0, ref_); - factory_ = gko::stop::ImplicitResidualNorm<>::build() - .with_reduction_factor(tol) - .on(hip_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr hip_; - std::shared_ptr ref_; -}; - - -TEST_F(ImplicitResidualNorm, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - auto rhs_norm = gko::initialize({0.0}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = std::pow(tol * 1.1 * rhs_norm->at(0), 2); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_FALSE(one_changed); - - res->at(0) = std::pow(tol * 0.9 * rhs_norm->at(0), 2); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ImplicitResidualNorm, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - auto rhs_norm = gko::initialize({{0.0, 0.0}}, this->ref_); - gko::as(rhs)->compute_norm2(rhs_norm.get()); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = std::pow(tol * 0.9 * rhs_norm->at(0, 0), 2); - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = std::pow(tol * 0.9 * rhs_norm->at(0, 1), 2); - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .implicit_sq_residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -class ResidualNormWithAbsolute : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - - ResidualNormWithAbsolute() - { - ref_ = gko::ReferenceExecutor::create(); - hip_ = gko::HipExecutor::create(0, ref_); - factory_ = gko::stop::ResidualNorm<>::build() - .with_baseline(gko::stop::mode::absolute) - .with_reduction_factor(tol) - .on(hip_); - } - - std::unique_ptr::Factory> factory_; - std::shared_ptr hip_; - std::shared_ptr ref_; -}; - - -TEST_F(ResidualNormWithAbsolute, WaitsTillResidualGoal) -{ - auto res = gko::initialize({100.0}, ref_); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = gko::initialize({10.0}, ref_); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 1); - stop_status.get_data()[0].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0) = tol * 1.1; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_FALSE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_FALSE(one_changed); - - res->at(0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - ASSERT_TRUE(one_changed); -} - - -TEST_F(ResidualNormWithAbsolute, WaitsTillResidualGoalMultipleRHS) -{ - auto res = gko::initialize({{100.0, 100.0}}, ref_); - auto d_res = gko::clone(hip_, res); - std::shared_ptr rhs = - gko::initialize({{10.0, 10.0}}, ref_); - std::shared_ptr d_rhs = gko::clone(hip_, rhs); - auto criterion = factory_->generate(nullptr, d_rhs, nullptr, d_res.get()); - bool one_changed{}; - constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(ref_, 2); - stop_status.get_data()[0].reset(); - stop_status.get_data()[1].reset(); - stop_status.set_executor(hip_); - - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - - res->at(0, 0) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_FALSE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[0].has_converged()); - stop_status.set_executor(hip_); - ASSERT_TRUE(one_changed); - - res->at(0, 1) = tol * 0.9; - d_res->copy_from(res.get()); - ASSERT_TRUE( - criterion->update() - .residual_norm(d_res.get()) - .check(RelativeStoppingId, true, &stop_status, &one_changed)); - stop_status.set_executor(ref_); - ASSERT_TRUE(stop_status.get_data()[1].has_converged()); - ASSERT_TRUE(one_changed); -} - - -} // namespace diff --git a/omp/test/CMakeLists.txt b/omp/test/CMakeLists.txt index cf7723a11f1..224d4b10e2b 100644 --- a/omp/test/CMakeLists.txt +++ b/omp/test/CMakeLists.txt @@ -1,10 +1,5 @@ include(${PROJECT_SOURCE_DIR}/cmake/create_test.cmake) add_subdirectory(base) -add_subdirectory(components) -add_subdirectory(factorization) add_subdirectory(matrix) -add_subdirectory(preconditioner) add_subdirectory(reorder) -add_subdirectory(solver) -add_subdirectory(stop) diff --git a/omp/test/components/CMakeLists.txt b/omp/test/components/CMakeLists.txt deleted file mode 100644 index eb29e1a478e..00000000000 --- a/omp/test/components/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -ginkgo_create_test(prefix_sum_kernels) diff --git a/omp/test/factorization/CMakeLists.txt b/omp/test/factorization/CMakeLists.txt deleted file mode 100644 index d3d66296318..00000000000 --- a/omp/test/factorization/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -ginkgo_create_test(par_ic_kernels) -ginkgo_create_test(par_ict_kernels) -ginkgo_create_test(par_ilu_kernels) -ginkgo_create_test(par_ilut_kernels) diff --git a/omp/test/factorization/par_ilu_kernels.cpp b/omp/test/factorization/par_ilu_kernels.cpp deleted file mode 100644 index b95d3063100..00000000000 --- a/omp/test/factorization/par_ilu_kernels.cpp +++ /dev/null @@ -1,366 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include "core/factorization/par_ilu_kernels.hpp" - - -#include -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/factorization/factorization_kernels.hpp" -#include "core/test/utils.hpp" -#include "matrices/config.hpp" - - -namespace { - - -template -class ParIlu : public ::testing::Test { -protected: - using value_type = - typename std::tuple_element<0, decltype(ValueIndexType())>::type; - using index_type = - typename std::tuple_element<1, decltype(ValueIndexType())>::type; - using Dense = gko::matrix::Dense; - using Coo = gko::matrix::Coo; - using Csr = gko::matrix::Csr; - - std::default_random_engine rand_engine; - std::shared_ptr ref; - std::shared_ptr omp; - std::shared_ptr csr_ref; - std::shared_ptr csr_omp; - - ParIlu() - : rand_engine(17), - ref(gko::ReferenceExecutor::create()), - omp(gko::OmpExecutor::create()), - csr_ref(nullptr), - csr_omp(nullptr) - {} - - void SetUp() override - { - std::string file_name(gko::matrices::location_ani1_mtx); - auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } - auto csr_ref_temp = gko::read(input_file, ref); - auto csr_omp_temp = gko::clone(omp, csr_ref_temp); - // Make sure there are diagonal elements present - gko::kernels::reference::factorization::add_diagonal_elements( - ref, gko::lend(csr_ref_temp), false); - gko::kernels::omp::factorization::add_diagonal_elements( - omp, gko::lend(csr_omp_temp), false); - csr_ref = gko::give(csr_ref_temp); - csr_omp = gko::give(csr_omp_temp); - } - - template - std::unique_ptr gen_mtx(index_type num_rows, index_type num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(0, num_cols - 1), - std::normal_distribution>(0.0, 1.0), - rand_engine, ref); - } - - std::unique_ptr gen_unsorted_mtx(index_type num_rows, - index_type num_cols) - { - using std::swap; - auto mtx = gen_mtx(num_rows, num_cols); - auto values = mtx->get_values(); - auto col_idxs = mtx->get_col_idxs(); - const auto row_ptrs = mtx->get_const_row_ptrs(); - for (int row = 0; row < num_rows; ++row) { - const auto row_start = row_ptrs[row]; - const auto row_end = row_ptrs[row + 1]; - const int num_row_elements = row_end - row_start; - auto idx_dist = std::uniform_int_distribution( - row_start, row_end - 1); - for (int i = 0; i < num_row_elements / 2; ++i) { - auto idx1 = idx_dist(rand_engine); - auto idx2 = idx_dist(rand_engine); - if (idx1 != idx2) { - swap(values[idx1], values[idx2]); - swap(col_idxs[idx1], col_idxs[idx2]); - } - } - } - return mtx; - } - - void initialize_row_ptrs(index_type* l_row_ptrs_ref, - index_type* u_row_ptrs_ref, - index_type* l_row_ptrs_omp, - index_type* u_row_ptrs_omp) - { - gko::kernels::reference::factorization::initialize_row_ptrs_l_u( - ref, gko::lend(csr_ref), l_row_ptrs_ref, u_row_ptrs_ref); - gko::kernels::omp::factorization::initialize_row_ptrs_l_u( - omp, gko::lend(csr_omp), l_row_ptrs_omp, u_row_ptrs_omp); - } - - void initialize_lu(std::unique_ptr* l_ref, std::unique_ptr* u_ref, - std::unique_ptr* l_omp, std::unique_ptr* u_omp) - { - auto num_row_ptrs = csr_ref->get_size()[0] + 1; - gko::array l_row_ptrs_ref{ref, num_row_ptrs}; - gko::array u_row_ptrs_ref{ref, num_row_ptrs}; - gko::array l_row_ptrs_omp{omp, num_row_ptrs}; - gko::array u_row_ptrs_omp{omp, num_row_ptrs}; - - initialize_row_ptrs( - l_row_ptrs_ref.get_data(), u_row_ptrs_ref.get_data(), - l_row_ptrs_omp.get_data(), u_row_ptrs_omp.get_data()); - // Since `initialize_row_ptrs` was already tested, it is expected that - // `*_ref` and `*_omp` contain identical values - auto l_nnz = l_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; - auto u_nnz = u_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; - - *l_ref = Csr::create(ref, csr_ref->get_size(), l_nnz); - *u_ref = Csr::create(ref, csr_ref->get_size(), u_nnz); - *l_omp = Csr::create(omp, csr_omp->get_size(), l_nnz); - *u_omp = Csr::create(omp, csr_omp->get_size(), u_nnz); - // Copy the already initialized `row_ptrs` to the new matrices - ref->copy(num_row_ptrs, l_row_ptrs_ref.get_data(), - (*l_ref)->get_row_ptrs()); - ref->copy(num_row_ptrs, u_row_ptrs_ref.get_data(), - (*u_ref)->get_row_ptrs()); - omp->copy(num_row_ptrs, l_row_ptrs_omp.get_data(), - (*l_omp)->get_row_ptrs()); - omp->copy(num_row_ptrs, u_row_ptrs_omp.get_data(), - (*u_omp)->get_row_ptrs()); - - gko::kernels::reference::factorization::initialize_l_u( - ref, gko::lend(csr_ref), gko::lend(*l_ref), gko::lend(*u_ref)); - gko::kernels::omp::factorization::initialize_l_u( - omp, gko::lend(csr_omp), gko::lend(*l_omp), gko::lend(*u_omp)); - } - - template - static std::unique_ptr static_unique_ptr_cast( - std::unique_ptr&& from) - { - return std::unique_ptr{static_cast(from.release())}; - } - - void compute_lu(std::unique_ptr* l_ref, std::unique_ptr* u_ref, - std::unique_ptr* l_omp, std::unique_ptr* u_omp, - gko::size_type iterations = 0) - { - auto coo_ref = Coo::create(ref); - csr_ref->convert_to(gko::lend(coo_ref)); - auto coo_omp = Coo::create(omp); - csr_omp->convert_to(gko::lend(coo_omp)); - initialize_lu(l_ref, u_ref, l_omp, u_omp); - auto u_transpose_lin_op_ref = (*u_ref)->transpose(); - auto u_transpose_csr_ref = - static_unique_ptr_cast(std::move(u_transpose_lin_op_ref)); - auto u_transpose_lin_op_omp = (*u_omp)->transpose(); - auto u_transpose_csr_omp = - static_unique_ptr_cast(std::move(u_transpose_lin_op_omp)); - - gko::kernels::reference::par_ilu_factorization::compute_l_u_factors( - ref, iterations, gko::lend(coo_ref), gko::lend(*l_ref), - gko::lend(u_transpose_csr_ref)); - gko::kernels::omp::par_ilu_factorization::compute_l_u_factors( - omp, iterations, gko::lend(coo_omp), gko::lend(*l_omp), - gko::lend(u_transpose_csr_omp)); - auto u_lin_op_ref = u_transpose_csr_ref->transpose(); - *u_ref = static_unique_ptr_cast(std::move(u_lin_op_ref)); - auto u_lin_op_omp = u_transpose_csr_omp->transpose(); - *u_omp = static_unique_ptr_cast(std::move(u_lin_op_omp)); - } -}; - -TYPED_TEST_SUITE(ParIlu, gko::test::ValueIndexTypes, PairTypenameNameGenerator); - - -TYPED_TEST(ParIlu, OmpKernelAddDiagonalElementsSortedEquivalentToRef) -{ - using index_type = typename TestFixture::index_type; - using Csr = typename TestFixture::Csr; - index_type num_rows{200}; - index_type num_cols{200}; - auto mtx_ref = this->template gen_mtx(num_rows, num_cols); - auto mtx_omp = gko::clone(this->omp, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - this->ref, gko::lend(mtx_ref), true); - gko::kernels::omp::factorization::add_diagonal_elements( - this->omp, gko::lend(mtx_omp), true); - - ASSERT_TRUE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_omp, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_omp); -} - - -TYPED_TEST(ParIlu, OmpKernelAddDiagonalElementsUnsortedEquivalentToRef) -{ - using index_type = typename TestFixture::index_type; - using Csr = typename TestFixture::Csr; - index_type num_rows{200}; - index_type num_cols{200}; - auto mtx_ref = this->gen_unsorted_mtx(num_rows, num_cols); - auto mtx_omp = gko::clone(this->omp, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - this->ref, gko::lend(mtx_ref), false); - gko::kernels::omp::factorization::add_diagonal_elements( - this->omp, gko::lend(mtx_omp), false); - - ASSERT_FALSE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_omp, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_omp); -} - - -TYPED_TEST(ParIlu, OmpKernelAddDiagonalElementsNonSquareEquivalentToRef) -{ - using index_type = typename TestFixture::index_type; - using Csr = typename TestFixture::Csr; - index_type num_rows{200}; - index_type num_cols{100}; - auto mtx_ref = this->template gen_mtx(num_rows, num_cols); - auto mtx_omp = gko::clone(this->omp, mtx_ref); - - gko::kernels::reference::factorization::add_diagonal_elements( - this->ref, gko::lend(mtx_ref), true); - gko::kernels::omp::factorization::add_diagonal_elements( - this->omp, gko::lend(mtx_omp), true); - - ASSERT_TRUE(mtx_ref->is_sorted_by_column_index()); - GKO_ASSERT_MTX_NEAR(mtx_ref, mtx_omp, 0.); - GKO_ASSERT_MTX_EQ_SPARSITY(mtx_ref, mtx_omp); -} - - -TYPED_TEST(ParIlu, OmpKernelInitializeRowPtrsLUEquivalentToRef) -{ - using index_type = typename TestFixture::index_type; - auto num_row_ptrs = this->csr_ref->get_size()[0] + 1; - gko::array l_row_ptrs_array_ref(this->ref, num_row_ptrs); - gko::array u_row_ptrs_array_ref(this->ref, num_row_ptrs); - gko::array l_row_ptrs_array_omp(this->omp, num_row_ptrs); - gko::array u_row_ptrs_array_omp(this->omp, num_row_ptrs); - auto l_row_ptrs_ref = l_row_ptrs_array_ref.get_data(); - auto u_row_ptrs_ref = u_row_ptrs_array_ref.get_data(); - auto l_row_ptrs_omp = l_row_ptrs_array_omp.get_data(); - auto u_row_ptrs_omp = u_row_ptrs_array_omp.get_data(); - - this->initialize_row_ptrs(l_row_ptrs_ref, u_row_ptrs_ref, l_row_ptrs_omp, - u_row_ptrs_omp); - - ASSERT_TRUE(std::equal(l_row_ptrs_ref, l_row_ptrs_ref + num_row_ptrs, - l_row_ptrs_omp)); - ASSERT_TRUE(std::equal(u_row_ptrs_ref, u_row_ptrs_ref + num_row_ptrs, - u_row_ptrs_omp)); -} - - -TYPED_TEST(ParIlu, KernelInitializeParILUIsEquivalentToRef) -{ - using Csr = typename TestFixture::Csr; - using value_type = typename TestFixture::value_type; - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_omp{}; - std::unique_ptr u_omp{}; - - this->initialize_lu(&l_ref, &u_ref, &l_omp, &u_omp); - - GKO_ASSERT_MTX_NEAR(l_ref, l_omp, r::value); - GKO_ASSERT_MTX_NEAR(u_ref, u_omp, r::value); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_omp); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_omp); -} - - -TYPED_TEST(ParIlu, KernelComputeParILUIsEquivalentToRef) -{ - using Csr = typename TestFixture::Csr; - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_omp{}; - std::unique_ptr u_omp{}; - - this->compute_lu(&l_ref, &u_ref, &l_omp, &u_omp); - - GKO_ASSERT_MTX_NEAR(l_ref, l_omp, 5e-2); - GKO_ASSERT_MTX_NEAR(u_ref, u_omp, 5e-2); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_omp); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_omp); -} - - -TYPED_TEST(ParIlu, KernelComputeParILUWithMoreIterationsIsEquivalentToRef) -{ - using Csr = typename TestFixture::Csr; - using value_type = typename TestFixture::value_type; - std::unique_ptr l_ref{}; - std::unique_ptr u_ref{}; - std::unique_ptr l_omp{}; - std::unique_ptr u_omp{}; - gko::size_type iterations{30}; - - this->compute_lu(&l_ref, &u_ref, &l_omp, &u_omp, iterations); - - GKO_ASSERT_MTX_NEAR(l_ref, l_omp, r::value); - GKO_ASSERT_MTX_NEAR(u_ref, u_omp, r::value); - GKO_ASSERT_MTX_EQ_SPARSITY(l_ref, l_omp); - GKO_ASSERT_MTX_EQ_SPARSITY(u_ref, u_omp); -} - - -} // namespace diff --git a/omp/test/matrix/CMakeLists.txt b/omp/test/matrix/CMakeLists.txt index 0c0442f61e0..88ab52e9c3f 100644 --- a/omp/test/matrix/CMakeLists.txt +++ b/omp/test/matrix/CMakeLists.txt @@ -1,9 +1 @@ -ginkgo_create_test(coo_kernels) -ginkgo_create_test(csr_kernels) -ginkgo_create_test(dense_kernels) -ginkgo_create_test(diagonal_kernels) -ginkgo_create_test(ell_kernels) ginkgo_create_test(fbcsr_kernels) -ginkgo_create_test(fft_kernels) -ginkgo_create_test(hybrid_kernels) -ginkgo_create_test(sellp_kernels) diff --git a/omp/test/matrix/coo_kernels.cpp b/omp/test/matrix/coo_kernels.cpp deleted file mode 100644 index 5b6765ef34c..00000000000 --- a/omp/test/matrix/coo_kernels.cpp +++ /dev/null @@ -1,365 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/matrix/coo_kernels.hpp" -#include "core/test/utils.hpp" -#include "core/test/utils/unsort_matrix.hpp" - - -namespace { - - -class Coo : public ::testing::Test { -protected: - using Mtx = gko::matrix::Coo<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; - - Coo() : mtx_size(532, 231), rand_engine(42) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data(int num_vectors = 1) - { - mtx = gen_mtx(mtx_size[0], mtx_size[1], 1); - expected = gen_mtx(mtx_size[0], num_vectors, 1); - y = gen_mtx(mtx_size[1], num_vectors, 1); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = gko::clone(omp, mtx); - dresult = gko::clone(omp, expected); - dy = gko::clone(omp, y); - dalpha = gko::clone(omp, alpha); - dbeta = gko::clone(omp, beta); - } - - struct matrix_pair { - std::unique_ptr ref; - std::unique_ptr omp; - }; - - matrix_pair gen_unsorted_mtx() - { - constexpr int min_nnz_per_row{2}; - auto local_mtx_ref = Mtx::create(ref); - auto generated = gen_mtx(mtx_size[0], mtx_size[1], min_nnz_per_row); - local_mtx_ref->copy_from(generated.get()); - gko::test::unsort_matrix(local_mtx_ref.get(), rand_engine); - - auto local_mtx_omp = gko::clone(omp, local_mtx_ref); - - return {std::move(local_mtx_ref), std::move(local_mtx_omp)}; - } - - - std::shared_ptr ref; - std::shared_ptr omp; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; -}; - - -TEST_F(Coo, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, SimpleApplyDoesntOverwritePadding) -{ - set_up_apply_data(); - auto dresult_padded = - Vec::create(omp, dresult->get_size(), dresult->get_stride() + 1); - dresult_padded->copy_from(dresult.get()); - dresult_padded->get_values()[1] = 1234.0; - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult_padded.get()); - - GKO_ASSERT_MTX_NEAR(dresult_padded, expected, 1e-14); - ASSERT_EQ(dresult_padded->get_values()[1], 1234.0); -} - - -TEST_F(Coo, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyDoesntOverwritePadding) -{ - set_up_apply_data(); - auto dresult_padded = - Vec::create(omp, dresult->get_size(), dresult->get_stride() + 1); - dresult_padded->copy_from(dresult.get()); - dresult_padded->get_values()[1] = 1234.0; - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult_padded.get()); - - GKO_ASSERT_MTX_NEAR(dresult_padded, expected, 1e-14); - ASSERT_EQ(dresult_padded->get_values()[1], 1234.0); -} - - -TEST_F(Coo, SimpleApplyAddIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyAddIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply2(alpha.get(), y.get(), expected.get()); - dmtx->apply2(dalpha.get(), dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, SimpleApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(4); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, SimpleApplyAddToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(5); - - mtx->apply2(y.get(), expected.get()); - dmtx->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, SimpleApplyAddToDenseMatrixIsEquivalentToRefUnsorted) -{ - set_up_apply_data(6); - auto pair = gen_unsorted_mtx(); - - pair.ref->apply2(y.get(), expected.get()); - pair.omp->apply2(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyAddToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(7); - - mtx->apply2(alpha.get(), y.get(), expected.get()); - dmtx->apply2(dalpha.get(), dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Coo, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Coo, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Coo, ApplyAddToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - mtx->apply2(alpha.get(), complex_b.get(), complex_x.get()); - dmtx->apply2(dalpha.get(), dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Coo, ConvertToCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(omp); - - mtx->convert_to(csr_mtx.get()); - dmtx->convert_to(dcsr_mtx.get()); - - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); -} - - -TEST_F(Coo, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Coo, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Coo, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -} // namespace diff --git a/omp/test/matrix/csr_kernels.cpp b/omp/test/matrix/csr_kernels.cpp deleted file mode 100644 index 521a923e55d..00000000000 --- a/omp/test/matrix/csr_kernels.cpp +++ /dev/null @@ -1,906 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include - - -#include "core/components/prefix_sum_kernels.hpp" -#include "core/matrix/csr_kernels.hpp" -#include "core/matrix/csr_lookup.hpp" -#include "core/test/utils.hpp" -#include "core/test/utils/unsort_matrix.hpp" -#include "core/utils/matrix_utils.hpp" - - -namespace { - - -class Csr : public ::testing::Test { -protected: - using Arr = gko::array; - using Mtx = gko::matrix::Csr<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; - using ComplexMtx = gko::matrix::Csr>; - - Csr() -#ifdef GINKGO_FAST_TESTS - : mtx_size(152, 185), -#else - : mtx_size(532, 231), -#endif - rand_engine(42) - {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row, int max_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, max_nnz_row), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gen_mtx(num_rows, num_cols, min_nnz_row, num_cols); - } - - void set_up_mat_data() - { - mtx2 = Mtx::create(ref); - mtx2->copy_from(gen_mtx(mtx_size[0], mtx_size[1], 5)); - dmtx2 = Mtx::create(omp); - dmtx2->copy_from(mtx2.get()); - } - - void set_up_apply_data(int num_vectors = 1) - { - mtx = gen_mtx(mtx_size[0], mtx_size[1], 1); - complex_mtx = gen_mtx(mtx_size[0], mtx_size[1], 1); - square_mtx = gen_mtx(mtx_size[0], mtx_size[0], 1); - expected = gen_mtx(mtx_size[0], num_vectors, 1); - y = gen_mtx(mtx_size[1], num_vectors, 1); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = gko::clone(omp, mtx); - complex_dmtx = gko::clone(omp, complex_mtx); - square_dmtx = gko::clone(omp, square_mtx); - dresult = gko::clone(omp, expected); - dy = gko::clone(omp, y); - dalpha = gko::clone(omp, alpha); - dbeta = gko::clone(omp, beta); - - std::vector tmp(mtx->get_size()[0], 0); - auto rng = std::default_random_engine{}; - std::iota(tmp.begin(), tmp.end(), 0); - std::shuffle(tmp.begin(), tmp.end(), rng); - std::vector tmp2(mtx->get_size()[1], 0); - std::iota(tmp2.begin(), tmp2.end(), 0); - std::shuffle(tmp2.begin(), tmp2.end(), rng); - rpermute_idxs = std::make_unique(ref, tmp.begin(), tmp.end()); - cpermute_idxs = std::make_unique(ref, tmp2.begin(), tmp2.end()); - } - - struct matrix_pair { - std::unique_ptr ref; - std::unique_ptr omp; - }; - - matrix_pair gen_unsorted_mtx() - { - constexpr int min_nnz_per_row{2}; - auto local_mtx_ref = - gen_mtx(mtx_size[0], mtx_size[1], min_nnz_per_row); - gko::test::unsort_matrix(gko::lend(local_mtx_ref), rand_engine); - - auto local_mtx_omp = gko::clone(omp, local_mtx_ref); - - return {std::move(local_mtx_ref), std::move(local_mtx_omp)}; - } - - std::shared_ptr ref; - std::shared_ptr omp; - - const gko::dim<2> mtx_size; - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr mtx2; - std::unique_ptr complex_mtx; - std::unique_ptr square_mtx; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dmtx2; - std::unique_ptr complex_dmtx; - std::unique_ptr square_dmtx; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; - std::unique_ptr rpermute_idxs; - std::unique_ptr cpermute_idxs; -}; - - -TEST_F(Csr, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefUnsorted) -{ - set_up_apply_data(3); - auto pair = gen_unsorted_mtx(); - - pair.ref->apply(y.get(), expected.get()); - pair.omp->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyToCsrMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - auto trans = mtx->transpose(); - auto d_trans = dmtx->transpose(); - - mtx->apply(alpha.get(), trans.get(), beta.get(), square_mtx.get()); - dmtx->apply(dalpha.get(), d_trans.get(), dbeta.get(), square_dmtx.get()); - - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); - ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); -} - - -TEST_F(Csr, SimpleApplyToCsrMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - auto trans = mtx->transpose(); - auto d_trans = dmtx->transpose(); - - mtx->apply(trans.get(), square_mtx.get()); - dmtx->apply(d_trans.get(), square_dmtx.get()); - - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); - ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); -} - - -TEST_F(Csr, SimpleApplyToSparseCsrMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - auto mtx2 = - gen_mtx(mtx->get_size()[1], square_mtx->get_size()[1], 0, 10); - auto dmtx2 = Mtx::create(omp, mtx2->get_size()); - dmtx2->copy_from(mtx2.get()); - - mtx->apply(mtx2.get(), square_mtx.get()); - dmtx->apply(dmtx2.get(), square_dmtx.get()); - - GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, 1e-14); - ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); -} - - -TEST_F(Csr, SimpleApplySparseToSparseCsrMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - auto mtx1 = gen_mtx(mtx->get_size()[0], mtx->get_size()[1], 0, 10); - auto mtx2 = - gen_mtx(mtx->get_size()[1], square_mtx->get_size()[1], 0, 10); - auto dmtx1 = Mtx::create(omp, mtx1->get_size()); - auto dmtx2 = Mtx::create(omp, mtx2->get_size()); - dmtx1->copy_from(mtx1.get()); - dmtx2->copy_from(mtx2.get()); - - mtx1->apply(mtx2.get(), square_mtx.get()); - dmtx1->apply(dmtx2.get(), square_dmtx.get()); - - GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, 1e-14); - ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); -} - - -TEST_F(Csr, SimpleApplyToEmptyCsrMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - auto mtx2 = - gen_mtx(mtx->get_size()[1], square_mtx->get_size()[1], 0, 0); - auto dmtx2 = Mtx::create(omp, mtx2->get_size()); - dmtx2->copy_from(mtx2.get()); - - mtx->apply(mtx2.get(), square_mtx.get()); - dmtx->apply(dmtx2.get(), square_dmtx.get()); - - GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, 1e-14); - ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); -} - - -TEST_F(Csr, AdvancedApplyToIdentityMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - auto a = gen_mtx(mtx_size[0], mtx_size[1], 0); - auto b = gen_mtx(mtx_size[0], mtx_size[1], 0); - auto da = gko::clone(omp, a); - auto db = gko::clone(omp, b); - auto id = gko::matrix::Identity::create(ref, mtx_size[1]); - auto did = gko::matrix::Identity::create(omp, mtx_size[1]); - - a->apply(alpha.get(), id.get(), beta.get(), b.get()); - da->apply(dalpha.get(), did.get(), dbeta.get(), db.get()); - - GKO_ASSERT_MTX_NEAR(b, db, 1e-14); - GKO_ASSERT_MTX_EQ_SPARSITY(b, db); - ASSERT_TRUE(db->is_sorted_by_column_index()); -} - - -TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(3); - auto complex_b = gen_mtx(this->mtx_size[1], 3, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(this->mtx_size[0], 3, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(3); - auto complex_b = gen_mtx(this->mtx_size[1], 3, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(this->mtx_size[0], 3, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Csr, TransposeIsEquivalentToRef) -{ - set_up_apply_data(); - - auto trans = gko::as(mtx->transpose()); - auto d_trans = gko::as(dmtx->transpose()); - - GKO_ASSERT_MTX_NEAR(d_trans, trans, 0.0); - ASSERT_TRUE(d_trans->is_sorted_by_column_index()); -} - - -TEST_F(Csr, ConjugateTransposeIsEquivalentToRef) -{ - set_up_apply_data(); - - auto trans = gko::as(complex_mtx->conj_transpose()); - auto d_trans = gko::as(complex_dmtx->conj_transpose()); - - GKO_ASSERT_MTX_NEAR(d_trans, trans, 0.0); - ASSERT_TRUE(d_trans->is_sorted_by_column_index()); -} - - -TEST_F(Csr, ConvertToCooIsEquivalentToRef) -{ - set_up_apply_data(); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(omp); - - mtx->convert_to(coo_mtx.get()); - dmtx->convert_to(dcoo_mtx.get()); - - GKO_ASSERT_MTX_NEAR(coo_mtx.get(), dcoo_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToCooIsEquivalentToRef) -{ - set_up_apply_data(); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(omp); - - mtx->move_to(coo_mtx.get()); - dmtx->move_to(dcoo_mtx.get()); - - GKO_ASSERT_MTX_NEAR(coo_mtx.get(), dcoo_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(omp); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(ddense_mtx.get(), dense_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToDenseIsEquivalentToRef) -{ - set_up_apply_data(); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(omp); - - mtx->move_to(dense_mtx.get()); - dmtx->move_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(ddense_mtx.get(), dense_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(omp); - - mtx->convert_to(sparsity_mtx.get()); - dmtx->convert_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToSparsityCsrIsEquivalentToRef) -{ - set_up_apply_data(); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(omp); - - mtx->move_to(sparsity_mtx.get()); - dmtx->move_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Csr, ConvertToHybridIsEquivalentToRef) -{ - using Hybrid_type = gko::matrix::Hybrid<>; - set_up_apply_data(); - auto hybrid_mtx = Hybrid_type::create( - ref, std::make_shared(2)); - auto dhybrid_mtx = Hybrid_type::create( - omp, std::make_shared(2)); - - mtx->convert_to(hybrid_mtx.get()); - dmtx->convert_to(dhybrid_mtx.get()); - - GKO_ASSERT_MTX_NEAR(hybrid_mtx.get(), dhybrid_mtx.get(), 0); -} - - -TEST_F(Csr, MoveToHybridIsEquivalentToRef) -{ - using Hybrid_type = gko::matrix::Hybrid<>; - set_up_apply_data(); - auto hybrid_mtx = Hybrid_type::create( - ref, std::make_shared(2)); - auto dhybrid_mtx = Hybrid_type::create( - omp, std::make_shared(2)); - - mtx->move_to(hybrid_mtx.get()); - dmtx->move_to(dhybrid_mtx.get()); - - GKO_ASSERT_MTX_NEAR(hybrid_mtx.get(), dhybrid_mtx.get(), 0); -} - - -TEST_F(Csr, IsPermutable) -{ - set_up_apply_data(); - - auto permuted = gko::as(square_mtx->permute(rpermute_idxs.get())); - auto dpermuted = gko::as(square_dmtx->permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(permuted, dpermuted); - GKO_ASSERT_MTX_NEAR(permuted, dpermuted, 0); -} - - -TEST_F(Csr, IsInversePermutable) -{ - set_up_apply_data(); - - auto permuted = - gko::as(square_mtx->inverse_permute(rpermute_idxs.get())); - auto dpermuted = - gko::as(square_dmtx->inverse_permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(permuted, dpermuted); - GKO_ASSERT_MTX_NEAR(permuted, dpermuted, 0); -} - - -TEST_F(Csr, IsRowPermutable) -{ - set_up_apply_data(); - - auto r_permute = gko::as(mtx->row_permute(rpermute_idxs.get())); - auto dr_permute = gko::as(dmtx->row_permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(r_permute, dr_permute); - GKO_ASSERT_MTX_NEAR(r_permute, dr_permute, 0); -} - - -TEST_F(Csr, IsColPermutable) -{ - set_up_apply_data(); - - auto c_permute = gko::as(mtx->column_permute(cpermute_idxs.get())); - auto dc_permute = gko::as(dmtx->column_permute(cpermute_idxs.get())); - - ASSERT_TRUE(dc_permute->is_sorted_by_column_index()); - GKO_ASSERT_MTX_EQ_SPARSITY(c_permute, dc_permute); - GKO_ASSERT_MTX_NEAR(c_permute, dc_permute, 0); -} - - -TEST_F(Csr, IsInverseRowPermutable) -{ - set_up_apply_data(); - - auto inverse_r_permute = - gko::as(mtx->inverse_row_permute(rpermute_idxs.get())); - auto d_inverse_r_permute = - gko::as(dmtx->inverse_row_permute(rpermute_idxs.get())); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse_r_permute, d_inverse_r_permute); - GKO_ASSERT_MTX_NEAR(inverse_r_permute, d_inverse_r_permute, 0); -} - - -TEST_F(Csr, IsInverseColPermutable) -{ - set_up_apply_data(); - - auto inverse_c_permute = - gko::as(mtx->inverse_column_permute(cpermute_idxs.get())); - auto d_inverse_c_permute = - gko::as(dmtx->inverse_column_permute(cpermute_idxs.get())); - - ASSERT_TRUE(d_inverse_c_permute->is_sorted_by_column_index()); - GKO_ASSERT_MTX_EQ_SPARSITY(inverse_c_permute, d_inverse_c_permute); - GKO_ASSERT_MTX_NEAR(inverse_c_permute, d_inverse_c_permute, 0); -} - - -TEST_F(Csr, RecognizeSortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - bool is_sorted_omp{}; - bool is_sorted_ref{}; - - is_sorted_ref = mtx->is_sorted_by_column_index(); - is_sorted_omp = dmtx->is_sorted_by_column_index(); - - ASSERT_EQ(is_sorted_ref, is_sorted_omp); -} - - -TEST_F(Csr, RecognizeUnsortedMatrixIsEquivalentToRef) -{ - auto uns_mtx = gen_unsorted_mtx(); - bool is_sorted_omp{}; - bool is_sorted_ref{}; - - is_sorted_ref = uns_mtx.ref->is_sorted_by_column_index(); - is_sorted_omp = uns_mtx.omp->is_sorted_by_column_index(); - - ASSERT_EQ(is_sorted_ref, is_sorted_omp); -} - - -TEST_F(Csr, SortSortedMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->sort_by_column_index(); - dmtx->sort_by_column_index(); - - // Values must be unchanged, therefore, tolerance is `0` - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 0); -} - - -TEST_F(Csr, SortUnsortedMatrixIsEquivalentToRef) -{ - auto uns_mtx = gen_unsorted_mtx(); - - uns_mtx.ref->sort_by_column_index(); - uns_mtx.omp->sort_by_column_index(); - - // Values must be unchanged, therefore, tolerance is `0` - GKO_ASSERT_MTX_NEAR(uns_mtx.ref, uns_mtx.omp, 0); -} - - -TEST_F(Csr, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Csr, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Csr, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -TEST_F(Csr, InplaceAbsoluteComplexMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - complex_mtx->compute_absolute_inplace(); - complex_dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(complex_mtx, complex_dmtx, 1e-14); -} - - -TEST_F(Csr, OutplaceAbsoluteComplexMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_mtx = complex_mtx->compute_absolute(); - auto dabs_mtx = complex_dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -TEST_F(Csr, CalculateNnzPerRowInSpanIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr<>; - set_up_mat_data(); - gko::span rspan{7, 51}; - gko::span cspan{22, 88}; - auto size = this->mtx2->get_size(); - auto row_nnz = gko::array(this->ref, rspan.length() + 1); - row_nnz.fill(gko::zero()); - auto drow_nnz = gko::array(this->omp, row_nnz); - - gko::kernels::reference::csr::calculate_nonzeros_per_row_in_span( - this->ref, this->mtx2.get(), rspan, cspan, &row_nnz); - gko::kernels::omp::csr::calculate_nonzeros_per_row_in_span( - this->omp, this->dmtx2.get(), rspan, cspan, &drow_nnz); - - GKO_ASSERT_ARRAY_EQ(row_nnz, drow_nnz); -} - - -TEST_F(Csr, ComputeSubmatrixIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr<>; - using IndexType = int; - using ValueType = double; - set_up_mat_data(); - gko::span rspan{7, 51}; - gko::span cspan{22, 88}; - auto size = this->mtx2->get_size(); - auto row_nnz = gko::array(this->ref, rspan.length() + 1); - row_nnz.fill(gko::zero()); - gko::kernels::reference::csr::calculate_nonzeros_per_row_in_span( - this->ref, this->mtx2.get(), rspan, cspan, &row_nnz); - gko::kernels::reference::components::prefix_sum( - this->ref, row_nnz.get_data(), row_nnz.get_num_elems()); - auto num_nnz = row_nnz.get_data()[rspan.length()]; - auto drow_nnz = gko::array(this->omp, row_nnz); - auto smat1 = - Mtx::create(this->ref, gko::dim<2>(rspan.length(), cspan.length()), - std::move(gko::array(this->ref, num_nnz)), - std::move(gko::array(this->ref, num_nnz)), - std::move(row_nnz)); - auto sdmat1 = - Mtx::create(this->omp, gko::dim<2>(rspan.length(), cspan.length()), - std::move(gko::array(this->omp, num_nnz)), - std::move(gko::array(this->omp, num_nnz)), - std::move(drow_nnz)); - - - gko::kernels::reference::csr::compute_submatrix(this->ref, this->mtx2.get(), - rspan, cspan, smat1.get()); - gko::kernels::omp::csr::compute_submatrix(this->omp, this->dmtx2.get(), - rspan, cspan, sdmat1.get()); - - GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); -} - - -TEST_F(Csr, CalculateNnzPerRowInindex_setIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr<>; - using IndexType = int; - using ValueType = double; - set_up_mat_data(); - gko::index_set rset{ - this->ref, {42, 7, 8, 9, 10, 22, 25, 26, 34, 35, 36, 51}}; - gko::index_set cset{this->ref, - {42, 22, 24, 26, 28, 30, 81, 82, 83, 88}}; - gko::index_set drset(this->omp, rset); - gko::index_set dcset(this->omp, cset); - auto size = this->mtx2->get_size(); - auto row_nnz = gko::array(this->ref, rset.get_num_elems() + 1); - row_nnz.fill(gko::zero()); - auto drow_nnz = gko::array(this->omp, row_nnz); - - gko::kernels::reference::csr::calculate_nonzeros_per_row_in_index_set( - this->ref, this->mtx2.get(), rset, cset, row_nnz.get_data()); - gko::kernels::omp::csr::calculate_nonzeros_per_row_in_index_set( - this->omp, this->dmtx2.get(), drset, dcset, drow_nnz.get_data()); - - GKO_ASSERT_ARRAY_EQ(row_nnz, drow_nnz); -} - - -TEST_F(Csr, ComputeSubmatrixFromindex_setIsEquivalentToRef) -{ - using Mtx = gko::matrix::Csr<>; - using IndexType = int; - using ValueType = double; - set_up_mat_data(); - gko::index_set rset{ - this->ref, {42, 7, 8, 9, 10, 22, 25, 26, 34, 35, 36, 51}}; - gko::index_set cset{this->ref, - {42, 22, 24, 26, 28, 30, 81, 82, 83, 88}}; - gko::index_set drset(this->omp, rset); - gko::index_set dcset(this->omp, cset); - auto size = this->mtx2->get_size(); - auto row_nnz = gko::array(this->ref, rset.get_num_elems() + 1); - row_nnz.fill(gko::zero()); - gko::kernels::reference::csr::calculate_nonzeros_per_row_in_index_set( - this->ref, this->mtx2.get(), rset, cset, row_nnz.get_data()); - gko::kernels::reference::components::prefix_sum( - this->ref, row_nnz.get_data(), row_nnz.get_num_elems()); - auto num_nnz = row_nnz.get_data()[rset.get_num_elems()]; - auto drow_nnz = gko::array(this->omp, row_nnz); - auto smat1 = Mtx::create( - this->ref, gko::dim<2>(rset.get_num_elems(), cset.get_num_elems()), - std::move(gko::array(this->ref, num_nnz)), - std::move(gko::array(this->ref, num_nnz)), - std::move(row_nnz)); - auto sdmat1 = Mtx::create( - this->omp, gko::dim<2>(rset.get_num_elems(), cset.get_num_elems()), - std::move(gko::array(this->omp, num_nnz)), - std::move(gko::array(this->omp, num_nnz)), - std::move(drow_nnz)); - - gko::kernels::reference::csr::compute_submatrix_from_index_set( - this->ref, this->mtx2.get(), rset, cset, smat1.get()); - gko::kernels::omp::csr::compute_submatrix_from_index_set( - this->omp, this->dmtx2.get(), drset, dcset, sdmat1.get()); - - GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); -} - - -TEST_F(Csr, CreateSubMatrixIsEquivalentToRef) -{ - set_up_mat_data(); - - gko::span rspan{36, 98}; - gko::span cspan{26, 104}; - auto smat1 = this->mtx2->create_submatrix(rspan, cspan); - auto sdmat1 = this->dmtx2->create_submatrix(rspan, cspan); - - GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); -} - - -TEST_F(Csr, CanDetectMissingDiagonalEntry) -{ - using T = double; - using Csr = Mtx; - auto ref_mtx = gen_mtx(103, 98, 10); - const auto rowptrs = ref_mtx->get_row_ptrs(); - const auto colidxs = ref_mtx->get_col_idxs(); - const int testrow = 15; - gko::utils::remove_diagonal_entry_from_row(ref_mtx.get(), testrow); - auto mtx = gko::clone(omp, ref_mtx); - bool has_diags = true; - - gko::kernels::omp::csr::check_diagonal_entries_exist(omp, mtx.get(), - has_diags); - - ASSERT_FALSE(has_diags); -} - - -TEST_F(Csr, CanDetectWhenAllDiagonalEntriesArePresent) -{ - using T = double; - using Csr = Mtx; - auto ref_mtx = gen_mtx(103, 98, 10); - gko::utils::ensure_all_diagonal_entries(ref_mtx.get()); - auto mtx = gko::clone(omp, ref_mtx); - bool has_diags = true; - - gko::kernels::omp::csr::check_diagonal_entries_exist(omp, mtx.get(), - has_diags); - - ASSERT_TRUE(has_diags); -} - - -TEST_F(Csr, AddScaledIdentityToNonSquare) -{ - set_up_apply_data(); - gko::utils::ensure_all_diagonal_entries(mtx.get()); - dmtx->copy_from(mtx.get()); - - mtx->add_scaled_identity(alpha.get(), beta.get()); - dmtx->add_scaled_identity(dalpha.get(), dbeta.get()); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); -} - - -TEST_F(Csr, CreateSubMatrixFromindex_setIsEquivalentToRef) -{ - using IndexType = int; - using ValueType = double; - set_up_mat_data(); - - gko::index_set rset{ - this->ref, {42, 7, 8, 9, 10, 22, 25, 26, 34, 35, 36, 51}}; - gko::index_set cset{this->ref, - {42, 22, 24, 26, 28, 30, 81, 82, 83, 88}}; - gko::index_set drset(this->omp, rset); - gko::index_set dcset(this->omp, cset); - auto smat1 = this->mtx2->create_submatrix(rset, cset); - auto sdmat1 = this->dmtx2->create_submatrix(drset, dcset); - - GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); -} - - -} // namespace diff --git a/omp/test/matrix/dense_kernels.cpp b/omp/test/matrix/dense_kernels.cpp deleted file mode 100644 index 27985701c6a..00000000000 --- a/omp/test/matrix/dense_kernels.cpp +++ /dev/null @@ -1,749 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#include "core/components/fill_array_kernels.hpp" -#include "core/matrix/dense_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Dense : public ::testing::Test { -protected: - using itype = int; - using vtype = double; - using Mtx = gko::matrix::Dense; - using MixedMtx = gko::matrix::Dense>; - using NormVector = gko::matrix::Dense>; - using Arr = gko::array; - using ComplexMtx = gko::matrix::Dense>; - using Diagonal = gko::matrix::Diagonal; - using MixedComplexMtx = - gko::matrix::Dense>>; - - Dense() : rand_engine(15) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_vector_data(gko::size_type num_vecs, - bool different_alpha = false) - { - x = gen_mtx(1000, num_vecs); - y = gen_mtx(1000, num_vecs); - if (different_alpha) { - alpha = gen_mtx(1, num_vecs); - } else { - alpha = gko::initialize({2.0}, ref); - } - dx = gko::clone(omp, x); - dy = gko::clone(omp, y); - dalpha = gko::clone(omp, alpha); - expected = Mtx::create(ref, gko::dim<2>{1, num_vecs}); - dresult = Mtx::create(omp, gko::dim<2>{1, num_vecs}); - } - - void set_up_apply_data() - { - x = gen_mtx(40, 25); - c_x = gen_mtx(40, 25); - y = gen_mtx(25, 35); - expected = gen_mtx(40, 35); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - square = gen_mtx(x->get_size()[0], x->get_size()[0]); - dx = gko::clone(omp, x); - dc_x = gko::clone(omp, c_x); - dy = gko::clone(omp, y); - dresult = gko::clone(omp, expected); - dalpha = gko::clone(omp, alpha); - dbeta = gko::clone(omp, beta); - dsquare = gko::clone(omp, square); - - std::vector tmp(x->get_size()[0], 0); - auto rng = std::default_random_engine{}; - std::iota(tmp.begin(), tmp.end(), 0); - std::shuffle(tmp.begin(), tmp.end(), rng); - std::vector tmp2(x->get_size()[1], 0); - std::iota(tmp2.begin(), tmp2.end(), 0); - std::shuffle(tmp2.begin(), tmp2.end(), rng); - std::vector tmp3(x->get_size()[0] / 10); - std::uniform_int_distribution row_dist(0, x->get_size()[0] - 1); - for (auto& i : tmp3) { - i = row_dist(rng); - } - rpermute_idxs = - std::unique_ptr(new Arr{ref, tmp.begin(), tmp.end()}); - cpermute_idxs = - std::unique_ptr(new Arr{ref, tmp2.begin(), tmp2.end()}); - rgather_idxs = - std::unique_ptr(new Arr{ref, tmp3.begin(), tmp3.end()}); - } - - template - std::unique_ptr convert(InputType&& input) - { - auto result = ConvertedType::create(input->get_executor()); - input->convert_to(result.get()); - return result; - } - - std::shared_ptr ref; - std::shared_ptr omp; - - std::default_random_engine rand_engine; - - std::unique_ptr x; - std::unique_ptr c_x; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - std::unique_ptr expected; - std::unique_ptr square; - std::unique_ptr dresult; - std::unique_ptr dx; - std::unique_ptr dc_x; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; - std::unique_ptr dsquare; - std::unique_ptr rpermute_idxs; - std::unique_ptr cpermute_idxs; - std::unique_ptr rgather_idxs; -}; - - -TEST_F(Dense, SingleVectorOmpComputeDotIsEquivalentToRef) -{ - set_up_vector_data(1); - - x->compute_dot(y.get(), expected.get()); - dx->compute_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, MultipleVectorOmpComputeDotIsEquivalentToRef) -{ - set_up_vector_data(20); - - x->compute_dot(y.get(), expected.get()); - dx->compute_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, SingleVectorOmpComputeConjDotIsEquivalentToRef) -{ - set_up_vector_data(1); - - x->compute_conj_dot(y.get(), expected.get()); - dx->compute_conj_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, MultipleVectorOmpComputeConjDotIsEquivalentToRef) -{ - set_up_vector_data(20); - - x->compute_conj_dot(y.get(), expected.get()); - dx->compute_conj_dot(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, SingleVectorComputesNorm2IsEquivalentToRef) -{ - set_up_vector_data(1); - auto norm_size = gko::dim<2>{1, x->get_size()[1]}; - auto norm_expected = NormVector::create(this->ref, norm_size); - auto dnorm = NormVector::create(this->omp, norm_size); - - x->compute_norm2(norm_expected.get()); - dx->compute_norm2(dnorm.get()); - - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, 1e-14); -} - - -TEST_F(Dense, MultipleVectorComputesNorm2IsEquivalentToRef) -{ - set_up_vector_data(20); - auto norm_size = gko::dim<2>{1, x->get_size()[1]}; - auto norm_expected = NormVector::create(this->ref, norm_size); - auto dnorm = NormVector::create(this->omp, norm_size); - - x->compute_norm2(norm_expected.get()); - dx->compute_norm2(dnorm.get()); - - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, 1e-14); -} - - -TEST_F(Dense, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(y.get(), expected.get()); - dx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, SimpleApplyMixedIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(convert(y).get(), convert(expected).get()); - dx->apply(convert(dy).get(), convert(dresult).get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7); -} - - -TEST_F(Dense, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Dense, AdvancedApplyMixedIsEquivalentToRef) -{ - set_up_apply_data(); - - x->apply(convert(alpha).get(), convert(y).get(), - convert(beta).get(), convert(expected).get()); - dx->apply(convert(dalpha).get(), convert(dy).get(), - convert(dbeta).get(), convert(dresult).get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7); -} - - -TEST_F(Dense, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(40, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - x->apply(complex_b.get(), complex_x.get()); - dx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Dense, ApplyToMixedComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(40, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - x->apply(complex_b.get(), complex_x.get()); - dx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7); -} - - -TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(40, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - x->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Dense, AdvancedApplyToMixedComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(25, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(40, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - x->apply(convert(alpha).get(), complex_b.get(), - convert(beta).get(), complex_x.get()); - dx->apply(convert(dalpha).get(), dcomplex_b.get(), - convert(dbeta).get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7); -} - - -TEST_F(Dense, ComputeDotComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(1234, 2); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(1234, 2); - auto dcomplex_x = gko::clone(omp, complex_x); - auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); - auto dresult = ComplexMtx::create(omp, gko::dim<2>{1, 2}); - - complex_b->compute_dot(complex_x.get(), result.get()); - dcomplex_b->compute_dot(dcomplex_x.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(result, dresult, 1e-14); -} - - -TEST_F(Dense, ComputeConjDotComplexIsEquivalentToRef) -{ - set_up_apply_data(); - auto complex_b = gen_mtx(1234, 2); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(1234, 2); - auto dcomplex_x = gko::clone(omp, complex_x); - auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); - auto dresult = ComplexMtx::create(omp, gko::dim<2>{1, 2}); - - complex_b->compute_conj_dot(complex_x.get(), result.get()); - dcomplex_b->compute_conj_dot(dcomplex_x.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(result, dresult, 1e-14); -} - - -TEST_F(Dense, ConvertToCooIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Coo<>::create(ref); - auto somtx = gko::matrix::Coo<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->convert_to(srmtx.get()); - omtx->convert_to(somtx.get()); - srmtx->convert_to(drmtx.get()); - somtx->convert_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, MoveToCooIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Coo<>::create(ref); - auto somtx = gko::matrix::Coo<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->move_to(srmtx.get()); - omtx->move_to(somtx.get()); - srmtx->move_to(drmtx.get()); - somtx->move_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, ConvertToCsrIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Csr<>::create(ref); - auto somtx = gko::matrix::Csr<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->convert_to(srmtx.get()); - omtx->convert_to(somtx.get()); - srmtx->convert_to(drmtx.get()); - somtx->convert_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, MoveToCsrIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Csr<>::create(ref); - auto somtx = gko::matrix::Csr<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->move_to(srmtx.get()); - omtx->move_to(somtx.get()); - srmtx->move_to(drmtx.get()); - somtx->move_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, ConvertToSparsityCsrIsEquivalentToRef) -{ - auto mtx = gen_mtx(532, 231); - auto dmtx = gko::clone(omp, mtx); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(omp); - - mtx->convert_to(sparsity_mtx.get()); - dmtx->convert_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Dense, MoveToSparsityCsrIsEquivalentToRef) -{ - auto mtx = gen_mtx(532, 231); - auto dmtx = gko::clone(omp, mtx); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(omp); - - mtx->move_to(sparsity_mtx.get()); - dmtx->move_to(d_sparsity_mtx.get()); - - GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); -} - - -TEST_F(Dense, ConvertToEllIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Ell<>::create(ref); - auto somtx = gko::matrix::Ell<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->convert_to(srmtx.get()); - omtx->convert_to(somtx.get()); - srmtx->convert_to(drmtx.get()); - somtx->convert_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, MoveToEllIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Ell<>::create(ref); - auto somtx = gko::matrix::Ell<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->move_to(srmtx.get()); - omtx->move_to(somtx.get()); - srmtx->move_to(drmtx.get()); - somtx->move_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, ConvertToHybridIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Hybrid<>::create(ref); - auto somtx = gko::matrix::Hybrid<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->convert_to(srmtx.get()); - omtx->convert_to(somtx.get()); - srmtx->convert_to(drmtx.get()); - somtx->convert_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, MoveToHybridIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Hybrid<>::create(ref); - auto somtx = gko::matrix::Hybrid<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->move_to(srmtx.get()); - omtx->move_to(somtx.get()); - srmtx->move_to(drmtx.get()); - somtx->move_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, ConvertToSellpIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Sellp<>::create(ref); - auto somtx = gko::matrix::Sellp<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->convert_to(srmtx.get()); - omtx->convert_to(somtx.get()); - srmtx->convert_to(drmtx.get()); - somtx->convert_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, MoveToSellpIsEquivalentToRef) -{ - auto rmtx = gen_mtx(532, 231); - auto omtx = gko::clone(omp, rmtx); - auto srmtx = gko::matrix::Sellp<>::create(ref); - auto somtx = gko::matrix::Sellp<>::create(omp); - auto drmtx = Mtx::create(ref); - auto domtx = Mtx::create(omp); - - rmtx->move_to(srmtx.get()); - omtx->move_to(somtx.get()); - srmtx->move_to(drmtx.get()); - somtx->move_to(domtx.get()); - - GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); - GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); - GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); -} - - -TEST_F(Dense, ConvertsEmptyToSellp) -{ - auto dempty_mtx = Mtx::create(omp); - auto dsellp_mtx = gko::matrix::Sellp<>::create(omp); - - dempty_mtx->convert_to(dsellp_mtx.get()); - - ASSERT_EQ(*dsellp_mtx->get_const_slice_sets(), 0); - ASSERT_FALSE(dsellp_mtx->get_size()); -} - - -TEST_F(Dense, CalculateNNZPerRowIsEquivalentToRef) -{ - set_up_apply_data(); - gko::array nnz_per_row(ref); - nnz_per_row.resize_and_reset(x->get_size()[0]); - gko::array dnnz_per_row(omp); - dnnz_per_row.resize_and_reset(dx->get_size()[0]); - - gko::kernels::reference::dense::count_nonzeros_per_row( - ref, x.get(), nnz_per_row.get_data()); - gko::kernels::omp::dense::count_nonzeros_per_row(omp, dx.get(), - dnnz_per_row.get_data()); - - GKO_ASSERT_ARRAY_EQ(nnz_per_row, dnnz_per_row); -} - - -TEST_F(Dense, ComputeMaxNNZPerRowIsEquivalentToRef) -{ - std::size_t ref_max_nnz_per_row = 0; - std::size_t omp_max_nnz_per_row = 0; - auto rmtx = gen_mtx(100, 100, 1); - auto omtx = gko::clone(omp, rmtx); - - gko::kernels::reference::dense::compute_max_nnz_per_row( - ref, rmtx.get(), ref_max_nnz_per_row); - gko::kernels::omp::dense::compute_max_nnz_per_row(omp, omtx.get(), - omp_max_nnz_per_row); - - ASSERT_EQ(ref_max_nnz_per_row, omp_max_nnz_per_row); -} - - -TEST_F(Dense, IsTransposable) -{ - set_up_apply_data(); - - auto trans = x->transpose(); - auto dtrans = dx->transpose(); - - GKO_ASSERT_MTX_NEAR(static_cast(dtrans.get()), - static_cast(trans.get()), 0); -} - - -TEST_F(Dense, IsTransposableIntoDenseCrossExecutor) -{ - set_up_apply_data(); - auto row_span = gko::span{0, x->get_size()[0] - 2}; - auto col_span = gko::span{0, x->get_size()[1] - 2}; - auto sub_x = x->create_submatrix(row_span, col_span); - auto sub_dx = dx->create_submatrix(row_span, col_span); - // create the target matrices on another executor to - // force temporary clone - auto trans = Mtx::create(ref, gko::transpose(sub_x->get_size())); - auto dtrans = Mtx::create(ref, gko::transpose(sub_x->get_size()), - sub_x->get_size()[0] + 4); - - sub_x->transpose(trans.get()); - sub_dx->transpose(dtrans.get()); - - GKO_ASSERT_MTX_NEAR(dtrans, trans, 0); -} - - -TEST_F(Dense, IsConjugateTransposable) -{ - set_up_apply_data(); - - auto trans = c_x->conj_transpose(); - auto dtrans = dc_x->conj_transpose(); - - GKO_ASSERT_MTX_NEAR(static_cast(dtrans.get()), - static_cast(trans.get()), 0); -} - - -TEST_F(Dense, IsConjugateTransposableIntoDenseCrossExecutor) -{ - set_up_apply_data(); - auto row_span = gko::span{0, c_x->get_size()[0] - 2}; - auto col_span = gko::span{0, c_x->get_size()[1] - 2}; - auto sub_x = c_x->create_submatrix(row_span, col_span); - auto sub_dx = dc_x->create_submatrix(row_span, col_span); - // create the target matrices on another executor to - // force temporary clone - auto trans = ComplexMtx::create(ref, gko::transpose(sub_x->get_size())); - auto dtrans = ComplexMtx::create(ref, gko::transpose(sub_x->get_size()), - sub_x->get_size()[0] + 4); - - sub_x->conj_transpose(trans.get()); - sub_dx->conj_transpose(dtrans.get()); - - GKO_ASSERT_MTX_NEAR(dtrans, trans, 0); -} - - -} // namespace diff --git a/omp/test/matrix/ell_kernels.cpp b/omp/test/matrix/ell_kernels.cpp deleted file mode 100644 index 292a543eef3..00000000000 --- a/omp/test/matrix/ell_kernels.cpp +++ /dev/null @@ -1,554 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include - - -#include "core/matrix/ell_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Ell : public ::testing::Test { -protected: - using Mtx = gko::matrix::Ell<>; - using Vec = gko::matrix::Dense<>; - using Vec2 = gko::matrix::Dense; - using ComplexVec = gko::matrix::Dense>; - - Ell() : rand_engine(42) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols, - int min_nnz_row) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_data(int max_nonzeros_per_row = 0, int stride = 0, - int num_vectors = 1) - { - mtx = Mtx::create(ref, gko::dim<2>{}, max_nonzeros_per_row, stride); - mtx->copy_from(gen_mtx(532, 231, 1)); - expected = gen_mtx(532, num_vectors, 1); - expected2 = Vec2::create(ref); - expected2->copy_from(expected.get()); - y = gen_mtx(231, num_vectors, 1); - y2 = Vec2::create(ref); - y2->copy_from(y.get()); - alpha = gko::initialize({2.0}, ref); - alpha2 = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - beta2 = gko::initialize({-1.0}, ref); - dmtx = gko::clone(omp, mtx); - dresult = gko::clone(omp, expected); - dresult2 = gko::clone(omp, expected2); - dy = gko::clone(omp, y); - dy2 = gko::clone(omp, y2); - dalpha = gko::clone(omp, alpha); - dalpha2 = gko::clone(omp, alpha2); - dbeta = gko::clone(omp, beta); - dbeta2 = gko::clone(omp, beta2); - } - - std::shared_ptr ref; - std::shared_ptr omp; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr expected; - std::unique_ptr expected2; - std::unique_ptr y; - std::unique_ptr y2; - std::unique_ptr alpha; - std::unique_ptr alpha2; - std::unique_ptr beta; - std::unique_ptr beta2; - - std::unique_ptr dmtx; - std::unique_ptr dresult; - std::unique_ptr dresult2; - std::unique_ptr dy; - std::unique_ptr dy2; - std::unique_ptr dalpha; - std::unique_ptr dalpha2; - std::unique_ptr dbeta; - std::unique_ptr dbeta2; -}; - - -TEST_F(Ell, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyIsEquivalentToRef1) -{ - set_up_apply_data(); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyIsEquivalentToRef2) -{ - set_up_apply_data(); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyIsEquivalentToRef3) -{ - set_up_apply_data(); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyIsEquivalentToRef1) -{ - set_up_apply_data(); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyIsEquivalentToRef2) -{ - set_up_apply_data(); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyIsEquivalentToRef3) -{ - set_up_apply_data(); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, SimpleApplyWithPaddingIsEquivalentToRef) -{ - set_up_apply_data(300, 600); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithPaddingIsEquivalentToRef1) -{ - set_up_apply_data(300, 600); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithPaddingIsEquivalentToRef2) -{ - set_up_apply_data(300, 600); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithPaddingIsEquivalentToRef3) -{ - set_up_apply_data(300, 600); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyWithPaddingIsEquivalentToRef) -{ - set_up_apply_data(300, 600); - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithPaddingIsEquivalentToRef1) -{ - set_up_apply_data(300, 600); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithPaddingIsEquivalentToRef2) -{ - set_up_apply_data(300, 600); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithPaddingIsEquivalentToRef3) -{ - set_up_apply_data(300, 600); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, SimpleApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(0, 0, 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyToDenseMatrixIsEquivalentToRef1) -{ - set_up_apply_data(0, 0, 4); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyToDenseMatrixIsEquivalentToRef2) -{ - set_up_apply_data(0, 0, 5); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyToDenseMatrixIsEquivalentToRef3) -{ - set_up_apply_data(0, 0, 6); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(0, 0, 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyToDenseMatrixIsEquivalentToRef1) -{ - set_up_apply_data(0, 0, 4); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyToDenseMatrixIsEquivalentToRef2) -{ - set_up_apply_data(0, 0, 5); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyToDenseMatrixIsEquivalentToRef3) -{ - set_up_apply_data(0, 0, 6); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, SimpleApplyWithPaddingToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(300, 600, 3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithPaddingToDenseMatrixIsEquivalentToRef1) -{ - set_up_apply_data(300, 600, 3); - - mtx->apply(y2.get(), expected2.get()); - dmtx->apply(dy2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithPaddingToDenseMatrixIsEquivalentToRef2) -{ - set_up_apply_data(300, 600, 3); - - mtx->apply(y2.get(), expected.get()); - dmtx->apply(dy2.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedSimpleApplyWithPaddingToDenseMatrixIsEquivalentToRef3) -{ - set_up_apply_data(300, 600, 3); - - mtx->apply(y.get(), expected2.get()); - dmtx->apply(dy.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyWithPaddingToDenseMatrixIsEquivalentToRef) -{ - set_up_apply_data(300, 600, 3); - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithPaddingToDenseMatrixIsEquivalentToRef1) -{ - set_up_apply_data(300, 600, 3); - - mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithPaddingToDenseMatrixIsEquivalentToRef2) -{ - set_up_apply_data(300, 600, 3); - - mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); - dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Ell, MixedAdvancedApplyWithPaddingToDenseMatrixIsEquivalentToRef3) -{ - set_up_apply_data(300, 600, 3); - - mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); - - GKO_ASSERT_MTX_NEAR(dresult2, expected2, 1e-14); -} - - -TEST_F(Ell, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(300, 600); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Ell, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_data(300, 600); - auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(omp, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Ell, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_data(); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Ell, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Ell, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_data(); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -} // namespace diff --git a/omp/test/matrix/sellp_kernels.cpp b/omp/test/matrix/sellp_kernels.cpp deleted file mode 100644 index d01633ef954..00000000000 --- a/omp/test/matrix/sellp_kernels.cpp +++ /dev/null @@ -1,315 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/matrix/sellp_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Sellp : public ::testing::Test { -protected: - using Mtx = gko::matrix::Sellp<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; - - Sellp() : rand_engine(42) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - - template - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - } - - void set_up_apply_matrix( - int total_cols = 1, int slice_size = gko::matrix::default_slice_size, - int stride_factor = gko::matrix::default_stride_factor) - { - mtx = gen_mtx(532, 231); - empty = Mtx::create(ref); - expected = gen_mtx(532, total_cols); - y = gen_mtx(231, total_cols); - alpha = gko::initialize({2.0}, ref); - beta = gko::initialize({-1.0}, ref); - dmtx = gko::clone(omp, mtx); - dempty = Mtx::create(omp); - dresult = gko::clone(omp, expected); - dy = gko::clone(omp, y); - dalpha = gko::clone(omp, alpha); - dbeta = gko::clone(omp, beta); - } - - std::shared_ptr ref; - std::shared_ptr omp; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr empty; - std::unique_ptr expected; - std::unique_ptr y; - std::unique_ptr alpha; - std::unique_ptr beta; - - std::unique_ptr dmtx; - std::unique_ptr dempty; - std::unique_ptr dresult; - std::unique_ptr dy; - std::unique_ptr dalpha; - std::unique_ptr dbeta; -}; - - -TEST_F(Sellp, SimpleApplyIsEquivalentToRef) -{ - set_up_apply_matrix(); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, AdvancedApplyIsEquivalentToRef) -{ - set_up_apply_matrix(); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, SimpleApplyWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(1, 32, 2); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, AdvancedApplyWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(1, 32, 2); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, SimpleApplyMultipleRHSIsEquivalentToRef) -{ - set_up_apply_matrix(3); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, AdvancedApplyMultipleRHSIsEquivalentToRef) -{ - set_up_apply_matrix(4); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, - SimpleApplyMultipleRHSWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(5, 2); - - mtx->apply(y.get(), expected.get()); - dmtx->apply(dy.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, - AdvancedApplyMultipleRHSWithSliceSizeAndStrideFActorIsEquivalentToRef) -{ - set_up_apply_matrix(6, 2); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Sellp, ApplyToComplexIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(omp, complex_x); - - mtx->apply(complex_b.get(), complex_x.get()); - dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Sellp, AdvancedApplyToComplexIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(omp, complex_b); - auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(omp, complex_x); - - mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); - dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); -} - - -TEST_F(Sellp, ConvertToDenseIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(omp); - - mtx->convert_to(dense_mtx.get()); - dmtx->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Sellp, ConvertEmptyToDenseIsEquivalentToRef) -{ - set_up_apply_matrix(64); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(omp); - - empty->convert_to(dense_mtx.get()); - dempty->convert_to(ddense_mtx.get()); - - GKO_ASSERT_MTX_NEAR(dense_mtx.get(), ddense_mtx.get(), 0); -} - - -TEST_F(Sellp, ExtractDiagonalIsEquivalentToRef) -{ - set_up_apply_matrix(64); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Sellp, ExtractDiagonalWithSliceSizeAndStrideFactorIsEquivalentToRef) -{ - set_up_apply_matrix(64, 32, 2); - - auto diag = mtx->extract_diagonal(); - auto ddiag = dmtx->extract_diagonal(); - - GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 0); -} - - -TEST_F(Sellp, InplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_matrix(64, 32, 2); - - mtx->compute_absolute_inplace(); - dmtx->compute_absolute_inplace(); - - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); -} - - -TEST_F(Sellp, OutplaceAbsoluteMatrixIsEquivalentToRef) -{ - set_up_apply_matrix(64, 32, 2); - - auto abs_mtx = mtx->compute_absolute(); - auto dabs_mtx = dmtx->compute_absolute(); - - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); -} - - -} // namespace diff --git a/omp/test/preconditioner/CMakeLists.txt b/omp/test/preconditioner/CMakeLists.txt deleted file mode 100644 index 575384a4c84..00000000000 --- a/omp/test/preconditioner/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -ginkgo_create_test(jacobi_kernels) -ginkgo_create_test(isai_kernels) diff --git a/omp/test/preconditioner/isai_kernels.cpp b/omp/test/preconditioner/isai_kernels.cpp deleted file mode 100644 index c1bbe99778c..00000000000 --- a/omp/test/preconditioner/isai_kernels.cpp +++ /dev/null @@ -1,654 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include - - -#include "core/preconditioner/isai_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -enum struct matrix_type { lower, upper, general, spd }; - - -class Isai : public ::testing::Test { -protected: - using value_type = double; - using index_type = gko::int32; - using Csr = gko::matrix::Csr; - using Dense = gko::matrix::Dense; - Isai() : rand_engine(42) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - std::unique_ptr clone_allocations(const Csr* csr_mtx) - { - if (csr_mtx->get_executor() != ref) { - return {nullptr}; - } - const auto num_elems = csr_mtx->get_num_stored_elements(); - auto sparsity = csr_mtx->clone(); - - // values are now filled with invalid data to catch potential errors - auto begin_values = sparsity->get_values(); - auto end_values = begin_values + num_elems; - std::fill(begin_values, end_values, -gko::one()); - return sparsity; - } - - void initialize_data(matrix_type type, gko::size_type n, - gko::size_type row_limit) - { - const bool for_lower_tm = type == matrix_type::lower; - auto nz_dist = std::uniform_int_distribution(1, row_limit); - auto val_dist = std::uniform_real_distribution(-1., 1.); - mtx = Csr::create(ref); - if (type == matrix_type::general) { - auto dense_mtx = gko::test::generate_random_matrix( - n, n, nz_dist, val_dist, rand_engine, ref, gko::dim<2>{n, n}); - ensure_diagonal(dense_mtx.get()); - mtx->copy_from(dense_mtx.get()); - } else if (type == matrix_type::spd) { - auto dense_mtx = gko::test::generate_random_band_matrix( - n, row_limit / 4, row_limit / 4, val_dist, rand_engine, ref, - gko::dim<2>{n, n}); - auto transp = gko::as(dense_mtx->transpose()); - auto spd_mtx = Dense::create(ref, gko::dim<2>{n, n}); - dense_mtx->apply(transp.get(), spd_mtx.get()); - mtx->copy_from(spd_mtx.get()); - } else { - mtx = gko::test::generate_random_triangular_matrix( - n, true, for_lower_tm, nz_dist, val_dist, rand_engine, ref, - gko::dim<2>{n, n}); - } - inverse = clone_allocations(mtx.get()); - - d_mtx = gko::clone(omp, mtx); - d_inverse = gko::clone(omp, inverse); - } - - void ensure_diagonal(Dense* mtx) - { - for (int i = 0; i < mtx->get_size()[0]; ++i) { - mtx->at(i, i) = gko::one(); - } - } - - - std::shared_ptr ref; - std::shared_ptr omp; - - std::default_random_engine rand_engine; - - std::unique_ptr mtx; - std::unique_ptr inverse; - - std::unique_ptr d_mtx; - std::unique_ptr d_inverse; -}; - - -TEST_F(Isai, OmpIsaiGenerateLinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 536, 31); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(omp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::omp::isai::generate_tri_inverse( - omp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, OmpIsaiGenerateUinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 615, 31); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(omp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::omp::isai::generate_tri_inverse( - omp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, OmpIsaiGenerateAinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 615, 31); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(omp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::omp::isai::generate_general_inverse( - omp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, OmpIsaiGenerateSpdinverseShortIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 15); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(omp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::omp::isai::generate_general_inverse( - omp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_EQ(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, OmpIsaiGenerateLinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 554, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(omp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::omp::isai::generate_tri_inverse( - omp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, OmpIsaiGenerateUinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 695, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(omp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::omp::isai::generate_tri_inverse( - omp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, OmpIsaiGenerateAinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 695, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(omp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::omp::isai::generate_general_inverse( - omp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - false); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, OmpIsaiGenerateSpdinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::array da1(omp, num_rows + 1); - auto da2 = da1; - - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::omp::isai::generate_general_inverse( - omp, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); - - GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, r::value); - GKO_ASSERT_ARRAY_EQ(a1, da1); - GKO_ASSERT_ARRAY_EQ(a2, da2); - ASSERT_GT(a1.get_const_data()[num_rows], 0); -} - - -TEST_F(Isai, OmpIsaiGenerateExcessLinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 518, 40); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(omp, a1); - gko::array da2(omp, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(omp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(omp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::omp::isai::generate_excess_system( - omp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, OmpIsaiGenerateExcessUinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 673, 51); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(omp, a1); - gko::array da2(omp, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(omp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(omp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::omp::isai::generate_excess_system( - omp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, OmpIsaiGenerateExcessAinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 100, 51); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(omp, a1); - gko::array da2(omp, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(omp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(omp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::omp::isai::generate_excess_system( - omp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, OmpIsaiGenerateExcessSpdinverseLongIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(omp, a1); - gko::array da2(omp, a2); - auto e_dim = a1.get_data()[num_rows]; - auto e_nnz = a2.get_data()[num_rows]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(omp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(omp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::omp::isai::generate_excess_system( - omp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, OmpIsaiGeneratePartialExcessIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(omp, a1); - gko::array da2(omp, a2); - auto e_dim = a1.get_data()[10] - a1.get_data()[5]; - auto e_nnz = a2.get_data()[10] - a2.get_data()[5]; - auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(omp, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(omp, gko::dim<2>(e_dim, 1)); - - gko::kernels::reference::isai::generate_excess_system( - ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), - excess.get(), e_rhs.get(), 5u, 10u); - gko::kernels::omp::isai::generate_excess_system( - omp, d_mtx.get(), d_inverse.get(), da1.get_const_data(), - da2.get_const_data(), dexcess.get(), de_rhs.get(), 5u, 10u); - - GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); - GKO_ASSERT_MTX_NEAR(excess, dexcess, 0); - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, OmpIsaiScaleExcessSolutionIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(omp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(omp, e_rhs); - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scale_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), 0, num_rows); - gko::kernels::omp::isai::scale_excess_solution(omp, da1.get_const_data(), - de_rhs.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); -} - - -TEST_F(Isai, OmpIsaiScalePartialExcessSolutionIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(omp, a1); - auto e_dim = a1.get_data()[10] - a1.get_data()[5]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(omp, e_rhs); - - gko::kernels::reference::isai::scale_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), 5u, 10u); - gko::kernels::omp::isai::scale_excess_solution(omp, da1.get_const_data(), - de_rhs.get(), 5u, 10u); - - GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); -} - - -TEST_F(Isai, OmpIsaiScatterExcessSolutionLIsEquivalentToRef) -{ - initialize_data(matrix_type::lower, 572, 52); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(omp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(omp, e_rhs); - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::omp::isai::scatter_excess_solution( - omp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, OmpIsaiScatterExcessSolutionUIsEquivalentToRef) -{ - initialize_data(matrix_type::upper, 702, 45); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_tri_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(omp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(omp, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::omp::isai::scatter_excess_solution( - omp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, OmpIsaiScatterExcessSolutionAIsEquivalentToRef) -{ - initialize_data(matrix_type::general, 702, 45); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(omp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(omp, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::omp::isai::scatter_excess_solution( - omp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, OmpIsaiScatterExcessSolutionSpdIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(omp, a1); - auto e_dim = a1.get_data()[num_rows]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(omp, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::omp::isai::scatter_excess_solution( - omp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -TEST_F(Isai, OmpIsaiScatterPartialExcessSolutionIsEquivalentToRef) -{ - initialize_data(matrix_type::spd, 100, 64); - const auto num_rows = mtx->get_size()[0]; - gko::array a1(ref, num_rows + 1); - auto a2 = a1; - gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(omp, a1); - auto e_dim = a1.get_data()[10] - a1.get_data()[5]; - auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(omp, e_rhs); - // overwrite -1 values with inverse - d_inverse->copy_from(lend(inverse)); - - gko::kernels::reference::isai::scatter_excess_solution( - ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 5u, 10u); - gko::kernels::omp::isai::scatter_excess_solution( - omp, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 5u, 10u); - - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); - ASSERT_GT(e_dim, 0); -} - - -} // namespace diff --git a/omp/test/preconditioner/jacobi_kernels.cpp b/omp/test/preconditioner/jacobi_kernels.cpp deleted file mode 100644 index c649e0b19f1..00000000000 --- a/omp/test/preconditioner/jacobi_kernels.cpp +++ /dev/null @@ -1,907 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include - - -#include "core/test/utils.hpp" -#include "core/test/utils/unsort_matrix.hpp" -#include "core/utils/matrix_utils.hpp" - - -namespace { - - -class Jacobi : public ::testing::Test { -protected: - using Bj = gko::preconditioner::Jacobi<>; - using Mtx = gko::matrix::Csr<>; - using Vec = gko::matrix::Dense<>; - using mtx_data = gko::matrix_data<>; - using value_type = typename Mtx::value_type; - using index_type = typename Mtx::index_type; - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - - void initialize_data( - std::initializer_list block_pointers, - std::initializer_list block_precisions, - std::initializer_list condition_numbers, - gko::uint32 max_block_size, int min_nnz, int max_nnz, int num_rhs = 1, - double accuracy = 0.1, bool skip_sorting = true) - { - std::default_random_engine engine(42); - const auto dim = *(end(block_pointers) - 1); - if (condition_numbers.size() == 0) { - mtx = gko::test::generate_random_matrix( - dim, dim, std::uniform_int_distribution<>(min_nnz, max_nnz), - std::normal_distribution<>(0.0, 1.0), engine, ref); - } else { - std::vector blocks; - for (gko::size_type i = 0; i < block_pointers.size() - 1; ++i) { - const auto size = - begin(block_pointers)[i + 1] - begin(block_pointers)[i]; - const auto cond = begin(condition_numbers)[i]; - blocks.push_back(mtx_data::cond( - size, cond, std::normal_distribution<>(-1, 1), engine)); - } - mtx = Mtx::create(ref); - mtx->read(mtx_data::diag(begin(blocks), end(blocks))); - } - gko::array block_ptrs(ref, block_pointers); - gko::array block_prec(ref, block_precisions); - if (block_prec.get_num_elems() == 0) { - bj_factory = Bj::build() - .with_max_block_size(max_block_size) - .with_block_pointers(block_ptrs) - .with_skip_sorting(skip_sorting) - .on(ref); - d_bj_factory = Bj::build() - .with_max_block_size(max_block_size) - .with_block_pointers(block_ptrs) - .with_skip_sorting(skip_sorting) - .on(omp); - } else { - bj_factory = Bj::build() - .with_max_block_size(max_block_size) - .with_block_pointers(block_ptrs) - .with_storage_optimization(block_prec) - .with_accuracy(accuracy) - .with_skip_sorting(skip_sorting) - .on(ref); - d_bj_factory = Bj::build() - .with_max_block_size(max_block_size) - .with_block_pointers(block_ptrs) - .with_storage_optimization(block_prec) - .with_accuracy(accuracy) - .with_skip_sorting(skip_sorting) - .on(omp); - } - b = gko::test::generate_random_matrix( - dim, num_rhs, std::uniform_int_distribution<>(num_rhs, num_rhs), - std::normal_distribution<>(0.0, 1.0), engine, ref); - d_b = gko::clone(omp, b); - x = gko::test::generate_random_matrix( - dim, num_rhs, std::uniform_int_distribution<>(num_rhs, num_rhs), - std::normal_distribution<>(0.0, 1.0), engine, ref); - d_x = gko::clone(omp, x); - } - - const gko::precision_reduction dp{}; - const gko::precision_reduction sp{0, 1}; - const gko::precision_reduction hp{0, 2}; - const gko::precision_reduction tp{1, 0}; - const gko::precision_reduction qp{2, 0}; - const gko::precision_reduction up{1, 1}; - const gko::precision_reduction ap{gko::precision_reduction::autodetect()}; - - std::shared_ptr ref; - std::shared_ptr omp; - std::shared_ptr mtx; - std::unique_ptr x; - std::unique_ptr b; - std::unique_ptr d_x; - std::unique_ptr d_b; - - std::unique_ptr bj_factory; - std::unique_ptr d_bj_factory; -}; - - -TEST_F(Jacobi, OmpFindNaturalBlocksEquivalentToRef) -{ - /* example matrix: - 1 1 - 1 1 - 1 1 - 1 1 - */ - auto mtx = share(Mtx::create(ref)); - mtx->read({{4, 4}, - {{0, 0, 1.0}, - {0, 1, 1.0}, - {1, 0, 1.0}, - {1, 1, 1.0}, - {2, 0, 1.0}, - {2, 2, 1.0}, - {3, 0, 1.0}, - {3, 2, 1.0}}}); - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(omp)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, OmpExecutesSupervariableAgglomerationEquivalentToRef) -{ - /* example matrix: - 1 1 - 1 1 - 1 1 - 1 1 - 1 - */ - auto mtx = share(Mtx::create(ref)); - mtx->read({{5, 5}, - {{0, 0, 1.0}, - {0, 1, 1.0}, - {1, 0, 1.0}, - {1, 1, 1.0}, - {2, 2, 1.0}, - {2, 3, 1.0}, - {3, 2, 1.0}, - {3, 3, 1.0}, - {4, 4, 1.0}}}); - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(omp)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, OmpFindNaturalBlocksInLargeMatrixEquivalentToRef) -{ - /* example matrix: - 1 1 - 1 1 - 1 1 - 1 1 - 1 1 - 1 1 - */ - using data = gko::matrix_data; - auto mtx = share(Mtx::create(ref)); - mtx->read(data::diag({550, 550}, {{1.0, 1.0, 0.0, 0.0, 0.0, 0.0}, - {1.0, 1.0, 0.0, 0.0, 0.0, 0.0}, - {1.0, 0.0, 1.0, 0.0, 0.0, 0.0}, - {1.0, 0.0, 1.0, 0.0, 0.0, 0.0}, - {1.0, 0.0, 1.0, 0.0, 0.0, 0.0}, - {1.0, 0.0, 1.0, 0.0, 0.0, 0.0}})); - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(omp)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, - OmpExecutesSupervariableAgglomerationInLargeMatrixEquivalentToRef) -{ - /* example matrix: - 1 1 - 1 1 - 1 1 - 1 1 - 1 - */ - using data = gko::matrix_data; - auto mtx = share(Mtx::create(ref)); - mtx->read(data::diag({550, 550}, {{1.0, 1.0, 0.0, 0.0, 0.0}, - {1.0, 1.0, 0.0, 0.0, 0.0}, - {0.0, 0.0, 1.0, 1.0, 0.0}, - {0.0, 0.0, 1.0, 1.0, 0.0}, - {0.0, 0.0, 0.0, 0.0, 1.0}})); - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(omp)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, - OmpExecutesSupervarAgglomerationEquivalentToRefFor150NonzerowsPerRow) -{ - /* example matrix duplicated 50 times: - 1 1 1 - 1 1 1 - 1 1 1 - 1 1 1 - 1 1 - */ - using data = gko::matrix_data; - auto mtx = share(Mtx::create(ref)); - mtx->read({{50, 50}, - {{1.0, 1.0, 0.0, 1.0, 0.0}, - {1.0, 1.0, 0.0, 1.0, 0.0}, - {1.0, 0.0, 1.0, 1.0, 0.0}, - {1.0, 0.0, 1.0, 1.0, 0.0}, - {0.0, 0.0, 1.0, 0.0, 1.0}}}); - - - auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(omp)->generate(mtx); - - ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); - // TODO: actually check if the results are the same -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithBlockSize32Sorted) -{ - initialize_data({0, 32, 64, 96, 128}, {}, {}, 32, 100, 110); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj.get()), gko::as(bj.get()), 1e-14); -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithBlockSize32Unsorted) -{ - std::default_random_engine engine(43); - initialize_data({0, 32, 64, 96, 128}, {}, {}, 32, 100, 110, 1, 0.1, false); - gko::test::unsort_matrix(mtx.get(), engine); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj.get()), gko::as(bj.get()), 1e-14); -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithDifferentBlockSize) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 32, - 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj.get()), gko::as(bj.get()), 1e-14); -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithMPW) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj.get()), gko::as(bj.get()), 1e-14); -} - - -TEST_F(Jacobi, OmpTransposedPreconditionerEquivalentToRefWithMPW) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - d_bj->copy_from(bj.get()); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj->transpose()), - gko::as(bj->transpose()), 1e-14); -} - - -TEST_F(Jacobi, OmpConjTransposedPreconditionerEquivalentToRefWithMPW) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - d_bj->copy_from(bj.get()); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj->conj_transpose()), - gko::as(bj->conj_transpose()), 1e-14); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRefWithBlockSize32) -{ - initialize_data({0, 32, 64, 96, 128}, {}, {}, 32, 100, 111); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRefWithDifferentBlockSize) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 32, - 97, 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, OmpScalarApplyEquivalentToRef) -{ - gko::size_type dim = 313; - std::default_random_engine engine(42); - auto dense_data = - gko::test::generate_random_matrix_data( - dim, dim, std::uniform_int_distribution<>(1, dim), - std::normal_distribution<>(1.0, 2.0), engine); - gko::utils::make_diag_dominant(dense_data); - auto dense_smtx = gko::share(Vec::create(ref)); - dense_smtx->read(dense_data); - auto smtx = gko::share(Mtx::create(ref)); - smtx->copy_from(dense_smtx.get()); - auto sb = gko::share(gko::test::generate_random_matrix( - dim, 3, std::uniform_int_distribution<>(1, 1), - std::normal_distribution<>(0.0, 1.0), engine, ref)); - auto sx = Vec::create(ref, sb->get_size()); - - auto d_smtx = gko::share(Mtx::create(omp)); - auto d_sb = gko::share(Vec::create(omp)); - auto d_sx = gko::share(Vec::create(omp, sb->get_size())); - d_smtx->copy_from(smtx.get()); - d_sb->copy_from(sb.get()); - - auto sj = Bj::build().with_max_block_size(1u).on(ref)->generate(smtx); - auto d_sj = Bj::build().with_max_block_size(1u).on(omp)->generate(d_smtx); - - sj->apply(sb.get(), sx.get()); - d_sj->apply(d_sb.get(), d_sx.get()); - - GKO_ASSERT_MTX_NEAR(sx.get(), d_sx.get(), 1e-12); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, OmpLinearCombinationApplyEquivalentToRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, omp); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, omp); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(alpha.get(), b.get(), beta.get(), x.get()); - d_bj->apply(d_alpha.get(), d_b.get(), d_beta.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, OmpScalarLinearCombinationApplyEquivalentToRef) -{ - gko::size_type dim = 5; - std::default_random_engine engine(42); - auto dense_data = - gko::test::generate_random_matrix_data( - dim, dim, std::uniform_int_distribution<>(1, dim), - std::normal_distribution<>(1.0, 2.0), engine); - gko::utils::make_diag_dominant(dense_data); - auto dense_smtx = gko::share(Vec::create(ref)); - dense_smtx->read(dense_data); - auto smtx = gko::share(Mtx::create(ref)); - smtx->copy_from(dense_smtx.get()); - auto sb = gko::share(gko::test::generate_random_matrix( - dim, 3, std::uniform_int_distribution<>(1, 1), - std::normal_distribution<>(0.0, 1.0), engine, ref, gko::dim<2>(dim, 3), - 4)); - auto sx = gko::share(gko::test::generate_random_matrix( - dim, 3, std::uniform_int_distribution<>(1, 1), - std::normal_distribution<>(0.0, 1.0), engine, ref, gko::dim<2>(dim, 3), - 4)); - - auto d_smtx = gko::share(gko::clone(omp, smtx)); - auto d_sb = gko::share(gko::clone(omp, sb)); - auto d_sx = gko::share(gko::clone(omp, sx)); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, omp); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, omp); - - auto sj = Bj::build().with_max_block_size(1u).on(ref)->generate(smtx); - auto d_sj = Bj::build().with_max_block_size(1u).on(omp)->generate(d_smtx); - - sj->apply(alpha.get(), sb.get(), beta.get(), sx.get()); - d_sj->apply(d_alpha.get(), d_sb.get(), d_beta.get(), d_sx.get()); - - GKO_ASSERT_MTX_NEAR(sx.get(), d_sx.get(), 1e-12); -} - - -TEST_F(Jacobi, OmpApplyToMultipleVectorsEquivalentToRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99, 5); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, OmpLinearCombinationApplyToMultipleVectorsEquivalentToRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, - 97, 99, 5); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, omp); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, omp); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(alpha.get(), b.get(), beta.get(), x.get()); - d_bj->apply(d_alpha.get(), d_b.get(), d_beta.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, ComputesTheSameConditionNumberAsRef) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = clone(ref, d_bj_factory->generate(mtx)); - - for (int i = 0; i < gko::as(bj.get())->get_num_blocks(); ++i) { - EXPECT_NEAR(bj->get_conditioning()[i], d_bj->get_conditioning()[i], - 1e-11); - } -} - - -TEST_F(Jacobi, SelectsTheSamePrecisionsAsRef) -{ - initialize_data( - {0, 2, 14, 27, 40, 51, 61, 70, 80, 92, 100}, - {ap, ap, ap, ap, ap, ap, ap, ap, ap, ap}, - {1e+0, 1e+0, 1e+2, 1e+3, 1e+4, 1e+4, 1e+6, 1e+7, 1e+8, 1e+9}, 13, 97, - 99, 1, 0.2); - - auto bj = bj_factory->generate(mtx); - auto d_bj = gko::clone(ref, d_bj_factory->generate(mtx)); - - auto bj_prec = - bj->get_parameters().storage_optimization.block_wise.get_const_data(); - auto d_bj_prec = - d_bj->get_parameters().storage_optimization.block_wise.get_const_data(); - for (int i = 0; i < gko::as(bj.get())->get_num_blocks(); ++i) { - EXPECT_EQ(bj_prec[i], d_bj_prec[i]); - } -} - - -TEST_F(Jacobi, AvoidsPrecisionsThatOverflow) -{ - auto mtx = gko::matrix::Csr<>::create(omp); - // clang-format off - mtx->read(mtx_data::diag({ - // perfectly conditioned block, small value difference, - // can use fp16 (5, 10) - {{2.0, 1.0}, - {1.0, 2.0}}, - // perfectly conditioned block (scaled orthogonal), - // with large value difference, need fp16 (7, 8) - {{1e-8, -1e-16}, - {1e-16, 1e-8}} - })); - // clang-format on - - auto bj = - Bj::build() - .with_max_block_size(13u) - .with_block_pointers(gko::array(omp, {0, 2, 4})) - .with_storage_optimization(gko::precision_reduction::autodetect()) - .with_accuracy(0.1) - .on(omp) - ->generate(give(mtx)); - - // both blocks are in the same group, both need (7, 8) - auto h_bj = clone(ref, bj); - auto prec = - h_bj->get_parameters().storage_optimization.block_wise.get_const_data(); - EXPECT_EQ(prec[0], gko::precision_reduction(1, 1)); - ASSERT_EQ(prec[1], gko::precision_reduction(1, 1)); -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithFullPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-14); -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, sp, sp, sp, sp, sp, sp, sp, sp, sp}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-7); -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithCustomReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {tp, tp, tp, tp, tp, tp, tp, tp, tp, tp, tp}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-6); -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithQuarteredPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {hp, hp, hp, hp, hp, hp, hp, hp, hp, hp, hp}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-3); -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithCustomQuarteredPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {qp, qp, qp, qp, qp, qp, qp, qp, qp, qp, qp}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-1); -} - - -TEST_F(Jacobi, OmpPreconditionerEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - GKO_ASSERT_MTX_NEAR(lend(d_bj), lend(bj), 1e-1); -} - - -TEST_F(Jacobi, OmpTransposedPreconditionerEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - d_bj->copy_from(bj.get()); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj->transpose()), - gko::as(bj->transpose()), 1e-14); -} - - -TEST_F(Jacobi, - OmpConjTransposedPreconditionerEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99); - - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - d_bj->copy_from(bj.get()); - - GKO_ASSERT_MTX_NEAR(gko::as(d_bj->conj_transpose()), - gko::as(bj->conj_transpose()), 1e-14); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRefWithFullPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRefWithReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, sp, sp, sp, sp, sp, sp, sp, sp, sp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRefWithCustomReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {tp, tp, tp, tp, tp, tp, tp, tp, tp, tp, tp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-5); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRefWithQuarteredPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {hp, hp, hp, hp, hp, hp, hp, hp, hp, hp, hp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-3); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRefWithCustomReducedAndReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {up, up, up, up, up, up, up, up, up, up, up}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-2); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRefWithCustomQuarteredPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {qp, qp, qp, qp, qp, qp, qp, qp, qp, qp, qp}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -TEST_F(Jacobi, OmpApplyEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-1); -} - - -TEST_F(Jacobi, OmpLinearCombinationApplyEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, dp, dp, sp, sp, sp, dp, dp, sp, dp, sp}, {}, 13, 97, - 99); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, omp); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, omp); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -TEST_F(Jacobi, OmpApplyToMultipleVectorsEquivalentToRefWithFullPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, - 99, 5); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Jacobi, OmpApplyToMultipleVectorsEquivalentToRefWithReducedPrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, sp, sp, sp, sp, sp, sp, sp, sp, sp}, {}, 13, 97, - 99, 5); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -TEST_F(Jacobi, OmpApplyToMultipleVectorsEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, - 99, 5); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-1); -} - - -TEST_F( - Jacobi, - OmpLinearCombinationApplyToMultipleVectorsEquivalentToRefWithAdaptivePrecision) -{ - initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, - {sp, dp, dp, sp, sp, sp, dp, dp, sp, dp, sp}, {}, 13, 97, - 99, 5); - auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, omp); - auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, omp); - auto bj = bj_factory->generate(mtx); - auto d_bj = d_bj_factory->generate(mtx); - - bj->apply(b.get(), x.get()); - d_bj->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); -} - - -} // namespace diff --git a/omp/test/solver/CMakeLists.txt b/omp/test/solver/CMakeLists.txt deleted file mode 100644 index d51f3d879b5..00000000000 --- a/omp/test/solver/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -ginkgo_create_test(gmres_kernels) -ginkgo_create_test(cb_gmres_kernels) -ginkgo_create_test(idr_kernels) -ginkgo_create_test(multigrid_kernels) diff --git a/omp/test/solver/cb_gmres_kernels.cpp b/omp/test/solver/cb_gmres_kernels.cpp deleted file mode 100644 index 2c11a2826d6..00000000000 --- a/omp/test/solver/cb_gmres_kernels.cpp +++ /dev/null @@ -1,346 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include -#include - - -#include - - -#include -#include -#include -#include -#include -#include - - -#include "core/solver/cb_gmres_accessor.hpp" -#include "core/solver/cb_gmres_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class CbGmres : public ::testing::Test { -protected: - using value_type = double; - using storage_type = float; - using index_type = int; - using size_type = gko::size_type; - using Range3dHelper = - gko::cb_gmres::Range3dHelper; - using Range3d = typename Range3dHelper::Range; - using Dense = gko::matrix::Dense; - using Mtx = Dense; - static constexpr unsigned int default_krylov_dim_mixed{100}; - - - CbGmres() : rand_engine(30) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(num_cols, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - Range3dHelper generate_krylov_helper(gko::dim<3> size) - { - auto helper = Range3dHelper{ref, size}; - auto& bases = helper.get_bases(); - const auto num_rows = size[0] * size[1]; - const auto num_cols = size[2]; - auto temp_krylov_bases = gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution(num_cols, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, - ref); - std::copy_n(temp_krylov_bases->get_const_values(), - bases.get_num_elems(), bases.get_data()); - // Only useful when the Accessor actually has a scale - auto range = helper.get_range(); - auto dist = std::normal_distribution(-1, 1); - for (size_type k = 0; k < size[0]; ++k) { - for (size_type i = 0; i < size[2]; ++i) { - gko::cb_gmres::helper_functions_accessor::write_scalar( - range, k, i, dist(rand_engine)); - } - } - return helper; - } - - void initialize_data() - { -#ifdef GINKGO_FAST_TESTS - int m = 123; -#else - int m = 597; -#endif - int n = 43; - x = gen_mtx(m, n); - y = gen_mtx(default_krylov_dim_mixed, n); - before_preconditioner = Mtx::create_with_config_of(x.get()); - b = gen_mtx(m, n); - arnoldi_norm = gen_mtx(3, n); - gko::dim<3> krylov_bases_dim(default_krylov_dim_mixed + 1, m, n); - range_helper = generate_krylov_helper(krylov_bases_dim); - - next_krylov_basis = gen_mtx(m, n); - hessenberg = - gen_mtx(default_krylov_dim_mixed + 1, default_krylov_dim_mixed * n); - hessenberg_iter = gen_mtx(default_krylov_dim_mixed + 1, n); - buffer_iter = gen_mtx(default_krylov_dim_mixed + 1, n); - residual = gen_mtx(m, n); - residual_norm = gen_mtx(1, n); - residual_norm_collection = gen_mtx(default_krylov_dim_mixed + 1, n); - givens_sin = gen_mtx(default_krylov_dim_mixed, n); - givens_cos = gen_mtx(default_krylov_dim_mixed, n); - stop_status = - std::make_unique>(ref, n); - for (size_t i = 0; i < stop_status->get_num_elems(); ++i) { - stop_status->get_data()[i].reset(); - } - reorth_status = - std::make_unique>(ref, n); - for (size_t i = 0; i < reorth_status->get_num_elems(); ++i) { - reorth_status->get_data()[i].reset(); - } - final_iter_nums = std::make_unique>(ref, n); - for (size_t i = 0; i < final_iter_nums->get_num_elems(); ++i) { - final_iter_nums->get_data()[i] = 5; - } - num_reorth = std::make_unique>(ref, n); - for (size_t i = 0; i < num_reorth->get_num_elems(); ++i) { - num_reorth->get_data()[i] = 0; - } - - d_x = gko::clone(omp, x); - d_before_preconditioner = Mtx::create_with_config_of(d_x.get()); - d_y = gko::clone(omp, y); - d_b = gko::clone(omp, b); - d_arnoldi_norm = gko::clone(omp, arnoldi_norm); - d_range_helper = Range3dHelper{omp, {}}; - d_range_helper = range_helper; - d_next_krylov_basis = gko::clone(omp, next_krylov_basis); - d_hessenberg = gko::clone(omp, hessenberg); - d_hessenberg_iter = gko::clone(omp, hessenberg_iter); - d_buffer_iter = Mtx::create(omp); - d_residual = gko::clone(omp, residual); - d_residual_norm = gko::clone(omp, residual_norm); - d_residual_norm_collection = gko::clone(omp, residual_norm_collection); - d_givens_sin = gko::clone(omp, givens_sin); - d_givens_cos = gko::clone(omp, givens_cos); - d_stop_status = std::make_unique>( - omp, *stop_status); - d_reorth_status = std::make_unique>( - omp, *reorth_status); - d_final_iter_nums = - std::make_unique>(omp, *final_iter_nums); - d_num_reorth = - std::make_unique>(omp, *num_reorth); - } - - void assert_krylov_bases_near() - { - gko::array d_to_host{ref}; - auto& krylov_bases = range_helper.get_bases(); - d_to_host = d_range_helper.get_bases(); - const auto tolerance = r::value; - using std::abs; - for (gko::size_type i = 0; i < krylov_bases.get_num_elems(); ++i) { - const auto ref_value = krylov_bases.get_const_data()[i]; - const auto dev_value = d_to_host.get_const_data()[i]; - ASSERT_LE(abs(dev_value - ref_value), tolerance); - } - } - - std::shared_ptr ref; - std::shared_ptr omp; - - std::default_random_engine rand_engine; - - std::unique_ptr before_preconditioner; - std::unique_ptr x; - std::unique_ptr y; - std::unique_ptr b; - std::unique_ptr arnoldi_norm; - Range3dHelper range_helper; - std::unique_ptr next_krylov_basis; - std::unique_ptr hessenberg; - std::unique_ptr hessenberg_iter; - std::unique_ptr buffer_iter; - std::unique_ptr residual; - std::unique_ptr residual_norm; - std::unique_ptr residual_norm_collection; - std::unique_ptr givens_sin; - std::unique_ptr givens_cos; - std::unique_ptr> stop_status; - std::unique_ptr> reorth_status; - std::unique_ptr> final_iter_nums; - std::unique_ptr> num_reorth; - - std::unique_ptr d_x; - std::unique_ptr d_before_preconditioner; - std::unique_ptr d_y; - std::unique_ptr d_b; - std::unique_ptr d_arnoldi_norm; - Range3dHelper d_range_helper; - std::unique_ptr d_next_krylov_basis; - std::unique_ptr d_hessenberg; - std::unique_ptr d_hessenberg_iter; - std::unique_ptr d_buffer_iter; - std::unique_ptr d_residual; - std::unique_ptr d_residual_norm; - std::unique_ptr d_residual_norm_collection; - std::unique_ptr d_givens_sin; - std::unique_ptr d_givens_cos; - std::unique_ptr> d_stop_status; - std::unique_ptr> d_reorth_status; - std::unique_ptr> d_final_iter_nums; - std::unique_ptr> d_num_reorth; -}; - - -TEST_F(CbGmres, OmpCbGmresInitialize1IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::cb_gmres::initialize_1( - ref, b.get(), residual.get(), givens_sin.get(), givens_cos.get(), - stop_status.get(), default_krylov_dim_mixed); - gko::kernels::omp::cb_gmres::initialize_1( - omp, d_b.get(), d_residual.get(), d_givens_sin.get(), - d_givens_cos.get(), d_stop_status.get(), default_krylov_dim_mixed); - - GKO_ASSERT_MTX_NEAR(d_residual, residual, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_stop_status, *stop_status); -} - -TEST_F(CbGmres, OmpCbGmresInitialize2IsEquivalentToRef) -{ - initialize_data(); - gko::array tmp{ref}; - gko::array dtmp{omp}; - - gko::kernels::reference::cb_gmres::initialize_2( - ref, residual.get(), residual_norm.get(), - residual_norm_collection.get(), arnoldi_norm.get(), - range_helper.get_range(), next_krylov_basis.get(), - final_iter_nums.get(), tmp, default_krylov_dim_mixed); - gko::kernels::omp::cb_gmres::initialize_2( - omp, d_residual.get(), d_residual_norm.get(), - d_residual_norm_collection.get(), d_arnoldi_norm.get(), - d_range_helper.get_range(), d_next_krylov_basis.get(), - d_final_iter_nums.get(), dtmp, default_krylov_dim_mixed); - - GKO_ASSERT_MTX_NEAR(d_arnoldi_norm, arnoldi_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - assert_krylov_bases_near(); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - -TEST_F(CbGmres, OmpCbGmresStep1IsEquivalentToRef) -{ - initialize_data(); - int iter = 5; - - gko::kernels::reference::cb_gmres::step_1( - ref, next_krylov_basis.get(), givens_sin.get(), givens_cos.get(), - residual_norm.get(), residual_norm_collection.get(), - range_helper.get_range(), hessenberg_iter.get(), buffer_iter.get(), - arnoldi_norm.get(), iter, final_iter_nums.get(), stop_status.get(), - reorth_status.get(), num_reorth.get()); - gko::kernels::omp::cb_gmres::step_1( - omp, d_next_krylov_basis.get(), d_givens_sin.get(), d_givens_cos.get(), - d_residual_norm.get(), d_residual_norm_collection.get(), - d_range_helper.get_range(), d_hessenberg_iter.get(), - d_buffer_iter.get(), d_arnoldi_norm.get(), iter, - d_final_iter_nums.get(), d_stop_status.get(), d_reorth_status.get(), - d_num_reorth.get()); - - GKO_ASSERT_MTX_NEAR(d_arnoldi_norm, arnoldi_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_next_krylov_basis, next_krylov_basis, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_hessenberg_iter, hessenberg_iter, 1e-14); - assert_krylov_bases_near(); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - -TEST_F(CbGmres, OmpCbGmresStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::cb_gmres::step_2( - ref, residual_norm_collection.get(), - range_helper.get_range().get_accessor().to_const(), hessenberg.get(), - y.get(), before_preconditioner.get(), final_iter_nums.get()); - gko::kernels::omp::cb_gmres::step_2( - omp, d_residual_norm_collection.get(), - d_range_helper.get_range().get_accessor().to_const(), - d_hessenberg.get(), d_y.get(), d_before_preconditioner.get(), - d_final_iter_nums.get()); - - GKO_ASSERT_MTX_NEAR(d_y, y, 1e-14); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-14); -} - - -} // namespace diff --git a/omp/test/solver/gmres_kernels.cpp b/omp/test/solver/gmres_kernels.cpp deleted file mode 100644 index f933b7a9e62..00000000000 --- a/omp/test/solver/gmres_kernels.cpp +++ /dev/null @@ -1,310 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include - - -#include "core/solver/gmres_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Gmres : public ::testing::Test { -protected: - using value_type = gko::default_precision; - using index_type = gko::int32; - using Mtx = gko::matrix::Dense; - using Solver = gko::solver::Gmres; - using norm_type = gko::remove_complex; - using NormVector = gko::matrix::Dense; - template - using Dense = typename gko::matrix::Dense; - - Gmres() : rand_engine(30) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - - mtx = gen_mtx(123, 123); - d_mtx = gko::clone(omp, mtx); - omp_gmres_factory = - Solver::build() - .with_criteria( - gko::stop::Iteration::build().with_max_iters(246u).on(omp), - gko::stop::ResidualNorm<>::build() - .with_reduction_factor(1e-15) - .on(omp)) - .on(omp); - - ref_gmres_factory = - Solver::build() - .with_criteria( - gko::stop::Iteration::build().with_max_iters(246u).on(ref), - gko::stop::ResidualNorm<>::build() - .with_reduction_factor(1e-15) - .on(ref)) - .on(ref); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - - template - std::unique_ptr> gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix>( - num_rows, num_cols, - std::uniform_int_distribution(num_cols, num_cols), - std::normal_distribution(-1.0, 1.0), rand_engine, ref); - } - - void initialize_data() - { -#ifdef GINKGO_FAST_TESTS - int m = 123; -#else - int m = 597; -#endif - int n = 43; - x = gen_mtx(m, n); - y = gen_mtx(gko::solver::default_krylov_dim, n); - before_preconditioner = Mtx::create_with_config_of(x.get()); - b = gen_mtx(m, n); - krylov_bases = gen_mtx(m * (gko::solver::default_krylov_dim + 1), n); - hessenberg = gen_mtx(gko::solver::default_krylov_dim + 1, - gko::solver::default_krylov_dim * n); - hessenberg_iter = gen_mtx(gko::solver::default_krylov_dim + 1, n); - residual = gen_mtx(m, n); - residual_norm = gen_mtx(1, n); - residual_norm_collection = - gen_mtx(gko::solver::default_krylov_dim + 1, n); - givens_sin = gen_mtx(gko::solver::default_krylov_dim, n); - givens_cos = gen_mtx(gko::solver::default_krylov_dim, n); - stop_status = - std::make_unique>(ref, n); - for (size_t i = 0; i < stop_status->get_num_elems(); ++i) { - stop_status->get_data()[i].reset(); - } - final_iter_nums = std::make_unique>(ref, n); - for (size_t i = 0; i < final_iter_nums->get_num_elems(); ++i) { - final_iter_nums->get_data()[i] = 5; - } - - d_x = gko::clone(omp, x); - d_before_preconditioner = Mtx::create_with_config_of(d_x.get()); - d_y = gko::clone(omp, y); - d_b = gko::clone(omp, b); - d_krylov_bases = gko::clone(omp, krylov_bases); - d_hessenberg = gko::clone(omp, hessenberg); - d_hessenberg_iter = gko::clone(omp, hessenberg_iter); - d_residual = gko::clone(omp, residual); - d_residual_norm = gko::clone(omp, residual_norm); - d_residual_norm_collection = gko::clone(omp, residual_norm_collection); - d_givens_sin = gko::clone(omp, givens_sin); - d_givens_cos = gko::clone(omp, givens_cos); - d_stop_status = std::make_unique>( - omp, *stop_status); - d_final_iter_nums = - std::make_unique>(omp, *final_iter_nums); - } - - std::shared_ptr ref; - std::shared_ptr omp; - - std::default_random_engine rand_engine; - - std::shared_ptr mtx; - std::shared_ptr d_mtx; - std::unique_ptr omp_gmres_factory; - std::unique_ptr ref_gmres_factory; - - std::unique_ptr before_preconditioner; - std::unique_ptr x; - std::unique_ptr y; - std::unique_ptr b; - std::unique_ptr krylov_bases; - std::unique_ptr hessenberg; - std::unique_ptr hessenberg_iter; - std::unique_ptr residual; - std::unique_ptr residual_norm; - std::unique_ptr residual_norm_collection; - std::unique_ptr givens_sin; - std::unique_ptr givens_cos; - std::unique_ptr> stop_status; - std::unique_ptr> final_iter_nums; - - std::unique_ptr d_x; - std::unique_ptr d_before_preconditioner; - std::unique_ptr d_y; - std::unique_ptr d_b; - std::unique_ptr d_krylov_bases; - std::unique_ptr d_hessenberg; - std::unique_ptr d_hessenberg_iter; - std::unique_ptr d_residual; - std::unique_ptr d_residual_norm; - std::unique_ptr d_residual_norm_collection; - std::unique_ptr d_givens_sin; - std::unique_ptr d_givens_cos; - std::unique_ptr> d_stop_status; - std::unique_ptr> d_final_iter_nums; -}; - - -TEST_F(Gmres, OmpGmresInitialize1IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::gmres::initialize_1( - ref, b.get(), residual.get(), givens_sin.get(), givens_cos.get(), - stop_status.get(), gko::solver::default_krylov_dim); - gko::kernels::omp::gmres::initialize_1( - omp, d_b.get(), d_residual.get(), d_givens_sin.get(), - d_givens_cos.get(), d_stop_status.get(), - gko::solver::default_krylov_dim); - - GKO_ASSERT_MTX_NEAR(d_residual, residual, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_stop_status, *stop_status); -} - - -TEST_F(Gmres, OmpGmresInitialize2IsEquivalentToRef) -{ - initialize_data(); - gko::array tmp{ref}; - gko::array dtmp{omp}; - - gko::kernels::reference::gmres::initialize_2( - ref, residual.get(), residual_norm.get(), - residual_norm_collection.get(), krylov_bases.get(), - final_iter_nums.get(), tmp, gko::solver::default_krylov_dim); - gko::kernels::omp::gmres::initialize_2( - omp, d_residual.get(), d_residual_norm.get(), - d_residual_norm_collection.get(), d_krylov_bases.get(), - d_final_iter_nums.get(), dtmp, gko::solver::default_krylov_dim); - - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_krylov_bases, krylov_bases, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(Gmres, OmpGmresStep1IsEquivalentToRef) -{ - initialize_data(); - int iter = 5; - - gko::kernels::reference::gmres::step_1( - ref, x->get_size()[0], givens_sin.get(), givens_cos.get(), - residual_norm.get(), residual_norm_collection.get(), krylov_bases.get(), - hessenberg_iter.get(), iter, final_iter_nums.get(), stop_status.get()); - gko::kernels::omp::gmres::step_1( - omp, d_x->get_size()[0], d_givens_sin.get(), d_givens_cos.get(), - d_residual_norm.get(), d_residual_norm_collection.get(), - d_krylov_bases.get(), d_hessenberg_iter.get(), iter, - d_final_iter_nums.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(d_givens_sin, givens_sin, 1e-14); - GKO_ASSERT_MTX_NEAR(d_givens_cos, givens_cos, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm, residual_norm, 1e-14); - GKO_ASSERT_MTX_NEAR(d_residual_norm_collection, residual_norm_collection, - 1e-14); - GKO_ASSERT_MTX_NEAR(d_hessenberg_iter, hessenberg_iter, 1e-14); - GKO_ASSERT_MTX_NEAR(d_krylov_bases, krylov_bases, 1e-14); - GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); -} - - -TEST_F(Gmres, OmpGmresStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::gmres::step_2(ref, residual_norm_collection.get(), - krylov_bases.get(), hessenberg.get(), - y.get(), before_preconditioner.get(), - final_iter_nums.get()); - gko::kernels::omp::gmres::step_2(omp, d_residual_norm_collection.get(), - d_krylov_bases.get(), d_hessenberg.get(), - d_y.get(), d_before_preconditioner.get(), - d_final_iter_nums.get()); - - GKO_ASSERT_MTX_NEAR(d_y, y, 1e-14); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-14); -} - - -TEST_F(Gmres, GmresApplyOneRHSIsEquivalentToRef) -{ - int m = 123; - int n = 1; - auto ref_solver = ref_gmres_factory->generate(mtx); - auto omp_solver = omp_gmres_factory->generate(d_mtx); - auto b = gen_mtx(m, n); - auto x = gen_mtx(m, n); - auto d_b = gko::clone(omp, b); - auto d_x = gko::clone(omp, x); - - ref_solver->apply(b.get(), x.get()); - omp_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 2e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 2e-13); -} - - -} // namespace diff --git a/omp/test/solver/idr_kernels.cpp b/omp/test/solver/idr_kernels.cpp deleted file mode 100644 index f5a1252db10..00000000000 --- a/omp/test/solver/idr_kernels.cpp +++ /dev/null @@ -1,363 +0,0 @@ -/************************************************************* -Copyright (c) 2017-2022, the Ginkgo authors -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*************************************************************/ - -#include - - -#include -#include - - -#include - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#include "core/solver/idr_kernels.hpp" -#include "core/test/utils.hpp" - - -namespace { - - -class Idr : public ::testing::Test { -protected: - using Mtx = gko::matrix::Dense<>; - using Solver = gko::solver::Idr<>; - using Csr = gko::matrix::Csr<>; - - Idr() : rand_engine(30) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - - omp_idr_factory = - Solver::build() - .with_deterministic(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(omp)) - .on(omp); - - ref_idr_factory = - Solver::build() - .with_deterministic(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(ref)) - .on(ref); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - - std::unique_ptr gen_mtx(int num_rows, int num_cols) - { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); - } - - void initialize_data(int size = 597, int input_nrhs = 17) - { - nrhs = input_nrhs; - int s = 4; - mtx = gen_mtx(size, size); - x = gen_mtx(size, nrhs); - b = gen_mtx(size, nrhs); - r = gen_mtx(size, nrhs); - m = gen_mtx(s, nrhs * s); - f = gen_mtx(s, nrhs); - g = gen_mtx(size, nrhs * s); - u = gen_mtx(size, nrhs * s); - c = gen_mtx(s, nrhs); - v = gen_mtx(size, nrhs); - p = gen_mtx(s, size); - alpha = gen_mtx(1, nrhs); - omega = gen_mtx(1, nrhs); - tht = gen_mtx(1, nrhs); - residual_norm = gen_mtx(1, nrhs); - stop_status = - std::make_unique>(ref, nrhs); - for (size_t i = 0; i < nrhs; ++i) { - stop_status->get_data()[i].reset(); - } - - d_mtx = gko::clone(omp, mtx); - d_x = gko::clone(omp, x); - d_b = gko::clone(omp, b); - d_r = gko::clone(omp, r); - d_m = gko::clone(omp, m); - d_f = gko::clone(omp, f); - d_g = gko::clone(omp, g); - d_u = gko::clone(omp, u); - d_c = gko::clone(omp, c); - d_v = gko::clone(omp, v); - d_p = gko::clone(omp, p); - d_alpha = gko::clone(omp, alpha); - d_omega = gko::clone(omp, omega); - d_tht = gko::clone(omp, tht); - d_residual_norm = gko::clone(omp, residual_norm); - d_stop_status = std::make_unique>( - omp, *stop_status); - } - - std::shared_ptr ref; - std::shared_ptr omp; - - std::default_random_engine rand_engine; - - std::shared_ptr mtx; - std::shared_ptr d_mtx; - std::unique_ptr omp_idr_factory; - std::unique_ptr ref_idr_factory; - - gko::size_type nrhs; - - std::unique_ptr x; - std::unique_ptr b; - std::unique_ptr r; - std::unique_ptr m; - std::unique_ptr f; - std::unique_ptr g; - std::unique_ptr u; - std::unique_ptr c; - std::unique_ptr v; - std::unique_ptr p; - std::unique_ptr alpha; - std::unique_ptr omega; - std::unique_ptr tht; - std::unique_ptr residual_norm; - std::unique_ptr> stop_status; - - std::unique_ptr d_x; - std::unique_ptr d_b; - std::unique_ptr d_r; - std::unique_ptr d_m; - std::unique_ptr d_f; - std::unique_ptr d_g; - std::unique_ptr d_u; - std::unique_ptr d_c; - std::unique_ptr d_v; - std::unique_ptr d_p; - std::unique_ptr d_alpha; - std::unique_ptr d_omega; - std::unique_ptr d_tht; - std::unique_ptr d_residual_norm; - std::unique_ptr> d_stop_status; -}; - - -TEST_F(Idr, IdrInitializeIsEquivalentToRef) -{ - initialize_data(); - - gko::kernels::reference::idr::initialize(ref, nrhs, m.get(), p.get(), true, - stop_status.get()); - gko::kernels::omp::idr::initialize(omp, nrhs, d_m.get(), d_p.get(), true, - d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(m, d_m, 1e-14); - GKO_ASSERT_MTX_NEAR(p, d_p, 1e-14); -} - - -TEST_F(Idr, IdrStep1IsEquivalentToRef) -{ - initialize_data(); - - gko::size_type k = 2; - gko::kernels::reference::idr::step_1(ref, nrhs, k, m.get(), f.get(), - r.get(), g.get(), c.get(), v.get(), - stop_status.get()); - gko::kernels::omp::idr::step_1(omp, nrhs, k, d_m.get(), d_f.get(), - d_r.get(), d_g.get(), d_c.get(), d_v.get(), - d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(c, d_c, 1e-14); - GKO_ASSERT_MTX_NEAR(v, d_v, 1e-14); -} - - -TEST_F(Idr, IdrStep2IsEquivalentToRef) -{ - initialize_data(); - - gko::size_type k = 2; - gko::kernels::reference::idr::step_2(ref, nrhs, k, omega.get(), v.get(), - c.get(), u.get(), stop_status.get()); - gko::kernels::omp::idr::step_2(omp, nrhs, k, d_omega.get(), d_v.get(), - d_c.get(), d_u.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(u, d_u, 1e-14); -} - - -TEST_F(Idr, IdrStep3IsEquivalentToRef) -{ - initialize_data(); - - gko::size_type k = 2; - gko::kernels::reference::idr::step_3( - ref, nrhs, k, p.get(), g.get(), v.get(), u.get(), m.get(), f.get(), - alpha.get(), r.get(), x.get(), stop_status.get()); - gko::kernels::omp::idr::step_3( - omp, nrhs, k, d_p.get(), d_g.get(), d_v.get(), d_u.get(), d_m.get(), - d_f.get(), d_alpha.get(), d_r.get(), d_x.get(), d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(g, d_g, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(v, d_v, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(u, d_u, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(m, d_m, 2 * 1e-14); - GKO_ASSERT_MTX_NEAR(f, d_f, 150 * 1e-14); - GKO_ASSERT_MTX_NEAR(r, d_r, 50 * 1e-14); - GKO_ASSERT_MTX_NEAR(x, d_x, 50 * 1e-14); -} - - -TEST_F(Idr, IdrComputeOmegaIsEquivalentToRef) -{ - initialize_data(); - - double kappa = 0.7; - gko::kernels::reference::idr::compute_omega(ref, nrhs, kappa, tht.get(), - residual_norm.get(), - omega.get(), stop_status.get()); - gko::kernels::omp::idr::compute_omega(omp, nrhs, kappa, d_tht.get(), - d_residual_norm.get(), d_omega.get(), - d_stop_status.get()); - - GKO_ASSERT_MTX_NEAR(omega, d_omega, 1e-14); -} - - -TEST_F(Idr, IdrIterationOneRHSIsEquivalentToRef) -{ - initialize_data(123, 1); - auto ref_solver = ref_idr_factory->generate(mtx); - auto omp_solver = omp_idr_factory->generate(d_mtx); - - ref_solver->apply(b.get(), x.get()); - omp_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-13); -} - - -TEST_F(Idr, IdrIterationWithComplexSubspaceOneRHSIsEquivalentToRef) -{ - initialize_data(123, 1); - omp_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(omp)) - .on(omp); - ref_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(ref)) - .on(ref); - auto ref_solver = ref_idr_factory->generate(mtx); - auto omp_solver = omp_idr_factory->generate(d_mtx); - - ref_solver->apply(b.get(), x.get()); - omp_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-13); -} - - -TEST_F(Idr, IdrIterationMultipleRHSIsEquivalentToRef) -{ - initialize_data(123, 16); - auto omp_solver = omp_idr_factory->generate(d_mtx); - auto ref_solver = ref_idr_factory->generate(mtx); - - ref_solver->apply(b.get(), x.get()); - omp_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-12); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); -} - - -TEST_F(Idr, IdrIterationWithComplexSubspaceMultipleRHSIsEquivalentToRef) -{ - initialize_data(123, 16); - omp_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(omp)) - .on(omp); - ref_idr_factory = - Solver::build() - .with_deterministic(true) - .with_complex_subspace(true) - .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(ref)) - .on(ref); - auto omp_solver = omp_idr_factory->generate(d_mtx); - auto ref_solver = ref_idr_factory->generate(mtx); - - ref_solver->apply(b.get(), x.get()); - omp_solver->apply(d_b.get(), d_x.get()); - - GKO_ASSERT_MTX_NEAR(d_b, b, 1e-13); - GKO_ASSERT_MTX_NEAR(d_x, x, 1e-13); -} - - -} // namespace diff --git a/omp/test/stop/CMakeLists.txt b/omp/test/stop/CMakeLists.txt deleted file mode 100644 index 0ba0781e077..00000000000 --- a/omp/test/stop/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -ginkgo_create_test(criterion_kernels) -ginkgo_create_test(residual_norm_kernels) diff --git a/reference/test/matrix/csr_kernels.cpp b/reference/test/matrix/csr_kernels.cpp index f7899cf7c89..6fdc543b132 100644 --- a/reference/test/matrix/csr_kernels.cpp +++ b/reference/test/matrix/csr_kernels.cpp @@ -600,8 +600,9 @@ TYPED_TEST(Csr, ConvertsToPrecision) tmp->convert_to(res.get()); GKO_ASSERT_MTX_NEAR(this->mtx2, res, residual); - ASSERT_EQ(typeid(*this->mtx2->get_strategy()), - typeid(*res->get_strategy())); + auto first_strategy = this->mtx2->get_strategy(); + auto second_strategy = res->get_strategy(); + ASSERT_EQ(typeid(*first_strategy), typeid(*second_strategy)); } @@ -624,8 +625,9 @@ TYPED_TEST(Csr, MovesToPrecision) tmp->move_to(res.get()); GKO_ASSERT_MTX_NEAR(this->mtx2, res, residual); - ASSERT_EQ(typeid(*this->mtx2->get_strategy()), - typeid(*res->get_strategy())); + auto first_strategy = this->mtx2->get_strategy(); + auto second_strategy = res->get_strategy(); + ASSERT_EQ(typeid(*first_strategy), typeid(*second_strategy)); } diff --git a/reference/test/stop/residual_norm_kernels.cpp b/reference/test/stop/residual_norm_kernels.cpp index 725026f3c30..a74898a477e 100644 --- a/reference/test/stop/residual_norm_kernels.cpp +++ b/reference/test/stop/residual_norm_kernels.cpp @@ -368,7 +368,7 @@ TYPED_TEST(ResidualNorm, SelfCalulatesAndWaitsTillResidualGoal) ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); ASSERT_EQ(one_changed, false); - solution->at(0) = rhs_val - r::value * T{0.9} * rhs_norm->at(0); + solution->at(0) = rhs_val - r::value * T{0.5} * rhs_norm->at(0); ASSERT_TRUE( rhs_criterion->update() .solution(solution.get()) @@ -398,7 +398,7 @@ TYPED_TEST(ResidualNorm, SelfCalulatesAndWaitsTillResidualGoal) ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); ASSERT_EQ(one_changed, false); - solution->at(0) = rhs_val - r::value * T{0.9} * initial_norm; + solution->at(0) = rhs_val - r::value * T{0.5} * initial_norm; ASSERT_TRUE( rel_criterion->update() .solution(solution.get()) @@ -426,7 +426,7 @@ TYPED_TEST(ResidualNorm, SelfCalulatesAndWaitsTillResidualGoal) ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); ASSERT_EQ(one_changed, false); - solution->at(0) = rhs_val - r::value * T{0.9}; + solution->at(0) = rhs_val - r::value * T{0.5}; ASSERT_TRUE( abs_criterion->update() .solution(solution.get()) @@ -652,7 +652,7 @@ TYPED_TEST(ResidualNormWithInitialResnorm, ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); ASSERT_EQ(one_changed, false); - x->at(0) = rhs_val - r::value * T{0.9} * initial_res; + x->at(0) = rhs_val - r::value * T{0.5} * initial_res; ASSERT_TRUE(criterion->update().solution(x.get()).check( RelativeStoppingId, true, &stop_status, &one_changed)); ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d10b51222ca..1095d9d9a6c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,4 +6,6 @@ add_subdirectory(distributed) add_subdirectory(factorization) add_subdirectory(matrix) add_subdirectory(multigrid) +add_subdirectory(preconditioner) add_subdirectory(solver) +add_subdirectory(stop) diff --git a/test/base/device_matrix_data_kernels.cpp b/test/base/device_matrix_data_kernels.cpp index 3d55ae021a6..b9b38a3c58a 100644 --- a/test/base/device_matrix_data_kernels.cpp +++ b/test/base/device_matrix_data_kernels.cpp @@ -51,11 +51,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - template -class DeviceMatrixData : public ::testing::Test { +class DeviceMatrixData : public CommonTestFixture { protected: using value_type = typename std::tuple_element<0, decltype(ValueIndexType())>::type; @@ -63,11 +60,8 @@ class DeviceMatrixData : public ::testing::Test { typename std::tuple_element<1, decltype(ValueIndexType())>::type; using nonzero_type = gko::matrix_data_entry; - DeviceMatrixData() : rand{82754} {} - - void SetUp() + DeviceMatrixData() : rand{82754} { - init_executor(gko::ReferenceExecutor::create(), exec); host_data.size = {100, 200}; std::uniform_int_distribution row_distr( 0, host_data.size[0] - 1); @@ -115,14 +109,6 @@ class DeviceMatrixData : public ::testing::Test { deduplicated_data.sum_duplicates(); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - - std::shared_ptr exec; std::default_random_engine rand; gko::matrix_data host_data; gko::matrix_data sorted_host_data; @@ -394,6 +380,3 @@ TYPED_TEST(DeviceMatrixData, DoesntSumDuplicatesIfThereAreNone) ASSERT_EQ(device_data.copy_to_host().nonzeros, this->sorted_host_data.nonzeros); } - - -} // namespace diff --git a/test/base/kernel_launch_generic.cpp b/test/base/kernel_launch_generic.cpp index db05ef560c4..046dff20a43 100644 --- a/test/base/kernel_launch_generic.cpp +++ b/test/base/kernel_launch_generic.cpp @@ -88,29 +88,24 @@ struct to_device_type_impl { } // namespace gko -#if GINKGO_DPCPP_SINGLE_MODE +#if GINKGO_COMMON_SINGLE_MODE using value_type = float; #else using value_type = double; -#endif +#endif // GINKGO_COMMON_SINGLE_MODE using Mtx = gko::matrix::Dense; -class KernelLaunch : public ::testing::Test { +class KernelLaunch : public CommonTestFixture { public: - void SetUp() + KernelLaunch() + : zero_array{ref, 16}, + iota_array{ref, 16}, + iota_transp_array{ref, 16}, + iota_dense{Mtx::create(exec, dim<2>{4, 4})}, + zero_dense{Mtx::create(exec, dim<2>{4, 4}, 6)}, + zero_dense2{Mtx::create(exec, dim<2>{4, 4}, 5)}, + vec_dense{Mtx::create(exec, dim<2>{1, 4})} { - init_executor(gko::ReferenceExecutor::create(), exec); - - zero_array.set_executor(exec->get_master()); - zero_array.resize_and_reset(16); - iota_array.set_executor(exec->get_master()); - iota_array.resize_and_reset(16); - iota_transp_array.set_executor(exec->get_master()); - iota_transp_array.resize_and_reset(16); - iota_dense = Mtx::create(exec, dim<2>{4, 4}); - zero_dense = Mtx::create(exec, dim<2>{4, 4}, 6); - zero_dense2 = Mtx::create(exec, dim<2>{4, 4}, 5); - vec_dense = Mtx::create(exec, dim<2>{1, 4}); auto ref_iota_dense = Mtx::create(exec->get_master(), dim<2>{4, 4}); for (int i = 0; i < 16; i++) { zero_array.get_data()[i] = 0; @@ -126,14 +121,6 @@ class KernelLaunch : public ::testing::Test { iota_transp_array.set_executor(exec); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - - std::shared_ptr exec; gko::array zero_array; gko::array iota_array; gko::array iota_transp_array; diff --git a/test/components/CMakeLists.txt b/test/components/CMakeLists.txt index 945c7974d6d..9b7d5c1aef7 100644 --- a/test/components/CMakeLists.txt +++ b/test/components/CMakeLists.txt @@ -2,4 +2,5 @@ ginkgo_create_common_test(absolute_array_kernels) ginkgo_create_common_test(fill_array_kernels) ginkgo_create_common_test(format_conversion_kernels) ginkgo_create_common_test(precision_conversion_kernels) +ginkgo_create_common_test(prefix_sum_kernels) ginkgo_create_common_test(reduce_array_kernels) diff --git a/test/components/absolute_array_kernels.cpp b/test/components/absolute_array_kernels.cpp index c7b985d8153..3b9b6c60e1c 100644 --- a/test/components/absolute_array_kernels.cpp +++ b/test/components/absolute_array_kernels.cpp @@ -48,43 +48,22 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class AbsoluteArray : public ::testing::Test { +class AbsoluteArray : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif using complex_type = std::complex; - AbsoluteArray() : total_size(6344) {} - - void SetUp() + AbsoluteArray() + : total_size(6344), + vals{ref, total_size}, + dvals{exec, total_size}, + complex_vals{ref, total_size}, + dcomplex_vals{exec, total_size} { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - vals = gko::array{ref, total_size}; - dvals = gko::array{exec, total_size}; - complex_vals = gko::array{ref, total_size}; - dcomplex_vals = gko::array{exec, total_size}; - std::fill_n(vals.get_data(), total_size, -1234.0); dvals = vals; std::fill_n(complex_vals.get_data(), total_size, complex_type{3, 4}); dcomplex_vals = complex_vals; } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - - std::shared_ptr ref; - std::shared_ptr exec; gko::size_type total_size; gko::array vals; gko::array dvals; @@ -141,6 +120,3 @@ TEST_F(AbsoluteArray, OutplaceComplexEqualsReference) GKO_ASSERT_ARRAY_EQ(abs_vals, dabs_vals); } - - -} // namespace diff --git a/test/components/fill_array_kernels.cpp b/test/components/fill_array_kernels.cpp index 0b4bb103830..dcc6bb25866 100644 --- a/test/components/fill_array_kernels.cpp +++ b/test/components/fill_array_kernels.cpp @@ -48,35 +48,20 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - template -class FillArray : public ::testing::Test { +class FillArray : public CommonTestFixture { protected: using value_type = T; - FillArray() : total_size(63531) {} - - void SetUp() + FillArray() + : total_size(63531), + vals{ref, total_size}, + dvals{exec, total_size}, + seqs{ref, total_size} { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - vals = gko::array{ref, total_size}; - dvals = gko::array{exec, total_size}; - seqs = gko::array{ref, total_size}; std::fill_n(vals.get_data(), total_size, T(1523)); std::iota(seqs.get_data(), seqs.get_data() + total_size, 0); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - - std::shared_ptr ref; - std::shared_ptr exec; gko::size_type total_size; gko::array vals; gko::array dvals; @@ -105,6 +90,3 @@ TYPED_TEST(FillArray, FillSeqEqualsReference) GKO_ASSERT_ARRAY_EQ(this->seqs, this->dvals); } - - -} // namespace diff --git a/test/components/format_conversion_kernels.cpp b/test/components/format_conversion_kernels.cpp index 212e0586f87..129e89fa68b 100644 --- a/test/components/format_conversion_kernels.cpp +++ b/test/components/format_conversion_kernels.cpp @@ -44,24 +44,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "core/test/utils.hpp" #include "test/utils/executor.hpp" -namespace { - template -class FormatConversion : public ::testing::Test { +class FormatConversion : public CommonTestFixture { protected: - FormatConversion() : rand(293), size(42793) {} - - void SetUp() + FormatConversion() + : rand(293), + size(42793), + sizes{ref, size}, + ptrs{ref, size + 1}, + idxs{ref} { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - sizes.set_executor(ref); - ptrs.set_executor(ref); - idxs.set_executor(ref); std::uniform_int_distribution row_dist{0, 10}; - sizes.resize_and_reset(size); - ptrs.resize_and_reset(size + 1); ptrs.get_data()[0] = 0; for (gko::size_type i = 0; i < size; i++) { sizes.get_data()[i] = row_dist(rand); @@ -80,15 +74,6 @@ class FormatConversion : public ::testing::Test { idxs.set_executor(exec); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - - std::shared_ptr ref; - std::shared_ptr exec; gko::size_type size; std::default_random_engine rand; gko::array sizes; @@ -164,6 +149,3 @@ TYPED_TEST(FormatConversion, ConvertPtrsToSizesIsEquivalentToRef) GKO_ASSERT_ARRAY_EQ(this->sizes, ref_sizes); } - - -} // namespace diff --git a/test/components/precision_conversion_kernels.cpp b/test/components/precision_conversion_kernels.cpp index 54312cdfaeb..c0547a798a0 100644 --- a/test/components/precision_conversion_kernels.cpp +++ b/test/components/precision_conversion_kernels.cpp @@ -46,28 +46,24 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "core/test/utils.hpp" #include "test/utils/executor.hpp" -namespace { - #if !(GINKGO_COMMON_SINGLE_MODE) -class PrecisionConversion : public ::testing::Test { +class PrecisionConversion : public CommonTestFixture { protected: - PrecisionConversion() : rand(293), total_size(42793) {} - - void SetUp() + PrecisionConversion() + : rand(293), + total_size(42793), + vals{ref, total_size}, + cvals{ref, total_size}, + vals2{ref, 1}, + expected_float{ref, 1}, + expected_double{ref, 1}, + dvals{exec}, + dcvals{exec}, + dvals2{exec} { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - vals = gko::array{ref, total_size}; - cvals = gko::array>{ref, total_size}; - vals2 = gko::array{ref, 1}; - expected_float = gko::array{ref, 1}; - expected_double = gko::array{ref, 1}; - dvals = gko::array{exec}; - dcvals = gko::array>{exec}; - dvals2 = gko::array{exec}; auto maxval = 1e10f; std::uniform_real_distribution dist(-maxval, maxval); for (gko::size_type i = 0; i < total_size; ++i) { @@ -85,15 +81,6 @@ class PrecisionConversion : public ::testing::Test { dvals2 = vals2; } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - - std::shared_ptr ref; - std::shared_ptr exec; std::default_random_engine rand; gko::size_type total_size; gko::array vals; @@ -181,6 +168,3 @@ TEST_F(PrecisionConversion, ConvertsComplexFromRef) #endif - - -} // namespace diff --git a/omp/test/components/prefix_sum_kernels.cpp b/test/components/prefix_sum_kernels.cpp similarity index 72% rename from omp/test/components/prefix_sum_kernels.cpp rename to test/components/prefix_sum_kernels.cpp index aa4b0a3a5c2..7756316f979 100644 --- a/omp/test/components/prefix_sum_kernels.cpp +++ b/test/components/prefix_sum_kernels.cpp @@ -45,22 +45,16 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "core/test/utils.hpp" - - -namespace { +#include "test/utils/executor.hpp" template -class PrefixSum : public ::testing::Test { +class PrefixSum : public CommonTestFixture { protected: using index_type = T; + PrefixSum() - : ref(gko::ReferenceExecutor::create()), - exec(gko::OmpExecutor::create()), - rand(293), - total_size(42793), - vals(ref, total_size), - dvals(exec) + : rand(293), total_size(42793), vals(ref, total_size), dvals(exec) { std::uniform_int_distribution dist(0, 1000); for (gko::size_type i = 0; i < total_size; ++i) { @@ -69,17 +63,6 @@ class PrefixSum : public ::testing::Test { dvals = vals; } - void test(gko::size_type size) - { - gko::kernels::reference::components::prefix_sum(ref, vals.get_data(), - size); - gko::kernels::omp::components::prefix_sum(exec, dvals.get_data(), size); - - GKO_ASSERT_ARRAY_EQ(vals, dvals); - } - - std::shared_ptr ref; - std::shared_ptr exec; std::default_random_engine rand; gko::size_type total_size; gko::array vals; @@ -89,17 +72,17 @@ class PrefixSum : public ::testing::Test { TYPED_TEST_SUITE(PrefixSum, gko::test::IndexTypes, TypenameNameGenerator); -TYPED_TEST(PrefixSum, TrivialCasesEqualReference) +TYPED_TEST(PrefixSum, EqualsReference) { - this->test(0); - this->test(1); + using gko::size_type; + for (auto size : + {size_type{0}, size_type{1}, size_type{131}, this->total_size}) { + SCOPED_TRACE(size); + gko::kernels::reference::components::prefix_sum( + this->ref, this->vals.get_data(), size); + gko::kernels::EXEC_NAMESPACE::components::prefix_sum( + this->exec, this->dvals.get_data(), size); + + GKO_ASSERT_ARRAY_EQ(this->vals, this->dvals); + } } - - -TYPED_TEST(PrefixSum, SmallEqualsReference) { this->test(100); } - - -TYPED_TEST(PrefixSum, BigEqualsReference) { this->test(this->total_size); } - - -} // namespace diff --git a/test/components/reduce_array_kernels.cpp b/test/components/reduce_array_kernels.cpp index b0a05521c38..01121cfb907 100644 --- a/test/components/reduce_array_kernels.cpp +++ b/test/components/reduce_array_kernels.cpp @@ -48,36 +48,21 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - template -class ReduceArray : public ::testing::Test { +class ReduceArray : public CommonTestFixture { protected: using value_type = T; - ReduceArray() : total_size(6355) {} - - void SetUp() + ReduceArray() + : total_size(6355), + out{ref, I{2}}, + dout{exec, out}, + vals{ref, total_size}, + dvals{exec} { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - out = gko::array{ref, I{2}}; - dout = gko::array{exec, out}; - vals = gko::array{ref, total_size}; - dvals = gko::array{exec}; std::fill_n(vals.get_data(), total_size, 3); dvals = vals; } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - - std::shared_ptr ref; - std::shared_ptr exec; gko::size_type total_size; gko::array out; gko::array dout; @@ -98,6 +83,3 @@ TYPED_TEST(ReduceArray, EqualsReference) GKO_ASSERT_ARRAY_EQ(this->out, this->dout); } - - -} // namespace diff --git a/test/distributed/partition_kernels.cpp b/test/distributed/partition_kernels.cpp index a70f68bf479..1c96c8cbec0 100644 --- a/test/distributed/partition_kernels.cpp +++ b/test/distributed/partition_kernels.cpp @@ -50,14 +50,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - using comm_index_type = gko::distributed::comm_index_type; template -class Partition : public ::testing::Test { +class Partition : public CommonTestFixture { protected: using local_index_type = typename std::tuple_element<0, decltype(LocalGlobalIndexType())>::type; @@ -68,19 +65,6 @@ class Partition : public ::testing::Test { Partition() : rand_engine(96457) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - void assert_equal(std::unique_ptr& part, std::unique_ptr& dpart) { @@ -119,9 +103,6 @@ class Partition : public ::testing::Test { } std::default_random_engine rand_engine; - - std::shared_ptr ref; - std::shared_ptr exec; }; TYPED_TEST_SUITE(Partition, gko::test::LocalGlobalIndexTypes, @@ -389,6 +370,3 @@ TYPED_TEST(Partition, IsOrderedRandom) ASSERT_EQ(part->has_ordered_parts(), dpart->has_ordered_parts()); } - - -} // namespace diff --git a/test/factorization/CMakeLists.txt b/test/factorization/CMakeLists.txt index 8993511942e..5af301d0224 100644 --- a/test/factorization/CMakeLists.txt +++ b/test/factorization/CMakeLists.txt @@ -1,2 +1,8 @@ ginkgo_create_common_test(cholesky_kernels) ginkgo_create_common_test(lu_kernels DISABLE_EXECUTORS dpcpp) +ginkgo_create_common_test(ic_kernels DISABLE_EXECUTORS dpcpp omp) +ginkgo_create_common_test(ilu_kernels DISABLE_EXECUTORS dpcpp omp) +ginkgo_create_common_test(par_ic_kernels) +ginkgo_create_common_test(par_ict_kernels) +ginkgo_create_common_test(par_ilu_kernels) +ginkgo_create_common_test(par_ilut_kernels DISABLE_EXECUTORS dpcpp) diff --git a/test/factorization/cholesky_kernels.cpp b/test/factorization/cholesky_kernels.cpp index d5148b417c5..8c084d7358f 100644 --- a/test/factorization/cholesky_kernels.cpp +++ b/test/factorization/cholesky_kernels.cpp @@ -56,7 +56,7 @@ namespace { template -class Cholesky : public ::testing::Test { +class Cholesky : public CommonTestFixture { protected: using value_type = typename std::tuple_element<0, decltype(ValueIndexType())>::type; @@ -64,12 +64,8 @@ class Cholesky : public ::testing::Test { typename std::tuple_element<1, decltype(ValueIndexType())>::type; using matrix_type = gko::matrix::Csr; - void SetUp() + Cholesky() : tmp{ref}, dtmp{exec} { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - dtmp.set_executor(ref); - dtmp.set_executor(exec); matrices.emplace_back("example", gko::initialize( {{1, 0, 1, 0, 0, 0, 0, 1, 0, 0}, {0, 1, 0, 1, 0, 0, 0, 0, 0, 1}, @@ -101,15 +97,6 @@ class Cholesky : public ::testing::Test { gko::read(ani1_amd_stream, ref)); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - - std::shared_ptr ref; - std::shared_ptr exec; std::vector>> matrices; gko::array tmp; diff --git a/test/factorization/ic_kernels.cpp b/test/factorization/ic_kernels.cpp new file mode 100644 index 00000000000..87a6597824c --- /dev/null +++ b/test/factorization/ic_kernels.cpp @@ -0,0 +1,113 @@ +/************************************************************* +Copyright (c) 2017-2022, the Ginkgo authors +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*************************************************************/ + +#include +#include +#include +#include + + +#include + + +#include +#include +#include + + +#include "core/test/utils.hpp" +#include "core/test/utils/unsort_matrix.hpp" +#include "matrices/config.hpp" +#include "test/utils/executor.hpp" + + +class Ic : public CommonTestFixture { +protected: + using Csr = gko::matrix::Csr; + + Ic() : rand_engine(6794) + { + std::string file_name(gko::matrices::location_ani4_mtx); + auto input_file = std::ifstream(file_name, std::ios::in); + mtx = gko::read(input_file, ref); + dmtx = gko::clone(exec, mtx); + } + + std::default_random_engine rand_engine; + std::shared_ptr mtx; + std::shared_ptr dmtx; +}; + + +TEST_F(Ic, ComputeICIsEquivalentToRefSorted) +{ + auto fact = gko::factorization::Ic<>::build() + .with_skip_sorting(true) + .on(ref) + ->generate(mtx); + auto dfact = gko::factorization::Ic<>::build() + .with_skip_sorting(true) + .on(exec) + ->generate(dmtx); + + GKO_ASSERT_MTX_NEAR(fact->get_l_factor(), dfact->get_l_factor(), 1e-14); + GKO_ASSERT_MTX_NEAR(fact->get_lt_factor(), dfact->get_lt_factor(), 1e-14); + GKO_ASSERT_MTX_EQ_SPARSITY(fact->get_l_factor(), dfact->get_l_factor()); + GKO_ASSERT_MTX_EQ_SPARSITY(fact->get_lt_factor(), dfact->get_lt_factor()); +} + + +TEST_F(Ic, ComputeICIsEquivalentToRefUnsorted) +{ + gko::test::unsort_matrix(gko::lend(mtx), rand_engine); + dmtx->copy_from(gko::lend(mtx)); + + auto fact = gko::factorization::Ic<>::build().on(ref)->generate(mtx); + auto dfact = gko::factorization::Ic<>::build().on(exec)->generate(dmtx); + + GKO_ASSERT_MTX_NEAR(fact->get_l_factor(), dfact->get_l_factor(), 1e-14); + GKO_ASSERT_MTX_NEAR(fact->get_lt_factor(), dfact->get_lt_factor(), 1e-14); + GKO_ASSERT_MTX_EQ_SPARSITY(fact->get_l_factor(), dfact->get_l_factor()); + GKO_ASSERT_MTX_EQ_SPARSITY(fact->get_lt_factor(), dfact->get_lt_factor()); +} + + +TEST_F(Ic, SetsCorrectStrategy) +{ + auto dfact = gko::factorization::Ic<>::build() + .with_l_strategy(std::make_shared()) + .on(exec) + ->generate(dmtx); + + ASSERT_EQ(dfact->get_l_factor()->get_strategy()->get_name(), "merge_path"); + ASSERT_EQ(dfact->get_lt_factor()->get_strategy()->get_name(), "merge_path"); +} diff --git a/test/factorization/ilu_kernels.cpp b/test/factorization/ilu_kernels.cpp new file mode 100644 index 00000000000..3d1d352fb38 --- /dev/null +++ b/test/factorization/ilu_kernels.cpp @@ -0,0 +1,115 @@ +/************************************************************* +Copyright (c) 2017-2022, the Ginkgo authors +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*************************************************************/ + +#include +#include +#include +#include + + +#include + + +#include +#include +#include + + +#include "core/test/utils.hpp" +#include "core/test/utils/unsort_matrix.hpp" +#include "matrices/config.hpp" +#include "test/utils/executor.hpp" + + +class Ilu : public CommonTestFixture { +protected: + using Csr = gko::matrix::Csr; + + Ilu() : rand_engine(1337) + { + std::string file_name(gko::matrices::location_ani4_mtx); + auto input_file = std::ifstream(file_name, std::ios::in); + mtx = gko::read(input_file, ref); + dmtx = gko::clone(exec, mtx); + } + + std::default_random_engine rand_engine; + std::shared_ptr mtx; + std::shared_ptr dmtx; +}; + + +TEST_F(Ilu, ComputeILUIsEquivalentToRefSorted) +{ + auto fact = gko::factorization::Ilu<>::build() + .with_skip_sorting(true) + .on(ref) + ->generate(mtx); + auto dfact = gko::factorization::Ilu<>::build() + .with_skip_sorting(true) + .on(exec) + ->generate(dmtx); + + GKO_ASSERT_MTX_NEAR(fact->get_l_factor(), dfact->get_l_factor(), 1e-14); + GKO_ASSERT_MTX_NEAR(fact->get_u_factor(), dfact->get_u_factor(), 1e-14); + GKO_ASSERT_MTX_EQ_SPARSITY(fact->get_l_factor(), dfact->get_l_factor()); + GKO_ASSERT_MTX_EQ_SPARSITY(fact->get_u_factor(), dfact->get_u_factor()); +} + + +TEST_F(Ilu, ComputeILUIsEquivalentToRefUnsorted) +{ + gko::test::unsort_matrix(gko::lend(mtx), rand_engine); + dmtx->copy_from(gko::lend(mtx)); + + auto fact = gko::factorization::Ilu<>::build().on(ref)->generate(mtx); + auto dfact = gko::factorization::Ilu<>::build().on(exec)->generate(dmtx); + + GKO_ASSERT_MTX_NEAR(fact->get_l_factor(), dfact->get_l_factor(), 1e-14); + GKO_ASSERT_MTX_NEAR(fact->get_u_factor(), dfact->get_u_factor(), 1e-14); + GKO_ASSERT_MTX_EQ_SPARSITY(fact->get_l_factor(), dfact->get_l_factor()); + GKO_ASSERT_MTX_EQ_SPARSITY(fact->get_u_factor(), dfact->get_u_factor()); +} + + +TEST_F(Ilu, SetsCorrectStrategy) +{ + auto dfact = gko::factorization::Ilu<>::build() + .with_l_strategy(std::make_shared()) + .with_u_strategy(std::make_shared(exec)) + .on(exec) + ->generate(dmtx); + + ASSERT_EQ(dfact->get_l_factor()->get_strategy()->get_name(), "merge_path"); + ASSERT_EQ(dfact->get_u_factor()->get_strategy()->get_name(), + "load_balance"); +} diff --git a/test/factorization/lu_kernels.cpp b/test/factorization/lu_kernels.cpp index 98208ae09cd..c972fe5c039 100644 --- a/test/factorization/lu_kernels.cpp +++ b/test/factorization/lu_kernels.cpp @@ -61,7 +61,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. template -class Lu : public ::testing::Test { +class Lu : public CommonTestFixture { protected: using value_type = typename std::tuple_element<0, decltype(ValueIndexType())>::type; @@ -69,24 +69,14 @@ class Lu : public ::testing::Test { typename std::tuple_element<1, decltype(ValueIndexType())>::type; using matrix_type = typename gko::matrix::Csr; - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - storage_offsets.set_executor(ref); - dstorage_offsets.set_executor(exec); - storage.set_executor(ref); - dstorage.set_executor(exec); - row_descs.set_executor(ref); - drow_descs.set_executor(exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } + Lu() + : storage_offsets{ref}, + dstorage_offsets{exec}, + storage{ref}, + dstorage{exec}, + row_descs{ref}, + drow_descs{exec} + {} void initialize_data(const char* mtx_filename) { @@ -121,8 +111,6 @@ class Lu : public ::testing::Test { dmtx_lu = gko::clone(exec, mtx_lu); } - std::shared_ptr ref; - std::shared_ptr exec; gko::size_type num_rows; std::shared_ptr mtx; std::shared_ptr mtx_lu; diff --git a/omp/test/factorization/par_ic_kernels.cpp b/test/factorization/par_ic_kernels.cpp similarity index 81% rename from omp/test/factorization/par_ic_kernels.cpp rename to test/factorization/par_ic_kernels.cpp index 669901c7cc6..f02627dd48e 100644 --- a/omp/test/factorization/par_ic_kernels.cpp +++ b/test/factorization/par_ic_kernels.cpp @@ -55,13 +55,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "core/matrix/csr_kernels.hpp" #include "core/test/utils.hpp" #include "matrices/config.hpp" - - -namespace { +#include "test/utils/executor.hpp" template -class ParIc : public ::testing::Test { +class ParIc : public CommonTestFixture { protected: using value_type = typename std::tuple_element<0, decltype(ValueIndexType())>::type; @@ -70,32 +68,19 @@ class ParIc : public ::testing::Test { using Coo = gko::matrix::Coo; using Csr = gko::matrix::Csr; - ParIc() - : mtx_size(532, 532), - rand_engine(35461), - ref(gko::ReferenceExecutor::create()), - omp(gko::OmpExecutor::create()) + ParIc() : mtx_size(624, 624), rand_engine(43456) { mtx_l = gko::test::generate_random_lower_triangular_matrix( mtx_size[0], false, std::uniform_int_distribution(10, mtx_size[0]), std::normal_distribution>(0, 10.0), rand_engine, ref); - - dmtx_ani = Csr::create(omp); - dmtx_l_ani = Csr::create(omp); - dmtx_l_ani_init = Csr::create(omp); - dmtx_l = gko::clone(omp, mtx_l); - } - - void SetUp() - { + dmtx_ani = Csr::create(exec); + dmtx_l_ani = Csr::create(exec); + dmtx_l_ani_init = Csr::create(exec); + dmtx_l = gko::clone(exec, mtx_l); std::string file_name(gko::matrices::location_ani4_mtx); auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } mtx_ani = gko::read(input_file, ref); mtx_ani->sort_by_column_index(); @@ -120,10 +105,7 @@ class ParIc : public ::testing::Test { dmtx_l_ani_init->copy_from(lend(mtx_l_ani_init)); } - std::shared_ptr ref; - std::shared_ptr omp; - - const gko::dim<2> mtx_size; + gko::dim<2> mtx_size; std::default_random_engine rand_engine; std::unique_ptr mtx_l; @@ -145,9 +127,9 @@ TYPED_TEST(ParIc, KernelInitFactorIsEquivalentToRef) using value_type = typename TestFixture::value_type; gko::kernels::reference::par_ic_factorization::init_factor( - this->ref, lend(this->mtx_l)); - gko::kernels::omp::par_ic_factorization::init_factor(this->omp, - lend(this->dmtx_l)); + this->ref, this->mtx_l.get()); + gko::kernels::EXEC_NAMESPACE::par_ic_factorization::init_factor( + this->exec, this->dmtx_l.get()); GKO_ASSERT_MTX_NEAR(this->mtx_l, this->dmtx_l, r::value); } @@ -160,16 +142,12 @@ TYPED_TEST(ParIc, KernelComputeFactorIsEquivalentToRef) auto square_size = this->mtx_ani->get_size(); auto mtx_l_coo = Coo::create(this->ref, square_size); this->mtx_l_ani->convert_to(lend(mtx_l_coo)); - auto dmtx_l_coo = Coo::create(this->omp, square_size); - dmtx_l_coo->copy_from(lend(mtx_l_coo)); + auto dmtx_l_coo = gko::clone(this->exec, mtx_l_coo); gko::kernels::reference::par_ic_factorization::compute_factor( - this->ref, 1, lend(mtx_l_coo), lend(this->mtx_l_ani_init)); - gko::kernels::omp::par_ic_factorization::compute_factor( - this->omp, 100, lend(dmtx_l_coo), lend(this->dmtx_l_ani_init)); + this->ref, 1, mtx_l_coo.get(), this->mtx_l_ani_init.get()); + gko::kernels::EXEC_NAMESPACE::par_ic_factorization::compute_factor( + this->exec, 100, dmtx_l_coo.get(), this->dmtx_l_ani_init.get()); GKO_ASSERT_MTX_NEAR(this->mtx_l_ani_init, this->dmtx_l_ani_init, 1e-4); } - - -} // namespace diff --git a/omp/test/factorization/par_ict_kernels.cpp b/test/factorization/par_ict_kernels.cpp similarity index 79% rename from omp/test/factorization/par_ict_kernels.cpp rename to test/factorization/par_ict_kernels.cpp index fb739bd955a..95ad4b918c8 100644 --- a/omp/test/factorization/par_ict_kernels.cpp +++ b/test/factorization/par_ict_kernels.cpp @@ -55,13 +55,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "core/matrix/csr_kernels.hpp" #include "core/test/utils.hpp" #include "matrices/config.hpp" - - -namespace { +#include "test/utils/executor.hpp" template -class ParIct : public ::testing::Test { +class ParIct : public CommonTestFixture { protected: using value_type = typename std::tuple_element<0, decltype(ValueIndexType())>::type; @@ -76,9 +74,7 @@ class ParIct : public ::testing::Test { #else : mtx_size(532, 532), #endif - rand_engine(567321), - ref(gko::ReferenceExecutor::create()), - omp(gko::OmpExecutor::create()) + rand_engine(45856) { mtx = gko::test::generate_random_matrix( mtx_size[0], mtx_size[1], @@ -93,20 +89,12 @@ class ParIct : public ::testing::Test { 1.0), rand_engine, ref); - dmtx_ani = Csr::create(omp); - dmtx_l_ani = Csr::create(omp); - dmtx = gko::clone(omp, mtx); - dmtx_l = gko::clone(omp, mtx_l); - } - - void SetUp() - { + dmtx_ani = Csr::create(exec); + dmtx_l_ani = Csr::create(exec); + dmtx = gko::clone(exec, mtx); + dmtx_l = gko::clone(exec, mtx_l); std::string file_name(gko::matrices::location_ani4_mtx); auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } mtx_ani = gko::read(input_file, ref); mtx_ani->sort_by_column_index(); @@ -127,20 +115,17 @@ class ParIct : public ::testing::Test { dmtx_l_ani->copy_from(lend(mtx_l_ani)); } - std::shared_ptr ref; - std::shared_ptr omp; - const gko::dim<2> mtx_size; std::default_random_engine rand_engine; + std::unique_ptr mtx; std::unique_ptr mtx_ani; std::unique_ptr mtx_l_ani; - std::unique_ptr mtx; std::unique_ptr mtx_l; + std::unique_ptr dmtx; std::unique_ptr dmtx_ani; std::unique_ptr dmtx_l_ani; - std::unique_ptr dmtx; std::unique_ptr dmtx_l; }; @@ -152,18 +137,18 @@ TYPED_TEST(ParIct, KernelAddCandidatesIsEquivalentToRef) using Csr = typename TestFixture::Csr; using value_type = typename TestFixture::value_type; auto mtx_llh = Csr::create(this->ref, this->mtx_size); - this->mtx_l->apply(lend(this->mtx_l->transpose()), lend(mtx_llh)); - auto dmtx_llh = Csr::create(this->omp, this->mtx_size); + this->mtx_l->apply(this->mtx_l->conj_transpose().get(), mtx_llh.get()); + auto dmtx_llh = Csr::create(this->exec, this->mtx_size); dmtx_llh->copy_from(lend(mtx_llh)); auto res_mtx_l = Csr::create(this->ref, this->mtx_size); - auto dres_mtx_l = Csr::create(this->omp, this->mtx_size); + auto dres_mtx_l = Csr::create(this->exec, this->mtx_size); gko::kernels::reference::par_ict_factorization::add_candidates( - this->ref, lend(mtx_llh), lend(this->mtx), lend(this->mtx_l), - lend(res_mtx_l)); - gko::kernels::omp::par_ict_factorization::add_candidates( - this->omp, lend(dmtx_llh), lend(this->dmtx), lend(this->dmtx_l), - lend(dres_mtx_l)); + this->ref, mtx_llh.get(), this->mtx.get(), this->mtx_l.get(), + res_mtx_l.get()); + gko::kernels::EXEC_NAMESPACE::par_ict_factorization::add_candidates( + this->exec, dmtx_llh.get(), this->dmtx.get(), this->dmtx_l.get(), + dres_mtx_l.get()); GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_l, dres_mtx_l); GKO_ASSERT_MTX_NEAR(res_mtx_l, dres_mtx_l, r::value); @@ -176,20 +161,17 @@ TYPED_TEST(ParIct, KernelComputeFactorIsEquivalentToRef) using Coo = typename TestFixture::Coo; auto square_size = this->mtx_ani->get_size(); auto mtx_l_coo = Coo::create(this->ref, square_size); - this->mtx_l_ani->convert_to(lend(mtx_l_coo)); - auto dmtx_l_coo = Coo::create(this->omp, square_size); - dmtx_l_coo->copy_from(lend(mtx_l_coo)); + this->mtx_l_ani->convert_to(mtx_l_coo.get()); + auto dmtx_l_coo = Coo::create(this->exec, square_size); + dmtx_l_coo->copy_from(mtx_l_coo.get()); gko::kernels::reference::par_ict_factorization::compute_factor( - this->ref, lend(this->mtx_ani), lend(this->mtx_l_ani), lend(mtx_l_coo)); + this->ref, this->mtx_ani.get(), this->mtx_l_ani.get(), mtx_l_coo.get()); for (int i = 0; i < 20; ++i) { - gko::kernels::omp::par_ict_factorization::compute_factor( - this->omp, lend(this->dmtx_ani), lend(this->dmtx_l_ani), - lend(dmtx_l_coo)); + gko::kernels::EXEC_NAMESPACE::par_ict_factorization::compute_factor( + this->exec, this->dmtx_ani.get(), this->dmtx_l_ani.get(), + dmtx_l_coo.get()); } GKO_ASSERT_MTX_NEAR(this->mtx_l_ani, this->dmtx_l_ani, 1e-2); } - - -} // namespace diff --git a/test/factorization/par_ilu_kernels.cpp b/test/factorization/par_ilu_kernels.cpp new file mode 100644 index 00000000000..d422452d5e2 --- /dev/null +++ b/test/factorization/par_ilu_kernels.cpp @@ -0,0 +1,310 @@ +/************************************************************* +Copyright (c) 2017-2022, the Ginkgo authors +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*************************************************************/ + +#include "core/factorization/par_ilu_kernels.hpp" + + +#include +#include +#include +#include +#include + + +#include + + +#include +#include +#include +#include +#include + + +#include "core/factorization/factorization_kernels.hpp" +#include "core/test/utils.hpp" +#include "matrices/config.hpp" +#include "test/utils/executor.hpp" + + +template +class ParIlu : public CommonTestFixture { +protected: + using value_type = + typename std::tuple_element<0, decltype(ValueIndexType())>::type; + using index_type = + typename std::tuple_element<1, decltype(ValueIndexType())>::type; + using Dense = gko::matrix::Dense; + using Coo = gko::matrix::Coo; + using Csr = gko::matrix::Csr; + + std::default_random_engine rand_engine; + std::shared_ptr mtx; + std::shared_ptr dmtx; + + ParIlu() : rand_engine(18) + { + std::string file_name(gko::matrices::location_ani4_mtx); + auto input_file = std::ifstream(file_name, std::ios::in); + auto mtx_temp = gko::read(input_file, ref); + // Make sure there are diagonal elements present + gko::kernels::reference::factorization::add_diagonal_elements( + ref, mtx_temp.get(), false); + auto dmtx_temp = gko::clone(exec, mtx_temp); + mtx = gko::give(mtx_temp); + dmtx = gko::give(dmtx_temp); + } + + template + std::unique_ptr gen_mtx(index_type num_rows, index_type num_cols) + { + return gko::test::generate_random_matrix( + num_rows, num_cols, + std::uniform_int_distribution(0, num_cols - 1), + std::normal_distribution>(0.0, 1.0), + rand_engine, ref); + } + + std::unique_ptr gen_unsorted_mtx(index_type num_rows, + index_type num_cols) + { + using std::swap; + auto mtx = gen_mtx(num_rows, num_cols); + auto values = mtx->get_values(); + auto col_idxs = mtx->get_col_idxs(); + const auto row_ptrs = mtx->get_const_row_ptrs(); + for (int row = 0; row < num_rows; ++row) { + const auto row_start = row_ptrs[row]; + const auto row_end = row_ptrs[row + 1]; + const int num_row_elements = row_end - row_start; + auto idx_dist = std::uniform_int_distribution( + row_start, row_end - 1); + for (int i = 0; i < num_row_elements / 2; ++i) { + auto idx1 = idx_dist(rand_engine); + auto idx2 = idx_dist(rand_engine); + if (idx1 != idx2) { + swap(values[idx1], values[idx2]); + swap(col_idxs[idx1], col_idxs[idx2]); + } + } + } + return mtx; + } + + void initialize_row_ptrs(index_type* l_row_ptrs, index_type* u_row_ptrs, + index_type* dl_row_ptrs, index_type* du_row_ptrs) + { + gko::kernels::reference::factorization::initialize_row_ptrs_l_u( + ref, mtx.get(), l_row_ptrs, u_row_ptrs); + gko::kernels::EXEC_NAMESPACE::factorization::initialize_row_ptrs_l_u( + exec, dmtx.get(), dl_row_ptrs, du_row_ptrs); + } + + void initialize_lu(std::unique_ptr& l, std::unique_ptr& u, + std::unique_ptr& dl, std::unique_ptr& du) + { + auto num_row_ptrs = mtx->get_size()[0] + 1; + gko::array l_row_ptrs{ref, num_row_ptrs}; + gko::array u_row_ptrs{ref, num_row_ptrs}; + gko::array dl_row_ptrs{exec, num_row_ptrs}; + gko::array du_row_ptrs{exec, num_row_ptrs}; + + initialize_row_ptrs(l_row_ptrs.get_data(), u_row_ptrs.get_data(), + dl_row_ptrs.get_data(), du_row_ptrs.get_data()); + // Since `initialize_row_ptrs` was already tested, it is expected that + // `*` and `d*` contain identical values + auto l_nnz = l_row_ptrs.get_const_data()[num_row_ptrs - 1]; + auto u_nnz = u_row_ptrs.get_const_data()[num_row_ptrs - 1]; + + l = Csr::create(ref, mtx->get_size(), l_nnz); + u = Csr::create(ref, mtx->get_size(), u_nnz); + dl = Csr::create(exec, dmtx->get_size(), l_nnz); + du = Csr::create(exec, dmtx->get_size(), u_nnz); + // Copy the already initialized `row_ptrs` to the new matrices + ref->copy(num_row_ptrs, l_row_ptrs.get_data(), l->get_row_ptrs()); + ref->copy(num_row_ptrs, u_row_ptrs.get_data(), u->get_row_ptrs()); + exec->copy(num_row_ptrs, dl_row_ptrs.get_data(), dl->get_row_ptrs()); + exec->copy(num_row_ptrs, du_row_ptrs.get_data(), du->get_row_ptrs()); + + gko::kernels::reference::factorization::initialize_l_u( + ref, mtx.get(), l.get(), u.get()); + gko::kernels::EXEC_NAMESPACE::factorization::initialize_l_u( + exec, dmtx.get(), dl.get(), du.get()); + } + + void compute_lu(std::unique_ptr& l, std::unique_ptr& u, + std::unique_ptr& dl, std::unique_ptr& du, + gko::size_type iterations = 0) + { + auto coo = Coo::create(ref); + mtx->convert_to(coo.get()); + auto dcoo = Coo::create(exec); + dmtx->convert_to(dcoo.get()); + initialize_lu(l, u, dl, du); + auto u_transpose_mtx = gko::as(u->transpose()); + auto u_transpose_dmtx = gko::as(du->transpose()); + + gko::kernels::reference::par_ilu_factorization::compute_l_u_factors( + ref, iterations, coo.get(), l.get(), u_transpose_mtx.get()); + gko::kernels::EXEC_NAMESPACE::par_ilu_factorization:: + compute_l_u_factors(exec, iterations, dcoo.get(), dl.get(), + u_transpose_dmtx.get()); + auto u_lin_op = u_transpose_mtx->transpose(); + u = gko::as(std::move(u_lin_op)); + auto du_lin_op = u_transpose_dmtx->transpose(); + du = gko::as(std::move(du_lin_op)); + } +}; + +TYPED_TEST_SUITE(ParIlu, gko::test::ValueIndexTypes, PairTypenameNameGenerator); + + +TYPED_TEST(ParIlu, KernelAddDiagonalElementsSortedEquivalentToRef) +{ + using Csr = typename TestFixture::Csr; + auto mtx = this->template gen_mtx(600, 600); + auto dmtx = gko::clone(this->exec, mtx); + + gko::kernels::reference::factorization::add_diagonal_elements( + this->ref, mtx.get(), true); + gko::kernels::EXEC_NAMESPACE::factorization::add_diagonal_elements( + this->exec, dmtx.get(), true); + + ASSERT_TRUE(mtx->is_sorted_by_column_index()); + GKO_ASSERT_MTX_NEAR(mtx, dmtx, 0.); + GKO_ASSERT_MTX_EQ_SPARSITY(mtx, dmtx); +} + + +TYPED_TEST(ParIlu, KernelAddDiagonalElementsUnsortedEquivalentToRef) +{ + auto mtx = this->gen_unsorted_mtx(600, 600); + auto dmtx = gko::clone(this->exec, mtx); + + gko::kernels::reference::factorization::add_diagonal_elements( + this->ref, mtx.get(), false); + gko::kernels::EXEC_NAMESPACE::factorization::add_diagonal_elements( + this->exec, dmtx.get(), false); + + ASSERT_FALSE(mtx->is_sorted_by_column_index()); + GKO_ASSERT_MTX_NEAR(mtx, dmtx, 0.); + GKO_ASSERT_MTX_EQ_SPARSITY(mtx, dmtx); +} + + +TYPED_TEST(ParIlu, KernelAddDiagonalElementsNonSquareEquivalentToRef) +{ + using Csr = typename TestFixture::Csr; + auto mtx = this->template gen_mtx(600, 500); + auto dmtx = gko::clone(this->exec, mtx); + + gko::kernels::reference::factorization::add_diagonal_elements( + this->ref, mtx.get(), true); + gko::kernels::EXEC_NAMESPACE::factorization::add_diagonal_elements( + this->exec, dmtx.get(), true); + + ASSERT_TRUE(mtx->is_sorted_by_column_index()); + GKO_ASSERT_MTX_NEAR(mtx, dmtx, 0.); + GKO_ASSERT_MTX_EQ_SPARSITY(mtx, dmtx); +} + + +TYPED_TEST(ParIlu, KernelInitializeRowPtrsLUEquivalentToRef) +{ + using index_type = typename TestFixture::index_type; + auto num_row_ptrs = this->mtx->get_size()[0] + 1; + gko::array l_row_ptrs_array(this->ref, num_row_ptrs); + gko::array u_row_ptrs_array(this->ref, num_row_ptrs); + gko::array dl_row_ptrs_array(this->exec, num_row_ptrs); + gko::array du_row_ptrs_array(this->exec, num_row_ptrs); + + this->initialize_row_ptrs( + l_row_ptrs_array.get_data(), u_row_ptrs_array.get_data(), + dl_row_ptrs_array.get_data(), du_row_ptrs_array.get_data()); + + GKO_ASSERT_ARRAY_EQ(l_row_ptrs_array, dl_row_ptrs_array); + GKO_ASSERT_ARRAY_EQ(u_row_ptrs_array, du_row_ptrs_array); +} + + +TYPED_TEST(ParIlu, KernelInitializeParILUIsEquivalentToRef) +{ + using Csr = typename TestFixture::Csr; + using value_type = typename TestFixture::value_type; + std::unique_ptr l_mtx{}; + std::unique_ptr u_mtx{}; + std::unique_ptr dl_mtx{}; + std::unique_ptr du_mtx{}; + + this->initialize_lu(l_mtx, u_mtx, dl_mtx, du_mtx); + + GKO_ASSERT_MTX_NEAR(l_mtx, dl_mtx, r::value); + GKO_ASSERT_MTX_NEAR(u_mtx, du_mtx, r::value); + GKO_ASSERT_MTX_EQ_SPARSITY(l_mtx, dl_mtx); + GKO_ASSERT_MTX_EQ_SPARSITY(u_mtx, du_mtx); +} + + +TYPED_TEST(ParIlu, KernelComputeParILUIsEquivalentToRef) +{ + using Csr = typename TestFixture::Csr; + std::unique_ptr l_mtx{}; + std::unique_ptr u_mtx{}; + std::unique_ptr dl_mtx{}; + std::unique_ptr du_mtx{}; + + this->compute_lu(l_mtx, u_mtx, dl_mtx, du_mtx); + + GKO_ASSERT_MTX_NEAR(l_mtx, dl_mtx, 5e-2); + GKO_ASSERT_MTX_NEAR(u_mtx, du_mtx, 5e-2); + GKO_ASSERT_MTX_EQ_SPARSITY(l_mtx, dl_mtx); + GKO_ASSERT_MTX_EQ_SPARSITY(u_mtx, du_mtx); +} + + +TYPED_TEST(ParIlu, KernelComputeParILUWithMoreIterationsIsEquivalentToRef) +{ + using Csr = typename TestFixture::Csr; + using value_type = typename TestFixture::value_type; + std::unique_ptr l_mtx{}; + std::unique_ptr u_mtx{}; + std::unique_ptr dl_mtx{}; + std::unique_ptr du_mtx{}; + gko::size_type iterations{200}; + + this->compute_lu(l_mtx, u_mtx, dl_mtx, du_mtx, iterations); + + GKO_ASSERT_MTX_NEAR(l_mtx, dl_mtx, r::value); + GKO_ASSERT_MTX_NEAR(u_mtx, du_mtx, r::value); + GKO_ASSERT_MTX_EQ_SPARSITY(l_mtx, dl_mtx); + GKO_ASSERT_MTX_EQ_SPARSITY(u_mtx, du_mtx); +} diff --git a/omp/test/factorization/par_ilut_kernels.cpp b/test/factorization/par_ilut_kernels.cpp similarity index 75% rename from omp/test/factorization/par_ilut_kernels.cpp rename to test/factorization/par_ilut_kernels.cpp index aa4b15025b8..732112bc8a8 100644 --- a/omp/test/factorization/par_ilut_kernels.cpp +++ b/test/factorization/par_ilut_kernels.cpp @@ -55,13 +55,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "core/matrix/csr_kernels.hpp" #include "core/test/utils.hpp" #include "matrices/config.hpp" - - -namespace { +#include "test/utils/executor.hpp" template -class ParIlut : public ::testing::Test { +class ParIlut : public CommonTestFixture { protected: using value_type = typename std::tuple_element<0, decltype(ValueIndexType())>::type; @@ -77,9 +75,7 @@ class ParIlut : public ::testing::Test { #else : mtx_size(532, 423), #endif - rand_engine(1337), - ref(gko::ReferenceExecutor::create()), - omp(gko::OmpExecutor::create()) + rand_engine(1337) { mtx1 = gko::test::generate_random_matrix( mtx_size[0], mtx_size[1], @@ -118,26 +114,19 @@ class ParIlut : public ::testing::Test { 1.0), rand_engine, ref); - dmtx1 = gko::clone(omp, mtx1); - dmtx2 = gko::clone(omp, mtx2); - dmtx_square = gko::clone(omp, mtx_square); - dmtx_ani = Csr::create(omp); - dmtx_l_ani = Csr::create(omp); - dmtx_u_ani = Csr::create(omp); - dmtx_ut_ani = Csr::create(omp); - dmtx_l = gko::clone(omp, mtx_l); - dmtx_l2 = gko::clone(omp, mtx_l2); - dmtx_u = gko::clone(omp, mtx_u); - } + dmtx1 = gko::clone(exec, mtx1); + dmtx2 = gko::clone(exec, mtx2); + dmtx_square = gko::clone(exec, mtx_square); + dmtx_ani = Csr::create(exec); + dmtx_l_ani = Csr::create(exec); + dmtx_u_ani = Csr::create(exec); + dmtx_ut_ani = Csr::create(exec); + dmtx_l = gko::clone(exec, mtx_l); + dmtx_l2 = gko::clone(exec, mtx_l2); + dmtx_u = gko::clone(exec, mtx_u); - void SetUp() - { std::string file_name(gko::matrices::location_ani4_mtx); auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } mtx_ani = gko::read(input_file, ref); mtx_ani->sort_by_column_index(); @@ -172,43 +161,49 @@ class ParIlut : public ::testing::Test { dmtx_ut_ani->copy_from(mtx_ut_ani.get()); } - void test_select(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, index_type rank, - gko::remove_complex tolerance = 0.0) + template + void test_select(const std::unique_ptr& mtx, + const std::unique_ptr& dmtx, index_type rank) { + double tolerance = + gko::is_complex() ? r::value : 0.0; auto size = index_type(mtx->get_num_stored_elements()); + using ValueType = typename Mtx::value_type; - gko::remove_complex res{}; - gko::remove_complex dres{}; - gko::array tmp(ref); - gko::array> tmp2(ref); - gko::array dtmp(omp); - gko::array> dtmp2(omp); + gko::remove_complex res{}; + gko::remove_complex dres{}; + gko::array tmp(ref); + gko::array> tmp2(ref); + gko::array dtmp(exec); + gko::array> dtmp2(exec); gko::kernels::reference::par_ilut_factorization::threshold_select( ref, mtx.get(), rank, tmp, tmp2, res); - gko::kernels::omp::par_ilut_factorization::threshold_select( - omp, dmtx.get(), rank, dtmp, dtmp2, dres); + gko::kernels::EXEC_NAMESPACE::par_ilut_factorization::threshold_select( + exec, dmtx.get(), rank, dtmp, dtmp2, dres); - ASSERT_EQ(res, dres); + ASSERT_NEAR(res, dres, tolerance); } - void test_filter(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, + template > + void test_filter(const std::unique_ptr& mtx, + const std::unique_ptr& dmtx, gko::remove_complex threshold, bool lower) { - auto res = Csr::create(ref, mtx_size); - auto dres = Csr::create(omp, mtx_size); + auto res = Mtx::create(ref, mtx_size); + auto dres = Mtx::create(exec, mtx_size); auto res_coo = Coo::create(ref, mtx_size); - auto dres_coo = Coo::create(omp, mtx_size); - auto local_mtx = gko::as(lower ? mtx->clone() : mtx->transpose()); + auto dres_coo = Coo::create(exec, mtx_size); + auto local_mtx = gko::as(lower ? mtx->clone() : mtx->transpose()); auto local_dmtx = - gko::as(lower ? dmtx->clone() : dmtx->transpose()); + gko::as(lower ? dmtx->clone() : dmtx->transpose()); gko::kernels::reference::par_ilut_factorization::threshold_filter( ref, local_mtx.get(), threshold, res.get(), res_coo.get(), lower); - gko::kernels::omp::par_ilut_factorization::threshold_filter( - omp, local_dmtx.get(), threshold, dres.get(), dres_coo.get(), + gko::kernels::EXEC_NAMESPACE::par_ilut_factorization::threshold_filter( + exec, local_dmtx.get(), threshold, dres.get(), dres_coo.get(), lower); GKO_ASSERT_MTX_NEAR(res, dres, 0); @@ -219,38 +214,43 @@ class ParIlut : public ::testing::Test { GKO_ASSERT_MTX_EQ_SPARSITY(dres, dres_coo); } - void test_filter_approx(const std::unique_ptr& mtx, - const std::unique_ptr& dmtx, index_type rank) + template > + void test_filter_approx(const std::unique_ptr& mtx, + const std::unique_ptr& dmtx, index_type rank) { - auto res = Csr::create(ref, mtx_size); - auto dres = Csr::create(omp, mtx_size); + double tolerance = + gko::is_complex() ? r::value : 0.0; + auto res = Mtx::create(ref, mtx_size); + auto dres = Mtx::create(exec, mtx_size); auto res_coo = Coo::create(ref, mtx_size); - auto dres_coo = Coo::create(omp, mtx_size); + auto dres_coo = Coo::create(exec, mtx_size); + using ValueType = typename Mtx::value_type; - gko::array tmp(ref); - gko::array dtmp(omp); - gko::remove_complex threshold{}; - gko::remove_complex dthreshold{}; + gko::array tmp(ref); + gko::array dtmp(exec); + gko::remove_complex threshold{}; + gko::remove_complex dthreshold{}; gko::kernels::reference::par_ilut_factorization:: threshold_filter_approx(ref, mtx.get(), rank, tmp, threshold, res.get(), res_coo.get()); - gko::kernels::omp::par_ilut_factorization::threshold_filter_approx( - omp, dmtx.get(), rank, dtmp, dthreshold, dres.get(), - dres_coo.get()); + gko::kernels::EXEC_NAMESPACE::par_ilut_factorization:: + threshold_filter_approx(exec, dmtx.get(), rank, dtmp, dthreshold, + dres.get(), dres_coo.get()); GKO_ASSERT_MTX_NEAR(res, dres, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); GKO_ASSERT_MTX_NEAR(res, res_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(res, res_coo); GKO_ASSERT_MTX_NEAR(dres, dres_coo, 0); - GKO_ASSERT_MTX_EQ_SPARSITY(dres, dres_coo); - ASSERT_EQ(threshold, dthreshold); + if (tolerance > 0.0) { + GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); + GKO_ASSERT_MTX_EQ_SPARSITY(res, res_coo); + GKO_ASSERT_MTX_EQ_SPARSITY(dres, dres_coo); + } + ASSERT_NEAR(threshold, dthreshold, tolerance); } - std::shared_ptr ref; - std::shared_ptr omp; - const gko::dim<2> mtx_size; std::default_random_engine rand_engine; @@ -306,13 +306,13 @@ TYPED_TEST(ParIlut, KernelThresholdFilterNullptrCooIsEquivalentToRef) using Csr = typename TestFixture::Csr; using Coo = typename TestFixture::Coo; auto res = Csr::create(this->ref, this->mtx_size); - auto dres = Csr::create(this->omp, this->mtx_size); + auto dres = Csr::create(this->exec, this->mtx_size); Coo* null_coo = nullptr; gko::kernels::reference::par_ilut_factorization::threshold_filter( this->ref, this->mtx_l.get(), 0.5, res.get(), null_coo, true); - gko::kernels::omp::par_ilut_factorization::threshold_filter( - this->omp, this->dmtx_l.get(), 0.5, dres.get(), null_coo, true); + gko::kernels::EXEC_NAMESPACE::par_ilut_factorization::threshold_filter( + this->exec, this->dmtx_l.get(), 0.5, dres.get(), null_coo, true); GKO_ASSERT_MTX_NEAR(res, dres, 0); GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); @@ -363,10 +363,10 @@ TYPED_TEST(ParIlut, KernelThresholdFilterApproxNullptrCooIsEquivalentToRef) using index_type = typename TestFixture::index_type; this->test_filter(this->mtx_l, this->dmtx_l, 0.5, true); auto res = Csr::create(this->ref, this->mtx_size); - auto dres = Csr::create(this->omp, this->mtx_size); + auto dres = Csr::create(this->exec, this->mtx_size); Coo* null_coo = nullptr; gko::array tmp(this->ref); - gko::array dtmp(this->omp); + gko::array dtmp(this->exec); gko::remove_complex threshold{}; gko::remove_complex dthreshold{}; index_type rank{}; @@ -374,9 +374,9 @@ TYPED_TEST(ParIlut, KernelThresholdFilterApproxNullptrCooIsEquivalentToRef) gko::kernels::reference::par_ilut_factorization::threshold_filter_approx( this->ref, this->mtx_l.get(), rank, tmp, threshold, res.get(), null_coo); - gko::kernels::omp::par_ilut_factorization::threshold_filter_approx( - this->omp, this->dmtx_l.get(), rank, dtmp, dthreshold, dres.get(), - null_coo); + gko::kernels::EXEC_NAMESPACE::par_ilut_factorization:: + threshold_filter_approx(this->exec, this->dmtx_l.get(), rank, dtmp, + dthreshold, dres.get(), null_coo); GKO_ASSERT_MTX_NEAR(res, dres, 0); GKO_ASSERT_MTX_EQ_SPARSITY(res, dres); @@ -411,18 +411,18 @@ TYPED_TEST(ParIlut, KernelAddCandidatesIsEquivalentToRef) auto square_size = this->mtx_square->get_size(); auto mtx_lu = Csr::create(this->ref, square_size); this->mtx_l2->apply(this->mtx_u.get(), mtx_lu.get()); - auto dmtx_lu = Csr::create(this->omp, square_size); + auto dmtx_lu = Csr::create(this->exec, square_size); dmtx_lu->copy_from(mtx_lu.get()); auto res_mtx_l = Csr::create(this->ref, square_size); auto res_mtx_u = Csr::create(this->ref, square_size); - auto dres_mtx_l = Csr::create(this->omp, square_size); - auto dres_mtx_u = Csr::create(this->omp, square_size); + auto dres_mtx_l = Csr::create(this->exec, square_size); + auto dres_mtx_u = Csr::create(this->exec, square_size); gko::kernels::reference::par_ilut_factorization::add_candidates( this->ref, mtx_lu.get(), this->mtx_square.get(), this->mtx_l2.get(), this->mtx_u.get(), res_mtx_l.get(), res_mtx_u.get()); - gko::kernels::omp::par_ilut_factorization::add_candidates( - this->omp, dmtx_lu.get(), this->dmtx_square.get(), this->dmtx_l2.get(), + gko::kernels::EXEC_NAMESPACE::par_ilut_factorization::add_candidates( + this->exec, dmtx_lu.get(), this->dmtx_square.get(), this->dmtx_l2.get(), this->dmtx_u.get(), dres_mtx_l.get(), dres_mtx_u.get()); GKO_ASSERT_MTX_EQ_SPARSITY(res_mtx_l, dres_mtx_l); @@ -441,8 +441,8 @@ TYPED_TEST(ParIlut, KernelComputeLUIsEquivalentToRef) auto mtx_u_coo = Coo::create(this->ref, square_size); this->mtx_l_ani->convert_to(mtx_l_coo.get()); this->mtx_u_ani->convert_to(mtx_u_coo.get()); - auto dmtx_l_coo = Coo::create(this->omp, square_size); - auto dmtx_u_coo = Coo::create(this->omp, square_size); + auto dmtx_l_coo = Coo::create(this->exec, square_size); + auto dmtx_u_coo = Coo::create(this->exec, square_size); dmtx_l_coo->copy_from(mtx_l_coo.get()); dmtx_u_coo->copy_from(mtx_u_coo.get()); @@ -450,10 +450,11 @@ TYPED_TEST(ParIlut, KernelComputeLUIsEquivalentToRef) this->ref, this->mtx_ani.get(), this->mtx_l_ani.get(), mtx_l_coo.get(), this->mtx_u_ani.get(), mtx_u_coo.get(), this->mtx_ut_ani.get()); for (int i = 0; i < 20; ++i) { - gko::kernels::omp::par_ilut_factorization::compute_l_u_factors( - this->omp, this->dmtx_ani.get(), this->dmtx_l_ani.get(), - dmtx_l_coo.get(), this->dmtx_u_ani.get(), dmtx_u_coo.get(), - this->dmtx_ut_ani.get()); + gko::kernels::EXEC_NAMESPACE::par_ilut_factorization:: + compute_l_u_factors(this->exec, this->dmtx_ani.get(), + this->dmtx_l_ani.get(), dmtx_l_coo.get(), + this->dmtx_u_ani.get(), dmtx_u_coo.get(), + this->dmtx_ut_ani.get()); } auto dmtx_utt_ani = gko::as(this->dmtx_ut_ani->transpose()); @@ -461,6 +462,3 @@ TYPED_TEST(ParIlut, KernelComputeLUIsEquivalentToRef) GKO_ASSERT_MTX_NEAR(this->mtx_u_ani, this->dmtx_u_ani, 1e-2); GKO_ASSERT_MTX_NEAR(this->dmtx_u_ani, dmtx_utt_ani, 0); } - - -} // namespace diff --git a/test/matrix/CMakeLists.txt b/test/matrix/CMakeLists.txt index ff8a6315aa8..e906a1b4242 100644 --- a/test/matrix/CMakeLists.txt +++ b/test/matrix/CMakeLists.txt @@ -1,3 +1,15 @@ ginkgo_create_common_device_test(csr_kernels) +ginkgo_create_common_test(csr_kernels2) +ginkgo_create_common_test(coo_kernels) ginkgo_create_common_test(dense_kernels) +ginkgo_create_common_test(diagonal_kernels) +ginkgo_create_common_test(ell_kernels) +ginkgo_create_common_test(fbcsr_kernels DISABLE_EXECUTORS dpcpp) +if (hipfft_FOUND) + ginkgo_create_common_test(fft_kernels DISABLE_EXECUTORS dpcpp) +else() + ginkgo_create_common_test(fft_kernels DISABLE_EXECUTORS dpcpp hip) +endif() +ginkgo_create_common_test(hybrid_kernels) ginkgo_create_common_test(matrix) +ginkgo_create_common_test(sellp_kernels) diff --git a/cuda/test/matrix/coo_kernels.cpp b/test/matrix/coo_kernels.cpp similarity index 71% rename from cuda/test/matrix/coo_kernels.cpp rename to test/matrix/coo_kernels.cpp index ce0471b4e24..f6ac8441a3f 100644 --- a/cuda/test/matrix/coo_kernels.cpp +++ b/test/matrix/coo_kernels.cpp @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/matrix/coo_kernels.hpp" #include @@ -42,47 +42,31 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include #include #include -#include "core/matrix/coo_kernels.hpp" +#include "core/test/utils.hpp" #include "core/test/utils/unsort_matrix.hpp" -#include "cuda/test/utils.hpp" +#include "test/utils/executor.hpp" -namespace { - - -class Coo : public ::testing::Test { +class Coo : public CommonTestFixture { protected: - using Mtx = gko::matrix::Coo<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; + using Mtx = gko::matrix::Coo; + using Vec = gko::matrix::Dense; + using ComplexVec = gko::matrix::Dense>; Coo() : rand_engine(42) {} - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() - { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } - } - template std::unique_ptr gen_mtx(int num_rows, int num_cols) { return gko::test::generate_random_matrix( num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); + std::normal_distribution(-1.0, 1.0), rand_engine, ref); } void set_up_apply_data(int num_vectors = 1) @@ -92,11 +76,11 @@ class Coo : public ::testing::Test { y = gen_mtx(231, num_vectors); alpha = gko::initialize({2.0}, ref); beta = gko::initialize({-1.0}, ref); - dmtx = gko::clone(cuda, mtx); - dresult = gko::clone(cuda, expected); - dy = gko::clone(cuda, y); - dalpha = gko::clone(cuda, alpha); - dbeta = gko::clone(cuda, beta); + dmtx = gko::clone(exec, mtx); + dresult = gko::clone(exec, expected); + dy = gko::clone(exec, y); + dalpha = gko::clone(exec, alpha); + dbeta = gko::clone(exec, beta); } void unsort_mtx() @@ -105,9 +89,6 @@ class Coo : public ::testing::Test { dmtx->copy_from(mtx.get()); } - std::shared_ptr ref; - std::shared_ptr cuda; - std::default_random_engine rand_engine; std::unique_ptr mtx; @@ -131,7 +112,7 @@ TEST_F(Coo, SimpleApplyIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -139,17 +120,17 @@ TEST_F(Coo, SimpleApplyDoesntOverwritePadding) { set_up_apply_data(); auto dresult_padded = - Vec::create(cuda, dresult->get_size(), dresult->get_stride() + 1); + Vec::create(exec, dresult->get_size(), dresult->get_stride() + 1); dresult_padded->copy_from(dresult.get()); - double padding_val{1234.0}; - cuda->copy_from(cuda->get_master().get(), 1, &padding_val, + value_type padding_val{1234.0}; + exec->copy_from(exec->get_master().get(), 1, &padding_val, dresult_padded->get_values() + 1); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult_padded.get()); - GKO_ASSERT_MTX_NEAR(dresult_padded, expected, 1e-14); - ASSERT_EQ(cuda->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0); + GKO_ASSERT_MTX_NEAR(dresult_padded, expected, r::value); + ASSERT_EQ(exec->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0); } @@ -161,7 +142,7 @@ TEST_F(Coo, SimpleApplyIsEquivalentToRefUnsorted) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -172,7 +153,7 @@ TEST_F(Coo, AdvancedApplyIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -180,17 +161,17 @@ TEST_F(Coo, AdvancedApplyDoesntOverwritePadding) { set_up_apply_data(); auto dresult_padded = - Vec::create(cuda, dresult->get_size(), dresult->get_stride() + 1); + Vec::create(exec, dresult->get_size(), dresult->get_stride() + 1); dresult_padded->copy_from(dresult.get()); - double padding_val{1234.0}; - cuda->copy_from(cuda->get_master().get(), 1, &padding_val, + value_type padding_val{1234.0}; + exec->copy_from(exec->get_master().get(), 1, &padding_val, dresult_padded->get_values() + 1); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult_padded.get()); - GKO_ASSERT_MTX_NEAR(dresult_padded, expected, 1e-14); - ASSERT_EQ(cuda->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0); + GKO_ASSERT_MTX_NEAR(dresult_padded, expected, r::value); + ASSERT_EQ(exec->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0); } @@ -201,7 +182,7 @@ TEST_F(Coo, SimpleApplyAddIsEquivalentToRef) mtx->apply2(y.get(), expected.get()); dmtx->apply2(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -212,7 +193,7 @@ TEST_F(Coo, AdvancedApplyAddIsEquivalentToRef) mtx->apply2(alpha.get(), y.get(), expected.get()); dmtx->apply2(dalpha.get(), dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -223,29 +204,41 @@ TEST_F(Coo, SimpleApplyToDenseMatrixIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Coo, AdvancedApplyToDenseMatrixIsEquivalentToRef) { - set_up_apply_data(3); + set_up_apply_data(4); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Coo, SimpleApplyAddToDenseMatrixIsEquivalentToRef) { - set_up_apply_data(3); + set_up_apply_data(5); mtx->apply2(y.get(), expected.get()); dmtx->apply2(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); +} + + +TEST_F(Coo, SimpleApplyAddToDenseMatrixIsEquivalentToRefUnsorted) +{ + set_up_apply_data(6); + unsort_mtx(); + + mtx->apply2(y.get(), expected.get()); + dmtx->apply2(dy.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -256,18 +249,18 @@ TEST_F(Coo, SimpleApplyAddToLargeDenseMatrixIsEquivalentToRef) mtx->apply2(y.get(), expected.get()); dmtx->apply2(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Coo, AdvancedApplyAddToDenseMatrixIsEquivalentToRef) { - set_up_apply_data(3); + set_up_apply_data(7); mtx->apply2(alpha.get(), y.get(), expected.get()); dmtx->apply2(dalpha.get(), dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -278,7 +271,7 @@ TEST_F(Coo, AdvancedApplyAddToLargeDenseMatrixIsEquivalentToRef) mtx->apply2(y.get(), expected.get()); dmtx->apply2(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -286,14 +279,14 @@ TEST_F(Coo, ApplyToComplexIsEquivalentToRef) { set_up_apply_data(); auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(cuda, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(cuda, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(complex_b.get(), complex_x.get()); dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } @@ -301,14 +294,14 @@ TEST_F(Coo, AdvancedApplyToComplexIsEquivalentToRef) { set_up_apply_data(); auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(cuda, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(cuda, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } @@ -316,22 +309,22 @@ TEST_F(Coo, ApplyAddToComplexIsEquivalentToRef) { set_up_apply_data(); auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(cuda, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(cuda, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply2(alpha.get(), complex_b.get(), complex_x.get()); dmtx->apply2(dalpha.get(), dcomplex_b.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } TEST_F(Coo, ConvertToDenseIsEquivalentToRef) { set_up_apply_data(); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(cuda); + auto dense_mtx = gko::matrix::Dense::create(ref); + auto ddense_mtx = gko::matrix::Dense::create(exec); mtx->convert_to(dense_mtx.get()); dmtx->convert_to(ddense_mtx.get()); @@ -343,9 +336,9 @@ TEST_F(Coo, ConvertToDenseIsEquivalentToRef) TEST_F(Coo, ConvertToCsrIsEquivalentToRef) { set_up_apply_data(); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(cuda); + auto dense_mtx = gko::matrix::Dense::create(ref); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); mtx->convert_to(dense_mtx.get()); dense_mtx->convert_to(csr_mtx.get()); @@ -373,7 +366,7 @@ TEST_F(Coo, InplaceAbsoluteMatrixIsEquivalentToRef) mtx->compute_absolute_inplace(); dmtx->compute_absolute_inplace(); - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); + GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); } @@ -384,8 +377,5 @@ TEST_F(Coo, OutplaceAbsoluteMatrixIsEquivalentToRef) auto abs_mtx = mtx->compute_absolute(); auto dabs_mtx = dmtx->compute_absolute(); - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); + GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); } - - -} // namespace diff --git a/test/matrix/csr_kernels.cpp b/test/matrix/csr_kernels.cpp index 02156291ec9..6ce94ef1b56 100644 --- a/test/matrix/csr_kernels.cpp +++ b/test/matrix/csr_kernels.cpp @@ -52,35 +52,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class Csr : public ::testing::Test { +class Csr : public CommonTestFixture { protected: - using itype = int; -#if GINKGO_COMMON_SINGLE_MODE - using vtype = float; -#else - using vtype = double; -#endif - using Mtx = gko::matrix::Csr; - using Vec = gko::matrix::Dense; + using Mtx = gko::matrix::Csr; + using Vec = gko::matrix::Dense; Csr() : rand_engine(15) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - template std::unique_ptr gen_mtx(int num_rows, int num_cols) { @@ -100,9 +78,6 @@ class Csr : public ::testing::Test { dalpha->copy_from(alpha.get()); } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; std::unique_ptr x; @@ -119,7 +94,7 @@ TEST_F(Csr, ScaleIsEquivalentToRef) x->scale(alpha.get()); dx->scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -130,23 +105,19 @@ TEST_F(Csr, InvScaleIsEquivalentToRef) x->inv_scale(alpha.get()); dx->inv_scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } template -class CsrLookup : public ::testing::Test { +class CsrLookup : public CommonTestFixture { public: using value_type = float; using index_type = IndexType; using Mtx = gko::matrix::Csr; - CsrLookup() : rand_engine(15) {} - - void SetUp() + CsrLookup() : rand_engine(15) { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); auto data = gko::test::generate_random_matrix_data( 628, 923, std::uniform_int_distribution(10, 300), @@ -178,16 +149,7 @@ class CsrLookup : public ::testing::Test { dstorage_array.set_executor(exec); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - std::default_random_engine rand_engine; - std::shared_ptr ref; - std::shared_ptr exec; std::unique_ptr mtx; std::unique_ptr dmtx; gko::array row_desc_array; @@ -318,6 +280,3 @@ TYPED_TEST(CsrLookup, BuildLookupWorks) } } } - - -} // namespace diff --git a/cuda/test/matrix/csr_kernels.cpp b/test/matrix/csr_kernels2.cpp similarity index 55% rename from cuda/test/matrix/csr_kernels.cpp rename to test/matrix/csr_kernels2.cpp index ace4ab59a9f..d7f24fd66ec 100644 --- a/cuda/test/matrix/csr_kernels.cpp +++ b/test/matrix/csr_kernels2.cpp @@ -34,6 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +#include #include @@ -53,21 +54,19 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "core/components/prefix_sum_kernels.hpp" #include "core/matrix/csr_kernels.hpp" +#include "core/test/utils.hpp" #include "core/test/utils/unsort_matrix.hpp" #include "core/utils/matrix_utils.hpp" -#include "cuda/test/utils.hpp" +#include "test/utils/executor.hpp" -namespace { - - -class Csr : public ::testing::Test { +class Csr : public CommonTestFixture { protected: using Arr = gko::array; - using Vec = gko::matrix::Dense<>; - using Mtx = gko::matrix::Csr<>; - using ComplexVec = gko::matrix::Dense>; - using ComplexMtx = gko::matrix::Csr>; + using Vec = gko::matrix::Dense; + using Mtx = gko::matrix::Csr; + using ComplexVec = gko::matrix::Dense>; + using ComplexMtx = gko::matrix::Csr>; Csr() #ifdef GINKGO_FAST_TESTS @@ -78,41 +77,74 @@ class Csr : public ::testing::Test { rand_engine(42) {} - void SetUp() - { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - cuda = gko::CudaExecutor::create(0, ref); - } - - void TearDown() + template + std::unique_ptr gen_mtx(int num_rows, int num_cols, + int min_nnz_row, int max_nnz_row) { - if (cuda != nullptr) { - ASSERT_NO_THROW(cuda->synchronize()); - } + return gko::test::generate_random_matrix( + num_rows, num_cols, + std::uniform_int_distribution<>(min_nnz_row, max_nnz_row), + std::normal_distribution(-1.0, 1.0), rand_engine, ref); } template std::unique_ptr gen_mtx(int num_rows, int num_cols, int min_nnz_row) { - return gko::test::generate_random_matrix( - num_rows, num_cols, - std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); + return gen_mtx(num_rows, num_cols, min_nnz_row, num_cols); } - void set_up_apply_data() + void set_up_mat_data() { mtx2 = Mtx::create(ref); mtx2->copy_from(gen_mtx(mtx_size[0], mtx_size[1], 5)); - dmtx2 = Mtx::create(cuda); + dmtx2 = Mtx::create(exec); dmtx2->copy_from(mtx2.get()); } - void set_up_apply_data(std::shared_ptr strategy, - int num_vectors = 1) + template + void set_up_strategy(std::shared_ptr& strategy) + { +#ifdef GKO_COMPILING_OMP + throw std::runtime_error{"We shouldn't be testing this"}; +#else + strategy = std::make_shared(exec); +#endif + } + + template + void set_up_strategy(std::shared_ptr& strategy) + { + strategy = std::make_shared(); + } + + template + void set_up_strategy(std::shared_ptr& strategy) + { +#ifdef GKO_COMPILING_OMP + throw std::runtime_error{"We shouldn't be testing this"}; +#else + strategy = std::make_shared(exec); +#endif + } + + template + void set_up_strategy(std::shared_ptr& strategy) + { + strategy = std::make_shared(); + } + + template + void set_up_strategy(std::shared_ptr& strategy) + { + strategy = std::make_shared(); + } + + template + void set_up_apply_data(int num_vectors = 1) { + std::shared_ptr strategy; + set_up_strategy(strategy); mtx = Mtx::create(ref, strategy); mtx->copy_from(gen_mtx(mtx_size[0], mtx_size[1], 1)); square_mtx = Mtx::create(ref, strategy); @@ -121,14 +153,14 @@ class Csr : public ::testing::Test { y = gen_mtx(mtx_size[1], num_vectors, 1); alpha = gko::initialize({2.0}, ref); beta = gko::initialize({-1.0}, ref); - dmtx = Mtx::create(cuda, strategy); + dmtx = Mtx::create(exec, strategy); dmtx->copy_from(mtx.get()); - square_dmtx = Mtx::create(cuda, strategy); + square_dmtx = Mtx::create(exec, strategy); square_dmtx->copy_from(square_mtx.get()); - dresult = gko::clone(cuda, expected); - dy = gko::clone(cuda, y); - dalpha = gko::clone(cuda, alpha); - dbeta = gko::clone(cuda, beta); + dresult = gko::clone(exec, expected); + dy = gko::clone(exec, y); + dalpha = gko::clone(exec, alpha); + dbeta = gko::clone(exec, beta); std::vector tmp(mtx->get_size()[0], 0); auto rng = std::default_random_engine{}; @@ -141,13 +173,15 @@ class Csr : public ::testing::Test { cpermute_idxs = std::make_unique(ref, tmp2.begin(), tmp2.end()); } - void set_up_apply_complex_data( - std::shared_ptr strategy) + template + void set_up_apply_complex_data() { + std::shared_ptr strategy; + set_up_strategy(strategy); complex_mtx = ComplexMtx::create(ref, strategy); complex_mtx->copy_from( gen_mtx(mtx_size[0], mtx_size[1], 1)); - complex_dmtx = ComplexMtx::create(cuda, strategy); + complex_dmtx = ComplexMtx::create(exec, strategy); complex_dmtx->copy_from(complex_mtx.get()); } @@ -157,10 +191,6 @@ class Csr : public ::testing::Test { dmtx->copy_from(mtx.get()); } - - std::shared_ptr ref; - std::shared_ptr cuda; - const gko::dim<2> mtx_size; std::default_random_engine rand_engine; @@ -188,236 +218,282 @@ class Csr : public ::testing::Test { TEST_F(Csr, StrategyAfterCopyIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); ASSERT_EQ(mtx->get_strategy()->get_name(), dmtx->get_strategy()->get_name()); } -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithLoadBalance) +TEST_F(Csr, SimpleApplyIsEquivalentToRefWithClassical) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithLoadBalanceUnsorted) +TEST_F(Csr, SimpleApplyIsEquivalentToRefWithClassicalUnsorted) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); unsort_mtx(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithLoadBalance) +TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithClassical) +{ + set_up_apply_data(); + + mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); + dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); +} + + +TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithClassical) +{ + set_up_apply_data(3); + + mtx->apply(y.get(), expected.get()); + dmtx->apply(dy.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); +} + + +TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithClassical) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(3); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithCusparse) +// OpenMP doesn't have strategies +#ifndef GKO_COMPILING_OMP + + +TEST_F(Csr, SimpleApplyIsEquivalentToRefWithLoadBalance) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithCusparseUnsorted) +TEST_F(Csr, SimpleApplyIsEquivalentToRefWithLoadBalanceUnsorted) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); unsort_mtx(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithCusparse) +TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithLoadBalance) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithMergePath) +TEST_F(Csr, SimpleApplyIsEquivalentToRefWithSparselib) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithMergePathUnsorted) +TEST_F(Csr, SimpleApplyIsEquivalentToRefWithSparselibUnsorted) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); unsort_mtx(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithMergePath) +TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithSparselib) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithClassical) +TEST_F(Csr, SimpleApplyIsEquivalentToRefWithMergePath) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, SimpleApplyIsEquivalentToRefWithClassicalUnsorted) +TEST_F(Csr, SimpleApplyIsEquivalentToRefWithMergePathUnsorted) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); unsort_mtx(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithClassical) +TEST_F(Csr, AdvancedApplyIsEquivalentToRefWithMergePath) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Csr, SimpleApplyIsEquivalentToRefWithAutomatical) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithLoadBalance) +TEST_F(Csr, SimpleApplyIsEquivalentToRefWithAutomaticalUnsorted) { - set_up_apply_data(std::make_shared(cuda), 3); + set_up_apply_data(); + unsort_mtx(); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); -} - - -TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithLoadBalance) -{ - set_up_apply_data(std::make_shared(cuda), 3); - - mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); - dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithClassical) +TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithLoadBalance) { - set_up_apply_data(std::make_shared(), 3); + set_up_apply_data(3); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } -TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithClassical) +TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithLoadBalance) { - set_up_apply_data(std::make_shared(), 3); + set_up_apply_data(3); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Csr, SimpleApplyToDenseMatrixIsEquivalentToRefWithMergePath) { - set_up_apply_data(std::make_shared(), 3); + set_up_apply_data(3); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Csr, AdvancedApplyToDenseMatrixIsEquivalentToRefWithMergePath) { - set_up_apply_data(std::make_shared(), 3); + set_up_apply_data(3); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); +} + + +TEST_F(Csr, OneAutomaticalWorksWithDifferentMatrices) +{ + auto automatical = std::make_shared(exec); +#ifdef GKO_COMPILING_CUDA + auto row_len_limit = automatical->nvidia_row_len_limit; +#elif defined(GKO_COMPILING_HIP) + auto row_len_limit = std::max(automatical->nvidia_row_len_limit, + automatical->amd_row_len_limit); +#else + auto row_len_limit = automatical->intel_row_len_limit; +#endif + auto load_balance_mtx = + gen_mtx(1, row_len_limit + 1000, row_len_limit + 1); + auto classical_mtx = gen_mtx(50, 50, 1); + auto load_balance_mtx_d = gko::clone(exec, load_balance_mtx); + auto classical_mtx_d = gko::clone(exec, classical_mtx); + + load_balance_mtx_d->set_strategy(automatical); + classical_mtx_d->set_strategy(automatical); + + EXPECT_EQ("load_balance", load_balance_mtx_d->get_strategy()->get_name()); + EXPECT_EQ("classical", classical_mtx_d->get_strategy()->get_name()); + ASSERT_NE(load_balance_mtx_d->get_strategy().get(), + classical_mtx_d->get_strategy().get()); } +#endif + + TEST_F(Csr, AdvancedApplyToCsrMatrixIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); auto trans = mtx->transpose(); auto d_trans = dmtx->transpose(); mtx->apply(alpha.get(), trans.get(), beta.get(), square_mtx.get()); dmtx->apply(dalpha.get(), d_trans.get(), dbeta.get(), square_dmtx.get()); - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, 1e-14); + GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, r::value); GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); } @@ -425,34 +501,93 @@ TEST_F(Csr, AdvancedApplyToCsrMatrixIsEquivalentToRef) TEST_F(Csr, SimpleApplyToCsrMatrixIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); auto trans = mtx->transpose(); auto d_trans = dmtx->transpose(); mtx->apply(trans.get(), square_mtx.get()); dmtx->apply(d_trans.get(), square_dmtx.get()); - GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, 1e-14); + GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, r::value); + GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); + ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); +} + + +TEST_F(Csr, SimpleApplyToSparseCsrMatrixIsEquivalentToRef) +{ + set_up_apply_data(); + auto mtx2 = + gen_mtx(mtx->get_size()[1], square_mtx->get_size()[1], 0, 10); + auto dmtx2 = Mtx::create(exec, mtx2->get_size()); + dmtx2->copy_from(mtx2.get()); + + mtx->apply(mtx2.get(), square_mtx.get()); + dmtx->apply(dmtx2.get(), square_dmtx.get()); + + GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); + GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, r::value); + ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); +} + + +TEST_F(Csr, SimpleApplySparseToSparseCsrMatrixIsEquivalentToRef) +{ + set_up_apply_data(); + auto mtx1 = gen_mtx(mtx->get_size()[0], mtx->get_size()[1], 0, 10); + auto mtx2 = + gen_mtx(mtx->get_size()[1], square_mtx->get_size()[1], 0, 10); + auto dmtx1 = gko::clone(exec, mtx1); + auto dmtx2 = gko::clone(exec, mtx2); + + mtx1->apply(mtx2.get(), square_mtx.get()); + dmtx1->apply(dmtx2.get(), square_dmtx.get()); + GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); + GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, r::value); ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); } +// TODO: broken in ROCm <= 4.5 +#ifndef GKO_COMPILING_HIP + + +TEST_F(Csr, SimpleApplyToEmptyCsrMatrixIsEquivalentToRef) +{ + set_up_apply_data(); + auto mtx2 = + gen_mtx(mtx->get_size()[1], square_mtx->get_size()[1], 0, 0); + auto dmtx2 = Mtx::create(exec, mtx2->get_size()); + dmtx2->copy_from(mtx2.get()); + + mtx->apply(mtx2.get(), square_mtx.get()); + dmtx->apply(dmtx2.get(), square_dmtx.get()); + + GKO_ASSERT_MTX_EQ_SPARSITY(square_dmtx, square_mtx); + GKO_ASSERT_MTX_NEAR(square_dmtx, square_mtx, r::value); + ASSERT_TRUE(square_dmtx->is_sorted_by_column_index()); +} + + +#endif + + TEST_F(Csr, AdvancedApplyToIdentityMatrixIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); auto a = gen_mtx(mtx_size[0], mtx_size[1], 0); auto b = gen_mtx(mtx_size[0], mtx_size[1], 0); - auto da = gko::clone(cuda, a); - auto db = gko::clone(cuda, b); + auto da = gko::clone(exec, a); + auto db = gko::clone(exec, b); auto id = gko::matrix::Identity::create(ref, mtx_size[1]); auto did = - gko::matrix::Identity::create(cuda, mtx_size[1]); + gko::matrix::Identity::create(exec, mtx_size[1]); a->apply(alpha.get(), id.get(), beta.get(), b.get()); da->apply(dalpha.get(), did.get(), dbeta.get(), db.get()); - GKO_ASSERT_MTX_NEAR(b, db, 1e-14); + GKO_ASSERT_MTX_NEAR(b, db, r::value); GKO_ASSERT_MTX_EQ_SPARSITY(b, db); ASSERT_TRUE(db->is_sorted_by_column_index()); } @@ -460,37 +595,37 @@ TEST_F(Csr, AdvancedApplyToIdentityMatrixIsEquivalentToRef) TEST_F(Csr, ApplyToComplexIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); auto complex_b = gen_mtx(this->mtx_size[1], 3, 1); - auto dcomplex_b = gko::clone(cuda, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(this->mtx_size[0], 3, 1); - auto dcomplex_x = gko::clone(cuda, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(complex_b.get(), complex_x.get()); dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } TEST_F(Csr, AdvancedApplyToComplexIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); auto complex_b = gen_mtx(this->mtx_size[1], 3, 1); - auto dcomplex_b = gko::clone(cuda, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(this->mtx_size[0], 3, 1); - auto dcomplex_x = gko::clone(cuda, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } TEST_F(Csr, TransposeIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); auto trans = gko::as(mtx->transpose()); auto d_trans = gko::as(dmtx->transpose()); @@ -502,9 +637,9 @@ TEST_F(Csr, TransposeIsEquivalentToRef) TEST_F(Csr, Transpose64IsEquivalentToRef) { - using Mtx64 = gko::matrix::Csr; + using Mtx64 = gko::matrix::Csr; auto mtx = gen_mtx(123, 234, 0); - auto dmtx = gko::clone(cuda, mtx); + auto dmtx = gko::clone(exec, mtx); auto trans = gko::as(mtx->transpose()); auto d_trans = gko::as(dmtx->transpose()); @@ -516,7 +651,7 @@ TEST_F(Csr, Transpose64IsEquivalentToRef) TEST_F(Csr, ConjugateTransposeIsEquivalentToRef) { - set_up_apply_complex_data(std::make_shared(cuda)); + set_up_apply_complex_data(); auto trans = gko::as(complex_mtx->conj_transpose()); auto d_trans = gko::as(complex_dmtx->conj_transpose()); @@ -528,9 +663,9 @@ TEST_F(Csr, ConjugateTransposeIsEquivalentToRef) TEST_F(Csr, ConjugateTranspose64IsEquivalentToRef) { - using Mtx64 = gko::matrix::Csr; + using Mtx64 = gko::matrix::Csr; auto mtx = gen_mtx(123, 234, 0); - auto dmtx = gko::clone(cuda, mtx); + auto dmtx = gko::clone(exec, mtx); auto trans = gko::as(mtx->transpose()); auto d_trans = gko::as(dmtx->transpose()); @@ -542,9 +677,9 @@ TEST_F(Csr, ConjugateTranspose64IsEquivalentToRef) TEST_F(Csr, ConvertToDenseIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(cuda); + set_up_apply_data(); + auto dense_mtx = gko::matrix::Dense::create(ref); + auto ddense_mtx = gko::matrix::Dense::create(exec); mtx->convert_to(dense_mtx.get()); dmtx->convert_to(ddense_mtx.get()); @@ -555,9 +690,9 @@ TEST_F(Csr, ConvertToDenseIsEquivalentToRef) TEST_F(Csr, MoveToDenseIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(cuda); + set_up_apply_data(); + auto dense_mtx = gko::matrix::Dense::create(ref); + auto ddense_mtx = gko::matrix::Dense::create(exec); mtx->move_to(dense_mtx.get()); dmtx->move_to(ddense_mtx.get()); @@ -568,9 +703,9 @@ TEST_F(Csr, MoveToDenseIsEquivalentToRef) TEST_F(Csr, ConvertToEllIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto ell_mtx = gko::matrix::Ell<>::create(ref); - auto dell_mtx = gko::matrix::Ell<>::create(cuda); + set_up_apply_data(); + auto ell_mtx = gko::matrix::Ell::create(ref); + auto dell_mtx = gko::matrix::Ell::create(exec); mtx->convert_to(ell_mtx.get()); dmtx->convert_to(dell_mtx.get()); @@ -581,9 +716,9 @@ TEST_F(Csr, ConvertToEllIsEquivalentToRef) TEST_F(Csr, MoveToEllIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto ell_mtx = gko::matrix::Ell<>::create(ref); - auto dell_mtx = gko::matrix::Ell<>::create(cuda); + set_up_apply_data(); + auto ell_mtx = gko::matrix::Ell::create(ref); + auto dell_mtx = gko::matrix::Ell::create(exec); mtx->move_to(ell_mtx.get()); dmtx->move_to(dell_mtx.get()); @@ -594,9 +729,9 @@ TEST_F(Csr, MoveToEllIsEquivalentToRef) TEST_F(Csr, ConvertToSparsityCsrIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(cuda); + set_up_apply_data(); + auto sparsity_mtx = gko::matrix::SparsityCsr::create(ref); + auto d_sparsity_mtx = gko::matrix::SparsityCsr::create(exec); mtx->convert_to(sparsity_mtx.get()); dmtx->convert_to(d_sparsity_mtx.get()); @@ -607,9 +742,9 @@ TEST_F(Csr, ConvertToSparsityCsrIsEquivalentToRef) TEST_F(Csr, MoveToSparsityCsrIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto sparsity_mtx = gko::matrix::SparsityCsr<>::create(ref); - auto d_sparsity_mtx = gko::matrix::SparsityCsr<>::create(cuda); + set_up_apply_data(); + auto sparsity_mtx = gko::matrix::SparsityCsr::create(ref); + auto d_sparsity_mtx = gko::matrix::SparsityCsr::create(exec); mtx->move_to(sparsity_mtx.get()); dmtx->move_to(d_sparsity_mtx.get()); @@ -620,9 +755,9 @@ TEST_F(Csr, MoveToSparsityCsrIsEquivalentToRef) TEST_F(Csr, ConvertToCooIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(cuda); + set_up_apply_data(); + auto coo_mtx = gko::matrix::Coo::create(ref); + auto dcoo_mtx = gko::matrix::Coo::create(exec); mtx->convert_to(coo_mtx.get()); dmtx->convert_to(dcoo_mtx.get()); @@ -633,9 +768,9 @@ TEST_F(Csr, ConvertToCooIsEquivalentToRef) TEST_F(Csr, MoveToCooIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto coo_mtx = gko::matrix::Coo<>::create(ref); - auto dcoo_mtx = gko::matrix::Coo<>::create(cuda); + set_up_apply_data(); + auto coo_mtx = gko::matrix::Coo::create(ref); + auto dcoo_mtx = gko::matrix::Coo::create(exec); mtx->move_to(coo_mtx.get()); dmtx->move_to(dcoo_mtx.get()); @@ -646,9 +781,9 @@ TEST_F(Csr, MoveToCooIsEquivalentToRef) TEST_F(Csr, ConvertToSellpIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto sellp_mtx = gko::matrix::Sellp<>::create(ref); - auto dsellp_mtx = gko::matrix::Sellp<>::create(cuda); + set_up_apply_data(); + auto sellp_mtx = gko::matrix::Sellp::create(ref); + auto dsellp_mtx = gko::matrix::Sellp::create(exec); mtx->convert_to(sellp_mtx.get()); dmtx->convert_to(dsellp_mtx.get()); @@ -659,9 +794,9 @@ TEST_F(Csr, ConvertToSellpIsEquivalentToRef) TEST_F(Csr, MoveToSellpIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); - auto sellp_mtx = gko::matrix::Sellp<>::create(ref); - auto dsellp_mtx = gko::matrix::Sellp<>::create(cuda); + set_up_apply_data(); + auto sellp_mtx = gko::matrix::Sellp::create(ref); + auto dsellp_mtx = gko::matrix::Sellp::create(exec); mtx->move_to(sellp_mtx.get()); dmtx->move_to(dsellp_mtx.get()); @@ -672,24 +807,24 @@ TEST_F(Csr, MoveToSellpIsEquivalentToRef) TEST_F(Csr, ConvertsEmptyToSellp) { - auto dempty_mtx = Mtx::create(cuda); - auto dsellp_mtx = gko::matrix::Sellp<>::create(cuda); + auto dempty_mtx = Mtx::create(exec); + auto dsellp_mtx = gko::matrix::Sellp::create(exec); dempty_mtx->convert_to(dsellp_mtx.get()); - ASSERT_EQ(cuda->copy_val_to_host(dsellp_mtx->get_const_slice_sets()), 0); + ASSERT_EQ(exec->copy_val_to_host(dsellp_mtx->get_const_slice_sets()), 0); ASSERT_FALSE(dsellp_mtx->get_size()); } TEST_F(Csr, ConvertToHybridIsEquivalentToRef) { - using Hybrid_type = gko::matrix::Hybrid<>; - set_up_apply_data(std::make_shared()); + using Hybrid_type = gko::matrix::Hybrid; + set_up_apply_data(); auto hybrid_mtx = Hybrid_type::create( ref, std::make_shared(2)); auto dhybrid_mtx = Hybrid_type::create( - cuda, std::make_shared(2)); + exec, std::make_shared(2)); mtx->convert_to(hybrid_mtx.get()); dmtx->convert_to(dhybrid_mtx.get()); @@ -700,12 +835,12 @@ TEST_F(Csr, ConvertToHybridIsEquivalentToRef) TEST_F(Csr, MoveToHybridIsEquivalentToRef) { - using Hybrid_type = gko::matrix::Hybrid<>; - set_up_apply_data(std::make_shared()); + using Hybrid_type = gko::matrix::Hybrid; + set_up_apply_data(); auto hybrid_mtx = Hybrid_type::create( ref, std::make_shared(2)); auto dhybrid_mtx = Hybrid_type::create( - cuda, std::make_shared(2)); + exec, std::make_shared(2)); mtx->move_to(hybrid_mtx.get()); dmtx->move_to(dhybrid_mtx.get()); @@ -716,7 +851,7 @@ TEST_F(Csr, MoveToHybridIsEquivalentToRef) TEST_F(Csr, IsPermutable) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); auto permuted = gko::as(square_mtx->permute(rpermute_idxs.get())); auto dpermuted = gko::as(square_dmtx->permute(rpermute_idxs.get())); @@ -728,7 +863,7 @@ TEST_F(Csr, IsPermutable) TEST_F(Csr, IsInversePermutable) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); auto permuted = gko::as(square_mtx->inverse_permute(rpermute_idxs.get())); @@ -742,7 +877,7 @@ TEST_F(Csr, IsInversePermutable) TEST_F(Csr, IsRowPermutable) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); auto r_permute = gko::as(mtx->row_permute(rpermute_idxs.get())); auto dr_permute = gko::as(dmtx->row_permute(rpermute_idxs.get())); @@ -754,7 +889,7 @@ TEST_F(Csr, IsRowPermutable) TEST_F(Csr, IsColPermutable) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); auto c_permute = gko::as(mtx->column_permute(cpermute_idxs.get())); auto dc_permute = gko::as(dmtx->column_permute(cpermute_idxs.get())); @@ -767,7 +902,7 @@ TEST_F(Csr, IsColPermutable) TEST_F(Csr, IsInverseRowPermutable) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); auto inverse_r_permute = gko::as(mtx->inverse_row_permute(rpermute_idxs.get())); @@ -781,7 +916,7 @@ TEST_F(Csr, IsInverseRowPermutable) TEST_F(Csr, IsInverseColPermutable) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); auto inverse_c_permute = gko::as(mtx->inverse_column_permute(cpermute_idxs.get())); @@ -796,34 +931,34 @@ TEST_F(Csr, IsInverseColPermutable) TEST_F(Csr, RecognizeSortedMatrixIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); - bool is_sorted_cuda{}; + set_up_apply_data(); + bool is_sorted_exec{}; bool is_sorted_ref{}; is_sorted_ref = mtx->is_sorted_by_column_index(); - is_sorted_cuda = dmtx->is_sorted_by_column_index(); + is_sorted_exec = dmtx->is_sorted_by_column_index(); - ASSERT_EQ(is_sorted_ref, is_sorted_cuda); + ASSERT_EQ(is_sorted_ref, is_sorted_exec); } TEST_F(Csr, RecognizeUnsortedMatrixIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); unsort_mtx(); - bool is_sorted_cuda{}; + bool is_sorted_exec{}; bool is_sorted_ref{}; is_sorted_ref = mtx->is_sorted_by_column_index(); - is_sorted_cuda = dmtx->is_sorted_by_column_index(); + is_sorted_exec = dmtx->is_sorted_by_column_index(); - ASSERT_EQ(is_sorted_ref, is_sorted_cuda); + ASSERT_EQ(is_sorted_ref, is_sorted_exec); } TEST_F(Csr, SortSortedMatrixIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); mtx->sort_by_column_index(); dmtx->sort_by_column_index(); @@ -835,7 +970,7 @@ TEST_F(Csr, SortSortedMatrixIsEquivalentToRef) TEST_F(Csr, SortUnsortedMatrixIsEquivalentToRef) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); unsort_mtx(); mtx->sort_by_column_index(); @@ -846,30 +981,9 @@ TEST_F(Csr, SortUnsortedMatrixIsEquivalentToRef) } -TEST_F(Csr, OneAutomaticalWorksWithDifferentMatrices) -{ - auto automatical = std::make_shared(cuda); - auto row_len_limit = std::max(automatical->nvidia_row_len_limit, - automatical->amd_row_len_limit); - auto load_balance_mtx = - gen_mtx(1, row_len_limit + 1000, row_len_limit + 1); - auto classical_mtx = gen_mtx(50, 50, 1); - auto load_balance_mtx_d = gko::clone(cuda, load_balance_mtx); - auto classical_mtx_d = gko::clone(cuda, classical_mtx); - - load_balance_mtx_d->set_strategy(automatical); - classical_mtx_d->set_strategy(automatical); - - EXPECT_EQ("load_balance", load_balance_mtx_d->get_strategy()->get_name()); - EXPECT_EQ("classical", classical_mtx_d->get_strategy()->get_name()); - ASSERT_NE(load_balance_mtx_d->get_strategy().get(), - classical_mtx_d->get_strategy().get()); -} - - TEST_F(Csr, ExtractDiagonalIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); auto diag = mtx->extract_diagonal(); auto ddiag = dmtx->extract_diagonal(); @@ -880,62 +994,62 @@ TEST_F(Csr, ExtractDiagonalIsEquivalentToRef) TEST_F(Csr, InplaceAbsoluteMatrixIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); mtx->compute_absolute_inplace(); dmtx->compute_absolute_inplace(); - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); + GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); } TEST_F(Csr, OutplaceAbsoluteMatrixIsEquivalentToRef) { - set_up_apply_data(std::make_shared(cuda)); + set_up_apply_data(); auto abs_mtx = mtx->compute_absolute(); auto dabs_mtx = dmtx->compute_absolute(); - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); + GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); } TEST_F(Csr, InplaceAbsoluteComplexMatrixIsEquivalentToRef) { - set_up_apply_complex_data(std::make_shared(cuda)); + set_up_apply_complex_data(); complex_mtx->compute_absolute_inplace(); complex_dmtx->compute_absolute_inplace(); - GKO_ASSERT_MTX_NEAR(complex_mtx, complex_dmtx, 1e-14); + GKO_ASSERT_MTX_NEAR(complex_mtx, complex_dmtx, r::value); } TEST_F(Csr, OutplaceAbsoluteComplexMatrixIsEquivalentToRef) { - set_up_apply_complex_data(std::make_shared(cuda)); + set_up_apply_complex_data(); auto abs_mtx = complex_mtx->compute_absolute(); auto dabs_mtx = complex_dmtx->compute_absolute(); - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); + GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); } TEST_F(Csr, CalculateNnzPerRowInSpanIsEquivalentToRef) { - using Mtx = gko::matrix::Csr<>; - set_up_apply_data(); + using Mtx = gko::matrix::Csr; + set_up_mat_data(); gko::span rspan{7, 51}; gko::span cspan{22, 88}; auto size = this->mtx2->get_size(); auto row_nnz = gko::array(this->ref, rspan.length() + 1); - auto drow_nnz = gko::array(this->cuda, row_nnz); + auto drow_nnz = gko::array(this->exec, row_nnz); gko::kernels::reference::csr::calculate_nonzeros_per_row_in_span( this->ref, this->mtx2.get(), rspan, cspan, &row_nnz); - gko::kernels::cuda::csr::calculate_nonzeros_per_row_in_span( - this->cuda, this->dmtx2.get(), rspan, cspan, &drow_nnz); + gko::kernels::EXEC_NAMESPACE::csr::calculate_nonzeros_per_row_in_span( + this->exec, this->dmtx2.get(), rspan, cspan, &drow_nnz); GKO_ASSERT_ARRAY_EQ(row_nnz, drow_nnz); } @@ -943,45 +1057,128 @@ TEST_F(Csr, CalculateNnzPerRowInSpanIsEquivalentToRef) TEST_F(Csr, ComputeSubmatrixIsEquivalentToRef) { - using Mtx = gko::matrix::Csr<>; - using IndexType = int; - using ValueType = double; - set_up_apply_data(); + using Mtx = gko::matrix::Csr; + set_up_mat_data(); gko::span rspan{7, 51}; gko::span cspan{22, 88}; auto size = this->mtx2->get_size(); auto row_nnz = gko::array(this->ref, rspan.length() + 1); + row_nnz.fill(gko::zero()); gko::kernels::reference::csr::calculate_nonzeros_per_row_in_span( this->ref, this->mtx2.get(), rspan, cspan, &row_nnz); gko::kernels::reference::components::prefix_sum( this->ref, row_nnz.get_data(), row_nnz.get_num_elems()); auto num_nnz = row_nnz.get_data()[rspan.length()]; - auto drow_nnz = gko::array(this->cuda, row_nnz); + auto drow_nnz = gko::array(this->exec, row_nnz); auto smat1 = Mtx::create(this->ref, gko::dim<2>(rspan.length(), cspan.length()), - std::move(gko::array(this->ref, num_nnz)), - std::move(gko::array(this->ref, num_nnz)), + std::move(gko::array(this->ref, num_nnz)), + std::move(gko::array(this->ref, num_nnz)), std::move(row_nnz)); auto sdmat1 = - Mtx::create(this->cuda, gko::dim<2>(rspan.length(), cspan.length()), - std::move(gko::array(this->cuda, num_nnz)), - std::move(gko::array(this->cuda, num_nnz)), + Mtx::create(this->exec, gko::dim<2>(rspan.length(), cspan.length()), + std::move(gko::array(this->exec, num_nnz)), + std::move(gko::array(this->exec, num_nnz)), std::move(drow_nnz)); gko::kernels::reference::csr::compute_submatrix(this->ref, this->mtx2.get(), rspan, cspan, smat1.get()); - gko::kernels::cuda::csr::compute_submatrix(this->cuda, this->dmtx2.get(), - rspan, cspan, sdmat1.get()); + gko::kernels::EXEC_NAMESPACE::csr::compute_submatrix( + this->exec, this->dmtx2.get(), rspan, cspan, sdmat1.get()); + + GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); +} + + +#ifdef GKO_COMPILING_OMP + + +TEST_F(Csr, CalculateNnzPerRowInIndexSetIsEquivalentToRef) +{ + using Mtx = gko::matrix::Csr; + set_up_mat_data(); + gko::index_set rset{ + this->ref, {42, 7, 8, 9, 10, 22, 25, 26, 34, 35, 36, 51}}; + gko::index_set cset{this->ref, + {42, 22, 24, 26, 28, 30, 81, 82, 83, 88}}; + gko::index_set drset(this->exec, rset); + gko::index_set dcset(this->exec, cset); + auto row_nnz = gko::array(this->ref, rset.get_num_elems() + 1); + row_nnz.fill(gko::zero()); + auto drow_nnz = gko::array(this->exec, row_nnz); + + gko::kernels::reference::csr::calculate_nonzeros_per_row_in_index_set( + this->ref, this->mtx2.get(), rset, cset, row_nnz.get_data()); + gko::kernels::EXEC_NAMESPACE::csr::calculate_nonzeros_per_row_in_index_set( + this->exec, this->dmtx2.get(), drset, dcset, drow_nnz.get_data()); + + GKO_ASSERT_ARRAY_EQ(row_nnz, drow_nnz); +} + + +TEST_F(Csr, ComputeSubmatrixFromIndexSetIsEquivalentToRef) +{ + using Mtx = gko::matrix::Csr; + set_up_mat_data(); + gko::index_set rset{ + this->ref, {42, 7, 8, 9, 10, 22, 25, 26, 34, 35, 36, 51}}; + gko::index_set cset{this->ref, + {42, 22, 24, 26, 28, 30, 81, 82, 83, 88}}; + gko::index_set drset(this->exec, rset); + gko::index_set dcset(this->exec, cset); + auto row_nnz = gko::array(this->ref, rset.get_num_elems() + 1); + row_nnz.fill(gko::zero()); + gko::kernels::reference::csr::calculate_nonzeros_per_row_in_index_set( + this->ref, this->mtx2.get(), rset, cset, row_nnz.get_data()); + gko::kernels::reference::components::prefix_sum( + this->ref, row_nnz.get_data(), row_nnz.get_num_elems()); + auto num_nnz = row_nnz.get_data()[rset.get_num_elems()]; + auto drow_nnz = gko::array(this->exec, row_nnz); + auto smat1 = Mtx::create( + this->ref, gko::dim<2>(rset.get_num_elems(), cset.get_num_elems()), + std::move(gko::array(this->ref, num_nnz)), + std::move(gko::array(this->ref, num_nnz)), + std::move(row_nnz)); + auto sdmat1 = Mtx::create( + this->exec, gko::dim<2>(rset.get_num_elems(), cset.get_num_elems()), + std::move(gko::array(this->exec, num_nnz)), + std::move(gko::array(this->exec, num_nnz)), + std::move(drow_nnz)); + + gko::kernels::reference::csr::compute_submatrix_from_index_set( + this->ref, this->mtx2.get(), rset, cset, smat1.get()); + gko::kernels::EXEC_NAMESPACE::csr::compute_submatrix_from_index_set( + this->exec, this->dmtx2.get(), drset, dcset, sdmat1.get()); GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); } +TEST_F(Csr, CreateSubMatrixFromIndexSetIsEquivalentToRef) +{ + set_up_mat_data(); + + gko::index_set rset{ + this->ref, {42, 7, 8, 9, 10, 22, 25, 26, 34, 35, 36, 51}}; + gko::index_set cset{this->ref, + {42, 22, 24, 26, 28, 30, 81, 82, 83, 88}}; + gko::index_set drset(this->exec, rset); + gko::index_set dcset(this->exec, cset); + auto smat1 = this->mtx2->create_submatrix(rset, cset); + auto sdmat1 = this->dmtx2->create_submatrix(drset, dcset); + + GKO_ASSERT_MTX_NEAR(sdmat1, smat1, 0.0); +} + + +#endif // GKO_COMPILING_OMP + + TEST_F(Csr, CreateSubMatrixIsEquivalentToRef) { - using Mtx = gko::matrix::Csr<>; - set_up_apply_data(); + using Mtx = gko::matrix::Csr; + set_up_mat_data(); gko::span rspan{47, 81}; gko::span cspan{2, 31}; @@ -992,6 +1189,9 @@ TEST_F(Csr, CreateSubMatrixIsEquivalentToRef) } +#ifndef GKO_COMPILING_DPCPP + + TEST_F(Csr, CanDetectMissingDiagonalEntry) { using T = double; @@ -1001,11 +1201,11 @@ TEST_F(Csr, CanDetectMissingDiagonalEntry) const auto colidxs = ref_mtx->get_col_idxs(); const int testrow = 15; gko::utils::remove_diagonal_entry_from_row(ref_mtx.get(), testrow); - auto mtx = gko::clone(cuda, ref_mtx); + auto mtx = gko::clone(exec, ref_mtx); bool has_diags = true; - gko::kernels::cuda::csr::check_diagonal_entries_exist(cuda, mtx.get(), - has_diags); + gko::kernels::EXEC_NAMESPACE::csr::check_diagonal_entries_exist( + exec, mtx.get(), has_diags); ASSERT_FALSE(has_diags); } @@ -1013,15 +1213,14 @@ TEST_F(Csr, CanDetectMissingDiagonalEntry) TEST_F(Csr, CanDetectWhenAllDiagonalEntriesArePresent) { - using T = double; using Csr = Mtx; auto ref_mtx = gen_mtx(103, 98, 10); gko::utils::ensure_all_diagonal_entries(ref_mtx.get()); - auto mtx = gko::clone(cuda, ref_mtx); + auto mtx = gko::clone(exec, ref_mtx); bool has_diags = true; - gko::kernels::cuda::csr::check_diagonal_entries_exist(cuda, mtx.get(), - has_diags); + gko::kernels::EXEC_NAMESPACE::csr::check_diagonal_entries_exist( + exec, mtx.get(), has_diags); ASSERT_TRUE(has_diags); } @@ -1029,15 +1228,15 @@ TEST_F(Csr, CanDetectWhenAllDiagonalEntriesArePresent) TEST_F(Csr, AddScaledIdentityToNonSquare) { - set_up_apply_data(std::make_shared()); + set_up_apply_data(); gko::utils::ensure_all_diagonal_entries(mtx.get()); dmtx->copy_from(mtx.get()); mtx->add_scaled_identity(alpha.get(), beta.get()); dmtx->add_scaled_identity(dalpha.get(), dbeta.get()); - GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); + GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); } -} // namespace +#endif // GKO_COMPILING_DPCPP diff --git a/test/matrix/dense_kernels.cpp b/test/matrix/dense_kernels.cpp index 70ed0970c81..3b198c20b28 100644 --- a/test/matrix/dense_kernels.cpp +++ b/test/matrix/dense_kernels.cpp @@ -59,49 +59,28 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class Dense : public ::testing::Test { +class Dense : public CommonTestFixture { protected: - using itype = int; -#if GINKGO_COMMON_SINGLE_MODE - using vtype = float; -#else - using vtype = double; -#endif - // in single mode, mixed_type will be the same as vtype + // in single mode, mixed_type will be the same as value_type using mixed_type = float; - using Mtx = gko::matrix::Dense; + using Mtx = gko::matrix::Dense; using MixedMtx = gko::matrix::Dense; - using NormVector = gko::matrix::Dense>; - using Arr = gko::array; - using ComplexMtx = gko::matrix::Dense>; - using Diagonal = gko::matrix::Diagonal; + using NormVector = gko::matrix::Dense>; + using Arr = gko::array; + using ComplexMtx = gko::matrix::Dense>; + using Diagonal = gko::matrix::Diagonal; using MixedComplexMtx = gko::matrix::Dense>; Dense() : rand_engine(15) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - template std::unique_ptr gen_mtx(int num_rows, int num_cols) { return gko::test::generate_random_matrix( num_rows, num_cols, std::uniform_int_distribution<>(num_cols, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); + std::normal_distribution>(0.0, 1.0), + rand_engine, ref); } template @@ -111,7 +90,9 @@ class Dense : public ::testing::Test { return gko::test::generate_random_matrix( num_rows, num_cols, std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); + std::normal_distribution>(-1.0, + 1.0), + rand_engine, ref); } void set_up_vector_data(gko::size_type num_vecs, @@ -126,8 +107,8 @@ class Dense : public ::testing::Test { c_alpha = gen_mtx(1, num_vecs); } else { alpha = gko::initialize({2.0}, ref); - c_alpha = - gko::initialize({std::complex{2.0}}, ref); + c_alpha = gko::initialize( + {std::complex{2.0}}, ref); } dx = gko::clone(exec, x); dy = gko::clone(exec, y); @@ -135,17 +116,23 @@ class Dense : public ::testing::Test { dc_y = gko::clone(exec, c_y); dalpha = gko::clone(exec, alpha); dc_alpha = gko::clone(exec, c_alpha); + result = Mtx::create(ref, gko::dim<2>{1, num_vecs}); + dresult = Mtx::create(exec, gko::dim<2>{1, num_vecs}); } void set_up_apply_data() { - x = gen_mtx(40, 25); + x = gen_mtx(65, 25); y = gen_mtx(25, 35); + c_x = gen_mtx(65, 25); alpha = gko::initialize({2.0}, ref); beta = gko::initialize({-1.0}, ref); + result = gen_mtx(65, 35); square = gen_mtx(x->get_size()[0], x->get_size()[0]); dx = gko::clone(exec, x); dy = gko::clone(exec, y); + dc_x = gko::clone(exec, c_x); + dresult = gko::clone(exec, result); dalpha = gko::clone(exec, alpha); dbeta = gko::clone(exec, beta); dsquare = gko::clone(exec, square); @@ -178,9 +165,6 @@ class Dense : public ::testing::Test { return result; } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; std::unique_ptr x; @@ -190,6 +174,7 @@ class Dense : public ::testing::Test { std::unique_ptr y; std::unique_ptr alpha; std::unique_ptr beta; + std::unique_ptr result; std::unique_ptr square; std::unique_ptr dx; std::unique_ptr dc_x; @@ -198,6 +183,7 @@ class Dense : public ::testing::Test { std::unique_ptr dy; std::unique_ptr dalpha; std::unique_ptr dbeta; + std::unique_ptr dresult; std::unique_ptr dsquare; std::unique_ptr rpermute_idxs; std::unique_ptr cpermute_idxs; @@ -205,12 +191,518 @@ class Dense : public ::testing::Test { }; +TEST_F(Dense, SingleVectorComputeDotIsEquivalentToRef) +{ + set_up_vector_data(1); + + x->compute_dot(y.get(), result.get()); + dx->compute_dot(dy.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(dresult, result, r::value); +} + + +TEST_F(Dense, MultipleVectorComputeDotIsEquivalentToRef) +{ + set_up_vector_data(20); + + x->compute_dot(y.get(), result.get()); + dx->compute_dot(dy.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(dresult, result, r::value); +} + + +TEST_F(Dense, SingleVectorComputeConjDotIsEquivalentToRef) +{ + set_up_vector_data(1); + + x->compute_conj_dot(y.get(), result.get()); + dx->compute_conj_dot(dy.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(dresult, result, r::value); +} + + +TEST_F(Dense, MultipleVectorComputeConjDotIsEquivalentToRef) +{ + set_up_vector_data(20); + + x->compute_conj_dot(y.get(), result.get()); + dx->compute_conj_dot(dy.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(dresult, result, r::value); +} + + +TEST_F(Dense, SingleVectorComputeNorm2IsEquivalentToRef) +{ + set_up_vector_data(1); + auto norm_size = gko::dim<2>{1, x->get_size()[1]}; + auto norm_expected = NormVector::create(this->ref, norm_size); + auto dnorm = NormVector::create(this->exec, norm_size); + + x->compute_norm2(norm_expected.get()); + dx->compute_norm2(dnorm.get()); + + GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); +} + + +TEST_F(Dense, MultipleVectorComputeNorm2IsEquivalentToRef) +{ + set_up_vector_data(20); + auto norm_size = gko::dim<2>{1, x->get_size()[1]}; + auto norm_expected = NormVector::create(this->ref, norm_size); + auto dnorm = NormVector::create(this->exec, norm_size); + + x->compute_norm2(norm_expected.get()); + dx->compute_norm2(dnorm.get()); + + GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); +} + + +TEST_F(Dense, SimpleApplyIsEquivalentToRef) +{ + set_up_apply_data(); + + x->apply(y.get(), result.get()); + dx->apply(dy.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(dresult, result, r::value); +} + + +TEST_F(Dense, SimpleApplyMixedIsEquivalentToRef) +{ + set_up_apply_data(); + + x->apply(convert(y).get(), convert(result).get()); + dx->apply(convert(dy).get(), convert(dresult).get()); + + GKO_ASSERT_MTX_NEAR(dresult, result, 1e-7); +} + + +TEST_F(Dense, AdvancedApplyIsEquivalentToRef) +{ + set_up_apply_data(); + + x->apply(alpha.get(), y.get(), beta.get(), result.get()); + dx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(dresult, result, r::value); +} + + +TEST_F(Dense, AdvancedApplyMixedIsEquivalentToRef) +{ + set_up_apply_data(); + + x->apply(convert(alpha).get(), convert(y).get(), + convert(beta).get(), convert(result).get()); + dx->apply(convert(dalpha).get(), convert(dy).get(), + convert(dbeta).get(), convert(dresult).get()); + + GKO_ASSERT_MTX_NEAR(dresult, result, 1e-7); +} + + +TEST_F(Dense, ApplyToComplexIsEquivalentToRef) +{ + set_up_apply_data(); + auto complex_b = gen_mtx(x->get_size()[1], 1); + auto dcomplex_b = gko::clone(exec, complex_b); + auto complex_x = gen_mtx(x->get_size()[0], 1); + auto dcomplex_x = gko::clone(exec, complex_x); + + x->apply(complex_b.get(), complex_x.get()); + dx->apply(dcomplex_b.get(), dcomplex_x.get()); + + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); +} + + +TEST_F(Dense, ApplyToMixedComplexIsEquivalentToRef) +{ + set_up_apply_data(); + auto complex_b = gen_mtx(x->get_size()[1], 1); + auto dcomplex_b = gko::clone(exec, complex_b); + auto complex_x = gen_mtx(x->get_size()[0], 1); + auto dcomplex_x = gko::clone(exec, complex_x); + + x->apply(complex_b.get(), complex_x.get()); + dx->apply(dcomplex_b.get(), dcomplex_x.get()); + + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 2e-7); +} + + +TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef) +{ + set_up_apply_data(); + auto complex_b = gen_mtx(x->get_size()[1], 1); + auto dcomplex_b = gko::clone(exec, complex_b); + auto complex_x = gen_mtx(x->get_size()[0], 1); + auto dcomplex_x = gko::clone(exec, complex_x); + + x->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); + dx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); + + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); +} + + +TEST_F(Dense, AdvancedApplyToMixedComplexIsEquivalentToRef) +{ + set_up_apply_data(); + auto complex_b = gen_mtx(x->get_size()[1], 1); + auto dcomplex_b = gko::clone(exec, complex_b); + auto complex_x = gen_mtx(x->get_size()[0], 1); + auto dcomplex_x = gko::clone(exec, complex_x); + + x->apply(convert(alpha).get(), complex_b.get(), + convert(beta).get(), complex_x.get()); + dx->apply(convert(dalpha).get(), dcomplex_b.get(), + convert(dbeta).get(), dcomplex_x.get()); + + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 2e-7); +} + + +TEST_F(Dense, ComputeDotComplexIsEquivalentToRef) +{ + set_up_apply_data(); + auto complex_b = gen_mtx(1234, 2); + auto dcomplex_b = gko::clone(exec, complex_b); + auto complex_x = gen_mtx(1234, 2); + auto dcomplex_x = gko::clone(exec, complex_x); + auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); + auto dresult = ComplexMtx::create(exec, gko::dim<2>{1, 2}); + + complex_b->compute_dot(complex_x.get(), result.get()); + dcomplex_b->compute_dot(dcomplex_x.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(result, dresult, r::value * 2); +} + + +TEST_F(Dense, ComputeConjDotComplexIsEquivalentToRef) +{ + set_up_apply_data(); + auto complex_b = gen_mtx(1234, 2); + auto dcomplex_b = gko::clone(exec, complex_b); + auto complex_x = gen_mtx(1234, 2); + auto dcomplex_x = gko::clone(exec, complex_x); + auto result = ComplexMtx::create(ref, gko::dim<2>{1, 2}); + auto dresult = ComplexMtx::create(exec, gko::dim<2>{1, 2}); + + complex_b->compute_conj_dot(complex_x.get(), result.get()); + dcomplex_b->compute_conj_dot(dcomplex_x.get(), dresult.get()); + + GKO_ASSERT_MTX_NEAR(result, dresult, r::value * 2); +} + + +TEST_F(Dense, ConvertToCooIsEquivalentToRef) +{ + set_up_apply_data(); + auto coo_mtx = gko::matrix::Coo::create(ref); + auto dcoo_mtx = gko::matrix::Coo::create(exec); + + x->convert_to(coo_mtx.get()); + dx->convert_to(dcoo_mtx.get()); + + ASSERT_EQ(dcoo_mtx->get_num_stored_elements(), + coo_mtx->get_num_stored_elements()); + GKO_ASSERT_MTX_NEAR(dcoo_mtx.get(), coo_mtx.get(), 0); +} + + +TEST_F(Dense, MoveToCooIsEquivalentToRef) +{ + set_up_apply_data(); + auto coo_mtx = gko::matrix::Coo::create(ref); + auto dcoo_mtx = gko::matrix::Coo::create(exec); + + x->move_to(coo_mtx.get()); + dx->move_to(dcoo_mtx.get()); + + ASSERT_EQ(dcoo_mtx->get_num_stored_elements(), + coo_mtx->get_num_stored_elements()); + GKO_ASSERT_MTX_NEAR(dcoo_mtx.get(), coo_mtx.get(), 0); +} + + +TEST_F(Dense, ConvertToCsrIsEquivalentToRef) +{ + set_up_apply_data(); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); + + x->convert_to(csr_mtx.get()); + dx->convert_to(dcsr_mtx.get()); + + GKO_ASSERT_MTX_NEAR(dcsr_mtx.get(), csr_mtx.get(), 0); +} + + +TEST_F(Dense, MoveToCsrIsEquivalentToRef) +{ + set_up_apply_data(); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); + + x->move_to(csr_mtx.get()); + dx->move_to(dcsr_mtx.get()); + + GKO_ASSERT_MTX_NEAR(dcsr_mtx.get(), csr_mtx.get(), 0); +} + + +TEST_F(Dense, ConvertToSparsityCsrIsEquivalentToRef) +{ + set_up_apply_data(); + auto sparsity_mtx = gko::matrix::SparsityCsr::create(ref); + auto d_sparsity_mtx = gko::matrix::SparsityCsr::create(exec); + + x->convert_to(sparsity_mtx.get()); + dx->convert_to(d_sparsity_mtx.get()); + + GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); +} + + +TEST_F(Dense, MoveToSparsityCsrIsEquivalentToRef) +{ + set_up_apply_data(); + auto sparsity_mtx = gko::matrix::SparsityCsr::create(ref); + auto d_sparsity_mtx = gko::matrix::SparsityCsr::create(exec); + + x->move_to(sparsity_mtx.get()); + dx->move_to(d_sparsity_mtx.get()); + + GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 0); +} + + +TEST_F(Dense, ConvertToEllIsEquivalentToRef) +{ + set_up_apply_data(); + auto ell_mtx = gko::matrix::Ell::create(ref); + auto dell_mtx = gko::matrix::Ell::create(exec); + + x->convert_to(ell_mtx.get()); + dx->convert_to(dell_mtx.get()); + + GKO_ASSERT_MTX_NEAR(dell_mtx.get(), ell_mtx.get(), 0); +} + + +TEST_F(Dense, MoveToEllIsEquivalentToRef) +{ + set_up_apply_data(); + auto ell_mtx = gko::matrix::Ell::create(ref); + auto dell_mtx = gko::matrix::Ell::create(exec); + + x->move_to(ell_mtx.get()); + dx->move_to(dell_mtx.get()); + + GKO_ASSERT_MTX_NEAR(dell_mtx.get(), ell_mtx.get(), 0); +} + + +TEST_F(Dense, ConvertToHybridIsEquivalentToRef) +{ + auto rmtx = gen_mtx(532, 231); + auto omtx = gko::clone(exec, rmtx); + auto srmtx = gko::matrix::Hybrid::create(ref); + auto somtx = gko::matrix::Hybrid::create(exec); + auto drmtx = Mtx::create(ref); + auto domtx = Mtx::create(exec); + + rmtx->convert_to(srmtx.get()); + omtx->convert_to(somtx.get()); + srmtx->convert_to(drmtx.get()); + somtx->convert_to(domtx.get()); + + GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); + GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); + GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); +} + + +TEST_F(Dense, MoveToHybridIsEquivalentToRef) +{ + auto rmtx = gen_mtx(532, 231); + auto omtx = gko::clone(exec, rmtx); + auto srmtx = gko::matrix::Hybrid::create(ref); + auto somtx = gko::matrix::Hybrid::create(exec); + auto drmtx = Mtx::create(ref); + auto domtx = Mtx::create(exec); + + rmtx->move_to(srmtx.get()); + omtx->move_to(somtx.get()); + srmtx->move_to(drmtx.get()); + somtx->move_to(domtx.get()); + + GKO_ASSERT_MTX_NEAR(drmtx, domtx, 0); + GKO_ASSERT_MTX_NEAR(srmtx, somtx, 0); + GKO_ASSERT_MTX_NEAR(domtx, omtx, 0); +} + + +TEST_F(Dense, ConvertToSellpIsEquivalentToRef) +{ + set_up_apply_data(); + auto sellp_mtx = gko::matrix::Sellp::create(ref); + auto dsellp_mtx = gko::matrix::Sellp::create(exec); + + x->convert_to(sellp_mtx.get()); + dx->convert_to(dsellp_mtx.get()); + + GKO_ASSERT_MTX_NEAR(sellp_mtx, dsellp_mtx, 0); +} + + +TEST_F(Dense, MoveToSellpIsEquivalentToRef) +{ + set_up_apply_data(); + auto sellp_mtx = gko::matrix::Sellp::create(ref); + auto dsellp_mtx = gko::matrix::Sellp::create(exec); + + x->move_to(sellp_mtx.get()); + dx->move_to(dsellp_mtx.get()); + + GKO_ASSERT_MTX_NEAR(sellp_mtx, dsellp_mtx, 0); +} + + +TEST_F(Dense, ConvertsEmptyToSellp) +{ + auto dempty_mtx = Mtx::create(exec); + auto dsellp_mtx = gko::matrix::Sellp::create(exec); + + dempty_mtx->convert_to(dsellp_mtx.get()); + + ASSERT_EQ(exec->copy_val_to_host(dsellp_mtx->get_const_slice_sets()), 0); + ASSERT_FALSE(dsellp_mtx->get_size()); +} + + +TEST_F(Dense, CalculateNNZPerRowIsEquivalentToRef) +{ + set_up_apply_data(); + gko::array nnz_per_row(ref); + nnz_per_row.resize_and_reset(x->get_size()[0]); + gko::array dnnz_per_row(exec); + dnnz_per_row.resize_and_reset(dx->get_size()[0]); + + gko::kernels::reference::dense::count_nonzeros_per_row( + ref, x.get(), nnz_per_row.get_data()); + gko::kernels::EXEC_NAMESPACE::dense::count_nonzeros_per_row( + exec, dx.get(), dnnz_per_row.get_data()); + + auto tmp = gko::array(ref, dnnz_per_row); + for (gko::size_type i = 0; i < nnz_per_row.get_num_elems(); i++) { + ASSERT_EQ(nnz_per_row.get_const_data()[i], tmp.get_const_data()[i]); + } +} + + +TEST_F(Dense, ComputeMaxNNZPerRowIsEquivalentToRef) +{ + set_up_apply_data(); + gko::size_type max_nnz; + gko::size_type dmax_nnz; + + gko::kernels::reference::dense::compute_max_nnz_per_row(ref, x.get(), + max_nnz); + gko::kernels::EXEC_NAMESPACE::dense::compute_max_nnz_per_row(exec, dx.get(), + dmax_nnz); + + ASSERT_EQ(max_nnz, dmax_nnz); +} + + +TEST_F(Dense, IsTransposable) +{ + set_up_apply_data(); + + auto trans = x->transpose(); + auto dtrans = dx->transpose(); + + GKO_ASSERT_MTX_NEAR(static_cast(dtrans.get()), + static_cast(trans.get()), 0); +} + + +TEST_F(Dense, IsTransposableIntoDenseCrossExecutor) +{ + set_up_apply_data(); + auto row_span = gko::span{0, x->get_size()[0] - 2}; + auto col_span = gko::span{0, x->get_size()[1] - 2}; + auto sub_x = x->create_submatrix(row_span, col_span); + auto sub_dx = dx->create_submatrix(row_span, col_span); + // create the target matrices on another executor to + // force temporary clone + auto trans = Mtx::create(ref, gko::transpose(sub_x->get_size())); + auto dtrans = Mtx::create(ref, gko::transpose(sub_x->get_size()), + sub_x->get_size()[0] + 4); + + sub_x->transpose(trans.get()); + sub_dx->transpose(dtrans.get()); + + GKO_ASSERT_MTX_NEAR(dtrans, trans, 0); +} + + +// HIP doesn't support complex in all our supported versions yet +#ifndef GKO_COMPILING_HIP + + +TEST_F(Dense, IsConjugateTransposable) +{ + set_up_apply_data(); + + auto trans = c_x->conj_transpose(); + auto dtrans = dc_x->conj_transpose(); + + GKO_ASSERT_MTX_NEAR(static_cast(dtrans.get()), + static_cast(trans.get()), 0); +} + + +TEST_F(Dense, IsConjugateTransposableIntoDenseCrossExecutor) +{ + set_up_apply_data(); + auto row_span = gko::span{0, c_x->get_size()[0] - 2}; + auto col_span = gko::span{0, c_x->get_size()[1] - 2}; + auto sub_x = c_x->create_submatrix(row_span, col_span); + auto sub_dx = dc_x->create_submatrix(row_span, col_span); + // create the target matrices on another executor to + // force temporary clone + auto trans = ComplexMtx::create(ref, gko::transpose(sub_x->get_size())); + auto dtrans = ComplexMtx::create(ref, gko::transpose(sub_x->get_size()), + sub_x->get_size()[0] + 4); + + sub_x->conj_transpose(trans.get()); + sub_dx->conj_transpose(dtrans.get()); + + GKO_ASSERT_MTX_NEAR(dtrans, trans, 0); +} + + +#endif + + TEST_F(Dense, CopyRespectsStride) { set_up_vector_data(3); auto stride = dx->get_size()[1] + 1; auto result = Mtx::create(exec, dx->get_size(), stride); - vtype val = 1234567.0; + value_type val = 1234567.0; auto original_data = result->get_values(); auto padding_ptr = original_data + dx->get_size()[1]; exec->copy_from(ref.get(), 1, &val, padding_ptr); @@ -237,7 +729,7 @@ TEST_F(Dense, FillIsEquivalentToRef) TEST_F(Dense, StridedFillIsEquivalentToRef) { - using T = vtype; + using T = value_type; auto x = gko::initialize>( 4, {I{1.0, 2.0}, I{3.0, 4.0}, I{5.0, 6.0}}, ref); auto dx = gko::initialize>( @@ -257,7 +749,7 @@ TEST_F(Dense, SingleVectorScaleIsEquivalentToRef) x->scale(alpha.get()); dx->scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -268,7 +760,7 @@ TEST_F(Dense, SingleVectorInvScaleIsEquivalentToRef) x->inv_scale(alpha.get()); dx->inv_scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -279,7 +771,7 @@ TEST_F(Dense, SingleVectorComplexScaleIsEquivalentToRef) c_x->scale(c_alpha.get()); dc_x->scale(dc_alpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -290,7 +782,7 @@ TEST_F(Dense, SingleVectorComplexInvScaleIsEquivalentToRef) c_x->inv_scale(c_alpha.get()); dc_x->inv_scale(dc_alpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -301,7 +793,7 @@ TEST_F(Dense, SingleVectorComplexRealScaleIsEquivalentToRef) c_x->scale(alpha.get()); dc_x->scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -312,7 +804,7 @@ TEST_F(Dense, SingleVectorComplexRealInvScaleIsEquivalentToRef) c_x->inv_scale(alpha.get()); dc_x->inv_scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -323,7 +815,7 @@ TEST_F(Dense, MultipleVectorScaleIsEquivalentToRef) x->scale(alpha.get()); dx->scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -334,7 +826,7 @@ TEST_F(Dense, MultipleVectorInvScaleIsEquivalentToRef) x->inv_scale(alpha.get()); dx->inv_scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -345,7 +837,7 @@ TEST_F(Dense, MultipleVectorComplexScaleIsEquivalentToRef) c_x->scale(c_alpha.get()); dc_x->scale(dc_alpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -356,7 +848,7 @@ TEST_F(Dense, MultipleVectorComplexInvScaleIsEquivalentToRef) c_x->inv_scale(c_alpha.get()); dc_x->inv_scale(dc_alpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -367,7 +859,7 @@ TEST_F(Dense, MultipleVectorComplexRealScaleIsEquivalentToRef) c_x->scale(alpha.get()); dc_x->scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -378,7 +870,7 @@ TEST_F(Dense, MultipleVectorComplexRealInvScaleIsEquivalentToRef) c_x->inv_scale(alpha.get()); dc_x->inv_scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -389,7 +881,7 @@ TEST_F(Dense, MultipleVectorScaleWithDifferentAlphaIsEquivalentToRef) x->scale(alpha.get()); dx->scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -400,7 +892,7 @@ TEST_F(Dense, MultipleVectorInvScaleWithDifferentAlphaIsEquivalentToRef) x->inv_scale(alpha.get()); dx->inv_scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -411,7 +903,7 @@ TEST_F(Dense, MultipleVectorComplexScaleWithDifferentAlphaIsEquivalentToRef) c_x->scale(c_alpha.get()); dc_x->scale(dc_alpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -422,7 +914,7 @@ TEST_F(Dense, MultipleVectorComplexInvScaleWithDifferentAlphaIsEquivalentToRef) c_x->inv_scale(c_alpha.get()); dc_x->inv_scale(dc_alpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -433,7 +925,7 @@ TEST_F(Dense, MultipleVectorComplexRealScaleWithDifferentAlphaIsEquivalentToRef) c_x->scale(alpha.get()); dc_x->scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -445,7 +937,7 @@ TEST_F(Dense, c_x->inv_scale(alpha.get()); dc_x->inv_scale(dalpha.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -456,7 +948,7 @@ TEST_F(Dense, SingleVectorAddScaledIsEquivalentToRef) x->add_scaled(alpha.get(), y.get()); dx->add_scaled(dalpha.get(), dy.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -467,7 +959,7 @@ TEST_F(Dense, SingleVectorSubtractScaledIsEquivalentToRef) x->sub_scaled(alpha.get(), y.get()); dx->sub_scaled(dalpha.get(), dy.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -478,7 +970,7 @@ TEST_F(Dense, SingleVectorComplexAddScaledIsEquivalentToRef) c_x->add_scaled(c_alpha.get(), c_y.get()); dc_x->add_scaled(dc_alpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -489,7 +981,7 @@ TEST_F(Dense, SingleVectorComplexSubtractScaledIsEquivalentToRef) c_x->sub_scaled(c_alpha.get(), c_y.get()); dc_x->sub_scaled(dc_alpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -500,7 +992,7 @@ TEST_F(Dense, SingleVectorComplexRealAddScaledIsEquivalentToRef) c_x->add_scaled(alpha.get(), c_y.get()); dc_x->add_scaled(dalpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -511,7 +1003,7 @@ TEST_F(Dense, SingleVectorComplexRealSubtractScaledIsEquivalentToRef) c_x->sub_scaled(alpha.get(), c_y.get()); dc_x->sub_scaled(dalpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -522,7 +1014,7 @@ TEST_F(Dense, MultipleVectorAddScaledIsEquivalentToRef) x->add_scaled(alpha.get(), y.get()); dx->add_scaled(dalpha.get(), dy.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -533,7 +1025,7 @@ TEST_F(Dense, MultipleVectorSubtractScaledIsEquivalentToRef) x->sub_scaled(alpha.get(), y.get()); dx->sub_scaled(dalpha.get(), dy.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -544,7 +1036,7 @@ TEST_F(Dense, MultipleVectorComplexAddScaledIsEquivalentToRef) c_x->add_scaled(c_alpha.get(), c_y.get()); dc_x->add_scaled(dc_alpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -555,7 +1047,7 @@ TEST_F(Dense, MultipleVectorComplexSubtractScaledIsEquivalentToRef) c_x->sub_scaled(c_alpha.get(), c_y.get()); dc_x->sub_scaled(dc_alpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -566,7 +1058,7 @@ TEST_F(Dense, MultipleVectorComplexRealAddScaledIsEquivalentToRef) c_x->add_scaled(alpha.get(), c_y.get()); dc_x->add_scaled(dalpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -577,7 +1069,7 @@ TEST_F(Dense, MultipleVectorComplexRealSubtractScaledIsEquivalentToRef) c_x->sub_scaled(alpha.get(), c_y.get()); dc_x->sub_scaled(dalpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -588,7 +1080,7 @@ TEST_F(Dense, MultipleVectorAddScaledWithDifferentAlphaIsEquivalentToRef) x->add_scaled(alpha.get(), y.get()); dx->add_scaled(dalpha.get(), dy.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -599,7 +1091,7 @@ TEST_F(Dense, MultipleVectorSubtractScaledWithDifferentAlphaIsEquivalentToRef) x->sub_scaled(alpha.get(), y.get()); dx->sub_scaled(dalpha.get(), dy.get()); - GKO_ASSERT_MTX_NEAR(dx, x, r::value); + GKO_ASSERT_MTX_NEAR(dx, x, r::value); } @@ -610,7 +1102,7 @@ TEST_F(Dense, MultipleVectorComplexAddScaledWithDifferentAlphaIsEquivalentToRef) c_x->add_scaled(c_alpha.get(), c_y.get()); dc_x->add_scaled(dc_alpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -622,7 +1114,7 @@ TEST_F(Dense, c_x->sub_scaled(c_alpha.get(), c_y.get()); dc_x->sub_scaled(dc_alpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -634,7 +1126,7 @@ TEST_F(Dense, c_x->add_scaled(alpha.get(), c_y.get()); dc_x->add_scaled(dalpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -647,7 +1139,7 @@ TEST_F( c_x->sub_scaled(alpha.get(), c_y.get()); dc_x->sub_scaled(dalpha.get(), dc_y.get()); - GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); + GKO_ASSERT_MTX_NEAR(dc_x, c_x, r::value); } @@ -667,7 +1159,7 @@ TEST_F(Dense, AddsScaledDiagIsEquivalentToRef) mat->add_scaled(alpha.get(), diag.get()); dmat->add_scaled(dalpha.get(), ddiag.get()); - GKO_ASSERT_MTX_NEAR(mat, dmat, r::value); + GKO_ASSERT_MTX_NEAR(mat, dmat, r::value); } @@ -687,7 +1179,7 @@ TEST_F(Dense, SubtractScaledDiagIsEquivalentToRef) mat->sub_scaled(alpha.get(), diag.get()); dmat->sub_scaled(dalpha.get(), ddiag.get()); - GKO_ASSERT_MTX_NEAR(mat, dmat, r::value); + GKO_ASSERT_MTX_NEAR(mat, dmat, r::value); } @@ -1010,7 +1502,7 @@ TEST_F(Dense, ComputeDotIsEquivalentToRef) x->compute_dot(y.get(), dot_expected.get()); dx->compute_dot(y.get(), ddot.get()); - GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value * 2); + GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value * 2); } @@ -1027,7 +1519,7 @@ TEST_F(Dense, ComputeDotWithPreallocatedTmpIsEquivalentToRef) x->compute_dot(y.get(), dot_expected.get()); dx->compute_dot(y.get(), ddot.get(), tmp); - GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value); + GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value); } @@ -1044,7 +1536,7 @@ TEST_F(Dense, ComputeDotWithTmpIsEquivalentToRef) x->compute_dot(y.get(), dot_expected.get()); dx->compute_dot(y.get(), ddot.get(), tmp); - GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value); + GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value); } @@ -1060,7 +1552,7 @@ TEST_F(Dense, ComputeConjDotIsEquivalentToRef) x->compute_conj_dot(y.get(), dot_expected.get()); dx->compute_conj_dot(y.get(), ddot.get()); - GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value * 2); + GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value * 2); } @@ -1077,7 +1569,7 @@ TEST_F(Dense, ComputeConjDotWithPreallocatedTmpIsEquivalentToRef) x->compute_conj_dot(y.get(), dot_expected.get()); dx->compute_conj_dot(y.get(), ddot.get(), tmp); - GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value); + GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value); } @@ -1094,7 +1586,7 @@ TEST_F(Dense, ComputeConjDotWithTmpIsEquivalentToRef) x->compute_conj_dot(y.get(), dot_expected.get()); dx->compute_conj_dot(y.get(), ddot.get(), tmp); - GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value); + GKO_ASSERT_MTX_NEAR(ddot, dot_expected, r::value); } @@ -1110,7 +1602,7 @@ TEST_F(Dense, ComputeNorm1IsEquivalentToRef) x->compute_norm1(norm_expected.get()); dx->compute_norm1(dnorm.get()); - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); + GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); } @@ -1127,7 +1619,7 @@ TEST_F(Dense, ComputeNorm1WithPreallocatedTmpIsEquivalentToRef) x->compute_norm1(norm_expected.get()); dx->compute_norm1(dnorm.get(), tmp); - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); + GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); } @@ -1144,7 +1636,7 @@ TEST_F(Dense, ComputeNorm1WithTmpIsEquivalentToRef) x->compute_norm1(norm_expected.get()); dx->compute_norm1(dnorm.get(), tmp); - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); + GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); } @@ -1160,7 +1652,7 @@ TEST_F(Dense, ComputeNorm2IsEquivalentToRef) x->compute_norm1(norm_expected.get()); dx->compute_norm1(dnorm.get()); - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); + GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); } @@ -1177,7 +1669,7 @@ TEST_F(Dense, ComputeNorm2WithPreallocatedTmpIsEquivalentToRef) x->compute_norm1(norm_expected.get()); dx->compute_norm1(dnorm.get(), tmp); - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); + GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); } @@ -1194,7 +1686,7 @@ TEST_F(Dense, ComputeNorm2WithTmpIsEquivalentToRef) x->compute_norm1(norm_expected.get()); dx->compute_norm1(dnorm.get(), tmp); - GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); + GKO_ASSERT_MTX_NEAR(norm_expected, dnorm, r::value); } @@ -1205,7 +1697,7 @@ TEST_F(Dense, InplaceAbsoluteMatrixIsEquivalentToRef) x->compute_absolute_inplace(); dx->compute_absolute_inplace(); - GKO_ASSERT_MTX_NEAR(x, dx, r::value); + GKO_ASSERT_MTX_NEAR(x, dx, r::value); } @@ -1216,7 +1708,7 @@ TEST_F(Dense, OutplaceAbsoluteMatrixIsEquivalentToRef) auto abs_x = x->compute_absolute(); auto dabs_x = dx->compute_absolute(); - GKO_ASSERT_MTX_NEAR(abs_x, dabs_x, r::value); + GKO_ASSERT_MTX_NEAR(abs_x, dabs_x, r::value); } @@ -1230,7 +1722,7 @@ TEST_F(Dense, OutplaceAbsoluteMatrixIntoDenseCrossExecutor) x->compute_absolute(abs_x.get()); dx->compute_absolute(dabs_x.get()); - GKO_ASSERT_MTX_NEAR(abs_x, dabs_x, r::value); + GKO_ASSERT_MTX_NEAR(abs_x, dabs_x, r::value); } @@ -1317,7 +1809,7 @@ TEST_F(Dense, AddScaledIdentityToNonSquare) x->add_scaled_identity(alpha.get(), beta.get()); dx->add_scaled_identity(dalpha.get(), dbeta.get()); - GKO_ASSERT_MTX_NEAR(x, dx, r::value); + GKO_ASSERT_MTX_NEAR(x, dx, r::value); } @@ -1328,8 +1820,5 @@ TEST_F(Dense, AddScaledIdentityToNonSquareOnDifferentExecutor) x->add_scaled_identity(alpha.get(), beta.get()); dx->add_scaled_identity(alpha.get(), beta.get()); - GKO_ASSERT_MTX_NEAR(x, dx, r::value); + GKO_ASSERT_MTX_NEAR(x, dx, r::value); } - - -} // namespace diff --git a/omp/test/matrix/diagonal_kernels.cpp b/test/matrix/diagonal_kernels.cpp similarity index 80% rename from omp/test/matrix/diagonal_kernels.cpp rename to test/matrix/diagonal_kernels.cpp index cb259934bd7..6d7abddf6a7 100644 --- a/omp/test/matrix/diagonal_kernels.cpp +++ b/test/matrix/diagonal_kernels.cpp @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/matrix/diagonal_kernels.hpp" #include @@ -46,17 +46,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include "core/matrix/diagonal_kernels.hpp" #include "core/test/utils.hpp" +#include "test/utils/executor.hpp" -namespace { - - -class Diagonal : public ::testing::Test { +class Diagonal : public CommonTestFixture { protected: - using ValueType = double; - using ComplexValueType = std::complex; + using ValueType = value_type; + using ComplexValueType = std::complex; using Csr = gko::matrix::Csr; using Diag = gko::matrix::Diagonal; using Dense = gko::matrix::Dense; @@ -64,26 +61,13 @@ class Diagonal : public ::testing::Test { Diagonal() #ifdef GINKGO_FAST_TESTS - : mtx_size(152, 185), + : mtx_size(152, 231), #else : mtx_size(532, 231), #endif rand_engine(42) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - template std::unique_ptr gen_mtx(int num_rows, int num_cols, int min_nnz_row) @@ -91,14 +75,14 @@ class Diagonal : public ::testing::Test { return gko::test::generate_random_matrix( num_rows, num_cols, std::uniform_int_distribution<>(min_nnz_row, num_cols), - std::normal_distribution<>(0.0, 1.0), rand_engine, ref); + std::normal_distribution(0.0, 1.0), rand_engine, ref); } std::unique_ptr gen_diag(int size) { auto diag = Diag::create(ref, size); auto vals = diag->get_values(); - auto value_dist = std::normal_distribution<>(0.0, 1.0); + auto value_dist = std::normal_distribution(0.0, 1.0); for (int i = 0; i < size; i++) { vals[i] = gko::test::detail::get_rand_value(value_dist, rand_engine); @@ -110,7 +94,7 @@ class Diagonal : public ::testing::Test { { auto cdiag = ComplexDiag::create(ref, size); auto vals = cdiag->get_values(); - auto value_dist = std::normal_distribution<>(0.0, 1.0); + auto value_dist = std::normal_distribution(0.0, 1.0); for (int i = 0; i < size; i++) { vals[i] = ComplexValueType{ gko::test::detail::get_rand_value( @@ -122,34 +106,31 @@ class Diagonal : public ::testing::Test { void set_up_apply_data() { diag = gen_diag(mtx_size[0]); - ddiag = gko::clone(omp, diag); - dense1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[0]); - dense2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[1]); - denseexpected1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[0]); - denseexpected2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[1]); - ddense1 = gko::clone(omp, dense1); - ddense2 = gko::clone(omp, dense2); - denseresult1 = gko::clone(omp, denseexpected1); - denseresult2 = gko::clone(omp, denseexpected2); + ddiag = gko::clone(exec, diag); + dense1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[1]); + dense2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[0]); + denseexpected1 = gen_mtx(mtx_size[0], mtx_size[1], mtx_size[1]); + denseexpected2 = gen_mtx(mtx_size[1], mtx_size[0], mtx_size[0]); + ddense1 = gko::clone(exec, dense1); + ddense2 = gko::clone(exec, dense2); + denseresult1 = gko::clone(exec, denseexpected1); + denseresult2 = gko::clone(exec, denseexpected2); csr1 = gen_mtx(mtx_size[0], mtx_size[1], 1); csr2 = gen_mtx(mtx_size[1], mtx_size[0], 1); csrexpected1 = gen_mtx(mtx_size[0], mtx_size[1], 1); csrexpected2 = gen_mtx(mtx_size[1], mtx_size[0], 1); - dcsr1 = gko::clone(omp, csr1); - dcsr2 = gko::clone(omp, csr2); - csrresult1 = gko::clone(omp, csrexpected1); - csrresult2 = gko::clone(omp, csrexpected2); + dcsr1 = gko::clone(exec, csr1); + dcsr2 = gko::clone(exec, csr2); + csrresult1 = gko::clone(exec, csrexpected1); + csrresult2 = gko::clone(exec, csrexpected2); } void set_up_complex_data() { cdiag = gen_cdiag(mtx_size[0]); - dcdiag = gko::clone(omp, cdiag); + dcdiag = gko::clone(exec, cdiag); } - std::shared_ptr ref; - std::shared_ptr omp; - const gko::dim<2> mtx_size; std::default_random_engine rand_engine; @@ -184,7 +165,7 @@ TEST_F(Diagonal, ApplyToDenseIsEquivalentToRef) diag->apply(dense1.get(), denseexpected1.get()); ddiag->apply(ddense1.get(), denseresult1.get()); - GKO_ASSERT_MTX_NEAR(denseexpected1, denseresult1, 1e-14); + GKO_ASSERT_MTX_NEAR(denseexpected1, denseresult1, r::value); } @@ -195,7 +176,7 @@ TEST_F(Diagonal, RightApplyToDenseIsEquivalentToRef) diag->rapply(dense2.get(), denseexpected2.get()); ddiag->rapply(ddense2.get(), denseresult2.get()); - GKO_ASSERT_MTX_NEAR(denseexpected2, denseresult2, 1e-14); + GKO_ASSERT_MTX_NEAR(denseexpected2, denseresult2, r::value); } @@ -206,7 +187,7 @@ TEST_F(Diagonal, ApplyToCsrIsEquivalentToRef) diag->apply(csr1.get(), csrexpected1.get()); ddiag->apply(dcsr1.get(), csrresult1.get()); - GKO_ASSERT_MTX_NEAR(csrexpected1, csrresult1, 1e-14); + GKO_ASSERT_MTX_NEAR(csrexpected1, csrresult1, r::value); } @@ -217,7 +198,7 @@ TEST_F(Diagonal, RightApplyToCsrIsEquivalentToRef) diag->rapply(csr2.get(), csrexpected2.get()); ddiag->rapply(dcsr2.get(), csrresult2.get()); - GKO_ASSERT_MTX_NEAR(csrexpected2, csrresult2, 1e-14); + GKO_ASSERT_MTX_NEAR(csrexpected2, csrresult2, r::value); } @@ -252,7 +233,7 @@ TEST_F(Diagonal, InplaceAbsoluteMatrixIsEquivalentToRef) diag->compute_absolute_inplace(); ddiag->compute_absolute_inplace(); - GKO_ASSERT_MTX_NEAR(diag, ddiag, 1e-14); + GKO_ASSERT_MTX_NEAR(diag, ddiag, r::value); } @@ -263,8 +244,5 @@ TEST_F(Diagonal, OutplaceAbsoluteMatrixIsEquivalentToRef) auto abs_diag = diag->compute_absolute(); auto dabs_diag = ddiag->compute_absolute(); - GKO_ASSERT_MTX_NEAR(abs_diag, dabs_diag, 1e-14); + GKO_ASSERT_MTX_NEAR(abs_diag, dabs_diag, r::value); } - - -} // namespace diff --git a/dpcpp/test/matrix/ell_kernels.cpp b/test/matrix/ell_kernels.cpp similarity index 81% rename from dpcpp/test/matrix/ell_kernels.cpp rename to test/matrix/ell_kernels.cpp index 766ddb97e4e..df561538bf5 100644 --- a/dpcpp/test/matrix/ell_kernels.cpp +++ b/test/matrix/ell_kernels.cpp @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/matrix/ell_kernels.hpp" #include @@ -45,46 +45,24 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include -#include "core/matrix/ell_kernels.hpp" #include "core/test/utils.hpp" -#include "dpcpp/test/utils.hpp" - - -namespace { +#include "test/utils/executor.hpp" -class Ell : public ::testing::Test { +class Ell : public CommonTestFixture { protected: -#if GINKGO_DPCPP_SINGLE_MODE - using vtype = float; -#else - using vtype = double; -#endif // GINKGO_DPCPP_SINGLE_MODE - using Mtx = gko::matrix::Ell; - using Vec = gko::matrix::Dense; + using Mtx = gko::matrix::Ell; + using Vec = gko::matrix::Dense; using Vec2 = gko::matrix::Dense; - using ComplexVec = gko::matrix::Dense>; + using ComplexVec = gko::matrix::Dense>; Ell() : rand_engine(42), size{532, 231}, num_els_rowwise{300}, ell_stride{600} {} - void SetUp() - { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } - } - template std::unique_ptr gen_mtx(int num_rows, int num_cols) { @@ -110,20 +88,17 @@ class Ell : public ::testing::Test { alpha2 = gko::initialize({2.0}, ref); beta = gko::initialize({-1.0}, ref); beta2 = gko::initialize({-1.0}, ref); - dmtx = gko::clone(dpcpp, mtx); - dresult = gko::clone(dpcpp, expected); - dresult2 = gko::clone(dpcpp, expected2); - dy = gko::clone(dpcpp, y); - dy2 = gko::clone(dpcpp, y2); - dalpha = gko::clone(dpcpp, alpha); - dalpha2 = gko::clone(dpcpp, alpha2); - dbeta = gko::clone(dpcpp, beta); - dbeta2 = gko::clone(dpcpp, beta2); + dmtx = gko::clone(exec, mtx); + dresult = gko::clone(exec, expected); + dresult2 = gko::clone(exec, expected2); + dy = gko::clone(exec, y); + dy2 = gko::clone(exec, y2); + dalpha = gko::clone(exec, alpha); + dalpha2 = gko::clone(exec, alpha2); + dbeta = gko::clone(exec, beta); + dbeta2 = gko::clone(exec, beta2); } - std::shared_ptr ref; - std::shared_ptr dpcpp; - std::default_random_engine rand_engine; gko::dim<2> size; gko::size_type num_els_rowwise; @@ -158,7 +133,7 @@ TEST_F(Ell, SimpleApplyIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -205,7 +180,7 @@ TEST_F(Ell, AdvancedApplyIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -252,7 +227,7 @@ TEST_F(Ell, SimpleApplyWithStrideIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -298,7 +273,7 @@ TEST_F(Ell, AdvancedApplyWithStrideIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -345,14 +320,14 @@ TEST_F(Ell, SimpleApplyWithStrideToDenseMatrixIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef1) { SKIP_IF_SINGLE_MODE; - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); + set_up_apply_data(size[0], size[1], 4, num_els_rowwise, ell_stride); mtx->apply(y2.get(), expected2.get()); dmtx->apply(dy2.get(), dresult2.get()); @@ -364,7 +339,7 @@ TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef1) TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef2) { SKIP_IF_SINGLE_MODE; - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); + set_up_apply_data(size[0], size[1], 5, num_els_rowwise, ell_stride); mtx->apply(y2.get(), expected.get()); dmtx->apply(dy2.get(), dresult.get()); @@ -376,7 +351,7 @@ TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef2) TEST_F(Ell, MixedSimpleApplyWithStrideToDenseMatrixIsEquivalentToRef3) { SKIP_IF_SINGLE_MODE; - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); + set_up_apply_data(size[0], size[1], 6, num_els_rowwise, ell_stride); mtx->apply(y.get(), expected2.get()); dmtx->apply(dy.get(), dresult2.get()); @@ -392,14 +367,14 @@ TEST_F(Ell, AdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef1) { SKIP_IF_SINGLE_MODE; - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); + set_up_apply_data(size[0], size[1], 4, num_els_rowwise, ell_stride); mtx->apply(alpha2.get(), y2.get(), beta2.get(), expected2.get()); dmtx->apply(dalpha2.get(), dy2.get(), dbeta2.get(), dresult2.get()); @@ -411,7 +386,7 @@ TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef1) TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef2) { SKIP_IF_SINGLE_MODE; - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); + set_up_apply_data(size[0], size[1], 5, num_els_rowwise, ell_stride); mtx->apply(alpha2.get(), y2.get(), beta.get(), expected.get()); dmtx->apply(dalpha2.get(), dy2.get(), dbeta.get(), dresult.get()); @@ -423,7 +398,7 @@ TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef2) TEST_F(Ell, MixedAdvancedApplyWithStrideToDenseMatrixIsEquivalentToRef3) { SKIP_IF_SINGLE_MODE; - set_up_apply_data(size[0], size[1], 3, num_els_rowwise, ell_stride); + set_up_apply_data(size[0], size[1], 6, num_els_rowwise, ell_stride); mtx->apply(alpha.get(), y.get(), beta2.get(), expected2.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta2.get(), dresult2.get()); @@ -439,7 +414,7 @@ TEST_F(Ell, SimpleApplyByAtomicIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value * 10); } @@ -450,7 +425,7 @@ TEST_F(Ell, AdvancedByAtomicApplyIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value * 10); } @@ -461,7 +436,7 @@ TEST_F(Ell, SimpleApplyByAtomicToDenseMatrixIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value * 10); } @@ -472,7 +447,7 @@ TEST_F(Ell, AdvancedByAtomicToDenseMatrixApplyIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value * 10); } @@ -483,7 +458,7 @@ TEST_F(Ell, SimpleApplyOnSmallMatrixIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 5 * r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, 5 * r::value); } @@ -494,7 +469,7 @@ TEST_F(Ell, AdvancedApplyOnSmallMatrixToDenseMatrixIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -505,7 +480,7 @@ TEST_F(Ell, SimpleApplyOnSmallMatrixToDenseMatrixIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -516,7 +491,7 @@ TEST_F(Ell, AdvancedApplyOnSmallMatrixIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, r::value * 5); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value * 5); } @@ -524,14 +499,14 @@ TEST_F(Ell, ApplyToComplexIsEquivalentToRef) { set_up_apply_data(); auto complex_b = gen_mtx(size[1], 3); - auto dcomplex_b = gko::clone(dpcpp, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(size[0], 3); - auto dcomplex_x = gko::clone(dpcpp, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(complex_b.get(), complex_x.get()); dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } @@ -539,14 +514,14 @@ TEST_F(Ell, AdvancedApplyToComplexIsEquivalentToRef) { set_up_apply_data(); auto complex_b = gen_mtx(size[1], 3); - auto dcomplex_b = gko::clone(dpcpp, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(size[0], 3); - auto dcomplex_x = gko::clone(dpcpp, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } @@ -554,8 +529,8 @@ TEST_F(Ell, ConvertToDenseIsEquivalentToRef) { set_up_apply_data(); - auto dense_mtx = gko::matrix::Dense::create(ref); - auto ddense_mtx = gko::matrix::Dense::create(dpcpp); + auto dense_mtx = gko::matrix::Dense::create(ref); + auto ddense_mtx = gko::matrix::Dense::create(exec); mtx->convert_to(dense_mtx.get()); dmtx->convert_to(ddense_mtx.get()); @@ -568,8 +543,8 @@ TEST_F(Ell, ConvertToCsrIsEquivalentToRef) { set_up_apply_data(); - auto csr_mtx = gko::matrix::Csr::create(ref); - auto dcsr_mtx = gko::matrix::Csr::create(dpcpp); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); mtx->convert_to(csr_mtx.get()); dmtx->convert_to(dcsr_mtx.get()); @@ -582,12 +557,12 @@ TEST_F(Ell, CalculateNNZPerRowIsEquivalentToRef) { set_up_apply_data(); gko::array nnz_per_row{ref, mtx->get_size()[0]}; - gko::array dnnz_per_row{dpcpp, dmtx->get_size()[0]}; + gko::array dnnz_per_row{exec, dmtx->get_size()[0]}; gko::kernels::reference::ell::count_nonzeros_per_row( ref, mtx.get(), nnz_per_row.get_data()); - gko::kernels::dpcpp::ell::count_nonzeros_per_row(dpcpp, dmtx.get(), - dnnz_per_row.get_data()); + gko::kernels::EXEC_NAMESPACE::ell::count_nonzeros_per_row( + exec, dmtx.get(), dnnz_per_row.get_data()); GKO_ASSERT_ARRAY_EQ(nnz_per_row, dnnz_per_row); } @@ -611,7 +586,7 @@ TEST_F(Ell, InplaceAbsoluteMatrixIsEquivalentToRef) mtx->compute_absolute_inplace(); dmtx->compute_absolute_inplace(); - GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); + GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); } @@ -622,8 +597,5 @@ TEST_F(Ell, OutplaceAbsoluteMatrixIsEquivalentToRef) auto abs_mtx = mtx->compute_absolute(); auto dabs_mtx = dmtx->compute_absolute(); - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); + GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); } - - -} // namespace diff --git a/test/matrix/fbcsr_kernels.cpp b/test/matrix/fbcsr_kernels.cpp new file mode 100644 index 00000000000..fda4214dff2 --- /dev/null +++ b/test/matrix/fbcsr_kernels.cpp @@ -0,0 +1,320 @@ +/************************************************************* +Copyright (c) 2017-2022, the Ginkgo authors +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*************************************************************/ + +#include "core/matrix/fbcsr_kernels.hpp" + + +#include + + +#include + + +#include +#include + + +#include "core/test/matrix/fbcsr_sample.hpp" +#include "core/test/utils.hpp" +#include "core/test/utils/fb_matrix_generator.hpp" +#include "test/utils/executor.hpp" + + +template +class Fbcsr : public CommonTestFixture { +protected: + using value_type = T; + using real_type = gko::remove_complex; + using Mtx = gko::matrix::Fbcsr; + using Dense = gko::matrix::Dense; + + Fbcsr() : distb(), engine(42) + { + const index_type rand_brows = 100; + const index_type rand_bcols = 70; + const int block_size = 3; + rsorted = gko::test::generate_random_fbcsr( + ref, rand_brows, rand_bcols, block_size, false, false, + std::default_random_engine(43)); + } + + std::unique_ptr rsorted; + + std::normal_distribution> distb; + std::default_random_engine engine; + + value_type get_random_value() + { + return gko::test::detail::get_rand_value(distb, engine); + } + + void generate_sin(Dense* const x) + { + value_type* const xarr = x->get_values(); + for (index_type i = 0; i < x->get_size()[0] * x->get_size()[1]; i++) { + xarr[i] = + static_cast(2.0) * + std::sin(static_cast(i / 2.0) + get_random_value()); + } + } +}; + +#ifdef GKO_COMPILING_HIP +TYPED_TEST_SUITE(Fbcsr, gko::test::RealValueTypes, TypenameNameGenerator); +#else +TYPED_TEST_SUITE(Fbcsr, gko::test::ValueTypes, TypenameNameGenerator); +#endif + +TYPED_TEST(Fbcsr, CanWriteFromMatrixOnDevice) +{ + using Mtx = typename TestFixture::Mtx; + using value_type = typename Mtx::value_type; + using index_type = typename Mtx::index_type; + using MatData = gko::matrix_data; + gko::testing::FbcsrSample sample(this->ref); + auto mat = sample.generate_fbcsr(); + auto dmat = gko::clone(this->exec, mat); + MatData refdata; + MatData devdata; + + mat->write(refdata); + dmat->write(devdata); + + ASSERT_TRUE(refdata.nonzeros == devdata.nonzeros); +} + + +TYPED_TEST(Fbcsr, TransposeIsEquivalentToRefSortedBS3) +{ + using Mtx = typename TestFixture::Mtx; + using value_type = typename Mtx::value_type; + using index_type = typename Mtx::index_type; + auto drand = gko::clone(this->exec, this->rsorted); + + auto trans = gko::as(this->rsorted->transpose()); + auto dtrans = gko::as(drand->transpose()); + + GKO_ASSERT_MTX_EQ_SPARSITY(trans, dtrans); + GKO_ASSERT_MTX_NEAR(trans, dtrans, 0.0); +} + + +TYPED_TEST(Fbcsr, TransposeIsEquivalentToRefSortedBS7) +{ + using Mtx = typename TestFixture::Mtx; + using value_type = typename Mtx::value_type; + using index_type = typename Mtx::index_type; + auto drand = Mtx::create(this->exec); + const index_type rand_brows = 50; + const index_type rand_bcols = 40; + const int block_size = 7; + auto rsorted2 = gko::test::generate_random_fbcsr( + this->ref, rand_brows, rand_bcols, block_size, false, false, + std::default_random_engine(43)); + drand->copy_from(gko::lend(rsorted2)); + + auto trans = gko::as(rsorted2->transpose()); + auto dtrans = gko::as(drand->transpose()); + + GKO_ASSERT_MTX_EQ_SPARSITY(trans, dtrans); + GKO_ASSERT_MTX_NEAR(trans, dtrans, 0.0); +} + + +TYPED_TEST(Fbcsr, SpmvIsEquivalentToRefSorted) +{ + using Mtx = typename TestFixture::Mtx; + using Dense = typename TestFixture::Dense; + using value_type = typename Mtx::value_type; + auto drand = gko::clone(this->exec, this->rsorted); + auto x = + Dense::create(this->ref, gko::dim<2>(this->rsorted->get_size()[1], 1)); + this->generate_sin(x.get()); + auto dx = gko::clone(this->exec, x); + auto prod = + Dense::create(this->ref, gko::dim<2>(this->rsorted->get_size()[0], 1)); + auto dprod = Dense::create(this->exec, prod->get_size()); + + drand->apply(dx.get(), dprod.get()); + this->rsorted->apply(x.get(), prod.get()); + + const double tol = r::value; + GKO_ASSERT_MTX_NEAR(prod, dprod, 5 * tol); +} + + +TYPED_TEST(Fbcsr, SpmvMultiIsEquivalentToRefSorted) +{ + using Mtx = typename TestFixture::Mtx; + using Dense = typename TestFixture::Dense; + using value_type = typename Mtx::value_type; + auto drand = gko::clone(this->exec, this->rsorted); + auto x = + Dense::create(this->ref, gko::dim<2>(this->rsorted->get_size()[1], 3)); + this->generate_sin(x.get()); + auto dx = gko::clone(this->exec, x); + auto prod = + Dense::create(this->ref, gko::dim<2>(this->rsorted->get_size()[0], 3)); + auto dprod = Dense::create(this->exec, prod->get_size()); + + drand->apply(dx.get(), dprod.get()); + this->rsorted->apply(x.get(), prod.get()); + + const double tol = r::value; + GKO_ASSERT_MTX_NEAR(prod, dprod, 5 * tol); +} + + +TYPED_TEST(Fbcsr, AdvancedSpmvIsEquivalentToRefSorted) +{ + using Mtx = typename TestFixture::Mtx; + using Dense = typename TestFixture::Dense; + using value_type = typename TestFixture::value_type; + using real_type = typename TestFixture::real_type; + auto drand = gko::clone(this->exec, this->rsorted); + auto x = + Dense::create(this->ref, gko::dim<2>(this->rsorted->get_size()[1], 1)); + this->generate_sin(x.get()); + auto dx = gko::clone(this->exec, x); + auto prod = + Dense::create(this->ref, gko::dim<2>(this->rsorted->get_size()[0], 1)); + this->generate_sin(prod.get()); + auto dprod = gko::clone(this->exec, prod); + auto alpha = Dense::create(this->ref, gko::dim<2>(1, 1)); + alpha->at(0, 0) = static_cast(2.4) + this->get_random_value(); + auto beta = Dense::create(this->ref, gko::dim<2>(1, 1)); + beta->at(0, 0) = -1.2; + auto dalpha = gko::clone(this->exec, alpha); + auto dbeta = gko::clone(this->exec, beta); + + drand->apply(dalpha.get(), dx.get(), dbeta.get(), dprod.get()); + this->rsorted->apply(alpha.get(), x.get(), beta.get(), prod.get()); + + const double tol = r::value; + GKO_ASSERT_MTX_NEAR(prod, dprod, 5 * tol); +} + + +TYPED_TEST(Fbcsr, AdvancedSpmvMultiIsEquivalentToRefSorted) +{ + using Mtx = typename TestFixture::Mtx; + using Dense = typename TestFixture::Dense; + using value_type = typename TestFixture::value_type; + using real_type = typename TestFixture::real_type; + auto drand = gko::clone(this->exec, this->rsorted); + auto x = + Dense::create(this->ref, gko::dim<2>(this->rsorted->get_size()[1], 3)); + this->generate_sin(x.get()); + auto dx = gko::clone(this->exec, x); + auto prod = + Dense::create(this->ref, gko::dim<2>(this->rsorted->get_size()[0], 3)); + this->generate_sin(prod.get()); + auto dprod = gko::clone(this->exec, prod); + auto alpha = Dense::create(this->ref, gko::dim<2>(1, 1)); + alpha->at(0, 0) = static_cast(2.4) + this->get_random_value(); + auto beta = Dense::create(this->ref, gko::dim<2>(1, 1)); + beta->at(0, 0) = -1.2; + auto dalpha = gko::clone(this->exec, alpha); + auto dbeta = gko::clone(this->exec, beta); + + drand->apply(dalpha.get(), dx.get(), dbeta.get(), dprod.get()); + this->rsorted->apply(alpha.get(), x.get(), beta.get(), prod.get()); + + const double tol = r::value; + GKO_ASSERT_MTX_NEAR(prod, dprod, 5 * tol); +} + + +TYPED_TEST(Fbcsr, ConjTransposeIsEquivalentToRefSortedBS3) +{ + using Mtx = typename TestFixture::Mtx; + using value_type = typename Mtx::value_type; + using index_type = typename Mtx::index_type; + auto drand = gko::clone(this->exec, this->rsorted); + + auto trans = gko::as(this->rsorted->conj_transpose()); + auto dtrans = gko::as(drand->conj_transpose()); + + GKO_ASSERT_MTX_EQ_SPARSITY(trans, dtrans); + GKO_ASSERT_MTX_NEAR(trans, dtrans, 0.0); +} + + +TYPED_TEST(Fbcsr, RecognizeSortedMatrix) +{ + using Mtx = typename TestFixture::Mtx; + auto drand = gko::clone(this->exec, this->rsorted); + + ASSERT_TRUE(drand->is_sorted_by_column_index()); +} + + +TYPED_TEST(Fbcsr, RecognizeUnsortedMatrix) +{ + using Mtx = typename TestFixture::Mtx; + using index_type = typename TestFixture::index_type; + auto mat = this->rsorted->clone(); + index_type* const colinds = mat->get_col_idxs(); + std::swap(colinds[0], colinds[1]); + auto dunsrt = gko::clone(this->exec, mat); + + ASSERT_FALSE(dunsrt->is_sorted_by_column_index()); +} + + +TYPED_TEST(Fbcsr, InplaceAbsoluteMatrixIsEquivalentToRef) +{ + using Mtx = typename TestFixture::Mtx; + using value_type = typename Mtx::value_type; + auto rand = gko::clone(this->ref, this->rsorted); + auto drand = gko::clone(this->exec, this->rsorted); + + rand->compute_absolute_inplace(); + drand->compute_absolute_inplace(); + + const double tol = r::value; + GKO_ASSERT_MTX_NEAR(rand, drand, tol); +} + + +TYPED_TEST(Fbcsr, OutplaceAbsoluteMatrixIsEquivalentToRef) +{ + using Mtx = typename TestFixture::Mtx; + using value_type = typename Mtx::value_type; + auto drand = gko::clone(this->exec, this->rsorted); + + auto abs_mtx = this->rsorted->compute_absolute(); + auto dabs_mtx = drand->compute_absolute(); + + const double tol = r::value; + GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, tol); +} diff --git a/omp/test/matrix/fft_kernels.cpp b/test/matrix/fft_kernels.cpp similarity index 91% rename from omp/test/matrix/fft_kernels.cpp rename to test/matrix/fft_kernels.cpp index c5741f07629..18803c30142 100644 --- a/omp/test/matrix/fft_kernels.cpp +++ b/test/matrix/fft_kernels.cpp @@ -30,9 +30,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include - - #include @@ -43,16 +40,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "core/test/utils.hpp" - - -namespace { +#include "test/utils/executor.hpp" template -class Fft : public ::testing::Test { +class Fft : public CommonTestFixture { protected: using value_type = ValueType; using Vec = gko::matrix::Dense; @@ -61,9 +57,7 @@ class Fft : public ::testing::Test { using Mtx3 = gko::matrix::Fft3; Fft() - : ref{gko::ReferenceExecutor::create()}, - omp{gko::OmpExecutor::create()}, - rand_engine{9876567}, + : rand_engine(1364245), n1{16}, n2{32}, n3{64}, @@ -75,30 +69,28 @@ class Fft : public ::testing::Test { data = gko::test::generate_random_matrix( n, cols, std::uniform_int_distribution<>(1, cols), std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); - ddata = Vec::create(omp); + ddata = Vec::create(exec); ddata->copy_from(this->data.get()); data_strided = data->create_submatrix({0, n}, {0, subcols}); ddata_strided = ddata->create_submatrix({0, n}, {0, subcols}); out = data->clone(); dout = data->clone(); out_strided = Vec::create(ref, data_strided->get_size(), out_stride); - dout_strided = Vec::create(omp, data_strided->get_size(), out_stride); + dout_strided = Vec::create(exec, data_strided->get_size(), out_stride); fft = Mtx::create(ref, n); - dfft = Mtx::create(omp, n); + dfft = Mtx::create(exec, n); ifft = Mtx::create(ref, n, true); - difft = Mtx::create(omp, n, true); + difft = Mtx::create(exec, n, true); fft2 = Mtx2::create(ref, n1 * n2, n3); - dfft2 = Mtx2::create(omp, n1 * n2, n3); + dfft2 = Mtx2::create(exec, n1 * n2, n3); ifft2 = Mtx2::create(ref, n1 * n2, n3, true); - difft2 = Mtx2::create(omp, n1 * n2, n3, true); + difft2 = Mtx2::create(exec, n1 * n2, n3, true); fft3 = Mtx3::create(ref, n1, n2, n3); - dfft3 = Mtx3::create(omp, n1, n2, n3); + dfft3 = Mtx3::create(exec, n1, n2, n3); ifft3 = Mtx3::create(ref, n1, n2, n3, true); - difft3 = Mtx3::create(omp, n1, n2, n3, true); + difft3 = Mtx3::create(exec, n1, n2, n3, true); } - std::shared_ptr ref; - std::shared_ptr omp; std::default_random_engine rand_engine; size_t n1; size_t n2; @@ -263,6 +255,3 @@ TYPED_TEST(Fft, ApplyStrided3DInverseIsEqualToReference) GKO_ASSERT_MTX_NEAR(this->out_strided, this->dout_strided, r::value); } - - -} // namespace diff --git a/omp/test/matrix/hybrid_kernels.cpp b/test/matrix/hybrid_kernels.cpp similarity index 77% rename from omp/test/matrix/hybrid_kernels.cpp rename to test/matrix/hybrid_kernels.cpp index 57ba1d2485f..6549575cf39 100644 --- a/omp/test/matrix/hybrid_kernels.cpp +++ b/test/matrix/hybrid_kernels.cpp @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/matrix/hybrid_kernels.hpp" #include @@ -46,35 +46,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include "core/matrix/hybrid_kernels.hpp" #include "core/test/utils.hpp" +#include "test/utils/executor.hpp" -namespace { - - -class Hybrid : public ::testing::Test { +class Hybrid : public CommonTestFixture { protected: - using value_type = double; - using Mtx = gko::matrix::Hybrid<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; + using Mtx = gko::matrix::Hybrid; + using Vec = gko::matrix::Dense; + using ComplexVec = gko::matrix::Dense>; Hybrid() : rand_engine(42) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - template std::unique_ptr gen_mtx(int num_rows, int num_cols, int min_nnz_row) @@ -89,7 +72,7 @@ class Hybrid : public ::testing::Test { return gko::test::generate_random_matrix( num_rows, num_cols, std::uniform_int_distribution<>(min_nnz_row, max_nnz_row), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); + std::normal_distribution(-1.0, 1.0), rand_engine, ref); } void set_up_apply_data(int num_vectors = 1, @@ -102,18 +85,14 @@ class Hybrid : public ::testing::Test { y = gen_mtx(231, num_vectors, 1); alpha = gko::initialize({2.0}, ref); beta = gko::initialize({-1.0}, ref); - dmtx = Mtx::create(omp, strategy); + dmtx = Mtx::create(exec, strategy); dmtx->copy_from(mtx.get()); - dresult = gko::clone(omp, expected); - dy = gko::clone(omp, y); - dalpha = gko::clone(omp, alpha); - dbeta = gko::clone(omp, beta); + dresult = gko::clone(exec, expected); + dy = gko::clone(exec, y); + dalpha = gko::clone(exec, alpha); + dbeta = gko::clone(exec, beta); } - - std::shared_ptr ref; - std::shared_ptr omp; - std::default_random_engine rand_engine; std::unique_ptr mtx; @@ -137,9 +116,9 @@ TEST_F(Hybrid, SubMatrixExecutorAfterCopyIsEquivalentToExcutor) auto coo_mtx = dmtx->get_coo(); auto ell_mtx = dmtx->get_ell(); - ASSERT_EQ(coo_mtx->get_executor(), omp); - ASSERT_EQ(ell_mtx->get_executor(), omp); - ASSERT_EQ(dmtx->get_executor(), omp); + ASSERT_EQ(coo_mtx->get_executor(), exec); + ASSERT_EQ(ell_mtx->get_executor(), exec); + ASSERT_EQ(dmtx->get_executor(), exec); } @@ -150,7 +129,7 @@ TEST_F(Hybrid, SimpleApplyIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -161,7 +140,7 @@ TEST_F(Hybrid, AdvancedApplyIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -172,7 +151,7 @@ TEST_F(Hybrid, SimpleApplyToDenseMatrixIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -183,7 +162,7 @@ TEST_F(Hybrid, AdvancedApplyToDenseMatrixIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -191,14 +170,14 @@ TEST_F(Hybrid, ApplyToComplexIsEquivalentToRef) { set_up_apply_data(); auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(omp, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(omp, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(complex_b.get(), complex_x.get()); dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } @@ -206,14 +185,14 @@ TEST_F(Hybrid, AdvancedApplyToComplexIsEquivalentToRef) { set_up_apply_data(); auto complex_b = gen_mtx(231, 3, 1); - auto dcomplex_b = gko::clone(omp, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(532, 3, 1); - auto dcomplex_x = gko::clone(omp, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } @@ -223,10 +202,10 @@ TEST_F(Hybrid, ConvertEmptyCooToCsrIsEquivalentToRef) Mtx::create(ref, std::make_shared(4)); balanced_mtx->copy_from(gen_mtx(400, 200, 4, 4).get()); auto dbalanced_mtx = - Mtx::create(omp, std::make_shared(4)); + Mtx::create(exec, std::make_shared(4)); dbalanced_mtx->copy_from(balanced_mtx.get()); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(omp); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); balanced_mtx->convert_to(csr_mtx.get()); dbalanced_mtx->convert_to(dcsr_mtx.get()); @@ -247,9 +226,9 @@ TEST_F(Hybrid, ConvertWithEmptyFirstAndLastRowToCsrIsEquivalentToRef) } // now convert them to hybrid matrices auto balanced_mtx = gko::clone(ref, dense_mtx); - auto dbalanced_mtx = gko::clone(omp, balanced_mtx); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(omp); + auto dbalanced_mtx = gko::clone(exec, balanced_mtx); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); balanced_mtx->convert_to(csr_mtx.get()); dbalanced_mtx->convert_to(dcsr_mtx.get()); @@ -261,8 +240,8 @@ TEST_F(Hybrid, ConvertWithEmptyFirstAndLastRowToCsrIsEquivalentToRef) TEST_F(Hybrid, ConvertToCsrIsEquivalentToRef) { set_up_apply_data(1, std::make_shared(2)); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(omp); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); mtx->convert_to(csr_mtx.get()); dmtx->convert_to(dcsr_mtx.get()); @@ -274,13 +253,13 @@ TEST_F(Hybrid, ConvertToCsrIsEquivalentToRef) TEST_F(Hybrid, MoveToCsrIsEquivalentToRef) { set_up_apply_data(1, std::make_shared(2)); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(omp); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); mtx->move_to(csr_mtx.get()); dmtx->move_to(dcsr_mtx.get()); - GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 1e-14); + GKO_ASSERT_MTX_NEAR(csr_mtx.get(), dcsr_mtx.get(), 0); } @@ -302,7 +281,7 @@ TEST_F(Hybrid, InplaceAbsoluteMatrixIsEquivalentToRef) mtx->compute_absolute_inplace(); dmtx->compute_absolute_inplace(); - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); + GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); } @@ -317,11 +296,8 @@ TEST_F(Hybrid, OutplaceAbsoluteMatrixIsEquivalentToRef) auto dabs_strategy = gko::as(dabs_mtx->get_strategy()); - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); + GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); GKO_ASSERT_EQ(abs_strategy->get_num_columns(), dabs_strategy->get_num_columns()); GKO_ASSERT_EQ(abs_strategy->get_num_columns(), 2); } - - -} // namespace diff --git a/test/matrix/matrix.cpp b/test/matrix/matrix.cpp index 98047c29517..1926edaee63 100644 --- a/test/matrix/matrix.cpp +++ b/test/matrix/matrix.cpp @@ -56,11 +56,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -#if GINKGO_DPCPP_SINGLE_MODE +#if GINKGO_COMMON_SINGLE_MODE using matrix_value_type = float; #else using matrix_value_type = double; -#endif // GINKGO_DPCPP_SINGLE_MODE +#endif // GINKGO_COMMON_SINGLE_MODE template @@ -153,8 +153,9 @@ struct CsrWithDefaultStrategy : CsrBase { static void assert_empty_state(const matrix_type* mtx) { CsrBase::assert_empty_state(mtx); - ASSERT_EQ(typeid(*mtx->create_default()->get_strategy()), - typeid(*mtx->get_strategy())); + auto first_strategy = mtx->create_default()->get_strategy(); + auto second_strategy = mtx->get_strategy(); + ASSERT_EQ(typeid(*first_strategy), typeid(*second_strategy)); } }; @@ -581,7 +582,7 @@ struct test_pair { template -class Matrix : public ::testing::Test { +class Matrix : public CommonTestFixture { protected: using Config = T; using Mtx = typename T::matrix_type; @@ -593,19 +594,6 @@ class Matrix : public ::testing::Test { Matrix() : rand_engine(15) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - template gko::matrix_data gen_mtx_data(int num_rows, int num_cols, @@ -865,9 +853,6 @@ class Matrix : public ::testing::Test { } } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; }; @@ -927,7 +912,7 @@ TYPED_TEST(Matrix, AdvancedSpMVIsEquivalentToRef) } -#if !(GINKGO_DPCPP_SINGLE_MODE) +#if !(GINKGO_COMMON_SINGLE_MODE) TYPED_TEST(Matrix, MixedSpMVIsEquivalentToRef) { using MixedVec = typename TestFixture::MixedVec; diff --git a/hip/test/matrix/sellp_kernels.hip.cpp b/test/matrix/sellp_kernels.cpp similarity index 75% rename from hip/test/matrix/sellp_kernels.hip.cpp rename to test/matrix/sellp_kernels.cpp index 081798348b2..0838f9ccdff 100644 --- a/hip/test/matrix/sellp_kernels.hip.cpp +++ b/test/matrix/sellp_kernels.cpp @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/matrix/sellp_kernels.hpp" #include @@ -47,41 +47,24 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include "core/matrix/sellp_kernels.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { +#include "core/test/utils.hpp" +#include "test/utils/executor.hpp" -class Sellp : public ::testing::Test { +class Sellp : public CommonTestFixture { protected: - using Mtx = gko::matrix::Sellp<>; - using Vec = gko::matrix::Dense<>; - using ComplexVec = gko::matrix::Dense>; + using Mtx = gko::matrix::Sellp; + using Vec = gko::matrix::Dense; + using ComplexVec = gko::matrix::Dense>; Sellp() : rand_engine(42) {} - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - template std::unique_ptr gen_mtx(int num_rows, int num_cols) { return gko::test::generate_random_matrix( num_rows, num_cols, std::uniform_int_distribution<>(1, num_cols), - std::normal_distribution<>(-1.0, 1.0), rand_engine, ref); + std::normal_distribution(-1.0, 1.0), rand_engine, ref); } void set_up_apply_matrix( @@ -94,17 +77,14 @@ class Sellp : public ::testing::Test { y = gen_mtx(231, total_cols); alpha = gko::initialize({2.0}, ref); beta = gko::initialize({-1.0}, ref); - dmtx = gko::clone(hip, mtx); - dempty = Mtx::create(hip); - dresult = gko::clone(hip, expected); - dy = gko::clone(hip, y); - dalpha = gko::clone(hip, alpha); - dbeta = gko::clone(hip, beta); + dmtx = gko::clone(exec, mtx); + dempty = Mtx::create(exec); + dresult = gko::clone(exec, expected); + dy = gko::clone(exec, y); + dalpha = gko::clone(exec, alpha); + dbeta = gko::clone(exec, beta); } - std::shared_ptr ref; - std::shared_ptr hip; - std::default_random_engine rand_engine; std::unique_ptr mtx; @@ -130,7 +110,7 @@ TEST_F(Sellp, SimpleApplyIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -141,7 +121,7 @@ TEST_F(Sellp, AdvancedApplyIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -152,7 +132,7 @@ TEST_F(Sellp, SimpleApplyWithSliceSizeAndStrideFactorIsEquivalentToRef) mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -163,53 +143,53 @@ TEST_F(Sellp, AdvancedApplyWithSliceSizeAndStrideFActorIsEquivalentToRef) mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Sellp, SimpleApplyMultipleRHSIsEquivalentToRef) { - set_up_apply_matrix(64); + set_up_apply_matrix(3); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Sellp, AdvancedApplyMultipleRHSIsEquivalentToRef) { - set_up_apply_matrix(64); + set_up_apply_matrix(4); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Sellp, SimpleApplyMultipleRHSWithSliceSizeAndStrideFactorIsEquivalentToRef) { - set_up_apply_matrix(32, 2); + set_up_apply_matrix(5, 2); mtx->apply(y.get(), expected.get()); dmtx->apply(dy.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } TEST_F(Sellp, AdvancedApplyMultipleRHSWithSliceSizeAndStrideFActorIsEquivalentToRef) { - set_up_apply_matrix(32, 2); + set_up_apply_matrix(6, 2); mtx->apply(alpha.get(), y.get(), beta.get(), expected.get()); dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get()); - GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14); + GKO_ASSERT_MTX_NEAR(dresult, expected, r::value); } @@ -217,14 +197,14 @@ TEST_F(Sellp, ApplyToComplexIsEquivalentToRef) { set_up_apply_matrix(64); auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(hip, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(hip, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(complex_b.get(), complex_x.get()); dmtx->apply(dcomplex_b.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } @@ -232,22 +212,22 @@ TEST_F(Sellp, AdvancedApplyToComplexIsEquivalentToRef) { set_up_apply_matrix(64); auto complex_b = gen_mtx(231, 3); - auto dcomplex_b = gko::clone(hip, complex_b); + auto dcomplex_b = gko::clone(exec, complex_b); auto complex_x = gen_mtx(532, 3); - auto dcomplex_x = gko::clone(hip, complex_x); + auto dcomplex_x = gko::clone(exec, complex_x); mtx->apply(alpha.get(), complex_b.get(), beta.get(), complex_x.get()); dmtx->apply(dalpha.get(), dcomplex_b.get(), dbeta.get(), dcomplex_x.get()); - GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-14); + GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, r::value); } TEST_F(Sellp, ConvertToDenseIsEquivalentToRef) { set_up_apply_matrix(64); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(hip); + auto dense_mtx = gko::matrix::Dense::create(ref); + auto ddense_mtx = gko::matrix::Dense::create(exec); mtx->convert_to(dense_mtx.get()); dmtx->convert_to(ddense_mtx.get()); @@ -259,9 +239,8 @@ TEST_F(Sellp, ConvertToDenseIsEquivalentToRef) TEST_F(Sellp, ConvertToCsrIsEquivalentToRef) { set_up_apply_matrix(64); - - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(hip); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); mtx->convert_to(csr_mtx.get()); dmtx->convert_to(dcsr_mtx.get()); @@ -273,8 +252,8 @@ TEST_F(Sellp, ConvertToCsrIsEquivalentToRef) TEST_F(Sellp, ConvertEmptyToDenseIsEquivalentToRef) { set_up_apply_matrix(64); - auto dense_mtx = gko::matrix::Dense<>::create(ref); - auto ddense_mtx = gko::matrix::Dense<>::create(hip); + auto dense_mtx = gko::matrix::Dense::create(ref); + auto ddense_mtx = gko::matrix::Dense::create(exec); empty->convert_to(dense_mtx.get()); dempty->convert_to(ddense_mtx.get()); @@ -286,8 +265,8 @@ TEST_F(Sellp, ConvertEmptyToDenseIsEquivalentToRef) TEST_F(Sellp, ConvertEmptyToCsrIsEquivalentToRef) { set_up_apply_matrix(64); - auto csr_mtx = gko::matrix::Csr<>::create(ref); - auto dcsr_mtx = gko::matrix::Csr<>::create(hip); + auto csr_mtx = gko::matrix::Csr::create(ref); + auto dcsr_mtx = gko::matrix::Csr::create(exec); empty->convert_to(csr_mtx.get()); dempty->convert_to(dcsr_mtx.get()); @@ -325,7 +304,7 @@ TEST_F(Sellp, InplaceAbsoluteMatrixIsEquivalentToRef) mtx->compute_absolute_inplace(); dmtx->compute_absolute_inplace(); - GKO_ASSERT_MTX_NEAR(mtx, dmtx, 1e-14); + GKO_ASSERT_MTX_NEAR(mtx, dmtx, r::value); } @@ -336,8 +315,5 @@ TEST_F(Sellp, OutplaceAbsoluteMatrixIsEquivalentToRef) auto abs_mtx = mtx->compute_absolute(); auto dabs_mtx = dmtx->compute_absolute(); - GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, 1e-14); + GKO_ASSERT_MTX_NEAR(abs_mtx, dabs_mtx, r::value); } - - -} // namespace diff --git a/test/multigrid/amgx_pgm_kernels.cpp b/test/multigrid/amgx_pgm_kernels.cpp index df99ceb5223..459d862d9dc 100644 --- a/test/multigrid/amgx_pgm_kernels.cpp +++ b/test/multigrid/amgx_pgm_kernels.cpp @@ -60,17 +60,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class AmgxPgm : public ::testing::Test { +class AmgxPgm : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif // GINKGO_COMMON_SINGLE_MODE - using index_type = gko::int32; using Mtx = gko::matrix::Dense; using Csr = gko::matrix::Csr; using SparsityCsr = gko::matrix::SparsityCsr; @@ -79,19 +70,6 @@ class AmgxPgm : public ::testing::Test { AmgxPgm() : rand_engine(30) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - std::unique_ptr gen_mtx(int num_rows, int num_cols) { return gko::test::generate_random_matrix( @@ -175,9 +153,6 @@ class AmgxPgm : public ::testing::Test { d_system_mtx = gko::clone(exec, system_mtx); } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; gko::array agg; @@ -362,6 +337,3 @@ TEST_F(AmgxPgm, GenerateMgLevelIsEquivalentToRefOnUnsortedMatrix) r::value); GKO_ASSERT_ARRAY_EQ(d_row_gather_view, row_gather_view); } - - -} // namespace diff --git a/test/multigrid/fixed_coarsening_kernels.cpp b/test/multigrid/fixed_coarsening_kernels.cpp index dbb7ccf1da5..57820a15fea 100644 --- a/test/multigrid/fixed_coarsening_kernels.cpp +++ b/test/multigrid/fixed_coarsening_kernels.cpp @@ -60,35 +60,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class FixedCoarsening : public ::testing::Test { +class FixedCoarsening : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif // GINKGO_COMMON_SINGLE_MODE - using index_type = gko::int32; using Mtx = gko::matrix::Dense; using Csr = gko::matrix::Csr; - FixedCoarsening() : rand_engine(30) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - m = 597; - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } + FixedCoarsening() : rand_engine(30), m{597} {} gko::array gen_coarse_array(gko::size_type num, gko::size_type num_rows) @@ -131,9 +108,6 @@ class FixedCoarsening : public ::testing::Test { d_system_mtx = gko::clone(exec, system_mtx); } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; gko::array coarse_rows; @@ -204,6 +178,3 @@ TEST_F(FixedCoarsening, GenerateMgLevelIsEquivalentToRefOnUnsortedMatrix) gko::as(mg_level->get_coarse_op()), r::value); } - - -} // namespace diff --git a/test/preconditioner/CMakeLists.txt b/test/preconditioner/CMakeLists.txt new file mode 100644 index 00000000000..5cbac8d2e84 --- /dev/null +++ b/test/preconditioner/CMakeLists.txt @@ -0,0 +1,2 @@ +ginkgo_create_common_test(jacobi_kernels DISABLE_EXECUTORS dpcpp) +ginkgo_create_common_test(isai_kernels) diff --git a/hip/test/preconditioner/isai_kernels.hip.cpp b/test/preconditioner/isai_kernels.cpp similarity index 74% rename from hip/test/preconditioner/isai_kernels.hip.cpp rename to test/preconditioner/isai_kernels.cpp index 2b947d9c3d8..3a2d5bac872 100644 --- a/hip/test/preconditioner/isai_kernels.hip.cpp +++ b/test/preconditioner/isai_kernels.cpp @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/preconditioner/isai_kernels.hpp" #include @@ -41,35 +41,27 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include #include #include +#include +#include -#include "core/preconditioner/isai_kernels.hpp" -#include "hip/test/utils.hip.hpp" - - -namespace { +#include "core/test/utils.hpp" +#include "matrices/config.hpp" +#include "test/utils/executor.hpp" enum struct matrix_type { lower, upper, general, spd }; -class Isai : public ::testing::Test { +class Isai : public CommonTestFixture { protected: - using value_type = double; - using index_type = gko::int32; using Csr = gko::matrix::Csr; using Dense = gko::matrix::Dense; Isai() : rand_engine(42) {} - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - std::unique_ptr clone_allocations(const Csr* csr_mtx) { if (csr_mtx->get_executor() != ref) { @@ -112,8 +104,8 @@ class Isai : public ::testing::Test { } inverse = clone_allocations(mtx.get()); - d_mtx = gko::clone(hip, mtx); - d_inverse = gko::clone(hip, inverse); + d_mtx = gko::clone(exec, mtx); + d_inverse = gko::clone(exec, inverse); } void ensure_diagonal(Dense* mtx) @@ -123,10 +115,6 @@ class Isai : public ::testing::Test { } } - - std::shared_ptr ref; - std::shared_ptr hip; - std::default_random_engine rand_engine; std::unique_ptr mtx; @@ -137,19 +125,19 @@ class Isai : public ::testing::Test { }; -TEST_F(Isai, HipIsaiGenerateLinverseShortIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateLinverseShortIsEquivalentToRef) { initialize_data(matrix_type::lower, 536, 31); const auto num_rows = mtx->get_size()[0]; gko::array a1(ref, num_rows + 1); auto a2 = a1; - gko::array da1(hip, num_rows + 1); + gko::array da1(exec, num_rows + 1); auto da2 = da1; gko::kernels::reference::isai::generate_tri_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::hip::isai::generate_tri_inverse( - hip, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_tri_inverse( + exec, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), true); GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); @@ -160,19 +148,19 @@ TEST_F(Isai, HipIsaiGenerateLinverseShortIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateUinverseShortIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateUinverseShortIsEquivalentToRef) { initialize_data(matrix_type::upper, 615, 31); const auto num_rows = mtx->get_size()[0]; gko::array a1(ref, num_rows + 1); auto a2 = a1; - gko::array da1(hip, num_rows + 1); + gko::array da1(exec, num_rows + 1); auto da2 = da1; gko::kernels::reference::isai::generate_tri_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::hip::isai::generate_tri_inverse( - hip, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_tri_inverse( + exec, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), false); GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); @@ -183,19 +171,19 @@ TEST_F(Isai, HipIsaiGenerateUinverseShortIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateAinverseShortIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateAinverseShortIsEquivalentToRef) { initialize_data(matrix_type::general, 615, 15); const auto num_rows = mtx->get_size()[0]; gko::array a1(ref, num_rows + 1); auto a2 = a1; - gko::array da1(hip, num_rows + 1); + gko::array da1(exec, num_rows + 1); auto da2 = da1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::hip::isai::generate_general_inverse( - hip, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_general_inverse( + exec, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), false); GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); @@ -206,19 +194,19 @@ TEST_F(Isai, HipIsaiGenerateAinverseShortIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateSpdinverseShortIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateSpdinverseShortIsEquivalentToRef) { initialize_data(matrix_type::spd, 100, 15); const auto num_rows = mtx->get_size()[0]; gko::array a1(ref, num_rows + 1); auto a2 = a1; - gko::array da1(hip, num_rows + 1); + gko::array da1(exec, num_rows + 1); auto da2 = da1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::hip::isai::generate_general_inverse( - hip, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_general_inverse( + exec, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), true); GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); @@ -229,19 +217,19 @@ TEST_F(Isai, HipIsaiGenerateSpdinverseShortIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateLinverseLongIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateLinverseLongIsEquivalentToRef) { initialize_data(matrix_type::lower, 554, 64); const auto num_rows = mtx->get_size()[0]; gko::array a1(ref, num_rows + 1); auto a2 = a1; - gko::array da1(hip, num_rows + 1); + gko::array da1(exec, num_rows + 1); auto da2 = da1; gko::kernels::reference::isai::generate_tri_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::hip::isai::generate_tri_inverse( - hip, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_tri_inverse( + exec, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), true); GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); @@ -252,19 +240,19 @@ TEST_F(Isai, HipIsaiGenerateLinverseLongIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateUinverseLongIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateUinverseLongIsEquivalentToRef) { initialize_data(matrix_type::upper, 695, 64); const auto num_rows = mtx->get_size()[0]; gko::array a1(ref, num_rows + 1); auto a2 = a1; - gko::array da1(hip, num_rows + 1); + gko::array da1(exec, num_rows + 1); auto da2 = da1; gko::kernels::reference::isai::generate_tri_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::hip::isai::generate_tri_inverse( - hip, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_tri_inverse( + exec, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), false); GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); @@ -275,43 +263,43 @@ TEST_F(Isai, HipIsaiGenerateUinverseLongIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateAinverseLongIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateAinverseLongIsEquivalentToRef) { initialize_data(matrix_type::general, 695, 64); const auto num_rows = mtx->get_size()[0]; gko::array a1(ref, num_rows + 1); auto a2 = a1; - gko::array da1(hip, num_rows + 1); + gko::array da1(exec, num_rows + 1); auto da2 = da1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::kernels::hip::isai::generate_general_inverse( - hip, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_general_inverse( + exec, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), false); GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); - GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 10 * r::value); + GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 100 * r::value); GKO_ASSERT_ARRAY_EQ(a1, da1); GKO_ASSERT_ARRAY_EQ(a2, da2); ASSERT_GT(a1.get_const_data()[num_rows], 0); } -TEST_F(Isai, HipIsaiGenerateSpdinverseLongIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateSpdinverseLongIsEquivalentToRef) { initialize_data(matrix_type::spd, 100, 64); const auto num_rows = mtx->get_size()[0]; gko::array a1(ref, num_rows + 1); auto a2 = a1; - gko::array da1(hip, num_rows + 1); + gko::array da1(exec, num_rows + 1); auto da2 = da1; gko::kernels::reference::isai::generate_general_inverse( - ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::kernels::hip::isai::generate_general_inverse( - hip, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), - true); + ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); + gko::kernels::EXEC_NAMESPACE::isai::generate_general_inverse( + exec, d_mtx.get(), d_inverse.get(), da1.get_data(), da2.get_data(), + false); GKO_ASSERT_MTX_EQ_SPARSITY(inverse, d_inverse); GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 10 * r::value); @@ -321,7 +309,7 @@ TEST_F(Isai, HipIsaiGenerateSpdinverseLongIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateExcessLinverseLongIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateExcessLinverseLongIsEquivalentToRef) { initialize_data(matrix_type::lower, 518, 40); const auto num_rows = mtx->get_size()[0]; @@ -329,20 +317,20 @@ TEST_F(Isai, HipIsaiGenerateExcessLinverseLongIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_tri_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(hip, a1); - gko::array da2(hip, a2); + gko::array da1(exec, a1); + gko::array da2(exec, a2); auto e_dim = a1.get_data()[num_rows]; auto e_nnz = a2.get_data()[num_rows]; auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(hip, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(hip, gko::dim<2>(e_dim, 1)); + auto dexcess = Csr::create(exec, gko::dim<2>(e_dim, e_dim), e_nnz); + auto de_rhs = Dense::create(exec, gko::dim<2>(e_dim, 1)); gko::kernels::reference::isai::generate_excess_system( ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::hip::isai::generate_excess_system( - hip, d_mtx.get(), d_inverse.get(), da1.get_const_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_excess_system( + exec, d_mtx.get(), d_inverse.get(), da1.get_const_data(), da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); @@ -352,7 +340,7 @@ TEST_F(Isai, HipIsaiGenerateExcessLinverseLongIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateExcessUinverseLongIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateExcessUinverseLongIsEquivalentToRef) { initialize_data(matrix_type::upper, 673, 51); const auto num_rows = mtx->get_size()[0]; @@ -360,20 +348,20 @@ TEST_F(Isai, HipIsaiGenerateExcessUinverseLongIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_tri_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(hip, a1); - gko::array da2(hip, a2); + gko::array da1(exec, a1); + gko::array da2(exec, a2); auto e_dim = a1.get_data()[num_rows]; auto e_nnz = a2.get_data()[num_rows]; auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(hip, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(hip, gko::dim<2>(e_dim, 1)); + auto dexcess = Csr::create(exec, gko::dim<2>(e_dim, e_dim), e_nnz); + auto de_rhs = Dense::create(exec, gko::dim<2>(e_dim, 1)); gko::kernels::reference::isai::generate_excess_system( ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::hip::isai::generate_excess_system( - hip, d_mtx.get(), d_inverse.get(), da1.get_const_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_excess_system( + exec, d_mtx.get(), d_inverse.get(), da1.get_const_data(), da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); @@ -383,7 +371,7 @@ TEST_F(Isai, HipIsaiGenerateExcessUinverseLongIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateExcessAinverseLongIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateExcessAinverseLongIsEquivalentToRef) { initialize_data(matrix_type::general, 100, 51); const auto num_rows = mtx->get_size()[0]; @@ -391,20 +379,20 @@ TEST_F(Isai, HipIsaiGenerateExcessAinverseLongIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(hip, a1); - gko::array da2(hip, a2); + gko::array da1(exec, a1); + gko::array da2(exec, a2); auto e_dim = a1.get_data()[num_rows]; auto e_nnz = a2.get_data()[num_rows]; auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(hip, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(hip, gko::dim<2>(e_dim, 1)); + auto dexcess = Csr::create(exec, gko::dim<2>(e_dim, e_dim), e_nnz); + auto de_rhs = Dense::create(exec, gko::dim<2>(e_dim, 1)); gko::kernels::reference::isai::generate_excess_system( ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::hip::isai::generate_excess_system( - hip, d_mtx.get(), d_inverse.get(), da1.get_const_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_excess_system( + exec, d_mtx.get(), d_inverse.get(), da1.get_const_data(), da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); @@ -414,7 +402,7 @@ TEST_F(Isai, HipIsaiGenerateExcessAinverseLongIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGenerateExcessSpdinverseLongIsEquivalentToRef) +TEST_F(Isai, IsaiGenerateExcessSpdinverseLongIsEquivalentToRef) { initialize_data(matrix_type::spd, 100, 64); const auto num_rows = mtx->get_size()[0]; @@ -422,20 +410,20 @@ TEST_F(Isai, HipIsaiGenerateExcessSpdinverseLongIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(hip, a1); - gko::array da2(hip, a2); + gko::array da1(exec, a1); + gko::array da2(exec, a2); auto e_dim = a1.get_data()[num_rows]; auto e_nnz = a2.get_data()[num_rows]; auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(hip, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(hip, gko::dim<2>(e_dim, 1)); + auto dexcess = Csr::create(exec, gko::dim<2>(e_dim, e_dim), e_nnz); + auto de_rhs = Dense::create(exec, gko::dim<2>(e_dim, 1)); gko::kernels::reference::isai::generate_excess_system( ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), excess.get(), e_rhs.get(), 0, num_rows); - gko::kernels::hip::isai::generate_excess_system( - hip, d_mtx.get(), d_inverse.get(), da1.get_const_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_excess_system( + exec, d_mtx.get(), d_inverse.get(), da1.get_const_data(), da2.get_const_data(), dexcess.get(), de_rhs.get(), 0, num_rows); GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); @@ -445,7 +433,7 @@ TEST_F(Isai, HipIsaiGenerateExcessSpdinverseLongIsEquivalentToRef) } -TEST_F(Isai, HipIsaiGeneratePartialExcessIsEquivalentToRef) +TEST_F(Isai, IsaiGeneratePartialExcessIsEquivalentToRef) { initialize_data(matrix_type::general, 100, 64); const auto num_rows = mtx->get_size()[0]; @@ -453,20 +441,20 @@ TEST_F(Isai, HipIsaiGeneratePartialExcessIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(hip, a1); - gko::array da2(hip, a2); + gko::array da1(exec, a1); + gko::array da2(exec, a2); auto e_dim = a1.get_data()[10] - a1.get_data()[5]; auto e_nnz = a2.get_data()[10] - a2.get_data()[5]; auto excess = Csr::create(ref, gko::dim<2>(e_dim, e_dim), e_nnz); auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); - auto dexcess = Csr::create(hip, gko::dim<2>(e_dim, e_dim), e_nnz); - auto de_rhs = Dense::create(hip, gko::dim<2>(e_dim, 1)); + auto dexcess = Csr::create(exec, gko::dim<2>(e_dim, e_dim), e_nnz); + auto de_rhs = Dense::create(exec, gko::dim<2>(e_dim, 1)); gko::kernels::reference::isai::generate_excess_system( ref, mtx.get(), inverse.get(), a1.get_const_data(), a2.get_const_data(), excess.get(), e_rhs.get(), 5u, 10u); - gko::kernels::hip::isai::generate_excess_system( - hip, d_mtx.get(), d_inverse.get(), da1.get_const_data(), + gko::kernels::EXEC_NAMESPACE::isai::generate_excess_system( + exec, d_mtx.get(), d_inverse.get(), da1.get_const_data(), da2.get_const_data(), dexcess.get(), de_rhs.get(), 5u, 10u); GKO_ASSERT_MTX_EQ_SPARSITY(excess, dexcess); @@ -476,7 +464,7 @@ TEST_F(Isai, HipIsaiGeneratePartialExcessIsEquivalentToRef) } -TEST_F(Isai, HipIsaiScaleExcessSolutionIsEquivalentToRef) +TEST_F(Isai, IsaiScaleExcessSolutionIsEquivalentToRef) { initialize_data(matrix_type::spd, 100, 64); const auto num_rows = mtx->get_size()[0]; @@ -484,23 +472,23 @@ TEST_F(Isai, HipIsaiScaleExcessSolutionIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(hip, a1); + gko::array da1(exec, a1); auto e_dim = a1.get_data()[num_rows]; auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(hip, e_rhs); + auto de_rhs = gko::clone(exec, e_rhs); d_inverse->copy_from(lend(inverse)); gko::kernels::reference::isai::scale_excess_solution( ref, a1.get_const_data(), e_rhs.get(), 0, num_rows); - gko::kernels::hip::isai::scale_excess_solution(hip, da1.get_const_data(), - de_rhs.get(), 0, num_rows); + gko::kernels::EXEC_NAMESPACE::isai::scale_excess_solution( + exec, da1.get_const_data(), de_rhs.get(), 0, num_rows); GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); } -TEST_F(Isai, HipIsaiScalePartialExcessSolutionIsEquivalentToRef) +TEST_F(Isai, IsaiScalePartialExcessSolutionIsEquivalentToRef) { initialize_data(matrix_type::spd, 100, 64); const auto num_rows = mtx->get_size()[0]; @@ -508,22 +496,22 @@ TEST_F(Isai, HipIsaiScalePartialExcessSolutionIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(hip, a1); + gko::array da1(exec, a1); auto e_dim = a1.get_data()[10] - a1.get_data()[5]; auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(hip, e_rhs); + auto de_rhs = gko::clone(exec, e_rhs); gko::kernels::reference::isai::scale_excess_solution( ref, a1.get_const_data(), e_rhs.get(), 5u, 10u); - gko::kernels::hip::isai::scale_excess_solution(hip, da1.get_const_data(), - de_rhs.get(), 5u, 10u); + gko::kernels::EXEC_NAMESPACE::isai::scale_excess_solution( + exec, da1.get_const_data(), de_rhs.get(), 5u, 10u); GKO_ASSERT_MTX_NEAR(e_rhs, de_rhs, 0); } -TEST_F(Isai, HipIsaiScatterExcessSolutionLIsEquivalentToRef) +TEST_F(Isai, IsaiScatterExcessSolutionLIsEquivalentToRef) { initialize_data(matrix_type::lower, 572, 52); const auto num_rows = mtx->get_size()[0]; @@ -531,24 +519,24 @@ TEST_F(Isai, HipIsaiScatterExcessSolutionLIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_tri_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(hip, a1); + gko::array da1(exec, a1); auto e_dim = a1.get_data()[num_rows]; auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(hip, e_rhs); + auto de_rhs = gko::clone(exec, e_rhs); d_inverse->copy_from(lend(inverse)); gko::kernels::reference::isai::scatter_excess_solution( ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::hip::isai::scatter_excess_solution( - hip, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); + gko::kernels::EXEC_NAMESPACE::isai::scatter_excess_solution( + exec, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); ASSERT_GT(e_dim, 0); } -TEST_F(Isai, HipIsaiScatterExcessSolutionUIsEquivalentToRef) +TEST_F(Isai, IsaiScatterExcessSolutionUIsEquivalentToRef) { initialize_data(matrix_type::upper, 702, 45); const auto num_rows = mtx->get_size()[0]; @@ -556,25 +544,25 @@ TEST_F(Isai, HipIsaiScatterExcessSolutionUIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_tri_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(hip, a1); + gko::array da1(exec, a1); auto e_dim = a1.get_data()[num_rows]; auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(hip, e_rhs); + auto de_rhs = gko::clone(exec, e_rhs); // overwrite -1 values with inverse d_inverse->copy_from(lend(inverse)); gko::kernels::reference::isai::scatter_excess_solution( ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::hip::isai::scatter_excess_solution( - hip, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); + gko::kernels::EXEC_NAMESPACE::isai::scatter_excess_solution( + exec, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); ASSERT_GT(e_dim, 0); } -TEST_F(Isai, HipIsaiScatterExcessSolutionAIsEquivalentToRef) +TEST_F(Isai, IsaiScatterExcessSolutionAIsEquivalentToRef) { initialize_data(matrix_type::general, 702, 45); const auto num_rows = mtx->get_size()[0]; @@ -582,25 +570,25 @@ TEST_F(Isai, HipIsaiScatterExcessSolutionAIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), false); - gko::array da1(hip, a1); + gko::array da1(exec, a1); auto e_dim = a1.get_data()[num_rows]; auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(hip, e_rhs); + auto de_rhs = gko::clone(exec, e_rhs); // overwrite -1 values with inverse d_inverse->copy_from(lend(inverse)); gko::kernels::reference::isai::scatter_excess_solution( ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::hip::isai::scatter_excess_solution( - hip, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); + gko::kernels::EXEC_NAMESPACE::isai::scatter_excess_solution( + exec, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); ASSERT_GT(e_dim, 0); } -TEST_F(Isai, HipIsaiScatterExcessSolutionSpdIsEquivalentToRef) +TEST_F(Isai, IsaiScatterExcessSolutionSpdIsEquivalentToRef) { initialize_data(matrix_type::spd, 100, 64); const auto num_rows = mtx->get_size()[0]; @@ -608,25 +596,25 @@ TEST_F(Isai, HipIsaiScatterExcessSolutionSpdIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(hip, a1); + gko::array da1(exec, a1); auto e_dim = a1.get_data()[num_rows]; auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(hip, e_rhs); + auto de_rhs = gko::clone(exec, e_rhs); // overwrite -1 values with inverse d_inverse->copy_from(lend(inverse)); gko::kernels::reference::isai::scatter_excess_solution( ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 0, num_rows); - gko::kernels::hip::isai::scatter_excess_solution( - hip, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); + gko::kernels::EXEC_NAMESPACE::isai::scatter_excess_solution( + exec, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 0, num_rows); GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); ASSERT_GT(e_dim, 0); } -TEST_F(Isai, HipIsaiScatterPartialExcessSolutionIsEquivalentToRef) +TEST_F(Isai, IsaiScatterPartialExcessSolutionIsEquivalentToRef) { initialize_data(matrix_type::spd, 100, 64); const auto num_rows = mtx->get_size()[0]; @@ -634,22 +622,19 @@ TEST_F(Isai, HipIsaiScatterPartialExcessSolutionIsEquivalentToRef) auto a2 = a1; gko::kernels::reference::isai::generate_general_inverse( ref, mtx.get(), inverse.get(), a1.get_data(), a2.get_data(), true); - gko::array da1(hip, a1); + gko::array da1(exec, a1); auto e_dim = a1.get_data()[10] - a1.get_data()[5]; auto e_rhs = Dense::create(ref, gko::dim<2>(e_dim, 1)); std::fill_n(e_rhs->get_values(), e_dim, 123456); - auto de_rhs = gko::clone(hip, e_rhs); + auto de_rhs = gko::clone(exec, e_rhs); // overwrite -1 values with inverse d_inverse->copy_from(lend(inverse)); gko::kernels::reference::isai::scatter_excess_solution( ref, a1.get_const_data(), e_rhs.get(), inverse.get(), 5u, 10u); - gko::kernels::hip::isai::scatter_excess_solution( - hip, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 5u, 10u); + gko::kernels::EXEC_NAMESPACE::isai::scatter_excess_solution( + exec, da1.get_const_data(), de_rhs.get(), d_inverse.get(), 5u, 10u); GKO_ASSERT_MTX_NEAR(inverse, d_inverse, 0); ASSERT_GT(e_dim, 0); } - - -} // namespace diff --git a/hip/test/preconditioner/jacobi_kernels.cpp b/test/preconditioner/jacobi_kernels.cpp similarity index 81% rename from hip/test/preconditioner/jacobi_kernels.cpp rename to test/preconditioner/jacobi_kernels.cpp index 6cc265ae4f3..f5af5a55bbe 100644 --- a/hip/test/preconditioner/jacobi_kernels.cpp +++ b/test/preconditioner/jacobi_kernels.cpp @@ -30,9 +30,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include - - #include @@ -41,17 +38,16 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include +#include "core/test/utils.hpp" #include "core/test/utils/unsort_matrix.hpp" #include "core/utils/matrix_utils.hpp" -#include "hip/test/utils.hip.hpp" +#include "test/utils/executor.hpp" -namespace { - - -class Jacobi : public ::testing::Test { +class Jacobi : public CommonTestFixture { protected: using Bj = gko::preconditioner::Jacobi<>; using Mtx = gko::matrix::Csr<>; @@ -60,20 +56,6 @@ class Jacobi : public ::testing::Test { using value_type = typename Mtx::value_type; using index_type = typename Mtx::index_type; - void SetUp() - { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - ref = gko::ReferenceExecutor::create(); - hip = gko::HipExecutor::create(0, ref); - } - - void TearDown() - { - if (hip != nullptr) { - ASSERT_NO_THROW(hip->synchronize()); - } - } - void initialize_data( std::initializer_list block_pointers, std::initializer_list block_precisions, @@ -106,19 +88,24 @@ class Jacobi : public ::testing::Test { Bj::build() .with_max_block_size(max_block_size) .with_block_pointers(block_ptrs) - .with_max_block_stride(gko::uint32(hip->get_warp_size())) +#ifdef GKO_COMPILING_HIP + .with_max_block_stride(gko::uint32(exec->get_warp_size())) +#endif .with_skip_sorting(skip_sorting) .on(ref); d_bj_factory = Bj::build() .with_max_block_size(max_block_size) .with_block_pointers(block_ptrs) - .on(hip); + .with_skip_sorting(skip_sorting) + .on(exec); } else { bj_factory = Bj::build() .with_max_block_size(max_block_size) .with_block_pointers(block_ptrs) - .with_max_block_stride(gko::uint32(hip->get_warp_size())) +#ifdef GKO_COMPILING_HIP + .with_max_block_stride(gko::uint32(exec->get_warp_size())) +#endif .with_storage_optimization(block_prec) .with_accuracy(accuracy) .with_skip_sorting(skip_sorting) @@ -129,16 +116,16 @@ class Jacobi : public ::testing::Test { .with_storage_optimization(block_prec) .with_accuracy(accuracy) .with_skip_sorting(skip_sorting) - .on(hip); + .on(exec); } b = gko::test::generate_random_matrix( dim, num_rhs, std::uniform_int_distribution<>(num_rhs, num_rhs), std::normal_distribution<>(0.0, 1.0), engine, ref); - d_b = gko::clone(hip, b); + d_b = gko::clone(exec, b); x = gko::test::generate_random_matrix( dim, num_rhs, std::uniform_int_distribution<>(num_rhs, num_rhs), std::normal_distribution<>(0.0, 1.0), engine, ref); - d_x = gko::clone(hip, x); + d_x = gko::clone(exec, x); } const gko::precision_reduction dp{}; @@ -149,8 +136,6 @@ class Jacobi : public ::testing::Test { const gko::precision_reduction up{1, 1}; const gko::precision_reduction ap{gko::precision_reduction::autodetect()}; - std::shared_ptr ref; - std::shared_ptr hip; std::shared_ptr mtx; std::unique_ptr x; std::unique_ptr b; @@ -162,7 +147,7 @@ class Jacobi : public ::testing::Test { }; -TEST_F(Jacobi, HipFindNaturalBlocksEquivalentToRef) +TEST_F(Jacobi, FindNaturalBlocksEquivalentToRef) { /* example matrix: 1 1 @@ -182,14 +167,14 @@ TEST_F(Jacobi, HipFindNaturalBlocksEquivalentToRef) {3, 2, 1.0}}}); auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(hip)->generate(mtx); + auto d_bj = Bj::build().with_max_block_size(3u).on(exec)->generate(mtx); ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); // TODO: actually check if the results are the same } -TEST_F(Jacobi, HipExecutesSupervariableAgglomerationEquivalentToRef) +TEST_F(Jacobi, ExecutesSupervariableAgglomerationEquivalentToRef) { /* example matrix: 1 1 @@ -211,14 +196,14 @@ TEST_F(Jacobi, HipExecutesSupervariableAgglomerationEquivalentToRef) {4, 4, 1.0}}}); auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(hip)->generate(mtx); + auto d_bj = Bj::build().with_max_block_size(3u).on(exec)->generate(mtx); ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); // TODO: actually check if the results are the same } -TEST_F(Jacobi, HipFindNaturalBlocksInLargeMatrixEquivalentToRef) +TEST_F(Jacobi, FindNaturalBlocksInLargeMatrixEquivalentToRef) { /* example matrix: 1 1 @@ -238,15 +223,14 @@ TEST_F(Jacobi, HipFindNaturalBlocksInLargeMatrixEquivalentToRef) {1.0, 0.0, 1.0, 0.0, 0.0, 0.0}})); auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(hip)->generate(mtx); + auto d_bj = Bj::build().with_max_block_size(3u).on(exec)->generate(mtx); ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); // TODO: actually check if the results are the same } -TEST_F(Jacobi, - HipExecutesSupervariableAgglomerationInLargeMatrixEquivalentToRef) +TEST_F(Jacobi, ExecutesSupervariableAgglomerationInLargeMatrixEquivalentToRef) { /* example matrix: 1 1 @@ -264,7 +248,7 @@ TEST_F(Jacobi, {0.0, 0.0, 0.0, 0.0, 1.0}})); auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(hip)->generate(mtx); + auto d_bj = Bj::build().with_max_block_size(3u).on(exec)->generate(mtx); ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); // TODO: actually check if the results are the same @@ -272,7 +256,7 @@ TEST_F(Jacobi, TEST_F(Jacobi, - HipExecutesSupervarAgglomerationEquivalentToRefFor150NonzerowsPerRow) + ExecutesSupervarAgglomerationEquivalentToRefFor150NonzerowsPerRow) { /* example matrix duplicated 50 times: 1 1 1 @@ -292,14 +276,14 @@ TEST_F(Jacobi, auto bj = Bj::build().with_max_block_size(3u).on(ref)->generate(mtx); - auto d_bj = Bj::build().with_max_block_size(3u).on(hip)->generate(mtx); + auto d_bj = Bj::build().with_max_block_size(3u).on(exec)->generate(mtx); ASSERT_EQ(d_bj->get_num_blocks(), bj->get_num_blocks()); // TODO: actually check if the results are the same } -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithBlockSize32Sorted) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithBlockSize32Sorted) { initialize_data({0, 32, 64, 96, 128}, {}, {}, 32, 100, 110); @@ -310,9 +294,9 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithBlockSize32Sorted) } -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithBlockSize32Unsorted) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithBlockSize32Unsorted) { - std::default_random_engine engine(43); + std::default_random_engine engine(42); initialize_data({0, 32, 64, 96, 128}, {}, {}, 32, 100, 110, 1, 0.1, false); gko::test::unsort_matrix(mtx.get(), engine); @@ -323,9 +307,12 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithBlockSize32Unsorted) } -#if GINKGO_HIP_PLATFORM_HCC -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithBlockSize64) +#ifdef GKO_COMPILING_HIP +TEST_F(Jacobi, PreconditionerEquivalentToRefWithBlockSize64) { + if (exec->get_warp_size() == 32) { + GTEST_SKIP(); + } initialize_data({0, 64, 128, 192, 256}, {}, {}, 64, 100, 110); auto bj = bj_factory->generate(mtx); @@ -333,10 +320,10 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithBlockSize64) GKO_ASSERT_MTX_NEAR(gko::as(d_bj.get()), gko::as(bj.get()), 1e-13); } -#endif // GINKGO_HIP_PLATFORM_HCC +#endif // GKO_COMPILING_HIP -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithDifferentBlockSize) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithDifferentBlockSize) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 32, 97, 99); @@ -348,7 +335,7 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithDifferentBlockSize) } -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithMPW) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithMPW) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, 97, 99); @@ -360,7 +347,7 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithMPW) } -TEST_F(Jacobi, HipTransposedPreconditionerEquivalentToRefWithMPW) +TEST_F(Jacobi, TransposedPreconditionerEquivalentToRefWithMPW) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, 97, 99); @@ -374,7 +361,7 @@ TEST_F(Jacobi, HipTransposedPreconditionerEquivalentToRefWithMPW) } -TEST_F(Jacobi, HipConjTransposedPreconditionerEquivalentToRefWithMPW) +TEST_F(Jacobi, ConjTransposedPreconditionerEquivalentToRefWithMPW) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, 97, 99); @@ -388,7 +375,7 @@ TEST_F(Jacobi, HipConjTransposedPreconditionerEquivalentToRefWithMPW) } -TEST_F(Jacobi, HipApplyEquivalentToRefWithBlockSize32) +TEST_F(Jacobi, ApplyEquivalentToRefWithBlockSize32) { initialize_data({0, 32, 64, 96, 128}, {}, {}, 32, 100, 111); auto bj = bj_factory->generate(mtx); @@ -401,9 +388,12 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithBlockSize32) } -#if GINKGO_HIP_PLATFORM_HCC -TEST_F(Jacobi, HipApplyEquivalentToRefWithBlockSize64) +#ifdef GKO_COMPILING_HIP +TEST_F(Jacobi, ApplyEquivalentToRefWithBlockSize64) { + if (exec->get_warp_size() == 32) { + GTEST_SKIP(); + } initialize_data({0, 64, 128, 192, 256}, {}, {}, 64, 100, 111); auto bj = bj_factory->generate(mtx); auto d_bj = d_bj_factory->generate(mtx); @@ -413,10 +403,10 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithBlockSize64) GKO_ASSERT_MTX_NEAR(d_x, x, 1e-12); } -#endif // GINKGO_HIP_PLATFORM_HCC +#endif // GKO_COMPILING_HIP -TEST_F(Jacobi, HipApplyEquivalentToRefWithDifferentBlockSize) +TEST_F(Jacobi, ApplyEquivalentToRefWithDifferentBlockSize) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 32, 97, 99); @@ -430,7 +420,7 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithDifferentBlockSize) } -TEST_F(Jacobi, HipApplyEquivalentToRef) +TEST_F(Jacobi, ApplyEquivalentToRef) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, 97, 99); @@ -444,7 +434,7 @@ TEST_F(Jacobi, HipApplyEquivalentToRef) } -TEST_F(Jacobi, HipScalarApplyEquivalentToRef) +TEST_F(Jacobi, ScalarApplyEquivalentToRef) { gko::size_type dim = 313; std::default_random_engine engine(42); @@ -462,14 +452,14 @@ TEST_F(Jacobi, HipScalarApplyEquivalentToRef) std::normal_distribution<>(0.0, 1.0), engine, ref)); auto sx = Vec::create(ref, sb->get_size()); - auto d_smtx = gko::share(Mtx::create(hip)); - auto d_sb = gko::share(Vec::create(hip)); - auto d_sx = gko::share(Vec::create(hip, sb->get_size())); + auto d_smtx = gko::share(Mtx::create(exec)); + auto d_sb = gko::share(Vec::create(exec)); + auto d_sx = gko::share(Vec::create(exec, sb->get_size())); d_smtx->copy_from(smtx.get()); d_sb->copy_from(sb.get()); auto sj = Bj::build().with_max_block_size(1u).on(ref)->generate(smtx); - auto d_sj = Bj::build().with_max_block_size(1u).on(hip)->generate(d_smtx); + auto d_sj = Bj::build().with_max_block_size(1u).on(exec)->generate(d_smtx); sj->apply(sb.get(), sx.get()); d_sj->apply(d_sb.get(), d_sx.get()); @@ -478,14 +468,14 @@ TEST_F(Jacobi, HipScalarApplyEquivalentToRef) } -TEST_F(Jacobi, HipLinearCombinationApplyEquivalentToRef) +TEST_F(Jacobi, LinearCombinationApplyEquivalentToRef) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, 97, 99); auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, hip); + auto d_alpha = gko::initialize({2.0}, exec); auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, hip); + auto d_beta = gko::initialize({-1.0}, exec); auto bj = bj_factory->generate(mtx); auto d_bj = d_bj_factory->generate(mtx); @@ -496,7 +486,7 @@ TEST_F(Jacobi, HipLinearCombinationApplyEquivalentToRef) } -TEST_F(Jacobi, HipScalarLinearCombinationApplyEquivalentToRef) +TEST_F(Jacobi, ScalarLinearCombinationApplyEquivalentToRef) { gko::size_type dim = 313; std::default_random_engine engine(42); @@ -518,16 +508,16 @@ TEST_F(Jacobi, HipScalarLinearCombinationApplyEquivalentToRef) std::normal_distribution<>(0.0, 1.0), engine, ref, gko::dim<2>(dim, 3), 4)); - auto d_smtx = gko::share(gko::clone(hip, smtx)); - auto d_sb = gko::share(gko::clone(hip, sb)); - auto d_sx = gko::share(gko::clone(hip, sx)); + auto d_smtx = gko::share(gko::clone(exec, smtx)); + auto d_sb = gko::share(gko::clone(exec, sb)); + auto d_sx = gko::share(gko::clone(exec, sx)); auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, hip); + auto d_alpha = gko::initialize({2.0}, exec); auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, hip); + auto d_beta = gko::initialize({-1.0}, exec); auto sj = Bj::build().with_max_block_size(1u).on(ref)->generate(smtx); - auto d_sj = Bj::build().with_max_block_size(1u).on(hip)->generate(d_smtx); + auto d_sj = Bj::build().with_max_block_size(1u).on(exec)->generate(d_smtx); sj->apply(alpha.get(), sb.get(), beta.get(), sx.get()); d_sj->apply(d_alpha.get(), d_sb.get(), d_beta.get(), d_sx.get()); @@ -536,7 +526,7 @@ TEST_F(Jacobi, HipScalarLinearCombinationApplyEquivalentToRef) } -TEST_F(Jacobi, HipApplyToMultipleVectorsEquivalentToRef) +TEST_F(Jacobi, ApplyToMultipleVectorsEquivalentToRef) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, 97, 99, 5); @@ -550,14 +540,14 @@ TEST_F(Jacobi, HipApplyToMultipleVectorsEquivalentToRef) } -TEST_F(Jacobi, HipLinearCombinationApplyToMultipleVectorsEquivalentToRef) +TEST_F(Jacobi, LinearCombinationApplyToMultipleVectorsEquivalentToRef) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {}, {}, 13, 97, 99, 5); auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, hip); + auto d_alpha = gko::initialize({2.0}, exec); auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, hip); + auto d_beta = gko::initialize({-1.0}, exec); auto bj = bj_factory->generate(mtx); auto d_bj = d_bj_factory->generate(mtx); @@ -594,19 +584,14 @@ TEST_F(Jacobi, SelectsTheSamePrecisionsAsRef) auto bj = bj_factory->generate(mtx); auto d_bj = gko::clone(ref, d_bj_factory->generate(mtx)); - auto bj_prec = - bj->get_parameters().storage_optimization.block_wise.get_const_data(); - auto d_bj_prec = - d_bj->get_parameters().storage_optimization.block_wise.get_const_data(); - for (int i = 0; i < gko::as(bj.get())->get_num_blocks(); ++i) { - EXPECT_EQ(bj_prec[i], d_bj_prec[i]); - } + GKO_ASSERT_ARRAY_EQ(bj->get_parameters().storage_optimization.block_wise, + d_bj->get_parameters().storage_optimization.block_wise); } TEST_F(Jacobi, AvoidsPrecisionsThatOverflow) { - auto mtx = gko::matrix::Csr<>::create(hip); + auto mtx = gko::matrix::Csr<>::create(exec); // clang-format off mtx->read(mtx_data::diag({ // perfectly conditioned block, small value difference, @@ -623,10 +608,10 @@ TEST_F(Jacobi, AvoidsPrecisionsThatOverflow) auto bj = Bj::build() .with_max_block_size(13u) - .with_block_pointers(gko::array(hip, {0, 2, 4})) + .with_block_pointers(gko::array(exec, {0, 2, 4})) .with_storage_optimization(gko::precision_reduction::autodetect()) .with_accuracy(0.1) - .on(hip) + .on(exec) ->generate(give(mtx)); // both blocks are in the same group, both need (7, 8) @@ -638,7 +623,7 @@ TEST_F(Jacobi, AvoidsPrecisionsThatOverflow) } -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithFullPrecision) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithFullPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, 99); @@ -650,7 +635,7 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithFullPrecision) } -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithReducedPrecision) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithReducedPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, sp, sp, sp, sp, sp, sp, sp, sp, sp, sp}, {}, 13, 97, @@ -663,7 +648,7 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithReducedPrecision) } -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithCustomReducedPrecision) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithCustomReducedPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {tp, tp, tp, tp, tp, tp, tp, tp, tp, tp, tp}, {}, 13, 97, @@ -676,7 +661,7 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithCustomReducedPrecision) } -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithQuarteredPrecision) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithQuarteredPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {hp, hp, hp, hp, hp, hp, hp, hp, hp, hp, hp}, {}, 13, 97, @@ -689,7 +674,7 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithQuarteredPrecision) } -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithCustomQuarteredPrecision) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithCustomQuarteredPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {qp, qp, qp, qp, qp, qp, qp, qp, qp, qp, qp}, {}, 13, 97, @@ -702,7 +687,7 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithCustomQuarteredPrecision) } -TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithAdaptivePrecision) +TEST_F(Jacobi, PreconditionerEquivalentToRefWithAdaptivePrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, @@ -715,7 +700,7 @@ TEST_F(Jacobi, HipPreconditionerEquivalentToRefWithAdaptivePrecision) } -TEST_F(Jacobi, HipTransposedPreconditionerEquivalentToRefWithAdaptivePrecision) +TEST_F(Jacobi, TransposedPreconditionerEquivalentToRefWithAdaptivePrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, @@ -723,15 +708,14 @@ TEST_F(Jacobi, HipTransposedPreconditionerEquivalentToRefWithAdaptivePrecision) auto bj = bj_factory->generate(mtx); auto d_bj = d_bj_factory->generate(mtx); - bj->copy_from(d_bj.get()); + d_bj->copy_from(bj.get()); GKO_ASSERT_MTX_NEAR(gko::as(d_bj->transpose()), - gko::as(bj->transpose()), 1e-14); + gko::as(bj->transpose()), 0); } -TEST_F(Jacobi, - HipConjTransposedPreconditionerEquivalentToRefWithAdaptivePrecision) +TEST_F(Jacobi, ConjTransposedPreconditionerEquivalentToRefWithAdaptivePrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, @@ -739,14 +723,14 @@ TEST_F(Jacobi, auto bj = bj_factory->generate(mtx); auto d_bj = d_bj_factory->generate(mtx); - bj->copy_from(d_bj.get()); + d_bj->copy_from(bj.get()); GKO_ASSERT_MTX_NEAR(gko::as(d_bj->conj_transpose()), - gko::as(bj->conj_transpose()), 1e-14); + gko::as(bj->conj_transpose()), 0); } -TEST_F(Jacobi, HipApplyEquivalentToRefWithFullPrecision) +TEST_F(Jacobi, ApplyEquivalentToRefWithFullPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, @@ -761,7 +745,7 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithFullPrecision) } -TEST_F(Jacobi, HipApplyEquivalentToRefWithReducedPrecision) +TEST_F(Jacobi, ApplyEquivalentToRefWithReducedPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, sp, sp, sp, sp, sp, sp, sp, sp, sp, sp}, {}, 13, 97, @@ -776,7 +760,7 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithReducedPrecision) } -TEST_F(Jacobi, HipApplyEquivalentToRefWithCustomReducedPrecision) +TEST_F(Jacobi, ApplyEquivalentToRefWithCustomReducedPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {tp, tp, tp, tp, tp, tp, tp, tp, tp, tp, tp}, {}, 13, 97, @@ -791,7 +775,7 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithCustomReducedPrecision) } -TEST_F(Jacobi, HipApplyEquivalentToRefWithQuarteredPrecision) +TEST_F(Jacobi, ApplyEquivalentToRefWithQuarteredPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {hp, hp, hp, hp, hp, hp, hp, hp, hp, hp, hp}, {}, 13, 97, @@ -806,7 +790,7 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithQuarteredPrecision) } -TEST_F(Jacobi, HipApplyEquivalentToRefWithCustomReducedAndReducedPrecision) +TEST_F(Jacobi, ApplyEquivalentToRefWithCustomReducedAndReducedPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {up, up, up, up, up, up, up, up, up, up, up}, {}, 13, 97, @@ -821,7 +805,7 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithCustomReducedAndReducedPrecision) } -TEST_F(Jacobi, HipApplyEquivalentToRefWithCustomQuarteredPrecision) +TEST_F(Jacobi, ApplyEquivalentToRefWithCustomQuarteredPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {qp, qp, qp, qp, qp, qp, qp, qp, qp, qp, qp}, {}, 13, 97, @@ -836,7 +820,7 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithCustomQuarteredPrecision) } -TEST_F(Jacobi, HipApplyEquivalentToRefWithAdaptivePrecision) +TEST_F(Jacobi, ApplyEquivalentToRefWithAdaptivePrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, @@ -851,15 +835,15 @@ TEST_F(Jacobi, HipApplyEquivalentToRefWithAdaptivePrecision) } -TEST_F(Jacobi, HipLinearCombinationApplyEquivalentToRefWithAdaptivePrecision) +TEST_F(Jacobi, LinearCombinationApplyEquivalentToRefWithAdaptivePrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, dp, dp, sp, sp, sp, dp, dp, sp, dp, sp}, {}, 13, 97, 99); auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, hip); + auto d_alpha = gko::initialize({2.0}, exec); auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, hip); + auto d_beta = gko::initialize({-1.0}, exec); auto bj = bj_factory->generate(mtx); auto d_bj = d_bj_factory->generate(mtx); @@ -870,7 +854,7 @@ TEST_F(Jacobi, HipLinearCombinationApplyEquivalentToRefWithAdaptivePrecision) } -TEST_F(Jacobi, HipApplyToMultipleVectorsEquivalentToRefWithFullPrecision) +TEST_F(Jacobi, ApplyToMultipleVectorsEquivalentToRefWithFullPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {dp, dp, dp, dp, dp, dp, dp, dp, dp, dp, dp}, {}, 13, 97, @@ -885,7 +869,7 @@ TEST_F(Jacobi, HipApplyToMultipleVectorsEquivalentToRefWithFullPrecision) } -TEST_F(Jacobi, HipApplyToMultipleVectorsEquivalentToRefWithReducedPrecision) +TEST_F(Jacobi, ApplyToMultipleVectorsEquivalentToRefWithReducedPrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, sp, sp, sp, sp, sp, sp, sp, sp, sp, sp}, {}, 13, 97, @@ -900,7 +884,7 @@ TEST_F(Jacobi, HipApplyToMultipleVectorsEquivalentToRefWithReducedPrecision) } -TEST_F(Jacobi, HipApplyToMultipleVectorsEquivalentToRefWithAdaptivePrecision) +TEST_F(Jacobi, ApplyToMultipleVectorsEquivalentToRefWithAdaptivePrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, sp, dp, dp, tp, tp, qp, qp, hp, dp, up}, {}, 13, 97, @@ -917,15 +901,15 @@ TEST_F(Jacobi, HipApplyToMultipleVectorsEquivalentToRefWithAdaptivePrecision) TEST_F( Jacobi, - HipLinearCombinationApplyToMultipleVectorsEquivalentToRefWithAdaptivePrecision) + LinearCombinationApplyToMultipleVectorsEquivalentToRefWithAdaptivePrecision) { initialize_data({0, 11, 24, 33, 45, 55, 67, 70, 80, 92, 100}, {sp, dp, dp, sp, sp, sp, dp, dp, sp, dp, sp}, {}, 13, 97, 99, 5); auto alpha = gko::initialize({2.0}, ref); - auto d_alpha = gko::initialize({2.0}, hip); + auto d_alpha = gko::initialize({2.0}, exec); auto beta = gko::initialize({-1.0}, ref); - auto d_beta = gko::initialize({-1.0}, hip); + auto d_beta = gko::initialize({-1.0}, exec); auto bj = bj_factory->generate(mtx); auto d_bj = d_bj_factory->generate(mtx); @@ -934,6 +918,3 @@ TEST_F( GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6); } - - -} // namespace diff --git a/test/solver/CMakeLists.txt b/test/solver/CMakeLists.txt index 62463edda35..4923f8f585a 100644 --- a/test/solver/CMakeLists.txt +++ b/test/solver/CMakeLists.txt @@ -1,9 +1,13 @@ ginkgo_create_common_test(bicg_kernels) ginkgo_create_common_test(bicgstab_kernels) +ginkgo_create_common_test(cb_gmres_kernels) ginkgo_create_common_test(cg_kernels) ginkgo_create_common_test(cgs_kernels) ginkgo_create_common_test(fcg_kernels) +ginkgo_create_common_test(gmres_kernels) +ginkgo_create_common_test(idr_kernels) ginkgo_create_common_test(ir_kernels) ginkgo_create_common_test(lower_trs_kernels DISABLE_EXECUTORS dpcpp) +ginkgo_create_common_test(multigrid_kernels DISABLE_EXECUTORS dpcpp) ginkgo_create_common_test(solver DISABLE_EXECUTORS dpcpp) ginkgo_create_common_test(upper_trs_kernels DISABLE_EXECUTORS dpcpp) diff --git a/test/solver/bicg_kernels.cpp b/test/solver/bicg_kernels.cpp index 6ed408a3651..1bc10bb53e2 100644 --- a/test/solver/bicg_kernels.cpp +++ b/test/solver/bicg_kernels.cpp @@ -54,43 +54,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class Bicg : public ::testing::Test { +class Bicg : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif using Mtx = gko::matrix::Dense; - using index_type = gko::int32; - - Bicg() : rand_engine(30) {} - void SetUp() + Bicg() : rand_engine(30) { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - std::string file_name(gko::matrices::location_ani1_mtx); auto input_file = std::ifstream(file_name, std::ios::in); - if (!input_file) { - FAIL() << "Could not find the file \"" << file_name - << "\", which is required for this test.\n"; - } mtx_ani = gko::read(input_file, ref); d_mtx_ani = gko::clone(exec, mtx_ani.get()); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - std::unique_ptr gen_mtx(gko::size_type num_rows, gko::size_type num_cols, gko::size_type stride) { @@ -149,9 +124,6 @@ class Bicg : public ::testing::Test { exec, *stop_status); } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; std::unique_ptr b; @@ -321,6 +293,3 @@ TEST_F(Bicg, ApplyWithSuiteSparseMatrixIsEquivalentToRef) GKO_ASSERT_MTX_NEAR(d_x, x, ::r::value * 100); } - - -} // namespace diff --git a/test/solver/bicgstab_kernels.cpp b/test/solver/bicgstab_kernels.cpp index 5cef434ab2e..f31865aa70f 100644 --- a/test/solver/bicgstab_kernels.cpp +++ b/test/solver/bicgstab_kernels.cpp @@ -54,27 +54,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class Bicgstab : public ::testing::Test { +class Bicgstab : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = gko::int32; using Mtx = gko::matrix::Dense; using Solver = gko::solver::Bicgstab; - Bicgstab() : rand_engine(30) {} - - void SetUp() + Bicgstab() : rand_engine(30) { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - auto data = gko::matrix_data( gko::dim<2>{123, 123}, std::normal_distribution(-1.0, 1.0), rand_engine); @@ -101,13 +87,6 @@ class Bicgstab : public ::testing::Test { .on(ref); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - std::unique_ptr gen_mtx(gko::size_type num_rows, gko::size_type num_cols, gko::size_type stride) { @@ -174,9 +153,6 @@ class Bicgstab : public ::testing::Test { exec, *stop_status); } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; std::shared_ptr mtx; @@ -337,6 +313,3 @@ TEST_F(Bicgstab, BicgstabApplyMultipleRHSIsEquivalentToRef) GKO_ASSERT_MTX_NEAR(d_x, x, ::r::value * 2000); } - - -} // namespace diff --git a/dpcpp/test/solver/cb_gmres_kernels.cpp b/test/solver/cb_gmres_kernels.cpp similarity index 80% rename from dpcpp/test/solver/cb_gmres_kernels.cpp rename to test/solver/cb_gmres_kernels.cpp index f4bdf10b47e..9947d3bd5f6 100644 --- a/dpcpp/test/solver/cb_gmres_kernels.cpp +++ b/test/solver/cb_gmres_kernels.cpp @@ -30,45 +30,34 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/solver/cb_gmres_kernels.hpp" +#include #include #include -#include - - #include #include #include #include +#include #include #include #include #include "core/solver/cb_gmres_accessor.hpp" -#include "core/solver/cb_gmres_kernels.hpp" #include "core/test/utils.hpp" +#include "test/utils/executor.hpp" -namespace { - - -class CbGmres : public ::testing::Test { +class CbGmres : public CommonTestFixture { protected: -// the CbGmres in single mode does not give benefit from mixed precision storage -#if GINKGO_DPCPP_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif using storage_type = float; - using index_type = int; using size_type = gko::size_type; using Range3dHelper = gko::cb_gmres::Range3dHelper; @@ -79,20 +68,6 @@ class CbGmres : public ::testing::Test { CbGmres() : rand_engine(30) {} - void SetUp() - { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } - } - std::unique_ptr gen_mtx(int num_rows, int num_cols) { return gko::test::generate_random_matrix( @@ -171,31 +146,30 @@ class CbGmres : public ::testing::Test { num_reorth->get_data()[i] = 5; } - d_x = gko::clone(dpcpp, x); + d_x = gko::clone(exec, x); d_before_preconditioner = Mtx::create_with_config_of(d_x.get()); - d_y = gko::clone(dpcpp, y); - d_b = gko::clone(dpcpp, b); - d_arnoldi_norm = gko::clone(dpcpp, arnoldi_norm); - d_range_helper = Range3dHelper{dpcpp, {}}; + d_y = gko::clone(exec, y); + d_b = gko::clone(exec, b); + d_arnoldi_norm = gko::clone(exec, arnoldi_norm); + d_range_helper = Range3dHelper{exec, {}}; d_range_helper = range_helper; - d_next_krylov_basis = gko::clone(dpcpp, next_krylov_basis); - d_hessenberg = gko::clone(dpcpp, hessenberg); - d_hessenberg_iter = gko::clone(dpcpp, hessenberg_iter); - d_buffer_iter = gko::clone(dpcpp, buffer_iter); - d_residual = gko::clone(dpcpp, residual); - d_residual_norm = gko::clone(dpcpp, residual_norm); - d_residual_norm_collection = - gko::clone(dpcpp, residual_norm_collection); - d_givens_sin = gko::clone(dpcpp, givens_sin); - d_givens_cos = gko::clone(dpcpp, givens_cos); + d_next_krylov_basis = gko::clone(exec, next_krylov_basis); + d_hessenberg = gko::clone(exec, hessenberg); + d_hessenberg_iter = gko::clone(exec, hessenberg_iter); + d_buffer_iter = Mtx::create(exec); + d_residual = gko::clone(exec, residual); + d_residual_norm = gko::clone(exec, residual_norm); + d_residual_norm_collection = gko::clone(exec, residual_norm_collection); + d_givens_sin = gko::clone(exec, givens_sin); + d_givens_cos = gko::clone(exec, givens_cos); d_stop_status = std::make_unique>( - dpcpp, *stop_status); + exec, *stop_status); d_reorth_status = std::make_unique>( - dpcpp, *reorth_status); + exec, *reorth_status); d_final_iter_nums = std::make_unique>( - dpcpp, *final_iter_nums); + exec, *final_iter_nums); d_num_reorth = - std::make_unique>(dpcpp, *num_reorth); + std::make_unique>(exec, *num_reorth); } void assert_krylov_bases_near() @@ -204,17 +178,14 @@ class CbGmres : public ::testing::Test { auto& krylov_bases = range_helper.get_bases(); d_to_host = d_range_helper.get_bases(); const auto tolerance = r::value; - using sycl::abs; + using std::abs; for (gko::size_type i = 0; i < krylov_bases.get_num_elems(); ++i) { const auto ref_value = krylov_bases.get_const_data()[i]; const auto dev_value = d_to_host.get_const_data()[i]; - ASSERT_LE(fabsf(dev_value - ref_value), tolerance); + ASSERT_LE(abs(dev_value - ref_value), tolerance); } } - std::shared_ptr ref; - std::shared_ptr dpcpp; - std::default_random_engine rand_engine; std::unique_ptr before_preconditioner; @@ -259,15 +230,15 @@ class CbGmres : public ::testing::Test { }; -TEST_F(CbGmres, DpcppCbGmresInitialize1IsEquivalentToRef) +TEST_F(CbGmres, CbGmresInitialize1IsEquivalentToRef) { initialize_data(); gko::kernels::reference::cb_gmres::initialize_1( ref, b.get(), residual.get(), givens_sin.get(), givens_cos.get(), stop_status.get(), default_krylov_dim_mixed); - gko::kernels::dpcpp::cb_gmres::initialize_1( - dpcpp, d_b.get(), d_residual.get(), d_givens_sin.get(), + gko::kernels::EXEC_NAMESPACE::cb_gmres::initialize_1( + exec, d_b.get(), d_residual.get(), d_givens_sin.get(), d_givens_cos.get(), d_stop_status.get(), default_krylov_dim_mixed); GKO_ASSERT_MTX_NEAR(d_residual, residual, r::value); @@ -276,20 +247,19 @@ TEST_F(CbGmres, DpcppCbGmresInitialize1IsEquivalentToRef) GKO_ASSERT_ARRAY_EQ(*d_stop_status, *stop_status); } - -TEST_F(CbGmres, DpcppCbGmresInitialize2IsEquivalentToRef) +TEST_F(CbGmres, CbGmresInitialize2IsEquivalentToRef) { initialize_data(); gko::array tmp{ref}; - gko::array dtmp{dpcpp}; + gko::array dtmp{exec}; gko::kernels::reference::cb_gmres::initialize_2( ref, residual.get(), residual_norm.get(), residual_norm_collection.get(), arnoldi_norm.get(), range_helper.get_range(), next_krylov_basis.get(), final_iter_nums.get(), tmp, default_krylov_dim_mixed); - gko::kernels::dpcpp::cb_gmres::initialize_2( - dpcpp, d_residual.get(), d_residual_norm.get(), + gko::kernels::EXEC_NAMESPACE::cb_gmres::initialize_2( + exec, d_residual.get(), d_residual_norm.get(), d_residual_norm_collection.get(), d_arnoldi_norm.get(), d_range_helper.get_range(), d_next_krylov_basis.get(), d_final_iter_nums.get(), dtmp, default_krylov_dim_mixed); @@ -302,8 +272,7 @@ TEST_F(CbGmres, DpcppCbGmresInitialize2IsEquivalentToRef) GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); } - -TEST_F(CbGmres, DpcppCbGmresStep1IsEquivalentToRef) +TEST_F(CbGmres, CbGmresStep1IsEquivalentToRef) { initialize_data(); int iter = 5; @@ -314,13 +283,13 @@ TEST_F(CbGmres, DpcppCbGmresStep1IsEquivalentToRef) range_helper.get_range(), hessenberg_iter.get(), buffer_iter.get(), arnoldi_norm.get(), iter, final_iter_nums.get(), stop_status.get(), reorth_status.get(), num_reorth.get()); - gko::kernels::dpcpp::cb_gmres::step_1( - dpcpp, d_next_krylov_basis.get(), d_givens_sin.get(), - d_givens_cos.get(), d_residual_norm.get(), - d_residual_norm_collection.get(), d_range_helper.get_range(), - d_hessenberg_iter.get(), d_buffer_iter.get(), d_arnoldi_norm.get(), - iter, d_final_iter_nums.get(), d_stop_status.get(), - d_reorth_status.get(), d_num_reorth.get()); + gko::kernels::EXEC_NAMESPACE::cb_gmres::step_1( + exec, d_next_krylov_basis.get(), d_givens_sin.get(), d_givens_cos.get(), + d_residual_norm.get(), d_residual_norm_collection.get(), + d_range_helper.get_range(), d_hessenberg_iter.get(), + d_buffer_iter.get(), d_arnoldi_norm.get(), iter, + d_final_iter_nums.get(), d_stop_status.get(), d_reorth_status.get(), + d_num_reorth.get()); GKO_ASSERT_MTX_NEAR(d_arnoldi_norm, arnoldi_norm, r::value); GKO_ASSERT_MTX_NEAR(d_next_krylov_basis, next_krylov_basis, @@ -336,8 +305,7 @@ TEST_F(CbGmres, DpcppCbGmresStep1IsEquivalentToRef) GKO_ASSERT_ARRAY_EQ(*d_final_iter_nums, *final_iter_nums); } - -TEST_F(CbGmres, DpcppCbGmresStep2IsEquivalentToRef) +TEST_F(CbGmres, CbGmresStep2IsEquivalentToRef) { initialize_data(); @@ -345,8 +313,8 @@ TEST_F(CbGmres, DpcppCbGmresStep2IsEquivalentToRef) ref, residual_norm_collection.get(), range_helper.get_range().get_accessor().to_const(), hessenberg.get(), y.get(), before_preconditioner.get(), final_iter_nums.get()); - gko::kernels::dpcpp::cb_gmres::step_2( - dpcpp, d_residual_norm_collection.get(), + gko::kernels::EXEC_NAMESPACE::cb_gmres::step_2( + exec, d_residual_norm_collection.get(), d_range_helper.get_range().get_accessor().to_const(), d_hessenberg.get(), d_y.get(), d_before_preconditioner.get(), d_final_iter_nums.get()); @@ -354,6 +322,3 @@ TEST_F(CbGmres, DpcppCbGmresStep2IsEquivalentToRef) GKO_ASSERT_MTX_NEAR(d_y, y, r::value); GKO_ASSERT_MTX_NEAR(d_x, x, r::value); } - - -} // namespace diff --git a/test/solver/cg_kernels.cpp b/test/solver/cg_kernels.cpp index 93d584bbf38..0baf1143800 100644 --- a/test/solver/cg_kernels.cpp +++ b/test/solver/cg_kernels.cpp @@ -53,34 +53,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class Cg : public ::testing::Test { +class Cg : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = int; using Mtx = gko::matrix::Dense; Cg() : rand_engine(30) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - std::unique_ptr gen_mtx(gko::size_type num_rows, gko::size_type num_cols, gko::size_type stride) { @@ -131,9 +109,6 @@ class Cg : public ::testing::Test { exec, *stop_status); } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; std::unique_ptr b; @@ -250,6 +225,3 @@ TEST_F(Cg, ApplyIsEquivalentToRef) GKO_ASSERT_MTX_NEAR(d_x, x, ::r::value * 1000); } - - -} // namespace diff --git a/test/solver/cgs_kernels.cpp b/test/solver/cgs_kernels.cpp index ff9c4079f74..a2909a611b7 100644 --- a/test/solver/cgs_kernels.cpp +++ b/test/solver/cgs_kernels.cpp @@ -53,27 +53,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class Cgs : public ::testing::Test { +class Cgs : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = int; using Mtx = gko::matrix::Dense; using Solver = gko::solver::Cgs; - Cgs() : rand_engine(30) {} - - void SetUp() + Cgs() : rand_engine(30) { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - auto data = gko::matrix_data( gko::dim<2>{123, 123}, std::normal_distribution(-1.0, 1.0), rand_engine); @@ -99,13 +85,6 @@ class Cgs : public ::testing::Test { .on(ref); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - std::unique_ptr gen_mtx(gko::size_type num_rows, gko::size_type num_cols, gko::size_type stride) { @@ -167,9 +146,6 @@ class Cgs : public ::testing::Test { exec, *stop_status); } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; std::shared_ptr mtx; @@ -330,5 +306,3 @@ TEST_F(Cgs, CgsApplyMultipleRHSIsEquivalentToRef) GKO_ASSERT_MTX_NEAR(d_b, b, ::r::value * 5e3); GKO_ASSERT_MTX_NEAR(d_x, x, ::r::value * 5e3); } - -} // namespace diff --git a/test/solver/fcg_kernels.cpp b/test/solver/fcg_kernels.cpp index 86b884226bc..3decc0ef24a 100644 --- a/test/solver/fcg_kernels.cpp +++ b/test/solver/fcg_kernels.cpp @@ -53,35 +53,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class Fcg : public ::testing::Test { +class Fcg : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = int; using Mtx = gko::matrix::Dense; using Solver = gko::solver::Fcg; Fcg() : rand_engine(30) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - std::unique_ptr gen_mtx(gko::size_type num_rows, gko::size_type num_cols, gko::size_type stride) { @@ -135,9 +113,6 @@ class Fcg : public ::testing::Test { exec, *stop_status); } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; std::unique_ptr b; @@ -259,6 +234,3 @@ TEST_F(Fcg, ApplyIsEquivalentToRef) GKO_ASSERT_MTX_NEAR(d_x, x, ::r::value * 1000); } - - -} // namespace diff --git a/dpcpp/test/solver/gmres_kernels.cpp b/test/solver/gmres_kernels.cpp similarity index 75% rename from dpcpp/test/solver/gmres_kernels.cpp rename to test/solver/gmres_kernels.cpp index 9461c821935..4740d946bce 100644 --- a/dpcpp/test/solver/gmres_kernels.cpp +++ b/test/solver/gmres_kernels.cpp @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/solver/gmres_kernels.hpp" #include @@ -43,46 +43,46 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include #include #include -#include "core/solver/gmres_kernels.hpp" #include "core/test/utils.hpp" +#include "test/utils/executor.hpp" -namespace { - - -class Gmres : public ::testing::Test { +class Gmres : public CommonTestFixture { protected: -#if GINKGO_DPCPP_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif - using index_type = gko::int32; using Mtx = gko::matrix::Dense; + using Solver = gko::solver::Gmres; using norm_type = gko::remove_complex; using NormVector = gko::matrix::Dense; template using Dense = typename gko::matrix::Dense; - Gmres() : rand_engine(30) {} - - void SetUp() + Gmres() : rand_engine(30) { - ASSERT_GT(gko::DpcppExecutor::get_num_devices("all"), 0); - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - } - - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } + mtx = gen_mtx(123, 123); + d_mtx = gko::clone(exec, mtx); + exec_gmres_factory = + Solver::build() + .with_criteria( + gko::stop::Iteration::build().with_max_iters(246u).on(exec), + gko::stop::ResidualNorm::build() + .with_reduction_factor(value_type{1e-15}) + .on(exec)) + .on(exec); + + ref_gmres_factory = + Solver::build() + .with_criteria( + gko::stop::Iteration::build().with_max_iters(246u).on(ref), + gko::stop::ResidualNorm::build() + .with_reduction_factor(value_type{1e-15}) + .on(ref)) + .on(ref); } template @@ -126,30 +126,31 @@ class Gmres : public ::testing::Test { final_iter_nums->get_data()[i] = 5; } - d_x = gko::clone(dpcpp, x); + d_x = gko::clone(exec, x); d_before_preconditioner = Mtx::create_with_config_of(d_x.get()); - d_y = gko::clone(dpcpp, y); - d_b = gko::clone(dpcpp, b); - d_krylov_bases = gko::clone(dpcpp, krylov_bases); - d_hessenberg = gko::clone(dpcpp, hessenberg); - d_hessenberg_iter = gko::clone(dpcpp, hessenberg_iter); - d_residual = gko::clone(dpcpp, residual); - d_residual_norm = gko::clone(dpcpp, residual_norm); - d_residual_norm_collection = - gko::clone(dpcpp, residual_norm_collection); - d_givens_sin = gko::clone(dpcpp, givens_sin); - d_givens_cos = gko::clone(dpcpp, givens_cos); + d_y = gko::clone(exec, y); + d_b = gko::clone(exec, b); + d_krylov_bases = gko::clone(exec, krylov_bases); + d_hessenberg = gko::clone(exec, hessenberg); + d_hessenberg_iter = gko::clone(exec, hessenberg_iter); + d_residual = gko::clone(exec, residual); + d_residual_norm = gko::clone(exec, residual_norm); + d_residual_norm_collection = gko::clone(exec, residual_norm_collection); + d_givens_sin = gko::clone(exec, givens_sin); + d_givens_cos = gko::clone(exec, givens_cos); d_stop_status = std::make_unique>( - dpcpp, *stop_status); + exec, *stop_status); d_final_iter_nums = std::make_unique>( - dpcpp, *final_iter_nums); + exec, *final_iter_nums); } - std::shared_ptr ref; - std::shared_ptr dpcpp; - std::default_random_engine rand_engine; + std::shared_ptr mtx; + std::shared_ptr d_mtx; + std::unique_ptr exec_gmres_factory; + std::unique_ptr ref_gmres_factory; + std::unique_ptr before_preconditioner; std::unique_ptr x; std::unique_ptr y; @@ -182,15 +183,15 @@ class Gmres : public ::testing::Test { }; -TEST_F(Gmres, DpcppGmresInitialize1IsEquivalentToRef) +TEST_F(Gmres, GmresInitialize1IsEquivalentToRef) { initialize_data(); gko::kernels::reference::gmres::initialize_1( ref, b.get(), residual.get(), givens_sin.get(), givens_cos.get(), stop_status.get(), gko::solver::default_krylov_dim); - gko::kernels::dpcpp::gmres::initialize_1( - dpcpp, d_b.get(), d_residual.get(), d_givens_sin.get(), + gko::kernels::EXEC_NAMESPACE::gmres::initialize_1( + exec, d_b.get(), d_residual.get(), d_givens_sin.get(), d_givens_cos.get(), d_stop_status.get(), gko::solver::default_krylov_dim); @@ -201,18 +202,18 @@ TEST_F(Gmres, DpcppGmresInitialize1IsEquivalentToRef) } -TEST_F(Gmres, DpcppGmresInitialize2IsEquivalentToRef) +TEST_F(Gmres, GmresInitialize2IsEquivalentToRef) { initialize_data(); gko::array tmp{ref}; - gko::array dtmp{dpcpp}; + gko::array dtmp{exec}; gko::kernels::reference::gmres::initialize_2( ref, residual.get(), residual_norm.get(), residual_norm_collection.get(), krylov_bases.get(), final_iter_nums.get(), tmp, gko::solver::default_krylov_dim); - gko::kernels::dpcpp::gmres::initialize_2( - dpcpp, d_residual.get(), d_residual_norm.get(), + gko::kernels::EXEC_NAMESPACE::gmres::initialize_2( + exec, d_residual.get(), d_residual_norm.get(), d_residual_norm_collection.get(), d_krylov_bases.get(), d_final_iter_nums.get(), dtmp, gko::solver::default_krylov_dim); @@ -224,7 +225,7 @@ TEST_F(Gmres, DpcppGmresInitialize2IsEquivalentToRef) } -TEST_F(Gmres, DpcppGmresStep1IsEquivalentToRef) +TEST_F(Gmres, GmresStep1IsEquivalentToRef) { initialize_data(); int iter = 5; @@ -233,8 +234,8 @@ TEST_F(Gmres, DpcppGmresStep1IsEquivalentToRef) ref, x->get_size()[0], givens_sin.get(), givens_cos.get(), residual_norm.get(), residual_norm_collection.get(), krylov_bases.get(), hessenberg_iter.get(), iter, final_iter_nums.get(), stop_status.get()); - gko::kernels::dpcpp::gmres::step_1( - dpcpp, d_x->get_size()[0], d_givens_sin.get(), d_givens_cos.get(), + gko::kernels::EXEC_NAMESPACE::gmres::step_1( + exec, d_x->get_size()[0], d_givens_sin.get(), d_givens_cos.get(), d_residual_norm.get(), d_residual_norm_collection.get(), d_krylov_bases.get(), d_hessenberg_iter.get(), iter, d_final_iter_nums.get(), d_stop_status.get()); @@ -251,7 +252,7 @@ TEST_F(Gmres, DpcppGmresStep1IsEquivalentToRef) } -TEST_F(Gmres, DpcppGmresStep1OnSingleRHSIsEquivalentToRef) +TEST_F(Gmres, GmresStep1OnSingleRHSIsEquivalentToRef) { initialize_data(1); int iter = 5; @@ -260,8 +261,8 @@ TEST_F(Gmres, DpcppGmresStep1OnSingleRHSIsEquivalentToRef) ref, x->get_size()[0], givens_sin.get(), givens_cos.get(), residual_norm.get(), residual_norm_collection.get(), krylov_bases.get(), hessenberg_iter.get(), iter, final_iter_nums.get(), stop_status.get()); - gko::kernels::dpcpp::gmres::step_1( - dpcpp, d_x->get_size()[0], d_givens_sin.get(), d_givens_cos.get(), + gko::kernels::EXEC_NAMESPACE::gmres::step_1( + exec, d_x->get_size()[0], d_givens_sin.get(), d_givens_cos.get(), d_residual_norm.get(), d_residual_norm_collection.get(), d_krylov_bases.get(), d_hessenberg_iter.get(), iter, d_final_iter_nums.get(), d_stop_status.get()); @@ -278,7 +279,7 @@ TEST_F(Gmres, DpcppGmresStep1OnSingleRHSIsEquivalentToRef) } -TEST_F(Gmres, DpcppGmresStep2IsEquivalentToRef) +TEST_F(Gmres, GmresStep2IsEquivalentToRef) { initialize_data(); @@ -286,14 +287,29 @@ TEST_F(Gmres, DpcppGmresStep2IsEquivalentToRef) krylov_bases.get(), hessenberg.get(), y.get(), before_preconditioner.get(), final_iter_nums.get()); - gko::kernels::dpcpp::gmres::step_2(dpcpp, d_residual_norm_collection.get(), - d_krylov_bases.get(), d_hessenberg.get(), - d_y.get(), d_before_preconditioner.get(), - d_final_iter_nums.get()); + gko::kernels::EXEC_NAMESPACE::gmres::step_2( + exec, d_residual_norm_collection.get(), d_krylov_bases.get(), + d_hessenberg.get(), d_y.get(), d_before_preconditioner.get(), + d_final_iter_nums.get()); GKO_ASSERT_MTX_NEAR(d_y, y, r::value); GKO_ASSERT_MTX_NEAR(d_x, x, r::value); } -} // namespace +TEST_F(Gmres, GmresApplyOneRHSIsEquivalentToRef) +{ + int m = 123; + int n = 1; + auto ref_solver = ref_gmres_factory->generate(mtx); + auto exec_solver = exec_gmres_factory->generate(d_mtx); + auto b = gen_mtx(m, n); + auto x = gen_mtx(m, n); + auto d_b = gko::clone(exec, b); + auto d_x = gko::clone(exec, x); + + ref_solver->apply(b.get(), x.get()); + exec_solver->apply(d_b.get(), d_x.get()); + + GKO_ASSERT_MTX_NEAR(d_x, x, r::value * 100); +} diff --git a/dpcpp/test/solver/idr_kernels.cpp b/test/solver/idr_kernels.cpp similarity index 76% rename from dpcpp/test/solver/idr_kernels.cpp rename to test/solver/idr_kernels.cpp index b0fd1668696..acd9364d494 100644 --- a/dpcpp/test/solver/idr_kernels.cpp +++ b/test/solver/idr_kernels.cpp @@ -30,9 +30,10 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/solver/idr_kernels.hpp" +#include #include @@ -42,46 +43,37 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include +#include #include +#include #include #include #include -#include "core/solver/idr_kernels.hpp" #include "core/test/utils.hpp" - - -namespace { +#include "test/utils/executor.hpp" // use another alias to avoid conflict name in the Idr template using rr = typename gko::test::reduction_factor; -class Idr : public ::testing::Test { + +class Idr : public CommonTestFixture { protected: -#if GINKGO_DPCPP_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif using Mtx = gko::matrix::Dense; using Solver = gko::solver::Idr; - Idr() : rand_engine(30) {} - - void SetUp() + Idr() : rand_engine(30) { - ref = gko::ReferenceExecutor::create(); - dpcpp = gko::DpcppExecutor::create(0, ref); - - dpcpp_idr_factory = + exec_idr_factory = Solver::build() .with_deterministic(true) .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(dpcpp)) - .on(dpcpp); + gko::stop::Iteration::build().with_max_iters(1u).on(exec)) + .on(exec); ref_idr_factory = Solver::build() @@ -91,13 +83,6 @@ class Idr : public ::testing::Test { .on(ref); } - void TearDown() - { - if (dpcpp != nullptr) { - ASSERT_NO_THROW(dpcpp->synchronize()); - } - } - std::unique_ptr gen_mtx(int num_rows, int num_cols) { return gko::test::generate_random_matrix( @@ -132,33 +117,30 @@ class Idr : public ::testing::Test { stop_status->get_data()[i].reset(); } - d_mtx = gko::clone(dpcpp, mtx); - d_x = gko::clone(dpcpp, x); - d_b = gko::clone(dpcpp, b); - d_r = gko::clone(dpcpp, r); - d_m = gko::clone(dpcpp, m); - d_f = gko::clone(dpcpp, f); - d_g = gko::clone(dpcpp, g); - d_u = gko::clone(dpcpp, u); - d_c = gko::clone(dpcpp, c); - d_v = gko::clone(dpcpp, v); - d_p = gko::clone(dpcpp, p); - d_alpha = gko::clone(dpcpp, alpha); - d_omega = gko::clone(dpcpp, omega); - d_tht = gko::clone(dpcpp, tht); - d_residual_norm = gko::clone(dpcpp, residual_norm); + d_mtx = gko::clone(exec, mtx); + d_x = gko::clone(exec, x); + d_b = gko::clone(exec, b); + d_r = gko::clone(exec, r); + d_m = gko::clone(exec, m); + d_f = gko::clone(exec, f); + d_g = gko::clone(exec, g); + d_u = gko::clone(exec, u); + d_c = gko::clone(exec, c); + d_v = gko::clone(exec, v); + d_p = gko::clone(exec, p); + d_alpha = gko::clone(exec, alpha); + d_omega = gko::clone(exec, omega); + d_tht = gko::clone(exec, tht); + d_residual_norm = gko::clone(exec, residual_norm); d_stop_status = std::make_unique>( - dpcpp, *stop_status); + exec, *stop_status); } - std::shared_ptr ref; - std::shared_ptr dpcpp; - std::default_random_engine rand_engine; std::shared_ptr mtx; std::shared_ptr d_mtx; - std::unique_ptr dpcpp_idr_factory; + std::unique_ptr exec_idr_factory; std::unique_ptr ref_idr_factory; gko::size_type nrhs; @@ -203,8 +185,8 @@ TEST_F(Idr, IdrInitializeIsEquivalentToRef) gko::kernels::reference::idr::initialize(ref, nrhs, m.get(), p.get(), true, stop_status.get()); - gko::kernels::dpcpp::idr::initialize(dpcpp, nrhs, d_m.get(), d_p.get(), - true, d_stop_status.get()); + gko::kernels::EXEC_NAMESPACE::idr::initialize( + exec, nrhs, d_m.get(), d_p.get(), true, d_stop_status.get()); GKO_ASSERT_MTX_NEAR(m, d_m, rr::value); GKO_ASSERT_MTX_NEAR(p, d_p, rr::value); @@ -219,9 +201,9 @@ TEST_F(Idr, IdrStep1IsEquivalentToRef) gko::kernels::reference::idr::step_1(ref, nrhs, k, m.get(), f.get(), r.get(), g.get(), c.get(), v.get(), stop_status.get()); - gko::kernels::dpcpp::idr::step_1(dpcpp, nrhs, k, d_m.get(), d_f.get(), - d_r.get(), d_g.get(), d_c.get(), d_v.get(), - d_stop_status.get()); + gko::kernels::EXEC_NAMESPACE::idr::step_1( + exec, nrhs, k, d_m.get(), d_f.get(), d_r.get(), d_g.get(), d_c.get(), + d_v.get(), d_stop_status.get()); GKO_ASSERT_MTX_NEAR(c, d_c, rr::value); GKO_ASSERT_MTX_NEAR(v, d_v, rr::value); @@ -235,8 +217,9 @@ TEST_F(Idr, IdrStep2IsEquivalentToRef) gko::size_type k = 2; gko::kernels::reference::idr::step_2(ref, nrhs, k, omega.get(), v.get(), c.get(), u.get(), stop_status.get()); - gko::kernels::dpcpp::idr::step_2(dpcpp, nrhs, k, d_omega.get(), d_v.get(), - d_c.get(), d_u.get(), d_stop_status.get()); + gko::kernels::EXEC_NAMESPACE::idr::step_2(exec, nrhs, k, d_omega.get(), + d_v.get(), d_c.get(), d_u.get(), + d_stop_status.get()); GKO_ASSERT_MTX_NEAR(u, d_u, rr::value); } @@ -250,8 +233,8 @@ TEST_F(Idr, IdrStep3IsEquivalentToRef) gko::kernels::reference::idr::step_3( ref, nrhs, k, p.get(), g.get(), v.get(), u.get(), m.get(), f.get(), alpha.get(), r.get(), x.get(), stop_status.get()); - gko::kernels::dpcpp::idr::step_3( - dpcpp, nrhs, k, d_p.get(), d_g.get(), d_v.get(), d_u.get(), d_m.get(), + gko::kernels::EXEC_NAMESPACE::idr::step_3( + exec, nrhs, k, d_p.get(), d_g.get(), d_v.get(), d_u.get(), d_m.get(), d_f.get(), d_alpha.get(), d_r.get(), d_x.get(), d_stop_status.get()); GKO_ASSERT_MTX_NEAR(g, d_g, 10 * rr::value); @@ -272,9 +255,9 @@ TEST_F(Idr, IdrComputeOmegaIsEquivalentToRef) gko::kernels::reference::idr::compute_omega(ref, nrhs, kappa, tht.get(), residual_norm.get(), omega.get(), stop_status.get()); - gko::kernels::dpcpp::idr::compute_omega(dpcpp, nrhs, kappa, d_tht.get(), - d_residual_norm.get(), - d_omega.get(), d_stop_status.get()); + gko::kernels::EXEC_NAMESPACE::idr::compute_omega( + exec, nrhs, kappa, d_tht.get(), d_residual_norm.get(), d_omega.get(), + d_stop_status.get()); GKO_ASSERT_MTX_NEAR(omega, d_omega, rr::value); } @@ -282,16 +265,18 @@ TEST_F(Idr, IdrComputeOmegaIsEquivalentToRef) TEST_F(Idr, IdrIterationOneRHSIsEquivalentToRef) { - if (dpcpp->get_queue()->get_device().is_gpu()) { +#ifdef GKO_COMPILING_DPCPP + if (exec->get_queue()->get_device().is_gpu()) { GTEST_SKIP() << "skip the test because oneMKL GEMM on gpu may give NaN " "(under investigation)"; } +#endif initialize_data(123, 1); auto ref_solver = ref_idr_factory->generate(mtx); - auto dpcpp_solver = dpcpp_idr_factory->generate(d_mtx); + auto exec_solver = exec_idr_factory->generate(d_mtx); ref_solver->apply(b.get(), x.get()); - dpcpp_solver->apply(d_b.get(), d_x.get()); + exec_solver->apply(d_b.get(), d_x.get()); GKO_ASSERT_MTX_NEAR(d_b, b, rr::value * 10); GKO_ASSERT_MTX_NEAR(d_x, x, rr::value * 10); @@ -301,13 +286,13 @@ TEST_F(Idr, IdrIterationOneRHSIsEquivalentToRef) TEST_F(Idr, IdrIterationWithComplexSubspaceOneRHSIsEquivalentToRef) { initialize_data(123, 1); - dpcpp_idr_factory = + exec_idr_factory = Solver::build() .with_deterministic(true) .with_complex_subspace(true) .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(dpcpp)) - .on(dpcpp); + gko::stop::Iteration::build().with_max_iters(1u).on(exec)) + .on(exec); ref_idr_factory = Solver::build() .with_deterministic(true) @@ -316,10 +301,10 @@ TEST_F(Idr, IdrIterationWithComplexSubspaceOneRHSIsEquivalentToRef) gko::stop::Iteration::build().with_max_iters(1u).on(ref)) .on(ref); auto ref_solver = ref_idr_factory->generate(mtx); - auto dpcpp_solver = dpcpp_idr_factory->generate(d_mtx); + auto exec_solver = exec_idr_factory->generate(d_mtx); ref_solver->apply(b.get(), x.get()); - dpcpp_solver->apply(d_b.get(), d_x.get()); + exec_solver->apply(d_b.get(), d_x.get()); GKO_ASSERT_MTX_NEAR(d_b, b, rr::value * 100); GKO_ASSERT_MTX_NEAR(d_x, x, rr::value * 100); @@ -329,11 +314,11 @@ TEST_F(Idr, IdrIterationWithComplexSubspaceOneRHSIsEquivalentToRef) TEST_F(Idr, IdrIterationMultipleRHSIsEquivalentToRef) { initialize_data(123, 16); - auto dpcpp_solver = dpcpp_idr_factory->generate(d_mtx); + auto exec_solver = exec_idr_factory->generate(d_mtx); auto ref_solver = ref_idr_factory->generate(mtx); ref_solver->apply(b.get(), x.get()); - dpcpp_solver->apply(d_b.get(), d_x.get()); + exec_solver->apply(d_b.get(), d_x.get()); GKO_ASSERT_MTX_NEAR(d_b, b, rr::value * 500); GKO_ASSERT_MTX_NEAR(d_x, x, rr::value * 500); @@ -342,14 +327,14 @@ TEST_F(Idr, IdrIterationMultipleRHSIsEquivalentToRef) TEST_F(Idr, IdrIterationWithComplexSubspaceMultipleRHSIsEquivalentToRef) { - initialize_data(123, 6); - dpcpp_idr_factory = + initialize_data(123, 16); + exec_idr_factory = Solver::build() .with_deterministic(true) .with_complex_subspace(true) .with_criteria( - gko::stop::Iteration::build().with_max_iters(1u).on(dpcpp)) - .on(dpcpp); + gko::stop::Iteration::build().with_max_iters(1u).on(exec)) + .on(exec); ref_idr_factory = Solver::build() .with_deterministic(true) @@ -357,15 +342,12 @@ TEST_F(Idr, IdrIterationWithComplexSubspaceMultipleRHSIsEquivalentToRef) .with_criteria( gko::stop::Iteration::build().with_max_iters(1u).on(ref)) .on(ref); - auto dpcpp_solver = dpcpp_idr_factory->generate(d_mtx); + auto exec_solver = exec_idr_factory->generate(d_mtx); auto ref_solver = ref_idr_factory->generate(mtx); ref_solver->apply(b.get(), x.get()); - dpcpp_solver->apply(d_b.get(), d_x.get()); + exec_solver->apply(d_b.get(), d_x.get()); - GKO_ASSERT_MTX_NEAR(d_b, b, rr::value * 10); - GKO_ASSERT_MTX_NEAR(d_x, x, rr::value * 10); + GKO_ASSERT_MTX_NEAR(d_b, b, rr::value * 100); + GKO_ASSERT_MTX_NEAR(d_x, x, rr::value * 100); } - - -} // namespace diff --git a/test/solver/ir_kernels.cpp b/test/solver/ir_kernels.cpp index 8235ceca13d..43e7a6fd098 100644 --- a/test/solver/ir_kernels.cpp +++ b/test/solver/ir_kernels.cpp @@ -52,26 +52,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -namespace { - - -class Ir : public ::testing::Test { +class Ir : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif using Mtx = gko::matrix::Dense; Ir() : rand_engine(30) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - std::unique_ptr gen_mtx(gko::size_type num_rows, gko::size_type num_cols, gko::size_type stride) { @@ -84,9 +70,6 @@ class Ir : public ::testing::Test { return result; } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; }; @@ -260,6 +243,3 @@ TEST_F(Ir, RichardsonApplyWithIterativeInnerSolverIsEquivalentToRef) // difference in IR. GKO_ASSERT_MTX_NEAR(d_x, x, r::value * 200); } - - -} // namespace diff --git a/test/solver/lower_trs_kernels.cpp b/test/solver/lower_trs_kernels.cpp index d9e3a6a0036..d437a188d50 100644 --- a/test/solver/lower_trs_kernels.cpp +++ b/test/solver/lower_trs_kernels.cpp @@ -49,29 +49,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -class LowerTrs : public ::testing::Test { +class LowerTrs : public CommonTestFixture { protected: - using value_type = double; - using index_type = int; using mtx_type = gko::matrix::Csr; using vec_type = gko::matrix::Dense<>; using solver_type = gko::solver::LowerTrs; LowerTrs() : rand_engine(30) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - std::unique_ptr gen_vec(int num_rows, int num_cols) { return gko::test::generate_random_matrix( @@ -119,8 +104,6 @@ class LowerTrs : public ::testing::Test { std::shared_ptr dx; std::shared_ptr dmtx; std::shared_ptr dmtx_l; - std::shared_ptr ref; - std::shared_ptr exec; std::default_random_engine rand_engine; }; diff --git a/omp/test/solver/multigrid_kernels.cpp b/test/solver/multigrid_kernels.cpp similarity index 70% rename from omp/test/solver/multigrid_kernels.cpp rename to test/solver/multigrid_kernels.cpp index 8bc8afedd66..ba7941d67af 100644 --- a/omp/test/solver/multigrid_kernels.cpp +++ b/test/solver/multigrid_kernels.cpp @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include +#include "core/solver/multigrid_kernels.hpp" #include @@ -47,31 +47,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include "core/solver/multigrid_kernels.hpp" #include "core/test/utils.hpp" +#include "test/utils/executor.hpp" -namespace { - - -class Multigrid : public ::testing::Test { +class Multigrid : public CommonTestFixture { protected: using Mtx = gko::matrix::Dense<>; Multigrid() : rand_engine(30) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - omp = gko::OmpExecutor::create(); - } - - void TearDown() - { - if (omp != nullptr) { - ASSERT_NO_THROW(omp->synchronize()); - } - } - std::unique_ptr gen_mtx(int num_rows, int num_cols) { return gko::test::generate_random_matrix( @@ -98,28 +82,17 @@ class Multigrid : public ::testing::Test { this->modify_norm(old_norm, new_norm); this->modify_scalar(alpha, rho, beta, gamma, zeta); - d_v = Mtx::create(omp); - d_v->copy_from(v.get()); - d_d = Mtx::create(omp); - d_d->copy_from(d.get()); - d_g = Mtx::create(omp); - d_g->copy_from(g.get()); - d_e = Mtx::create(omp); - d_e->copy_from(e.get()); - d_alpha = Mtx::create(omp); - d_alpha->copy_from(alpha.get()); - d_rho = Mtx::create(omp); - d_rho->copy_from(rho.get()); - d_beta = Mtx::create(omp); - d_beta->copy_from(beta.get()); - d_gamma = Mtx::create(omp); - d_gamma->copy_from(gamma.get()); - d_zeta = Mtx::create(omp); - d_zeta->copy_from(zeta.get()); - d_old_norm = Mtx::create(omp); - d_old_norm->copy_from(old_norm.get()); - d_new_norm = Mtx::create(omp); - d_new_norm->copy_from(new_norm.get()); + d_v = gko::clone(exec, v); + d_d = gko::clone(exec, d); + d_g = gko::clone(exec, g); + d_e = gko::clone(exec, e); + d_alpha = gko::clone(exec, alpha); + d_rho = gko::clone(exec, rho); + d_beta = gko::clone(exec, beta); + d_gamma = gko::clone(exec, gamma); + d_zeta = gko::clone(exec, zeta); + d_old_norm = gko::clone(exec, old_norm); + d_new_norm = gko::clone(exec, new_norm); } void modify_norm(std::unique_ptr& old_norm, @@ -165,9 +138,6 @@ class Multigrid : public ::testing::Test { zeta->at(0, 2) = 2.0; } - std::shared_ptr ref; - std::shared_ptr omp; - std::default_random_engine rand_engine; std::unique_ptr v; @@ -196,16 +166,15 @@ class Multigrid : public ::testing::Test { }; -TEST_F(Multigrid, OmpMultigridKCycleStep1IsEquivalentToRef) +TEST_F(Multigrid, MultigridKCycleStep1IsEquivalentToRef) { initialize_data(); gko::kernels::reference::multigrid::kcycle_step_1( - ref, gko::lend(alpha), gko::lend(rho), gko::lend(v), gko::lend(g), - gko::lend(d), gko::lend(e)); - gko::kernels::omp::multigrid::kcycle_step_1( - omp, gko::lend(d_alpha), gko::lend(d_rho), gko::lend(d_v), - gko::lend(d_g), gko::lend(d_d), gko::lend(d_e)); + ref, alpha.get(), rho.get(), v.get(), g.get(), d.get(), e.get()); + gko::kernels::EXEC_NAMESPACE::multigrid::kcycle_step_1( + exec, d_alpha.get(), d_rho.get(), d_v.get(), d_g.get(), d_d.get(), + d_e.get()); GKO_ASSERT_MTX_NEAR(d_g, g, 1e-14); GKO_ASSERT_MTX_NEAR(d_d, d, 1e-14); @@ -213,22 +182,22 @@ TEST_F(Multigrid, OmpMultigridKCycleStep1IsEquivalentToRef) } -TEST_F(Multigrid, OmpMultigridKCycleStep2IsEquivalentToRef) +TEST_F(Multigrid, MultigridKCycleStep2IsEquivalentToRef) { initialize_data(); gko::kernels::reference::multigrid::kcycle_step_2( - ref, gko::lend(alpha), gko::lend(rho), gko::lend(gamma), - gko::lend(beta), gko::lend(zeta), gko::lend(d), gko::lend(e)); - gko::kernels::omp::multigrid::kcycle_step_2( - omp, gko::lend(d_alpha), gko::lend(d_rho), gko::lend(d_gamma), - gko::lend(d_beta), gko::lend(d_zeta), gko::lend(d_d), gko::lend(d_e)); + ref, alpha.get(), rho.get(), gamma.get(), beta.get(), zeta.get(), + d.get(), e.get()); + gko::kernels::EXEC_NAMESPACE::multigrid::kcycle_step_2( + exec, d_alpha.get(), d_rho.get(), d_gamma.get(), d_beta.get(), + d_zeta.get(), d_d.get(), d_e.get()); GKO_ASSERT_MTX_NEAR(d_e, e, 1e-14); } -TEST_F(Multigrid, OmpMultigridKCycleCheckStopIsEquivalentToRef) +TEST_F(Multigrid, MultigridKCycleCheckStopIsEquivalentToRef) { initialize_data(); bool is_stop_10; @@ -237,19 +206,16 @@ TEST_F(Multigrid, OmpMultigridKCycleCheckStopIsEquivalentToRef) bool d_is_stop_5; gko::kernels::reference::multigrid::kcycle_check_stop( - ref, gko::lend(old_norm), gko::lend(new_norm), 1.0, is_stop_10); - gko::kernels::omp::multigrid::kcycle_check_stop( - omp, gko::lend(d_old_norm), gko::lend(d_new_norm), 1.0, d_is_stop_10); + ref, old_norm.get(), new_norm.get(), 1.0, is_stop_10); + gko::kernels::EXEC_NAMESPACE::multigrid::kcycle_check_stop( + exec, d_old_norm.get(), d_new_norm.get(), 1.0, d_is_stop_10); gko::kernels::reference::multigrid::kcycle_check_stop( - ref, gko::lend(old_norm), gko::lend(new_norm), 0.5, is_stop_5); - gko::kernels::omp::multigrid::kcycle_check_stop( - omp, gko::lend(d_old_norm), gko::lend(d_new_norm), 0.5, d_is_stop_5); + ref, old_norm.get(), new_norm.get(), 0.5, is_stop_5); + gko::kernels::EXEC_NAMESPACE::multigrid::kcycle_check_stop( + exec, d_old_norm.get(), d_new_norm.get(), 0.5, d_is_stop_5); GKO_ASSERT_EQ(d_is_stop_10, is_stop_10); GKO_ASSERT_EQ(d_is_stop_10, true); GKO_ASSERT_EQ(d_is_stop_5, is_stop_5); GKO_ASSERT_EQ(d_is_stop_5, false); } - - -} // namespace diff --git a/test/solver/solver.cpp b/test/solver/solver.cpp index 8d81b6efe1f..bbda5153de7 100644 --- a/test/solver/solver.cpp +++ b/test/solver/solver.cpp @@ -62,11 +62,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -#if GINKGO_DPCPP_SINGLE_MODE +#if GINKGO_COMMON_SINGLE_MODE using solver_value_type = float; #else using solver_value_type = double; -#endif // GINKGO_DPCPP_SINGLE_MODE +#endif // GINKGO_COMMON_SINGLE_MODE template @@ -513,34 +513,23 @@ class FailOnAllocationFreeLogger : public gko::log::Logger { template -class Solver : public ::testing::Test { +class Solver : public CommonTestFixture { protected: using Config = T; using SolverType = typename T::solver_type; using Precond = typename T::precond_type; using Mtx = typename T::matrix_type; - using index_type = gko::int32; using value_type = typename Mtx::value_type; using mixed_value_type = gko::next_precision; using Vec = gko::matrix::Dense; using MixedVec = gko::matrix::Dense; - Solver() { reset_rand(); } - - void SetUp() + Solver() { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); + reset_rand(); logger = std::make_shared(); } - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - void reset_rand() { rand_engine.seed(15); } test_pair gen_mtx(int num_rows, int num_cols, int min_cols, @@ -607,23 +596,15 @@ class Solver : public ::testing::Test { template double tol(const test_pair& x) { - return Config::tolerance() * - std::sqrt(x.ref->get_size()[1] * - (gko::is_complex() - ? 2.0 - : 1.0)); + return Config::tolerance() * std::sqrt(x.ref->get_size()[1]); } template double mixed_tol(const test_pair& x) { - return std::max( - r_mixed() * - std::sqrt(x.ref->get_size()[1] * - (gko::is_complex() - ? 2.0 - : 1.0)), - tol(x)); + return std::max(r_mixed() * + std::sqrt(x.ref->get_size()[1]), + tol(x)); } template @@ -795,8 +776,6 @@ class Solver : public ::testing::Test { ASSERT_EQ(Config::get_preconditioner(solver), nullptr); } - std::shared_ptr ref; - std::shared_ptr exec; std::shared_ptr logger; std::default_random_engine rand_engine; diff --git a/test/solver/upper_trs_kernels.cpp b/test/solver/upper_trs_kernels.cpp index 43d7f7b6988..141ab78c86c 100644 --- a/test/solver/upper_trs_kernels.cpp +++ b/test/solver/upper_trs_kernels.cpp @@ -49,29 +49,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "test/utils/executor.hpp" -class UpperTrs : public ::testing::Test { +class UpperTrs : public CommonTestFixture { protected: - using value_type = double; - using index_type = int; using mtx_type = gko::matrix::Csr; using vec_type = gko::matrix::Dense<>; using solver_type = gko::solver::UpperTrs; UpperTrs() : rand_engine(30) {} - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } - std::unique_ptr gen_vec(int num_rows, int num_cols) { return gko::test::generate_random_matrix( @@ -119,8 +104,6 @@ class UpperTrs : public ::testing::Test { std::shared_ptr dx; std::shared_ptr dmtx; std::shared_ptr dmtx_u; - std::shared_ptr ref; - std::shared_ptr exec; std::default_random_engine rand_engine; }; diff --git a/test/stop/CMakeLists.txt b/test/stop/CMakeLists.txt new file mode 100644 index 00000000000..51c9df2f508 --- /dev/null +++ b/test/stop/CMakeLists.txt @@ -0,0 +1,2 @@ +ginkgo_create_common_test(criterion_kernels) +ginkgo_create_common_test(residual_norm_kernels) diff --git a/omp/test/stop/criterion_kernels.cpp b/test/stop/criterion_kernels.cpp similarity index 81% rename from omp/test/stop/criterion_kernels.cpp rename to test/stop/criterion_kernels.cpp index 35540d7e9b1..1465ab2ff59 100644 --- a/omp/test/stop/criterion_kernels.cpp +++ b/test/stop/criterion_kernels.cpp @@ -30,35 +30,31 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include - - #include +#include #include -namespace { +#include "test/utils/executor.hpp" constexpr gko::size_type test_iterations = 10; -class Criterion : public ::testing::Test { +class Criterion : public CommonTestFixture { protected: Criterion() { - omp_ = gko::OmpExecutor::create(); // Actually use an iteration stopping criterion because Criterion is an // abstract class - factory_ = gko::stop::Iteration::build() - .with_max_iters(test_iterations) - .on(omp_); + factory = gko::stop::Iteration::build() + .with_max_iters(test_iterations) + .on(exec); } - std::unique_ptr factory_; - std::shared_ptr omp_; + std::unique_ptr factory; }; @@ -66,13 +62,15 @@ TEST_F(Criterion, SetsOneStopStatus) { bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - auto criterion = factory_->generate(nullptr, nullptr, nullptr); - gko::array stop_status(omp_, 1); + auto criterion = factory->generate(nullptr, nullptr, nullptr); + gko::array stop_status(ref, 1); stop_status.get_data()[0].reset(); + stop_status.set_executor(exec); criterion->update() .num_iterations(test_iterations) .check(RelativeStoppingId, true, &stop_status, &one_changed); + stop_status.set_executor(ref); ASSERT_EQ(stop_status.get_data()[0].has_stopped(), true); } @@ -82,20 +80,19 @@ TEST_F(Criterion, SetsMultipleStopStatuses) { bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - auto criterion = factory_->generate(nullptr, nullptr, nullptr); - gko::array stop_status(omp_, 3); + auto criterion = factory->generate(nullptr, nullptr, nullptr); + gko::array stop_status(ref, 3); stop_status.get_data()[0].reset(); stop_status.get_data()[1].reset(); stop_status.get_data()[2].reset(); + stop_status.set_executor(exec); criterion->update() .num_iterations(test_iterations) .check(RelativeStoppingId, true, &stop_status, &one_changed); + stop_status.set_executor(ref); ASSERT_EQ(stop_status.get_data()[0].has_stopped(), true); ASSERT_EQ(stop_status.get_data()[1].has_stopped(), true); ASSERT_EQ(stop_status.get_data()[2].has_stopped(), true); } - - -} // namespace diff --git a/omp/test/stop/residual_norm_kernels.cpp b/test/stop/residual_norm_kernels.cpp similarity index 57% rename from omp/test/stop/residual_norm_kernels.cpp rename to test/stop/residual_norm_kernels.cpp index 5cf628817ba..f83b69ba7b9 100644 --- a/omp/test/stop/residual_norm_kernels.cpp +++ b/test/stop/residual_norm_kernels.cpp @@ -30,47 +30,60 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *************************************************************/ -#include - - #include #include +#include #include "core/test/utils.hpp" +#include "test/utils/executor.hpp" -namespace { +template +void write(std::unique_ptr& mtx, gko::size_type row, gko::size_type col, + typename Mtx::value_type val) +{ + mtx->get_executor()->copy_from( + mtx->get_executor()->get_master().get(), 1, &val, + mtx->get_values() + col + mtx->get_stride() * row); +} + + +template +typename Mtx::value_type read(std::unique_ptr& mtx, gko::size_type row, + gko::size_type col) +{ + return mtx->get_executor()->copy_val_to_host(mtx->get_values() + col + + mtx->get_stride() * row); +} template -class ResidualNorm : public ::testing::Test { +class ResidualNorm : public CommonTestFixture { protected: using Mtx = gko::matrix::Dense; using NormVector = gko::matrix::Dense>; ResidualNorm() { - exec_ = gko::OmpExecutor::create(); - factory_ = gko::stop::ResidualNorm::build() - .with_reduction_factor(r::value) - .on(exec_); - rel_factory_ = gko::stop::ResidualNorm::build() - .with_reduction_factor(r::value) - .with_baseline(gko::stop::mode::initial_resnorm) - .on(exec_); - abs_factory_ = gko::stop::ResidualNorm::build() - .with_reduction_factor(r::value) - .with_baseline(gko::stop::mode::absolute) - .on(exec_); + factory = gko::stop::ResidualNorm::build() + .with_reduction_factor(r::value) + .on(exec); + rel_factory = gko::stop::ResidualNorm::build() + .with_reduction_factor(r::value) + .with_baseline(gko::stop::mode::initial_resnorm) + .on(exec); + abs_factory = gko::stop::ResidualNorm::build() + .with_reduction_factor(r::value) + .with_baseline(gko::stop::mode::absolute) + .on(exec); } - std::unique_ptr::Factory> factory_; - std::unique_ptr::Factory> rel_factory_; - std::unique_ptr::Factory> abs_factory_; - std::shared_ptr exec_; + std::unique_ptr::Factory> factory; + std::unique_ptr::Factory> rel_factory; + std::unique_ptr::Factory> abs_factory; }; TYPED_TEST_SUITE(ResidualNorm, gko::test::ValueTypes, TypenameNameGenerator); @@ -80,100 +93,114 @@ TYPED_TEST(ResidualNorm, WaitsTillResidualGoal) { using Mtx = typename TestFixture::Mtx; using NormVector = typename TestFixture::NormVector; - using T_nc = gko::remove_complex; - auto initial_res = gko::initialize({100.0}, this->exec_); - std::shared_ptr rhs = gko::initialize({10.0}, this->exec_); + auto initial_res = gko::initialize({100.0}, this->exec); + std::shared_ptr rhs = gko::initialize({10.0}, this->exec); auto criterion = - this->factory_->generate(nullptr, rhs, nullptr, initial_res.get()); + this->factory->generate(nullptr, rhs, nullptr, initial_res.get()); auto rel_criterion = - this->rel_factory_->generate(nullptr, rhs, nullptr, initial_res.get()); + this->rel_factory->generate(nullptr, rhs, nullptr, initial_res.get()); auto abs_criterion = - this->abs_factory_->generate(nullptr, rhs, nullptr, initial_res.get()); + this->abs_factory->generate(nullptr, rhs, nullptr, initial_res.get()); { - auto res_norm = gko::initialize({10.0}, this->exec_); - auto rhs_norm = gko::initialize({100.0}, this->exec_); + auto res_norm = gko::initialize({10.0}, this->exec); + auto rhs_norm = gko::initialize({100.0}, this->exec); gko::as(rhs)->compute_norm2(rhs_norm.get()); constexpr gko::uint8 RelativeStoppingId{1}; bool one_changed{}; - gko::array stop_status(this->exec_, 1); + gko::array stop_status(this->ref, 1); stop_status.get_data()[0].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0) = r::value * 1.1 * res_norm->at(0); + write(res_norm, 0, 0, r::value * 1.1 * read(res_norm, 0, 0)); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); - ASSERT_EQ(one_changed, false); + stop_status.set_executor(this->ref); + ASSERT_FALSE(stop_status.get_data()[0].has_converged()); + ASSERT_FALSE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0) = r::value * 0.9 * res_norm->at(0); + write(res_norm, 0, 0, r::value * 0.9 * read(res_norm, 0, 0)); ASSERT_TRUE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } { - auto res_norm = gko::initialize({100.0}, this->exec_); + auto res_norm = gko::initialize({100.0}, this->exec); constexpr gko::uint8 RelativeStoppingId{1}; bool one_changed{}; - gko::array stop_status(this->exec_, 1); + gko::array stop_status(this->ref, 1); stop_status.get_data()[0].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( rel_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0) = r::value * 1.1 * res_norm->at(0); + write(res_norm, 0, 0, r::value * 1.1 * read(res_norm, 0, 0)); ASSERT_FALSE( rel_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); - ASSERT_EQ(one_changed, false); + stop_status.set_executor(this->ref); + ASSERT_FALSE(stop_status.get_data()[0].has_converged()); + ASSERT_FALSE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0) = r::value * 0.9 * res_norm->at(0); + write(res_norm, 0, 0, r::value * 0.9 * read(res_norm, 0, 0)); ASSERT_TRUE( rel_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } { - auto res_norm = gko::initialize({100.0}, this->exec_); + auto res_norm = gko::initialize({100.0}, this->exec); constexpr gko::uint8 RelativeStoppingId{1}; bool one_changed{}; - gko::array stop_status(this->exec_, 1); + gko::array stop_status(this->ref, 1); stop_status.get_data()[0].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( abs_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0) = r::value * 1.1; + write(res_norm, 0, 0, r::value * 1.1); ASSERT_FALSE( abs_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); - ASSERT_EQ(one_changed, false); + stop_status.set_executor(this->ref); + ASSERT_FALSE(stop_status.get_data()[0].has_converged()); + ASSERT_FALSE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0) = r::value * 0.9; + write(res_norm, 0, 0, r::value * 0.9); ASSERT_TRUE( abs_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } } @@ -184,127 +211,140 @@ TYPED_TEST(ResidualNorm, WaitsTillResidualGoalMultipleRHS) using NormVector = typename TestFixture::NormVector; using T = TypeParam; using T_nc = gko::remove_complex; - auto res = gko::initialize({I{100.0, 100.0}}, this->exec_); + auto res = gko::initialize({I{100.0, 100.0}}, this->exec); std::shared_ptr rhs = - gko::initialize({I{10.0, 10.0}}, this->exec_); - auto criterion = this->factory_->generate(nullptr, rhs, nullptr, res.get()); + gko::initialize({I{10.0, 10.0}}, this->exec); + auto criterion = this->factory->generate(nullptr, rhs, nullptr, res.get()); auto rel_criterion = - this->rel_factory_->generate(nullptr, rhs, nullptr, res.get()); + this->rel_factory->generate(nullptr, rhs, nullptr, res.get()); auto abs_criterion = - this->abs_factory_->generate(nullptr, rhs, nullptr, res.get()); + this->abs_factory->generate(nullptr, rhs, nullptr, res.get()); { auto res_norm = - gko::initialize({I{100.0, 100.0}}, this->exec_); + gko::initialize({I{100.0, 100.0}}, this->exec); auto rhs_norm = - gko::initialize({I{100.0, 100.0}}, this->exec_); + gko::initialize({I{100.0, 100.0}}, this->exec); gko::as(rhs)->compute_norm2(rhs_norm.get()); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 2); + gko::array stop_status(this->ref, 2); stop_status.get_data()[0].reset(); stop_status.get_data()[1].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0, 0) = r::value * 0.9 * rhs_norm->at(0, 0); + write(res_norm, 0, 0, r::value * 0.9 * read(rhs_norm, 0, 0)); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0, 1) = r::value * 0.9 * rhs_norm->at(0, 1); + write(res_norm, 0, 1, r::value * 0.9 * read(rhs_norm, 0, 1)); ASSERT_TRUE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[1].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[1].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } { auto res_norm = - gko::initialize({I{100.0, 100.0}}, this->exec_); + gko::initialize({I{100.0, 100.0}}, this->exec); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 2); + gko::array stop_status(this->ref, 2); stop_status.get_data()[0].reset(); stop_status.get_data()[1].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( rel_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0, 0) = r::value * 0.9 * res_norm->at(0, 0); + write(res_norm, 0, 0, r::value * 0.9 * read(res_norm, 0, 0)); ASSERT_FALSE( rel_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0, 1) = r::value * 0.9 * res_norm->at(0, 1); + write(res_norm, 0, 1, r::value * 0.9 * read(res_norm, 0, 1)); ASSERT_TRUE( rel_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[1].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[1].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } { auto res_norm = - gko::initialize({I{100.0, 100.0}}, this->exec_); + gko::initialize({I{100.0, 100.0}}, this->exec); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 2); + gko::array stop_status(this->ref, 2); stop_status.get_data()[0].reset(); stop_status.get_data()[1].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( abs_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0, 0) = r::value * 0.9; + write(res_norm, 0, 0, r::value * 0.9); ASSERT_FALSE( abs_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0, 1) = r::value * 0.9; + write(res_norm, 0, 1, r::value * 0.9); ASSERT_TRUE( abs_criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[1].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[1].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } } template -class ResidualNormWithInitialResnorm : public ::testing::Test { +class ResidualNormWithInitialResnorm : public CommonTestFixture { protected: using Mtx = gko::matrix::Dense; using NormVector = gko::matrix::Dense>; ResidualNormWithInitialResnorm() { - exec_ = gko::OmpExecutor::create(); - factory_ = gko::stop::ResidualNorm::build() - .with_baseline(gko::stop::mode::initial_resnorm) - .with_reduction_factor(r::value) - .on(exec_); + factory = gko::stop::ResidualNorm::build() + .with_baseline(gko::stop::mode::initial_resnorm) + .with_reduction_factor(r::value) + .on(exec); } - std::unique_ptr::Factory> factory_; - std::shared_ptr exec_; + std::unique_ptr::Factory> factory; }; TYPED_TEST_SUITE(ResidualNormWithInitialResnorm, gko::test::ValueTypes, @@ -315,36 +355,41 @@ TYPED_TEST(ResidualNormWithInitialResnorm, WaitsTillResidualGoal) { using Mtx = typename TestFixture::Mtx; using NormVector = typename TestFixture::NormVector; - auto initial_res = gko::initialize({100.0}, this->exec_); - std::shared_ptr rhs = gko::initialize({10.0}, this->exec_); - auto res_norm = gko::initialize({100.0}, this->exec_); + auto initial_res = gko::initialize({100.0}, this->exec); + std::shared_ptr rhs = gko::initialize({10.0}, this->exec); + auto res_norm = gko::initialize({100.0}, this->exec); auto criterion = - this->factory_->generate(nullptr, rhs, nullptr, initial_res.get()); + this->factory->generate(nullptr, rhs, nullptr, initial_res.get()); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 1); + gko::array stop_status(this->ref, 1); stop_status.get_data()[0].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0) = r::value * 1.1 * res_norm->at(0); + write(res_norm, 0, 0, r::value * 1.1 * read(res_norm, 0, 0)); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); - ASSERT_EQ(one_changed, false); + stop_status.set_executor(this->ref); + ASSERT_FALSE(stop_status.get_data()[0].has_converged()); + ASSERT_FALSE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0) = r::value * 0.9 * res_norm->at(0); + write(res_norm, 0, 0, r::value * 0.9 * read(res_norm, 0, 0)); ASSERT_TRUE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } @@ -354,58 +399,61 @@ TYPED_TEST(ResidualNormWithInitialResnorm, WaitsTillResidualGoalMultipleRHS) using NormVector = typename TestFixture::NormVector; using T = TypeParam; using T_nc = gko::remove_complex; - auto res = gko::initialize({I{100.0, 100.0}}, this->exec_); + auto res = gko::initialize({I{100.0, 100.0}}, this->exec); auto res_norm = - gko::initialize({I{100.0, 100.0}}, this->exec_); + gko::initialize({I{100.0, 100.0}}, this->exec); std::shared_ptr rhs = - gko::initialize({I{10.0, 10.0}}, this->exec_); - auto criterion = this->factory_->generate(nullptr, rhs, nullptr, res.get()); + gko::initialize({I{10.0, 10.0}}, this->exec); + auto criterion = this->factory->generate(nullptr, rhs, nullptr, res.get()); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 2); + gko::array stop_status(this->ref, 2); stop_status.get_data()[0].reset(); stop_status.get_data()[1].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0, 0) = r::value * 0.9 * res_norm->at(0, 0); + write(res_norm, 0, 0, r::value * 0.9 * read(res_norm, 0, 0)); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0, 1) = r::value * 0.9 * res_norm->at(0, 1); + write(res_norm, 0, 1, r::value * 0.9 * read(res_norm, 0, 1)); ASSERT_TRUE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[1].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[1].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } template -class ResidualNormWithRhsNorm : public ::testing::Test { +class ResidualNormWithRhsNorm : public CommonTestFixture { protected: using Mtx = gko::matrix::Dense; using NormVector = gko::matrix::Dense>; ResidualNormWithRhsNorm() { - exec_ = gko::OmpExecutor::create(); - factory_ = gko::stop::ResidualNorm::build() - .with_baseline(gko::stop::mode::rhs_norm) - .with_reduction_factor(r::value) - .on(exec_); + factory = gko::stop::ResidualNorm::build() + .with_baseline(gko::stop::mode::rhs_norm) + .with_reduction_factor(r::value) + .on(exec); } - std::unique_ptr::Factory> factory_; - std::shared_ptr exec_; + std::unique_ptr::Factory> factory; }; TYPED_TEST_SUITE(ResidualNormWithRhsNorm, gko::test::ValueTypes, @@ -418,38 +466,43 @@ TYPED_TEST(ResidualNormWithRhsNorm, WaitsTillResidualGoal) using T_nc = gko::remove_complex; using Mtx = typename TestFixture::Mtx; using NormVector = typename TestFixture::NormVector; - auto initial_res = gko::initialize({100.0}, this->exec_); - std::shared_ptr rhs = gko::initialize({10.0}, this->exec_); - auto rhs_norm = gko::initialize({I{0.0}}, this->exec_); + auto initial_res = gko::initialize({100.0}, this->exec); + std::shared_ptr rhs = gko::initialize({10.0}, this->exec); + auto rhs_norm = gko::initialize({I{0.0}}, this->exec); gko::as(rhs)->compute_norm2(rhs_norm.get()); - auto res_norm = gko::initialize({100.0}, this->exec_); + auto res_norm = gko::initialize({100.0}, this->exec); auto criterion = - this->factory_->generate(nullptr, rhs, nullptr, initial_res.get()); + this->factory->generate(nullptr, rhs, nullptr, initial_res.get()); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 1); + gko::array stop_status(this->ref, 1); stop_status.get_data()[0].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0) = r::value * 1.1 * rhs_norm->at(0); + write(res_norm, 0, 0, r::value * 1.1 * read(rhs_norm, 0, 0)); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); - ASSERT_EQ(one_changed, false); + stop_status.set_executor(this->ref); + ASSERT_FALSE(stop_status.get_data()[0].has_converged()); + ASSERT_FALSE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0) = r::value * 0.9 * rhs_norm->at(0); + write(res_norm, 0, 0, r::value * 0.9 * read(rhs_norm, 0, 0)); ASSERT_TRUE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } @@ -459,61 +512,64 @@ TYPED_TEST(ResidualNormWithRhsNorm, WaitsTillResidualGoalMultipleRHS) using NormVector = typename TestFixture::NormVector; using T = TypeParam; using T_nc = gko::remove_complex; - auto res = gko::initialize({I{100.0, 100.0}}, this->exec_); + auto res = gko::initialize({I{100.0, 100.0}}, this->exec); auto res_norm = - gko::initialize({I{100.0, 100.0}}, this->exec_); + gko::initialize({I{100.0, 100.0}}, this->exec); std::shared_ptr rhs = - gko::initialize({I{10.0, 10.0}}, this->exec_); + gko::initialize({I{10.0, 10.0}}, this->exec); auto rhs_norm = - gko::initialize({I{0.0, 0.0}}, this->exec_); + gko::initialize({I{0.0, 0.0}}, this->exec); gko::as(rhs)->compute_norm2(rhs_norm.get()); - auto criterion = this->factory_->generate(nullptr, rhs, nullptr, res.get()); + auto criterion = this->factory->generate(nullptr, rhs, nullptr, res.get()); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 2); + gko::array stop_status(this->ref, 2); stop_status.get_data()[0].reset(); stop_status.get_data()[1].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0, 0) = r::value * 0.9 * rhs_norm->at(0, 0); + write(res_norm, 0, 0, r::value * 0.9 * read(rhs_norm, 0, 0)); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0, 1) = r::value * 0.9 * rhs_norm->at(0, 1); + write(res_norm, 0, 1, r::value * 0.9 * read(rhs_norm, 0, 1)); ASSERT_TRUE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[1].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[1].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } template -class ImplicitResidualNorm : public ::testing::Test { +class ImplicitResidualNorm : public CommonTestFixture { protected: using Mtx = gko::matrix::Dense; using NormVector = gko::matrix::Dense>; ImplicitResidualNorm() { - exec_ = gko::OmpExecutor::create(); - factory_ = gko::stop::ImplicitResidualNorm::build() - .with_reduction_factor(r::value) - .on(exec_); + factory = gko::stop::ImplicitResidualNorm::build() + .with_reduction_factor(r::value) + .on(exec); } std::unique_ptr::Factory> - factory_; - std::shared_ptr exec_; + factory; }; TYPED_TEST_SUITE(ImplicitResidualNorm, gko::test::ValueTypes, @@ -526,38 +582,45 @@ TYPED_TEST(ImplicitResidualNorm, WaitsTillResidualGoal) using T_nc = gko::remove_complex; using Mtx = typename TestFixture::Mtx; using NormVector = typename TestFixture::NormVector; - auto initial_res = gko::initialize({100.0}, this->exec_); - std::shared_ptr rhs = gko::initialize({10.0}, this->exec_); - auto res_norm = gko::initialize({100.0}, this->exec_); - auto rhs_norm = gko::initialize({I{0.0}}, this->exec_); + auto initial_res = gko::initialize({100.0}, this->exec); + std::shared_ptr rhs = gko::initialize({10.0}, this->exec); + auto res_norm = gko::initialize({100.0}, this->exec); + auto rhs_norm = gko::initialize({I{0.0}}, this->exec); gko::as(rhs)->compute_norm2(rhs_norm.get()); auto criterion = - this->factory_->generate(nullptr, rhs, nullptr, initial_res.get()); + this->factory->generate(nullptr, rhs, nullptr, initial_res.get()); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 1); + gko::array stop_status(this->ref, 1); stop_status.get_data()[0].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .implicit_sq_residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0) = std::pow(r::value * 1.1 * rhs_norm->at(0), 2); + write(res_norm, 0, 0, + std::pow(r::value * 1.1 * read(rhs_norm, 0, 0), 2)); ASSERT_FALSE( criterion->update() .implicit_sq_residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); - ASSERT_EQ(one_changed, false); + stop_status.set_executor(this->ref); + ASSERT_FALSE(stop_status.get_data()[0].has_converged()); + ASSERT_FALSE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0) = std::pow(r::value * 0.9 * rhs_norm->at(0), 2); + write(res_norm, 0, 0, + std::pow(r::value * 0.9 * read(rhs_norm, 0, 0), 2)); ASSERT_TRUE( criterion->update() .implicit_sq_residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } @@ -567,62 +630,65 @@ TYPED_TEST(ImplicitResidualNorm, WaitsTillResidualGoalMultipleRHS) using NormVector = typename TestFixture::NormVector; using T = TypeParam; using T_nc = gko::remove_complex; - auto res = gko::initialize({I{100.0, 100.0}}, this->exec_); - auto res_norm = gko::initialize({I{100.0, 100.0}}, this->exec_); + auto res = gko::initialize({I{100.0, 100.0}}, this->exec); + auto res_norm = gko::initialize({I{100.0, 100.0}}, this->exec); std::shared_ptr rhs = - gko::initialize({I{10.0, 10.0}}, this->exec_); + gko::initialize({I{10.0, 10.0}}, this->exec); auto rhs_norm = - gko::initialize({I{0.0, 0.0}}, this->exec_); + gko::initialize({I{0.0, 0.0}}, this->exec); gko::as(rhs)->compute_norm2(rhs_norm.get()); - auto criterion = this->factory_->generate(nullptr, rhs, nullptr, res.get()); + auto criterion = this->factory->generate(nullptr, rhs, nullptr, res.get()); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 2); + gko::array stop_status(this->ref, 2); stop_status.get_data()[0].reset(); stop_status.get_data()[1].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .implicit_sq_residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0, 0) = - std::pow(r::value * 0.9 * rhs_norm->at(0, 0), 2); + write(res_norm, 0, 0, + std::pow(r::value * 0.9 * read(rhs_norm, 0, 0), 2)); ASSERT_FALSE( criterion->update() .implicit_sq_residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0, 1) = - std::pow(r::value * 0.9 * rhs_norm->at(0, 1), 2); + write(res_norm, 0, 1, + std::pow(r::value * 0.9 * read(rhs_norm, 0, 1), 2)); ASSERT_TRUE( criterion->update() .implicit_sq_residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[1].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[1].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } template -class ResidualNormWithAbsolute : public ::testing::Test { +class ResidualNormWithAbsolute : public CommonTestFixture { protected: using Mtx = gko::matrix::Dense; using NormVector = gko::matrix::Dense>; ResidualNormWithAbsolute() { - exec_ = gko::OmpExecutor::create(); - factory_ = gko::stop::ResidualNorm::build() - .with_baseline(gko::stop::mode::absolute) - .with_reduction_factor(r::value) - .on(exec_); + factory = gko::stop::ResidualNorm::build() + .with_baseline(gko::stop::mode::absolute) + .with_reduction_factor(r::value) + .on(exec); } - std::unique_ptr::Factory> factory_; - std::shared_ptr exec_; + std::unique_ptr::Factory> factory; }; TYPED_TEST_SUITE(ResidualNormWithAbsolute, gko::test::ValueTypes, @@ -633,36 +699,41 @@ TYPED_TEST(ResidualNormWithAbsolute, WaitsTillResidualGoal) { using Mtx = typename TestFixture::Mtx; using NormVector = typename TestFixture::NormVector; - auto initial_res = gko::initialize({100.0}, this->exec_); - std::shared_ptr rhs = gko::initialize({10.0}, this->exec_); - auto res_norm = gko::initialize({100.0}, this->exec_); + auto initial_res = gko::initialize({100.0}, this->exec); + std::shared_ptr rhs = gko::initialize({10.0}, this->exec); + auto res_norm = gko::initialize({100.0}, this->exec); auto criterion = - this->factory_->generate(nullptr, rhs, nullptr, initial_res.get()); + this->factory->generate(nullptr, rhs, nullptr, initial_res.get()); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 1); + gko::array stop_status(this->ref, 1); stop_status.get_data()[0].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0) = r::value * 1.1; + write(res_norm, 0, 0, r::value * 1.1); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), false); - ASSERT_EQ(one_changed, false); + stop_status.set_executor(this->ref); + ASSERT_FALSE(stop_status.get_data()[0].has_converged()); + ASSERT_FALSE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0) = r::value * 0.9; + write(res_norm, 0, 0, r::value * 0.9); ASSERT_TRUE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } @@ -672,39 +743,41 @@ TYPED_TEST(ResidualNormWithAbsolute, WaitsTillResidualGoalMultipleRHS) using NormVector = typename TestFixture::NormVector; using T = TypeParam; using T_nc = gko::remove_complex; - auto res = gko::initialize({I{100.0, 100.0}}, this->exec_); + auto res = gko::initialize({I{100.0, 100.0}}, this->exec); auto res_norm = - gko::initialize({I{100.0, 100.0}}, this->exec_); + gko::initialize({I{100.0, 100.0}}, this->exec); std::shared_ptr rhs = - gko::initialize({I{10.0, 10.0}}, this->exec_); - auto criterion = this->factory_->generate(nullptr, rhs, nullptr, res.get()); + gko::initialize({I{10.0, 10.0}}, this->exec); + auto criterion = this->factory->generate(nullptr, rhs, nullptr, res.get()); bool one_changed{}; constexpr gko::uint8 RelativeStoppingId{1}; - gko::array stop_status(this->exec_, 2); + gko::array stop_status(this->ref, 2); stop_status.get_data()[0].reset(); stop_status.get_data()[1].reset(); + stop_status.set_executor(this->exec); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - res_norm->at(0, 0) = r::value * 0.9; + write(res_norm, 0, 0, r::value * 0.9); ASSERT_FALSE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[0].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[0].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); - res_norm->at(0, 1) = r::value * 0.9; + write(res_norm, 0, 1, r::value * 0.9); ASSERT_TRUE( criterion->update() .residual_norm(res_norm.get()) .check(RelativeStoppingId, true, &stop_status, &one_changed)); - ASSERT_EQ(stop_status.get_data()[1].has_converged(), true); - ASSERT_EQ(one_changed, true); + stop_status.set_executor(this->ref); + ASSERT_TRUE(stop_status.get_data()[1].has_converged()); + ASSERT_TRUE(one_changed); + stop_status.set_executor(this->exec); } - - -} // namespace diff --git a/test/utils/executor.hpp b/test/utils/executor.hpp index a21ce962f4d..ce7ea243a22 100644 --- a/test/utils/executor.hpp +++ b/test/utils/executor.hpp @@ -33,56 +33,111 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef GKO_TEST_UTILS_EXECUTOR_HPP_ #define GKO_TEST_UTILS_EXECUTOR_HPP_ + #include #include +#include #include -void init_executor(std::shared_ptr ref, - std::shared_ptr& exec) +#if GINKGO_COMMON_SINGLE_MODE +#define SKIP_IF_SINGLE_MODE GTEST_SKIP() << "Skip due to single mode" +#else +#define SKIP_IF_SINGLE_MODE \ + static_assert(true, \ + "This assert is used to counter the false positive extra " \ + "semi-colon warnings") +#endif + + +template +std::shared_ptr init_executor( + std::shared_ptr); + + +template <> +inline std::shared_ptr + init_executor( + std::shared_ptr) { - exec = gko::ReferenceExecutor::create(); + return gko::ReferenceExecutor::create(); } -void init_executor(std::shared_ptr ref, - std::shared_ptr& exec) +template <> +inline std::shared_ptr init_executor( + std::shared_ptr) { - exec = gko::OmpExecutor::create(); + return gko::OmpExecutor::create(); } -void init_executor(std::shared_ptr ref, - std::shared_ptr& exec) +template <> +inline std::shared_ptr init_executor( + std::shared_ptr ref) { - ASSERT_GT(gko::CudaExecutor::get_num_devices(), 0); - exec = gko::CudaExecutor::create(0, ref); + { + if (gko::CudaExecutor::get_num_devices() == 0) { + throw std::runtime_error{"No suitable CUDA devices"}; + } + return gko::CudaExecutor::create(0, ref); + } } -void init_executor(std::shared_ptr ref, - std::shared_ptr& exec) +template <> +inline std::shared_ptr init_executor( + std::shared_ptr ref) { - ASSERT_GT(gko::HipExecutor::get_num_devices(), 0); - exec = gko::HipExecutor::create(0, ref); + if (gko::HipExecutor::get_num_devices() == 0) { + throw std::runtime_error{"No suitable HIP devices"}; + } + return gko::HipExecutor::create(0, ref); } -void init_executor(std::shared_ptr ref, - std::shared_ptr& exec) +template <> +inline std::shared_ptr init_executor( + std::shared_ptr ref) { if (gko::DpcppExecutor::get_num_devices("gpu") > 0) { - exec = gko::DpcppExecutor::create(0, ref, "gpu"); + return gko::DpcppExecutor::create(0, ref, "gpu"); } else if (gko::DpcppExecutor::get_num_devices("cpu") > 0) { - exec = gko::DpcppExecutor::create(0, ref, "cpu"); + return gko::DpcppExecutor::create(0, ref, "cpu"); } else { - FAIL() << "No suitable DPC++ devices"; + throw std::runtime_error{"No suitable DPC++ devices"}; } } +class CommonTestFixture : public ::testing::Test { +public: +#if GINKGO_COMMON_SINGLE_MODE + using value_type = float; +#else + using value_type = double; +#endif + using index_type = int; + + CommonTestFixture() + : ref{gko::ReferenceExecutor::create()}, + exec{init_executor(ref)} + {} + + void TearDown() final + { + if (exec != nullptr) { + ASSERT_NO_THROW(exec->synchronize()); + } + } + + std::shared_ptr ref; + std::shared_ptr exec; +}; + + #endif // GKO_TEST_UTILS_EXECUTOR_HPP_