The preconditioned solver example.
This example depends on simple-solver.
Introduction
About the example
The commented program
#include <ginkgo/ginkgo.hpp>
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
Some shortcuts
Print version information
Figure out where to run the code
std::shared_ptr<gko::Executor> exec;
if (argc == 1 || std::string(argv[1]) == "reference") {
exec = gko::ReferenceExecutor::create();
} else if (argc == 2 && std::string(argv[1]) == "omp") {
} else if (argc == 2 && std::string(argv[1]) == "cuda" &&
} else {
std::cerr << "Usage: " << argv[0] << " [executor]" << std::endl;
std::exit(-1);
}
Read data
auto A =
share(gko::read<mtx>(std::ifstream(
"data/A.mtx"), exec));
auto b = gko::read<vec>(std::ifstream("data/b.mtx"), exec);
auto x = gko::read<vec>(std::ifstream("data/x0.mtx"), exec);
Create solver factory
auto solver_gen =
cg::build()
.with_criteria(
gko::stop::Iteration::build().with_max_iters(20u).on(exec),
.with_reduction_factor(1e-20)
.on(exec))
Add preconditioner, these 2 lines are the only difference from the simple solver example
.with_preconditioner(bj::build().with_max_block_size(8u).on(exec))
.on(exec);
Create solver
auto solver = solver_gen->generate(A);
Solve system
Print solution
std::cout << "Solution (x): \n";
Calculate residual
auto one = gko::initialize<vec>({1.0}, exec);
auto neg_one = gko::initialize<vec>({-1.0}, exec);
auto res = gko::initialize<vec>({0.0}, exec);
b->compute_norm2(
lend(res));
std::cout << "Residual norm sqrt(r^T r): \n";
}
Results
This is the expected output:
Solution (x):
19 1
0.252218
0.108645
0.0662811
0.0630433
0.0384088
0.0396536
0.0402648
0.0338935
0.0193098
0.0234653
0.0211499
0.0196413
0.0199151
0.0181674
0.0162722
0.0150714
0.0107016
0.0121141
0.0123025
Residual norm sqrt(r^T r):
1 1
9.08137e-16
Comments about programming and debugging
The plain program
#include <ginkgo/ginkgo.hpp>
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
std::shared_ptr<gko::Executor> exec;
if (argc == 1 || std::string(argv[1]) == "reference") {
exec = gko::ReferenceExecutor::create();
} else if (argc == 2 && std::string(argv[1]) == "omp") {
} else if (argc == 2 && std::string(argv[1]) == "cuda" &&
} else {
std::cerr << "Usage: " << argv[0] << " [executor]" << std::endl;
std::exit(-1);
}
auto A =
share(gko::read<mtx>(std::ifstream(
"data/A.mtx"), exec));
auto b = gko::read<vec>(std::ifstream("data/b.mtx"), exec);
auto x = gko::read<vec>(std::ifstream("data/x0.mtx"), exec);
auto solver_gen =
cg::build()
.with_criteria(
gko::stop::Iteration::build().with_max_iters(20u).on(exec),
.with_reduction_factor(1e-20)
.on(exec))
.with_preconditioner(bj::build().with_max_block_size(8u).on(exec))
.on(exec);
auto solver = solver_gen->generate(A);
std::cout << "Solution (x): \n";
auto one = gko::initialize<vec>({1.0}, exec);
auto neg_one = gko::initialize<vec>({-1.0}, exec);
auto res = gko::initialize<vec>({0.0}, exec);
b->compute_norm2(
lend(res));
std::cout << "Residual norm sqrt(r^T r): \n";
}