-
Notifications
You must be signed in to change notification settings - Fork 54
Use scratch arrays in CG, coupled CG, and GMRES on the CPU #2754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,6 +43,8 @@ module cg | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use scalar_bc_projector, only : scalar_bc_projector_t | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use vector_bc_projector, only : vector_bc_projector_t, & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| vector_bc_projector_components | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use host_array, only : host_array_t | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use scratch_registry, only : neko_scratch_registry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use math, only : glsc3, abscmp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use comm, only : MPI_EXTRA_PRECISION, MPI_REAL_PRECISION, NEKO_COMM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use mpi_f08, only : MPI_Allreduce, MPI_IN_PLACE, MPI_SUM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -51,23 +53,35 @@ module cg | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| integer, parameter :: CG_P_SPACE = 7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Standard preconditioned conjugate gradient method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> CPU implementation of the preconditioned conjugate gradient method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !! Workspace pointers are associated with host arrays from the scratch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !! registry only for the duration of a solve. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type, public, extends(ksp_t) :: cg_t | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), allocatable :: w(:) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), allocatable :: r(:) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), allocatable :: p(:,:) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), allocatable :: z(:) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), allocatable :: alpha(:) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Operator action \f$w = A p\f$. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), pointer :: w(:) => null() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Residual \f$r = f - A x\f$. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), pointer :: r(:) => null() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Rolling space of search directions \f$p\f$. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), pointer :: p(:,:) => null() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Preconditioned residual \f$z = M^{-1} r\f$. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), pointer :: z(:) => null() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Step lengths associated with the stored search directions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp), pointer :: alpha(:) => null() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contains | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Initialise a CPU PCG solver. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| procedure, pass(this) :: init => cg_init | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Free a CPU PCG solver. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| procedure, pass(this) :: free => cg_free | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Solve a linear system with the CPU PCG method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| procedure, pass(this) :: solve => cg_solve | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Solve three independent systems with the CPU PCG method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| procedure, pass(this) :: solve_coupled => cg_solve_coupled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end type cg_t | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contains | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Initialise a standard PCG solver | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Initialise a CPU PCG solver. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| subroutine cg_init(this, n, max_iter, M, rel_tol, abs_tol, monitor) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class(cg_t), intent(inout), target :: this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| integer, intent(in) :: max_iter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -79,12 +93,6 @@ subroutine cg_init(this, n, max_iter, M, rel_tol, abs_tol, monitor) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call this%free() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allocate(this%w(n)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allocate(this%r(n)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allocate(this%p(n, CG_P_SPACE)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allocate(this%z(n)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allocate(this%alpha(CG_P_SPACE)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (present(M)) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this%M => M | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -108,37 +116,23 @@ subroutine cg_init(this, n, max_iter, M, rel_tol, abs_tol, monitor) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end subroutine cg_init | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Deallocate a standard PCG solver | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Free a CPU PCG solver. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| subroutine cg_free(this) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class(cg_t), intent(inout) :: this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call this%ksp_free() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (allocated(this%w)) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deallocate(this%w) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (allocated(this%r)) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deallocate(this%r) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (allocated(this%p)) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deallocate(this%p) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (allocated(this%z)) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deallocate(this%z) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (allocated(this%alpha)) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deallocate(this%alpha) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nullify(this%w) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nullify(this%r) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nullify(this%p) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nullify(this%z) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nullify(this%alpha) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nullify(this%M) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end subroutine cg_free | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Standard PCG solve | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Solve a linear system with the CPU PCG method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function cg_solve(this, Ax, x, f, n, coef, bc_projector, gs_h, niter) & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result(ksp_results) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class(cg_t), intent(inout) :: this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -154,6 +148,8 @@ function cg_solve(this, Ax, x, f, n, coef, bc_projector, gs_h, niter) & | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| integer :: iter, max_iter, i, j, k, p_cur, p_prev, ierr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp) :: rnorm, rtr, rtz2, rtz1, x_plus(NEKO_BLK_SIZE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real(kind=rp) :: beta, pap, norm_fac, tmp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type(host_array_t), pointer :: w_tmp, r_tmp, p_tmp, z_tmp, alpha_tmp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| integer :: temp_indices(5) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
timofeymukha marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (present(niter)) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_iter = niter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -162,6 +158,23 @@ function cg_solve(this, Ax, x, f, n, coef, bc_projector, gs_h, niter) & | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| norm_fac = 1.0_rp / sqrt(coef%volume) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call neko_scratch_registry%request_host_array(w_tmp, temp_indices(1), & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n, .false.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call neko_scratch_registry%request_host_array(r_tmp, temp_indices(2), & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n, .false.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call neko_scratch_registry%request_host_array(p_tmp, temp_indices(3), & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n * CG_P_SPACE, .false.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call neko_scratch_registry%request_host_array(z_tmp, temp_indices(4), & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n, .false.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call neko_scratch_registry%request_host_array(alpha_tmp, & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| temp_indices(5), CG_P_SPACE, .false.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this%w => w_tmp%x | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this%r => r_tmp%x | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this%p(1:n, 1:CG_P_SPACE) => p_tmp%x | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes me a bit nervous. Can't we add a rank-2 registry as well (@timfelle )?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Array reshaping like this seem to be completely standard in the fortran community, But sure we can easily add another host_array_t to the system. But explicitly for a host implementation the only difference between host_array_t and vector_t is a %x_d=C_NULL_PTR, so I actually don't think the host array thing makes a difference at all since it should never be active in a device build.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So my point is, you should probably just use a matrix_t here.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
standard, and working on compilers are different things, as we know ;) But sure let's give it a try
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but we do it all over the place already, we just do it at subroutine interfaces, with out of bound indexing and such. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this%z => z_tmp%x | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this%alpha => alpha_tmp%x | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| associate(w => this%w, r => this%r, p => this%p, & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| z => this%z, alpha => this%alpha) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+161
to
179
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -185,6 +198,8 @@ function cg_solve(this, Ax, x, f, n, coef, bc_projector, gs_h, niter) & | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ksp_results%iter = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (abscmp(rnorm, 0.0_rp)) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ksp_results%converged = .true. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nullify(this%w, this%r, this%p, this%z, this%alpha) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call neko_scratch_registry%relinquish_host_array(temp_indices) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -254,6 +269,8 @@ function cg_solve(this, Ax, x, f, n, coef, bc_projector, gs_h, niter) & | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end associate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nullify(this%w, this%r, this%p, this%z, this%alpha) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call neko_scratch_registry%relinquish_host_array(temp_indices) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| call this%monitor_stop() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ksp_results%res_final = rnorm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ksp_results%iter = iter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -280,7 +297,7 @@ subroutine second_cg_part(rtr, r, mult, w, alpha, n) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end subroutine second_cg_part | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Standard PCG coupled solve | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !> Solve three independent systems with the CPU PCG method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function cg_solve_coupled(this, Ax, x, y, z, fx, fy, fz, & | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n, coef, bc_projector, gs_h, niter) result(ksp_results) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class(cg_t), intent(inout) :: this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.