Skip to content

Instantly share code, notes, and snippets.

@ddemidov
Created October 23, 2023 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddemidov/3292c6fea940c785dc8b43461846ae45 to your computer and use it in GitHub Desktop.
Save ddemidov/3292c6fea940c785dc8b43461846ae45 to your computer and use it in GitHub Desktop.
rebuild: rebuild.cpp
mpic++ -o $@ $^ -I$(AMGCL_ROOT) -O3 -fopenmp -DNDEBUG
#include <vector>
#include <iostream>
#include <amgcl/backend/builtin.hpp>
#include <amgcl/value_type/static_matrix.hpp>
#include <amgcl/mpi/distributed_matrix.hpp>
#include <amgcl/mpi/make_solver.hpp>
#include <amgcl/mpi/amg.hpp>
#include <amgcl/mpi/coarsening/smoothed_aggregation.hpp>
#include <amgcl/mpi/relaxation/spai0.hpp>
#include <amgcl/mpi/solver/bicgstab.hpp>
#include <amgcl/io/binary.hpp>
#include <amgcl/profiler.hpp>
//---------------------------------------------------------------------------
int main(int argc, char *argv[])
{
amgcl::mpi::init mpi(&argc, &argv);
amgcl::mpi::communicator world(MPI_COMM_WORLD);
int localsize = world.rank == 0 ? 3 : 2;
std::vector<ptrdiff_t> ptr, col;
std::vector<double> val;
ptr.push_back(0);
if (world.rank == 0)
{
col.push_back(0); val.push_back(1.0); ptr.push_back(col.size());
col.push_back(1); val.push_back(1.0); ptr.push_back(col.size());
col.push_back(2); val.push_back(1.0); ptr.push_back(col.size());
}
else
{
col.push_back(3); val.push_back(1.0); ptr.push_back(col.size());
col.push_back(4); val.push_back(1.0); ptr.push_back(col.size());
}
amgcl::profiler<> prof;
// Declare the backend and the solver types
typedef amgcl::backend::builtin<double> DBackend;
typedef amgcl::backend::builtin<float> FBackend;
typedef amgcl::mpi::make_solver<
amgcl::mpi::amg<
DBackend,
amgcl::mpi::coarsening::smoothed_aggregation<DBackend>,
amgcl::mpi::relaxation::spai0<DBackend>
>,
amgcl::mpi::solver::bicgstab<DBackend>
>
Solver;
// Solver parameters
Solver::params prm;
prm.solver.maxiter = 200;
prm.precond.allow_rebuild = true; // !!!
std::vector<double> rhs(localsize, 1.0);
// Initialize the solver:
auto A = std::tie(localsize, ptr, col, val);
prof.tic("setup");
Solver solve(world, A, prm);
prof.toc("setup");
prof.tic("rebuild");
solve.precond().rebuild(A);
prof.toc("rebuild");
// Show the mini-report on the constructed solver:
if (world.rank == 0)
std::cout << solve << std::endl;
// Solve the system with the zero initial approximation:
int iters;
double error;
std::vector<double> x(localsize, 0.0);
prof.tic("solve");
std::tie(iters, error) = solve(rhs, x);
prof.toc("solve");
// Output the number of iterations, the relative error,
// and the profiling data:
if (world.rank == 0)
{
std::cout
<< "Iterations: " << iters << std::endl
<< "Error: " << error << std::endl
<< prof << std::endl;
std::cout << "sol: " << std::endl;
for (auto &xi : x)
std::cout << xi << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment