Skip to content

Commit

Permalink
Compile vexcl version of the mixed_precision example
Browse files Browse the repository at this point in the history
  • Loading branch information
ddemidov committed Aug 26, 2018
1 parent 332fdc6 commit 31f49ad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ if (VexCL_FOUND)
vexcl_add_executables(cpr_drs_vexcl cpr_drs.cpp)
target_link_libraries(cpr_drs_vexcl INTERFACE amgcl_example)
target_compile_definitions(cpr_drs_vexcl INTERFACE SOLVER_BACKEND_VEXCL)

vexcl_add_executables(mixed_precision_vexcl mixed_precision.cpp)
target_link_libraries(mixed_precision_vexcl INTERFACE amgcl_example)
target_compile_definitions(mixed_precision_vexcl INTERFACE SOLVER_BACKEND_VEXCL)
endif()

if (TARGET viennacl_target)
Expand Down
55 changes: 43 additions & 12 deletions examples/mixed_precision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@
#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>

#if defined(SOLVER_BACKEND_VEXCL)
# include <amgcl/backend/vexcl.hpp>
typedef amgcl::backend::vexcl<float> fBackend;
typedef amgcl::backend::vexcl<double> dBackend;
#else
# ifndef SOLVER_BACKEND_BUILTIN
# define SOLVER_BACKEND_BUILTIN
# endif
# include <amgcl/backend/builtin.hpp>
typedef amgcl::backend::builtin<float> fBackend;
typedef amgcl::backend::builtin<double> dBackend;
#endif

#include "sample_problem.hpp"

namespace amgcl { profiler<> prof; }
Expand All @@ -19,44 +31,63 @@ int main() {
// double-precision Krylov solver.
typedef
amgcl::amg<
amgcl::backend::builtin<float>,
fBackend,
amgcl::coarsening::smoothed_aggregation,
amgcl::relaxation::spai0
>
Precond;

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

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

fBackend::params bprm_f;
dBackend::params bprm_d;

#ifdef SOLVER_BACKEND_VEXCL
vex::Context ctx(vex::Filter::Env);
std::cout << ctx << std::endl;

bprm_f.q = ctx;
bprm_d.q = ctx;
#endif

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);

#if defined(SOLVER_BACKEND_VEXCL)
dBackend::matrix A_d(ctx, n, n, ptr, col, val_d);

vex::vector<double> f(ctx, rhs);
vex::vector<double> x(ctx, n);
x = 0;
#elif defined(SOLVER_BACKEND_BUILTIN)
auto A_d = std::tie(n, ptr, col, val_d);
std::vector<double> &f = rhs;
std::vector<double> x(n, 0.0);
#endif

prof.tic("setup");
Solver S(n);
Precond P(A_f);
Solver S(n, Solver::params(), bprm_d);
Precond P(A_f, Precond::params(), bprm_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);
std::tie(iters, error) = S(A_d, P, f, x);
prof.toc("solve");

std::cout << "Iterations: " << iters << std::endl
Expand Down

0 comments on commit 31f49ad

Please sign in to comment.