Skip to content

Instantly share code, notes, and snippets.

@ChrisRackauckas
Last active August 6, 2016 04:19
Show Gist options
  • Save ChrisRackauckas/7992e7e230805911fb25aea7254a0ee4 to your computer and use it in GitHub Desktop.
Save ChrisRackauckas/7992e7e230805911fb25aea7254a0ee4 to your computer and use it in GitHub Desktop.
Adding timing tests
function addingTimings()
b = rand(35);
updateIdxs = [1:3;5;7;8;10:12;14:16;18:35]
dim1 = 40; dim2 = 16
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
truesol = b[1]*k[1] + b[2]*k[2] + b[3]*k[3] + b[5]*k[5] + b[7]*k[7] + b[8]*k[8] + b[10]*k[10] + b[11]*k[11] + b[12]*k[12] + b[14]*k[14] + b[15]*k[15] + b[16]*k[16] + b[18]*k[18] + b[19]*k[19] + b[20]*k[20] + b[21]*k[21] + b[22]*k[22] + b[23]*k[23] + b[24]*k[24] + b[25]*k[25] + b[26]*k[26] + b[27]*k[27] + b[28]*k[28] + b[29]*k[29] + b[30]*k[30] + b[31]*k[31] + b[32]*k[32] + b[33]*k[33] + b[34]*k[34] + b[35]*k[35]
print("First method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
sol = b[1]*k[1] + b[2]*k[2] + b[3]*k[3] + b[5]*k[5] + b[7]*k[7] + b[8]*k[8] + b[10]*k[10] + b[11]*k[11] + b[12]*k[12] + b[14]*k[14] + b[15]*k[15] + b[16]*k[16] + b[18]*k[18] + b[19]*k[19] + b[20]*k[20] + b[21]*k[21] + b[22]*k[22] + b[23]*k[23] + b[24]*k[24] + b[25]*k[25] + b[26]*k[26] + b[27]*k[27] + b[28]*k[28] + b[29]*k[29] + b[30]*k[30] + b[31]*k[31] + b[32]*k[32] + b[33]*k[33] + b[34]*k[34] + b[35]*k[35]
#println(maximum(abs(truesol-sol))<5e-15)
end
print("Second method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
sol2 = (b[1]*k[1] + b[2]*k[2] + b[3]*k[3] + b[5]*k[5]) + (b[7]*k[7] + b[8]*k[8] + b[10]*k[10] + b[11]*k[11]) + (b[12]*k[12] + b[14]*k[14] + b[15]*k[15] + b[16]*k[16]) + (b[18]*k[18] + b[19]*k[19] + b[20]*k[20] + b[21]*k[21]) + (b[22]*k[22] + b[23]*k[23] + b[24]*k[24] + b[25]*k[25]) + (b[26]*k[26] + b[27]*k[27] + b[28]*k[28] + b[29]*k[29]) + (b[30]*k[30] + b[31]*k[31] + b[32]*k[32] + b[33]*k[33]) + (b[34]*k[34] + b[35]*k[35])
end
print("Third method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
tmp = zeros(dim1,dim2)
for i in updateIdxs
tmp += b[i]*k[i]
end
sol3 = tmp
end
print("Fourth method:")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
sol4 = (b[1]*k[1] + b[2]*k[2]) + (b[3]*k[3] + b[5]*k[5]) + (b[7]*k[7] + b[8]*k[8]) + (b[10]*k[10] + b[11]*k[11]) + (b[12]*k[12] + b[14]*k[14]) + (b[15]*k[15] + b[16]*k[16]) + (b[18]*k[18] + b[19]*k[19]) + (b[20]*k[20] + b[21]*k[21]) + (b[22]*k[22] + b[23]*k[23]) + (b[24]*k[24] + b[25]*k[25]) + (b[26]*k[26] + b[27]*k[27]) + (b[28]*k[28] + b[29]*k[29]) + (b[30]*k[30] + b[31]*k[31]) + (b[32]*k[32] + b[33]*k[33]) + (b[34]*k[34] + b[35]*k[35])
end
print("Fifth method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
sol4 = (b[1]*k[1] + b[2]*k[2] + b[3]*k[3] + b[5]*k[5] + b[7]*k[7] + b[8]*k[8]) + (b[10]*k[10] + b[11]*k[11] + b[12]*k[12] + b[14]*k[14] + b[15]*k[15] + b[16]*k[16]) + (b[18]*k[18] + b[19]*k[19] + b[20]*k[20] + b[21]*k[21] + b[22]*k[22] + b[23]*k[23]) + (b[24]*k[24] + b[25]*k[25] + b[26]*k[26] + b[27]*k[27] + b[28]*k[28] + b[29]*k[29]) + (b[30]*k[30] + b[31]*k[31] + b[32]*k[32] + b[33]*k[33] + b[34]*k[34] + b[35]*k[35])
end
print("Sixth method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
sol4 = (b[1]*k[1] + b[2]*k[2] + b[3]*k[3] + b[5]*k[5] + b[7]*k[7] + b[8]*k[8] + b[10]*k[10] + b[11]*k[11]) + (b[12]*k[12] + b[14]*k[14] + b[15]*k[15] + b[16]*k[16] + b[18]*k[18] + b[19]*k[19] + b[20]*k[20] + b[21]*k[21]) + (b[22]*k[22] + b[23]*k[23] + b[24]*k[24] + b[25]*k[25] + b[26]*k[26] + b[27]*k[27] + b[28]*k[28] + b[29]*k[29]) + (b[30]*k[30] + b[31]*k[31] + b[32]*k[32] + b[33]*k[33] + b[34]*k[34] + b[35]*k[35])
end
print("Seventh method:")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
sol4 = (b[1]*k[1] + b[2]*k[2] + b[3]*k[3] + b[5]*k[5] + b[7]*k[7] + b[8]*k[8] + b[10]*k[10] + b[11]*k[11] + b[12]*k[12] + b[14]*k[14] + b[15]*k[15] + b[16]*k[16] + b[18]*k[18] + b[19]*k[19] + b[20]*k[20] + b[21]*k[21]) + (b[22]*k[22] + b[23]*k[23] + b[24]*k[24] + b[25]*k[25] + b[26]*k[26] + b[27]*k[27] + b[28]*k[28] + b[29]*k[29] + b[30]*k[30] + b[31]*k[31] + b[32]*k[32] + b[33]*k[33] + b[34]*k[34] + b[35]*k[35])
end
print("Eighth method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
tmp = zeros(dim1,dim2)
Threads.@threads for i in updateIdxs
tmp += b[i]*k[i]
end
sol3 = tmp
end
print("Ninth method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
tmp = zeros(dim1,dim2)
for i=1:4:25, j in eachindex(tmp)
tmp[j] += b[updateIdxs[i]]*k[updateIdxs[i]][j] + b[updateIdxs[i+1]]*k[updateIdxs[i+1]][j] + b[updateIdxs[i+2]]*k[updateIdxs[i+2]][j] + b[updateIdxs[i+3]]*k[updateIdxs[i+3]][j]
end
tmp += b[34]*k[34] + b[35]*k[35]
#println(maximum(abs(truesol - tmp))<5e-15)
end
print("Ninth.2 method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
tmp = zeros(dim1,dim2)
for i=1:4:25
tmp[:] .+= b[updateIdxs[i]].*k[updateIdxs[i]][:] .+ b[updateIdxs[i+1]].*k[updateIdxs[i+1]][:] .+ b[updateIdxs[i+2]].*k[updateIdxs[i+2]][:] .+ b[updateIdxs[i+3]].*k[updateIdxs[i+3]][:]
end
tmp += b[34].*k[34] .+ b[35].*k[35]
#println(maximum(abs(truesol - tmp))<5e-15)
end
print("Ninth.3 method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
tmp = zeros(dim1,dim2)
for i=1:5:30
tmp += b[updateIdxs[i]]*k[updateIdxs[i]] + b[updateIdxs[i+1]]*k[updateIdxs[i+1]] + b[updateIdxs[i+2]]*k[updateIdxs[i+2]] + b[updateIdxs[i+3]]*k[updateIdxs[i+3]] + b[updateIdxs[i+4]]*k[updateIdxs[i+4]]
end
#println(maximum(abs(truesol - tmp))<5e-15)
end
print("Tenth method: ")
gc()
@time @inbounds for iter = 1:NUM_RUNS
k = Vector{Matrix{Float64}}(0)
for i in 1:35
push!(k,rand(dim1,dim2))
end
tmp = zeros(dim1,dim2)
for i=1:6:30
tmp += b[updateIdxs[i]]*k[updateIdxs[i]] + b[updateIdxs[i+1]]*k[updateIdxs[i+1]] + b[updateIdxs[i+2]]*k[updateIdxs[i+2]] + b[updateIdxs[i+3]]*k[updateIdxs[i+3]] + b[updateIdxs[i+4]]*k[updateIdxs[i+4]] + b[updateIdxs[i+5]]*k[updateIdxs[i+5]]
end
#println(maximum(abs(truesol - tmp))<5e-15)
end
#maximum(abs(sol - sol2))< 5e-15 && maximum(abs(sol - sol3))< 5e-15 # Checked to make sure correct
end
const NUM_RUNS = 10
println("Compile")
addingTimings()
println("Test")
for i=1:2 addingTimings() end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment