LilithHafner / Chairmarks.jl

Benchmarks with back support
GNU General Public License v3.0
81 stars 8 forks source link

@b Produces `Infs` in matrix #106

Closed mipals closed 3 months ago

mipals commented 3 months ago

Hi there,

I might just be using the @b macro wrongly. But i was confused that in the following code

# This is on v. 1.2.1
using LinearAlgebra, Chairmarks
M = rand(3,3)
D = Diagonal(2ones(3))
@b lmul!(D,M) # M is not filled with Infs

the M gets filled with Infs.

LilithHafner commented 3 months ago

@b may execute the expression being benchmarked many times to get more reliable timings. In this case, when you call lmul!(D,M), all the elements of M are doubled. When @b does this repeatedly, eventually they overflow to Inf.

You can see a similar behavior with this

using LinearAlgebra
M = rand(3,3)
D = Diagonal(2ones(3))
for _ in 1:2000
    lmul!(D,M)
end
M # M is filled with Infs

And you can avoid that behavior by resetting the contents of M each iteration

using LinearAlgebra, Chairmarks
M = rand(3,3)
M0 = copy(M)
D = Diagonal(2ones(3))
@b lmul!(D,copyto!(M, M0))
M # M is not filled with Infs
mipals commented 3 months ago

Ahh, that makes sense. Thanks 👍