ocean_budgets_register_contributor Subroutine

private subroutine ocean_budgets_register_contributor(this, name, quantity, per_cell, device_resident)

Register a per-kernel contributor: store a non-owning pointer to the kernel-owned per_cell + metadata. Set device_resident when per_cell is GPU-mapped (drain syncs it). Idempotent on name (re-registering updates the pointer, resets the integral).

Type Bound

ocean_budgets_t

Arguments

Type IntentOptional Attributes Name
class(ocean_budgets_t), intent(inout) :: this
character(len=*), intent(in) :: name
integer, intent(in) :: quantity
real(kind=wp), intent(in), target :: per_cell(:,:,:)
logical, intent(in), optional :: device_resident

Variables

Type Visibility Attributes Name Initial
integer, private :: i
integer, private :: slot
type(budget_contributor_t), private, allocatable :: tmp(:)

Source Code

   subroutine ocean_budgets_register_contributor(this, name, quantity, per_cell, device_resident)
      !! Register a per-kernel contributor: store a non-owning pointer to
      !! the kernel-owned `per_cell` + metadata.  Set `device_resident`
      !! when `per_cell` is GPU-mapped (drain syncs it).  Idempotent on
      !! `name` (re-registering updates the pointer, resets the integral).
      class(ocean_budgets_t), intent(inout) :: this
      character(len=*), intent(in) :: name
      integer, intent(in) :: quantity
      real(wp), intent(in), target :: per_cell(:, :, :)
      logical, intent(in), optional :: device_resident
      type(budget_contributor_t), allocatable :: tmp(:)
      integer :: i, slot

      if (.not. this%is_init) return

      slot = 0
      do i = 1, this%n_contributors
         if (trim(this%contributors(i)%name) == trim(name)) then
            slot = i
            exit
         end if
      end do

      if (slot == 0) then
         if (this%n_contributors == this%n_contributors_max) then
            allocate (tmp(2*this%n_contributors_max))
            do i = 1, this%n_contributors
               tmp(i) = this%contributors(i)
            end do
            call move_alloc(tmp, this%contributors)
            this%n_contributors_max = size(this%contributors)
         end if
         this%n_contributors = this%n_contributors + 1
         slot = this%n_contributors
      end if

      associate (c => this%contributors(slot))
         c%name = name
         c%quantity = quantity
         c%per_cell => per_cell
         c%total_integrated = 0.0_wp
         c%is_active = .true.
         c%device_resident = .false.
         if (present(device_resident)) c%device_resident = device_resident
      end associate
   end subroutine ocean_budgets_register_contributor