Skip to content

Commit

Permalink
More robust show, fix one, fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsignorelli committed Feb 21, 2024
1 parent fe069e3 commit e856645
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/src/man/fastgtpsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ Without using the macro, each time an operation is performed using a TPS, a new

The macro `@FastGTPSA` basically tells the code to instead use a permanent, pre-allocated buffer of TPSs to contain the temporaries during evaluation of the expression, so there is no dynamic memory allocation until the result is obtained; the number of allocations is reduced to 1. Furthermore, these temporaries are accessed and deleted in a stack-like manner from the buffer, so that temporaries involved in operations are right next to each other in memory. This ensures minimal cache misses throughout the evaluation of the expression.

The speedup of using the macro can be quite significant. See our [example](https://github.com/bmad-sim/GTPSA.jl/blob/main/benchmark/taylormap.jl), where we observe a roughly x2.5 speedup.
The speedup of using the macro can be quite significant. See our [example](https://github.com/bmad-sim/GTPSA.jl/blob/main/benchmark/track.jl), where we observe a roughly x2.5 speedup.
2 changes: 1 addition & 1 deletion docs/src/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ x = vars(use=d);
@btime @FastGTPSA $x[1]^3*sin($x[2])/log(2+$x[3])-exp($x[1]*$x[2])*im;
```

The advantages of using the macro become especially apparent in more complicated systems, for example in `benchmark/taylormap.jl`.
The advantages of using the macro become especially apparent in more complicated systems, for example in `benchmark/track.jl`.

## Promotion of `TPS` to `ComplexTPS`

Expand Down
10 changes: 5 additions & 5 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ function zero(ct::ComplexTPS)::ComplexTPS
end

# --- one ---
function one(t::TPS)::TPS
t = TPS(mad_tpsa_new(t.tpsa, MAD_TPSA_SAME))
function one(t1::TPS)::TPS
t = TPS(mad_tpsa_new(t1.tpsa, MAD_TPSA_SAME))
mad_tpsa_set0!(t.tpsa, 0.0, 1.0)
return t
end

function one(ct::ComplexTPS)::ComplexTPS
ct = ComplexTPS(mad_ctpsa_new(ct.tpsa, MAD_TPSA_SAME))
mad_ctpsa_set0!(t.tpsa, ComplexF64(0.0), ComplexF64(1.0))
function one(ct1::ComplexTPS)::ComplexTPS
ct = ComplexTPS(mad_ctpsa_new(ct1.tpsa, MAD_TPSA_SAME))
mad_ctpsa_set0!(ct.tpsa, ComplexF64(0.0), ComplexF64(1.0))
return ct
end

Expand Down
24 changes: 17 additions & 7 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,13 @@ show(io::IO, ::MIME"text/plain", m::Vector{<:Union{TPS,ComplexTPS}}) = show_vec(

function show_vec(io::IO, m::Vector{<:Union{TPS,ComplexTPS}})
N = length(m)
lines_used=Ref{Int}(0)
if N < 1
print(io, "$(eltype(m))[]")
return
end
println(io, N, "-element $(typeof(m)):")
lines_used[] += 1
for i in eachindex(m)
if !isassigned(m, i)
println(io, "\n\tAtleast one $(eltype(m)) is undefined!")
Expand All @@ -282,25 +284,28 @@ function show_vec(io::IO, m::Vector{<:Union{TPS,ComplexTPS}})
if desc != unsafe_load(Base.unsafe_convert(Ptr{Desc}, unsafe_load(m[i].tpsa).d))
println(io, "WARNING: Atleast one $(eltype(m)) has a different Descriptor!")
diffdescs = true
lines_used[] += 1
end
end
extralines=0
if GTPSA.show_header
if diffdescs
println(io, "Cannot show GTPSA header (non-unique Descriptor).")
lines_used[] += 1
else
println(io, "-----------------------")
show_GTPSA_info(io, desc)
println(io, "-----------------------")
extralines = 2 + (desc.nv > 0 ? 2 : 0) + (desc.np > 0 ? 2 : 0)
lines_used[] += 2 + (desc.nv > 0 ? 2 : 0) + (desc.np > 0 ? 2 : 0)
end
end
show_map(io, m, extralines)
show_map!(io, m, lines_used)
end

# WARNING: only_vars should ONLY be set by developers who know what they're doing!
function show_map(io::IO, m::Vector{<:Union{TPS,ComplexTPS}}, extralines=0, only_vars=false)
# Same for varnames!
function show_map!(io::IO, m::Vector{<:Union{TPS,ComplexTPS}}, lines_used::Ref=Ref{Int}(0), only_vars=false, varnames=1:length(m))
N = only_vars ? min(unsafe_load(Base.unsafe_convert(Ptr{Desc}, unsafe_load(m[1].tpsa).d)).nv,length(m)) : length(m)
length(varnames)== N || error("invalid varnames length")
tf_GTPSA = TextFormat(up_right_corner = '-',
up_left_corner = '-',
bottom_left_corner = ' ',
Expand All @@ -327,14 +332,16 @@ function show_map(io::IO, m::Vector{<:Union{TPS,ComplexTPS}}, extralines=0, only
end
hlines = Int[0]
out, formatters = format(m[1], coloffset=1, max_nn=max_nn)
out[:,1] .= 1

out[:,1] .= varnames[1]
for i=2:N
push!(hlines, length(out[:,1]))
tmpout, __ = format(m[i],coloffset=1, max_nn=max_nn)
tmpout[:,1] .= i
tmpout[:,1] .= varnames[i]
out = vcat(out, tmpout)
end
# Check if sparse monomial or exponent:
!get(io, :limit, false) || lines_used[] < displaysize(io)[1]-5 || (println(io, "\t"); return)
if GTPSA.show_sparse
if eltype(m) == TPS
println(io, " Out Coefficient Order Monomial")
Expand All @@ -348,5 +355,8 @@ function show_map(io::IO, m::Vector{<:Union{TPS,ComplexTPS}}, extralines=0, only
println(io, " Out Real Imag Order Exponent")
end
end
pretty_table(io, out,tf=tf_GTPSA,formatters=(ft_printf("%3i:",1), formatters...),show_header=false, alignment=:l, hlines=hlines, body_hlines_format=('-','-','-','-'),display_size=(displaysize(io)[1]-4-extralines,displaysize(io)[2]),vlines=[])
lines_used[] += 1
!get(io, :limit, false) || lines_used[] < displaysize(io)[1]-5 || (println(io, "\t"); return)
pretty_table(io, out,tf=tf_GTPSA,formatters=(ft_printf("%3i:",1), formatters...),show_header=false, alignment=:l, hlines=hlines, body_hlines_format=('-','-','-','-'),display_size=(displaysize(io)[1]-2-lines_used[],displaysize(io)[2]),vlines=[])
lines_used[] += length(out[:,1])+N # each border is a line
end

0 comments on commit e856645

Please sign in to comment.