Skip to content

Commit

Permalink
Review update:
Browse files Browse the repository at this point in the history
- reverts norm/dot overload with out pointer paramter

Co-authored-by: Tobias Ribizel <ribizel@kit.edu>
  • Loading branch information
MarcelKoch and upsj committed Jul 22, 2021
1 parent 24c355b commit 1c4c37c
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ int main(int argc, char *argv[])
b->copy_from(host_x.get());

// Calculate initial residual by overwriting b
auto initres = 0.0;
auto initres = gko::initialize<real_vec>({0.0}, exec->get_master());
A->apply(1.0, lend(x), -1.0, lend(b));
b->compute_norm2(&initres);
b->compute_norm2(lend(initres));

// copy b again
b->copy_from(host_x.get());
Expand Down Expand Up @@ -144,15 +144,15 @@ int main(int argc, char *argv[])
time += std::chrono::duration_cast<std::chrono::nanoseconds>(toc - tic);

// Calculate residual
auto res = 0.0;
auto res = gko::initialize<real_vec>({0.0}, exec->get_master());
A->apply(1.0, lend(x), -1.0, lend(b));
b->compute_norm2(&res);
b->compute_norm2(lend(res));
auto impl_res = gko::as<real_vec>(logger->get_implicit_sq_resnorm());

std::cout << "Initial residual norm sqrt(r^T r):\n";
gko::write(std::cout, initres);
write(std::cout, lend(initres));
std::cout << "Final residual norm sqrt(r^T r):\n";
gko::write(std::cout, res);
write(std::cout, lend(res));
std::cout << "Implicit residual norm squared (r^2):\n";
gko::write(std::cout, lend(impl_res));

Expand Down
16 changes: 8 additions & 8 deletions examples/cb-gmres/cb-gmres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ int main(int argc, char *argv[])
b_host->at(i, 0) =
ValueType{1} / std::sqrt(static_cast<ValueType>(A_size[0]));
}
auto b_norm = 0.0;
b_host->compute_norm2(&b_norm);
auto b_norm = gko::initialize<real_vec>({0.0}, exec->get_master());
b_host->compute_norm2(lend(b_norm));
auto b = clone(exec, lend(b_host));

// As an initial guess, use the right-hand side
Expand Down Expand Up @@ -195,21 +195,21 @@ int main(int argc, char *argv[])
// To measure if your solution has actually converged, the error of the
// solution is measured.
// To compute the residual, the (advanced) apply method is used.
auto res_norm_keep = 0.0;
auto res_norm_reduce = 0.0;
auto res_norm_keep = gko::initialize<real_vec>({0.0}, exec->get_master());
auto res_norm_reduce = gko::initialize<real_vec>({0.0}, exec->get_master());
auto tmp = gko::clone(gko::lend(b));

// tmp = Ax - tmp
A->apply(1.0, lend(x_keep), -1.0, lend(tmp));
tmp->compute_norm2(&res_norm_keep);
tmp->compute_norm2(lend(res_norm_keep));

std::cout << "\nResidual norm without compression:\n";
gko::write(std::cout, res_norm_keep);
gko::write(std::cout, lend(res_norm_keep));

tmp->copy_from(lend(b));
A->apply(1.0, lend(x_reduce), -1.0, lend(tmp));
tmp->compute_norm2(&res_norm_reduce);
tmp->compute_norm2(lend(res_norm_reduce));

std::cout << "\nResidual norm with compression:\n";
gko::write(std::cout, res_norm_reduce);
write(std::cout, lend(res_norm_reduce));
}
14 changes: 8 additions & 6 deletions examples/custom-logger/custom-logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ gko::remove_complex<ValueType> compute_norm(
// Get the executor of the vector
auto exec = b->get_executor();
// Initialize a result scalar containing the value 0.0.
auto b_norm = gko::remove_complex<ValueType>{0.0};
auto b_norm =
gko::initialize<gko::matrix::Dense<gko::remove_complex<ValueType>>>(
{0.0}, exec);
// Use the dense `compute_norm2` function to compute the norm.
b->compute_norm2(&b_norm);
b->compute_norm2(gko::lend(b_norm));
// Use the other utility function to return the norm contained in `b_norm`
return b_norm;
return get_first_element(gko::lend(b_norm));
}

// Custom logger class which intercepts the residual norm scalar and solution
Expand Down Expand Up @@ -345,10 +347,10 @@ int main(int argc, char *argv[])
// call the apply method, which in this case is an spmv and equivalent to
// the LAPACK z_spmv routine. Finally, you compute the euclidean 2-norm with
// the compute_norm2 function.
auto res = 0.0;
auto res = gko::initialize<real_vec>({0.0}, exec->get_master());
A->apply(1.0, gko::lend(x), -1.0, gko::lend(b));
b->compute_norm2(&res);
b->compute_norm2(lend(res));

std::cout << "Residual norm sqrt(r^T r):\n";
gko::write(std::cout, res);
write(std::cout, lend(res));
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ void run_solver(volatile bool *stop_iteration_process,
write(std::cout, lend(x));

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

std::cout << "Residual norm sqrt(r^T r): \n";
gko::write(std::cout, res);
write(std::cout, lend(res));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ int main(int argc, char *argv[])
write(std::cout, gko::lend(x));

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

std::cout << "Residual norm sqrt(r^T r):\n";
gko::write(std::cout, res);
write(std::cout, lend(res));
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ int main(int argc, char *argv[])
write(std::cout, gko::lend(x));

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

std::cout << "GMRES iteration count: " << logger->get_num_iterations()
<< "\n";
std::cout << "GMRES execution time [ms]: "
<< static_cast<double>(time.count()) / 100000000.0 << "\n";
std::cout << "Residual norm sqrt(r^T r):\n";
gko::write(std::cout, res);
write(std::cout, lend(res));
}
12 changes: 6 additions & 6 deletions examples/iterative-refinement/iterative-refinement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ int main(int argc, char *argv[])
b->copy_from(host_x.get());

// Calculate initial residual by overwriting b
auto initres = 0.0;
auto initres = gko::initialize<real_vec>({0.0}, exec->get_master());
A->apply(1.0, lend(x), -1.0, lend(b));
b->compute_norm2(&initres);
b->compute_norm2(lend(initres));

// copy b again
b->copy_from(host_x.get());
Expand Down Expand Up @@ -146,14 +146,14 @@ int main(int argc, char *argv[])
time += std::chrono::duration_cast<std::chrono::nanoseconds>(toc - tic);

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

std::cout << "Initial residual norm sqrt(r^T r):\n";
gko::write(std::cout, initres);
write(std::cout, lend(initres));
std::cout << "Final residual norm sqrt(r^T r):\n";
gko::write(std::cout, res);
write(std::cout, lend(res));

// Print solver statistics
std::cout << "IR iteration count: " << logger->get_num_iterations()
Expand Down
17 changes: 9 additions & 8 deletions examples/mixed-precision-ir/mixed-precision-ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ int main(int argc, char *argv[])
b->copy_from(host_x.get());

// Calculate initial residual by overwriting b
auto initres = 0.0;
auto initres = gko::initialize<real_vec>({0.0}, exec->get_master());
A->apply(1.0, lend(x), -1.0, lend(b));
b->compute_norm2(&initres);
b->compute_norm2(lend(initres));

// Build lower-precision system matrix and residual
auto solver_A = solver_mtx::create(exec);
Expand All @@ -137,7 +137,7 @@ int main(int argc, char *argv[])
// Solve system
exec->synchronize();
std::chrono::nanoseconds time(0);
auto res = 0.0;
auto res = gko::initialize<real_vec>({0.0}, exec->get_master());
auto inner_solution = solver_vec::create(exec);
auto outer_delta = vec::create(exec);
auto tic = std::chrono::steady_clock::now();
Expand All @@ -147,10 +147,11 @@ int main(int argc, char *argv[])

// convert residual to inner precision
outer_residual->convert_to(lend(inner_residual));
outer_residual->compute_norm2(&res);
outer_residual->compute_norm2(lend(res));

// break if we exceed the number of iterations or have converged
if (iter > max_outer_iters || res / initres < outer_reduction_factor) {
if (iter > max_outer_iters ||
res->at(0) / initres->at(0) < outer_reduction_factor) {
break;
}

Expand All @@ -176,12 +177,12 @@ int main(int argc, char *argv[])

// Calculate residual
A->apply(1.0, lend(x), -1.0, lend(b));
b->compute_norm2(&res);
b->compute_norm2(lend(res));

std::cout << "Initial residual norm sqrt(r^T r):\n";
gko::write(std::cout, initres);
write(std::cout, lend(initres));
std::cout << "Final residual norm sqrt(r^T r):\n";
gko::write(std::cout, res);
write(std::cout, lend(res));

// Print solver statistics
std::cout << "MPIR iteration count: " << iter << std::endl;
Expand Down
35 changes: 18 additions & 17 deletions examples/mixed-spmv/mixed-spmv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,41 +244,42 @@ int main(int argc, char *argv[])
// method, which in this case is an axpy and equivalent to the LAPACK axpy
// routine. Finally, you compute the euclidean 2-norm with the compute_norm2
// function.
auto hp_x_norm = 0.0;
auto lp_diff_norm = 0.0;
auto hplp_diff_norm = 0.0;
auto lplp_diff_norm = 0.0;
auto lphp_diff_norm = 0.0;
auto hp_x_norm = gko::initialize<real_vec>({0.0}, exec->get_master());
auto lp_diff_norm = gko::initialize<real_vec>({0.0}, exec->get_master());
auto hplp_diff_norm = gko::initialize<real_vec>({0.0}, exec->get_master());
auto lplp_diff_norm = gko::initialize<real_vec>({0.0}, exec->get_master());
auto lphp_diff_norm = gko::initialize<real_vec>({0.0}, exec->get_master());
auto lp_diff = hp_x->clone();
auto hplp_diff = hp_x->clone();
auto lplp_diff = hp_x->clone();
auto lphp_diff = hp_x->clone();

hp_x->compute_norm2(&hp_x_norm);
hp_x->compute_norm2(lend(hp_x_norm));
lp_diff->add_scaled(-1.0, lend(lp_x));
lp_diff->compute_norm2(&lp_diff_norm);
lp_diff->compute_norm2(lend(lp_diff_norm));
hplp_diff->add_scaled(-1.0, lend(hplp_x));
hplp_diff->compute_norm2(&hplp_diff_norm);
hplp_diff->compute_norm2(lend(hplp_diff_norm));
lplp_diff->add_scaled(-1.0, lend(lplp_x));
lplp_diff->compute_norm2(&lplp_diff_norm);
lplp_diff->compute_norm2(lend(lplp_diff_norm));
lphp_diff->add_scaled(-1.0, lend(lphp_x));
lphp_diff->compute_norm2(&lphp_diff_norm);
lphp_diff->compute_norm2(lend(lphp_diff_norm));
exec->synchronize();

std::cout.precision(10);
std::cout << std::scientific;
std::cout << "High Precision time(s): " << hp_sec << std::endl;
std::cout << "High Precision result norm: " << hp_x_norm << std::endl;
std::cout << "High Precision result norm: " << hp_x_norm->get_values()[0]
<< std::endl;
std::cout << "Low Precision time(s): " << lp_sec << std::endl;
std::cout << "Low Precision relative error: " << lp_diff_norm / hp_x_norm
std::cout << "Low Precision relative error: "
<< lp_diff_norm->get_values()[0] / hp_x_norm->get_values()[0]
<< "\n";
std::cout << "Hp * Lp -> Hp time(s): " << hplp_sec << std::endl;
std::cout << "Hp * Lp -> Hp relative error: " << hplp_diff_norm / hp_x_norm
std::cout << "Hp * Lp -> Hp relative error: "
<< hplp_diff_norm->get_values()[0] / hp_x_norm->get_values()[0]
<< "\n";
std::cout << "Lp * Lp -> Hp time(s): " << lplp_sec << std::endl;
std::cout << "Lp * Lp -> Hp relative error: " << lplp_diff_norm / hp_x_norm
<< "\n";
std::cout << "Lp * Hp -> Hp time(s): " << lplp_sec << std::endl;
std::cout << "Lp * Hp -> Hp relative error: " << lphp_diff_norm / hp_x_norm
std::cout << "Lp * Lp -> Hp relative error: "
<< lplp_diff_norm->get_values()[0] / hp_x_norm->get_values()[0]
<< "\n";
}
6 changes: 3 additions & 3 deletions examples/papi-logging/papi-logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ int main(int argc, char *argv[])
write(std::cout, lend(x));

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

std::cout << "Residual norm sqrt(r^T r): \n";
gko::write(std::cout, res);
write(std::cout, lend(res));
}
6 changes: 3 additions & 3 deletions examples/preconditioned-solver/preconditioned-solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ int main(int argc, char *argv[])
write(std::cout, lend(x));

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

std::cout << "Residual norm sqrt(r^T r):\n";
gko::write(std::cout, res);
write(std::cout, lend(res));
}
6 changes: 3 additions & 3 deletions examples/simple-solver-logging/simple-solver-logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ int main(int argc, char *argv[])
write(std::cout, lend(x));

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

std::cout << "Residual norm sqrt(r^T r):\n";
gko::write(std::cout, res);
write(std::cout, lend(res));
}
6 changes: 3 additions & 3 deletions examples/simple-solver/simple-solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ int main(int argc, char *argv[])
// which in this case is an spmv and equivalent to the LAPACK z_spmv
// routine. Finally, you compute the euclidean 2-norm with the compute_norm2
// function.
auto res = 0.0;
auto res = gko::initialize<real_vec>({0.0}, exec->get_master());
A->apply(1.0, lend(x), -1.0, lend(b));
b->compute_norm2(&res);
b->compute_norm2(lend(res));

std::cout << "Residual norm sqrt(r^T r):\n";
gko::write(std::cout, res);
write(std::cout, lend(res));
}
20 changes: 0 additions & 20 deletions include/ginkgo/core/base/mtx_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,6 @@ inline void write(StreamType &&os, MatrixType *matrix,
write_raw(os, data, layout);
}

/**
* Helper to write a single value in the same format as @ref write
*
* @note Cannot use ADL to find this overload.
*
* @tparam ValueType a scalar type.
* @tparam StreamType type of stream used to write the data to
*
* @param os output stream where the data is to be written
* @param val the value to write
* @param layout the layout used in the output
*/
template <typename ValueType, typename StreamType>
inline void write(StreamType &&os, ValueType val,
layout_type layout = layout_type::array)
{
matrix_data<ValueType, int32> data{{val}};
write_raw(os, data, layout);
}


} // namespace gko

Expand Down
Loading

0 comments on commit 1c4c37c

Please sign in to comment.