|
| 1 | +#ifndef KRATOS_AMGCL_MPI_SOLVE_FUNCTIONS_H |
| 2 | +#define KRATOS_AMGCL_MPI_SOLVE_FUNCTIONS_H |
| 3 | + |
| 4 | +#include <boost/range/iterator_range.hpp> |
| 5 | +#include <boost/property_tree/ptree.hpp> |
| 6 | + |
| 7 | +#include <amgcl/adapter/crs_tuple.hpp> |
| 8 | +#include <amgcl/adapter/epetra.hpp> |
| 9 | +#include <amgcl/adapter/ublas.hpp> |
| 10 | +#include <amgcl/adapter/zero_copy.hpp> |
| 11 | +#include <amgcl/adapter/block_matrix.hpp> |
| 12 | +#include <amgcl/backend/builtin.hpp> |
| 13 | +#include <amgcl/value_type/static_matrix.hpp> |
| 14 | +#include <amgcl/solver/runtime.hpp> |
| 15 | + |
| 16 | +#include <amgcl/mpi/util.hpp> |
| 17 | +#include <amgcl/mpi/make_solver.hpp> |
| 18 | +#include <amgcl/mpi/preconditioner.hpp> |
| 19 | + |
| 20 | +#ifdef AMGCL_GPGPU |
| 21 | +# include <amgcl/backend/vexcl.hpp> |
| 22 | +# include <amgcl/backend/vexcl_static_matrix.hpp> |
| 23 | +#endif |
| 24 | + |
| 25 | +#include "Epetra_FECrsMatrix.h" |
| 26 | +#include "Epetra_FEVector.h" |
| 27 | +#include "trilinos_space.h" |
| 28 | + |
| 29 | +#include "external_includes/amgcl_mpi_solve_functions.h" |
| 30 | + |
| 31 | +namespace Kratos |
| 32 | +{ |
| 33 | + |
| 34 | +#ifdef AMGCL_GPGPU |
| 35 | +vex::Context& vexcl_context(); |
| 36 | + |
| 37 | +template <int TBlockSize> |
| 38 | +void register_vexcl_static_matrix_type(); |
| 39 | +#endif |
| 40 | + |
| 41 | +// Spacialization of AMGCLScalarSolve for distribued systems. |
| 42 | +void AMGCLScalarSolve( |
| 43 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::MatrixType& rA, |
| 44 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rX, |
| 45 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rB, |
| 46 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::IndexType& rIterationNumber, |
| 47 | + double& rResidual, |
| 48 | + const boost::property_tree::ptree &amgclParams, |
| 49 | + int verbosity_level, |
| 50 | + bool use_gpgpu |
| 51 | + ) |
| 52 | +{ |
| 53 | +#ifdef AMGCL_GPGPU |
| 54 | + if (use_gpgpu && vexcl_context()) { |
| 55 | + auto &ctx = vexcl_context(); |
| 56 | + |
| 57 | + typedef amgcl::backend::vexcl<double> Backend; |
| 58 | + |
| 59 | + typedef |
| 60 | + amgcl::mpi::make_solver< |
| 61 | + amgcl::runtime::mpi::preconditioner<Backend>, |
| 62 | + amgcl::runtime::solver::wrapper |
| 63 | + > |
| 64 | + Solver; |
| 65 | + |
| 66 | + Backend::params bprm; |
| 67 | + bprm.q = ctx; |
| 68 | + |
| 69 | + Solver solve(MPI_COMM_WORLD, amgcl::adapter::map(rA), amgclParams, bprm); |
| 70 | + |
| 71 | + std::size_t n = rA.NumMyRows(); |
| 72 | + |
| 73 | + vex::vector<double> b(ctx, n, rB.Values()); |
| 74 | + vex::vector<double> x(ctx, n, rX.Values()); |
| 75 | + |
| 76 | + std::tie(rIterationNumber, rResidual) = solve(b, x); |
| 77 | + |
| 78 | + vex::copy(x.begin(), x.end(), rX.Values()); |
| 79 | + } else |
| 80 | +#endif |
| 81 | + { |
| 82 | + typedef amgcl::backend::builtin<double> Backend; |
| 83 | + |
| 84 | + typedef |
| 85 | + amgcl::mpi::make_solver< |
| 86 | + amgcl::runtime::mpi::preconditioner<Backend>, |
| 87 | + amgcl::runtime::solver::wrapper |
| 88 | + > |
| 89 | + Solver; |
| 90 | + |
| 91 | + Solver solve(MPI_COMM_WORLD, amgcl::adapter::map(rA), amgclParams); |
| 92 | + |
| 93 | + std::size_t n = rA.NumMyRows(); |
| 94 | + |
| 95 | + auto b_range = boost::make_iterator_range(rB.Values(), rB.Values() + n); |
| 96 | + auto x_range = boost::make_iterator_range(rX.Values(), rX.Values() + n); |
| 97 | + |
| 98 | + std::tie(rIterationNumber, rResidual) = solve(b_range, x_range); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Spacialization of AMGCLBlockSolve for distribued systems. |
| 103 | +template <int TBlockSize> |
| 104 | +void AMGCLBlockSolve( |
| 105 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::MatrixType & rA, |
| 106 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rX, |
| 107 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rB, |
| 108 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::IndexType& rIterationNumber, |
| 109 | + double& rResidual, |
| 110 | + boost::property_tree::ptree amgclParams, |
| 111 | + int verbosity_level, |
| 112 | + bool use_gpgpu |
| 113 | + ) |
| 114 | +{ |
| 115 | + if(amgclParams.get<std::string>("precond.class") != "amg") |
| 116 | + amgclParams.erase("precond.coarsening"); |
| 117 | + else |
| 118 | + amgclParams.put("precond.coarsening.aggr.block_size",1); |
| 119 | + |
| 120 | + typedef amgcl::static_matrix<double, TBlockSize, TBlockSize> val_type; |
| 121 | + typedef amgcl::static_matrix<double, TBlockSize, 1> rhs_type; |
| 122 | + |
| 123 | + std::size_t n = rA.RowMap().NumMyElements(); |
| 124 | + std::size_t nb = n / TBlockSize; |
| 125 | + |
| 126 | +#ifdef AMGCL_GPGPU |
| 127 | + if (use_gpgpu && vexcl_context()) { |
| 128 | + auto &ctx = vexcl_context(); |
| 129 | + register_vexcl_static_matrix_type<TBlockSize>(); |
| 130 | + |
| 131 | + typedef amgcl::backend::vexcl<val_type> Backend; |
| 132 | + |
| 133 | + typedef |
| 134 | + amgcl::mpi::make_solver< |
| 135 | + amgcl::runtime::mpi::preconditioner<Backend>, |
| 136 | + amgcl::runtime::solver::wrapper |
| 137 | + > |
| 138 | + Solver; |
| 139 | + |
| 140 | + typename Backend::params bprm; |
| 141 | + bprm.q = ctx; |
| 142 | + |
| 143 | + Solver solve( |
| 144 | + MPI_COMM_WORLD, |
| 145 | + amgcl::adapter::block_matrix<val_type>(amgcl::adapter::map(rA)), |
| 146 | + amgclParams, bprm |
| 147 | + ); |
| 148 | + |
| 149 | + auto b_begin = reinterpret_cast<const rhs_type*>(rB.Values()); |
| 150 | + auto x_begin = reinterpret_cast<rhs_type*>(rX.Values()); |
| 151 | + |
| 152 | + vex::vector<rhs_type> x(ctx, nb, x_begin); |
| 153 | + vex::vector<rhs_type> b(ctx, nb, b_begin); |
| 154 | + |
| 155 | + std::tie(rIterationNumber, rResidual) = solve(b, x); |
| 156 | + |
| 157 | + vex::copy(x.begin(), x.end(), x_begin); |
| 158 | + } else |
| 159 | +#endif |
| 160 | + { |
| 161 | + typedef amgcl::backend::builtin<val_type> Backend; |
| 162 | + |
| 163 | + typedef |
| 164 | + amgcl::mpi::make_solver< |
| 165 | + amgcl::runtime::mpi::preconditioner<Backend>, |
| 166 | + amgcl::runtime::solver::wrapper |
| 167 | + > |
| 168 | + Solver; |
| 169 | + |
| 170 | + Solver solve( |
| 171 | + MPI_COMM_WORLD, |
| 172 | + amgcl::adapter::block_matrix<val_type>(amgcl::adapter::map(rA)), |
| 173 | + amgclParams |
| 174 | + ); |
| 175 | + |
| 176 | + auto b_begin = reinterpret_cast<const rhs_type*>(rB.Values()); |
| 177 | + auto x_begin = reinterpret_cast<rhs_type*>(rX.Values()); |
| 178 | + |
| 179 | + auto b_range = boost::make_iterator_range(b_begin, b_begin + nb); |
| 180 | + auto x_range = boost::make_iterator_range(x_begin, x_begin + nb); |
| 181 | + |
| 182 | + std::tie(rIterationNumber, rResidual) = solve(b_range, x_range); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +void AMGCLSolve( |
| 187 | + int block_size, |
| 188 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::MatrixType& rA, |
| 189 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rX, |
| 190 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rB, |
| 191 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::IndexType& rIterationNumber, |
| 192 | + double& rResidual, |
| 193 | + const boost::property_tree::ptree &amgclParams, |
| 194 | + int verbosity_level, |
| 195 | + bool use_gpgpu |
| 196 | + ) |
| 197 | +{ |
| 198 | + switch (block_size) { |
| 199 | + case 2: |
| 200 | + AMGCLBlockSolve<2>(rA, rX, rB, rIterationNumber, rResidual, amgclParams, verbosity_level, use_gpgpu); |
| 201 | + return; |
| 202 | + case 3: |
| 203 | + AMGCLBlockSolve<3>(rA, rX, rB, rIterationNumber, rResidual, amgclParams, verbosity_level, use_gpgpu); |
| 204 | + return; |
| 205 | + case 4: |
| 206 | + AMGCLBlockSolve<4>(rA, rX, rB, rIterationNumber, rResidual, amgclParams, verbosity_level, use_gpgpu); |
| 207 | + return; |
| 208 | + default: |
| 209 | + AMGCLScalarSolve(rA, rX, rB, rIterationNumber, rResidual, amgclParams, verbosity_level, use_gpgpu); |
| 210 | + return; |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +} // namespace Kratos |
| 215 | + |
| 216 | +#endif |
0 commit comments