Fast Distance Histograms

If I understand you need only the distances smaller than rmax. For that the cell lists are particularly useful. But of course there is an overhead which might or not be worthwhile. For roughly homogeneous distributions of 10k points I am pretty sure that it is worthwhile if rmax is about 1/10 of the maximum distance (the difference between running a loop over 100m pairs vs ~1m pairs).

Anyway, to parallelize those loops I would probably try: GitHub - JuliaFolds/FLoops.jl: Fast sequential, threaded, and distributed for-loops for Julia—fold for humans™

I could not accelerate that by parallelizing (everything I tried the overhead was too high). But I found a slightly faster serial version at least (and shorter):

function dist1_2(r, histo, rmax, nbins)
    rb = nbins/rmax
    @inbounds for i = 1:size(r)[1]-1
        for j = i+1:size(r)[1]
            d=sqrt((r[i,1]-r[j,1])^2+(r[i,2]-r[j,2])^2)
            rIndex=ceil(Int, d*rb)
            (rIndex < nbins) && (histo[rIndex] += 1)
        end
    end
    return histo
end
julia> @btime dist1($points, hist, 5, 100) setup=(hist=copy(hist0));
  1.863 ms (0 allocations: 0 bytes)

julia> @btime dist1_2($points, hist, 5, 100) setup=(hist=copy(hist0));
  1.530 ms (0 allocations: 0 bytes)

julia> dist1(points, copy(hist0), 5, 100) == dist1_2(points, copy(hist0), 5, 100)
true



1 Like