Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ add_subdirectory(preconditioner-export)
add_subdirectory(simple-solver)
add_subdirectory(simple-solver-logging)
add_subdirectory(three-pt-stencil-solver)
add_subdirectory(twentyseven-pt-stencil-solver)
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 bj = gko::preconditioner::Jacobi<ValueType, IndexType>;
Expand Down Expand Up @@ -86,13 +88,13 @@ 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());
const gko::remove_complex<ValueType> reduction_factor = 1e-7;
const RealValueType reduction_factor = 1e-7;
auto iter_stop =
gko::stop::Iteration::build().with_max_iters(10000u).on(exec);
auto tol_stop = gko::stop::ResidualNormReduction<ValueType>::build()
Expand Down Expand Up @@ -129,7 +131,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
10 changes: 5 additions & 5 deletions examples/adaptiveprecision-blockjacobi/doc/results.dox
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ This is the expected output:

@code{.cpp}

Initial residual norm sqrt(r^T r):
Initial residual norm sqrt(r^T r):
%%MatrixMarket matrix array real general
1 1
194.679
Final residual norm sqrt(r^T r):
Final residual norm sqrt(r^T r):
%%MatrixMarket matrix array real general
1 1
2.8994e-11
CG iteration count: 8
CG execution time [ms]: 4.10581
5.69384e-06
CG iteration count: 5
CG execution time [ms]: 2.04779

@endcode

Expand Down
52 changes: 29 additions & 23 deletions examples/custom-logger/custom-logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Add the vector header for storing the logger's data
#include <vector>

// Utility function which gets the scalar value of a Ginkgo gko::matrix::Dense
// matrix representing the norm of a vector.

// Utility function which returns the first element (position [0, 0]) from a
// given gko::matrix::Dense matrix / vector.
template <typename ValueType>
gko::remove_complex<ValueType> get_norm(
const gko::matrix::Dense<ValueType> *norm)
ValueType get_first_element(const gko::matrix::Dense<ValueType> *mtx)
{
// Put the value on CPU thanks to the master executor
auto cpu_norm = clone(norm->get_executor()->get_master(), norm);
// Return the scalar value contained at position (0, 0)
return std::real(cpu_norm->at(0, 0));
// Copy the matrix / vector to the host device before accessing the value in
// case it is stored in a GPU.
return mtx->get_executor()->copy_val_to_host(mtx->get_const_values());
}


// Utility function which computes the norm of a Ginkgo gko::matrix::Dense
// vector.
template <typename ValueType>
Expand All @@ -69,18 +69,21 @@ 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::initialize<gko::matrix::Dense<ValueType>>({0.0}, exec);
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(lend(b_norm));
// Use the other utility function to return the norm contained in `b_norm``
return get_norm(lend(b_norm));
b->compute_norm2(gko::lend(b_norm));
// Use the other utility function to return the norm contained in `b_norm`
return get_first_element(gko::lend(b_norm));
}

// Custom logger class which intercepts the residual norm scalar and solution
// vector in order to print a table of real vs recurrent (internal to the
// solvers) residual norms.
template <typename ValueType>
struct ResidualLogger : gko::log::Logger {
using RealValueType = gko::remove_complex<ValueType>;
// Output the logger's data in a table format
void write() const
{
Expand Down Expand Up @@ -111,6 +114,7 @@ struct ResidualLogger : gko::log::Logger {
}

using gko_dense = gko::matrix::Dense<ValueType>;
using gko_real_dense = gko::matrix::Dense<RealValueType>;

// Customize the logging hook which is called everytime an iteration is
// completed
Expand All @@ -122,9 +126,9 @@ struct ResidualLogger : gko::log::Logger {
{
// If the solver shares a residual norm, log its value
if (residual_norm) {
auto dense_norm = gko::as<gko_dense>(residual_norm);
auto dense_norm = gko::as<gko_real_dense>(residual_norm);
// Add the norm to the `recurrent_norms` vector
recurrent_norms.push_back(get_norm(dense_norm));
recurrent_norms.push_back(get_first_element(gko::lend(dense_norm)));
// Otherwise, use the recurrent residual vector
} else {
auto dense_residual = gko::as<gko_dense>(residual);
Expand Down Expand Up @@ -176,9 +180,9 @@ struct ResidualLogger : gko::log::Logger {
// Pointer to the right hand sides
const gko_dense *b;
// Vector which stores all the recurrent residual norms
mutable std::vector<ValueType> recurrent_norms{};
mutable std::vector<RealValueType> recurrent_norms{};
// Vector which stores all the real residual norms
mutable std::vector<ValueType> real_norms{};
mutable std::vector<RealValueType> real_norms{};
// Vector which stores all the iteration numbers
mutable std::vector<std::size_t> iterations{};
};
Expand All @@ -191,8 +195,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 @@ -241,7 +247,7 @@ int main(int argc, char *argv[])
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);
const gko::remove_complex<ValueType> reduction_factor = 1e-7;
const RealValueType reduction_factor = 1e-7;

// @sect3{Creating the solver}
// Generate the gko::solver factory. Ginkgo uses the concept of Factories to
Expand Down Expand Up @@ -283,11 +289,11 @@ int main(int argc, char *argv[])
// Finally, solve the system. The solver, being a gko::LinOp, can be applied
// to a right hand side, b to
// obtain the solution, x.
solver->apply(lend(b), lend(x));
solver->apply(gko::lend(b), gko::lend(x));

// Print the solution to the command line.
std::cout << "Solution (x): \n";
write(std::cout, lend(x));
write(std::cout, gko::lend(x));

// Print the table of the residuals obtained from the logger
logger->write();
Expand All @@ -301,10 +307,10 @@ 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);
A->apply(lend(one), lend(x), lend(neg_one), lend(b));
b->compute_norm2(lend(res));
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));

std::cout << "Residual norm sqrt(r^T r): \n";
write(std::cout, lend(res));
write(std::cout, gko::lend(res));
}
6 changes: 4 additions & 2 deletions examples/custom-logger/doc/results.dox
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
The following is the expected result:

@code{.cpp}

Solution (x):
%%MatrixMarket matrix array real general
19 1
Expand All @@ -24,8 +25,8 @@ Solution (x):
0.0107016
0.0121141
0.0123025
Recurrent vs real residual norm:
| Iteration| Recurrent Residual Norm| Real Residual Norm|
Recurrent vs true residual norm:
| Iteration| Recurrent Residual Norm| True Residual Norm|
|----------|-------------------------|-------------------------|
| 0| 4.358899e+00| 4.358899e+00|
| 1| 2.304548e+00| 2.304548e+00|
Expand All @@ -52,6 +53,7 @@ Residual norm sqrt(r^T r):
%%MatrixMarket matrix array real general
1 1
2.10788e-15

@endcode

<h3> Comments about programming and debugging </h3>
5 changes: 3 additions & 2 deletions examples/custom-matrix-format/custom-matrix-format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ 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>;
Expand Down Expand Up @@ -274,7 +275,7 @@ int main(int argc, char *argv[])

// problem:
auto correct_u = [](ValueType x) { return x * x * x; };
auto f = [](ValueType x) { return ValueType(6) * x; };
auto f = [](ValueType x) { return ValueType{6} * x; };
auto u0 = correct_u(0);
auto u1 = correct_u(1);

Expand All @@ -286,7 +287,7 @@ int main(int argc, char *argv[])
u->get_values()[i] = 0.0;
}

const ValueType 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
2 changes: 1 addition & 1 deletion examples/custom-matrix-format/stencil_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ void stencil_kernel(std::size_t size, const ValueType *coefs,
stencil_kernel_impl<<<grid_size, block_size>>>(size, coefs, b, x);
}

INSTANTIATE_FOR_EACH_VALUE_TYPE(STENCIL_KERNEL);
INSTANTIATE_FOR_EACH_VALUE_TYPE(STENCIL_KERNEL);
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
90 changes: 46 additions & 44 deletions examples/custom-stopping-criterion/doc/results.dox
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,59 @@
This is the expected output:

@code{.cpp}

.
.
.
.
.
.
[LOG] >>> iteration 15331 completed with solver LinOp[gko::solver::Bicgstab<double>,0x7f2f38003c10] with residual LinOp[gko::matrix::Dense<double>,0x7f2f380048e0], solution LinOp[gko::LinOp const*,0] and residual_norm LinOp[gko::LinOp const*,0]
LinOp[gko::matrix::Dense<double>,0x7f2f380048e0][
6.21705e-164
-1.18919e-164
7.89129e-165
-6.78013e-165
-2.42405e-164
-4.29503e-165
6.16567e-166
-3.34064e-164
6.38335e-165
7.86768e-165
-1.80969e-165
-4.17609e-166
2.5395e-165
-5.34283e-166
-4.10476e-166
-1.50132e-166
-1.25732e-165
-1.82819e-166
-2.0927e-165
]
[LOG] >>> iteration 22516 completed with solver LinOp[gko::solver::Bicgstab<double>,0x7fe6a4003710] with residual LinOp[gko::matrix::Dense<double>,0x7fe6a40050b0], solution LinOp[gko::matrix::Dense<double>,0x7fe6a40048e0] and residual_norm LinOp[gko::LinOp const*,0]
LinOp[gko::matrix::Dense<double>,0x7fe6a40050b0][
5.17803e-164
-7.6865e-165
-2.06149e-164
-4.84737e-165
-3.36597e-164
2.22353e-164
1.47594e-165
-1.78592e-165
-6.17274e-166
-3.02681e-166
7.82009e-166
8.57102e-165
-1.28879e-164
-2.62076e-165
2.55329e-165
-5.95988e-166
-5.79273e-166
-5.20172e-166
-6.79458e-166
]

// Typing 'stop' stops the solver.

User input command 'stop' - The solver will stop

LinOp[gko::matrix::Dense<double>,0x7f2f38004730][
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
LinOp[gko::matrix::Dense<double>,0x7fe6a40048e0][
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
]

Solver stopped
Expand Down Expand Up @@ -82,7 +83,8 @@ Solution (x):
Residual norm sqrt(r^T r):
%%MatrixMarket matrix array real general
1 1
1.06135e-15
6.50306e-16

@endcode

<h3> Comments about programming and debugging </h3>
Loading