Skip to content

Replace intrinsics in allocate statements #850

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 3 commits into from
Jul 14, 2025

Conversation

prathi-wind
Copy link
Collaborator

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

User description

Description

Replace instrinsics in allocate statements and save them in global parameters

Fixes #803

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)
  • Something else

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 a Rocprof Systems profile using ./mfc.sh run XXXX --gpu -t simulation --rsys --hip-trace, 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

PR Type

Enhancement


Description

  • Replace intrinsic functions in allocate statements with global parameters

  • Add cell bounds structure to store min/max values

  • Update allocation calls to use precomputed bounds

  • Improve memory allocation efficiency across modules


Changes diagram

flowchart LR
  A["Intrinsic functions in allocate"] --> B["cell_num_bounds structure"]
  B --> C["Global parameters"]
  C --> D["Updated allocate statements"]
  D --> E["Improved allocation efficiency"]
Loading

Changes walkthrough 📝

Relevant files
Enhancement
14 files
m_derived_types.fpp
Add cell_num_bounds type definition                                           
+6/-0     
m_helper_basic.fpp
Add cell bounds update subroutine                                               
+22/-1   
m_mpi_common.fpp
Replace intrinsics with cell bounds variables                       
+4/-2     
m_variables_conversion.fpp
Use Re_size_max instead of maxval intrinsic                           
+1/-1     
m_global_parameters.fpp
Add cells_bounds global variable and initialization           
+5/-0     
m_start_up.f90
Update cell bounds after reading input                                     
+3/-0     
m_global_parameters.fpp
Add cells_bounds variable and initialization                         
+5/-0     
m_start_up.fpp
Update cell bounds after input processing                               
+3/-0     
m_global_parameters.fpp
Add Re_size_max and cells_bounds variables                             
+11/-5   
m_mpi_proxy.fpp
Replace min/max intrinsics with bounds variables                 
+2/-2     
m_pressure_relaxation.fpp
Use Re_size_max for allocation                                                     
+1/-1     
m_riemann_solvers.fpp
Replace maxval with Re_size_max                                                   
+1/-1     
m_start_up.fpp
Update cell bounds after startup                                                 
+2/-0     
m_viscous.fpp
Use Re_size_max for viscous allocation                                     
+1/-1     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @prathi-wind
    Copy link
    Collaborator Author

    Should I make these new global parameters into a derived type like

     type cell_num_bounds
            !> @name Max and min number of cells in a direction of each combination of x-,y-, and z-
            !> @{
            integer :: mn_max, np_max, mp_max, mnp_max
            integer :: mn_min, np_min, mp_min, mnp_min
            !> @}   
    end type cell_num_bounds
    

    Or would you like them to be kept as separate variables?

    @sbryngelson
    Copy link
    Member

    that would be fine i believe

    Copy link

    codecov bot commented May 29, 2025

    Codecov Report

    Attention: Patch coverage is 81.48148% with 5 lines in your changes missing coverage. Please review.

    Project coverage is 43.76%. Comparing base (adcc0dd) to head (50aca4c).
    Report is 4 commits behind head on master.

    Files with missing lines Patch % Lines
    src/common/m_variables_conversion.fpp 0.00% 1 Missing ⚠️
    src/simulation/m_global_parameters.fpp 75.00% 1 Missing ⚠️
    src/simulation/m_pressure_relaxation.fpp 0.00% 1 Missing ⚠️
    src/simulation/m_riemann_solvers.fpp 0.00% 1 Missing ⚠️
    src/simulation/m_viscous.fpp 0.00% 1 Missing ⚠️
    Additional details and impacted files
    @@            Coverage Diff             @@
    ##           master     #850      +/-   ##
    ==========================================
    + Coverage   43.71%   43.76%   +0.05%     
    ==========================================
      Files          68       68              
      Lines       18360    18378      +18     
      Branches     2292     2292              
    ==========================================
    + Hits         8026     8044      +18     
      Misses       8945     8945              
      Partials     1389     1389              

    ☔ 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 30, 2025 18:05
    @prathi-wind prathi-wind requested a review from a team as a code owner May 30, 2025 18:05
    @prathi-wind prathi-wind requested a review from a team as a code owner July 13, 2025 20:44
    @prathi-wind prathi-wind marked this pull request as draft July 13, 2025 20:44
    @prathi-wind prathi-wind marked this pull request as ready for review July 14, 2025 15:39
    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis 🔶

    803 - Partially compliant

    Compliant requirements:

    • Remove intrinsics from allocate statements (specifically max and maxval)
    • Store relevant quantities in variables in m_global_parameters
    • Replace @:ALLOCATE(Res(1:2, 1:maxval(Re_size))) with pre-computed variable

    Non-compliant requirements:

    • Replace the q_cons_buff_send allocate statement example (this specific case was not addressed in the PR)

    Requires further human verification:

    • Verify that all intrinsic usages in allocate statements have been identified and replaced
    • Confirm that the new approach doesn't introduce performance regressions

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Possible Issue

    The subroutine s_update_cell_bounds is declared as pure elemental but has intent(out) parameter. This combination may cause compilation issues or unexpected behavior since elemental procedures with intent(out) parameters have specific constraints.

    pure elemental subroutine s_update_cell_bounds(bounds, m, n, p)
        type(cell_num_bounds), intent(out) :: bounds
    Logic Error

    The replacement uses cells_bounds%mnp_min and cells_bounds%mn_max but cells_bounds may not be properly initialized at this point in the code execution, potentially causing runtime errors or incorrect calculations.

                                 & (cells_bounds%mnp_min + 2*buff_size + 1))
    else
        halo_size = -1 + buff_size*(v_size)* &
                                 & (cells_bounds%mn_max + 2*buff_size + 1)
    Duplicate Code

    The same pattern of calling s_update_cell_bounds(cells_bounds, m, n, p) is repeated across multiple modules (pre_process, post_process, simulation) in similar contexts, suggesting potential for consolidation.

    call s_update_cell_bounds(cells_bounds, m, n, p)
    

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Replace intrinsic with explicit comparison

    The maxval intrinsic should be avoided in allocation contexts as suggested by
    the PR title. Consider using an explicit comparison to determine the maximum
    value between Re_size(1) and Re_size(2).

    src/simulation/m_global_parameters.fpp [1045]

    -Re_size_max = maxval(Re_size)
    +Re_size_max = max(Re_size(1), Re_size(2))
    Suggestion importance[1-10]: 6

    __

    Why: The suggestion correctly proposes replacing maxval(Re_size) with max(Re_size(1), Re_size(2)), which is more explicit and aligns with the PR's goal of removing maxval from allocation contexts.

    Low
    Remove unnecessary elemental attribute

    The elemental attribute is unnecessary and potentially problematic for this
    subroutine since it operates on a derived type with multiple components. Remove
    elemental to avoid potential compilation issues and improve clarity.

    src/common/m_helper_basic.fpp [155-157]

    -pure elemental subroutine s_update_cell_bounds(bounds, m, n, p)
    +pure subroutine s_update_cell_bounds(bounds, m, n, p)
         type(cell_num_bounds), intent(out) :: bounds
         integer, intent(in) :: m, n, p
    Suggestion importance[1-10]: 4

    __

    Why: The suggestion correctly identifies that the elemental attribute is not utilized, as the subroutine is only called with scalar arguments, making its removal a valid code simplification.

    Low
    • More

    @sbryngelson sbryngelson merged commit 293557c into MFlowCode:master Jul 14, 2025
    33 checks passed
    @prathi-wind prathi-wind deleted the alloc branch July 18, 2025 22:29
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Development

    Successfully merging this pull request may close these issues.

    Remove intrinsics from allocate statements
    2 participants