using Plots , LinearAlgebra , ForwardDiff , Roots , PlutoUI
using BenchmarkTools ; TableOfContents(title="Table of Contents", indent=true)
plotly()
Code Improvement
md"
# Code Improvement
"
Tips for speeding it up
focus on kernel routines: e.g. mvup, mvdown, findneighbors
there are only finite number of results for findneighbors, so a look-up table for staple / Esite or dedicated fast function will be efficient
simplicity is king!
specify the types of inputs
in-line, avoid nesting, check closure, type stability
fixed / preallocated memory runs faster
e.g. hard code the dim = 4 case would give us faster code
tools: @code_warntype, ProfileView, etc.
last but not least...
DO NOT OVERDO IT
md"
# Tips for speeding it up
* focus on kernel routines: e.g. mvup, mvdown, findneighbors
* there are only finite number of results for findneighbors, so a look-up table for staple / Esite or dedicated fast function will be efficient
* simplicity is king!
* specify the types of inputs
* in-line, avoid nesting, check closure, type stability
* fixed / preallocated memory runs faster
- e.g. hard code the dim = 4 case would give us faster code
* tools: @code_warntype, ProfileView, etc.
last but not least...
* DO NOT OVERDO IT
"
old (generic function with 1 method)
function old(Tnow; Ns = 8, dim = 3)
# MC implementation of ND Ising Model
jcoup = 1.0
mu = 1.0
Bnow = 0.
# handle periodicity
function Ip(i1::Int)::Int
# map 0 to Ns
end
function Ipc(x::CartesianIndex)::CartesianIndex
end
function findneighbors(latt, site::CartesianIndex; bothway = true)
# find all the neighbors given a site (index)
new (generic function with 3 methods)
function new(Tnow, Ns=8, dim=3)
# simplify mvup, mvdown
# dim must be known ntuple in the same scope
# remove garbage in findneighbors: e.g. bothway: i<j = (i != j) / 2
# tabulate / make fast function for staple to find Esite
# MC implementation of ND Ising Model
jcoup = 1.0
mu = 1.0
Bnow = 0.01
function mvup(x::CartesianIndex, d::Int64)
Enter cell code...
BenchmarkTools.Trial: 4 samples with 1 evaluation. Range (min … max): 1.654 s … 1.675 s ┊ GC (min … max): 1.42% … 1.46% Time (median): 1.666 s ┊ GC (median): 1.50% Time (mean ± σ): 1.666 s ± 9.644 ms ┊ GC (mean ± σ): 1.58% ± 0.22% █ █ █ █ █▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁█ ▁ 1.65 s Histogram: frequency by time 1.68 s < Memory estimate: 826.21 MiB, allocs estimate: 30466372.
BenchmarkTools.Trial: 777 samples with 1 evaluation. Range (min … max): 6.357 ms … 6.957 ms ┊ GC (min … max): 0.00% … 0.00% Time (median): 6.408 ms ┊ GC (median): 0.00% Time (mean ± σ): 6.437 ms ± 72.294 μs ┊ GC (mean ± σ): 0.00% ± 0.00% ▅█▆▄▂ ▁▃▁ ▅█████▇▇▇▄▄▄▄▄▄▄▃▄▃▄▃▄▄███▇▇▆▄▄▃▂▃▂▃▃▃▂▁▂▃▂▃▂▁▂▁▁▂▁▂▁▂▂▁▁▃ ▃ 6.36 ms Histogram: frequency by time 6.67 ms < Memory estimate: 26.81 KiB, allocs estimate: 1328.
Code Analysis
md"
# Code Analysis
"
FASTEST ISING CODE ON EARTH
(constantly update :D) DO NOT GO CRAZY!!
md"
# FASTEST ISING CODE ON EARTH
(constantly update :D)
DO NOT GO CRAZY!!
"
pokemon1 (generic function with 1 method)
function pokemon1(Tnow; Ns=12, dim=3)
# code improvement due to Biplab and Sasha!
# MC implementation of ND Ising Model
jcoup = 1.0
mu = 1.0
Bnow = 0.01
function mvup(x::CartesianIndex, d::Int64)
end
function mvdown(x::CartesianIndex, d::Int64)
pokemon2 (generic function with 1 method)
function pokemon2(Tnow; Ns=12, dim=3)
# MC implementation of ND Ising Model
# using iterators and tuples
jcoup = 1.0
mu = 1.0
Bnow = 0.01
# sites = Iterators.product(ntuple(i1->1:Ns, dim)...)
function mvup(x, d::Int64)
end
function mvdown(x, d::Int64)
sasha1 (generic function with 1 method)
function sasha1(T::Float64; B=0.01, Nd = 3, Ns = 12, Nstir = 150, Nm = 100, dNm = 10)
J = 1.0
μ = 1.0
# Spins
# Coordinate Grid
# Neighbours (directional vectors)
# Periodic Index
# Neighbours
# Energy of one spin
sasha2 (generic function with 1 method)
function sasha2(T::Float64, ; B = 0.01, Nd = 3, Ns = 12, Nstir = 150, Nm = 100, dNm = 10)
J = 1.0
μ = 1.0
# Spins
# Coordinate Grid
# Strides and increments
# Neighbours (with periodicity)
nh(x::Int64, s::Int64, a::Int64)::Int64 = x + s - a * (cld(x + s, a) - cld(x, a))
nl(x::Int64, s::Int64, a::Int64)::Int64 = x - s + a * (cld(x, a) - cld(x - s, a))
#nf = [[nh(site, str[i], steps[i]) for i in 1:Nd] for site in sites]
biplab1 (generic function with 1 method)
function biplab1(T;B=0.01,Ns=12,dim=3)
## Define Parameters
J = 1.0
μ = 1.0
## Create Lattice
## Unit Vectors
## Periodic Boundary Condition
## List to save the neighbors data
## List of possible energy for lookup
biplab2 (generic function with 1 method)
function biplab2(T::Float64;B=0.01,Ns=12,dim=3)
## Define Parameters
J = 1.0
μ = 1.0
## Create Lattice
## Unit Vectors
## Periodic Boundary Condition
BenchmarkTools.Trial: 127 samples with 1 evaluation. Range (min … max): 38.551 ms … 41.251 ms ┊ GC (min … max): 0.00% … 0.00% Time (median): 39.276 ms ┊ GC (median): 0.00% Time (mean ± σ): 39.444 ms ± 667.939 μs ┊ GC (mean ± σ): 0.00% ± 0.00% █▃█ ▂▂ ▆ ▅▄▅████▄▇▅▇▇███▇█▅█▇▄▅█▅▄▄▁▄▅▄▇▄▄█▄▄▅▇▁▇▁▁▅▁▄▁▁▄▄▅▄▄▁▄▄▄▁▁▁▄ ▄ 38.6 ms Histogram: frequency by time 41.2 ms < Memory estimate: 1.19 MiB, allocs estimate: 27273.
BenchmarkTools.Trial: 181 samples with 1 evaluation. Range (min … max): 27.083 ms … 29.023 ms ┊ GC (min … max): 0.00% … 0.00% Time (median): 27.649 ms ┊ GC (median): 0.00% Time (mean ± σ): 27.661 ms ± 376.113 μs ┊ GC (mean ± σ): 0.00% ± 0.00% ▁ ▁ ▁ █▄▆▁▂ ▁ ▄▆██▅▆▄▇█▅█▇▆▄█▅███████▇██▆▄▃▆▁▃▁▁▁▃▃▁▁▁▅▁▁▃▁▄▁▃▃▃▄▃▃▁▁▁▁▁▁▃ ▃ 27.1 ms Histogram: frequency by time 28.9 ms < Memory estimate: 257.58 KiB, allocs estimate: 5006.
BenchmarkTools.Trial: 168 samples with 1 evaluation. Range (min … max): 28.893 ms … 39.723 ms ┊ GC (min … max): 0.00% … 25.38% Time (median): 29.576 ms ┊ GC (median): 0.00% Time (mean ± σ): 29.770 ms ± 1.152 ms ┊ GC (mean ± σ): 0.37% ± 2.38% ▁▇█▄█▃ ▃▆████████▅▅▄▃▃▁▁▃▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▃▁▁▁▁▁▁▁▃ ▃ 28.9 ms Histogram: frequency by time 35.1 ms < Memory estimate: 3.04 MiB, allocs estimate: 2158.
BenchmarkTools.Trial: 178 samples with 1 evaluation. Range (min … max): 27.148 ms … 36.560 ms ┊ GC (min … max): 0.00% … 24.76% Time (median): 27.985 ms ┊ GC (median): 0.00% Time (mean ± σ): 28.104 ms ± 834.281 μs ┊ GC (mean ± σ): 0.35% ± 2.30% ▁▃▇█▂▁▅▃▁ ▃▁▃▁▄▇█████████▆▆▃▃▃▃▃▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃ ▃ 27.1 ms Histogram: frequency by time 31.9 ms < Memory estimate: 3.04 MiB, allocs estimate: 2158.
BenchmarkTools.Trial: 238 samples with 1 evaluation. Range (min … max): 20.838 ms … 21.462 ms ┊ GC (min … max): 0.00% … 0.00% Time (median): 21.084 ms ┊ GC (median): 0.00% Time (mean ± σ): 21.086 ms ± 125.525 μs ┊ GC (mean ± σ): 0.00% ± 0.00% ▃ █▄ ▂▅ ▃ ▅▇▃ ▃▂ ▂ ▂ ▂ ▅▁▃▃▃▃█▆█▇█▆██▃██▆▅██▇██▇▆▅███▇██▇█▅▅████▆██▇▅▇▇█▃▃▅▃█▃▃▅▁▅▃ ▅ 20.8 ms Histogram: frequency by time 21.4 ms < Memory estimate: 36.31 KiB, allocs estimate: 1328.
BenchmarkTools.Trial: 247 samples with 1 evaluation. Range (min … max): 19.938 ms … 20.688 ms ┊ GC (min … max): 0.00% … 0.00% Time (median): 20.242 ms ┊ GC (median): 0.00% Time (mean ± σ): 20.241 ms ± 142.488 μs ┊ GC (mean ± σ): 0.00% ± 0.00% ▂ ▂█▄ ▅▁ ▂▁▄▁▁▂ ▅ ▂▇▂ ▂ ▂▅ ▇▅▇▄▄▄▁▄ ▃▁▁▃▁▅▅█▆███▆███▆█████████████▆█▅███████████▆▅▆▃▆▅▁▆▃▁▁▁▃▁▁▅ ▅ 19.9 ms Histogram: frequency by time 20.6 ms < Memory estimate: 76.86 KiB, allocs estimate: 1331.
0.99478
0.00516408
-2.97916
0.995093
0.00480562
-2.99528
0.994757
0.00441708
-2.97405
0.0729466