Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
krylov_fctry.f90
Go to the documentation of this file.
1! Copyright (c) 2021-2025, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32!
33submodule(krylov) krylov_fctry
34 use cg, only : cg_t
35 use cg_sx, only : sx_cg_t
36 use cg_cpld, only : cg_cpld_t
37 use cg_device, only : cg_device_t
39 use cacg, only : cacg_t
40 use pipecg, only : pipecg_t
41 use pipecg_sx, only : sx_pipecg_t
45 use bicgstab, only : bicgstab_t
48 use gmres, only : gmres_t
49 use cheby, only : cheby_t
51 use gmres_sx, only : sx_gmres_t
53 use num_types, only : rp
54 use precon, only : pc_t
56 use json_utils, only : json_get
58 implicit none
59
60 ! List of all possible types created by the factory routine
61 character(len=20) :: KSP_KNOWN_TYPES(10) = [character(len=20) :: &
62 "cg", &
63 "pipecg", &
64 "fused_cg", &
65 "cacg", &
66 "gmres", &
67 "cheby", &
68 "bicgstab", &
69 "coupled_bicgstab", &
70 "fused_coupled_cg", &
71 "coupled_cg"]
72
73contains
74
83 module subroutine krylov_solver_factory_components(object, n, type_name, &
84 max_iter, abstol, m, monitor)
85 class(ksp_t), allocatable, intent(inout) :: object
86 integer, intent(in), value :: n
87 character(len=*), intent(in) :: type_name
88 integer, intent(in) :: max_iter
89 real(kind=rp), optional :: abstol
90 class(pc_t), optional, intent(in), target :: M
91 logical, optional, intent(in) :: monitor
92
93 call krylov_solver_allocator(object, type_name)
94
95 call object%init(n, max_iter, m = m, abs_tol = abstol, monitor = monitor)
96
97 end subroutine krylov_solver_factory_components
98
105 module subroutine krylov_solver_factory_json(object, n, json, m)
106 class(ksp_t), allocatable, intent(inout) :: object
107 integer, intent(in), value :: n
108 type(json_file), intent(inout) :: json
109 class(pc_t), optional, intent(in), target :: M
110 character(len=:), allocatable :: type_name
111
112 call json_get(json, 'type', type_name)
113 call krylov_solver_allocator(object, type_name)
114
115 call object%init_from_json(n, json, m = m)
116
117 end subroutine krylov_solver_factory_json
118
122 module subroutine krylov_solver_allocator(object, type_name)
123 class(ksp_t), allocatable, intent(inout) :: object
124 character(len=*), intent(in) :: type_name
125 integer :: i
126
127 if (allocated(object)) then
128 call object%free()
129 deallocate(object)
130 end if
131
132 select case (trim(type_name))
133 case ('cg')
134 if (neko_bcknd_sx .eq. 1) then
135 allocate(sx_cg_t::object)
136 else if (neko_bcknd_device .eq. 1) then
137 allocate(cg_device_t::object)
138 else
139 allocate(cg_t::object)
140 end if
141
142 case ('coupled_cg')
143 if (neko_bcknd_device .eq. 1) then
144 allocate(cg_cpld_device_t::object)
145 else
146 allocate(cg_cpld_t::object)
147 end if
148
149 case ('pipecg')
150 if (neko_bcknd_sx .eq. 1) then
151 allocate(sx_pipecg_t::object)
152 else if (neko_bcknd_device .eq. 1) then
153 if (neko_bcknd_opencl .eq. 1 .or. neko_bcknd_metal .eq. 1) then
154 call neko_error('PipeCG not supported for OpenCL/Metal')
155 end if
156 allocate(pipecg_device_t::object)
157 else
158 allocate(pipecg_t::object)
159 end if
160
161 case ('fused_cg')
162 if (neko_bcknd_device .eq. 1) then
163 if (neko_bcknd_opencl .eq. 1 .or. neko_bcknd_metal .eq. 1) then
164 call neko_error('FusedCG not supported for OpenCL/Metal')
165 end if
166 allocate(fusedcg_device_t::object)
167 else
168 call neko_error('FusedCG only supported for CUDA/HIP')
169 end if
170
171 case ('fused_coupled_cg')
172 if (neko_bcknd_device .eq. 1) then
173 if (neko_bcknd_opencl .eq. 1 .or. neko_bcknd_metal .eq. 1) then
174 call neko_error('Coupled FusedCG not supported for OpenCL/Metal')
175 end if
176 allocate(fusedcg_cpld_device_t::object)
177 else
178 call neko_error('Coupled FusedCG only supported for CUDA/HIP')
179 end if
180
181 case ('cacg')
182 allocate(cacg_t::object)
183
184 case ('gmres')
185 if (neko_bcknd_sx .eq. 1) then
186 allocate(sx_gmres_t::object)
187 else if (neko_bcknd_device .eq. 1) then
188 allocate(gmres_device_t::object)
189 else
190 allocate(gmres_t::object)
191 end if
192
193 case ('cheby')
194 if (neko_bcknd_device .eq. 1) then
195 allocate(cheby_device_t::object)
196 else
197 allocate(cheby_t::object)
198 end if
199
200 case ('bicgstab')
201 if (neko_bcknd_device .eq. 1) then
202 allocate(bicgstab_device_t::object)
203 else
204 allocate(bicgstab_t::object)
205 end if
206
207 case ('coupled_bicgstab')
208 if (neko_bcknd_device .eq. 1) then
209 call neko_error('Coupled BiCGStab is not supported on devices')
210 else
211 allocate(bicgstab_cpld_t::object)
212 end if
213
214 case default
215 do i = 1, krylov_registry_size
216 if (trim(type_name) .eq. trim(krylov_registry(i)%type_name)) then
217 call krylov_registry(i)%allocator(object)
218 return
219 end if
220 end do
221
222 call neko_type_error('Krylov solver', type_name, ksp_known_types)
223 end select
224
225 end subroutine krylov_solver_allocator
226
232 module subroutine register_krylov(type_name, allocator)
233 character(len=*), intent(in) :: type_name
234 procedure(krylov_allocate), pointer, intent(in) :: allocator
235 type(krylov_allocator_entry), allocatable :: temp(:)
236 integer :: i
237
238 do i = 1, size(ksp_known_types)
239 if (trim(type_name) .eq. trim(ksp_known_types(i))) then
240 call neko_type_registration_error("Krylov solver", type_name, &
241 .true.)
242 end if
243 end do
244
245 do i = 1, krylov_registry_size
246 if (trim(type_name) .eq. trim(krylov_registry(i)%type_name)) then
247 call neko_type_registration_error("Krylov solver", type_name, &
248 .false.)
249 end if
250 end do
251
252 if (krylov_registry_size .eq. 0) then
253 allocate(krylov_registry(1))
254 else
255 allocate(temp(krylov_registry_size + 1))
256 temp(1:krylov_registry_size) = krylov_registry
257 call move_alloc(temp, krylov_registry)
258 end if
259
260 krylov_registry_size = krylov_registry_size + 1
261 krylov_registry(krylov_registry_size)%type_name = type_name
262 krylov_registry(krylov_registry_size)%allocator => allocator
263 end subroutine register_krylov
264
265end submodule krylov_fctry
Retrieves a parameter by name or throws an error.
Provides a coupled CPU implementation of the BiCGStab method.
Provides a device implementation of the BiCGStab method.
Provides a CPU implementation of the BiCGStab method.
Definition bicgstab.f90:34
Defines a communication avoiding Conjugate Gradient method.
Definition cacg.f90:34
Defines a coupled Conjugate Gradient methods for accelerators.
Defines a coupled Conjugate Gradient method.
Defines various Conjugate Gradient methods for accelerators.
Definition cg_device.f90:34
Defines various Conjugate Gradient methods.
Definition cg_sx.f90:34
Defines various Conjugate Gradient methods.
Definition cg.f90:34
Chebyshev preconditioner.
Chebyshev preconditioner.
Definition cheby.f90:34
Defines a fused Conjugate Gradient method for accelerators.
Defines a fused Conjugate Gradient method for accelerators.
Defines various GMRES methods.
Defines various GMRES methods.
Definition gmres_sx.f90:34
Implements gmres_t.
Definition gmres.f90:34
Utilities for retrieving parameters from the case files.
Implements the base abstract type for Krylov solvers plus helper types.
Definition krylov.f90:34
Build configurations.
integer, parameter neko_bcknd_sx
integer, parameter neko_bcknd_device
integer, parameter neko_bcknd_opencl
integer, parameter neko_bcknd_metal
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Defines a pipelined Conjugate Gradient methods.
Defines a pipelined Conjugate Gradient methods SX-Aurora backend.
Definition pipecg_sx.f90:34
Defines a pipelined Conjugate Gradient methods.
Definition pipecg.f90:34
Krylov preconditioner.
Definition precon.f90:34
Utilities.
Definition utils.f90:35
subroutine, public neko_type_registration_error(base_type, wrong_type, known)
Definition utils.f90:433
subroutine, public neko_type_error(base_type, wrong_type, known_types)
Reports an error allocating a type for a particular base pointer class.
Definition utils.f90:415
CPU implementation of the right-preconditioned BiCGStab method.
Definition bicgstab.f90:59
Coupled right-preconditioned CPU BiCGStab method.
Device implementation of the right-preconditioned BiCGStab method.
S-step communication avoiding preconditioned conjugate gradient method.
Definition cacg.f90:55
CPU implementation of the preconditioned conjugate gradient method.
Definition cg.f90:60
CPU implementation of the coupled preconditioned conjugate gradient method.
Device based coupled preconditioned conjugate gradient method.
Device based preconditioned conjugate gradient method.
Definition cg_device.f90:55
Standard preconditioned conjugate gradient method (SX version)
Definition cg_sx.f90:50
Defines a Chebyshev preconditioner.
Definition cheby.f90:55
Defines a Chebyshev preconditioner.
Fused preconditioned conjugate gradient method.
Fused preconditioned conjugate gradient method.
CPU implementation of the preconditioned standard GMRES method.
Definition gmres.f90:67
Standard preconditioned generalized minimal residual method.
Standard preconditioned generalized minimal residual method (SX version)
Definition gmres_sx.f90:57
Pipelined preconditioned conjugate gradient method.
Definition pipecg.f90:56
Pipelined preconditioned conjugate gradient method.
Pipelined preconditioned conjugate gradient method for SX-Aurora.
Definition pipecg_sx.f90:53
Defines a canonical Krylov preconditioner.
Definition precon.f90:44