Skip to content

Commit d249404

Browse files
Move non-zip extrema to attic
1 parent 030ea58 commit d249404

File tree

2 files changed

+136
-137
lines changed

2 files changed

+136
-137
lines changed

attic/vextrema_nonzip.jl

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
function vvextrema_nonzip(f::F, initmin::Iₘᵢₙ, initmax::Iₘₐₓ, A::AbstractArray{T, N}, dims::NTuple{M, Int}) where {F, Iₘᵢₙ, Iₘₐₓ, T, N, M}
3+
Dᴬ = size(A)
4+
Dᴮ = ntuple(d -> d dims ? 1 : Dᴬ[d], Val(N))
5+
Tₒ = Base.promote_op(f, T)
6+
B = similar(A, Tuple{Tₒ, Tₒ}, Dᴮ)
7+
_vvextrema_nonzip!(f, initmin, initmax, B, A, dims)
8+
return B
9+
end
10+
11+
function staticdim_extrema_nonzip_quote(Iₘᵢₙ, Iₘₐₓ, static_dims::Vector{Int}, N::Int)
12+
A = Expr(:ref, :A, ntuple(d -> Symbol(:i_, d), N)...)
13+
Bᵥ = Expr(:call, :view, :B)
14+
Bᵥ′ = Expr(:ref, :Bᵥ)
15+
rinds = Int[]
16+
nrinds = Int[]
17+
for d = 1:N
18+
if d static_dims
19+
push!(Bᵥ.args, Expr(:call, :firstindex, :B, d))
20+
push!(rinds, d)
21+
else
22+
push!(Bᵥ.args, :)
23+
push!(nrinds, d)
24+
push!(Bᵥ′.args, Symbol(:i_, d))
25+
end
26+
end
27+
reverse!(rinds)
28+
reverse!(nrinds)
29+
if !isempty(nrinds)
30+
block = Expr(:block)
31+
loops = Expr(:for, :($(Symbol(:i_, nrinds[1])) = indices((A, B), $(nrinds[1]))), block)
32+
for d @view(nrinds[2:end])
33+
newblock = Expr(:block)
34+
push!(block.args, Expr(:for, :($(Symbol(:i_, d)) = indices((A, B), $d)), newblock))
35+
block = newblock
36+
end
37+
rblock = block
38+
# Pre-reduction
39+
mn = Expr(:(=), :mn, Expr(:call, Symbol(Iₘᵢₙ.instance), Expr(:call, :eltype, Expr(:call, :eltype, :Bᵥ))))
40+
mx = Expr(:(=), :mx, Expr(:call, Symbol(Iₘₐₓ.instance), Expr(:call, :eltype, Expr(:call, :eltype, :Bᵥ))))
41+
push!(rblock.args, mn, mx)
42+
# Reduction loop alt
43+
newblock = Expr(:block)
44+
d = first(rinds)
45+
il1 = Expr(:for, :($(Symbol(:i_, d)) = axes(A, $d)), newblock)
46+
push!(block.args, :(@turbo $il1))
47+
# push!(block.args, quote @turbo $il1 end)
48+
block = newblock
49+
for d @view(rinds[2:end])
50+
newblock = Expr(:block)
51+
push!(block.args, Expr(:for, :($(Symbol(:i_, d)) = axes(A, $d)), newblock))
52+
block = newblock
53+
end
54+
# Push to inside innermost loop
55+
setmin = Expr(:(=), :mn, Expr(:call, :min, Expr(:call, :f, A), :mn))
56+
setmax = Expr(:(=), :mx, Expr(:call, :max, Expr(:call, :f, A), :mx))
57+
push!(block.args, setmin, setmax)
58+
setb = Expr(:(=), Bᵥ′, Expr(:tuple, :mn, :mx))
59+
push!(rblock.args, setb)
60+
return quote
61+
Bᵥ = $Bᵥ
62+
$loops
63+
return B
64+
end
65+
else
66+
# Pre-reduction
67+
mn = Expr(:(=), :mn, Expr(:call, Symbol(Iₘᵢₙ.instance), Expr(:call, :eltype, Expr(:call, :eltype, :Bᵥ))))
68+
mx = Expr(:(=), :mx, Expr(:call, Symbol(Iₘₐₓ.instance), Expr(:call, :eltype, Expr(:call, :eltype, :Bᵥ))))
69+
# Reduction loop
70+
block = Expr(:block)
71+
loops = Expr(:for, :($(Symbol(:i_, rinds[1])) = axes(A, $(rinds[1]))), block)
72+
for d @view(rinds[2:end])
73+
newblock = Expr(:block)
74+
push!(block.args, Expr(:for, :($(Symbol(:i_, d)) = axes(A, $d)), newblock))
75+
block = newblock
76+
end
77+
# Push to inside innermost loop
78+
setmin = Expr(:(=), :mn, Expr(:call, :min, Expr(:call, :f, A), :mn))
79+
setmax = Expr(:(=), :mx, Expr(:call, :max, Expr(:call, :f, A), :mx))
80+
push!(block.args, setmin, setmax)
81+
return quote
82+
Bᵥ = $Bᵥ
83+
$mn
84+
$mx
85+
@turbo $loops
86+
Bᵥ[] = (mn, mx)
87+
return B
88+
end
89+
end
90+
end
91+
92+
function branches_extrema_nonzip_quote(Iₘᵢₙ, Iₘₐₓ, N::Int, M::Int, D)
93+
static_dims = Int[]
94+
for m 1:M
95+
param = D.parameters[m]
96+
if param <: StaticInt
97+
new_dim = _dim(param)::Int
98+
push!(static_dims, new_dim)
99+
else
100+
# tuple of static dimensions
101+
t = Expr(:tuple)
102+
for n static_dims
103+
push!(t.args, :(StaticInt{$n}()))
104+
end
105+
q = Expr(:block, :(dimm = dims[$m]))
106+
qold = q
107+
# if-elseif statements
108+
ifsym = :if
109+
for n 1:N
110+
n static_dims && continue
111+
tc = copy(t)
112+
push!(tc.args, :(StaticInt{$n}()))
113+
qnew = Expr(ifsym, :(dimm == $n), :(return _vvextrema_nonzip!(f, initmin, initmax, B, A, $tc)))
114+
for r m+1:M
115+
push!(tc.args, :(dims[$r]))
116+
end
117+
push!(qold.args, qnew)
118+
qold = qnew
119+
ifsym = :elseif
120+
end
121+
# else statement
122+
tc = copy(t)
123+
for r m+1:M
124+
push!(tc.args, :(dims[$r]))
125+
end
126+
push!(qold.args, Expr(:block, :(return _vvextrema_nonzip!(f, initmin, initmax, B, A, $tc))))
127+
return q
128+
end
129+
end
130+
return staticdim_extrema_nonzip_quote(Iₘᵢₙ, Iₘₐₓ, static_dims, N)
131+
end
132+
133+
134+
@generated function _vvextrema_nonzip!(f::F, initmin::Iₘᵢₙ, initmax::Iₘₐₓ, B::AbstractArray{Tuple{Tₒ, Tₒ}, N}, A::AbstractArray{T, N}, dims::D) where {F, Iₘᵢₙ, Iₘₐₓ, Tₒ, T, N, M, D<:Tuple{Vararg{Integer, M}}}
135+
branches_extrema_nonzip_quote(Iₘᵢₙ, Iₘₐₓ, N, M, D)
136+
end

src/vextrema.jl

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -243,143 +243,6 @@ end
243243
end
244244

245245
################
246-
# function vvextrema_nonzip(f::F, initmin::Iₘᵢₙ, initmax::Iₘₐₓ, A::AbstractArray{T, N}, dims::NTuple{M, Int}) where {F, Iₘᵢₙ, Iₘₐₓ, T, N, M}
247-
# Dᴬ = size(A)
248-
# Dᴮ = ntuple(d -> d ∈ dims ? 1 : Dᴬ[d], Val(N))
249-
# Tₒ = Base.promote_op(f, T)
250-
# B = similar(A, Tuple{Tₒ, Tₒ}, Dᴮ)
251-
# _vvextrema_nonzip!(f, initmin, initmax, B, A, dims)
252-
# return B
253-
# end
254-
255-
# function staticdim_extrema_nonzip_quote(Iₘᵢₙ, Iₘₐₓ, static_dims::Vector{Int}, N::Int)
256-
# A = Expr(:ref, :A, ntuple(d -> Symbol(:i_, d), N)...)
257-
# Bᵥ = Expr(:call, :view, :B)
258-
# Bᵥ′ = Expr(:ref, :Bᵥ)
259-
# rinds = Int[]
260-
# nrinds = Int[]
261-
# for d = 1:N
262-
# if d ∈ static_dims
263-
# push!(Bᵥ.args, Expr(:call, :firstindex, :B, d))
264-
# push!(rinds, d)
265-
# else
266-
# push!(Bᵥ.args, :)
267-
# push!(nrinds, d)
268-
# push!(Bᵥ′.args, Symbol(:i_, d))
269-
# end
270-
# end
271-
# reverse!(rinds)
272-
# reverse!(nrinds)
273-
# if !isempty(nrinds)
274-
# block = Expr(:block)
275-
# loops = Expr(:for, :($(Symbol(:i_, nrinds[1])) = indices((A, B), $(nrinds[1]))), block)
276-
# for d ∈ @view(nrinds[2:end])
277-
# newblock = Expr(:block)
278-
# push!(block.args, Expr(:for, :($(Symbol(:i_, d)) = indices((A, B), $d)), newblock))
279-
# block = newblock
280-
# end
281-
# rblock = block
282-
# # Pre-reduction
283-
# mn = Expr(:(=), :mn, Expr(:call, Symbol(Iₘᵢₙ.instance), Expr(:call, :eltype, Expr(:call, :eltype, :Bᵥ))))
284-
# mx = Expr(:(=), :mx, Expr(:call, Symbol(Iₘₐₓ.instance), Expr(:call, :eltype, Expr(:call, :eltype, :Bᵥ))))
285-
# push!(rblock.args, mn, mx)
286-
# # Reduction loop alt
287-
# newblock = Expr(:block)
288-
# d = first(rinds)
289-
# il1 = Expr(:for, :($(Symbol(:i_, d)) = axes(A, $d)), newblock)
290-
# push!(block.args, :(@turbo $il1))
291-
# # push!(block.args, quote @turbo $il1 end)
292-
# block = newblock
293-
# for d ∈ @view(rinds[2:end])
294-
# newblock = Expr(:block)
295-
# push!(block.args, Expr(:for, :($(Symbol(:i_, d)) = axes(A, $d)), newblock))
296-
# block = newblock
297-
# end
298-
# # Push to inside innermost loop
299-
# setmin = Expr(:(=), :mn, Expr(:call, :min, Expr(:call, :f, A), :mn))
300-
# setmax = Expr(:(=), :mx, Expr(:call, :max, Expr(:call, :f, A), :mx))
301-
# push!(block.args, setmin, setmax)
302-
# setb = Expr(:(=), Bᵥ′, Expr(:tuple, :mn, :mx))
303-
# push!(rblock.args, setb)
304-
# return quote
305-
# Bᵥ = $Bᵥ
306-
# $loops
307-
# return B
308-
# end
309-
# else
310-
# # Pre-reduction
311-
# mn = Expr(:(=), :mn, Expr(:call, Symbol(Iₘᵢₙ.instance), Expr(:call, :eltype, Expr(:call, :eltype, :Bᵥ))))
312-
# mx = Expr(:(=), :mx, Expr(:call, Symbol(Iₘₐₓ.instance), Expr(:call, :eltype, Expr(:call, :eltype, :Bᵥ))))
313-
# # Reduction loop
314-
# block = Expr(:block)
315-
# loops = Expr(:for, :($(Symbol(:i_, rinds[1])) = axes(A, $(rinds[1]))), block)
316-
# for d ∈ @view(rinds[2:end])
317-
# newblock = Expr(:block)
318-
# push!(block.args, Expr(:for, :($(Symbol(:i_, d)) = axes(A, $d)), newblock))
319-
# block = newblock
320-
# end
321-
# # Push to inside innermost loop
322-
# setmin = Expr(:(=), :mn, Expr(:call, :min, Expr(:call, :f, A), :mn))
323-
# setmax = Expr(:(=), :mx, Expr(:call, :max, Expr(:call, :f, A), :mx))
324-
# push!(block.args, setmin, setmax)
325-
# return quote
326-
# Bᵥ = $Bᵥ
327-
# $mn
328-
# $mx
329-
# @turbo $loops
330-
# Bᵥ[] = (mn, mx)
331-
# return B
332-
# end
333-
# end
334-
# end
335-
336-
# function branches_extrema_nonzip_quote(Iₘᵢₙ, Iₘₐₓ, N::Int, M::Int, D)
337-
# static_dims = Int[]
338-
# for m ∈ 1:M
339-
# param = D.parameters[m]
340-
# if param <: StaticInt
341-
# new_dim = _dim(param)::Int
342-
# push!(static_dims, new_dim)
343-
# else
344-
# # tuple of static dimensions
345-
# t = Expr(:tuple)
346-
# for n ∈ static_dims
347-
# push!(t.args, :(StaticInt{$n}()))
348-
# end
349-
# q = Expr(:block, :(dimm = dims[$m]))
350-
# qold = q
351-
# # if-elseif statements
352-
# ifsym = :if
353-
# for n ∈ 1:N
354-
# n ∈ static_dims && continue
355-
# tc = copy(t)
356-
# push!(tc.args, :(StaticInt{$n}()))
357-
# qnew = Expr(ifsym, :(dimm == $n), :(return _vvextrema_nonzip!(f, initmin, initmax, B, A, $tc)))
358-
# for r ∈ m+1:M
359-
# push!(tc.args, :(dims[$r]))
360-
# end
361-
# push!(qold.args, qnew)
362-
# qold = qnew
363-
# ifsym = :elseif
364-
# end
365-
# # else statement
366-
# tc = copy(t)
367-
# for r ∈ m+1:M
368-
# push!(tc.args, :(dims[$r]))
369-
# end
370-
# push!(qold.args, Expr(:block, :(return _vvextrema_nonzip!(f, initmin, initmax, B, A, $tc))))
371-
# return q
372-
# end
373-
# end
374-
# return staticdim_extrema_nonzip_quote(Iₘᵢₙ, Iₘₐₓ, static_dims, N)
375-
# end
376-
377-
378-
# @generated function _vvextrema_nonzip!(f::F, initmin::Iₘᵢₙ, initmax::Iₘₐₓ, B::AbstractArray{Tuple{Tₒ, Tₒ}, N}, A::AbstractArray{T, N}, dims::D) where {F, Iₘᵢₙ, Iₘₐₓ, Tₒ, T, N, M, D<:Tuple{Vararg{Integer, M}}}
379-
# branches_extrema_nonzip_quote(Iₘᵢₙ, Iₘₐₓ, N, M, D)
380-
# end
381-
382-
############################################################################################
383246
# Version wherein an initial value is supplied
384247
function vvextrema(f::F, initmin::Iₘᵢₙ, initmax::Iₘₐₓ, A::AbstractArray{T, N}, ::Colon) where {F, Iₘᵢₙ<:Number, Iₘₐₓ<:Number, T, N}
385248
Tₒ = Base.promote_op(f, T)

0 commit comments

Comments
 (0)