-
Notifications
You must be signed in to change notification settings - Fork 5
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
Make swap_spaces
not allocating and more GPU compatible
#249
Conversation
swap_spaces
not allocatingswap_spaces
as not allocating
8cc5767
to
26aa4a6
Compare
ab0213d
to
528ec61
Compare
swap_spaces
as not allocatingswap_spaces
not allocating and GPU compatible
8e488c8
to
213bd2a
Compare
swap_spaces
not allocating and GPU compatibleswap_spaces
not allocating and more GPU compatible
Hi @valeriabarra, To clarify, Calling
ClimaCoupler.jl/src/Utilities.jl Line 73 in b50046f
zeros defined on a ClimaCore space, so it may already be (or can be made on the ClimaCore-side) GPU-compatible-- but it's still allocating. I suggest you try to make swap_space! a 3-argument function, and pass field_out as an argument (and then make required changes to use this new 3-argument function). Doing so will allow us to push field_out outside of any loops and avoid repeated allocations.
|
Thank you so much for the thorough suggestion. It was previously suggested to me to use this |
02acd0d
to
b052662
Compare
Hey @charleskawczynski I applied the suggestions. I made Can you please confirm that what I implemented aligns with what you had in mind? Or clarify otherwise? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @valeriabarra . I left a comment on passing a boundary_field
like we do with boundary_space
right now. Hopefully that'll help with allocations. It would be good to demonstrate on a minimal working example (MWE) the approaches you tried to tackle this problem (as far as I know there are 1. the flag turning off the instance error, 2. creating a field dynamically as the swap_space is being called, and 3. creating a dummy field similar to the boundary_space
as mentioned above). Since we are kind of blocked by this long-standing issue of mismatching ClimaCore space instances, we may need to defer this issue (hopefully it will be solved soon, now that it's tracked by CliMA/ClimaCore.jl#1120). That said, it would be good to justify this by summarising your findings using the MWE.
the temperature of the atmosphere at the lowest level, and the heigh | ||
of the lowest level. | ||
""" | ||
function set_ρ_sfc!(ρ_sfc, T_S, integrator) | ||
ts = integrator.p.ᶜts | ||
thermo_params = CAP.thermodynamics_params(integrator.p.params) | ||
ts_int = Spaces.level(ts, 1) | ||
parent(ρ_sfc) .= parent(ρ_sfc_at_point.(thermo_params, ts_int, swap_space!(T_S, axes(ts_int)))) | ||
parent(ρ_sfc) .= parent(ρ_sfc_at_point.(thermo_params, ts_int, swap_space!(zeros(axes(ts_int)), T_S))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of creating a new field here, can we create it as part of the CouplerSimulation
struct? Similar to how we treat boundary_space
. @charleskawczynski is this what you meant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Here is the MWE gist where I compare all the different approaches. Creating it as part of the CoupledSimulation
struct is fine, but we need to be careful to create a temp copy, wherever we have repeated usages, such as in these conservation checks. I can proceed with the proposed changes (Case 4 in the MWE gist) if you agree that's the best option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually seems like Case 2 in the gist is the best option for our purposes, so I will leave the current version of the code. I am going to squash/rebase and you're good to merge. Thanks for the review!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Output of the gist at first run:
[ Info: Case 1:
alloc_u_ocn = 19493429
u_ocn = Float64-valued Field:
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 … 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
alloc_u_ice = 0
u_ice = Float64-valued Field:
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 … 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
[ Info: Case 2:
alloc_u_ocn = 12800
u_ocn = Float64-valued Field:
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 … 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
alloc_u_ice = 12800
u_ice = Float64-valued Field:
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 … 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
[ Info: Case 3:
alloc_u_ocn = 33705
u_ocn = Float64-valued Field:
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 … 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
alloc_u_ice = 1008
u_ice = Float64-valued Field:
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 … 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
[ Info: Case 4:
alloc_u_ocn = 465631
u_ocn = Float64-valued Field:
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 … 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
alloc_u_ice = 1008
u_ice = Float64-valued Field:
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 … 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
Float64-valued Field:
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 … 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
b052662
to
7afd5b0
Compare
bors try |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, Valeria. After adjusting the alloc limit, feel free to merge.
Thank you! Just to clarify, the alloc limit adjustment is not due to this PR but to upstream changes. Thanks, will do. |
7afd5b0
to
41de7df
Compare
bors r+ |
249: Make `swap_spaces` not allocating and more GPU compatible r=valeriabarra a=valeriabarra ## Purpose The purspose of this PR is to improve `swap_space` so that it is not allocating and to make it GPU compatible. Closes #248 ## To-do - [x] ~~Remove internal call to ClimaCore `parent`~~ This cannot be done internally in the function (see error: `ERROR: LoadError: syntax: Global method definition around .... needs to be placed at the top level, or use "eval".` because of the global method table). So it needs to be worked around wherever it's used. - [x] Make it ``@inline`?` ## Content We remove the internal usage of ClimaCore `parent` by using ClimaCore's `Fields.allow_mismatched_diagonalized_spaces()` function as a temporary workaround, in the absence of a better ClimaCore's interface Review checklist I have: - followed the codebase contribution guide: https://clima.github.io/ClimateMachine.jl/latest/Contributing/ - followed the style guide: https://clima.github.io/ClimateMachine.jl/latest/DevDocs/CodeStyle/ - followed the documentation policy: https://github.com/CliMA/policies/wiki/Documentation-Policy - checked that this PR does not duplicate an open PR. In the Content, I have included - relevant unit tests, and integration tests, - appropriate docstrings on all functions, structs, and modules, and included relevant documentation. - [x] I have read and checked the items on the review checklist. Co-authored-by: Valeria Barra <[email protected]>
tryBuild failed: |
Build failed: |
bors r+ |
249: Make `swap_spaces` not allocating and more GPU compatible r=valeriabarra a=valeriabarra ## Purpose The purspose of this PR is to improve `swap_space` so that it is not allocating and to make it GPU compatible. Closes #248 ## To-do - [x] ~~Remove internal call to ClimaCore `parent`~~ This cannot be done internally in the function (see error: `ERROR: LoadError: syntax: Global method definition around .... needs to be placed at the top level, or use "eval".` because of the global method table). So it needs to be worked around wherever it's used. - [x] Make it ``@inline`?` ## Content We remove the internal usage of ClimaCore `parent` by using ClimaCore's `Fields.allow_mismatched_diagonalized_spaces()` function as a temporary workaround, in the absence of a better ClimaCore's interface Review checklist I have: - followed the codebase contribution guide: https://clima.github.io/ClimateMachine.jl/latest/Contributing/ - followed the style guide: https://clima.github.io/ClimateMachine.jl/latest/DevDocs/CodeStyle/ - followed the documentation policy: https://github.com/CliMA/policies/wiki/Documentation-Policy - checked that this PR does not duplicate an open PR. In the Content, I have included - relevant unit tests, and integration tests, - appropriate docstrings on all functions, structs, and modules, and included relevant documentation. - [x] I have read and checked the items on the review checklist. Co-authored-by: Valeria Barra <[email protected]>
Build failed: |
41de7df
to
8ac167d
Compare
bors r+ |
Purpose
The purspose of this PR is to improve
swap_space
so that it is not allocating and to make it GPU compatible.Closes #248
To-do
Remove internal call to ClimaCoreThis cannot be done internally in the function (see error:parent
ERROR: LoadError: syntax: Global method definition around .... needs to be placed at the top level, or use "eval".
because of the global method table). So it needs to be worked around wherever it's used.@inline
?Content
We remove the internal usage of ClimaCore
parent
by using ClimaCore'sFields.allow_mismatched_diagonalized_spaces()
function as a temporary workaround, in the absence of a better ClimaCore's interfaceReview checklist
I have:
In the Content, I have included
relevant unit tests, and integration tests,
appropriate docstrings on all functions, structs, and modules, and included relevant documentation.
I have read and checked the items on the review checklist.