138 rel_tol, abs_tol, monitor)
140 integer,
intent(in) :: n
141 integer,
intent(in) :: max_iter
142 class(pc_t),
optional,
intent(in),
target :: M
143 integer,
optional,
intent(in) :: m_restart
144 real(kind=rp),
optional,
intent(in) :: rel_tol
145 real(kind=rp),
optional,
intent(in) :: abs_tol
146 logical,
optional,
intent(in) :: monitor
147 type(device_ident_t),
target :: M_ident
149 integer(c_size_t) :: z_size
152 if (
present(m_restart))
then
153 this%m_restart = m_restart
169 call device_map(this%w, this%w_d, n)
170 call device_map(this%r, this%r_d, n)
172 allocate(this%c(this%m_restart))
173 allocate(this%s(this%m_restart))
174 allocate(this%gam(this%m_restart + 1))
175 call device_map(this%c, this%c_d, this%m_restart)
176 call device_map(this%s, this%s_d, this%m_restart)
177 call device_map(this%gam, this%gam_d, this%m_restart+1)
179 allocate(this%z(n, this%m_restart))
180 allocate(this%v(n, this%m_restart))
181 allocate(this%h(this%m_restart, this%m_restart))
182 allocate(this%z_d(this%m_restart))
183 allocate(this%v_d(this%m_restart))
184 allocate(this%h_d(this%m_restart))
185 do i = 1, this%m_restart
186 this%z_d(i) = c_null_ptr
187 call device_map(this%z(:,i), this%z_d(i), n)
189 this%v_d(i) = c_null_ptr
190 call device_map(this%v(:,i), this%v_d(i), n)
192 this%h_d(i) = c_null_ptr
193 call device_map(this%h(:,i), this%h_d(i), this%m_restart)
196 z_size = c_sizeof(c_null_ptr) * (this%m_restart)
197 call device_alloc(this%z_d_d, z_size)
198 call device_alloc(this%v_d_d, z_size)
199 call device_alloc(this%h_d_d, z_size)
200 ptr = c_loc(this%z_d)
201 call device_memcpy(ptr, this%z_d_d, z_size, &
202 host_to_device, sync = .false.)
203 ptr = c_loc(this%v_d)
204 call device_memcpy(ptr, this%v_d_d, z_size, &
205 host_to_device, sync = .false.)
206 ptr = c_loc(this%h_d)
207 call device_memcpy(ptr, this%h_d_d, z_size, &
208 host_to_device, sync = .false.)
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)
229 call device_event_create(this%gs_event, 2)
324 class(ax_t),
intent(in) :: ax
325 type(field_t),
intent(inout) :: x
326 integer,
intent(in) :: n
327 real(kind=rp),
dimension(n),
intent(in) :: f
328 type(coef_t),
intent(inout) :: coef
329 type(bc_list_t),
intent(inout) :: blst
330 type(gs_t),
intent(inout) :: gs_h
331 type(ksp_monitor_t) :: ksp_results
332 integer,
optional,
intent(in) :: niter
333 integer :: iter, max_iter
335 real(kind=rp) :: rnorm, alpha, temp, lr, alpha2, norm_fac
339 f_d = device_get_ptr(f)
344 if (
present(niter))
then
347 max_iter = this%max_iter
350 associate(w => this%w, c => this%c, r => this%r, z => this%z, h => this%h, &
351 v => this%v, s => this%s, gam => this%gam, v_d => this%v_d, &
352 w_d => this%w_d, r_d => this%r_d, h_d => this%h_d, &
353 v_d_d => this%v_d_d, x_d => x%x_d, z_d_d => this%z_d_d, &
356 norm_fac = 1.0_rp / sqrt(coef%volume)
357 call rzero(gam, this%m_restart + 1)
358 call rone(s, this%m_restart)
359 call rone(c, this%m_restart)
360 call rzero(h, this%m_restart * this%m_restart)
361 call device_rzero(x%x_d, n)
362 call device_rzero(this%gam_d, this%m_restart + 1)
363 call device_rone(this%s_d, this%m_restart)
364 call device_rone(this%c_d, this%m_restart)
366 call rzero(this%h, this%m_restart**2)
371 call this%monitor_start(
'GMRES')
372 do while (.not. conv .and. iter .lt. max_iter)
374 if (iter .eq. 0)
then
375 call device_copy(r_d, f_d, n)
377 call device_copy(r_d, f_d, n)
378 call ax%compute(w, x%x, coef, x%msh, x%Xh)
379 call gs_h%op(w, n, gs_op_add, this%gs_event)
380 call device_event_sync(this%gs_event)
381 call blst%apply_scalar(w, n)
382 call device_sub2(r_d, w_d, n)
385 gam(1) = sqrt(device_glsc3(r_d, r_d, coef%mult_d, n))
386 if (iter .eq. 0)
then
387 ksp_results%res_start = gam(1) * norm_fac
390 if (abscmp(gam(1), 0.0_rp))
return
393 temp = 1.0_rp / gam(1)
394 call device_cmult2(v_d(1), r_d, temp, n)
395 do j = 1, this%m_restart
398 call this%M%solve(z(1,j), v(1,j), n)
400 call ax%compute(w, z(1,j), coef, x%msh, x%Xh)
401 call gs_h%op(w, n, gs_op_add, this%gs_event)
402 call device_event_sync(this%gs_event)
403 call blst%apply_scalar(w, n)
405 if (neko_bcknd_opencl .eq. 1)
then
407 h(i,j) = device_glsc3(w_d, v_d(i), coef%mult_d, n)
409 call device_add2s2(w_d, v_d(i), -h(i,j), n)
411 alpha2 = device_glsc3(w_d, w_d, coef%mult_d, n)
414 call device_glsc3_many(h(1,j), w_d, v_d_d, coef%mult_d, j, n)
416 call device_memcpy(h(:,j), h_d(j), j, &
417 host_to_device, sync = .false.)
427 h(i,j) = c(i)*temp + s(i) * h(i+1,j)
428 h(i+1,j) = -s(i)*temp + c(i) * h(i+1,j)
432 if (abscmp(alpha, 0.0_rp))
then
437 lr = sqrt(h(j,j) * h(j,j) + alpha2)
442 call device_memcpy(h(:,j), h_d(j), j, &
443 host_to_device, sync = .false.)
444 gam(j+1) = -s(j) * gam(j)
445 gam(j) = c(j) * gam(j)
447 rnorm = abs(gam(j+1)) * norm_fac
448 call this%monitor_iter(iter, rnorm)
449 if (rnorm .lt. this%abs_tol)
then
454 if (iter + 1 .gt. max_iter)
exit
456 if (j .lt. this%m_restart)
then
457 temp = 1.0_rp / alpha
458 call device_cmult2(v_d(j+1), w_d, temp, n)
463 j = min(j, this%m_restart)
467 temp = temp - h(k,i) * c(i)
472 if (neko_bcknd_opencl .eq. 1)
then
474 call device_add2s2(x_d, this%z_d(i), c(i), n)
477 call device_memcpy(c, c_d, j, host_to_device, sync = .false.)
478 call device_add2s2_many(x_d, z_d_d, c_d, j, n)
483 call this%monitor_stop()
484 ksp_results%res_final = rnorm
485 ksp_results%iter = iter
486 ksp_results%converged = this%is_converged(iter, rnorm)
492 n, coef, blstx, blsty, blstz, gs_h, niter)
result(ksp_results)
494 class(ax_t),
intent(in) :: ax
495 type(field_t),
intent(inout) :: x
496 type(field_t),
intent(inout) :: y
497 type(field_t),
intent(inout) :: z
498 integer,
intent(in) :: n
499 real(kind=rp),
dimension(n),
intent(in) :: fx
500 real(kind=rp),
dimension(n),
intent(in) :: fy
501 real(kind=rp),
dimension(n),
intent(in) :: fz
502 type(coef_t),
intent(inout) :: coef
503 type(bc_list_t),
intent(inout) :: blstx
504 type(bc_list_t),
intent(inout) :: blsty
505 type(bc_list_t),
intent(inout) :: blstz
506 type(gs_t),
intent(inout) :: gs_h
507 type(ksp_monitor_t),
dimension(3) :: ksp_results
508 integer,
optional,
intent(in) :: niter
510 ksp_results(1) = this%solve(ax, x, fx, n, coef, blstx, gs_h, niter)
511 ksp_results(2) = this%solve(ax, y, fy, n, coef, blsty, gs_h, niter)
512 ksp_results(3) = this%solve(ax, z, fz, n, coef, blstz, gs_h, niter)