Skip to content

Instantly share code, notes, and snippets.

@ChrisRackauckas
Last active August 6, 2016 07:53
Show Gist options
  • Save ChrisRackauckas/e84553294be74a0d368d982071139f2b to your computer and use it in GitHub Desktop.
Save ChrisRackauckas/e84553294be74a0d368d982071139f2b to your computer and use it in GitHub Desktop.
function testLoops()
b = rand(35);
updateIdxs = [1:3;5;7;8;10:12;14:16;18:35];
dim1 = 1000; const dim2 = 1000;
NUM_RUNS = 1;
k = Vector{Matrix{Float64}}(0);
for i in 1:35
push!(k,rand(dim1,dim2));
end
const 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];
tmp = zeros(dim1,dim2)
@time @inbounds for iter = 1:NUM_RUNS
tmp[:] = zero(eltype(tmp))
for i=1:30, j in eachindex(tmp)
tmp[j] += b[updateIdxs[i]]*k[updateIdxs[i]][j]
end
tmp
#println(maximum(abs(tmp - truesol))<1e-14)
end
@time @inbounds for iter = 1:NUM_RUNS
tmp[:] = zero(eltype(tmp))
for i=1:2:30, j in eachindex(tmp)
tmp[j] += b[updateIdxs[i]]*k[updateIdxs[i]][j] + b[updateIdxs[i+1]]*k[updateIdxs[i+1]][j]
end
tmp
#println(maximum(abs(tmp - truesol))<1e-14)
end
@time @inbounds for iter = 1:NUM_RUNS
tmp[:] = zero(eltype(tmp))
for i=1:3:30, 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]
end
tmp
#println(maximum(abs(tmp - truesol))<1e-14)
end
@time @inbounds for iter = 1:NUM_RUNS
tmp[:] = zero(eltype(tmp))
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
for j in eachindex(tmp)
tmp[j] += b[34]*k[34][j] + b[35]*k[35][j]
end
tmp
#println(maximum(abs(tmp - truesol))<1e-14)
end
#Definitely worse
@time @inbounds for iter = 1:NUM_RUNS
tmp[:] = zero(eltype(tmp))
for i=1:5:30, 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] + b[updateIdxs[i+4]]*k[updateIdxs[i+4]][j]
end
tmp
#println(maximum(abs(tmp - truesol))<1e-14)
end
@time @inbounds for iter = 1:NUM_RUNS
tmp[:] = zero(eltype(tmp))
for i=1:6:30, 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] + b[updateIdxs[i+4]]*k[updateIdxs[i+4]][j] + b[updateIdxs[i+5]]*k[updateIdxs[i+5]][j]
end
tmp
#println(maximum(abs(tmp - truesol))<1e-14)
end
b = rand(35);
k = Vector{Matrix{Float64}}(0);
for i in 1:35
push!(k,rand(dim1,dim2));
end
@time @inbounds for iter = 1:NUM_RUNS
tmp[:] = zero(eltype(tmp))
for j in eachindex(tmp)
tmp[j] = b[1]*k[1][j] + b[2]*k[2][j] + b[3]*k[3][j] + b[5]*k[5][j] + b[7]*k[7][j] + b[8]*k[8][j] + b[10]*k[10][j] + b[11]*k[11][j] + b[12]*k[12][j] + b[14]*k[14][j] + b[15]*k[15][j] + b[16]*k[16][j] + b[18]*k[18][j] + b[19]*k[19][j] + b[20]*k[20][j] + b[21]*k[21][j] + b[22]*k[22][j] + b[23]*k[23][j] + b[24]*k[24][j] + b[25]*k[25][j] + b[26]*k[26][j] + b[27]*k[27][j] + b[28]*k[28][j] + b[29]*k[29][j] + b[30]*k[30][j] + b[31]*k[31][j] + b[32]*k[32][j] + b[33]*k[33][j] + b[34]*k[34][j] + b[35]*k[35][j]
end
tmp
end
b = rand(35);
k = Vector{Matrix{Float64}}(0);
for i in 1:35
push!(k,rand(dim1,dim2));
end
@time @inbounds for iter = 1:NUM_RUNS
tmp[:] = zero(eltype(tmp))
for j in eachindex(tmp)
tmp[j] = (b[1]*k[1][j] + b[2]*k[2][j] + b[3]*k[3][j] + b[5]*k[5][j]) + (b[7]*k[7][j] + b[8]*k[8][j] + b[10]*k[10][j] + b[11]*k[11][j]) + (b[12]*k[12][j] + b[14]*k[14][j] + b[15]*k[15][j] + b[16]*k[16][j]) + (b[18]*k[18][j] + b[19]*k[19][j] + b[20]*k[20][j] + b[21]*k[21][j]) + (b[22]*k[22][j] + b[23]*k[23][j] + b[24]*k[24][j] + b[25]*k[25][j]) + (b[26]*k[26][j] + b[27]*k[27][j] + b[28]*k[28][j] + b[29]*k[29][j]) + (b[30]*k[30][j] + b[31]*k[31][j] + b[32]*k[32][j] + b[33]*k[33][j]) + (b[34]*k[34][j] + b[35]*k[35][j])
end
tmp
end
b = rand(35);
k = Vector{Matrix{Float64}}(0);
for i in 1:35
push!(k,rand(dim1,dim2));
end
@time @inbounds for iter = 1:NUM_RUNS
tmp[:] = zero(eltype(tmp))
for j in eachindex(tmp)
tmp[j] = (b[1]*k[1][j] + b[2]*k[2][j]) + (b[3]*k[3][j] + b[5]*k[5][j]) + (b[7]*k[7][j] + b[8]*k[8][j]) + (b[10]*k[10][j] + b[11]*k[11][j]) + (b[12]*k[12][j] + b[14]*k[14][j]) + (b[15]*k[15][j] + b[16]*k[16][j]) + (b[18]*k[18][j] + b[19]*k[19][j]) + (b[20]*k[20][j] + b[21]*k[21][j]) + (b[22]*k[22][j] + b[23]*k[23][j]) + (b[24]*k[24][j] + b[25]*k[25][j]) + (b[26]*k[26][j] + b[27]*k[27][j]) + (b[28]*k[28][j] + b[29]*k[29][j]) + (b[30]*k[30][j] + b[31]*k[31][j]) + (b[32]*k[32][j] + b[33]*k[33][j]) + (b[34]*k[34][j] + b[35]*k[35][j])
end
tmp
end
b = rand(35);
k = Vector{Matrix{Float64}}(0);
for i in 1:35
push!(k,rand(dim1,dim2));
end
end
testLoops() # compile
println("run")
testLoops() # run
### Runtimes
# 6.123939 seconds (209.98 M allocations: 3.129 GB, 1.55% gc time)
# 5.051467 seconds (179.98 M allocations: 2.682 GB, 1.60% gc time)
# 4.568466 seconds (159.98 M allocations: 2.384 GB, 1.54% gc time)
# 4.150308 seconds (139.99 M allocations: 2.086 GB, 1.48% gc time)
# 4.128596 seconds (143.98 M allocations: 2.146 GB, 1.55% gc time)
# 6.019585 seconds (169.98 M allocations: 2.607 GB, 1.51% gc time)
# 0.711936 seconds (31.00 M allocations: 671.387 MB, 9.80% gc time)
# 0.175091 seconds (7.00 M allocations: 106.812 MB, 29.76% gc time)
# 0.776436 seconds (35.00 M allocations: 625.610 MB, 14.86% gc time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment