Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Compressed Basis GMRES (CB-GMRES) #693

Merged
merged 87 commits into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
cd1ffc9
First commit for the GmresMixed class:
Apr 27, 2020
d7ba04c
Inclusion of the omp executor in the repository:
Apr 27, 2020
969358f
Inclusion of the cuda executor in the repository:
Apr 29, 2020
2b7122f
The test files are finally included, but:
Apr 29, 2020
6dc6470
The first unoptimized version is done. Next tasks:
Apr 30, 2020
4d4fab1
The default value for ValueTypeKrylovBasis has been changed to avoid …
josealiaga Apr 30, 2020
e0d39a6
Definition of the CG2 variant of the finish_arnoldi routine for omp a…
josealiaga May 3, 2020
27019da
Add GMRES_mixed to benchmark
May 7, 2020
f2b5a3a
Definition of the CGS2 version of finish_arnoldi method, for omp and …
josealiaga May 7, 2020
bfd4196
Finally a good implementation of the multidot_kernels_num_iters_1 is …
josealiaga May 12, 2020
4ad491e
Add Accessor support and extend reference test
May 13, 2020
fbcc4f2
Made GmresMixed compile with complex types
May 13, 2020
911fd5c
The update routines have been improved. Now the computational time is…
josealiaga May 19, 2020
db081ac
Add specialization for integer types for Accessor
May 19, 2020
7db4796
Make the scale work with integer types
May 20, 2020
9bbcf87
Add helper to determine if we need a scale or not
May 20, 2020
97689ab
Add a helper structure to manage the scale writing in common
May 20, 2020
c0aa2ed
Testing the push command
josealiaga May 20, 2020
da8a56e
Definition of norm2 and norminf routines in CUDA. Only the first one …
josealiaga May 22, 2020
23efe7a
remove_complex has been added to the norms variables, and multinorm2_…
josealiaga May 24, 2020
f3cdbd7
Fixed cuda step2 to take a view into account
May 25, 2020
d4865f2
Change const accessor to non-const in check_arnoldi_norms_new
May 25, 2020
42a779b
The set_scale method finally works!!
josealiaga May 27, 2020
6b25d2e
Change storage layout of krylov_bases
Jun 1, 2020
e3ca7cc
Make memory access to krylov_bases coalesced again
Jun 2, 2020
911fccb
Transpose grid when launching singledot kernel
Jun 4, 2020
5dd8bf2
Reversed the transpose of the grid dim
Jun 4, 2020
b214a5f
Add half precision support to GmresMixed
Jun 15, 2020
f1f178e
Hopefully improve singledot performance
Jun 15, 2020
fb64f5c
Infinity norm only computed when scale is present
Jun 17, 2020
155c552
Add another RHS generation in the benchmark
Jun 23, 2020
d660c47
Fix residual_norm calculation in GmresMixed
Jun 23, 2020
8ede6d6
Make sure GmresMixed does not exit early
Jun 25, 2020
af6fb48
Add benchmark parameter for GMRES krylov_dim
Jun 26, 2020
49f1763
Add forced iterations when convergence is detected
Jun 26, 2020
188ff18
Add debug output to forced iterations
Jun 26, 2020
297004b
Fix reference bug in GmresMixed
Jul 8, 2020
b7765c3
DEBUG: Add write output for integral accessor
Jul 30, 2020
f31e87d
DEBUG: Move towards `at` with accessor
Aug 14, 2020
6f82f47
Remove Accessor3dConst
Aug 17, 2020
c385a2b
Adopt OpenMP support to new Accessor
Aug 17, 2020
9a0a0f0
Remove unused GMRES_mixed code from Ref & OMP
Aug 17, 2020
3d44a21
Adopt CUDA to the new accessor format (NOT `at`)
Aug 17, 2020
e1654b4
Make HIP and CUDA work with new accessor (NOT at)
Aug 17, 2020
5d09f2a
Remove unused code from CUDA
Aug 17, 2020
48e0899
CUDA implementation is now using `at`
Aug 18, 2020
b542646
Re-add ConstAccessor
Aug 18, 2020
b0a2ac3
Fix accessor by adding additional __restrict__
Aug 19, 2020
5d1f173
GmresMixed storage prec is now a factory parameter
Aug 25, 2020
267bbf1
Improve reference test and include the enum there
Aug 25, 2020
6b92a4f
Fix the reference test to pass
Aug 26, 2020
1d4a773
Adopt to new parameter macros
Sep 1, 2020
30381e2
Update the helper to throw when complex
Sep 7, 2020
6d30e36
Make GmresMixed work properly with multiple RHS
Sep 9, 2020
7f86d9c
Fix benchmark to work with new GmresMixed layout
Sep 10, 2020
b2b9ebc
Use new reduced_row_major Accessor in GmresMixed
Sep 11, 2020
db3046f
Remove unnecessary code from CUDA GmresMixed
Sep 14, 2020
da87d06
Add HIP kernels
Sep 14, 2020
65a8a8b
Fix GmresMixed core problem
Sep 17, 2020
a9646e8
Improve force-reset behavior
Dec 12, 2020
c6020db
Rename GmresMixed to CbGmres
Jan 22, 2021
36a1a19
Format files
ginkgo-bot Jan 23, 2021
c4a7335
Add DPCPP stubs to allow compilation
Jan 24, 2021
fa1fc39
Make cb-gmres benchmarks dependent on etype
Jan 28, 2021
c509263
Fix implementation and reference test for CB-GMRES
Feb 1, 2021
6bb09da
Update tolerance for one reference CB-GMRES test
Feb 1, 2021
3655021
Update atomic_max
Feb 2, 2021
5270478
Remove unnecessary kernels and properly name them
Feb 2, 2021
2f0a32e
Review update
Feb 2, 2021
58e1104
Add Helper INSTANTIATE macro for CB-GMRES
Feb 3, 2021
c47a4ca
Remove CB-GMRES and GMRES example
Feb 3, 2021
2b587ba
Review update
Feb 3, 2021
da8a6b4
Remove unnecessary includes of iostream and time.h
Feb 3, 2021
c4c1270
Remove circular dependency of compute_norm2 in (CB)-GMRES
Feb 3, 2021
00d33b5
Update solver generation in benchmark
Feb 3, 2021
b163893
Update eta and arnoldi_norms in CB-GMRES
Feb 4, 2021
342957a
Remove CUDA 9.0 exception for constexpr parameter
Feb 4, 2021
370d208
Review Update
Feb 5, 2021
1b2071d
Sonarcloud update
Feb 6, 2021
b4a6fc9
Review update; Improve run_all_benchmarks.sh
Feb 8, 2021
599e261
Put storage_precision enum into cb_gmres namespace
Feb 9, 2021
5126858
Add CB-GMRES example
Feb 9, 2021
169040d
Remove unnecessary included files for CB-GMRES
Feb 9, 2021
05374fd
Review update
Feb 15, 2021
389d038
Review update
Feb 16, 2021
6d6bbab
Update contributors.txt
josealiaga Feb 19, 2021
4d722f1
Update contributors.txt
josealiaga Feb 19, 2021
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
Add CB-GMRES example
  • Loading branch information
Thomas Grützmacher committed Feb 19, 2021
commit 5126858cf7f1c524e303c702f16f0b8ed194a980
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ option(GINKGO_RUN_EXAMPLES " Compile run and validation targets for the examples

set(EXAMPLES_EXEC_LIST
adaptiveprecision-blockjacobi
cb-gmres
custom-logger
ginkgo-ranges
ilu-preconditioned-solver
Expand Down
5 changes: 5 additions & 0 deletions examples/cb-gmres/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(target_name "cb-gmres")
add_executable(${target_name} ${target_name}.cpp)
target_link_libraries(${target_name} Ginkgo::ginkgo)
target_include_directories(${target_name} PRIVATE ${PROJECT_SOURCE_DIR})
configure_file("${Ginkgo_SOURCE_DIR}/matrices/test/ani1.mtx" data/A.mtx COPYONLY)
16 changes: 16 additions & 0 deletions examples/cb-gmres/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# set up script
if [ $# -ne 1 ]; then
echo -e "Usage: $0 GINKGO_BUILD_DIRECTORY"
exit 1
fi
BUILD_DIR=$1
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null && pwd )

source ${THIS_DIR}/../build-setup.sh

# build
${CXX} -std=c++14 -o ${THIS_DIR}/cb-gmres ${THIS_DIR}/cb-gmres.cpp \
-I${THIS_DIR}/../../include -I${BUILD_DIR}/include \
-L${THIS_DIR} ${LINK_FLAGS}
220 changes: 220 additions & 0 deletions examples/cb-gmres/cb-gmres.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, 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>*******************************/

// This is the main ginkgo header file.
#include <ginkgo/ginkgo.hpp>

#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <string>


// Helper function which measures the time of `solver->apply(b, x)` in seconds
// To get an accurate result, the solve is repeated multiple times (while
// ensuring the initial guess is always the same). The result of the solve will
// be written to x.
double measure_solve_time_in_s(const gko::Executor *exec, gko::LinOp *solver,
const gko::LinOp *b, gko::LinOp *x)
{
constexpr int repeats{5};
double duration{0};
// Make a copy of x, so we can re-use the same initial guess multiple times
auto x_copy = clone(x);
for (int i = 0; i < repeats; ++i) {
// No need to copy it in the first iteration
if (i != 0) {
x_copy->copy_from(x);
}
// Make sure all previous executor operations have finished before
// starting the time
exec->synchronize();
auto tic = std::chrono::steady_clock::now();
solver->apply(b, lend(x_copy));
// Make sure all computations are done before stopping the time
exec->synchronize();
auto tac = std::chrono::steady_clock::now();
duration += std::chrono::duration<double>(tac - tic).count();
}
// Copy the solution back to x, so the caller has the result
x->copy_from(lend(x_copy));
return duration / static_cast<double>(repeats);
}


int main(int argc, char *argv[])
{
// Use some shortcuts. In Ginkgo, vectors are seen as a gko::matrix::Dense
// with one column/one row. The advantage of this concept is that using
// 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.
using mtx = gko::matrix::Csr<ValueType, IndexType>;
// The gko::solver::CbGmres is used here, but any other solver class can
// also be used.
using cb_gmres = gko::solver::CbGmres<ValueType>;

// Print the ginkgo version information.
std::cout << gko::version_info::get() << std::endl;

if (argc == 2 && (std::string(argv[1]) == "--help")) {
std::cerr << "Usage: " << argv[0] << " [executor] " << std::endl;
std::exit(-1);
}

// Map which generates the appropriate executor
const auto executor_string = argc >= 2 ? argv[1] : "reference";
std::map<std::string, std::function<std::shared_ptr<gko::Executor>()>>
exec_map{
{"omp", [] { return gko::OmpExecutor::create(); }},
{"cuda",
[] {
return gko::CudaExecutor::create(0, gko::OmpExecutor::create(),
true);
}},
{"hip",
[] {
return gko::HipExecutor::create(0, gko::OmpExecutor::create(),
true);
}},
{"dpcpp",
[] {
return gko::DpcppExecutor::create(0,
gko::OmpExecutor::create());
}},
{"reference", [] { return gko::ReferenceExecutor::create(); }}};

// executor where Ginkgo will perform the computation
const auto exec = exec_map.at(executor_string)(); // throws if not valid

// Note: this matrix is copied from "SOURCE_DIR/matrices" instead of from
// the local directory. For details, see
// "examples/cb-gmres/CMakeLists.txt"
auto A = share(gko::read<mtx>(std::ifstream("data/A.mtx"), exec));
// Create a uniform right-hand side with a norm2 of 1 on the host
// (norm2(b) == 1), followed by copying it to the actual executor
// (to make sure it also works for GPUs)
const auto A_size = A->get_size();
auto b_host = vec::create(exec->get_master(), gko::dim<2>{A_size[0], 1});
for (gko::size_type i = 0; i < A_size[0]; ++i) {
b_host->at(i, 0) =
ValueType{1} / std::sqrt(static_cast<ValueType>(A_size[0]));
}
auto b_norm = gko::initialize<real_vec>({0.0}, exec);
b_host->compute_norm2(lend(b_norm));
auto b = clone(exec, lend(b_host));

// As an initial guess, use the right-hand side
auto x_keep = clone(lend(b));
auto x_reduce = clone(x_keep);

const RealValueType reduction_factor{1e-6};

// Generate two solver factories: `_keep` uses the same precision for the
// krylov basis as the matrix, and `_reduce` uses one precision below it.
// If `ValueType` is double, then `_reduce` uses float as the krylov basis
// storage type
auto solver_gen_keep =
cb_gmres::build()
.with_criteria(
gko::stop::Iteration::build().with_max_iters(1000u).on(exec),
gko::stop::RelativeResidualNorm<ValueType>::build()
.with_tolerance(reduction_factor)
.on(exec))
.with_krylov_dim(100u)
.with_storage_precision(
gko::solver::cb_gmres::storage_precision::keep)
.on(exec);

auto solver_gen_reduce =
cb_gmres::build()
.with_criteria(
gko::stop::Iteration::build().with_max_iters(1000u).on(exec),
gko::stop::RelativeResidualNorm<ValueType>::build()
.with_tolerance(reduction_factor)
.on(exec))
.with_krylov_dim(100u)
.with_storage_precision(
gko::solver::cb_gmres::storage_precision::reduce1)
.on(exec);
// Generate the actual solver from the factory and the matrix.
auto solver_keep = solver_gen_keep->generate(A);
auto solver_reduce = solver_gen_reduce->generate(A);

// Solve both system and measure the time for each.
auto time_keep = measure_solve_time_in_s(lend(exec), lend(solver_keep),
lend(b), lend(x_keep));
auto time_reduce = measure_solve_time_in_s(lend(exec), lend(solver_reduce),
lend(b), lend(x_reduce));

// Make sure the output is in scientific notation for easier comparison
std::cout << std::scientific;
// Note: The time might not be significantly different since the matrix is
// quite small
std::cout << "Solve time without compression: " << time_keep << " s\n"
<< "Solve time with compression: " << time_reduce << " s\n";

// To measure if your solution has actually converged, the error of the
// solution is measured.
// one, neg_one are objects that represent the numbers which allow for a
// uniform interface when computing on any device. To compute the residual,
// the (advanced) apply method is used.
auto one = gko::initialize<vec>({1.0}, exec);
auto neg_one = gko::initialize<vec>({-1.0}, exec);

auto res_norm_keep = gko::initialize<real_vec>({0.0}, exec);
auto res_norm_reduce = gko::initialize<real_vec>({0.0}, exec);
auto tmp = gko::clone(gko::lend(b));

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

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

tmp->copy_from(lend(b));
A->apply(lend(one), lend(x_reduce), lend(neg_one), lend(tmp));
tmp->compute_norm2(lend(res_norm_reduce));

std::cout << "\nResidual norm with compression:\n";
write(std::cout, lend(res_norm_reduce));
}
1 change: 1 addition & 0 deletions examples/cb-gmres/doc/builds-on
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

15 changes: 15 additions & 0 deletions examples/cb-gmres/doc/intro.dox
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<a name="Intro"></a>
<h1>Introduction</h1>

<h3> About the example </h3>
This example showcases the usage of the Ginkgo solver CB-GMRES (Compressed
Basis GMRES). A small system is solved with two un-preconditioned CB-GMRES
solvers:
1. without compressing the krylov basis; it uses double precision for
both the matrix and the krylov basis, and
2. with a compression of the krylov basis; it uses double precision for the
matrix and all arithmetic operations, while using single precision for the
krylov basis
thoasm marked this conversation as resolved.
Show resolved Hide resolved

Both solves are timed and the residual norm of each solution is computed to
show that both solutions are correct.
1 change: 1 addition & 0 deletions examples/cb-gmres/doc/kind
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
techniques
21 changes: 21 additions & 0 deletions examples/cb-gmres/doc/results.dox
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h1>Results</h1>
The following is the expected result:

@code{.cpp}

Solve time without compression: 1.842690e-04 s
Solve time with compression: 1.589936e-04 s

Residual norm without compression:
%%MatrixMarket matrix array real general
1 1
2.430544e-07

Residual norm with compression:
%%MatrixMarket matrix array real general
1 1
3.437257e-07

@endcode

<h3> Comments about programming and debugging </h3>
1 change: 1 addition & 0 deletions examples/cb-gmres/doc/short-intro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The CB-GMRES solver example.
1 change: 1 addition & 0 deletions examples/cb-gmres/doc/tooltip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Solve a linear system with CB-GMRES, both with and without compression. Benchmark the solve time and validate the result.