Skip to content

Commit 86bdcf3

Browse files
Merge pull request #2139 from CliMA/ck/fix_missing_debug_call
Add missing `post_op_callback` calls
2 parents fc3c499 + 0c49a12 commit 86bdcf3

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ ClimaCore.jl Release Notes
44
main
55
-------
66

7+
### ![][badge-✨feature/enhancement] A new DebugOnly module can help find where
8+
NaNs/Inf come from. PRs [2115](https://github.com/CliMA/ClimaCore.jl/pull/2115) and
9+
[2139](https://github.com/CliMA/ClimaCore.jl/pull/2139)
10+
11+
A new `ClimaCore.DebugOnly` module was added, which can help users find where
12+
NaNs or Infs come from in a simulation, interactively. Documentation, with a
13+
simple representative example can be found [here](https://clima.github.io/ClimaCore.jl/dev/debugging/#Infiltrating).
14+
715
### ![][badge-✨feature/enhancement] Various improvements to `Remapper` [2060](https://github.com/CliMA/ClimaCore.jl/pull/2060)
816

917
The `ClimaCore.Remapping` module received two improvements. First, `Remapper` is

docs/src/debugging.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,21 @@ import ClimaCore
7878
import Infiltrator # must be in your default environment
7979
ClimaCore.DebugOnly.call_post_op_callback() = true
8080
function ClimaCore.DebugOnly.post_op_callback(result, args...; kwargs...)
81-
if any(isnan, parent(result))
82-
println("NaNs found!")
81+
has_nans = any(isnan, parent(result))
82+
has_inf = any(isinf, parent(result))
83+
if has_nans || has_inf
84+
has_nans && println("NaNs found!")
85+
has_inf && println("Infs found!")
8386
# Let's define the stack trace so that we know where this came from
8487
st = stacktrace()
8588

8689
# Let's use Infiltrator.jl to exfiltrate to drop into the REPL.
8790
# Now, `Infiltrator.safehouse` will be a NamedTuple
88-
# containing `result`, `args` and `kwargs`.
91+
# containing `result`, `args`, `kwargs` and `st`.
8992
Infiltrator.@exfiltrate
93+
# sometimes code execution doesn't stop, I'm not sure why. Let's
94+
# make sure we exfiltrate immediately with the data we want.
95+
error("Exfiltrating.")
9096
end
9197
end
9298

@@ -97,7 +103,7 @@ data = ClimaCore.DataLayouts.VIJFH{FT}(Array{FT}, zeros; Nv=5, Nij=2, Nh=2)
97103
(;result, args, kwargs, st) = Infiltrator.safehouse;
98104

99105
# You can print the stack trace, to see where the NaNs were found:
100-
ClimaCore.DebugOnly.print_depth_limited_stack_trace(st;maxtypedepth=1)
106+
ClimaCore.DebugOnly.print_depth_limited_stack_trace(st; maxtypedepth=1)
101107

102108
# Once there, you can see that the call lead you to `copyto!`,
103109
# Inspecting `args` shows that the `Broadcasted` object used to populate the
@@ -122,3 +128,10 @@ Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}}(identity, (NaN,)
122128
`post_op_callback` is called in many places, so this is a
123129
performance-critical code path and expensive operations performed in
124130
`post_op_callback` may significantly slow down your code.
131+
132+
!!! warn
133+
134+
It is _highly_ recommended to use `post_op_callback` _without_ `@testset`,
135+
as Test.jl may continue running through code execution, until all of the
136+
tests in a given `@testset` are complete, and the result will be that you
137+
will get the _last_ observed instance of `NaN` or `Inf`.

src/CommonSpaces/CommonSpaces.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ space = ExtrudedCubedSphereSpace(;
9191
z_max = 1,
9292
radius = 10,
9393
h_elem = 10,
94-
n_quad_points = 4
94+
n_quad_points = 4,
9595
staggering = CellCenter()
9696
)
9797
```

src/Fields/fieldvector.jl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,14 @@ end
325325
parent(getfield(_values(fv), symb))
326326
@inline transform_broadcasted(x, symb, axes) = x
327327

328-
@inline Base.copyto!(
328+
@inline function Base.copyto!(
329329
dest::FieldVector,
330330
bc::Union{FieldVector, Base.Broadcast.Broadcasted{FieldVectorStyle}},
331-
) = copyto_per_field!(dest, bc)
331+
)
332+
copyto_per_field!(dest, bc)
333+
call_post_op_callback() && post_op_callback(dest, dest, bc)
334+
return dest
335+
end
332336

333337
@inline function copyto_per_field!(
334338
dest::FieldVector,
@@ -351,18 +355,29 @@ end
351355
return dest
352356
end
353357

354-
@inline Base.copyto!(
358+
@inline function Base.copyto!(
355359
dest::FieldVector,
356360
bc::Base.Broadcast.Broadcasted{<:Base.Broadcast.Style{Tuple}},
357-
) = copyto_per_field_scalar!(dest, bc)
361+
)
362+
copyto_per_field_scalar!(dest, bc)
363+
call_post_op_callback() && post_op_callback(dest, dest, bc)
364+
return dest
365+
end
358366

359-
@inline Base.copyto!(
367+
@inline function Base.copyto!(
360368
dest::FieldVector,
361369
bc::Base.Broadcast.Broadcasted{<:Base.Broadcast.AbstractArrayStyle{0}},
362-
) = copyto_per_field_scalar!(dest, bc)
370+
)
371+
copyto_per_field_scalar!(dest, bc)
372+
call_post_op_callback() && post_op_callback(dest, dest, bc)
373+
return dest
374+
end
363375

364-
@inline Base.copyto!(dest::FieldVector, bc::Real) =
376+
@inline function Base.copyto!(dest::FieldVector, bc::Real)
365377
copyto_per_field_scalar!(dest, bc)
378+
call_post_op_callback() && post_op_callback(dest, dest, bc)
379+
return dest
380+
end
366381

367382
@inline function copyto_per_field_scalar!(dest::FieldVector, bc)
368383
map(propertynames(dest)) do symb

0 commit comments

Comments
 (0)