Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy broadcasting macro #21

Merged
merged 5 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ julia> B = BroadcastArray(+, A, 2);
julia> B == A .+ 2
true
```
Such arrays can also be created using the macro `@lazy` which acts on ordinary
broadcasting expressions, or the macro `@lazydot` which applies `@.` to add dots first:
```julia
julia> C = rand(1000)';

julia> D = @lazy exp.(C)

julia> E = @lazydot 2 + log(C)

julia> @btime sum(@lazy C .* C'; dims=1) # 1.438 ms (5 allocations: 7.64 MiB) without @lazy
74.425 μs (7 allocations: 8.08 KiB)
```

## Multiplication

Expand Down
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
julia 0.7
FillArrays 0.3
StaticArrays 0.8.3
MacroTools
73 changes: 70 additions & 3 deletions src/lazybroadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,76 @@ getindex(B::BroadcastArray{<:Any,1}, kr::AbstractVector{<:Integer}) =

copy(bc::Broadcasted{<:LazyArrayStyle}) = BroadcastArray(bc)

# issue 16: sum(b, dims=(1,2,3)) faster than sum(b)
Base._sum(b::BroadcastArray{T,N}, ::Colon) where {T,N} = first(Base._sum(b, ntuple(identity, N)))
Base._prod(b::BroadcastArray{T,N}, ::Colon) where {T,N} = first(Base._prod(b, ntuple(identity, N)))
# Replacement for #18.
# Could extend this to other similar reductions in Base... or apply at lower level?
# for (fname, op) in [(:sum, :add_sum), (:prod, :mul_prod),
# (:maximum, :max), (:minimum, :min),
# (:all, :&), (:any, :|)]
function Base._sum(f, A::BroadcastArray, ::Colon)
bc = A.broadcasted
T = Broadcast.combine_eltypes(f ∘ bc.f, bc.args)
out = zero(T)
@simd for I in eachindex(bc)
@inbounds out += f(bc[I])
end
out
end
function Base._prod(f, A::BroadcastArray, ::Colon)
bc = A.broadcasted
T = Broadcast.combine_eltypes(f ∘ bc.f, bc.args)
out = one(T)
@simd for I in eachindex(bc)
@inbounds out *= f(bc[I])
end
out
end

# Macros for lazy broadcasting, #21 WIP
# based on @dawbarton https://discourse.julialang.org/t/19641/20
# and @tkf https://github.com/JuliaLang/julia/issues/19198#issuecomment-457967851
# and @chethega https://github.com/JuliaLang/julia/pull/30939

export @~

lazy(::Any) = throw(ArgumentError("function `lazy` exists only for its effect on broadcasting, see the macro @~"))
struct LazyCast{T}
value::T
end
Broadcast.broadcasted(::typeof(lazy), x) = LazyCast(x)
Broadcast.materialize(x::LazyCast) = BroadcastArray(x.value)

"""
@~ expr

Macro for creating lazy `BroadcastArray`s.
Expects a broadcasting expression, possibly created by the `@.` macro:
```
julia> @~ A .+ B ./ 2

julia> @~ @. A + B / 2
```
"""
macro ~(ex)
checkex(ex)
esc( :( $lazy.($ex) ) )
end

using MacroTools

function checkex(ex)
if @capture(ex, (arg__,) = val_ )
if arg[2]==:dims
throw(ArgumentError("@~ is capturing keyword arguments, try with `; dims = $val` instead of a comma"))
else
throw(ArgumentError("@~ is probably capturing capturing keyword arguments, try with ; or brackets"))
end
end
if @capture(ex, (arg_,rest__) )
throw(ArgumentError("@~ is capturing more than one expression, try $name($arg) with brackets"))
end
ex
end


BroadcastStyle(::Type{<:BroadcastArray{<:Any,N}}) where N = LazyArrayStyle{N}()
BroadcastStyle(L::LazyArrayStyle{N}, ::StaticArrayStyle{N}) where N = L
Expand Down
16 changes: 15 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,28 @@ end

@testset "BroadcastArray" begin
A = randn(6,6)

B = BroadcastArray(exp, A)
B′ = @~ exp.(A)
B′′ = @~ @. exp(A)
@test Matrix(B) == exp.(A)
@test Matrix(B′) == exp.(A)
@test Matrix(B′′) == exp.(A)

C = BroadcastArray(+, A, 2)
C′ = @~ A .+ 2
C′′ = @~ @. A + 2
@test C == A .+ 2
@test C′ == A .+ 2
@test C′′ == A .+ 2

D = BroadcastArray(+, A, C)
D′ = @~ A + C
D′′ = @~ @. A + C
@test D == A + C

@test D′ == A + C
@test D′′ == A + C

@test sum(B) ≈ sum(exp, A)
@test sum(C) ≈ sum(A .+ 2)

Expand Down