Skip to content

Commit

Permalink
check for test slowdowns + test optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Oct 12, 2021
1 parent d71dbc6 commit 3bce0cf
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 130 deletions.
24 changes: 13 additions & 11 deletions src/linalg/blockdiagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,19 @@ function Base.exp(B::BlockDiagonal)
BlockDiagonal(map(block -> exp(block), B.blocks)...)
end

function Base.log(B::BlockDiagonal)
BlockDiagonal(map(block -> log(block), B.blocks)...)
end

function LinearAlgebra.det(B::BlockDiagonal{T}) where {T}
output = T(1)
for b in B.blocks
output *= det(b)
end
output
end
# This takes super long to compile...?
# Dot syntax is another 6x slower
# function Base.log(B::BlockDiagonal)
# BlockDiagonal(map(block -> log(block), B.blocks))
# end

# function LinearAlgebra.det(B::BlockDiagonal{T}) where {T}
# output = T(1)
# for b in B.blocks
# output *= det(b)
# end
# output
# end


# I thought this would be needed for greens(k, l), but it's not?
Expand Down
69 changes: 33 additions & 36 deletions test/linalg.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using LinearAlgebra
using MonteCarlo: vmul!, lvmul!, rvmul!, rdivp!, udt_AVX_pivot!, rvadd!, vsub!
using MonteCarlo: vmin!, vmininv!, vmax!, vmaxinv!, vinv!
using MonteCarlo: BlockDiagonal, CMat64, CVec64, StructArray
let


@testset "Custom Linear Algebra " begin

# Complex and Real Matrix mults, BlockDiagonal, UDT

for type in (Float64, ComplexF64)
@testset "avx multiplications ($type)" begin
@time for type in (Float64, ComplexF64)
println("avx multiplications ($type)")
@time @testset "avx multiplications ($type)" begin
A = rand(type, 8, 8)
B = rand(type, 8, 8)
C = rand(type, 8, 8)
Expand Down Expand Up @@ -75,7 +69,8 @@ using MonteCarlo: BlockDiagonal, CMat64, CVec64, StructArray



@testset "UDT transformations + rdivp! ($type)" begin
println("UDT transformations + rdivp! ($type)")
@time @testset "UDT transformations + rdivp! ($type)" begin
U = Matrix{Float64}(undef, 8, 8)
D = Vector{Float64}(undef, 8)
T = rand(8, 8)
Expand Down Expand Up @@ -111,7 +106,8 @@ using MonteCarlo: BlockDiagonal, CMat64, CVec64, StructArray



@testset "BlockDiagonal ($type)" begin
println("BlockDiagonal ($type)")
@time @testset "BlockDiagonal ($type)" begin
# setindex!
B = BlockDiagonal(zeros(4, 4), zeros(4, 4))
for i in 1:8, j in 1:8
Expand All @@ -127,7 +123,7 @@ using MonteCarlo: BlockDiagonal, CMat64, CVec64, StructArray
b1 = rand(type, 4, 4)
b2 = rand(type, 4, 4)
atol = 100eps(Float64)

B = BlockDiagonal(b1, b2)
@test B isa BlockDiagonal{type, 2, Matrix{type}}

Expand All @@ -138,67 +134,67 @@ using MonteCarlo: BlockDiagonal, CMat64, CVec64, StructArray
@test B[4+i, j] == 0.0
@test B[i, 4+j] == 0.0
end

# Matrix()
B1 = copy(B)
M1 = Matrix(B1)
@test M1 == B1

B2 = BlockDiagonal(rand(type, 4, 4), rand(type, 4, 4))
M2 = Matrix(B2)
B3 = BlockDiagonal(rand(type, 4, 4), rand(type, 4, 4))
M3 = Matrix(B3)

# Test (avx) multiplications
vmul!(B1, B2, B3)
vmul!(M1, M2, M3)
@test M1 B1 atol = atol

vmul!(B1, B2, adjoint(B3))
vmul!(M1, M2, adjoint(M3))
@test M1 B1

vmul!(B1, adjoint(B2), B3)
vmul!(M1, adjoint(M2), M3)
@test M1 B1 atol = atol

D = Diagonal(rand(8))
vmul!(B1, B2, D)
vmul!(M1, M2, D)
@test M1 B1 atol = atol

rvmul!(B1, D)
rvmul!(M1, D)
@test M1 B1 atol = atol

lvmul!(D, B1)
lvmul!(D, M1)
@test M1 B1 atol = atol

rvadd!(B1, D)
rvadd!(M1, D)
@test M1 B1 atol = atol

rvadd!(B1, B2)
rvadd!(M1, M2)
@test M1 B1 atol = atol

vsub!(B1, B2, I)
@test M2 - I B1 atol = atol

x = rand()
@test B2 * x M2 * x atol = atol

@test log(B2) log(M2) atol = atol

@test det(B2) det(M2) atol = atol

# @test log(B2) ≈ log(M2) atol = atol
# @test det(B2) ≈ det(M2) atol = atol
transpose!(B1, B2)
@test B1 transpose(M2) atol = atol

vmul!(B1, B2', B3')
@test B1 M2' * M3' atol = atol

# Test UDT and rdivp!
M2 = Matrix(B2)
D = rand(8)
Expand All @@ -216,7 +212,7 @@ using MonteCarlo: BlockDiagonal, CMat64, CVec64, StructArray
for i in 1:N; pivot[(i-1)*n+1 : i*n] .-= (i-1)*n; end

@test Matrix(B1) * Diagonal(D) * UpperTriangular(Matrix(B2)) * P M2

M1 = Matrix(B1)
M2 = Matrix(B2)
rdivp!(B1, B2, B3, pivot)
Expand All @@ -225,8 +221,9 @@ using MonteCarlo: BlockDiagonal, CMat64, CVec64, StructArray
end



@testset "Complex StructArray" begin

println("Complex StructArray")
@time @testset "Complex StructArray" begin
M1 = rand(ComplexF64, 8, 8)
C1 = StructArray(M1)
M2 = rand(ComplexF64, 8, 8)
Expand Down Expand Up @@ -340,4 +337,4 @@ using MonteCarlo: BlockDiagonal, CMat64, CVec64, StructArray
@test Matrix(C1) * Diagonal(D) * Matrix(C2) M2

end
end
end
170 changes: 87 additions & 83 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ using Test
using LinearAlgebra, Random, Dates
using MonteCarlo: @bm, TimerOutputs

using MonteCarlo: vmul!, lvmul!, rvmul!, rdivp!, udt_AVX_pivot!, rvadd!, vsub!
using MonteCarlo: vmin!, vmininv!, vmax!, vmaxinv!, vinv!
using MonteCarlo: BlockDiagonal, CMat64, CVec64, StructArray

struct DummyModel <: MonteCarlo.Model
lattice
end
Expand Down Expand Up @@ -37,93 +41,93 @@ macro benchmark_test(name, code)
end

@testset "All Tests" begin
println("Utilities")
@time @testset "Utilities" begin
@bm function test1(x, y)
sleep(x+y)
end
@bm test2(x, y) = sleep(x+y)
# @eval MonteCarlo ... will put the body of test3, test4 in the wrong scope
# eval(timer_expr(...)) doesn't work
function test3(x, y)
@benchmark_test "test1(::Any, ::Any)" begin sleep(x+y) end
end
test4(x, y) = @benchmark_test "test2(::Any, ::Any)" begin sleep(x+y) end

x = code_lowered(test1, Tuple{Float64, Float64})[1]
y = code_lowered(test3, Tuple{Float64, Float64})[1]
@test x.code == y.code

x = code_lowered(test2, Tuple{Float64, Float64})[1]
y = code_lowered(test4, Tuple{Float64, Float64})[1]
@test x.code == y.code

@test !MonteCarlo.timeit_debug_enabled()
enable_benchmarks()
@test MonteCarlo.timeit_debug_enabled()
disable_benchmarks()
@test !MonteCarlo.timeit_debug_enabled()
end
# 0.473232 seconds (778.10 k allocations: 47.050 MiB, 2.72% gc time, 99.50% compilation time)

println("Lattices")
@time @testset "Lattices" begin
include("lattices.jl")
end
# 22.329261 seconds (51.70 M allocations: 2.487 GiB, 3.09% gc time, 63.07% compilation time)

println("Model")
@time @testset "Models" begin
include("modeltests_IsingModel.jl")
include("modeltests_HubbardModel.jl")
end
# 11.418479 seconds (29.34 M allocations: 1.301 GiB, 4.78% gc time, 7.60% compilation time)
# println("Utilities")
# @time @testset "Utilities" begin
# @bm function test1(x, y)
# sleep(x+y)
# end
# @bm test2(x, y) = sleep(x+y)
# # @eval MonteCarlo ... will put the body of test3, test4 in the wrong scope
# # eval(timer_expr(...)) doesn't work
# function test3(x, y)
# @benchmark_test "test1(::Any, ::Any)" begin sleep(x+y) end
# end
# test4(x, y) = @benchmark_test "test2(::Any, ::Any)" begin sleep(x+y) end

# x = code_lowered(test1, Tuple{Float64, Float64})[1]
# y = code_lowered(test3, Tuple{Float64, Float64})[1]
# @test x.code == y.code

# x = code_lowered(test2, Tuple{Float64, Float64})[1]
# y = code_lowered(test4, Tuple{Float64, Float64})[1]
# @test x.code == y.code

# @test !MonteCarlo.timeit_debug_enabled()
# enable_benchmarks()
# @test MonteCarlo.timeit_debug_enabled()
# disable_benchmarks()
# @test !MonteCarlo.timeit_debug_enabled()
# end
# # 0.473232 seconds (778.10 k allocations: 47.050 MiB, 2.72% gc time, 99.50% compilation time)

# println("Lattices")
# @time @testset "Lattices" begin
# include("lattices.jl")
# end
# # 22.329261 seconds (51.70 M allocations: 2.487 GiB, 3.09% gc time, 63.07% compilation time)

# println("Model")
# @time @testset "Models" begin
# include("modeltests_IsingModel.jl")
# include("modeltests_HubbardModel.jl")
# end
# # 11.418479 seconds (29.34 M allocations: 1.301 GiB, 4.78% gc time, 7.60% compilation time)

println("Linear Algebra")
@time @testset "Linear Algebra" begin
include("linalg.jl")
@time include("linalg.jl")
end
# 72.325652 seconds (119.03 M allocations: 5.922 GiB, 3.30% gc time, 33.24% compilation time)

println("Slice Matrices")
@time @testset "Slice Matrices" begin
include("slice_matrices.jl")
end
# 5.212298 seconds (7.85 M allocations: 424.303 MiB, 1.66% gc time, 96.86% compilation time)

println("DQMC")
@time @testset "Flavors" begin
include("flavortests_DQMC.jl")
end
# 27.297888 seconds (55.24 M allocations: 3.149 GiB, 2.60% gc time, 2.67% compilation time)

println("Scheduler")
@time @testset "Scheduler & (DQMC) Updates" begin
include("updates.jl")
end
# 17.197345 seconds (33.06 M allocations: 1.846 GiB, 2.53% gc time, 4.69% compilation time)

println("Measurement")
@time @testset "Measurements" begin
include("measurements.jl")
end
# 20.091780 seconds (37.56 M allocations: 1.877 GiB, 2.46% gc time, 1.43% compilation time)

println("Integration")
@time @testset "Integration tests" begin
include("integration_tests.jl")
end
# 11.359702 seconds (19.67 M allocations: 1.132 GiB, 2.00% gc time, 28.81% compilation time)

println("ED")
@time @testset "Exact Diagonalization" begin
include("ED/ED_tests.jl")
end
# 71.714791 seconds (52.37 M allocations: 14.521 GiB, 1.82% gc time, 4.57% compilation time)

println("File IO")
@time @testset "File IO" begin
include("FileIO.jl")
end
# 72.591676 seconds (86.55 M allocations: 4.172 GiB, 1.27% gc time, 9.25% compilation time)
# println("Slice Matrices")
# @time @testset "Slice Matrices" begin
# include("slice_matrices.jl")
# end
# # 5.212298 seconds (7.85 M allocations: 424.303 MiB, 1.66% gc time, 96.86% compilation time)

# println("DQMC")
# @time @testset "Flavors" begin
# include("flavortests_DQMC.jl")
# end
# # 27.297888 seconds (55.24 M allocations: 3.149 GiB, 2.60% gc time, 2.67% compilation time)

# println("Scheduler")
# @time @testset "Scheduler & (DQMC) Updates" begin
# include("updates.jl")
# end
# # 17.197345 seconds (33.06 M allocations: 1.846 GiB, 2.53% gc time, 4.69% compilation time)

# println("Measurement")
# @time @testset "Measurements" begin
# include("measurements.jl")
# end
# # 20.091780 seconds (37.56 M allocations: 1.877 GiB, 2.46% gc time, 1.43% compilation time)

# println("Integration")
# @time @testset "Integration tests" begin
# include("integration_tests.jl")
# end
# # 11.359702 seconds (19.67 M allocations: 1.132 GiB, 2.00% gc time, 28.81% compilation time)

# println("ED")
# @time @testset "Exact Diagonalization" begin
# include("ED/ED_tests.jl")
# end
# # 71.714791 seconds (52.37 M allocations: 14.521 GiB, 1.82% gc time, 4.57% compilation time)

# println("File IO")
# @time @testset "File IO" begin
# include("FileIO.jl")
# end
# # 72.591676 seconds (86.55 M allocations: 4.172 GiB, 1.27% gc time, 9.25% compilation time)
end

0 comments on commit 3bce0cf

Please sign in to comment.