Skip to content

Commit c6d1e83

Browse files
abussyTechnici4n
andauthored
Fix slow compute_density with FD (#1183)
Co-authored-by: Bruno Ploumhans <[email protected]>
1 parent 6b80720 commit c6d1e83

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/densities.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ using an optional `occupation_threshold`. By default all occupation numbers are
1212
"""
1313
@views @timing function compute_density(basis::PlaneWaveBasis{T,VT}, ψ, occupation;
1414
occupation_threshold=zero(T)) where {T,VT}
15-
= promote_type(T, real(eltype(ψ[1])))
16-
= promote_type(VT, real(eltype(ψ[1])))
17-
# Note, that we special-case Tψ, since when T is Dual and eltype(ψ[1]) is not
18-
# (e.g. stress calculation), then only the normalisation factor introduces
19-
# dual numbers, but not yet the FFT
20-
2115
# Occupation should be on the CPU as we are going to be doing scalar indexing.
2216
occupation = [to_cpu(oc) for oc in occupation]
2317
mask_occ = [findall(occnk -> abs(occnk) occupation_threshold, occk)
2418
for occk in occupation]
2519

2620
function allocate_local_storage()
21+
# The types were moved inside here to avoid a type instability,
22+
# as it seems that captures over types do not get specialized!
23+
= promote_type(T, real(eltype(ψ[1])))
24+
# Note, that we special-case Tψ, since when T is Dual and eltype(ψ[1]) is not
25+
# (e.g. stress calculation), then only the normalisation factor introduces
26+
# dual numbers, but not yet the FFT
27+
= promote_type(VT, real(eltype(ψ[1])))
28+
2729
(; ρ=zeros_like(G_vectors(basis), Tρ, basis.fft_size..., basis.model.n_spin_components),
2830
ψnk_real=zeros_like(G_vectors(basis), complex(Tψ), basis.fft_size...))
2931
end
@@ -39,7 +41,7 @@ using an optional `occupation_threshold`. By default all occupation numbers are
3941
.* abs2.(storage.ψnk_real))
4042

4143
end
42-
ρ = sum(getfield.(storages, ))
44+
ρ = sum(storage -> storage.ρ, storages)
4345

4446
mpi_sum!(ρ, basis.comm_kpts)
4547
ρ = symmetrize_ρ(basis, ρ; do_lowpass=false)
@@ -49,7 +51,7 @@ using an optional `occupation_threshold`. By default all occupation numbers are
4951
negtol = max(sqrt(eps(T)), 10occupation_threshold)
5052
minimum(ρ) < -negtol && @warn("Negative ρ detected", min_ρ=minimum(ρ))
5153

52-
ρ::AbstractArray{Tρ, 4}
54+
ρ
5355
end
5456

5557
# Variation in density corresponding to a variation in the orbitals and occupations.

0 commit comments

Comments
 (0)