Skip to content

Commit

Permalink
Merge pull request #333 from pablosanjose/less-mccopy
Browse files Browse the repository at this point in the history
Remove unnecessary calls to `minimal_callsafe_copy`
  • Loading branch information
pablosanjose authored Feb 6, 2025
2 parents 905cb1b + 61c8c34 commit 0a1837b
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 49 deletions.
9 changes: 2 additions & 7 deletions src/greenfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ call!_output(c::Contacts) = selfenergyblocks(c)
# These kwargs are currently not documented - see specialmatrices.jl
#region

(g::GreenFunction)(ω; params...) = minimal_callsafe_copy(call!(g, ω; params...))
# no need to minimal_callsafe_copy here, because a GreenSolution cannot be call!ed again
(g::GreenFunction)(ω; params...) = call!(g, ω; params...)
(g::GreenSlice)(ω; params...) = copy(call!(g, ω; params...))

call!(g::G, ω; params...) where {T,G<:Union{GreenFunction{T},GreenSlice{T}}} =
Expand Down Expand Up @@ -609,17 +610,11 @@ end
ensure_mutable_matrix(m::SMatrix) = Matrix(m)
ensure_mutable_matrix(m::AbstractMatrix) = m

minimal_callsafe_copy(s::TMatrixSlicer, parentham, parentcontacts) = TMatrixSlicer(
minimal_callsafe_copy(s.g0slicer, parentham, parentcontacts),
s.tmatrix, s.gcontacts, s.contactorbs)

Base.view(::NothingSlicer, i::Union{Integer,Colon}...) =
internalerror("view(::NothingSlicer): unreachable reached")

Base.getindex(::NothingSlicer, i::CellOrbitals...) = argerror("Slicer does not support generic indexing")

minimal_callsafe_copy(s::NothingSlicer, parentham, parentcontacts) = s

#endregion
#endregion

Expand Down
2 changes: 0 additions & 2 deletions src/solvers/green/bands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -877,8 +877,6 @@ view_or_copy(ψ, rows::Union{Colon,AbstractRange}, cols::Union{Colon,AbstractRan
view(ψ, rows, cols)
view_or_copy(ψ, rows, cols) = ψ[rows, cols]

minimal_callsafe_copy(s::BandsGreenSlicer, parentham, parentcontacts) = s # it is read-only

#endregion

############################################################################################
Expand Down
2 changes: 1 addition & 1 deletion src/solvers/green/internal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ end

needs_omega_shift(s::AppliedModelGreenSolver) = false

minimal_callsafe_copy(s::Union{ModelGreenSlicer,AppliedModelGreenSolver}, args...) = s
minimal_callsafe_copy(s::AppliedModelGreenSolver, args...) = s

Base.getindex(s::ModelGreenSlicer, is::CellOrbitals, js::CellOrbitals) =
[s.(i, j) for i in cellorbs(is), j in cellorbs(js)]
Expand Down
17 changes: 0 additions & 17 deletions src/solvers/green/schur.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,20 +621,6 @@ end
maybe_SMatrix(G::Matrix, rows::SVector{L}, cols::SVector{L´}) where {L,L´} = SMatrix{L,L´}(G)
maybe_SMatrix(G, rows, cols) = G

# TODO: Perhaps too conservative
function minimal_callsafe_copy(s::SchurGreenSlicer, parentham, parentcontacts)
= SchurGreenSlicer(s.ω, minimal_callsafe_copy(s.solver, parentham, parentcontacts))
isdefined(s, :G₋₁₋₁) && (s´.G₋₁₋₁ = minimal_callsafe_copy(s.G₋₁₋₁, parentham, parentcontacts))
isdefined(s, :G₁₁) && (s´.G₁₁ = minimal_callsafe_copy(s.G₁₁, parentham, parentcontacts))
isdefined(s, :G∞₀₀) && (s´.G∞₀₀ = minimal_callsafe_copy(s.G∞₀₀, parentham, parentcontacts))
isdefined(s, :L´G∞₀₀) && (s´.L´G∞₀₀ = copy(s.L´G∞₀₀))
isdefined(s, :R´G∞₀₀) && (s´.R´G∞₀₀ = copy(s.R´G∞₀₀))
isdefined(s, :G₁₁L) && (s´.G₁₁L = copy(s.G₁₁L))
isdefined(s, :G₋₁₋₁R) && (s´.G₋₁₋₁R = copy(s.G₋₁₋₁R))
isdefined(s, :R´G₁₁L) && (s´.R´G₁₁L = copy(s.R´G₁₁L))
isdefined(s, :L´G₋₁₋₁R) && (s´.L´G₋₁₋₁R = copy(s.L´G₋₁₋₁R))
return
end
#endregion

#endregion
Expand Down Expand Up @@ -725,9 +711,6 @@ function minimal_callsafe_copy(s::AppliedSchurGreenSolver2D, args...)
return AppliedSchurGreenSolver2D(h1D´, solver1D´, s.axis1D, s.wrapped_axes, s.phase_func, s.integrate_opts)
end

minimal_callsafe_copy(s::SchurGreenSlicer2D, args...) =
SchurGreenSlicer2D(s.ω, minimal_callsafe_copy(s.solver, args...), s.slicer_generator)

function build_slicer(s::AppliedSchurGreenSolver2D, g, ω, Σblocks, corbitals; params...)
function slicer_generator(s, ϕ_internal)
# updates h1D that is aliased into solver1D.fsolver with the apropriate phases,
Expand Down
8 changes: 0 additions & 8 deletions src/solvers/green/sparselu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,6 @@ similar_source64(s::SparseLUGreenSlicer, j::CellOrbitals) =
# getindex must return a Matrix
Base.getindex(s::SparseLUGreenSlicer, i::CellOrbitals, j::CellOrbitals) = copy(view(s, i, j))

# the lazy unitg field only aliases source64 or a copy of it. It is not necessary to
# maintain the alias, as this is just a prealloc for a full-cell slice. We don't even need
# to copy it, since once it is computed, it is never modified, only read
function minimal_callsafe_copy(s::SparseLUGreenSlicer{C}, parentham, parentcontacts) where {C}
= SparseLUGreenSlicer{C}(s.fact, s.nonextrng, s.unitcinds, s.unitcindsall, copy(s.source64))
isdefined(s, :unitg) && (s´.unitg = s.unitg)
return
end

#endregion

Expand Down
3 changes: 0 additions & 3 deletions src/solvers/green/spectrum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ spectrum(s::AppliedSpectrumGreenSolver) = s.spectrum
minimal_callsafe_copy(s::AppliedSpectrumGreenSolver, parentham, parentcontacts) =
AppliedSpectrumGreenSolver(s.spectrum)

minimal_callsafe_copy(s::SpectrumGreenSlicer, parentham, parentcontacts) =
SpectrumGreenSlicer(s.ω, s.solver)

#endregion

#region ## apply ##
Expand Down
3 changes: 1 addition & 2 deletions src/solvers/greensolvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
# - view(gs, ::Int, ::Int) -> g(ω; kw...) between specific contacts (has error fallback)
# - view(gs, ::Colon, ::Colon) -> g(ω; kw...) between all contacts (has error fallback)
# - Both of the above are of type `SubArray`
# It must also implement generic slicing, and minimal copying
# It must also implement generic slicing
# - gs[i::CellOrbitals, j::CellOrbitals] -> must return a `Matrix` for type stability
# - minimal_callsafe_copy(gs, parentham, parentcontacts)
# The user-facing indexing API accepts:
# - i::Integer -> Sites of Contact number i
# - sites(cell::Tuple, sind::Int)::Subcell -> Single site in a cell
Expand Down
13 changes: 4 additions & 9 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,8 @@ end

Base.size(s::SparseMatrixView, i...) = size(s.mat, i...)

# Definition of minimal_callsafe_copy
# if x´ = minimal_callsafe_copy(x), then a call!(x, ...; ...) will not affect x´ in any way
minimal_callsafe_copy(s::SparseMatrixView) =
SparseMatrixView(view(copy(parent(s.matview)), s.matview.indices...), copy(s.mat), s.ptrs)

Expand Down Expand Up @@ -1681,6 +1683,8 @@ blocktype(h::ParametricHamiltonian) = blocktype(parent(h))

lattice(h::ParametricHamiltonian) = lattice(hamiltonian(h))

# Definition of minimal_callsafe_copy
# if x´ = minimal_callsafe_copy(x), then a call!(x, ...; ...) will not affect x´ in any way
function minimal_callsafe_copy(p::ParametricHamiltonian)
= minimal_callsafe_copy(p.h)
modifiers´ = maybe_relink_serializer.(p.modifiers, Ref(h´))
Expand Down Expand Up @@ -2420,15 +2424,6 @@ function minimal_callsafe_copy(g::GreenFunction)
return GreenFunction(parent´, solver´, contacts´)
end

function minimal_callsafe_copy(g::GreenSolution)
parentg´ = minimal_callsafe_copy(g.parent)
parentham = hamiltonian(parentg´)
parentcontacts = contacts(parentg´)
slicer´ = minimal_callsafe_copy(g.slicer, parentham, parentcontacts)
= GreenSolution(parentg´, slicer´, g.contactΣs, g.contactorbs)
return
end

minimal_callsafe_copy(g::GreenSlice) =
GreenSlice(minimal_callsafe_copy(g.parent), g.rows, g.cols, copy(g.output))

Expand Down

0 comments on commit 0a1837b

Please sign in to comment.