Skip to content

Commit

Permalink
Implement mpi::relaxation::as_preconditioner
Browse files Browse the repository at this point in the history
  • Loading branch information
ddemidov committed Aug 27, 2018
1 parent 26e1858 commit 425f432
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 11 deletions.
105 changes: 105 additions & 0 deletions amgcl/mpi/relaxation/as_preconditioner.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#ifndef AMGCL_MPI_RELAXATION_AS_PRECONDITIONER_HPP
#define AMGCL_MPI_RELAXATION_AS_PRECONDITIONER_HPP

/*
The MIT License
Copyright (c) 2012-2018 Denis Demidov <dennis.demidov@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
* \file amgcl/mpi/relaxation/as_preconditioner.hpp
* \author Denis Demidov <dennis.demidov@gmail.com>
* \brief Use a distributed amgcl smoother as a standalone preconditioner.
*/

#include <vector>
#include <memory>
#include <amgcl/backend/builtin.hpp>
#include <amgcl/mpi/util.hpp>

namespace amgcl {
namespace mpi {
namespace relaxation {

template <class Relaxation>
struct as_preconditioner {
typedef typename Relaxation::params params;
typedef typename Relaxation::backend_type backend_type;
typedef typename backend_type::params backend_params;
typedef typename backend_type::value_type value_type;
typedef typename math::scalar_of<value_type>::type scalar_type;
typedef distributed_matrix<backend_type> matrix;
typedef typename backend_type::vector vector;

template <class Matrix>
as_preconditioner(
communicator comm,
const Matrix &A,
const params &prm = params(),
const backend_params &bprm = backend_params()
) : A(std::make_shared>(comm, A, backend::rows(A))),
S(A, prm, bprm)
{
this->A->move_to_backend(bprm);
}

as_preconditioner(
communicator,
std::shared_ptr<matrix> A,
const params &prm = params(),
const backend_params &bprm = backend_params()
) : A(A), S(*A, prm, bprm)
{
this->A->move_to_backend(bprm);
}

template <class Vec1, class Vec2>
void apply(const Vec1 &rhs, Vec2 &&x) const {
S.apply(*A, rhs, x);
}

std::shared_ptr<matrix> system_matrix_ptr() const {
return A;
}

const matrix& system_matrix() const {
return *system_matrix_ptr();
}

private:
std::shared_ptr<matrix> A;
Relaxation S;

friend std::ostream& operator<<(std::ostream &os, const as_preconditioner &p) {
os << "Relaxation as preconditioner" << std::endl;
os << " unknowns: " << p.system_matrix().glob_rows() << std::endl;
os << " nonzeros: " << p.system_matrix().glob_nonzeros() << std::endl;

return os;
}
};

} // namespace relaxation
} // namespace mpi
} // namespace amgcl

#endif
4 changes: 2 additions & 2 deletions amgcl/mpi/relaxation/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ namespace relaxation {

template <class Backend>
struct wrapper {
typedef boost::property_tree::ptree params;
typedef Backend backend_type;
typedef typename Backend::params backend_params;
typedef boost::property_tree::ptree params;

runtime::relaxation::type r;
void *handle;
Expand Down Expand Up @@ -321,7 +322,6 @@ struct wrapper {
call_apply(const Matrix&, const VectorRHS&, VectorX&) const {
throw std::logic_error("The relaxation is not supported by the backend");
}

};

} // namespace relaxation
Expand Down
1 change: 1 addition & 0 deletions amgcl/mpi/relaxation/spai0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace relaxation {

template <class Backend>
struct spai0 {
typedef Backend backend_type;
typedef typename Backend::value_type value_type;
typedef typename Backend::matrix_diagonal matrix_diagonal;
typedef typename math::scalar_of<value_type>::type scalar_type;
Expand Down
2 changes: 1 addition & 1 deletion amgcl/relaxation/as_preconditioner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ THE SOFTWARE.
/**
* \file amgcl/relaxation/as_preconditioner.hpp
* \author Denis Demidov <dennis.demidov@gmail.com>
* \brief Use an amgcl smoother as standalone preconditioner.
* \brief Use an amgcl smoother as a standalone preconditioner.
*/

#include <vector>
Expand Down
11 changes: 3 additions & 8 deletions examples/mpi/cpr_mpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <amgcl/mpi/amg.hpp>
#include <amgcl/mpi/coarsening/runtime.hpp>
#include <amgcl/mpi/relaxation/runtime.hpp>
#include <amgcl/mpi/relaxation/as_preconditioner.hpp>
#include <amgcl/mpi/direct_solver/runtime.hpp>
#include <amgcl/mpi/direct_solver/runtime.hpp>
#include <amgcl/mpi/partition/runtime.hpp>
Expand Down Expand Up @@ -102,8 +103,6 @@ partition(amgcl::mpi::communicator comm, const Matrix &Astrip,
std::vector<double> &rhs, const typename Backend::params &bprm,
amgcl::runtime::mpi::partition::type ptype, int block_size = 1)
{
typedef typename Backend::value_type val_type;
typedef typename amgcl::math::rhs_of<val_type>::type rhs_type;
typedef amgcl::mpi::distributed_matrix<Backend> DMatrix;

using amgcl::prof;
Expand Down Expand Up @@ -266,12 +265,8 @@ int main(int argc, char *argv[]) {
amgcl::runtime::mpi::direct::solver<double>,
amgcl::runtime::mpi::partition::wrapper<Backend>
>,
amgcl::mpi::amg<
Backend,
amgcl::runtime::mpi::coarsening::wrapper<Backend>,
amgcl::runtime::mpi::relaxation::wrapper<Backend>,
amgcl::runtime::mpi::direct::solver<double>,
amgcl::runtime::mpi::partition::wrapper<Backend>
amgcl::mpi::relaxation::as_preconditioner<
amgcl::runtime::mpi::relaxation::wrapper<Backend>
>
>,
amgcl::runtime::solver::wrapper
Expand Down

0 comments on commit 425f432

Please sign in to comment.