Ginkgo  Generated from pipelines/130473384 branch based on develop. Ginkgo version 1.1.1
A numerical linear algebra library targeting many-core architectures
The three-pt-stencil-solver program

The 3-point stencil example..

This example depends on simple-solver, poisson-solver.

Table of contents
  1. Introduction
  2. The commented program
  1. Results
  2. The plain program

Introduction

This example solves a 1D Poisson equation:

$ u : [0, 1] \rightarrow R\\ u'' = f\\ u(0) = u0\\ u(1) = u1 $

using a finite difference method on an equidistant grid with K discretization points (K can be controlled with a command line parameter). The discretization is done via the second order Taylor polynomial:

$ u(x + h) = u(x) - u'(x)h + 1/2 u''(x)h^2 + O(h^3)\\ u(x - h) = u(x) + u'(x)h + 1/2 u''(x)h^2 + O(h^3) / +\\ ---------------------- \\ -u(x - h) + 2u(x) + -u(x + h) = -f(x)h^2 + O(h^3) $

For an equidistant grid with K "inner" discretization points $x1, ..., xk, $and step size $ h = 1 / (K + 1)$, the formula produces a system of linear equations

$ 2u_1 - u_2 = -f_1 h^2 + u0\\ -u_(k-1) + 2u_k - u_(k+1) = -f_k h^2, k = 2, ..., K - 1\\ -u_(K-1) + 2u_K = -f_K h^2 + u1\\ $

which is then solved using Ginkgo's implementation of the CG method preconditioned with block-Jacobi. It is also possible to specify on which executor Ginkgo will solve the system via the command line. The function $`f` $is set to $`f(x) = 6x`$ (making the solution $`u(x) = x^3`$), but that can be changed in the main function.

The intention of the example is to show how Ginkgo can be integrated into existing software - the generate_stencil_matrix, generate_rhs, print_solution, compute_error and main function do not reference Ginkgo at all (i.e. they could have been there before the application developer decided to use Ginkgo, and the only part where Ginkgo is introduced is inside the solve_system function.

About the example

The commented program

/ *****************************<DESCRIPTION>***********************************
This example solves a 1D Poisson equation:
u : [0, 1] -> R
u'' = f
u(0) = u0
u(1) = u1
using a finite difference method on an equidistant grid with `K` discretization
points (`K` can be controlled with a command line parameter). The discretization
is done via the second order Taylor polynomial:
u(x + h) = u(x) - u'(x)h + 1/2 u''(x)h^2 + O(h^3)
u(x - h) = u(x) + u'(x)h + 1/2 u''(x)h^2 + O(h^3) / +
---------------------------------------------
-u(x - h) + 2u(x) + -u(x + h) = -f(x)h^2 + O(h^3)
For an equidistant grid with K "inner" discretization points x1, ..., xk, and
step size h = 1 / (K + 1), the formula produces a system of linear equations
2u_1 - u_2 = -f_1 h^2 + u0
-u_(k-1) + 2u_k - u_(k+1) = -f_k h^2, k = 2, ..., K - 1
-u_(K-1) + 2u_K = -f_K h^2 + u1
which is then solved using Ginkgo's implementation of the CG method
preconditioned with block-Jacobi. It is also possible to specify on which
executor Ginkgo will solve the system via the command line.
The function `f` is set to `f(x) = 6x` (making the solution `u(x) = x^3`), but
that can be changed in the `main` function.
The intention of the example is to show how Ginkgo can be integrated into
existing software - the `generate_stencil_matrix`, `generate_rhs`,
`print_solution`, `compute_error` and `main` function do not reference Ginkgo at
all (i.e. they could have been there before the application developer decided to
use Ginkgo, and the only part where Ginkgo is introduced is inside the
`solve_system` function.
*****************************<DESCRIPTION>********************************** /
#include <ginkgo/ginkgo.hpp>
#include <iostream>
#include <map>
#include <string>
#include <vector>

Creates a stencil matrix in CSR format for the given number of discretization points.

void generate_stencil_matrix(int discretization_points, int *row_ptrs,
int *col_idxs, double *values)
{
int pos = 0;
const double coefs[] = {-1, 2, -1};
row_ptrs[0] = pos;
for (int i = 0; i < discretization_points; ++i) {
for (auto ofs : {-1, 0, 1}) {
if (0 <= i + ofs && i + ofs < discretization_points) {
values[pos] = coefs[ofs + 1];
col_idxs[pos] = i + ofs;
++pos;
}
}
row_ptrs[i + 1] = pos;
}
}

Generates the RHS vector given f and the boundary conditions.

template <typename Closure>
void generate_rhs(int discretization_points, Closure f, double u0, double u1,
double *rhs)
{
const auto h = 1.0 / (discretization_points + 1);
for (int i = 0; i < discretization_points; ++i) {
const auto xi = (i + 1) * h;
rhs[i] = -f(xi) * h * h;
}
rhs[0] += u0;
rhs[discretization_points - 1] += u1;
}

Prints the solution u.

void print_solution(int discretization_points, double u0, double u1,
const double *u)
{
std::cout << u0 << '\n';
for (int i = 0; i < discretization_points; ++i) {
std::cout << u[i] << '\n';
}
std::cout << u1 << std::endl;
}

Computes the 1-norm of the error given the computed u and the correct solution function correct_u.

template <typename Closure>
double calculate_error(int discretization_points, const double *u,
Closure correct_u)
{
const auto h = 1.0 / (discretization_points + 1);
auto error = 0.0;
for (int i = 0; i < discretization_points; ++i) {
using std::abs;
const auto xi = (i + 1) * h;
error += abs(u[i] - correct_u(xi)) / abs(correct_u(xi));
}
return error;
}
void solve_system(const std::string &executor_string,
unsigned int discretization_points, int *row_ptrs,
int *col_idxs, double *values, double *rhs, double *u,
double accuracy)
{

Some shortcuts

using val_array = gko::Array<double>;
using idx_array = gko::Array<int>;
const auto &dp = discretization_points;

Figure out where to run the code

const auto omp = gko::OmpExecutor::create();
std::map<std::string, std::shared_ptr<gko::Executor>> exec_map{
{"omp", omp},
{"cuda", gko::CudaExecutor::create(0, omp)},
{"hip", gko::HipExecutor::create(0, omp)},
{"reference", gko::ReferenceExecutor::create()}};

executor where Ginkgo will perform the computation

const auto exec = exec_map.at(executor_string); // throws if not valid

executor where the application initialized the data

const auto app_exec = exec_map["omp"];

Tell Ginkgo to use the data in our application

Matrix: we have to set the executor of the matrix to the one where we want SpMVs to run (in this case exec). When creating array views, we have to specify the executor where the data is (in this case app_exec).

If the two do not match, Ginkgo will automatically create a copy of the data on exec (however, it will not copy the data back once it is done

  • here this is not important since we are not modifying the matrix).
auto matrix = mtx::create(exec, gko::dim<2>(dp),
val_array::view(app_exec, 3 * dp - 2, values),
idx_array::view(app_exec, 3 * dp - 2, col_idxs),
idx_array::view(app_exec, dp + 1, row_ptrs));

RHS: similar to matrix

auto b = vec::create(exec, gko::dim<2>(dp, 1),
val_array::view(app_exec, dp, rhs), 1);

Solution: we have to be careful here - if the executors are different, once we compute the solution the array will not be automatically copied back to the original memory locations. Fortunately, whenever apply is called on a linear operator (e.g. matrix, solver) the arguments automatically get copied to the executor where the operator is, and copied back once the operation is completed. Thus, in this case, we can just define the solution on app_exec, and it will be automatically transferred to/from exec if needed.

auto x = vec::create(app_exec, gko::dim<2>(dp, 1),
val_array::view(app_exec, dp, u), 1);

Generate solver

auto solver_gen =
cg::build()
.with_criteria(
gko::stop::Iteration::build().with_max_iters(dp).on(exec),
.with_reduction_factor(accuracy)
.on(exec))
.with_preconditioner(bj::build().on(exec))
.on(exec);
auto solver = solver_gen->generate(gko::give(matrix));

Solve system

solver->apply(gko::lend(b), gko::lend(x));
}
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " DISCRETIZATION_POINTS [executor]"
<< std::endl;
std::exit(-1);
}
const int discretization_points = argc >= 2 ? std::atoi(argv[1]) : 100;
const auto executor_string = argc >= 3 ? argv[2] : "reference";

problem:

auto correct_u = [](double x) { return x * x * x; };
auto f = [](double x) { return 6 * x; };
auto u0 = correct_u(0);
auto u1 = correct_u(1);

matrix

std::vector<int> row_ptrs(discretization_points + 1);
std::vector<int> col_idxs(3 * discretization_points - 2);
std::vector<double> values(3 * discretization_points - 2);

right hand side

std::vector<double> rhs(discretization_points);

solution

std::vector<double> u(discretization_points, 0.0);
generate_stencil_matrix(discretization_points, row_ptrs.data(),
col_idxs.data(), values.data());

looking for solution u = x^3: f = 6x, u(0) = 0, u(1) = 1

generate_rhs(discretization_points, f, u0, u1, rhs.data());
solve_system(executor_string, discretization_points, row_ptrs.data(),
col_idxs.data(), values.data(), rhs.data(), u.data(), 1e-12);
print_solution(discretization_points, 0, 1, u.data());
std::cout << "The average relative error is "
<< calculate_error(discretization_points, u.data(), correct_u) /
discretization_points
<< std::endl;
}

Results

This is the expected output:

0
0.00010798
0.000863838
0.00291545
0.0069107
0.0134975
0.0233236
0.037037
0.0552856
0.0787172
0.10798
0.143721
0.186589
0.237231
0.296296
0.364431
0.442285
0.530504
0.629738
0.740633
0.863838
1
The average relative error is 1.87318e-15

Comments about programming and debugging

The plain program

/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2020, the Ginkgo authors
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/
/*****************************<DESCRIPTION>***********************************
This example solves a 1D Poisson equation:
u : [0, 1] -> R
u'' = f
u(0) = u0
u(1) = u1
using a finite difference method on an equidistant grid with `K` discretization
points (`K` can be controlled with a command line parameter). The discretization
is done via the second order Taylor polynomial:
u(x + h) = u(x) - u'(x)h + 1/2 u''(x)h^2 + O(h^3)
u(x - h) = u(x) + u'(x)h + 1/2 u''(x)h^2 + O(h^3) / +
---------------------------------------------
-u(x - h) + 2u(x) + -u(x + h) = -f(x)h^2 + O(h^3)
For an equidistant grid with K "inner" discretization points x1, ..., xk, and
step size h = 1 / (K + 1), the formula produces a system of linear equations
2u_1 - u_2 = -f_1 h^2 + u0
-u_(k-1) + 2u_k - u_(k+1) = -f_k h^2, k = 2, ..., K - 1
-u_(K-1) + 2u_K = -f_K h^2 + u1
which is then solved using Ginkgo's implementation of the CG method
preconditioned with block-Jacobi. It is also possible to specify on which
executor Ginkgo will solve the system via the command line.
The function `f` is set to `f(x) = 6x` (making the solution `u(x) = x^3`), but
that can be changed in the `main` function.
The intention of the example is to show how Ginkgo can be integrated into
existing software - the `generate_stencil_matrix`, `generate_rhs`,
`print_solution`, `compute_error` and `main` function do not reference Ginkgo at
all (i.e. they could have been there before the application developer decided to
use Ginkgo, and the only part where Ginkgo is introduced is inside the
`solve_system` function.
*****************************<DESCRIPTION>**********************************/
#include <ginkgo/ginkgo.hpp>
#include <iostream>
#include <map>
#include <string>
#include <vector>
void generate_stencil_matrix(int discretization_points, int *row_ptrs,
int *col_idxs, double *values)
{
int pos = 0;
const double coefs[] = {-1, 2, -1};
row_ptrs[0] = pos;
for (int i = 0; i < discretization_points; ++i) {
for (auto ofs : {-1, 0, 1}) {
if (0 <= i + ofs && i + ofs < discretization_points) {
values[pos] = coefs[ofs + 1];
col_idxs[pos] = i + ofs;
++pos;
}
}
row_ptrs[i + 1] = pos;
}
}
template <typename Closure>
void generate_rhs(int discretization_points, Closure f, double u0, double u1,
double *rhs)
{
const auto h = 1.0 / (discretization_points + 1);
for (int i = 0; i < discretization_points; ++i) {
const auto xi = (i + 1) * h;
rhs[i] = -f(xi) * h * h;
}
rhs[0] += u0;
rhs[discretization_points - 1] += u1;
}
void print_solution(int discretization_points, double u0, double u1,
const double *u)
{
std::cout << u0 << '\n';
for (int i = 0; i < discretization_points; ++i) {
std::cout << u[i] << '\n';
}
std::cout << u1 << std::endl;
}
template <typename Closure>
double calculate_error(int discretization_points, const double *u,
Closure correct_u)
{
const auto h = 1.0 / (discretization_points + 1);
auto error = 0.0;
for (int i = 0; i < discretization_points; ++i) {
using std::abs;
const auto xi = (i + 1) * h;
error += abs(u[i] - correct_u(xi)) / abs(correct_u(xi));
}
return error;
}
void solve_system(const std::string &executor_string,
unsigned int discretization_points, int *row_ptrs,
int *col_idxs, double *values, double *rhs, double *u,
double accuracy)
{
using val_array = gko::Array<double>;
using idx_array = gko::Array<int>;
const auto &dp = discretization_points;
const auto omp = gko::OmpExecutor::create();
std::map<std::string, std::shared_ptr<gko::Executor>> exec_map{
{"omp", omp},
{"cuda", gko::CudaExecutor::create(0, omp)},
{"hip", gko::HipExecutor::create(0, omp)},
{"reference", gko::ReferenceExecutor::create()}};
const auto exec = exec_map.at(executor_string); // throws if not valid
const auto app_exec = exec_map["omp"];
auto matrix = mtx::create(exec, gko::dim<2>(dp),
val_array::view(app_exec, 3 * dp - 2, values),
idx_array::view(app_exec, 3 * dp - 2, col_idxs),
idx_array::view(app_exec, dp + 1, row_ptrs));
auto b = vec::create(exec, gko::dim<2>(dp, 1),
val_array::view(app_exec, dp, rhs), 1);
auto x = vec::create(app_exec, gko::dim<2>(dp, 1),
val_array::view(app_exec, dp, u), 1);
auto solver_gen =
cg::build()
.with_criteria(
gko::stop::Iteration::build().with_max_iters(dp).on(exec),
.with_reduction_factor(accuracy)
.on(exec))
.with_preconditioner(bj::build().on(exec))
.on(exec);
auto solver = solver_gen->generate(gko::give(matrix));
solver->apply(gko::lend(b), gko::lend(x));
}
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " DISCRETIZATION_POINTS [executor]"
<< std::endl;
std::exit(-1);
}
const int discretization_points = argc >= 2 ? std::atoi(argv[1]) : 100;
const auto executor_string = argc >= 3 ? argv[2] : "reference";
auto correct_u = [](double x) { return x * x * x; };
auto f = [](double x) { return 6 * x; };
auto u0 = correct_u(0);
auto u1 = correct_u(1);
std::vector<int> row_ptrs(discretization_points + 1);
std::vector<int> col_idxs(3 * discretization_points - 2);
std::vector<double> values(3 * discretization_points - 2);
std::vector<double> rhs(discretization_points);
std::vector<double> u(discretization_points, 0.0);
generate_stencil_matrix(discretization_points, row_ptrs.data(),
col_idxs.data(), values.data());
generate_rhs(discretization_points, f, u0, u1, rhs.data());
solve_system(executor_string, discretization_points, row_ptrs.data(),
col_idxs.data(), values.data(), rhs.data(), u.data(), 1e-12);
print_solution(discretization_points, 0, 1, u.data());
std::cout << "The average relative error is "
<< calculate_error(discretization_points, u.data(), correct_u) /
discretization_points
<< std::endl;
}
gko::abs
constexpr T abs(const T &x)
Returns the absolute value of the object.
Definition: math.hpp:572
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: coo.hpp:51
gko::HipExecutor::create
static std::shared_ptr< HipExecutor > create(int device_id, std::shared_ptr< Executor > master)
Creates a new HipExecutor.
gko::give
std::remove_reference< OwningPointer >::type && give(OwningPointer &&p)
Marks that the object pointed to by p can be given to the callee.
Definition: utils.hpp:231
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: coo.hpp:55
gko::CudaExecutor::create
static std::shared_ptr< CudaExecutor > create(int device_id, std::shared_ptr< Executor > master)
Creates a new CudaExecutor.
gko::OmpExecutor::create
static std::shared_ptr< OmpExecutor > create()
Creates a new OmpExecutor.
Definition: executor.hpp:775
gko::stop::ResidualNormReduction
The ResidualNormReduction class is a stopping criterion which stops the iteration process when the re...
Definition: residual_norm_reduction.hpp:64
gko::preconditioner::Jacobi
A block-Jacobi preconditioner is a block-diagonal linear operator, obtained by inverting the diagonal...
Definition: jacobi.hpp:207
gko::lend
std::enable_if< detail::have_ownership_s< Pointer >::value, detail::pointee< Pointer > * >::type lend(const Pointer &p)
Returns a non-owning (plain) pointer to the object pointed to by p.
Definition: utils.hpp:253
gko::dim< 2 >
gko::solver::Cg
CG or the conjugate gradient method is an iterative type Krylov subspace method which is suitable for...
Definition: cg.hpp:72
gko::Array
An Array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the Arr...
Definition: array.hpp:65