52 use,
intrinsic :: iso_c_binding, only : c_ptr, c_int, &
53 c_null_ptr, c_associated
59 real(kind=
rp),
allocatable :: d(:)
60 real(kind=
rp),
allocatable :: w(:)
61 real(kind=
rp),
allocatable :: r(:)
62 type(c_ptr) :: d_d = c_null_ptr
63 type(c_ptr) :: w_d = c_null_ptr
64 type(c_ptr) :: r_d = c_null_ptr
65 type(c_ptr) :: gs_event = c_null_ptr
66 real(kind=
rp) :: tha, dlt
67 integer :: power_its = 150
68 logical :: recompute_eigs = .true.
69 logical :: zero_initial_guess = .false.
81 bind(c, name =
'hip_cheby_part1')
82 use,
intrinsic :: iso_c_binding
85 type(c_ptr),
value :: d_d, x_d, strm
93 bind(c, name =
'hip_cheby_part2')
94 use,
intrinsic :: iso_c_binding
97 type(c_ptr),
value :: d_d, w_d, x_d, strm
98 real(c_rp) :: tmp1, tmp2
104 subroutine cuda_cheby_device_part1(d_d, x_d, inv_tha, n, strm) &
105 bind(c, name =
'cuda_cheby_part1')
106 use,
intrinsic :: iso_c_binding
109 type(c_ptr),
value :: d_d, x_d, strm
110 real(c_rp) :: inv_tha
112 end subroutine cuda_cheby_device_part1
116 subroutine cuda_cheby_device_part2(d_d, w_d, x_d, tmp1, tmp2, n, strm) &
117 bind(c, name =
'cuda_cheby_part2')
118 use,
intrinsic :: iso_c_binding
121 type(c_ptr),
value :: d_d, w_d, x_d, strm
122 real(c_rp) :: tmp1, tmp2
124 end subroutine cuda_cheby_device_part2
128 subroutine metal_cheby_device_part1(d_d, x_d, inv_tha, n, strm) &
129 bind(c, name =
'metal_cheby_part1')
130 use,
intrinsic :: iso_c_binding
133 type(c_ptr),
value :: d_d, x_d, strm
134 real(c_rp) :: inv_tha
136 end subroutine metal_cheby_device_part1
140 subroutine metal_cheby_device_part2(d_d, w_d, x_d, tmp1, tmp2, n, strm) &
141 bind(c, name =
'metal_cheby_part2')
142 use,
intrinsic :: iso_c_binding
145 type(c_ptr),
value :: d_d, w_d, x_d, strm
146 real(c_rp) :: tmp1, tmp2
148 end subroutine metal_cheby_device_part2
154 type(c_ptr) :: d_d, x_d
155 real(c_rp) :: inv_tha
160 call cuda_cheby_device_part1(d_d, x_d, inv_tha, n,
glb_cmd_queue)
162 call metal_cheby_device_part1(d_d, x_d, inv_tha, n,
glb_cmd_queue)
163#else !Fallback to device_math for missing device kernels
172 type(c_ptr) :: d_d, w_d, x_d
173 real(c_rp) :: tmp1, tmp2
178 call cuda_cheby_device_part2(d_d, w_d, x_d, tmp1, tmp2, n,
glb_cmd_queue)
180 call metal_cheby_device_part2(d_d, w_d, x_d, tmp1, tmp2, n,
glb_cmd_queue)
181#else !Fallback to device_math for missing device kernels
191 integer,
intent(in) :: max_iter
192 class(
pc_t),
optional,
intent(in),
target :: M
193 integer,
intent(in) :: n
194 real(kind=
rp),
optional,
intent(in) :: rel_tol
195 real(kind=
rp),
optional,
intent(in) :: abs_tol
196 logical,
optional,
intent(in) :: monitor
211 if (
present(rel_tol) .and.
present(abs_tol) .and.
present(monitor))
then
212 call this%ksp_init(max_iter, rel_tol, abs_tol, monitor = monitor)
213 else if (
present(rel_tol) .and.
present(abs_tol))
then
214 call this%ksp_init(max_iter, rel_tol, abs_tol)
215 else if (
present(monitor) .and.
present(abs_tol))
then
216 call this%ksp_init(max_iter, abs_tol = abs_tol, monitor = monitor)
217 else if (
present(rel_tol) .and.
present(monitor))
then
218 call this%ksp_init(max_iter, rel_tol, monitor = monitor)
219 else if (
present(rel_tol))
then
220 call this%ksp_init(max_iter, rel_tol = rel_tol)
221 else if (
present(abs_tol))
then
222 call this%ksp_init(max_iter, abs_tol = abs_tol)
223 else if (
present(monitor))
then
224 call this%ksp_init(max_iter, monitor = monitor)
226 call this%ksp_init(max_iter)
238 if (
allocated(this%d))
then
239 if (c_associated(this%d_d))
then
245 if (
allocated(this%w))
then
246 if (c_associated(this%w_d))
then
252 if (
allocated(this%r))
then
253 if (c_associated(this%r_d))
then
261 if (c_associated(this%gs_event))
then
269 class(
ax_t),
intent(in) :: Ax
270 type(
field_t),
intent(inout) :: x
271 integer,
intent(in) :: n
272 type(
coef_t),
intent(inout) :: coef
274 type(
gs_t),
intent(inout) :: gs_h
275 real(kind=
rp) :: lam, b, a, rn
276 real(kind=
rp) :: boost = 1.1_rp
277 real(kind=
rp) :: lam_factor = 30.0_rp
278 real(kind=
rp) :: wtw, dtw, dtd
279 integer,
allocatable :: fixed_seed(:), saved_seed(:)
282 associate(w => this%w, w_d => this%w_d, d => this%d, d_d => this%d_d)
285 call random_seed(
size = rnd_n )
286 allocate(saved_seed(rnd_n))
287 allocate(fixed_seed(rnd_n))
289 call random_seed( get = saved_seed )
290 call random_seed( put = fixed_seed )
293 call random_number(rn)
299 call random_seed( put = saved_seed )
301 call gs_h%op(d, n, gs_op_add, this%gs_event)
302 call blst%apply(d, n)
305 do i = 1, this%power_its
306 call ax%compute(w, d, coef, x%msh, x%Xh)
307 call gs_h%op(w, n, gs_op_add, this%gs_event)
308 call blst%apply(w, n)
309 if (
associated(this%schwarz))
then
310 call this%schwarz%compute(this%r, w)
313 call this%M%solve(this%r, w, n)
319 call blst%apply(d, n)
322 call ax%compute(w, d, coef, x%msh, x%Xh)
323 call gs_h%op(w, n, gs_op_add, this%gs_event)
324 call blst%apply(w, n)
325 if (
associated(this%schwarz))
then
326 call this%schwarz%compute(this%r, w)
329 call this%M%solve(this%r, w, n)
338 this%tha = (b+a)/2.0_rp
339 this%dlt = (b-a)/2.0_rp
341 this%recompute_eigs = .false.
349 class(ax_t),
intent(in) :: ax
350 type(field_t),
intent(inout) :: x
351 integer,
intent(in) :: n
352 real(kind=rp),
dimension(n),
intent(in) :: f
353 type(coef_t),
intent(inout) :: coef
354 type(bc_list_t),
intent(inout) :: blst
355 type(gs_t),
intent(inout) :: gs_h
356 type(ksp_monitor_t) :: ksp_results
357 integer,
optional,
intent(in) :: niter
358 integer :: iter, max_iter
359 real(kind=rp) :: a, b, rtr, rnorm, norm_fac
362 f_d = device_get_ptr(f)
364 if (this%recompute_eigs)
then
368 if (
present(niter))
then
371 max_iter = this%max_iter
373 norm_fac = 1.0_rp / sqrt(coef%volume)
375 associate( w => this%w, r => this%r, d => this%d, &
376 w_d => this%w_d, r_d => this%r_d, d_d => this%d_d)
378 call device_copy(r_d, f_d, n)
379 call ax%compute(w, x%x, coef, x%msh, x%Xh)
380 call gs_h%op(w, n, gs_op_add, this%gs_event)
381 call blst%apply(w, n)
382 call device_sub2(r_d, w_d, n)
384 rtr = device_glsc3(r_d, coef%mult_d, r_d, n)
385 rnorm = sqrt(rtr) * norm_fac
386 ksp_results%res_start = rnorm
387 ksp_results%res_final = rnorm
391 call this%M%solve(w, r, n)
392 call device_copy(d_d, w_d, n)
393 a = 2.0_rp / this%tha
394 call device_add2s2(x%x_d, d_d, a, n)
397 do iter = 2, max_iter
399 call device_copy(r_d, f_d, n)
400 call ax%compute(w, x%x, coef, x%msh, x%Xh)
401 call gs_h%op(w, n, gs_op_add, this%gs_event)
402 call blst%apply(w, n)
403 call device_sub2(r_d, w_d, n)
405 call this%M%solve(w, r, n)
407 if (iter .eq. 2)
then
408 b = 0.5_rp * (this%dlt * a)**2
410 b = (this%dlt * a / 2.0_rp)**2
412 a = 1.0_rp/(this%tha - b/a)
413 call device_add2s1(d_d, w_d, b, n)
415 call device_add2s2(x%x_d, d_d, a, n)
419 call device_copy(r_d, f_d, n)
420 call ax%compute(w, x%x, coef, x%msh, x%Xh)
421 call gs_h%op(w, n, gs_op_add, this%gs_event)
422 call blst%apply(w, n)
423 call device_sub2(r_d, w_d, n)
424 rtr = device_glsc3(r_d, coef%mult_d, r_d, n)
425 rnorm = sqrt(rtr) * norm_fac
428 ksp_results%res_final = rnorm
429 ksp_results%iter = iter
430 ksp_results%converged = this%is_converged(iter, rnorm)
438 class(ax_t),
intent(in) :: ax
439 type(field_t),
intent(inout) :: x
440 integer,
intent(in) :: n
441 real(kind=rp),
dimension(n),
intent(in) :: f
442 type(coef_t),
intent(inout) :: coef
443 type(bc_list_t),
intent(inout) :: blst
444 type(gs_t),
intent(inout) :: gs_h
445 type(ksp_monitor_t) :: ksp_results
446 integer,
optional,
intent(in) :: niter
447 integer :: iter, max_iter
448 real(kind=rp) :: a, b, rtr, rnorm, norm_fac
449 real(kind=rp) :: rhok, rhokp1, sig1, tmp1, tmp2
452 f_d = device_get_ptr(f)
454 if (this%recompute_eigs)
then
458 if (
present(niter))
then
461 max_iter = this%max_iter
463 norm_fac = 1.0_rp / sqrt(coef%volume)
465 associate( w => this%w, r => this%r, d => this%d, &
466 w_d => this%w_d, r_d => this%r_d, d_d => this%d_d)
468 if (.not.this%zero_initial_guess)
then
469 call ax%compute(w, x%x, coef, x%msh, x%Xh)
470 call gs_h%op(w, n, gs_op_add, this%gs_event)
471 call blst%apply(w, n)
472 call device_sub3(r_d, f_d, w_d, n)
474 call device_copy(r_d, f_d, n)
475 this%zero_initial_guess = .false.
479 if (
associated(this%schwarz))
then
480 call this%schwarz%compute(d, r)
482 call this%M%solve(d, r, n)
485 tmp1 = 1.0_rp / this%tha
488 sig1 = this%tha / this%dlt
492 do iter = 2, max_iter
493 rhokp1 = 1.0_rp / (2.0_rp * sig1 - rhok)
495 tmp2 = 2.0_rp * rhokp1 / this%dlt
498 call ax%compute(w, x%x, coef, x%msh, x%Xh)
499 call gs_h%op(w, n, gs_op_add, this%gs_event)
500 call blst%apply(w, n)
501 call device_sub3(r_d, f_d, w_d, n)
503 if (
associated(this%schwarz))
then
504 call this%schwarz%compute(w, r)
506 call this%M%solve(w, r, n)
518 n, coef, blstx, blsty, blstz, gs_h, niter)
result(ksp_results)
520 class(ax_t),
intent(in) :: ax
521 type(field_t),
intent(inout) :: x
522 type(field_t),
intent(inout) :: y
523 type(field_t),
intent(inout) :: z
524 integer,
intent(in) :: n
525 real(kind=rp),
dimension(n),
intent(in) :: fx
526 real(kind=rp),
dimension(n),
intent(in) :: fy
527 real(kind=rp),
dimension(n),
intent(in) :: fz
528 type(coef_t),
intent(inout) :: coef
529 type(bc_list_t),
intent(inout) :: blstx
530 type(bc_list_t),
intent(inout) :: blsty
531 type(bc_list_t),
intent(inout) :: blstz
532 type(gs_t),
intent(inout) :: gs_h
533 type(ksp_monitor_t),
dimension(3) :: ksp_results
534 integer,
optional,
intent(in) :: niter
536 ksp_results(1) = this%solve(ax, x, fx, n, coef, blstx, gs_h, niter)
537 ksp_results(2) = this%solve(ax, y, fy, n, coef, blsty, gs_h, niter)
538 ksp_results(3) = this%solve(ax, z, fz, n, coef, blstz, gs_h, niter)
__device__ T solve(const T u, const T y, const T guess, const T nu, const T kappa, const T B)
Return the device pointer for an associated Fortran array.
Map a Fortran array to a device (allocate and associate)
Copy data between host and device (or device and device)
Unmap a Fortran array from a device (deassociate and free)
Defines a Matrix-vector product.
Chebyshev preconditioner.
subroutine cheby_device_init(this, n, max_iter, m, rel_tol, abs_tol, monitor)
Initialise a standard solver.
subroutine cheby_device_part2(d_d, w_d, x_d, tmp1, tmp2, n)
type(ksp_monitor_t) function, dimension(3) cheby_device_solve_coupled(this, ax, x, y, z, fx, fy, fz, n, coef, blstx, blsty, blstz, gs_h, niter)
Standard Cheby_Deviceshev coupled solve.
type(ksp_monitor_t) function cheby_device_solve(this, ax, x, f, n, coef, blst, gs_h, niter)
A chebyshev preconditioner.
subroutine cheby_device_free(this)
subroutine cheby_device_part1(d_d, x_d, inv_tha, n)
subroutine cheby_device_power(this, ax, x, n, coef, blst, gs_h)
type(ksp_monitor_t) function cheby_device_impl(this, ax, x, f, n, coef, blst, gs_h, niter)
A chebyshev preconditioner.
subroutine, public device_add2s1(a_d, b_d, c1, n, strm)
subroutine, public device_sub3(a_d, b_d, c_d, n, strm)
Vector subtraction .
subroutine, public device_add2s2(a_d, b_d, c1, n, strm)
Vector addition with scalar multiplication (multiplication on first argument)
subroutine, public device_add2(a_d, b_d, n, strm)
Vector addition .
subroutine, public device_cmult(a_d, c, n, strm)
Multiplication by constant c .
subroutine, public device_sub2(a_d, b_d, n, strm)
Vector substraction .
subroutine, public device_copy(a_d, b_d, n, strm)
Copy a vector .
real(kind=rp) function, public device_glsc3(a_d, b_d, c_d, n, strm)
Weighted inner product .
subroutine, public device_cmult2(a_d, b_d, c, n, strm)
Multiplication by constant c .
Device abstraction, common interface for various accelerators.
integer, parameter, public host_to_device
subroutine, public device_event_destroy(event)
Destroy a device event.
type(c_ptr), bind(C), public glb_cmd_queue
Global command queue.
subroutine, public device_event_create(event, flags)
Create a device event queue.
Implements the base abstract type for Krylov solvers plus helper types.
integer, parameter, public c_rp
integer, parameter, public rp
Global precision used in computations.
Overlapping schwarz solves.
Defines a function space.
Base type for a matrix-vector product providing .
A list of allocatable `bc_t`. Follows the standard interface of lists.
Defines a Chebyshev preconditioner.
Coefficients defined on a given (mesh, ) tuple. Arrays use indices (i,j,k,e): element e,...
Type for storing initial and final residuals in a Krylov solver.
Base abstract type for a canonical Krylov method, solving .
Defines a canonical Krylov preconditioner.
The function space for the SEM solution fields.