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

Replace format labels with strings #253

Merged
merged 42 commits into from
Feb 3, 2023
Merged

Replace format labels with strings #253

merged 42 commits into from
Feb 3, 2023

Conversation

sseraj
Copy link
Collaborator

@sseraj sseraj commented Dec 16, 2022

Purpose

This PR replaces all format labels with strings. This is necessary to get idempotent formatting with fprettify (that is, the formatting will not change when fprettify is run multiple times in a row on the same code). This is a separate PR from #229 because these changes are manual whereas the fprettify changes will be automated.

If I set the format strings correctly, the output should look the same as before. I checked that this is true for common outputs, like load balancing, cell count, NK switch, adjoint monitor, and negative volumes.

Expected time until merged

One week. This is blocking #229 so it is a bit urgent.

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (non-backwards-compatible fix or feature)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Documentation update
  • Maintenance update
  • Other (please describe)

Testing

Test coverage for output is non-existent, so it would be great if reviewers could run this code on their regular cases and check that the output looks the same.

Checklist

  • I have run flake8 and black to make sure the Python code adheres to PEP-8 and is consistently formatted
  • I have formatted the Fortran code with fprettify or C/C++ code with clang-format as applicable
  • I have run unit and regression tests which pass locally with my changes
  • I have added new tests that prove my fix is effective or that my feature works
  • I have added necessary documentation

@sseraj sseraj requested a review from a team as a code owner December 16, 2022 20:31
@codecov
Copy link

codecov bot commented Dec 16, 2022

Codecov Report

Merging #253 (d729491) into main (d5fe462) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #253   +/-   ##
=======================================
  Coverage   40.77%   40.77%           
=======================================
  Files          13       13           
  Lines        3882     3882           
=======================================
  Hits         1583     1583           
  Misses       2299     2299           

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@anilyil anilyil left a comment

Choose a reason for hiding this comment

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

Left a few comments, but I am stopping now to give my big picture feedback: I think we can improve the consistency here a bit. I like the module idea, but I am not sure how necessary it is. Primarily because it looks like we don't need to move all formats to the module, but then again, how do we decide some lives in there compared to the files themselves?

I was going to recommend renaming the format module but I think my current recommendation would be to remove the format module and if a format is used more than one time in a file, define it as a variable. If it is used once, just typing it in the write/print statements is fine.

Let me know what you think. Some of my individual comments are related to this.

Finally, just so that we document this here: Tapenade produced non F2008 standard code when the failure check in BCData was ignored in a certain way. I pushed a fix for this in this PR: fbe2224#diff-9c42070cc0184316dd2c54cc31a4b7fdf4d3b89ee7493c148b844c3b42831d86

In the future, as @sseraj recommended to me in private, we may want to either consider using different compiler flags for the tapenade generated code, or tweak tapenade options to generate only F2008 valid code. I am guessing this issue will be automatically addressed when we update the tapenade version.

src/adjoint/adjointAPI.F90 Show resolved Hide resolved

! Write the residual norm to stdout every adjMonStep iterations.

if(mod(n, adjMonStep) ==0 ) then
if( myid==0 ) write(*, 10) n, rnorm
if( myid==0 ) write(*, monitorFormat) n, 'KSP Residual norm', rnorm
Copy link
Contributor

Choose a reason for hiding this comment

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

I also realize this is a way to do this formatting. Why didn't you do this everywhere? E.g. in adjoint API, there are formats that repeat. Maybe lets also change those to variables?

else
write(*,40) "PETSc solver converged after", adjConvIts, &
"iterations."
write(*, "(1X, A, 1X, I5, 1X, A)") "PETSc solver converged after", adjConvIts, "iterations."
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like there's some repeated usage in this file. Any particular reason you did not define a variable for these?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, we when we have repeated patterns we should make this a variable, especially for related operations.

src/modules/format.F90 Outdated Show resolved Hide resolved
@@ -0,0 +1,27 @@
module format
!
! This module contains common format specifiers for printing output.
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the rule to decide if a format will be defined in here compared to the files they are used? If you have a rule, can you please explain it so people can follow it? If not, we can move the declarations here, but that might be unnecessary effort.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would say if the same formatting patterns are spanning multiple subroutines within a module then they should be a module variable in the respective module, and if they are spanning multiple modules, it should be moved into this module. The patterns that are currently here should work for a lot of cases.

Copy link
Collaborator Author

@sseraj sseraj left a comment

Choose a reason for hiding this comment

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

Using a module was originally @eirikurj's idea, so maybe he can comment on my implementation. I put format strings that showed up across different files in the format module, but there ended up being only a few common ones.

I also agree that I was not very consistent in using variables and directly using strings. I would be fine with doing what you suggested with the variables.

src/adjoint/adjointAPI.F90 Show resolved Hide resolved
src/modules/format.F90 Outdated Show resolved Hide resolved
@sseraj sseraj requested review from eirikurj and removed request for joanibal December 19, 2022 16:32
Copy link
Contributor

@eirikurj eirikurj left a comment

Choose a reason for hiding this comment

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

I think it looks good overall, but as @anilyil mentioned, there are perhaps places that could be improved.
On the module discussion, I think we should utilize a module for these very common patterns, instead of having the same pattern or similar patters across files. This makes it easier to change and maintain, e.g., by changing print precision etc.
However, I do think that there is a tradeoff, and we should not necessarily force everything to be a subroutine or a module variable, so just defining the pattern for these one-off print/write cases should also be fine.

src/modules/format.F90 Outdated Show resolved Hide resolved
src/modules/format.F90 Outdated Show resolved Hide resolved
@@ -0,0 +1,27 @@
module format
!
! This module contains common format specifiers for printing output.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would say if the same formatting patterns are spanning multiple subroutines within a module then they should be a module variable in the respective module, and if they are spanning multiple modules, it should be moved into this module. The patterns that are currently here should work for a lot of cases.

else
write(*,40) "PETSc solver converged after", adjConvIts, &
"iterations."
write(*, "(1X, A, 1X, I5, 1X, A)") "PETSc solver converged after", adjConvIts, "iterations."
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, we when we have repeated patterns we should make this a variable, especially for related operations.

@sseraj
Copy link
Collaborator Author

sseraj commented Dec 20, 2022

Thanks for the feedback. I will make changes to specify the format patterns more consistently using the following convention:

  • commonFormats module variable: formats spanning multiple modules
  • module variable: formats spanning multiple subroutines within a module
  • subroutine variable: formats that are repeated within a subroutine
  • string: one-off cases

@sseraj
Copy link
Collaborator Author

sseraj commented Dec 21, 2022

I think I addressed all the comments. One exception is that I kept instances of "(A)" as strings because this is the most basic format and appears all over the code.

I also checked the outputs again after the changes.

Copy link
Contributor

@eirikurj eirikurj left a comment

Choose a reason for hiding this comment

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

Did another quick pass. Left a few very minor comments.

@@ -1,5 +1,10 @@
module adjointAPI

use constants
Copy link
Contributor

Choose a reason for hiding this comment

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

Since its imported here at the top, we could remove the same statement from the subroutines.
Also, if its not too difficult, it would be great to use only, although its less important for this module.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

src/bcdata/BCData.F90 Show resolved Hide resolved
src/output/tecplotIO.F90 Outdated Show resolved Hide resolved
src/partitioning/gridChecking.F90 Show resolved Hide resolved
src/partitioning/loadBalance.F90 Outdated Show resolved Hide resolved
src/utils/utils.F90 Outdated Show resolved Hide resolved
@sseraj
Copy link
Collaborator Author

sseraj commented Jan 17, 2023

Thanks for the comments @eirikurj. I will address them by the end of the week.

Copy link
Contributor

@eirikurj eirikurj left a comment

Choose a reason for hiding this comment

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

Looks good!

@anilyil anilyil merged commit 09d2ffd into main Feb 3, 2023
@anilyil anilyil deleted the format-string branch February 3, 2023 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants