Skip to content

Commit b10d131

Browse files
Sébastien LoiselSébastien Loisel
authored andcommitted
Improve assert_uniform: use stdout, isequal, add delay before abort
1 parent 5734297 commit b10d131

1 file changed

Lines changed: 30 additions & 36 deletions

File tree

src/MultiGridBarrierMPI.jl

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,10 @@ function MultiGridBarrier.amgb_assert_uniform(x::T, msg::String="") where T<:Num
102102
if rank == 0
103103
ref_val = all_values[1]
104104
for i in 2:nranks
105-
# Use exact equality for booleans, tolerance for floats
106-
if T <: Bool
107-
if all_values[i] != ref_val
108-
is_uniform = false
109-
break
110-
end
111-
else
112-
# For floats, check relative tolerance
113-
if !isapprox(all_values[i], ref_val; rtol=1e-10, atol=1e-14)
114-
is_uniform = false
115-
break
116-
end
105+
# Use isequal for exact equality (handles NaN correctly: isequal(NaN,NaN)=true)
106+
if !isequal(all_values[i], ref_val)
107+
is_uniform = false
108+
break
117109
end
118110
end
119111
end
@@ -122,28 +114,26 @@ function MultiGridBarrier.amgb_assert_uniform(x::T, msg::String="") where T<:Num
122114
is_uniform = MPI.Bcast(is_uniform, 0, comm)
123115

124116
if !is_uniform
125-
# Print error info on rank 0 only
117+
# Print error info on rank 0 only (use stdout for visibility)
126118
if rank == 0
127-
println(stderr, "\n" * "="^60)
128-
println(stderr, "MPI UNIFORMITY ASSERTION FAILED: $msg")
129-
println(stderr, "="^60)
130-
println(stderr, "Values across ranks:")
119+
println("\n" * "="^60)
120+
println("MPI UNIFORMITY ASSERTION FAILED: $msg")
121+
println("="^60)
122+
println("Values across ranks:")
131123
for i in 1:nranks
132-
println(stderr, " Rank $(i-1): $(all_values[i])")
133-
end
134-
println(stderr, "\nStack trace:")
135-
for (exc, bt) in current_exceptions()
136-
showerror(stderr, exc, bt)
137-
println(stderr)
124+
println(" Rank $(i-1): $(all_values[i])")
138125
end
139-
# Print current stack
126+
println("\nStack trace:")
140127
for frame in stacktrace()
141-
println(stderr, " ", frame)
128+
println(" ", frame)
142129
end
143-
println(stderr, "="^60)
144-
flush(stderr)
130+
println("="^60)
131+
flush(stdout)
145132
end
146133

134+
# Small delay to ensure output is flushed before abort
135+
sleep(0.1)
136+
147137
# Abort all ranks
148138
MPI.Abort(comm, 1)
149139
end
@@ -177,22 +167,26 @@ function MultiGridBarrier.amgb_assert_uniform(x::Bool, msg::String="")
177167
is_uniform = MPI.Bcast(is_uniform, 0, comm)
178168

179169
if !is_uniform
170+
# Print error info on rank 0 only (use stdout for visibility)
180171
if rank == 0
181-
println(stderr, "\n" * "="^60)
182-
println(stderr, "MPI UNIFORMITY ASSERTION FAILED: $msg")
183-
println(stderr, "="^60)
184-
println(stderr, "Boolean values across ranks:")
172+
println("\n" * "="^60)
173+
println("MPI UNIFORMITY ASSERTION FAILED: $msg")
174+
println("="^60)
175+
println("Boolean values across ranks:")
185176
for i in 1:nranks
186-
println(stderr, " Rank $(i-1): $(Bool(all_values[i]))")
177+
println(" Rank $(i-1): $(Bool(all_values[i]))")
187178
end
188-
println(stderr, "\nStack trace:")
179+
println("\nStack trace:")
189180
for frame in stacktrace()
190-
println(stderr, " ", frame)
181+
println(" ", frame)
191182
end
192-
println(stderr, "="^60)
193-
flush(stderr)
183+
println("="^60)
184+
flush(stdout)
194185
end
195186

187+
# Small delay to ensure output is flushed before abort
188+
sleep(0.1)
189+
196190
MPI.Abort(comm, 1)
197191
end
198192

0 commit comments

Comments
 (0)