Skip to content

Commit

Permalink
Add a mixed precision example
Browse files Browse the repository at this point in the history
  • Loading branch information
ddemidov committed Aug 26, 2018
1 parent f90255c commit bc78704
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_amgcl_example(schur_pressure_correction schur_pressure_correction.cpp)
add_amgcl_example(cpr cpr.cpp)
add_amgcl_example(cpr_drs cpr_drs.cpp)
add_amgcl_example(custom_adapter custom_adapter.cpp)
add_amgcl_example(mixed_precision mixed_precision.cpp)

add_amgcl_example(ublas ublas.cpp)
target_link_libraries(ublas ${Boost_SERIALIZATION_LIBRARY})
Expand Down
65 changes: 65 additions & 0 deletions examples/mixed_precision.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <vector>
#include <tuple>

#include <amgcl/adapter/crs_tuple.hpp>
#include <amgcl/backend/builtin.hpp>
#include <amgcl/amg.hpp>
#include <amgcl/coarsening/smoothed_aggregation.hpp>
#include <amgcl/relaxation/spai0.hpp>
#include <amgcl/solver/cg.hpp>
#include <amgcl/profiler.hpp>

#include "sample_problem.hpp"

namespace amgcl { profiler<> prof; }
using amgcl::prof;

int main() {
// Combine single-precision preconditioner with a
// double-precision Krylov solver.
typedef
amgcl::amg<
amgcl::backend::builtin<float>,
amgcl::coarsening::smoothed_aggregation,
amgcl::relaxation::spai0
>
Precond;

// Solver is in double precision:
typedef
amgcl::solver::cg<
amgcl::backend::builtin<double>
>
Solver;

std::vector<int> ptr, col;
std::vector<double> val_d;
std::vector<double> rhs;

prof.tic("assemble");
int n = sample_problem(128, val_d, col, ptr, rhs);
prof.toc("assemble");

std::vector<double> x(n, 0.0);
std::vector<float> val_f(val_d.begin(), val_d.end());

auto A_f = std::tie(n, ptr, col, val_f);
auto A_d = std::tie(n, ptr, col, val_d);

prof.tic("setup");
Solver S(n);
Precond P(A_f);
prof.toc("setup");

std::cout << P << std::endl;

int iters;
double error;
prof.tic("solve");
std::tie(iters, error) = S(A_d, P, rhs, x);
prof.toc("solve");

std::cout << "Iterations: " << iters << std::endl
<< "Error: " << error << std::endl
<< prof << std::endl;
}

0 comments on commit bc78704

Please sign in to comment.