Can someone help me to accelerate the pairwise damage computation? This problem is very similar to the one in Accelerate pairwise L-J force computation. And I adopted the conclusion that the pairlist should be sorted first to have a better performance. The following codes are modified based on the one in Accelerate pairwise Lennard-Jones force computation given by @ lmiq
using BenchmarkTools
using StaticArrays
const Ξ» = 1.0E-6
@inline @fastmath function damage_pair!(f,x,y,i,j,d)
new_d = 0.0
for idim = 1:2
new_d += (x[idim] - y[idim]) ^ 2
end
new_d = sqrt(new_d)
str = new_d - d
strhis = 0.0
pairdam = 0.0
if str > strhis
strhis = str
end
if strhis > Ξ»
pairdam = 1.0 - exp(-1000.0*(str-Ξ»))
end
@inbounds f[i] += pairdam
@inbounds f[j] += pairdam
return f
end
function cal_damage!(f,x,pairlist,pairlength)
for ipair in 1:length(pairlist)
i = pairlist[ipair][1]
j = pairlist[ipair][2]
d = pairlength[ipair]
f = damage_pair!(f,x[i],x[j],i,j,d)
end
return f
end
n = 10_000 # number of particles
npairs = 8026946 # number of pairs within cutoff
pairlist = [ SVector{2,Int}(rand(1:n),rand(1:n)) for _ in 1:npairs ]
sort!(pairlist)
pairlength = [rand() for _ in 1:npairs]
x = [rand(SVector{2,Float64}) for _ in 1:n]
f = zeros(Float64,n)
@benchmark cal_damage!(f,x,pairlist,pairlength)
The performance is 130ms and much slower than 31ms in Accelerate pairwise Lennard-Jones force computation
BenchmarkTools.Trial: 39 samples with 1 evaluation.
Range (min β¦ max): 130.446 ms β¦ 133.848 ms β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 131.095 ms β GC (median): 0.00%
Time (mean Β± Ο): 131.420 ms Β± 875.379 ΞΌs β GC (mean Β± Ο): 0.00% Β± 0.00%
β β ββββββ β β β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
130 ms Histogram: frequency by time 134 ms <
Memory estimate: 0 bytes, allocs estimate: 0.
Then I commented out the exp
in the pairdam
pairdam = 1.0 #- exp(-1000.0*str)
The result is 37ms, which means evaluating the exp
function takes 100ms!
BenchmarkTools.Trial: 132 samples with 1 evaluation.
Range (min β¦ max): 36.775 ms β¦ 44.848 ms β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 37.599 ms β GC (median): 0.00%
Time (mean Β± Ο): 37.924 ms Β± 1.224 ms β GC (mean Β± Ο): 0.00% Β± 0.00%
ββββ
β
ββ
βββββββββββ
βββ
βββββββββββββββββββββββββββββββββββββββββββββ β
36.8 ms Histogram: frequency by time 44.4 ms <
Memory estimate: 0 bytes, allocs estimate: 0.
Then I commented out two if
in the function damage_pair!
@inline @fastmath function damage_pair!(f,x,y,i,j,d)
new_d = 0.0
for idim = 1:2
new_d += (x[idim] - y[idim]) ^ 2
end
new_d = sqrt(new_d)
str = new_d - d
strhis = 0.0
pairdam = 0.0
# if str > strhis
strhis = str
# end
# if strhis > Ξ»
pairdam = 1.0 #- exp(-1000.0*(str-Ξ»))
# end
@inbounds f[i] += pairdam
@inbounds f[j] += pairdam
return f
end
The result is 22ms.
BenchmarkTools.Trial: 222 samples with 1 evaluation.
Range (min β¦ max): 22.185 ms β¦ 25.645 ms β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 22.393 ms β GC (median): 0.00%
Time (mean Β± Ο): 22.545 ms Β± 456.674 ΞΌs β GC (mean Β± Ο): 0.00% Β± 0.00%
βββ
ββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
22.2 ms Histogram: frequency by time 24.9 ms <
Memory estimate: 0 bytes, allocs estimate: 0.
As shown in above, evaluating the exp
function takes 100ms, and two if
takes 15ms. I have tried to put the exp into a function, but the performance is the same, and using the 2.718281828459045^(-1000.0*(strhis-Ξ»))
as shown in Slow exp()? , the performance is worse, around 340ms.
Is there any possible way to accelerate the computation of exp
and evaluation of if
?