Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update the remaining examples
  • Loading branch information
Thomas Grützmacher committed Aug 11, 2020
commit 4b9d9c2b86b908984da7b458b58f58238b51d69e
2 changes: 1 addition & 1 deletion examples/custom-logger/custom-logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ValueType get_first_element(const gko::matrix::Dense<ValueType> *mtx)
{
// Copy the matrix / vector to the host device before accessing the value in
// case it is stored in a GPU.
return gko::clone(mtx->get_executor()->get_master(), mtx)->at(0, 0);
return mtx->get_executor()->copy_val_to_host(mtx->get_const_values());
}


Expand Down
2 changes: 1 addition & 1 deletion examples/custom-matrix-format/custom-matrix-format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ int main(int argc, char *argv[])
u->get_values()[i] = 0.0;
}

const RealValueType reduction_factor = 1e-7;
const RealValueType reduction_factor{1e-7};
// Generate solver and solve the system
cg::build()
.with_criteria(gko::stop::Iteration::build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ void run_solver(volatile bool *stop_iteration_process,
{
// Some shortcuts
using ValueType = double;
using RealValueType = gko::remove_complex<ValueType>;
using IndexType = int;

using mtx = gko::matrix::Csr<ValueType, IndexType>;
using vec = gko::matrix::Dense<ValueType>;
using real_vec = gko::matrix::Dense<RealValueType>;
using bicg = gko::solver::Bicgstab<ValueType>;

// Read Data
Expand Down Expand Up @@ -126,7 +128,7 @@ void run_solver(volatile bool *stop_iteration_process,
// 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);
auto res = gko::initialize<real_vec>({0.0}, exec);
A->apply(lend(one), lend(x), lend(neg_one), lend(b));
b->compute_norm2(lend(res));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ int main(int argc, char *argv[])
{
// Some shortcuts
using ValueType = double;
using RealValueType = gko::remove_complex<ValueType>;
using IndexType = int;

using vec = gko::matrix::Dense<ValueType>;
using real_vec = gko::matrix::Dense<RealValueType>;
using mtx = gko::matrix::Csr<ValueType, IndexType>;
using gmres = gko::solver::Gmres<ValueType>;

Expand Down Expand Up @@ -96,7 +98,7 @@ int main(int argc, char *argv[])
// Generating a solver factory tied to a specific preconditioner makes sense
// if there are several very similar systems to solve, and the same
// solver+preconditioner combination is expected to be effective.
const gko::remove_complex<ValueType> reduction_factor = 1e-7;
const RealValueType reduction_factor{1e-7};
auto ilu_gmres_factory =
gmres::build()
.with_criteria(
Expand All @@ -120,7 +122,7 @@ int main(int argc, char *argv[])
// 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);
auto res = gko::initialize<real_vec>({0.0}, exec);
A->apply(gko::lend(one), gko::lend(x), gko::lend(neg_one), gko::lend(b));
b->compute_norm2(gko::lend(res));

Expand Down
2 changes: 1 addition & 1 deletion examples/inverse-iteration/inverse-iteration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int main(int argc, char *argv[])
}();
auto y = clone(x);
auto tmp = clone(x);
auto norm = gko::share(gko::initialize<real_vec>({1.0}, exec));
auto norm = gko::initialize<real_vec>({1.0}, exec);
auto inv_norm = clone(this_exec, one);
Comment thread
pratikvn marked this conversation as resolved.
auto g = clone(one);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ int main(int argc, char *argv[])
{
// Some shortcuts
using ValueType = double;
using RealValueType = gko::remove_complex<ValueType>;
using IndexType = int;

using vec = gko::matrix::Dense<ValueType>;
using real_vec = gko::matrix::Dense<RealValueType>;
using mtx = gko::matrix::Csr<ValueType, IndexType>;
using gmres = gko::solver::Gmres<ValueType>;
using ir = gko::solver::Ir<ValueType>;
Expand Down Expand Up @@ -125,7 +127,7 @@ int main(int argc, char *argv[])
auto ilu_preconditioner = ilu_pre_factory->generate(gko::share(par_ilu));

// Create stopping criteria for Gmres
const gko::remove_complex<ValueType> reduction_factor = 1e-12;
const RealValueType reduction_factor{1e-12};
auto iter_stop =
gko::stop::Iteration::build().with_max_iters(1000u).on(exec);
auto tol_stop = gko::stop::ResidualNormReduction<ValueType>::build()
Expand Down Expand Up @@ -172,7 +174,7 @@ int main(int argc, char *argv[])
// 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);
auto res = gko::initialize<real_vec>({0.0}, exec);
A->apply(gko::lend(one), gko::lend(x), gko::lend(neg_one), gko::lend(b));
b->compute_norm2(gko::lend(res));

Expand Down
10 changes: 6 additions & 4 deletions examples/iterative-refinement/iterative-refinement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ int main(int argc, char *argv[])
{
// Some shortcuts
using ValueType = double;
using RealValueType = gko::remove_complex<ValueType>;
using IndexType = int;
using vec = gko::matrix::Dense<ValueType>;
using real_vec = gko::matrix::Dense<RealValueType>;
using mtx = gko::matrix::Csr<ValueType, IndexType>;
using cg = gko::solver::Cg<ValueType>;
using ir = gko::solver::Ir<ValueType>;
Expand Down Expand Up @@ -87,14 +89,14 @@ int main(int argc, char *argv[])
// Calculate initial residual by overwriting b
auto one = gko::initialize<vec>({1.0}, exec);
auto neg_one = gko::initialize<vec>({-1.0}, exec);
auto initres = gko::initialize<vec>({0.0}, exec);
auto initres = gko::initialize<real_vec>({0.0}, exec);
A->apply(lend(one), lend(x), lend(neg_one), lend(b));
b->compute_norm2(lend(initres));

// copy b again
b->copy_from(host_x.get());
gko::size_type max_iters = 10000u;
gko::remove_complex<ValueType> outer_reduction_factor = 1e-12;
RealValueType outer_reduction_factor{1e-12};
auto iter_stop =
gko::stop::Iteration::build().with_max_iters(max_iters).on(exec);
auto tol_stop = gko::stop::ResidualNormReduction<ValueType>::build()
Expand All @@ -107,7 +109,7 @@ int main(int argc, char *argv[])
tol_stop->add_logger(logger);

// Create solver factory
gko::remove_complex<ValueType> inner_reduction_factor = 1e-2;
RealValueType inner_reduction_factor{1e-2};
auto solver_gen =
ir::build()
.with_solver(
Expand All @@ -132,7 +134,7 @@ int main(int argc, char *argv[])
time += std::chrono::duration_cast<std::chrono::nanoseconds>(toc - tic);

// Calculate residual
auto res = gko::initialize<vec>({0.0}, exec);
auto res = gko::initialize<real_vec>({0.0}, exec);
A->apply(lend(one), lend(x), lend(neg_one), lend(b));
b->compute_norm2(lend(res));

Expand Down
11 changes: 7 additions & 4 deletions examples/mixed-precision-ir/mixed-precision-ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ int main(int argc, char *argv[])
{
// Some shortcuts
using ValueType = double;
using RealValueType = gko::remove_complex<ValueType>;
using SolverType = float;
using RealSolverType = gko::remove_complex<SolverType>;
using IndexType = int;
using vec = gko::matrix::Dense<ValueType>;
using real_vec = gko::matrix::Dense<RealValueType>;
using solver_vec = gko::matrix::Dense<SolverType>;
using mtx = gko::matrix::Csr<ValueType, IndexType>;
using solver_mtx = gko::matrix::Csr<SolverType, IndexType>;
using cg = gko::solver::Cg<SolverType>;

gko::size_type max_outer_iters = 100u;
gko::size_type max_inner_iters = 100u;
gko::remove_complex<ValueType> outer_reduction_factor = 1e-12;
gko::remove_complex<SolverType> inner_reduction_factor = 1e-2;
RealValueType outer_reduction_factor{1e-12};
RealSolverType inner_reduction_factor{1e-2};

// Print version information
std::cout << gko::version_info::get() << std::endl;
Expand Down Expand Up @@ -93,7 +96,7 @@ int main(int argc, char *argv[])
// Calculate initial residual by overwriting b
auto one = gko::initialize<vec>({1.0}, exec);
auto neg_one = gko::initialize<vec>({-1.0}, exec);
auto initres_vec = gko::initialize<vec>({0.0}, exec);
auto initres_vec = gko::initialize<real_vec>({0.0}, exec);
A->apply(lend(one), lend(x), lend(neg_one), lend(b));
b->compute_norm2(lend(initres_vec));

Expand Down Expand Up @@ -122,7 +125,7 @@ int main(int argc, char *argv[])
// Solve system
exec->synchronize();
std::chrono::nanoseconds time(0);
auto res_vec = gko::initialize<vec>({0.0}, exec);
auto res_vec = gko::initialize<real_vec>({0.0}, exec);
auto initres = exec->copy_val_to_host(initres_vec->get_const_values());
auto inner_solution = solver_vec::create(exec);
auto outer_delta = vec::create(exec);
Expand Down
6 changes: 4 additions & 2 deletions examples/papi-logging/papi-logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ int main(int argc, char *argv[])
{
// Some shortcuts
using ValueType = double;
using RealValueType = gko::remove_complex<ValueType>;
using IndexType = int;

using vec = gko::matrix::Dense<ValueType>;
using real_vec = gko::matrix::Dense<RealValueType>;
using mtx = gko::matrix::Csr<ValueType, IndexType>;
using cg = gko::solver::Cg<ValueType>;

Expand Down Expand Up @@ -159,7 +161,7 @@ int main(int argc, char *argv[])
auto x = gko::read<vec>(std::ifstream("data/x0.mtx"), exec);

// Generate solver
const gko::remove_complex<ValueType> reduction_factor = 1e-7;
const RealValueType reduction_factor{1e-7};
auto solver_gen =
cg::build()
.with_criteria(
Expand Down Expand Up @@ -199,7 +201,7 @@ int main(int argc, char *argv[])
// 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);
auto res = gko::initialize<real_vec>({0.0}, exec);
A->apply(lend(one), lend(x), lend(neg_one), lend(b));
b->compute_norm2(lend(res));

Expand Down
20 changes: 12 additions & 8 deletions examples/performance-debugging/performance-debugging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ template <typename ValueType>
using vec = gko::matrix::Dense<ValueType>;


template <typename ValueType>
using real_vec = gko::matrix::Dense<gko::remove_complex<ValueType>>;


namespace utils {


Expand All @@ -70,19 +74,19 @@ std::unique_ptr<vec<ValueType>> create_vector(

// utilities for computing norms and residuals
template <typename ValueType>
gko::remove_complex<ValueType> get_norm(const vec<ValueType> *norm)
ValueType get_first_element(const vec<ValueType> *norm)
{
return std::real(clone(norm->get_executor()->get_master(), norm)->at(0, 0));
return norm->get_executor()->copy_val_to_host(norm->get_const_values());
}


template <typename ValueType>
gko::remove_complex<ValueType> compute_norm(const vec<ValueType> *b)
{
auto exec = b->get_executor();
auto b_norm = gko::initialize<vec<ValueType>>({0.0}, exec);
auto b_norm = gko::initialize<real_vec<ValueType>>({0.0}, exec);
b->compute_norm2(gko::lend(b_norm));
return get_norm(gko::lend(b_norm));
return get_first_element(gko::lend(b_norm));
}


Expand Down Expand Up @@ -261,8 +265,8 @@ struct ResidualLogger : gko::log::Logger {
const gko::LinOp *residual_norm) const override
{
if (residual_norm) {
rec_res_norms.push_back(
utils::get_norm(gko::as<vec<ValueType>>(residual_norm)));
rec_res_norms.push_back(utils::get_first_element(
gko::as<real_vec<ValueType>>(residual_norm)));
} else {
rec_res_norms.push_back(
utils::compute_norm(gko::as<vec<ValueType>>(residual)));
Expand Down Expand Up @@ -302,8 +306,8 @@ struct ResidualLogger : gko::log::Logger {
private:
const gko::LinOp *matrix;
const vec<ValueType> *b;
mutable std::vector<ValueType> rec_res_norms;
mutable std::vector<ValueType> true_res_norms;
mutable std::vector<gko::remove_complex<ValueType>> rec_res_norms;
mutable std::vector<gko::remove_complex<ValueType>> true_res_norms;
};


Expand Down
2 changes: 1 addition & 1 deletion examples/poisson-solver/poisson-solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ gko::remove_complex<ValueType> calculate_error(
Closure correct_u)
{
const ValueType h = 1.0 / static_cast<ValueType>(discretization_points + 1);
auto error = 0.0;
gko::remove_complex<ValueType> error = 0.0;
for (int i = 0; i < discretization_points; ++i) {
using std::abs;
const auto xi = static_cast<ValueType>(i + 1) * h;
Expand Down
6 changes: 4 additions & 2 deletions examples/preconditioned-solver/preconditioned-solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ int main(int argc, char *argv[])
{
// Some shortcuts
using ValueType = double;
using RealValueType = gko::remove_complex<ValueType>;
using IndexType = int;

using vec = gko::matrix::Dense<ValueType>;
using real_vec = gko::matrix::Dense<RealValueType>;
using mtx = gko::matrix::Csr<ValueType, IndexType>;
using cg = gko::solver::Cg<ValueType>;
using bj = gko::preconditioner::Jacobi<ValueType, IndexType>;
Expand Down Expand Up @@ -75,7 +77,7 @@ int main(int argc, char *argv[])
auto b = gko::read<vec>(std::ifstream("data/b.mtx"), exec);
auto x = gko::read<vec>(std::ifstream("data/x0.mtx"), exec);

const gko::remove_complex<ValueType> reduction_factor = 1e-7;
const RealValueType reduction_factor{1e-7};
// Create solver factory
auto solver_gen =
cg::build()
Expand All @@ -101,7 +103,7 @@ int main(int argc, char *argv[])
// 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);
auto res = gko::initialize<real_vec>({0.0}, exec);
A->apply(lend(one), lend(x), lend(neg_one), lend(b));
b->compute_norm2(lend(res));

Expand Down
6 changes: 4 additions & 2 deletions examples/simple-solver-logging/simple-solver-logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ int main(int argc, char *argv[])
{
// Some shortcuts
using ValueType = double;
using RealValueType = gko::remove_complex<ValueType>;
using IndexType = int;

using vec = gko::matrix::Dense<ValueType>;
using real_vec = gko::matrix::Dense<RealValueType>;
using mtx = gko::matrix::Csr<ValueType, IndexType>;
using cg = gko::solver::Cg<ValueType>;

Expand Down Expand Up @@ -110,7 +112,7 @@ int main(int argc, char *argv[])
// Add stream_logger only to the ResidualNormReduction criterion Factory
// Note that the logger will get automatically propagated to every criterion
// generated from this factory.
const gko::remove_complex<ValueType> reduction_factor = 1e-7;
const RealValueType reduction_factor{1e-7};
using ResidualCriterionFactory =
gko::stop::ResidualNormReduction<ValueType>::Factory;
std::shared_ptr<ResidualCriterionFactory> residual_criterion =
Expand Down Expand Up @@ -173,7 +175,7 @@ int main(int argc, char *argv[])
// 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);
auto res = gko::initialize<real_vec>({0.0}, exec);
A->apply(lend(one), lend(x), lend(neg_one), lend(b));
b->compute_norm2(lend(res));

Expand Down
8 changes: 5 additions & 3 deletions examples/simple-solver/simple-solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ int main(int argc, char *argv[])
// multiple vectors is a now a natural extension of adding columns/rows are
// necessary.
using ValueType = double;
using RealValueType = gko::remove_complex<ValueType>;
using IndexType = int;
using vec = gko::matrix::Dense<ValueType>;
using real_vec = gko::matrix::Dense<RealValueType>;
// The gko::matrix::Csr class is used here, but any other matrix class such
// as gko::matrix::Coo, gko::matrix::Hybrid, gko::matrix::Ell or
// gko::matrix::Sellp could also be used.
Expand Down Expand Up @@ -106,11 +108,11 @@ int main(int argc, char *argv[])
// build solvers with certain
// properties. Observe the Fluent interface used here. Here a cg solver is
// generated with a stopping criteria of maximum iterations of 20 and a
// residual norm reduction of 1e-15. You also observe that the stopping
// residual norm reduction of 1e-7. You also observe that the stopping
// criteria(gko::stop) are also generated from factories using their build
// methods. You need to specify the executors which each of the object needs
// to be built on.
const gko::remove_complex<ValueType> reduction_factor = 1e-7;
const RealValueType reduction_factor{1e-7};
auto solver_gen =
cg::build()
.with_criteria(
Expand Down Expand Up @@ -147,7 +149,7 @@ int main(int argc, char *argv[])
// the euclidean 2-norm with the compute_norm2 function.
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);
auto res = gko::initialize<real_vec>({0.0}, exec);
A->apply(lend(one), lend(x), lend(neg_one), lend(b));
b->compute_norm2(lend(res));

Expand Down