Skip to content
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

Create the provis_state subpool at RK4 initialization to avoid memory leak #87

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 137 additions & 67 deletions components/mpas-framework/src/framework/mpas_pool_routines.F
Original file line number Diff line number Diff line change
Expand Up @@ -932,20 +932,32 @@ end subroutine mpas_pool_clone_pool!}}}
!> copy the data from the members of srcPool into the members of destPool.
!
!-----------------------------------------------------------------------
recursive subroutine mpas_pool_copy_pool(srcPool, destPool)!{{{
recursive subroutine mpas_pool_copy_pool(srcPool, destPool, overrideTimeLevels)!{{{

implicit none

type (mpas_pool_type), pointer :: srcPool
type (mpas_pool_type), pointer :: destPool
integer, intent(in), optional :: overrideTimeLevels


integer :: i, j, threadNum
integer :: timeLevels
type (mpas_pool_member_type), pointer :: ptr
type (mpas_pool_data_type), pointer :: dptr
type (mpas_pool_data_type), pointer :: mem

threadNum = mpas_threading_get_thread_num()
timeLevels = -1

if (present(overrideTimeLevels)) then
timeLevels = overrideTimeLevels

if (timeLevels < 1) then
call mpas_pool_set_error_level(MPAS_POOL_FATAL)
call pool_mesg('ERROR in mpas_pool_copy_pool: Input time levels cannot be less than 1.')
end if
end if

if ( threadNum == 0 ) then
do i=1,srcPool % size
Expand Down Expand Up @@ -985,8 +997,14 @@ recursive subroutine mpas_pool_copy_pool(srcPool, destPool)!{{{

dptr => ptr % data

! Do this through brute force...
mem => pool_get_member(destPool, ptr % key, MPAS_POOL_FIELD)

! Allow for overrideTimeLevels
if (timeLevels == -1) then
timeLevels = mem % contentsTimeLevs
endif

! Do this through brute force...
if (associated(dptr % r0)) then
call mpas_duplicate_field(dptr % r0, mem % r0, copy_array_only=.true.)
else if (associated(dptr % r1)) then
Expand Down Expand Up @@ -1014,83 +1032,135 @@ recursive subroutine mpas_pool_copy_pool(srcPool, destPool)!{{{
else if (associated(dptr % l0)) then
call mpas_duplicate_field(dptr % l0, mem % l0, copy_array_only=.true.)
else if (associated(dptr % r0a)) then
do j=1,mem % contentsTimeLevs
mem % r0 => mem % r0a(j)
call mpas_duplicate_field(dptr % r0a(j), mem % r0, copy_array_only=.true.)
nullify(mem % r0)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % r0 => mem % r0a(j)
call mpas_duplicate_field(dptr % r0a(j), mem % r0, copy_array_only=.true.)
nullify(mem % r0)
end do
else
call mpas_duplicate_field(dptr % r0a(1), mem % r0, copy_array_only=.true.)
end if
else if (associated(dptr % r1a)) then
do j=1,mem % contentsTimeLevs
mem % r1 => mem % r1a(j)
call mpas_duplicate_field(dptr % r1a(j), mem % r1, copy_array_only=.true.)
nullify(mem % r1)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % r1 => mem % r1a(j)
call mpas_duplicate_field(dptr % r1a(j), mem % r1, copy_array_only=.true.)
nullify(mem % r1)
end do
else
call mpas_duplicate_field(dptr % r1a(1), mem % r1, copy_array_only=.true.)
end if
else if (associated(dptr % r2a)) then
do j=1,mem % contentsTimeLevs
mem % r2 => mem % r2a(j)
call mpas_duplicate_field(dptr % r2a(j), mem % r2, copy_array_only=.true.)
nullify(mem % r2)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % r2 => mem % r2a(j)
call mpas_duplicate_field(dptr % r2a(j), mem % r2, copy_array_only=.true.)
nullify(mem % r2)
end do
else
call mpas_duplicate_field(dptr % r2a(1), mem % r2, copy_array_only=.true.)
end if
else if (associated(dptr % r3a)) then
do j=1,mem % contentsTimeLevs
mem % r3 => mem % r3a(j)
call mpas_duplicate_field(dptr % r3a(j), mem % r3, copy_array_only=.true.)
nullify(mem % r3)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % r3 => mem % r3a(j)
call mpas_duplicate_field(dptr % r3a(j), mem % r3, copy_array_only=.true.)
nullify(mem % r3)
end do
else
call mpas_duplicate_field(dptr % r3a(1), mem % r3, copy_array_only=.true.)
end if
else if (associated(dptr % r4a)) then
do j=1,mem % contentsTimeLevs
mem % r4 => mem % r4a(j)
call mpas_duplicate_field(dptr % r4a(j), mem % r4, copy_array_only=.true.)
nullify(mem % r4)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % r4 => mem % r4a(j)
call mpas_duplicate_field(dptr % r4a(j), mem % r4, copy_array_only=.true.)
nullify(mem % r4)
end do
else
call mpas_duplicate_field(dptr % r4a(1), mem % r4, copy_array_only=.true.)
end if
else if (associated(dptr % r5a)) then
do j=1,mem % contentsTimeLevs
mem % r5 => mem % r5a(j)
call mpas_duplicate_field(dptr % r5a(j), mem % r5, copy_array_only=.true.)
nullify(mem % r5)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % r5 => mem % r5a(j)
call mpas_duplicate_field(dptr % r5a(j), mem % r5, copy_array_only=.true.)
nullify(mem % r5)
end do
else
call mpas_duplicate_field(dptr % r5a(1), mem % r5, copy_array_only=.true.)
end if
else if (associated(dptr % i0a)) then
do j=1,mem % contentsTimeLevs
mem % i0 => mem % i0a(j)
call mpas_duplicate_field(dptr % i0a(j), mem % i0, copy_array_only=.true.)
nullify(mem % i0)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % i0 => mem % i0a(j)
call mpas_duplicate_field(dptr % i0a(j), mem % i0, copy_array_only=.true.)
nullify(mem % i0)
end do
else
call mpas_duplicate_field(dptr % i0a(1), mem % i0, copy_array_only=.true.)
end if
else if (associated(dptr % i1a)) then
do j=1,mem % contentsTimeLevs
mem % i1 => mem % i1a(j)
call mpas_duplicate_field(dptr % i1a(j), mem % i1, copy_array_only=.true.)
nullify(mem % i1)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % i1 => mem % i1a(j)
call mpas_duplicate_field(dptr % i1a(j), mem % i1, copy_array_only=.true.)
nullify(mem % i1)
end do
else
call mpas_duplicate_field(dptr % i1a(1), mem % i1, copy_array_only=.true.)
end if
else if (associated(dptr % i2a)) then
do j=1,mem % contentsTimeLevs
mem % i2 => mem % i2a(j)
call mpas_duplicate_field(dptr % i2a(j), mem % i2, copy_array_only=.true.)
nullify(mem % i2)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % i2 => mem % i2a(j)
call mpas_duplicate_field(dptr % i2a(j), mem % i2, copy_array_only=.true.)
nullify(mem % i2)
end do
else
call mpas_duplicate_field(dptr % i2a(1), mem % i2, copy_array_only=.true.)
end if
else if (associated(dptr % i3a)) then
do j=1,mem % contentsTimeLevs
mem % i3 => mem % i3a(j)
call mpas_duplicate_field(dptr % i3a(j), mem % i3, copy_array_only=.true.)
nullify(mem % i3)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % i3 => mem % i3a(j)
call mpas_duplicate_field(dptr % i3a(j), mem % i3, copy_array_only=.true.)
nullify(mem % i3)
end do
else
call mpas_duplicate_field(dptr % i3a(1), mem % i3, copy_array_only=.true.)
end if
else if (associated(dptr % c0a)) then
do j=1,mem % contentsTimeLevs
mem % c0 => mem % c0a(j)
call mpas_duplicate_field(dptr % c0a(j), mem % c0, copy_array_only=.true.)
nullify(mem % c0)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % c0 => mem % c0a(j)
call mpas_duplicate_field(dptr % c0a(j), mem % c0, copy_array_only=.true.)
nullify(mem % c0)
end do
else
call mpas_duplicate_field(dptr % c0a(1), mem % c0, copy_array_only=.true.)
end if
else if (associated(dptr % c1a)) then
do j=1,mem % contentsTimeLevs
mem % c1 => mem % c1a(j)
call mpas_duplicate_field(dptr % c1a(j), mem % c1, copy_array_only=.true.)
nullify(mem % c1)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % c1 => mem % c1a(j)
call mpas_duplicate_field(dptr % c1a(j), mem % c1, copy_array_only=.true.)
nullify(mem % c1)
end do
else
call mpas_duplicate_field(dptr % c1a(1), mem % c1, copy_array_only=.true.)
end if
else if (associated(dptr % l0a)) then
do j=1,mem % contentsTimeLevs
mem % l0 => mem % l0a(j)
call mpas_duplicate_field(dptr % l0a(j), mem % l0, copy_array_only=.true.)
nullify(mem % l0)
end do
if (timeLevels > 1) then
do j=1,timeLevels
mem % l0 => mem % l0a(j)
call mpas_duplicate_field(dptr % l0a(j), mem % l0, copy_array_only=.true.)
nullify(mem % l0)
end do
else
call mpas_duplicate_field(dptr % l0a(1), mem % l0, copy_array_only=.true.)
end if
else
call pool_mesg('While copying pool, member '//trim(ptr % key)//' has no valid field pointers.')
end if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,9 @@ subroutine ocn_time_integrator_rk4(domain, dt)!{{{
call mpas_pool_get_subpool(block % structs, 'mesh', meshPool)
call mpas_pool_get_subpool(block % structs, 'diagnostics', diagnosticsPool)

call mpas_pool_create_pool(provisStatePool)
call mpas_pool_get_subpool(block % structs, 'provis_state', provisStatePool)

call mpas_pool_clone_pool(statePool, provisStatePool, 1)
call mpas_pool_add_subpool(block % structs, 'provis_state', provisStatePool)
call mpas_pool_copy_pool(statePool, provisStatePool, 1)

call mpas_pool_get_dimension(block % dimensions, 'nCells', nCells)
call mpas_pool_get_dimension(block % dimensions, 'nEdges', nEdges)
Expand Down Expand Up @@ -315,37 +314,6 @@ subroutine ocn_time_integrator_rk4(domain, dt)!{{{
block => block % next
end do

block => domain % blocklist
do while(associated(block))
if (associated(block % prev)) then
call mpas_pool_get_subpool(block % prev % structs, 'provis_state', prevProvisPool)
else
nullify(prevProvisPool)
end if

if (associated(block % next)) then
call mpas_pool_get_subpool(block % next % structs, 'provis_state', nextProvisPool)
else
nullify(nextProvisPool)
end if

call mpas_pool_get_subpool(block % structs, 'provis_state', provisStatePool)

if (associated(prevProvisPool) .and. associated(nextProvisPool)) then
call mpas_pool_link_pools(provisStatePool, prevProvisPool, nextProvisPool)
else if (associated(prevProvisPool)) then
call mpas_pool_link_pools(provisStatePool, prevProvisPool)
else if (associated(nextProvisPool)) then
call mpas_pool_link_pools(provisStatePool, nextPool=nextProvisPool)
else
call mpas_pool_link_pools(provisStatePool)
end if

call mpas_pool_link_parinfo(block, provisStatePool)

block => block % next
end do

! Fourth-order Runge-Kutta, solving dy/dt = f(t,y) is typically written as follows
! where h = delta t is the large time step. Here f(t,y) is the right hand side,
! called the tendencies in the code below.
Expand Down Expand Up @@ -853,16 +821,6 @@ subroutine ocn_time_integrator_rk4(domain, dt)!{{{

call mpas_timer_stop("RK4-cleanup phase")

block => domain % blocklist
do while(associated(block))
call mpas_pool_get_subpool(block % structs, 'provis_state', provisStatePool)

call mpas_pool_destroy_pool(provisStatePool)

call mpas_pool_remove_subpool(block % structs, 'provis_state')
block => block % next
end do

end subroutine ocn_time_integrator_rk4!}}}

subroutine ocn_time_integrator_rk4_compute_vel_tends(domain, block, dt, &
Expand Down Expand Up @@ -1731,7 +1689,8 @@ subroutine ocn_time_integration_rk4_init(domain)!{{{
! local variables
!-----------------------------------------------------------------
type (block_type), pointer :: block
type (mpas_pool_type), pointer :: meshPool
type (mpas_pool_type), pointer :: meshPool, statePool, provisStatePool
type (mpas_pool_type), pointer :: nextProvisPool, prevProvisPool
logical, pointer :: config_use_debugTracers
integer, pointer :: nVertLevels
! End preamble
Expand All @@ -1742,12 +1701,43 @@ subroutine ocn_time_integration_rk4_init(domain)!{{{
block => domain % blocklist
do while (associated(block))
call mpas_pool_get_subpool(block % structs, 'mesh', meshPool)
call mpas_pool_get_subpool(block % structs, 'state', statePool)
call mpas_pool_get_dimension(meshPool, 'nVertLevels', nVertLevels)
if (config_use_debugTracers .and. nVertLevels == 1) then
call mpas_log_write('Debug tracers may cause failures in a ' &
// 'single layer case. Consider setting ' &
// 'config_use_debugTracers to .false.', MPAS_LOG_WARN)
endif

call mpas_pool_create_pool(provisStatePool)
call mpas_pool_clone_pool(statePool, provisStatePool, 1)
call mpas_pool_add_subpool(block % structs, 'provis_state', provisStatePool)

if (associated(block % prev)) then
call mpas_pool_get_subpool(block % prev % structs, 'provis_state', prevProvisPool)
else
nullify(prevProvisPool)
end if

if (associated(block % next)) then
call mpas_pool_get_subpool(block % next % structs, 'provis_state', nextProvisPool)
else
nullify(nextProvisPool)
end if

call mpas_pool_get_subpool(block % structs, 'provis_state', provisStatePool)

if (associated(prevProvisPool) .and. associated(nextProvisPool)) then
call mpas_pool_link_pools(provisStatePool, prevProvisPool, nextProvisPool)
else if (associated(prevProvisPool)) then
call mpas_pool_link_pools(provisStatePool, prevProvisPool)
else if (associated(nextProvisPool)) then
call mpas_pool_link_pools(provisStatePool, nextPool=nextProvisPool)
else
call mpas_pool_link_pools(provisStatePool)
end if

call mpas_pool_link_parinfo(block, provisStatePool)
block => block % next
end do

Expand Down