|
| 1 | +/*******************************<GINKGO LICENSE>****************************** |
| 2 | +Copyright (c) 2017-2019, the Ginkgo authors |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions |
| 7 | +are met: |
| 8 | +
|
| 9 | +1. Redistributions of source code must retain the above copyright |
| 10 | +notice, this list of conditions and the following disclaimer. |
| 11 | +
|
| 12 | +2. Redistributions in binary form must reproduce the above copyright |
| 13 | +notice, this list of conditions and the following disclaimer in the |
| 14 | +documentation and/or other materials provided with the distribution. |
| 15 | +
|
| 16 | +3. Neither the name of the copyright holder nor the names of its |
| 17 | +contributors may be used to endorse or promote products derived from |
| 18 | +this software without specific prior written permission. |
| 19 | +
|
| 20 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 21 | +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 23 | +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +******************************<GINKGO LICENSE>*******************************/ |
| 32 | + |
| 33 | +#include "core/factorization/par_ilu_kernels.hpp" |
| 34 | + |
| 35 | + |
| 36 | +#include <algorithm> |
| 37 | +#include <fstream> |
| 38 | +#include <memory> |
| 39 | +#include <string> |
| 40 | + |
| 41 | + |
| 42 | +#include <gtest/gtest.h> |
| 43 | + |
| 44 | + |
| 45 | +#include <ginkgo/core/base/array.hpp> |
| 46 | +#include <ginkgo/core/base/executor.hpp> |
| 47 | +#include <ginkgo/core/matrix/coo.hpp> |
| 48 | +#include <ginkgo/core/matrix/csr.hpp> |
| 49 | +#include <ginkgo/core/matrix/dense.hpp> |
| 50 | + |
| 51 | + |
| 52 | +#include "core/test/utils.hpp" |
| 53 | +#include "matrices/config.hpp" |
| 54 | + |
| 55 | + |
| 56 | +namespace { |
| 57 | + |
| 58 | + |
| 59 | +class ParIlu : public ::testing::Test { |
| 60 | +protected: |
| 61 | + using value_type = gko::default_precision; |
| 62 | + using index_type = gko::int32; |
| 63 | + using Dense = gko::matrix::Dense<value_type>; |
| 64 | + using Coo = gko::matrix::Coo<value_type, index_type>; |
| 65 | + using Csr = gko::matrix::Csr<value_type, index_type>; |
| 66 | + |
| 67 | + ParIlu() |
| 68 | + : ref(gko::ReferenceExecutor::create()), |
| 69 | + hip(gko::HipExecutor::create(0, ref)), |
| 70 | + csr_ref(nullptr), |
| 71 | + csr_hip(nullptr) |
| 72 | + {} |
| 73 | + |
| 74 | + void SetUp() override |
| 75 | + { |
| 76 | + std::string file_name(gko::matrices::location_ani4_mtx); |
| 77 | + auto input_file = std::ifstream(file_name, std::ios::in); |
| 78 | + if (!input_file) { |
| 79 | + FAIL() << "Could not find the file \"" << file_name |
| 80 | + << "\", which is required for this test.\n"; |
| 81 | + } |
| 82 | + csr_ref = gko::read<Csr>(input_file, ref); |
| 83 | + auto csr_hip_temp = Csr::create(hip); |
| 84 | + csr_hip_temp->copy_from(gko::lend(csr_ref)); |
| 85 | + csr_hip = gko::give(csr_hip_temp); |
| 86 | + } |
| 87 | + |
| 88 | + std::shared_ptr<gko::ReferenceExecutor> ref; |
| 89 | + std::shared_ptr<gko::HipExecutor> hip; |
| 90 | + std::shared_ptr<const Csr> csr_ref; |
| 91 | + std::shared_ptr<const Csr> csr_hip; |
| 92 | + |
| 93 | + void initialize_row_ptrs(index_type *l_row_ptrs_ref, |
| 94 | + index_type *u_row_ptrs_ref, |
| 95 | + index_type *l_row_ptrs_hip, |
| 96 | + index_type *u_row_ptrs_hip) |
| 97 | + { |
| 98 | + gko::kernels::reference::par_ilu_factorization::initialize_row_ptrs_l_u( |
| 99 | + ref, gko::lend(csr_ref), l_row_ptrs_ref, u_row_ptrs_ref); |
| 100 | + gko::kernels::hip::par_ilu_factorization::initialize_row_ptrs_l_u( |
| 101 | + hip, gko::lend(csr_hip), l_row_ptrs_hip, u_row_ptrs_hip); |
| 102 | + } |
| 103 | + |
| 104 | + void initialize_lu(std::unique_ptr<Csr> *l_ref, std::unique_ptr<Csr> *u_ref, |
| 105 | + std::unique_ptr<Csr> *l_hip, std::unique_ptr<Csr> *u_hip) |
| 106 | + { |
| 107 | + auto num_row_ptrs = csr_ref->get_size()[0] + 1; |
| 108 | + gko::Array<index_type> l_row_ptrs_ref{ref, num_row_ptrs}; |
| 109 | + gko::Array<index_type> u_row_ptrs_ref{ref, num_row_ptrs}; |
| 110 | + gko::Array<index_type> l_row_ptrs_hip{hip, num_row_ptrs}; |
| 111 | + gko::Array<index_type> u_row_ptrs_hip{hip, num_row_ptrs}; |
| 112 | + |
| 113 | + initialize_row_ptrs( |
| 114 | + l_row_ptrs_ref.get_data(), u_row_ptrs_ref.get_data(), |
| 115 | + l_row_ptrs_hip.get_data(), u_row_ptrs_hip.get_data()); |
| 116 | + // Since `initialize_row_ptrs` was already tested, it is expected that |
| 117 | + // `*_ref` and `*_hip` contain identical values |
| 118 | + auto l_nnz = l_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; |
| 119 | + auto u_nnz = u_row_ptrs_ref.get_const_data()[num_row_ptrs - 1]; |
| 120 | + |
| 121 | + *l_ref = Csr::create(ref, csr_ref->get_size(), l_nnz); |
| 122 | + *u_ref = Csr::create(ref, csr_ref->get_size(), u_nnz); |
| 123 | + *l_hip = Csr::create(hip, csr_hip->get_size(), l_nnz); |
| 124 | + *u_hip = Csr::create(hip, csr_hip->get_size(), u_nnz); |
| 125 | + // Copy the already initialized `row_ptrs` to the new matrices |
| 126 | + ref->copy_from(gko::lend(ref), num_row_ptrs, l_row_ptrs_ref.get_data(), |
| 127 | + (*l_ref)->get_row_ptrs()); |
| 128 | + ref->copy_from(gko::lend(ref), num_row_ptrs, u_row_ptrs_ref.get_data(), |
| 129 | + (*u_ref)->get_row_ptrs()); |
| 130 | + hip->copy_from(gko::lend(hip), num_row_ptrs, l_row_ptrs_hip.get_data(), |
| 131 | + (*l_hip)->get_row_ptrs()); |
| 132 | + hip->copy_from(gko::lend(hip), num_row_ptrs, u_row_ptrs_hip.get_data(), |
| 133 | + (*u_hip)->get_row_ptrs()); |
| 134 | + |
| 135 | + gko::kernels::reference::par_ilu_factorization::initialize_l_u( |
| 136 | + ref, gko::lend(csr_ref), gko::lend(*l_ref), gko::lend(*u_ref)); |
| 137 | + gko::kernels::hip::par_ilu_factorization::initialize_l_u( |
| 138 | + hip, gko::lend(csr_hip), gko::lend(*l_hip), gko::lend(*u_hip)); |
| 139 | + } |
| 140 | + |
| 141 | + template <typename ToType, typename FromType> |
| 142 | + static std::unique_ptr<ToType> static_unique_ptr_cast( |
| 143 | + std::unique_ptr<FromType> &&from) |
| 144 | + { |
| 145 | + return std::unique_ptr<ToType>{static_cast<ToType *>(from.release())}; |
| 146 | + } |
| 147 | + |
| 148 | + void compute_lu(std::unique_ptr<Csr> *l_ref, std::unique_ptr<Csr> *u_ref, |
| 149 | + std::unique_ptr<Csr> *l_hip, std::unique_ptr<Csr> *u_hip, |
| 150 | + gko::size_type iterations = 0) |
| 151 | + { |
| 152 | + auto coo_ref = Coo::create(ref); |
| 153 | + csr_ref->convert_to(gko::lend(coo_ref)); |
| 154 | + auto coo_hip = Coo::create(hip); |
| 155 | + csr_hip->convert_to(gko::lend(coo_hip)); |
| 156 | + initialize_lu(l_ref, u_ref, l_hip, u_hip); |
| 157 | + auto u_transpose_lin_op_ref = (*u_ref)->transpose(); |
| 158 | + auto u_transpose_csr_ref = |
| 159 | + static_unique_ptr_cast<Csr>(std::move(u_transpose_lin_op_ref)); |
| 160 | + auto u_transpose_lin_op_hip = (*u_hip)->transpose(); |
| 161 | + auto u_transpose_csr_hip = |
| 162 | + static_unique_ptr_cast<Csr>(std::move(u_transpose_lin_op_hip)); |
| 163 | + |
| 164 | + gko::kernels::reference::par_ilu_factorization::compute_l_u_factors( |
| 165 | + ref, iterations, gko::lend(coo_ref), gko::lend(*l_ref), |
| 166 | + gko::lend(u_transpose_csr_ref)); |
| 167 | + gko::kernels::hip::par_ilu_factorization::compute_l_u_factors( |
| 168 | + hip, iterations, gko::lend(coo_hip), gko::lend(*l_hip), |
| 169 | + gko::lend(u_transpose_csr_hip)); |
| 170 | + auto u_lin_op_ref = u_transpose_csr_ref->transpose(); |
| 171 | + *u_ref = static_unique_ptr_cast<Csr>(std::move(u_lin_op_ref)); |
| 172 | + auto u_lin_op_hip = u_transpose_csr_hip->transpose(); |
| 173 | + *u_hip = static_unique_ptr_cast<Csr>(std::move(u_lin_op_hip)); |
| 174 | + } |
| 175 | +}; |
| 176 | + |
| 177 | + |
| 178 | +TEST_F(ParIlu, KernelInitializeRowPtrsLUEquivalentToRef) |
| 179 | +{ |
| 180 | + auto num_row_ptrs = csr_ref->get_size()[0] + 1; |
| 181 | + gko::Array<index_type> l_row_ptrs_array_ref(ref, num_row_ptrs); |
| 182 | + gko::Array<index_type> u_row_ptrs_array_ref(ref, num_row_ptrs); |
| 183 | + gko::Array<index_type> l_row_ptrs_array_hip(hip, num_row_ptrs); |
| 184 | + gko::Array<index_type> u_row_ptrs_array_hip(hip, num_row_ptrs); |
| 185 | + |
| 186 | + initialize_row_ptrs( |
| 187 | + l_row_ptrs_array_ref.get_data(), u_row_ptrs_array_ref.get_data(), |
| 188 | + l_row_ptrs_array_hip.get_data(), u_row_ptrs_array_hip.get_data()); |
| 189 | + |
| 190 | + GKO_ASSERT_ARRAY_EQ(&l_row_ptrs_array_ref, &l_row_ptrs_array_hip); |
| 191 | + GKO_ASSERT_ARRAY_EQ(&u_row_ptrs_array_ref, &u_row_ptrs_array_hip); |
| 192 | +} |
| 193 | + |
| 194 | + |
| 195 | +TEST_F(ParIlu, KernelInitializeParILUIsEquivalentToRef) |
| 196 | +{ |
| 197 | + std::unique_ptr<Csr> l_ref{}; |
| 198 | + std::unique_ptr<Csr> u_ref{}; |
| 199 | + std::unique_ptr<Csr> l_hip{}; |
| 200 | + std::unique_ptr<Csr> u_hip{}; |
| 201 | + |
| 202 | + initialize_lu(&l_ref, &u_ref, &l_hip, &u_hip); |
| 203 | + |
| 204 | + GKO_ASSERT_MTX_NEAR(l_ref, l_hip, 1e-14); |
| 205 | + GKO_ASSERT_MTX_NEAR(u_ref, u_hip, 1e-14); |
| 206 | +} |
| 207 | + |
| 208 | + |
| 209 | +TEST_F(ParIlu, KernelComputeParILUIsEquivalentToRef) |
| 210 | +{ |
| 211 | + std::unique_ptr<Csr> l_ref{}; |
| 212 | + std::unique_ptr<Csr> u_ref{}; |
| 213 | + std::unique_ptr<Csr> l_hip{}; |
| 214 | + std::unique_ptr<Csr> u_hip{}; |
| 215 | + |
| 216 | + compute_lu(&l_ref, &u_ref, &l_hip, &u_hip); |
| 217 | + |
| 218 | + GKO_ASSERT_MTX_NEAR(l_ref, l_hip, 5e-2); |
| 219 | + GKO_ASSERT_MTX_NEAR(u_ref, u_hip, 5e-2); |
| 220 | +} |
| 221 | + |
| 222 | + |
| 223 | +TEST_F(ParIlu, KernelComputeParILUWithMoreIterationsIsEquivalentToRef) |
| 224 | +{ |
| 225 | + std::unique_ptr<Csr> l_ref{}; |
| 226 | + std::unique_ptr<Csr> u_ref{}; |
| 227 | + std::unique_ptr<Csr> l_hip{}; |
| 228 | + std::unique_ptr<Csr> u_hip{}; |
| 229 | + gko::size_type iterations{200}; |
| 230 | + |
| 231 | + compute_lu(&l_ref, &u_ref, &l_hip, &u_hip, iterations); |
| 232 | + |
| 233 | + GKO_ASSERT_MTX_NEAR(l_ref, l_hip, 1e-14); |
| 234 | + GKO_ASSERT_MTX_NEAR(u_ref, u_hip, 1e-14); |
| 235 | +} |
| 236 | + |
| 237 | + |
| 238 | +} // namespace |
0 commit comments