134 rel_tol, abs_tol, monitor)
136 integer,
intent(in) :: n
137 integer,
intent(in) :: max_iter
138 class(pc_t),
optional,
intent(in),
target :: M
139 integer,
optional,
intent(in) :: m_restart
140 real(kind=rp),
optional,
intent(in) :: rel_tol
141 real(kind=rp),
optional,
intent(in) :: abs_tol
142 logical,
optional,
intent(in) :: monitor
143 type(device_ident_t),
target :: M_ident
145 integer(c_size_t) :: z_size
148 if (
present(m_restart))
then
149 this%m_restart = m_restart
165 call device_map(this%w, this%w_d, n)
166 call device_map(this%r, this%r_d, n)
168 allocate(this%c(this%m_restart))
169 allocate(this%s(this%m_restart))
170 allocate(this%gam(this%m_restart + 1))
171 call device_map(this%c, this%c_d, this%m_restart)
172 call device_map(this%s, this%s_d, this%m_restart)
173 call device_map(this%gam, this%gam_d, this%m_restart+1)
175 allocate(this%z(n, this%m_restart))
176 allocate(this%v(n, this%m_restart))
177 allocate(this%h(this%m_restart, this%m_restart))
178 allocate(this%z_d(this%m_restart))
179 allocate(this%v_d(this%m_restart))
180 allocate(this%h_d(this%m_restart))
181 do i = 1, this%m_restart
182 this%z_d(i) = c_null_ptr
183 call device_map(this%z(:,i), this%z_d(i), n)
185 this%v_d(i) = c_null_ptr
186 call device_map(this%v(:,i), this%v_d(i), n)
188 this%h_d(i) = c_null_ptr
189 call device_map(this%h(:,i), this%h_d(i), this%m_restart)
192 z_size = c_sizeof(c_null_ptr) * (this%m_restart)
193 call device_alloc(this%z_d_d, z_size)
194 call device_alloc(this%v_d_d, z_size)
195 call device_alloc(this%h_d_d, z_size)
196 ptr = c_loc(this%z_d)
197 call device_memcpy(ptr, this%z_d_d, z_size, &
198 host_to_device, sync = .false.)
199 ptr = c_loc(this%v_d)
200 call device_memcpy(ptr, this%v_d_d, z_size, &
201 host_to_device, sync = .false.)
202 ptr = c_loc(this%h_d)
203 call device_memcpy(ptr, this%h_d_d, z_size, &
204 host_to_device, sync = .false.)
207 if (
present(rel_tol) .and.
present(abs_tol) .and.
present(monitor))
then
208 call this%ksp_init(max_iter, rel_tol, abs_tol, monitor = monitor)
209 else if (
present(rel_tol) .and.
present(abs_tol))
then
210 call this%ksp_init(max_iter, rel_tol, abs_tol)
211 else if (
present(monitor) .and.
present(abs_tol))
then
212 call this%ksp_init(max_iter, abs_tol = abs_tol, monitor = monitor)
213 else if (
present(rel_tol) .and.
present(monitor))
then
214 call this%ksp_init(max_iter, rel_tol, monitor = monitor)
215 else if (
present(rel_tol))
then
216 call this%ksp_init(max_iter, rel_tol = rel_tol)
217 else if (
present(abs_tol))
then
218 call this%ksp_init(max_iter, abs_tol = abs_tol)
219 else if (
present(monitor))
then
220 call this%ksp_init(max_iter, monitor = monitor)
222 call this%ksp_init(max_iter)
225 call device_event_create(this%gs_event, 2)
320 class(ax_t),
intent(in) :: ax
321 type(field_t),
intent(inout) :: x
322 integer,
intent(in) :: n
323 real(kind=rp),
dimension(n),
intent(in) :: f
324 type(coef_t),
intent(inout) :: coef
325 type(bc_list_t),
intent(inout) :: blst
326 type(gs_t),
intent(inout) :: gs_h
327 type(ksp_monitor_t) :: ksp_results
328 integer,
optional,
intent(in) :: niter
329 integer :: iter, max_iter
331 real(kind=rp) :: rnorm, alpha, temp, lr, alpha2, norm_fac
335 f_d = device_get_ptr(f)
340 if (
present(niter))
then
343 max_iter = this%max_iter
346 associate(w => this%w, c => this%c, r => this%r, z => this%z, h => this%h, &
347 v => this%v, s => this%s, gam => this%gam, v_d => this%v_d, &
348 w_d => this%w_d, r_d => this%r_d, h_d => this%h_d, &
349 v_d_d => this%v_d_d, x_d => x%x_d, z_d_d => this%z_d_d, &
352 norm_fac = 1.0_rp / sqrt(coef%volume)
353 call rzero(gam, this%m_restart + 1)
354 call rone(s, this%m_restart)
355 call rone(c, this%m_restart)
356 call rzero(h, this%m_restart * this%m_restart)
357 call device_rzero(x%x_d, n)
358 call device_rzero(this%gam_d, this%m_restart + 1)
359 call device_rone(this%s_d, this%m_restart)
360 call device_rone(this%c_d, this%m_restart)
362 call rzero(this%h, this%m_restart**2)
367 call this%monitor_start(
'GMRES')
368 do while (.not. conv .and. iter .lt. max_iter)
370 if (iter .eq. 0)
then
371 call device_copy(r_d, f_d, n)
373 call device_copy(r_d, f_d, n)
374 call ax%compute(w, x%x, coef, x%msh, x%Xh)
375 call gs_h%op(w, n, gs_op_add, this%gs_event)
376 call device_event_sync(this%gs_event)
377 call blst%apply_scalar(w, n)
378 call device_sub2(r_d, w_d, n)
381 gam(1) = sqrt(device_glsc3(r_d, r_d, coef%mult_d, n))
382 if (iter .eq. 0)
then
383 ksp_results%res_start = gam(1) * norm_fac
386 if (abscmp(gam(1), 0.0_rp))
return
389 temp = 1.0_rp / gam(1)
390 call device_cmult2(v_d(1), r_d, temp, n)
391 do j = 1, this%m_restart
394 call this%M%solve(z(1,j), v(1,j), n)
396 call ax%compute(w, z(1,j), coef, x%msh, x%Xh)
397 call gs_h%op(w, n, gs_op_add, this%gs_event)
398 call device_event_sync(this%gs_event)
399 call blst%apply_scalar(w, n)
401 if (neko_bcknd_opencl .eq. 1)
then
403 h(i,j) = device_glsc3(w_d, v_d(i), coef%mult_d, n)
405 call device_add2s2(w_d, v_d(i), -h(i,j), n)
407 alpha2 = device_glsc3(w_d, w_d, coef%mult_d, n)
410 call device_glsc3_many(h(1,j), w_d, v_d_d, coef%mult_d, j, n)
412 call device_memcpy(h(:,j), h_d(j), j, &
413 host_to_device, sync = .false.)
423 h(i,j) = c(i)*temp + s(i) * h(i+1,j)
424 h(i+1,j) = -s(i)*temp + c(i) * h(i+1,j)
428 if (abscmp(alpha, 0.0_rp))
then
433 lr = sqrt(h(j,j) * h(j,j) + alpha2)
438 call device_memcpy(h(:,j), h_d(j), j, &
439 host_to_device, sync = .false.)
440 gam(j+1) = -s(j) * gam(j)
441 gam(j) = c(j) * gam(j)
443 rnorm = abs(gam(j+1)) * norm_fac
444 call this%monitor_iter(iter, rnorm)
445 if (rnorm .lt. this%abs_tol)
then
450 if (iter + 1 .gt. max_iter)
exit
452 if (j .lt. this%m_restart)
then
453 temp = 1.0_rp / alpha
454 call device_cmult2(v_d(j+1), w_d, temp, n)
459 j = min(j, this%m_restart)
463 temp = temp - h(k,i) * c(i)
468 if (neko_bcknd_opencl .eq. 1)
then
470 call device_add2s2(x_d, this%z_d(i), c(i), n)
473 call device_memcpy(c, c_d, j, host_to_device, sync = .false.)
474 call device_add2s2_many(x_d, z_d_d, c_d, j, n)
479 call this%monitor_stop()
480 ksp_results%res_final = rnorm
481 ksp_results%iter = iter
482 ksp_results%converged = this%is_converged(iter, rnorm)
488 n, coef, blstx, blsty, blstz, gs_h, niter)
result(ksp_results)
490 class(ax_t),
intent(in) :: ax
491 type(field_t),
intent(inout) :: x
492 type(field_t),
intent(inout) :: y
493 type(field_t),
intent(inout) :: z
494 integer,
intent(in) :: n
495 real(kind=rp),
dimension(n),
intent(in) :: fx
496 real(kind=rp),
dimension(n),
intent(in) :: fy
497 real(kind=rp),
dimension(n),
intent(in) :: fz
498 type(coef_t),
intent(inout) :: coef
499 type(bc_list_t),
intent(inout) :: blstx
500 type(bc_list_t),
intent(inout) :: blsty
501 type(bc_list_t),
intent(inout) :: blstz
502 type(gs_t),
intent(inout) :: gs_h
503 type(ksp_monitor_t),
dimension(3) :: ksp_results
504 integer,
optional,
intent(in) :: niter
506 ksp_results(1) = this%solve(ax, x, fx, n, coef, blstx, gs_h, niter)
507 ksp_results(2) = this%solve(ax, y, fy, n, coef, blsty, gs_h, niter)
508 ksp_results(3) = this%solve(ax, z, fz, n, coef, blstz, gs_h, niter)