Skip to content

Commit 3ec23a4

Browse files
committed
make preconditionner a linop
1 parent 84f4bc9 commit 3ec23a4

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

example/linalg/example_solve_custom.f90

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ subroutine solve_pccg_custom(A,b,x,di,tol,maxiter,restart,workspace)
1515
type(solver_workspace_dp), optional, intent(inout), target :: workspace
1616
!-------------------------
1717
type(linop_dp) :: op
18+
type(linop_dp) :: M
1819
type(solver_workspace_dp), pointer :: workspace_
1920
integer :: n, maxiter_
2021
real(dp) :: tol_
@@ -31,7 +32,7 @@ subroutine solve_pccg_custom(A,b,x,di,tol,maxiter,restart,workspace)
3132
! internal memory setup
3233
op%matvec => my_matvec
3334
op%inner_product => my_dot
34-
op%preconditionner => jacobi_preconditionner
35+
M%matvec => jacobi_preconditionner
3536
if(present(di))then
3637
di_ => di
3738
else
@@ -50,7 +51,7 @@ subroutine solve_pccg_custom(A,b,x,di,tol,maxiter,restart,workspace)
5051
where(abs(diagonal)>epsilon(0.d0)) diagonal = 1._dp/diagonal
5152
!-------------------------
5253
! main call to the solver
53-
call solve_pccg_generic(op,b,x,di_,tol_,maxiter_,restart_,workspace_)
54+
call solve_pccg_generic(op,M,b,x,di_,tol_,maxiter_,restart_,workspace_)
5455

5556
!-------------------------
5657
! internal memory cleanup

src/stdlib_linalg_iterative_solvers.fypp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module stdlib_linalg_iterative_solvers
1919
#:for k, t, s in R_KINDS_TYPES
2020
type, public :: linop_${s}$
2121
procedure(vector_sub_${s}$), nopass, pointer :: matvec => null()
22-
procedure(vector_sub_${s}$), nopass, pointer :: preconditionner => null()
2322
procedure(reduction_sub_${s}$), nopass, pointer :: inner_product => default_dot_${s}$
2423
end type
2524
#:endfor
@@ -86,8 +85,9 @@ module stdlib_linalg_iterative_solvers
8685

8786
interface solve_pccg_generic
8887
#:for k, t, s in R_KINDS_TYPES
89-
module subroutine solve_pccg_generic_${s}$(A,b,x,di,tol,maxiter,restart,workspace)
88+
module subroutine solve_pccg_generic_${s}$(A,M,b,x,di,tol,maxiter,restart,workspace)
9089
class(linop_${s}$), intent(in) :: A
90+
class(linop_${s}$), intent(in) :: M !> preconditionner
9191
${t}$, intent(in) :: b(:)
9292
${t}$, intent(inout) :: x(:)
9393
${t}$, intent(in) :: tol
@@ -103,7 +103,7 @@ module stdlib_linalg_iterative_solvers
103103
interface solve_pccg
104104
#:for matrix in MATRIX_TYPES
105105
#:for k, t, s in R_KINDS_TYPES
106-
module subroutine solve_pccg_${matrix}$_${s}$(A,b,x,di,tol,maxiter,restart,workspace)
106+
module subroutine solve_pccg_${matrix}$_${s}$(A,b,x,di,tol,maxiter,restart,M,workspace)
107107
#:if matrix == "dense"
108108
${t}$, intent(in) :: A(:,:)
109109
#:else
@@ -115,6 +115,7 @@ module stdlib_linalg_iterative_solvers
115115
logical(1), intent(in), optional, target :: di(:)
116116
integer, intent(in), optional :: maxiter
117117
logical, intent(in), optional :: restart
118+
class(linop_${s}$), optional , intent(in), target :: M !> preconditionner
118119
type(solver_workspace_${s}$), optional, intent(inout), target :: workspace
119120
end subroutine
120121
#:endfor

src/stdlib_linalg_iterative_solvers_pccg.fypp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ submodule(stdlib_linalg_iterative_solvers) stdlib_linalg_iterative_pccg
1414
contains
1515

1616
#:for k, t, s in R_KINDS_TYPES
17-
module subroutine solve_pccg_generic_${s}$(A,b,x,di,tol,maxiter,restart,workspace)
17+
module subroutine solve_pccg_generic_${s}$(A,M,b,x,di,tol,maxiter,restart,workspace)
1818
class(linop_${s}$), intent(in) :: A
19+
class(linop_${s}$), intent(in) :: M !> preconditionner
1920
${t}$, intent(in) :: b(:), tol
2021
${t}$, intent(inout) :: x(:)
2122
logical(1), intent(in) :: di(:)
@@ -41,7 +42,7 @@ contains
4142
call A%matvec(X, R)
4243
R = merge( zero_${s}$, B - R , di ) !> R = B - A*X
4344

44-
call A%preconditionner(R,P) !> P = M^{-1}*R
45+
call M%matvec(R,P) !> P = M^{-1}*R
4546
P = merge( zero_${s}$, P, di )
4647

4748
tolsq = tol*tol
@@ -50,7 +51,7 @@ contains
5051
zr2 = one_${s}$
5152
do while ( (iter < maxiter) .AND. (norm_sq > tolsq * norm_sq0) )
5253

53-
call A%preconditionner(R,S) !> S = M^{-1}*R
54+
call M%matvec(R,S) !> S = M^{-1}*R
5455
S = merge( zero_${s}$, S, di )
5556
zr2 = A%inner_product( R, S )
5657

@@ -80,7 +81,7 @@ contains
8081

8182
#:for matrix in MATRIX_TYPES
8283
#:for k, t, s in R_KINDS_TYPES
83-
module subroutine solve_pccg_${matrix}$_${s}$(A,b,x,di,tol,maxiter,restart,workspace)
84+
module subroutine solve_pccg_${matrix}$_${s}$(A,b,x,di,tol,maxiter,restart,M,workspace)
8485
#:if matrix == "dense"
8586
${t}$, intent(in) :: A(:,:)
8687
#:else
@@ -92,9 +93,11 @@ contains
9293
logical(1), intent(in), optional, target :: di(:)
9394
integer, intent(in), optional :: maxiter
9495
logical, intent(in), optional :: restart
96+
class(linop_${s}$), optional , intent(in), target :: M !> preconditionner
9597
type(solver_workspace_${s}$), optional, intent(inout), target :: workspace
9698
!-------------------------
9799
type(linop_${s}$) :: op
100+
type(linop_${s}$), pointer :: M_ => null()
98101
type(solver_workspace_${s}$), pointer :: workspace_
99102
integer :: n, maxiter_
100103
${t}$ :: tol_
@@ -109,8 +112,12 @@ contains
109112
!-------------------------
110113
! internal memory setup
111114
op%matvec => default_matvec
112-
! op%inner_product => default_dot
113-
op%preconditionner => default_preconditionner
115+
if(present(M)) then
116+
M_ => M
117+
else
118+
allocate( M_ )
119+
M_%matvec => default_preconditionner
120+
end if
114121
if(present(di))then
115122
di_ => di
116123
else
@@ -125,7 +132,7 @@ contains
125132
if(.not.allocated(workspace_%tmp)) allocate( workspace_%tmp(n,size_wksp_pccg) , source = zero_${s}$ )
126133
!-------------------------
127134
! main call to the solver
128-
call solve_pccg_generic(op,b,x,di_,tol_,maxiter_,restart_,workspace_)
135+
call solve_pccg_generic(op,M_,b,x,di_,tol_,maxiter_,restart_,workspace_)
129136

130137
!-------------------------
131138
! internal memory cleanup
@@ -136,6 +143,7 @@ contains
136143
deallocate( workspace_%tmp )
137144
deallocate( workspace_ )
138145
end if
146+
M_ => null()
139147
workspace_ => null()
140148
contains
141149

0 commit comments

Comments
 (0)