Skip to content

Adding (some) pure functions and subroutines #840

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

Merged
merged 28 commits into from
Jun 2, 2025

Conversation

prathi-wind
Copy link
Collaborator

@prathi-wind prathi-wind commented May 19, 2025

Description

I have marked functions and subroutines as pure and elemental to allow for easier compiler parallelization and better code practice.

Fixes #712

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code cleanup

Scope

  • This PR comprises a set of related changes with a common goal

If you cannot check the above box, please split your PR into multiple PRs that each have a common goal.

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.
Provide instructions so we can reproduce.
Please also list any relevant details for your test configuration

  • Test A
  • Test B

Test Configuration:

  • What computers and compilers did you use to test this:

Checklist

  • I have added comments for the new code
  • I added Doxygen docstrings to the new code
  • I have made corresponding changes to the documentation (docs/)
  • I have added regression tests to the test suite so that people can verify in the future that the feature is behaving as expected
  • I have added example cases in examples/ that demonstrate my new feature performing as expected.
    They run to completion and demonstrate "interesting physics"
  • I ran ./mfc.sh format before committing my code
  • New and existing tests pass locally with my changes, including with GPU capability enabled (both NVIDIA hardware with NVHPC compilers and AMD hardware with CRAY compilers) and disabled
  • This PR does not introduce any repeated code (it follows the DRY principle)
  • I cannot think of a way to condense this code and reduce any introduced additional line count

If your code changes any code source files (anything in src/simulation)

To make sure the code is performing as expected on GPU devices, I have:

  • Checked that the code compiles using NVHPC compilers
  • Checked that the code compiles using CRAY compilers
  • Ran the code on either V100, A100, or H100 GPUs and ensured the new feature performed as expected (the GPU results match the CPU results)
  • Ran the code on MI200+ GPUs and ensure the new features performed as expected (the GPU results match the CPU results)
  • Enclosed the new feature via nvtx ranges so that they can be identified in profiles
  • Ran a Nsight Systems profile using ./mfc.sh run XXXX --gpu -t simulation --nsys, and have attached the output file (.nsys-rep) and plain text results to this PR
  • Ran an Omniperf profile using ./mfc.sh run XXXX --gpu -t simulation --omniperf, and have attached the output file and plain text results to this PR.
  • Ran my code using various numbers of different GPUs (1, 2, and 8, for example) in parallel and made sure that the results scale similarly to what happens if you run without the new code/feature

@sbryngelson
Copy link
Member

@prathi-wind FYI you're creating more merge conflicts. merge with base branch before continuing (I recommend)

Copy link

codecov bot commented May 21, 2025

Codecov Report

Attention: Patch coverage is 77.84314% with 113 lines in your changes missing coverage. Please review.

Project coverage is 43.47%. Comparing base (6aba752) to head (c41a530).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/common/m_helper.fpp 34.48% 19 Missing ⚠️
src/pre_process/m_model.fpp 45.83% 13 Missing ⚠️
src/pre_process/m_check_patches.fpp 66.66% 8 Missing ⚠️
src/simulation/m_bubbles.fpp 72.41% 8 Missing ⚠️
src/common/m_mpi_common.fpp 56.25% 7 Missing ⚠️
src/post_process/m_data_output.fpp 50.00% 7 Missing ⚠️
src/post_process/m_mpi_proxy.fpp 54.54% 5 Missing ⚠️
src/common/m_compile_specific.f90 33.33% 4 Missing ⚠️
src/post_process/m_derived_variables.fpp 60.00% 4 Missing ⚠️
src/simulation/m_bubbles_EL.fpp 80.00% 4 Missing ⚠️
... and 19 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #840      +/-   ##
==========================================
- Coverage   43.48%   43.47%   -0.02%     
==========================================
  Files          68       68              
  Lines       19778    19766      -12     
  Branches     2377     2375       -2     
==========================================
- Hits         8601     8593       -8     
+ Misses       9729     9726       -3     
+ Partials     1448     1447       -1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@prathi-wind prathi-wind marked this pull request as ready for review May 22, 2025 01:36
@prathi-wind prathi-wind requested a review from a team as a code owner May 22, 2025 01:36
@sbryngelson sbryngelson changed the title Adding pure functions and subroutines Adding (some) pure functions and subroutines May 22, 2025
Copy link
Collaborator

@abbotts abbotts left a comment

Choose a reason for hiding this comment

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

Most of this looks good. I suggested one additional routine that could be made elemental in addition to pure.

There's one set of changes I don't understand, @prathi-wind . You touched a lot of code in m_bubbles.fpp to pass global parameters in as intent(in) arguments. Was this necessary? Since they're all intent(in) the original code should have never written them, so there should have been no side effects inhibiting the pure. Did a compiler refuse to accept the pure elemental without this?

@@ -54,7 +54,7 @@ end function f_is_default

!> Checks if ALL elements of a real(wp) array are of default value.
!! @param var_array Array to check.
logical function f_all_default(var_array) result(res)
logical pure function f_all_default(var_array) result(res)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now that f_is_default is elemental you could rewrite the implementation of f_all_default to take advantage of that using all(f_is_default(var_array)) instead of the loop.

Not necessary, but could serve as a bell weather function for how well a given compiler is going to support elemental.

@@ -532,7 +532,7 @@ contains
!! @param ray Ray.
!! @param triangle Triangle.
!! @return True if the ray intersects the triangle, false otherwise.
function f_intersects_triangle(ray, triangle) result(intersects)
pure function f_intersects_triangle(ray, triangle) result(intersects)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be made elemental, but I don't know if it would ever be used that way.

Copy link
Member

Choose a reason for hiding this comment

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

well, i'm curious to see if it works if it could!

Comment on lines 52 to 53
real(wp), intent(in) :: gam_l, Ca_l, Web_l, Re_inv_l
logical, intent(in) :: bubbles_euler_l, polytropic_l
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was adding these as dummy arguments necessary for the compiler to accept this as a pure elemental procedure? Since these can be made intent(in) they must not have been modified prior to the refactoring, and so the routine should have had no side effects.

Copy link
Member

Choose a reason for hiding this comment

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

see my comment on slack:
i’m curious what you think is a good strategy here. i think was the one who recommended passing in globals as parameters to a subroutine. i’m not sure if this is required to for a subroutine to be pure or not. it does seem that if they aren’t passed with intent(in) then they could be changed within the routine and thus cause side effects? for what it’s worth - this is partially an effort to use fewer module globals and make subroutines more self-contained, but maybe that’s orthogonal to making things pure or elemental? thoughts?

Comment on lines 98 to 99
real(wp), intent(in) :: gam_l, Ca_l, Web_l, Re_inv_l
logical, intent(in) :: polytropic_l
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to above, since these are intent(in) why was the refactor necessary to make it pure?

Comment on lines 198 to 199
real(wp), intent(in) :: gam_l, Ca_l, Web_l, Re_inv_l
logical, intent(in) :: polytropic_l
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to above, since these are intent(in) why was the refactor necessary to make it pure?

!$acc routine seq
real(wp), intent(in) :: fCpbw, fR, fV, fH, fHdot
real(wp), intent(in) :: fcgas, fntait, fBtait
real(wp), intent(in) :: Re_inv_l
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to above, since these are intent(in) why was the refactor necessary to make it pure?

Comment on lines 288 to 289
real(wp), intent(in) :: gam_l, Ca_l, Web_l, Re_inv_l
logical, intent(in) :: polytropic_l
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to above, since these are intent(in) why was the refactor necessary to make it pure?

Comment on lines 324 to 325
real(wp), intent(in) :: gam_l, Ca_l, Web_l, Re_inv_l
logical, intent(in) :: polytropic_l
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to above, since these are intent(in) why was the refactor necessary to make it pure?

Comment on lines 475 to 480
real(wp), intent(in) :: Tw_l, R_v_l, pv_l
real(wp), dimension(:), allocatable, intent(in) :: pb0_l, mass_n0_l, mass_v0_l, Pe_T_l
real(wp), dimension(:), allocatable, intent(in) :: Re_trans_T_l
real(wp), intent(in) :: k_mw_l
integer, intent(in) :: thermal_l
logical, intent(in) :: bubbles_lagrange_l
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to above, since these are intent(in) why was the refactor necessary to make it pure?

Copy link
Collaborator

@abbotts abbotts left a comment

Choose a reason for hiding this comment

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

This all looks good to me! There's enough changes that I need to trust the tested compilers are doing the work to verify pure and elemental, but I spot-checked a dozen or so an they all look right to me.
I also looked at the few more substantial code changes and they look good.

@sbryngelson
Copy link
Member

This all looks good to me! There's enough changes that I need to trust the tested compilers are doing the work to verify pure and elemental, but I spot-checked a dozen or so an they all look right to me. I also looked at the few more substantial code changes and they look good.

Much appreciated! Will merge.

@sbryngelson sbryngelson merged commit 2f8eef1 into MFlowCode:master Jun 2, 2025
29 checks passed
@prathi-wind prathi-wind deleted the master branch June 2, 2025 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Mark pure subroutines and functions as pure where possible
3 participants