Repository navigation
Collapse the duplicated strain-rate routines onto one implementation - #1166
TillRasmussen wants to merge 11 commits into
Conversation
Three routines held the same expressions. strain_rates computed the divergence, tension, shear and Delta at the four corners of a T cell. strain_rates_Deltapp recomputed every one of them, discarded all but the strain rates, and returned Delta_G instead. strain_rates_1d in ice_dyn_core1d was the same arithmetic again with its stencil named from a different origin -- and it had already drifted: it never gained Delta_G, so the 1-D path and the block path had stopped agreeing about what a strain rate routine returns. The arithmetic now lives once, in strain_rates_scalar, which takes the fourteen numbers the stencil actually reads rather than the arrays they sit in. That is what lets one routine serve every caller: a block caller indexes at the call site, and the gathered 1-D path passes its own neighbours. strain_rates becomes a thin wrapper that pulls the stencil out of the arrays and defers, so no existing block caller changes. Delta_G is computed unconditionally rather than behind an optional argument. The strain rates it needs are already in registers, so the four square roots cost less than a branch -- and far less than a second routine recomputing twelve expressions to reach them. deformations consequently makes one call where it made two. ice_dyn_core1d keeps its own stencil names; only the call is remapped, cc/ee/se/ne there being 00/W/S/SW here. strain_rates_Tdt, strain_rates_Tdtsd and strain_rates_U are deliberately untouched: they evaluate at the T and U points of the C and CD grids from different stencils, and share nothing with this one but the name. Bit-for-bit against 2d271f5 on vp, evp1d, eap and gx3 -- restart and history files alike. evp1d is the one that matters, being the path that used to run strain_rates_1d.
5a26e89 to
875366a
Compare
| Deltappse, Deltappsw ) | ||
| ! authors: Elizabeth C. Hunke, LANL | ||
|
|
||
| subroutine strain_rates (nx_block , ny_block , & |
There was a problem hiding this comment.
Should this subroutine also be updated to pass scalars and not block data plus i and j? It's only operating on one grid point at a time.
I haven't looked at the code, but is there a reason why these subroutines only operate 1 grid point at a time instead of a block at a time. I know there is benefit to having a gridpoint operation here because it can be reused with 2d and 1d structures, but is there a performance penalty?
There was a problem hiding this comment.
I think that it is because it is in the stress loop and therefore it is already in a loop.
There was a problem hiding this comment.
I have been thinking a bit more on this. Yes we should probably do like stress_1d. and extract scalars outside of strain_rates. That also make the two routines almost identical. (Except for the calculation of the cx styled variables). I need to check vp though as that is a bit different as @eclare108213 points out in her comment.
|
The routine is called within the stress loop . 2 parts:
|
The collapse returned Delta_G from strain_rates_scalar on the grounds that the strain rates it needs are already in registers. That is true, but it put the work in the wrong place: strain_rates is called from the stress function AND from deformations, and only deformations wants Delta_G. The stress path runs ndte times per timestep -- 240 by default -- so four square roots per cell were being computed and discarded 240 times for every one time they were used. Everything Delta_G needs is intent(out) from the call, so deformations forms it from the divergence, tension and shear it already received: Deltappne = sqrt(divune**2 + epp2i*(tensionne**2 + shearne**2)) Nothing is duplicated by this. Delta_FG keeps its single definition inside strain_rates_scalar; Delta_G now has a single definition too, four lines in the one routine that consumes it. The argument list loses four outputs, and ice_dyn_core1d goes back to upstream's handling of epp2i -- imported and shared, unused -- with no Deltapp locals and nothing added to its OMP clause. deformations still makes ONE call where it made two, which was the point of the collapse: strain_rates_Deltapp used to recompute all twelve strain-rate expressions to reach Delta_G. ⚠ deformations recomputes the strain rates rather than reusing the stress loop's, and that is necessary, not wasteful: within a subcycle stress runs before stepu, so the last stress call's strain rates belong to the velocities BEFORE the final update. deformations runs on the final uvel/vvel, which no stress call ever saw. VP and EAP call it with no such loop at all. Bit-for-bit against 875366a on evp1d, evpB, vp and eap -- restart files and diagnostic logs alike -- and evp1d builds and runs clean under ifx as well as gfortran.
|
I realized that my first solution was more complex than needed. All scalars needed to calculate the new parameters in strain_rates_dapp (upstream in deformations.) Therefore I have "killed" the strain_rates_dAPP routine and replaced with 4 lines in deformations. The 2D and the 1D strain_rates are still collapsed into one routine. This only required the small wrapper that extracted velocities and grid parameters. It is currently testing but it passed on my local machine (limited number of test). |
eclare108213
left a comment
There was a problem hiding this comment.
It makes sense to combine subroutines that do essentially the same thing, and I do not have any major quibbles with the approach in this PR, just comments.
That said, I do not understand why VP calls strain_rates multiple times and only uses some of the output variables each time, which don't appear to overlap, plus there are more than one of these calls from within anderson_solver. In more detail: strain_rates is called from calc_zeta_dPr, matvec and stress_vp, and strain rates are also calculated independently in formDiag_step1 without calling the subroutine strain_rates. Of those four calls, three of them are in anderson_solver. Maybe there are some subtleties that I'm missing, but this seems like a lot of extra unused calculations. Would it make sense to refactor strain_rates so that already-computed values can be reused in VP? Would this cause efficiency loss for EVP (1D or 2D)?
| @@ -2112,108 +2109,113 @@ end subroutine deformationsC_T | |||
| ! | |||
| ! 2019: subroutine created by Philippe Blain, ECCC | |||
There was a problem hiding this comment.
This line can be removed or else moved into the author list for the subroutine below
| uvel00, uvelW, uvelS, uvelSW , & ! x-velocity, this corner's 2x2 | ||
| vvel00, vvelW, vvelS, vvelSW , & ! y-velocity, likewise | ||
| dxT, dyT , & ! T-cell width and height (m) | ||
| cxp, cyp, cxm, cym ! metric terms |
There was a problem hiding this comment.
I prefer the more detailed comments as before, but update for new variables, e.g.
uvel00, & ! uvel(i,j), x-component of velocity at northeast corner (m/s)
uvelW , & ! uvel(i-1,j), x-component of velocity at northwest corner (m/s)
uvelS , & ! uvel(i,j-1), x-component of velocity at southeast corner (m/s)
uvelSW, & ! uvel(i-1,j-1), x-component of velocity at southwest corner (m/s)
etc. The names here are a little inconsistent with usage elsewhere in the code. I always named things relative to the T-grid. Here, the naming is relative to the U-grid.
There was a problem hiding this comment.
So uvelW would uvelNW?
If we should change names. Should uvel also change name uvelNE to be consistent with the C and CD grids?
…re the kernel formDiag_step1 extracts the Jacobian diagonal by applying the rheology to eight unit basis vectors, so it has no velocity array to index. That is why it carried its own copy of the strain-rate algebra: twenty-nine lines repeating divune/tensionne/shearne verbatim, with the array lookups replaced by the basis scalars. strain_rates_scalar takes those components as scalars, so the copy is no longer needed and the call replaces it. The obstacle was Delta. formDiag_step1 never forms one -- it has no Delta locals at all -- and would have paid four square roots per corner per basis direction for a result it discards, thirty-two per cell. So Delta leaves the kernel. strain_rates_scalar returns divergence, tension and shear; the two callers that need Delta form it immediately after, from the intent(out) values they already have. That is what deformations does with Delta_G, for the reason its comment gives: the stress path runs ndte times a step and should not carry a quantity only a colder caller wants. Two shapes were tried and rejected. Optional Delta arguments put a present() test and hidden presence flags in a kernel that ice_dyn_core1d calls per cell per subcycle. A lean inner routine wrapped by the existing one left a real call instruction at -O2 -- gfortran did not inline it -- so the hot path would have paid a call per cell. Forming Delta at the use site costs neither. Bit-for-bit against upstream d92f18a on gfortran and ifx: vp, eap, evp1d, evpCD, histall and hist3d, history output identical in every one.
|
@eclare108213 The reason why there are
In principle at least the two in the Anderson solver could be replaced. I have not fully checked. There is a but though and at least the routines calc_zeta_dPr and matvec work on arrays. This means to transfer the values it should be changed to arrays. I am not to keen on doing that as this adds extra memory usage to the evp. I have changed the calls there to use the strain_rates_scalar as the deltas are used in some places and others not. They are not used here. I have also changed the "strain_rate dublicate code" to a call to strain_rate Updated: So I looked at it. And I think is to split matvec in two. It is already partly split as T and U cell work. |
The comments added while the routines were being collapsed explained the reasoning of the change rather than the code that resulted, which is what the commit messages are for. Remove them and add the authorship lines for deformations and the Deltapp calculation. strain_rates_scalar keeps its subname parameter, matching every other strain_rates variant.
Both called strain_rates, which forms Delta after the strain rates, and neither reads it. In matvec the only mention is inside commented-out code; stress_vp declares the four locals and passes them to nothing. So both paid four square roots per cell for a discarded result. They now call strain_rates_scalar with the corner velocities the wrapper would have passed, and their Delta locals are gone. matvec is where this is worth having: it runs once per nonlinear iteration and again for every Arnoldi step inside fgmres, so the roots were being taken on each application of the operator. stress_vp runs once per step after convergence and is tidied for consistency. calc_zeta_dPr keeps the wrapper -- it feeds Delta to visc_replpress to form zetax2 and etax2, which is what Delta is for. Bit-for-bit against upstream d92f18a on gfortran and ifx: vp, vprepro, eap, evp1d, evpB, evpCD, histall and hist3d.
The routine extracts the Jacobian diagonal by applying the rheology to eight unit basis vectors. It did that with do cc = 1, 8 around the whole cell loop, where each cc set one of eight velocity components to c1 and the rest to c0. Two branches therefore sat inside the cell loop: the one-hot selection, and the eight-way choice of which Drheo term to write. Unrolled into eight terms, each with its own cell loop. The one-hot values are substituted as c1/c0 literals and the neighbour offset is written into the index (i = iu, or i = iu + 1), so both per-cell branches are gone. The strain-rate algebra is taken verbatim from strain_rates_scalar with its dummy names mapped onto the one-hot scalars -- a pure substitution, with no arithmetic rewritten by hand. The arithmetic is therefore unchanged: the same expressions are evaluated the same number of times, eight blocks running once each instead of one block running eight times. Note that gfortran does NOT fold the c0 and c1 products at -O2 or -O3: under IEEE rules x*0.0 is not 0.0 when x is Inf or NaN, and a + 0.0 is not a when a is -0.0. Only -ffast-math folds them, which CICE cannot use. Removing that arithmetic is the next commit's business, not this one's. formDiag_step1 no longer calls strain_rates_scalar; the other three call sites in this file keep it.
With the velocity one-hot, each strain-rate expression has at most one
surviving term, and that term is a bare grid metric:
divune = cyp(i,j) tensionne = -cym(i,j)
divunw = dyT(i,j) shearne = -cxm(i,j)
divusw = c0 shearse = dxT(i,j)
Half of them -- 48 of the 96 across the eight terms -- are identically
zero. The previous commit wrote the products out as c1/c0 literals on the
assumption the compiler would fold them, but it does not: gfortran keeps
every multiply and add at -O2 and -O3, because under IEEE rules x*0.0 is
not 0.0 when x is Inf or NaN, and a + 0.0 is not a when a is -0.0. Only
-ffast-math folds them, which CICE cannot use. So the reduction has to be
written down.
It is still not hand algebra: the surviving term and its sign are taken
from strain_rates_scalar's own expressions by substituting the one-hot
values, the same way the previous commit substituted the literals.
Bit-for-bit rests on one assumption, which is why this is a separate
commit: that no grid metric is negative zero. cym and cxm are negations,
so they are -0.0 wherever HTE or HTN are zero, but those appear only where
a later + c0 would have turned -0.0 into +0.0 anyway. cyp, cxp, dxT and
dyT are not negated and would have to be -0.0 themselves, which a length
should not be.
Half the strain rates are identically zero, so half the stresses are too,
and the sums built from them collapse:
ssigps = stressp_3 + stressp_4 -> c0
ssigpe = stressp_1 + stressp_4 -> stressp_1
ssigp1 =(stressp_1 + stressp_3)*p055 -> (stressp_1)*p055
72 statements become c0 outright and 224 lose terms. Across the file the
generated float arithmetic drops from 907 instructions to 707; with the
previous commit, from 1211 to 707. All of it is in formDiag_step1.
The elimination is order-preserving: surviving terms keep their original
order, signs and parentheses, and only the exactly-zero operands are
dropped. Nothing is factored, reassociated or otherwise rearranged, so
the arithmetic that remains is the arithmetic that was there.
Same bit-for-bit assumption as the previous commit: dropping a + c0 is
exact unless the accumulator is negative zero.
After the previous commit each strain rate was a one-line alias for a grid
metric, defined and then read exactly once:
divune = cyp(i,j)
stressp_1 = zetax2(i,j,1)*divune
Substitute the metric into the use and drop the alias:
stressp_1 = zetax2(i,j,1)*cyp(i,j)
stressm_1 = etax2(i,j,1)*(-cym(i,j))
48 uses substituted, 96 definitions removed, and divune..shearsw leave the
declarations. A negated metric is parenthesised rather than folded into
the sign of the product, so the substitution stays textual.
This generates identical code -- 707 float arithmetic instructions before
and after -- because the compiler already eliminated the temporaries. It
is a readability change: what multiplies zetax2 is now visible at the
multiplication instead of eleven lines above it.
Three leftovers from generating the eight terms: The reduction rewrote the right-hand side of a vanished stress as c0 rather than deleting the statement, leaving 72 dead stores -- written and never read, because the sums that used to consume them had already dropped the operand. Terms 1 and 5 perturb the T cell at the U point itself, so i = iu and j = ju were pure aliases. Those two take their indices straight from indxUi/indxUj and write Drheo(i,j,..); the six offset terms keep both pairs, since iu,ju is the U point receiving the contribution and i,j the offset T cell being perturbed. The term banners had been spliced into nonsense by an earlier edit. Generated code is unchanged: 707 float arithmetic instructions and 97 stack spills before and after. The compiler already discarded the dead stores and the aliases.
PR checklist
Merge of strain_rates and strain_rates_1D into strain_rate_scalar.
@TillRasmussen and Claude
ENTER INFORMATION HERE
Three routines held the same expressions. strain_rates computed the divergence, tension, shear and Delta at the four corners of a T cell. strain_rates_Deltapp recomputed every one of them, discarded all but the strain rates, and returned Delta_G instead. strain_rates_1d had drifted away from main after the updates of strain_rates Deltapp. Missing the 4 scalars. The price to pay is that ice_dyn_vp calculates 4 scalars that it does not use. The evp solution on the other hand do not re calculate divergence, tension and shear.
Strain_rates is now a wrapper to strain_rates_scalar.
strain_rates becomes a thin wrapper that pulls the stencil out of the arrays and defers, so no existing block caller changes.