Skip to content

Parallelization

Letícia Maria Pequeno Madureira edited this page Jul 31, 2024 · 1 revision
  • Identify the most costly function (repulsion)
function repulsion(basis, molecule::Molecule)
    n = length(basis)
    G = zeros(n, n, n, n)

    for c in CartesianIndices(G)
        i, j, t, u = c[1], c[2], c[3], c[4]

        basisᵢ, basisⱼ, basisₜ, basisᵤ = basis[i], basis[j], basis[t], basis[u]
        Rᵢ, Rⱼ, Rₜ, Rᵤ = basisᵢ.R, basisⱼ.R, basisₜ.R, basisᵤ.R
        m, p, q, r = basisᵢ.size, basisⱼ.size, basisₜ.size, basisᵤ.size

        ℓᵢ, mᵢ, nᵢ = basisᵢ.ℓ, basisᵢ.m, basisᵢ.n
        ℓⱼ, mⱼ, nⱼ = basisⱼ.ℓ, basisⱼ.m, basisⱼ.n
        ℓₜ, mₜ, nₜ = basisₜ.ℓ, basisₜ.m, basisₜ.n
        ℓᵤ, mᵤ, nᵤ = basisᵤ.ℓ, basisᵤ.m, basisᵤ.n

        for e in CartesianIndices((m, p, q, r))
            k, l, s, v = e[1], e[2], e[3], e[4]

            αᵢ, αⱼ, αₜ, αᵤ = basisᵢ.α[k], basisⱼ.α[l], basisₜ.α[s], basisᵤ.α[v]
            dᵢ, dⱼ, dₜ, dᵤ = basisᵢ.d[k], basisⱼ.d[l], basisₜ.d[s], basisᵤ.d[v]
            Nᵢ, Nⱼ, Nₜ, Nᵤ = basisᵢ.N[k], basisⱼ.N[l], basisₜ.N[s], basisᵤ.N[v]

            tei = dᵢ * dⱼ * dₜ * dᵤ * Nᵢ * Nⱼ * Nₜ * Nᵤ
            tei *= Gxyz(ℓᵢ, mᵢ, nᵢ, ℓⱼ, mⱼ, nⱼ, ℓₜ, mₜ, nₜ, ℓᵤ, mᵤ, nᵤ, αᵢ, αⱼ, αₜ, αᵤ, Rᵢ, Rⱼ, Rₜ, Rᵤ)

            G[i, j, t, u] += tei
        end
    end

    return G
end

If I parallelize the for loop, I cannot write on G in parallel form. Take G out of the for loop.

This is the change on the BasisSets.jl package

julia> @benchmark parsebasis(mol, "6-21g")
BenchmarkTools.Trial: 132 samples with 1 evaluation.
 Range (min  max):  26.997 ms  129.966 ms  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     35.509 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   38.202 ms ±  11.909 ms  ┊ GC (mean ± σ):  0.00% ± 0.00%

    ▁ ▁ █ ▆
  ▃▄█▅█▇█▅███▄█▄▄▃▄▃▃▃▃▃▁▃▁▁▁▃▁▃▁▃▃▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃ ▃
  27 ms           Histogram: frequency by time         87.5 ms <

 Memory estimate: 114.91 KiB, allocs estimate: 1232.
Clone this wiki locally