-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1059 from ginkgo-project/reordered_interface
This PR adds an interface for wrapping an inner operator (like a direct or iterative solver) into a (for now symmetrical, e.g. RCM) reordering via a reordering factory and diagonal scaling via one row and one column scaling diagonal matrix. The ScaledReordered LinOp applies the given scaling and reordering to the system matrix and generates the inner operator on the modified problem. It manages three vectors, a right-hand side and a solution vector for the inner operator, and an intermediate vector used for the permutations. With a permutation matrix P, a row scaling matrix R, and a column matrix C, the system matrix and vectors are scaled and permuted such that instead of the original system of equations A*x = b the inner operator solves P*R*A*C*P^T*y = P*R*b. Hence, when generating a ScaledReordered LinOp, the new system matrix P*R*A*C*P^T is computed. When it is applied to vectors b and x, the right-hand side vector is modified to P*R*b, and the solution y to the inner system gives the solution x to the original system via x = C*P^T*y. When using an inner operator that uses an initial guess, currently the initial guess for the inner system is computed by y_0 = P*C^(-1)*x_0, which is the same as using x_0 for the system P*R*A*x = P*R*b. For the inverse diagonal scaling here I added an inverse_apply to the matrix::Diagonal. Related PR: #1059
- Loading branch information
Showing
24 changed files
with
1,330 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/*******************************<GINKGO LICENSE>****************************** | ||
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. | ||
******************************<GINKGO LICENSE>*******************************/ | ||
|
||
#include <ginkgo/core/reorder/scaled_reordered.hpp> | ||
|
||
|
||
#include <utility> | ||
|
||
|
||
#include <ginkgo/core/base/precision_dispatch.hpp> | ||
#include <ginkgo/core/matrix/permutation.hpp> | ||
|
||
|
||
namespace gko { | ||
namespace experimental { | ||
namespace reorder { | ||
|
||
|
||
template <typename ValueType, typename IndexType> | ||
void ScaledReordered<ValueType, IndexType>::apply_impl(const LinOp* b, | ||
LinOp* x) const | ||
{ | ||
precision_dispatch_real_complex<ValueType>( | ||
[this](auto dense_b, auto dense_x) { | ||
auto exec = this->get_executor(); | ||
this->set_cache_to(dense_b, dense_x); | ||
|
||
// Preprocess the input vectors before applying the inner operator. | ||
if (row_scaling_) { | ||
row_scaling_->apply(cache_.inner_b.get(), | ||
cache_.intermediate.get()); | ||
std::swap(cache_.inner_b, cache_.intermediate); | ||
} | ||
// Col scaling for x is only necessary if the inner operator uses an | ||
// initial guess. Otherwise x is overwritten anyway. | ||
if (col_scaling_ && inner_operator_->apply_uses_initial_guess()) { | ||
col_scaling_->inverse_apply(cache_.inner_x.get(), | ||
cache_.intermediate.get()); | ||
std::swap(cache_.inner_x, cache_.intermediate); | ||
} | ||
if (permutation_array_.get_num_elems() > 0) { | ||
cache_.inner_b->row_permute(&permutation_array_, | ||
cache_.intermediate.get()); | ||
std::swap(cache_.inner_b, cache_.intermediate); | ||
if (inner_operator_->apply_uses_initial_guess()) { | ||
cache_.inner_x->row_permute(&permutation_array_, | ||
cache_.intermediate.get()); | ||
std::swap(cache_.inner_x, cache_.intermediate); | ||
} | ||
} | ||
|
||
inner_operator_->apply(cache_.inner_b.get(), cache_.inner_x.get()); | ||
|
||
// Permute and scale the solution vector back. | ||
if (permutation_array_.get_num_elems() > 0) { | ||
cache_.inner_x->inverse_row_permute(&permutation_array_, | ||
cache_.intermediate.get()); | ||
std::swap(cache_.inner_x, cache_.intermediate); | ||
} | ||
if (col_scaling_) { | ||
col_scaling_->apply(cache_.inner_x.get(), | ||
cache_.intermediate.get()); | ||
std::swap(cache_.inner_x, cache_.intermediate); | ||
} | ||
dense_x->copy_from(cache_.inner_x.get()); | ||
}, | ||
b, x); | ||
} | ||
|
||
|
||
template <typename ValueType, typename IndexType> | ||
void ScaledReordered<ValueType, IndexType>::apply_impl(const LinOp* alpha, | ||
const LinOp* b, | ||
const LinOp* beta, | ||
LinOp* x) const | ||
{ | ||
precision_dispatch_real_complex<ValueType>( | ||
[this](auto dense_alpha, auto dense_b, auto dense_beta, auto dense_x) { | ||
auto x_clone = dense_x->clone(); | ||
this->apply_impl(dense_b, x_clone.get()); | ||
dense_x->scale(dense_beta); | ||
dense_x->add_scaled(dense_alpha, x_clone.get()); | ||
}, | ||
alpha, b, beta, x); | ||
} | ||
|
||
|
||
#define GKO_DECLARE_SCALED_REORDERED(ValueType, IndexType) \ | ||
class ScaledReordered<ValueType, IndexType> | ||
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_SCALED_REORDERED); | ||
|
||
|
||
} // namespace reorder | ||
} // namespace experimental | ||
} // namespace gko |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
ginkgo_create_test(rcm) | ||
ginkgo_create_test(scaled_reordered) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/*******************************<GINKGO LICENSE>****************************** | ||
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. | ||
******************************<GINKGO LICENSE>*******************************/ | ||
|
||
#include <ginkgo/core/reorder/scaled_reordered.hpp> | ||
|
||
|
||
#include <memory> | ||
|
||
|
||
#include <gtest/gtest.h> | ||
|
||
|
||
#include <ginkgo/core/base/executor.hpp> | ||
#include <ginkgo/core/factorization/par_ic.hpp> | ||
#include <ginkgo/core/matrix/diagonal.hpp> | ||
#include <ginkgo/core/reorder/rcm.hpp> | ||
#include <ginkgo/core/solver/bicgstab.hpp> | ||
|
||
|
||
namespace { | ||
|
||
|
||
class ScaledReorderedFactory : public ::testing::Test { | ||
protected: | ||
using value_type = double; | ||
using index_type = gko::int32; | ||
using diag = gko::matrix::Diagonal<value_type>; | ||
using solver_type = gko::solver::Bicgstab<value_type>; | ||
using reorder_type = gko::reorder::Rcm<value_type, index_type>; | ||
using scaled_reordered_type = | ||
gko::experimental::reorder::ScaledReordered<value_type, index_type>; | ||
|
||
ScaledReorderedFactory() | ||
: exec(gko::ReferenceExecutor::create()), | ||
scaled_reordered_factory(scaled_reordered_type::build().on(exec)), | ||
reordering_factory(reorder_type::build().on(exec)), | ||
solver_factory(solver_type::build().on(exec)), | ||
diag_matrix(diag::create(exec)) | ||
{} | ||
|
||
std::shared_ptr<const gko::Executor> exec; | ||
std::shared_ptr<diag> diag_matrix; | ||
std::shared_ptr<typename scaled_reordered_type::Factory> | ||
scaled_reordered_factory; | ||
std::shared_ptr<typename reorder_type::Factory> reordering_factory; | ||
std::shared_ptr<typename solver_type::Factory> solver_factory; | ||
}; | ||
|
||
|
||
TEST_F(ScaledReorderedFactory, KnowsItsExecutor) | ||
{ | ||
auto scaled_reordered_factory = | ||
scaled_reordered_type::build().on(this->exec); | ||
|
||
ASSERT_EQ(scaled_reordered_factory->get_executor(), this->exec); | ||
} | ||
|
||
|
||
TEST_F(ScaledReorderedFactory, CanSetReorderingFactory) | ||
{ | ||
auto scaled_reordered_factory = | ||
scaled_reordered_type::build() | ||
.with_reordering(this->reordering_factory) | ||
.on(this->exec); | ||
|
||
ASSERT_EQ(scaled_reordered_factory->get_parameters().reordering, | ||
this->reordering_factory); | ||
} | ||
|
||
|
||
TEST_F(ScaledReorderedFactory, CanSetInnerOperatorFactory) | ||
{ | ||
auto scaled_reordered_factory = | ||
scaled_reordered_type::build() | ||
.with_inner_operator(this->solver_factory) | ||
.on(this->exec); | ||
|
||
ASSERT_EQ(scaled_reordered_factory->get_parameters().inner_operator, | ||
this->solver_factory); | ||
} | ||
|
||
|
||
TEST_F(ScaledReorderedFactory, CanSetRowScaling) | ||
{ | ||
auto scaled_reordered_factory = scaled_reordered_type::build() | ||
.with_row_scaling(this->diag_matrix) | ||
.on(this->exec); | ||
|
||
ASSERT_EQ(scaled_reordered_factory->get_parameters().row_scaling, | ||
this->diag_matrix); | ||
} | ||
|
||
|
||
TEST_F(ScaledReorderedFactory, CanSetColScaling) | ||
{ | ||
auto scaled_reordered_factory = scaled_reordered_type::build() | ||
.with_col_scaling(this->diag_matrix) | ||
.on(this->exec); | ||
|
||
ASSERT_EQ(scaled_reordered_factory->get_parameters().col_scaling, | ||
this->diag_matrix); | ||
} | ||
|
||
|
||
} // namespace |
Oops, something went wrong.