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

GCR solver rewrite #1239

Merged
merged 33 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cb55490
Merge kho19/ginkgo 'gcr_devel' to origin 'develop'
yanjen Nov 11, 2022
8bc6de2
Change GCR parameters to workspace_traits
yanjen Nov 15, 2022
c0ada87
Update parameter names and test functions
yanjen Nov 18, 2022
d7ecd72
Fix bugs for common kernel
yanjen Nov 25, 2022
2a66410
Fix bugs for common kernel
yanjen Dec 2, 2022
681cc64
Update reference test for GCR
yanjen Dec 2, 2022
4aeb6be
Change value type for normal vector
yanjen Dec 6, 2022
b353586
Use SPD matrix in test to reduce GCR roundoff error
yanjen Dec 12, 2022
ac827a1
Add compute squared norm2 functions
yanjen Dec 12, 2022
8778550
Add comments
yanjen Dec 12, 2022
be170fe
Improve code style
yanjen Dec 12, 2022
4ae7f5c
Add tests
yanjen Dec 12, 2022
0b78698
Remove unused function
yanjen Dec 16, 2022
1a6fe50
Format files
ginkgo-bot Dec 16, 2022
62c3eca
Add seed for random engine
yanjen Dec 19, 2022
4dd56ae
Revert wrong fix
yanjen Dec 19, 2022
4b55a74
Revise based on Mike's review
yanjen Dec 29, 2022
01eb157
Fix repeated allocation bug.
yanjen Jan 4, 2023
2e149b1
Fix bug
yanjen Jan 5, 2023
229c573
Format files
ginkgo-bot Jan 10, 2023
cfee2d6
Remove test with deprecated variable.
yanjen Feb 7, 2023
af78bbb
Minor fixes of reviews
yanjen Feb 13, 2023
4ad7ee4
Revise memory movement
yanjen Feb 20, 2023
220982c
Add comment
yanjen Feb 20, 2023
a386e23
Revise variable order
yanjen Feb 20, 2023
37b4b99
Update compute_squared_norm2
yanjen Feb 20, 2023
0f7b049
Format files
ginkgo-bot Mar 7, 2023
71026fa
Rename variables
yanjen Mar 21, 2023
79db591
Improve comment
yanjen Mar 21, 2023
8acfb7a
Add default stride
yanjen Mar 23, 2023
bc49cdd
Update memory movement calculation
yanjen Mar 23, 2023
28e3176
Add const to restart A_residual
yanjen Mar 23, 2023
f15a156
Improve comment
yanjen Mar 23, 2023
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
5 changes: 4 additions & 1 deletion benchmark/solver/solver_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ DEFINE_uint32(
nrhs, 1,
"The number of right hand sides. Record the residual only when nrhs == 1.");

DEFINE_uint32(gcr_restart, 100,
"Maximum dimension of the Krylov space to use in GCR");

DEFINE_uint32(gmres_restart, 100,
"What maximum dimension of the Krylov space to use in GMRES");
"Maximum dimension of the Krylov space to use in GMRES");

DEFINE_uint32(idr_subspace_dim, 2,
"What dimension of the subspace to use in IDR");
Expand Down
1 change: 1 addition & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(UNIFIED_SOURCES
solver/cgs_kernels.cpp
solver/common_gmres_kernels.cpp
solver/fcg_kernels.cpp
solver/gcr_kernels.cpp
solver/gmres_kernels.cpp
solver/ir_kernels.cpp
)
Expand Down
126 changes: 126 additions & 0 deletions common/unified/solver/gcr_kernels.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2023, 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>*******************************/

#include "core/solver/gcr_kernels.hpp"


#include <ginkgo/core/base/math.hpp>


#include "common/unified/base/kernel_launch_solver.hpp"


namespace gko {
namespace kernels {
namespace GKO_DEVICE_NAMESPACE {
/**
* @brief The GCR solver namespace.
*
* @ingroup grc
*/
namespace gcr {


template <typename ValueType>
yanjen marked this conversation as resolved.
Show resolved Hide resolved
void initialize(std::shared_ptr<const DefaultExecutor> exec,
const matrix::Dense<ValueType>* b,
matrix::Dense<ValueType>* residual,
stopping_status* stop_status)
{
run_kernel_solver(
exec,
[] GKO_KERNEL(auto row, auto col, auto b, auto residual, auto stop) {
if (row == 0) {
stop[col].reset();
}
residual(row, col) = b(row, col);
},
b->get_size(), b->get_stride(), default_stride(b),
default_stride(residual), stop_status);
}

GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_GCR_INITIALIZE_KERNEL);


template <typename ValueType>
void restart(std::shared_ptr<const DefaultExecutor> exec,
const matrix::Dense<ValueType>* residual,
const matrix::Dense<ValueType>* A_residual,
matrix::Dense<ValueType>* p_bases,
matrix::Dense<ValueType>* Ap_bases, size_type* final_iter_nums)
{
run_kernel_solver(
exec,
[] GKO_KERNEL(auto row, auto col, auto residual, auto A_residual,
auto p_bases, auto Ap_bases, auto final_iter_nums) {
if (row == 0) {
final_iter_nums[col] = 0;
}
p_bases(row, col) = residual(row, col);
Ap_bases(row, col) = A_residual(row, col);
},
residual->get_size(), residual->get_stride(), default_stride(residual),
default_stride(A_residual), p_bases, Ap_bases, final_iter_nums);
}

GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_GCR_RESTART_KERNEL);


template <typename ValueType>
void step_1(std::shared_ptr<const DefaultExecutor> exec,
matrix::Dense<ValueType>* x, matrix::Dense<ValueType>* residual,
const matrix::Dense<ValueType>* p,
const matrix::Dense<ValueType>* Ap,
const matrix::Dense<remove_complex<ValueType>>* Ap_norm,
const matrix::Dense<ValueType>* rAp,
const stopping_status* stop_status)
{
run_kernel_solver(
exec,
[] GKO_KERNEL(auto row, auto col, auto x, auto residual, auto p,
auto Ap, auto Ap_norm, auto rAp, auto stop) {
if (!stop[col].has_stopped()) {
auto tmp = rAp[col] / Ap_norm[col];
x(row, col) += tmp * p(row, col);
residual(row, col) -= tmp * Ap(row, col);
}
},
x->get_size(), p->get_stride(), x, residual, p, Ap, Ap_norm, rAp,
stop_status);
}

GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_GCR_STEP_1_KERNEL);

} // namespace gcr
} // namespace GKO_DEVICE_NAMESPACE
} // namespace kernels
} // namespace gko
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ target_sources(ginkgo
solver/cgs.cpp
solver/direct.cpp
solver/fcg.cpp
solver/gcr.cpp
solver/gmres.cpp
solver/idr.cpp
solver/ir.cpp
Expand Down
10 changes: 10 additions & 0 deletions core/device_hooks/common_kernels.inc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "core/solver/cgs_kernels.hpp"
#include "core/solver/common_gmres_kernels.hpp"
#include "core/solver/fcg_kernels.hpp"
#include "core/solver/gcr_kernels.hpp"
#include "core/solver/gmres_kernels.hpp"
#include "core/solver/idr_kernels.hpp"
#include "core/solver/ir_kernels.hpp"
Expand Down Expand Up @@ -437,6 +438,15 @@ GKO_STUB_VALUE_TYPE(GKO_DECLARE_CGS_STEP_3_KERNEL);

} // namespace cgs

namespace gcr {


GKO_STUB_VALUE_TYPE(GKO_DECLARE_GCR_INITIALIZE_KERNEL);
GKO_STUB_VALUE_TYPE(GKO_DECLARE_GCR_RESTART_KERNEL);
GKO_STUB_VALUE_TYPE(GKO_DECLARE_GCR_STEP_1_KERNEL);


} // namespace gcr

namespace common_gmres {

Expand Down
36 changes: 36 additions & 0 deletions core/matrix/dense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ void Dense<ValueType>::compute_norm1(ptr_param<LinOp> result) const
}


template <typename ValueType>
void Dense<ValueType>::compute_squared_norm2(LinOp* result) const
{
auto exec = this->get_executor();
this->compute_squared_norm2_impl(
make_temporary_output_clone(exec, result).get());
}


template <typename ValueType>
void Dense<ValueType>::inv_scale_impl(const LinOp* alpha)
{
Expand Down Expand Up @@ -470,6 +479,33 @@ void Dense<ValueType>::compute_norm1_impl(LinOp* result) const
}


template <typename ValueType>
void Dense<ValueType>::compute_squared_norm2(LinOp* result,
array<char>& tmp) const
{
GKO_ASSERT_EQUAL_DIMENSIONS(result, dim<2>(1, this->get_size()[1]));
auto exec = this->get_executor();
if (tmp.get_executor() != exec) {
tmp.clear();
tmp.set_executor(exec);
}
auto local_result = make_temporary_clone(exec, result);
auto dense_res = make_temporary_conversion<remove_complex<ValueType>>(
local_result.get());
exec->run(dense::make_compute_squared_norm2(this, dense_res.get(), tmp));
}


template <typename ValueType>
void Dense<ValueType>::compute_squared_norm2_impl(LinOp* result) const
{
auto exec = this->get_executor();
array<char> tmp{exec};
this->compute_squared_norm2(make_temporary_output_clone(exec, result).get(),
tmp);
}


template <typename ValueType>
Dense<ValueType>& Dense<ValueType>::operator=(const Dense& other)
{
Expand Down
Loading