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

NRL single-precision changes #797

Closed
wants to merge 3 commits into from

Conversation

grantfirl
Copy link
Collaborator

@grantfirl grantfirl commented Dec 7, 2021

DRAFT

TODO:

  1. What changes are needed to "activate" single precision at compile time?
  2. These changes only correspond to "CCPP physics suite four". How should this be tested and generalized to other suites?

@grantfirl
Copy link
Collaborator Author

grantfirl commented Dec 7, 2021

Replaces #772 along with #798

@@ -1470,8 +1471,11 @@ SUBROUTINE boulac_length(kts,kte,zw,dz,qtke,theta,lb1,lb2)
dld(iz) = min(dld(iz),zw(iz+1))!not used in PBL anyway, only free atmos
lb1(iz) = min(dlu(iz),dld(iz)) !minimum
!JOE-fight floating point errors
#ifdef SINGLE_PREC
!JM: keep up the fight, JOE
Copy link
Collaborator

Choose a reason for hiding this comment

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

Best comment ever

Copy link
Contributor

Choose a reason for hiding this comment

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

Should the #ifdef SINGLE_PREC at line 1474 and the #endif at line 1478 be removed so that dlu and dld are MIN/MAX'd unconditionally, whether or not the code is being compiled single or double floating point precision? I added the #ifdef/#endif to a version of the code where the two lines they enclose had been commented out. The effect of adding #ifdef/#endif was to reinstate them for SINGLE precision; but could this also be a benefit for double precision too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@joeolson42 Can you confirm that you want these two lines (bounds for dlu/dld) active regardless of precision?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm in favor of keeping both lines regardless of precision.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The latest NCAR main already MIN/MAXes these so the #ifdef SINGLE_PREC should not be there.

@@ -1470,8 +1471,11 @@ SUBROUTINE boulac_length(kts,kte,zw,dz,qtke,theta,lb1,lb2)
dld(iz) = min(dld(iz),zw(iz+1))!not used in PBL anyway, only free atmos
lb1(iz) = min(dlu(iz),dld(iz)) !minimum
!JOE-fight floating point errors
#ifdef SINGLE_PREC
!JM: keep up the fight, JOE
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the #ifdef SINGLE_PREC at line 1474 and the #endif at line 1478 be removed so that dlu and dld are MIN/MAX'd unconditionally, whether or not the code is being compiled single or double floating point precision? I added the #ifdef/#endif to a version of the code where the two lines they enclose had been commented out. The effect of adding #ifdef/#endif was to reinstate them for SINGLE precision; but could this also be a benefit for double precision too?

@@ -1733,6 +1733,10 @@ subroutine rswinit &
tfn = float(i) / float(NTBMX-i)
tau = bpade * tfn
exp_tbl(i) = exp( -tau )
#ifdef SINGLE_PREC
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe do this unconditionally, single or double precision?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@michalakes To be clear, this change was required for single precision, but you're wanting to know whether it is OK to do this regardless of precision? If so, I can ask the codeowner for this file.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that is correct. The chances of exp_tbl(i) equalling zero and causing a div-zero error are significant lower at double precision, but maybe not impossible.
The WRF version of this routine (in module_ra_rrtmg_swf.F) does this unconditionally.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The latest NCAR main already MIN/MAXes these so the #ifdef SINGLE_PREC should not be there.

! real b,qtmp,rate,qc
real,external :: xmytw
! real(kind=kind_phys) b,qtmp,rate,qc
! real(kind=kind_phys),external :: xmytw (now inside the module)
Copy link
Collaborator

Choose a reason for hiding this comment

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

No, xmytw is not in a module; it is inside this file outside of a module. Removing the "external" causes it to be undefined and the code fails to compile.

Copy link
Contributor

@michalakes michalakes May 3, 2022

Choose a reason for hiding this comment

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

It looks like the module declaration in my original pull request (https://github.com/NCAR/ccpp-physics/pull/772/files#) didn't make it into the current fork that you're reviewing. The original PR version of this file can be compared from here:

https://github.com/NCAR/ccpp-physics/pull/772/files#diff-e6c0659a30420d8a82797b25ef6b176c4d2c13241d6272e33bb9f956abb92b8d

The intent of this and a number of other changes to enclose bare external subroutines into modules was to make it easier to debug the code (especially catching type mismatches) by making subroutine interfaces visible, and I'd recommend keeping them.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see you added a lot of "module...end module" in CCPP files that presently use Fortran 77 calling conventions. That seems like a good idea to me, though I'll have to adjust the names slightly to match CCPP's latest file naming standard. (Or rename the files.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was able to get your "module...end module" addition to many files working, though I needed to add "use" statements in more files than you did.

& cdfun2(nlay,ngptsw)
real (kind=kind_dbl_prec) :: rand2d(nlay*ngptsw), rand1d(ngptsw)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The code fails to compile when these are kind_dbl_prec since the Mersenne Twister code expects default real kind.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I suggest we use kind_dbl_prec explicitly in mersenne_twister. It does have more than the 23 bits of mantissa precision one can store in a 32-bit floating-point number; it has 32 bits of mantissa. The bulk of the CCPP code assumes default real kind is real(8), so this is consistent with what the code expects.

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 the mersenne twister code should be modified to use kind_dbl_prec explicitly instead of relying on compiler flags (-r8) to promote the real type.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I have applied that change to my branch.

@SamuelTrahanNOAA
Copy link
Collaborator

I am preparing a replacement for this PR, along with PRs in stochastic-physics, fv3atm, ufs-weather-model, and atmos-cubed-sphere, to get this to work for FV3_RAP in UFS. This will let us include a regression test to ensure UFS development does not break functionality that Neptune requires.

@michalakes
Copy link
Contributor

I am preparing a replacement for this PR, along with PRs in stochastic-physics, fv3atm, ufs-weather-model, and atmos-cubed-sphere, to get this to work for FV3_RAP in UFS. This will let us include a regression test to ensure UFS development does not break functionality that Neptune requires.

Sounds good, Sam. Let me know how I can help or if you need me as a reviewer. Thanks for looking into this.

@SamuelTrahanNOAA
Copy link
Collaborator

Sounds good, Sam. Let me know how I can help or if you need me as a reviewer. Thanks for looking into this.

I think I have what I need for now, but I'll let you know when I need your help again. I certainly will need reviewers for the final product.

@SamuelTrahanNOAA
Copy link
Collaborator

This PR is replaced by these:

#918
NOAA-EMC/fv3atm#532
ufs-community/ufs-weather-model#1206

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.

4 participants