|
| 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/amg.hpp> |
| 19 | +#include <amgcl/mpi/coarsening/runtime.hpp> |
| 20 | +#include <amgcl/mpi/relaxation/runtime.hpp> |
| 21 | +#include <amgcl/mpi/direct_solver/runtime.hpp> |
| 22 | +#include <amgcl/mpi/partition/runtime.hpp> |
| 23 | + |
| 24 | +#ifdef KRATOS_HAVE_OPENCL |
| 25 | +# include <amgcl/backend/vexcl.hpp> |
| 26 | +# include <amgcl/backend/vexcl_static_matrix.hpp> |
| 27 | +#endif |
| 28 | + |
| 29 | +#include "Epetra_FECrsMatrix.h" |
| 30 | +#include "Epetra_FEVector.h" |
| 31 | +#include "trilinos_space.h" |
| 32 | + |
| 33 | +namespace Kratos |
| 34 | +{ |
| 35 | + |
| 36 | +#ifdef KRATOS_HAVE_OPENCL |
| 37 | +vex::Context& vexcl_context(); |
| 38 | + |
| 39 | +template <int TBlockSize> |
| 40 | +void register_vexcl_static_matrix_type(); |
| 41 | +#endif |
| 42 | + |
| 43 | +// Spacialization of AMGCLScalarSolve for distribued systems. |
| 44 | +template <class TSparseSpaceType> |
| 45 | +typename std::enable_if<TSparseSpaceType::IsDistributed(), void>::type |
| 46 | +AMGCLScalarSolve( |
| 47 | + typename TSparseSpaceType::MatrixType& rA, |
| 48 | + typename TSparseSpaceType::VectorType& rX, |
| 49 | + typename TSparseSpaceType::VectorType& rB, |
| 50 | + typename TSparseSpaceType::IndexType& rIterationNumber, |
| 51 | + double& rResidual, |
| 52 | + const boost::property_tree::ptree &amgclParams, |
| 53 | + int verbosity_level, |
| 54 | + bool use_opencl |
| 55 | + ) |
| 56 | +{ |
| 57 | +#ifdef KRATOS_HAVE_OPENCL |
| 58 | + if (use_opencl && vexcl_context()) { |
| 59 | + auto &ctx = vexcl_context(); |
| 60 | + |
| 61 | + typedef amgcl::backend::vexcl<double> Backend; |
| 62 | + |
| 63 | + typedef |
| 64 | + amgcl::mpi::make_solver< |
| 65 | + amgcl::mpi::amg< |
| 66 | + Backend, |
| 67 | + amgcl::runtime::mpi::coarsening::wrapper<Backend>, |
| 68 | + amgcl::runtime::mpi::relaxation::wrapper<Backend>, |
| 69 | + amgcl::runtime::mpi::direct::solver<double>, |
| 70 | + amgcl::runtime::mpi::partition::wrapper<Backend> |
| 71 | + >, |
| 72 | + amgcl::runtime::solver::wrapper |
| 73 | + > |
| 74 | + Solver; |
| 75 | + |
| 76 | + Backend::params bprm; |
| 77 | + bprm.q = ctx; |
| 78 | + |
| 79 | + Solver solve(MPI_COMM_WORLD, amgcl::adapter::map(rA), amgclParams, bprm); |
| 80 | + |
| 81 | + std::size_t n = rA.NumMyRows(); |
| 82 | + |
| 83 | + vex::vector<double> b(ctx, n, rB.Values()); |
| 84 | + vex::vector<double> x(ctx, n, rX.Values()); |
| 85 | + |
| 86 | + std::tie(rIterationNumber, rResidual) = solve(b, x); |
| 87 | + |
| 88 | + vex::copy(x.begin(), x.end(), rX.Values()); |
| 89 | + } else |
| 90 | +#endif |
| 91 | + { |
| 92 | + typedef amgcl::backend::builtin<double> Backend; |
| 93 | + |
| 94 | + typedef |
| 95 | + amgcl::mpi::make_solver< |
| 96 | + amgcl::mpi::amg< |
| 97 | + Backend, |
| 98 | + amgcl::runtime::mpi::coarsening::wrapper<Backend>, |
| 99 | + amgcl::runtime::mpi::relaxation::wrapper<Backend>, |
| 100 | + amgcl::runtime::mpi::direct::solver<double>, |
| 101 | + amgcl::runtime::mpi::partition::wrapper<Backend> |
| 102 | + >, |
| 103 | + amgcl::runtime::solver::wrapper |
| 104 | + > |
| 105 | + Solver; |
| 106 | + |
| 107 | + Solver solve(MPI_COMM_WORLD, amgcl::adapter::map(rA), amgclParams); |
| 108 | + |
| 109 | + std::size_t n = rA.NumMyRows(); |
| 110 | + |
| 111 | + auto b_range = boost::make_iterator_range(rB.Values(), rB.Values() + n); |
| 112 | + auto x_range = boost::make_iterator_range(rX.Values(), rX.Values() + n); |
| 113 | + |
| 114 | + std::tie(rIterationNumber, rResidual) = solve(b_range, x_range); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Spacialization of AMGCLBlockSolve for distribued systems. |
| 119 | +template <int TBlockSize, class TSparseSpaceType> |
| 120 | +typename std::enable_if<TSparseSpaceType::IsDistributed(), void>::type |
| 121 | +AMGCLBlockSolve( |
| 122 | + typename TSparseSpaceType::MatrixType & rA, |
| 123 | + typename TSparseSpaceType::VectorType& rX, |
| 124 | + typename TSparseSpaceType::VectorType& rB, |
| 125 | + typename TSparseSpaceType::IndexType& rIterationNumber, |
| 126 | + double& rResidual, |
| 127 | + boost::property_tree::ptree amgclParams, |
| 128 | + int verbosity_level, |
| 129 | + bool use_opencl |
| 130 | + ) |
| 131 | +{ |
| 132 | + amgclParams.put("precond.coarsening.aggr.block_size",1); |
| 133 | + |
| 134 | + typedef amgcl::static_matrix<double, TBlockSize, TBlockSize> val_type; |
| 135 | + typedef amgcl::static_matrix<double, TBlockSize, 1> rhs_type; |
| 136 | + |
| 137 | + std::size_t n = rA.RowMap().NumMyElements(); |
| 138 | + std::size_t nb = n / TBlockSize; |
| 139 | + |
| 140 | +#ifdef KRATOS_HAVE_OPENCL |
| 141 | + if (use_opencl && vexcl_context()) { |
| 142 | + auto &ctx = vexcl_context(); |
| 143 | + register_vexcl_static_matrix_type<TBlockSize>(); |
| 144 | + |
| 145 | + typedef amgcl::backend::vexcl<val_type> Backend; |
| 146 | + |
| 147 | + typedef |
| 148 | + amgcl::mpi::make_solver< |
| 149 | + amgcl::mpi::amg< |
| 150 | + Backend, |
| 151 | + amgcl::runtime::mpi::coarsening::wrapper<Backend>, |
| 152 | + amgcl::runtime::mpi::relaxation::wrapper<Backend>, |
| 153 | + amgcl::runtime::mpi::direct::solver<val_type>, |
| 154 | + amgcl::runtime::mpi::partition::wrapper<Backend> |
| 155 | + >, |
| 156 | + amgcl::runtime::solver::wrapper |
| 157 | + > |
| 158 | + Solver; |
| 159 | + |
| 160 | + typename Backend::params bprm; |
| 161 | + bprm.q = ctx; |
| 162 | + |
| 163 | + Solver solve( |
| 164 | + MPI_COMM_WORLD, |
| 165 | + amgcl::adapter::block_matrix<val_type>(amgcl::adapter::map(rA)), |
| 166 | + amgclParams, bprm |
| 167 | + ); |
| 168 | + |
| 169 | + auto b_begin = reinterpret_cast<const rhs_type*>(rB.Values()); |
| 170 | + auto x_begin = reinterpret_cast<rhs_type*>(rX.Values()); |
| 171 | + |
| 172 | + vex::vector<rhs_type> x(ctx, nb, x_begin); |
| 173 | + vex::vector<rhs_type> b(ctx, nb, b_begin); |
| 174 | + |
| 175 | + std::tie(rIterationNumber, rResidual) = solve(b, x); |
| 176 | + |
| 177 | + vex::copy(x.begin(), x.end(), x_begin); |
| 178 | + } else |
| 179 | +#endif |
| 180 | + { |
| 181 | + typedef amgcl::backend::builtin<val_type> Backend; |
| 182 | + |
| 183 | + typedef |
| 184 | + amgcl::mpi::make_solver< |
| 185 | + amgcl::mpi::amg< |
| 186 | + Backend, |
| 187 | + amgcl::runtime::mpi::coarsening::wrapper<Backend>, |
| 188 | + amgcl::runtime::mpi::relaxation::wrapper<Backend>, |
| 189 | + amgcl::runtime::mpi::direct::solver<val_type>, |
| 190 | + amgcl::runtime::mpi::partition::wrapper<Backend> |
| 191 | + >, |
| 192 | + amgcl::runtime::solver::wrapper |
| 193 | + > |
| 194 | + Solver; |
| 195 | + |
| 196 | + Solver solve( |
| 197 | + MPI_COMM_WORLD, |
| 198 | + amgcl::adapter::block_matrix<val_type>(amgcl::adapter::map(rA)), |
| 199 | + amgclParams |
| 200 | + ); |
| 201 | + |
| 202 | + auto b_begin = reinterpret_cast<const rhs_type*>(rB.Values()); |
| 203 | + auto x_begin = reinterpret_cast<rhs_type*>(rX.Values()); |
| 204 | + |
| 205 | + auto b_range = boost::make_iterator_range(b_begin, b_begin + n / TBlockSize); |
| 206 | + auto x_range = boost::make_iterator_range(x_begin, x_begin + n / TBlockSize); |
| 207 | + |
| 208 | + std::tie(rIterationNumber, rResidual) = solve(b_range, x_range); |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +// Exlplicit instantiations: |
| 213 | +template void AMGCLScalarSolve< TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector> >( |
| 214 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::MatrixType& rA, |
| 215 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rX, |
| 216 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rB, |
| 217 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::IndexType& rIterationNumber, |
| 218 | + double& rResidual, |
| 219 | + const boost::property_tree::ptree &amgclParams, |
| 220 | + int verbosity_level, |
| 221 | + bool use_opencl |
| 222 | + ); |
| 223 | + |
| 224 | +#define INSTANTIATE_BLOCK_SOLVER(B) \ |
| 225 | +template void AMGCLBlockSolve<B, TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector> >( \ |
| 226 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::MatrixType& rA, \ |
| 227 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rX, \ |
| 228 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::VectorType& rB, \ |
| 229 | + TrilinosSpace<Epetra_FECrsMatrix, Epetra_FEVector>::IndexType& rIterationNumber, \ |
| 230 | + double& rResidual, \ |
| 231 | + boost::property_tree::ptree amgclParams, \ |
| 232 | + int verbosity_level, \ |
| 233 | + bool use_opencl \ |
| 234 | + ) |
| 235 | + |
| 236 | +INSTANTIATE_BLOCK_SOLVER(2); |
| 237 | +INSTANTIATE_BLOCK_SOLVER(3); |
| 238 | +INSTANTIATE_BLOCK_SOLVER(4); |
| 239 | + |
| 240 | +#undef INSTANTIATE_BLOCK_SOLVER |
| 241 | + |
| 242 | +} // namespace Kratos |
| 243 | + |
| 244 | +#endif |
0 commit comments