Skip to content

Conversation

yhmtsai
Copy link
Member

@yhmtsai yhmtsai commented Mar 19, 2025

This PR gives different way than adding a new type (#1029), which allows ValueType can be used in template LSolverType of preconditioner::Ic (similar to Ilu later).
Both PRs aims to reduce the challenges of instantiating many types for preconditioner::Ic and preconditioner::Ilu because we need to instantiate them first such that users can select one of them.

when the SolverType is LinOp,

  • no default solver and factorization factory in general

TODO:

@yhmtsai yhmtsai added 1:ST:WIP This PR is a work in progress. Not ready for review. 1:ST:need-feedback The PR is somewhat ready but feedback on a blocking topic is required before a proper review. labels Mar 19, 2025
@yhmtsai yhmtsai requested a review from a team March 19, 2025 13:28
@yhmtsai yhmtsai self-assigned this Mar 19, 2025
@ginkgo-bot ginkgo-bot added reg:testing This is related to testing. mod:core This is related to the core module. mod:reference This is related to the reference module. type:preconditioner This is related to the preconditioners labels Mar 19, 2025
@yhmtsai yhmtsai force-pushed the ic_ilu_with_linop branch from a371088 to 62e7195 Compare March 19, 2025 15:22
@yhmtsai yhmtsai added this to the Ginkgo 1.10.0 milestone Mar 26, 2025
Copy link
Member

@upsj upsj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for how we could make the changes less intrusive, and more consistent with other LinOpFactory types

@yhmtsai yhmtsai force-pushed the ic_ilu_with_linop branch from 06f44f1 to 9138755 Compare April 8, 2025 15:34
@MarcelKoch MarcelKoch self-requested a review April 9, 2025 12:43
@yhmtsai yhmtsai requested a review from upsj April 9, 2025 17:10
upsj
upsj previously requested changes Apr 11, 2025
Copy link
Member

@upsj upsj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for the most part, a handful of questions and suggestions, and one concern about mixed precision. Maybe change the title to remove ILU?

if (!comp) {
GKO_NOT_SUPPORTED(comp);
if constexpr (std::is_same_v<value_type, void>) {
GKO_NOT_IMPLEMENTED;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GKO_NOT_SUPPORTED maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GKO_NOT_SUPPORTED require a object, so I currently put the GKO_NOT_IMPLEMENTED

lh_solver_ = as<lh_solver_type>(l_solver_->conj_transpose());
if constexpr (std::is_same_v<l_solver_type, LinOp>) {
// linop can not have a default factory generation
GKO_NOT_IMPLEMENTED;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GKO_NOT_SUPPORTED

Comment on lines 417 to 345
this->set_cache_to(b);
if (l_solver_->apply_uses_initial_guess()) {
cache_.intermediate->copy_from(b);
}
l_solver_->apply(b, cache_.intermediate);
if (lh_solver_->apply_uses_initial_guess()) {
x->copy_from(cache_.intermediate);
}
lh_solver_->apply(cache_.intermediate, x);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the mixed-precision behavior slightly, meaning we require more allocations, and may lose some precision. Is that intended?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I miss to delete the copy_from in the set_cache_to.
I think the current one will be less than the original one.
assume the LSolver have the same precision as IC, so cache vector is not needed to copy for precision

original one does not support mixed precision.
original: (no matter the underlying support mixed or not)
input (copy in), output (copy in)
Lsolver (always copy in set_cache_to without considering initial_guess), LHsolver no precision change
output (copy out)

now:
LSolver (copy input (in))
LHSolver (copy output (inout))
if the underlying solver support mixed precision, it will reduce the copy.

When we consider more different settings: the LSolver and LHSolver do not use the same precision
original will need to pay Lsolver(input, cache) LHSolver(cache, output) additionally because it dispatches the precision first.
but the current one: only need to pay cache conversion in LSolver and LHsolver additionally, which is less than the original one.

This is one of reason for additional stuff may need for mixed-precision (internally not just outside)

Copy link
Member Author

@yhmtsai yhmtsai Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, do you only mean the x->copy_from(cache)?
If so, you are right, it should use the user input directly.

This part is the original behavior

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more than what I thought because of real apply on the complex vector. I will move this out of the current pr. the apply and set_cache_to should be the same function as the original one

Copy link
Member

@MarcelKoch MarcelKoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to accepting ValueType as first template parameter seems not to be done fully.

@yhmtsai yhmtsai changed the title Allow to use LinOp as SolverType in template of preconditioner IC and ILU Allow to use LinOp as SolverType in template of preconditioner IC Apr 14, 2025
@yhmtsai yhmtsai force-pushed the ic_ilu_with_linop branch from 9138755 to 69a30a4 Compare April 15, 2025 11:47
@yhmtsai yhmtsai requested review from MarcelKoch and upsj April 15, 2025 11:47
using factory_type = typename factory_type_impl<Type>::type;

template <typename Type>
constexpr bool is_ginkgo_linop = std::is_convertible_v<Type*, LinOp*>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO std::is_base_of would be a better check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought is_convertible_v more appropriate check?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least not to me. Here we want to know if LinOp is a base class of Type, which the first suggestion does very explicitly. For the other one you have to remember that this is equivalent to LinOp being a base class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point from readability.
If we do not face the some situation (like private or ambiguous base) mentioned by std, we can go for that

is_instantiation_of<Type, solver::Ir>::value ||
is_instantiation_of<Type, solver::Gmres>::value ||
is_instantiation_of<Type, preconditioner::LowerIsai>::value;
std::is_same_v<gko::detail::get_solver_type<SolverTypeOrValueType>, LinOp>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more direct:

Suggested change
std::is_same_v<gko::detail::get_solver_type<SolverTypeOrValueType>, LinOp>;
!detail::is_ginkgo_linop<SolverTypeOrValueType>;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change it a bit to only require the class not the first template, which will simplify the process for Ilu

@yhmtsai yhmtsai changed the title Allow to use LinOp as SolverType in template of preconditioner IC Allow to use ValueType in template of preconditioner IC Apr 15, 2025
@yhmtsai yhmtsai requested a review from MarcelKoch April 16, 2025 07:06
@yhmtsai yhmtsai force-pushed the ic_ilu_with_linop branch from f70c7b6 to 71e45e6 Compare April 16, 2025 07:08
Copy link
Member

@MarcelKoch MarcelKoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only two minor comments left.

@yhmtsai yhmtsai added 1:ST:ready-for-review This PR is ready for review and removed 1:ST:need-feedback The PR is somewhat ready but feedback on a blocking topic is required before a proper review. labels Apr 17, 2025
@yhmtsai yhmtsai requested a review from MarcelKoch April 17, 2025 09:14
@yhmtsai yhmtsai force-pushed the ic_ilu_with_linop branch from 2b7c086 to 3bc88de Compare April 24, 2025 15:56
@yhmtsai yhmtsai force-pushed the ic_ilu_with_linop branch from 3bc88de to e09c3a9 Compare April 24, 2025 16:20
@yhmtsai yhmtsai removed the 1:ST:WIP This PR is a work in progress. Not ready for review. label Apr 28, 2025
@yhmtsai yhmtsai dismissed stale reviews from upsj and MarcelKoch April 28, 2025 11:16

stale

@yhmtsai yhmtsai added 1:ST:ready-to-merge This PR is ready to merge. and removed 1:ST:ready-for-review This PR is ready for review labels May 5, 2025
yhmtsai and others added 6 commits May 7, 2025 21:33
if LSolverType is LinOp, value_type will be void and Ic will not give the default factory for l_solver and factorization
… as template

Co-authored-by: Marcel Koch <marcel.koch@kit.edu>
Co-authored-by: Tobias Ribizel <mail@ribizel.de>
… revert the apply due to real apply on complex vector

Co-authored-by: Marcel Koch <marcel.koch@kit.edu>
Co-authored-by: Marcel Koch <marcel.koch@kit.edu>
@yhmtsai yhmtsai force-pushed the ic_ilu_with_linop branch from e09c3a9 to 802d992 Compare May 7, 2025 19:33
@yhmtsai yhmtsai merged commit 6922832 into develop May 8, 2025
14 of 15 checks passed
@yhmtsai yhmtsai deleted the ic_ilu_with_linop branch May 8, 2025 08:01
Copy link

sonarqubecloud bot commented May 8, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1:ST:ready-to-merge This PR is ready to merge. mod:core This is related to the core module. mod:reference This is related to the reference module. reg:testing This is related to testing. type:preconditioner This is related to the preconditioners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants