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

Utility to remove duplicate arguments for calls and callees #367

Merged
merged 2 commits into from
Oct 11, 2024

Conversation

MichaelSt98
Copy link
Collaborator

Transform/convert e.g.,

 SUBROUTINE driver (nlon, nlev, nb, var)
  USE kernel_mod, ONLY: kernel
  IMPLICIT NONE
  INTEGER, INTENT(IN) :: nlon, nlev, nb
  REAL, INTENT(INOUT) :: var(nlon, nlev, 5, nb)
  INTEGER :: ibl
  INTEGER :: offset
  INTEGER :: some_val
  INTEGER :: loop_start, loop_end
  loop_start = 2
  loop_end = nb
  some_val = 0
  offset = 1
!$omp test
  DO ibl=loop_start,loop_end
    CALL kernel(nlon, nlev, var(:, :, 1, ibl), var(:, :, 1, ibl), var(:, :, 2:5, ibl), offset, loop_start, loop_end, nlev)
    CALL kernel(nlon, nlev, var(:, :, 1, ibl), var(:, :, 1, ibl), var(:, :, 2:5, ibl), offset, loop_start, loop_end, nlev)
  END DO
END SUBROUTINE driver
SUBROUTINE kernel (nlon, nlev, var1, var2, another_var, icend, lstart, lend, kend)
  USE compute_mod, ONLY: compute
  IMPLICIT NONE
  INTEGER, INTENT(IN) :: nlon, nlev, icend, lstart, lend, kend
  REAL, INTENT(INOUT) :: var1(nlon, nlev)
  REAL, INTENT(INOUT) :: var2(nlon, nlev)
  REAL, INTENT(INOUT) :: another_var(nlon, nlev, 4)
  INTEGER :: jk, jl, jt
  var1(:, :) = 0.
  DO jk=1,kend
    DO jl=1,nlon
      var1(jl, jk) = 0.
      var2(jl, jk) = 1.0
      DO jt=1,4
        another_var(jl, jk, jt) = 0.0
      END DO
    END DO
  END DO
  CALL compute(nlon, nlev, var1, var2)
  CALL compute(nlon, nlev, var1, var2)
END SUBROUTINE kernel
SUBROUTINE compute (nlon, nlev, b_var, a_var)
  IMPLICIT NONE
  INTEGER, INTENT(IN) :: nlon, nlev
  REAL, INTENT(INOUT) :: b_var(nlon, nlev)
  REAL, INTENT(INOUT) :: a_var(nlon, nlev)
  b_var(:, :) = 0.
  a_var(:, :) = 1.0
END SUBROUTINE compute

to

SUBROUTINE driver (nlon, nlev, nb, var)
  USE kernel_mod, ONLY: kernel
  IMPLICIT NONE
  INTEGER, INTENT(IN) :: nlon, nlev, nb
  REAL, INTENT(INOUT) :: var(nlon, nlev, 5, nb)
  INTEGER :: ibl
  INTEGER :: offset
  INTEGER :: some_val
  INTEGER :: loop_start, loop_end
  loop_start = 2
  loop_end = nb
  some_val = 0
  offset = 1
!$omp test
  DO ibl=loop_start,loop_end
    CALL kernel(nlon, nlev, var(:, :, 1, ibl), var(:, :, 2:5, ibl), offset, loop_start, loop_end)
    CALL kernel(nlon, nlev, var(:, :, 1, ibl), var(:, :, 2:5, ibl), offset, loop_start, loop_end)
  END DO
END SUBROUTINE driver
SUBROUTINE kernel (nlon, nlev, var, another_var, icend, lstart, lend)
  USE compute_mod, ONLY: compute
  IMPLICIT NONE
  INTEGER, INTENT(IN) :: nlon, nlev, icend, lstart, lend
  REAL, INTENT(INOUT) :: var(nlon, nlev)
  REAL, INTENT(INOUT) :: another_var(nlon, nlev, 4)
  INTEGER :: jk, jl, jt
  var(:, :) = 0.
  DO jk=1,nlev
    DO jl=1,nlon
      var(jl, jk) = 0.
      var(jl, jk) = 1.0
      DO jt=1,4
        another_var(jl, jk, jt) = 0.0
      END DO
    END DO
  END DO
  CALL compute(nlon, nlev, var)
  CALL compute(nlon, nlev, var)
END SUBROUTINE kernel
SUBROUTINE compute (nlon, nlev, var)
  IMPLICIT NONE
  INTEGER, INTENT(IN) :: nlon, nlev
  REAL, INTENT(INOUT) :: var(nlon, nlev)
  var(:, :) = 0.
  var(:, :) = 1.0
END SUBROUTINE compute

Copy link

Documentation for this branch can be viewed at https://sites.ecmwf.int/docs/loki/367/index.html

Copy link

codecov bot commented Aug 28, 2024

Codecov Report

Attention: Patch coverage is 99.32886% with 1 line in your changes missing coverage. Please review.

Project coverage is 95.54%. Comparing base (0c5750b) to head (949bf65).
Report is 36 commits behind head on main.

Files with missing lines Patch % Lines
loki/transformations/routine_signatures.py 98.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #367      +/-   ##
==========================================
+ Coverage   95.52%   95.54%   +0.01%     
==========================================
  Files         186      188       +2     
  Lines       38956    39105     +149     
==========================================
+ Hits        37214    37363     +149     
  Misses       1742     1742              
Flag Coverage Δ
lint_rules 96.39% <ø> (ø)
loki 95.53% <99.32%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@reuterbal reuterbal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, apologies for the appallingly long time it took me to review this!

This is a really nice and useful utility and a prime example of the inter-procedural way of working in Loki. I think the implementation could be improved in a few places to make it more pythonic and I've left comments in the code.

From a functionality point of view, I left a question whether this sufficiently safeguards against accidental name clashes when enabling the rename_common mode.

loki/transformations/utilities.py Outdated Show resolved Hide resolved
loki/transformations/tests/test_utilities.py Outdated Show resolved Hide resolved
loki/transformations/tests/test_utilities.py Outdated Show resolved Hide resolved
loki/transformations/tests/test_utilities.py Outdated Show resolved Hide resolved
loki/transformations/tests/test_utilities.py Outdated Show resolved Hide resolved
loki/transformations/utilities.py Outdated Show resolved Hide resolved
loki/transformations/utilities.py Outdated Show resolved Hide resolved
loki/transformations/utilities.py Outdated Show resolved Hide resolved
loki/transformations/utilities.py Outdated Show resolved Hide resolved
rename_kwarguments(relevant_calls, rename_common_map_routine)


def modify_variable_declarations(routine, remove_symbols=(), rename_symbols=None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This partially re-implements the setter for ProgramUnit,.variables:

def variables(self, variables):

Should we maybe make this functionality a method on ProgramUnit and use it in both cases? (Cc @mlange05 for input)

@MichaelSt98
Copy link
Collaborator Author

@reuterbal all your comments and requested changes should be addressed except for the modify_variable_declarations re-implementing partially the setter in ProgramUnit.variables

Copy link
Collaborator

@reuterbal reuterbal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for implementing the suggested changes. I think this looks ready now!

@reuterbal reuterbal added the ready for merge This PR has been approved and is ready to be merged label Oct 8, 2024
@reuterbal reuterbal merged commit 1d95896 into main Oct 11, 2024
13 checks passed
@reuterbal reuterbal deleted the nams-remove-duplicate-args branch October 11, 2024 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for merge This PR has been approved and is ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants