Skip to content

Commit 77495de

Browse files
committed
Added vmap and vmap!
1 parent 69ab0a6 commit 77495de

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/LoopVectorization.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ using MacroTools: prewalk, postwalk
99

1010

1111
export LowDimArray, stridedpointer, vectorizable,
12-
@vectorize, @vvectorize, @avx,
12+
@vectorize, @vvectorize, @avx, ,
13+
vmap, vmap!
1314

1415
function isdense end #
1516

@@ -904,6 +905,7 @@ include("broadcast.jl")
904905
include("determinestrategy.jl")
905906
include("lowering.jl")
906907
include("constructors.jl")
908+
include("map.jl")
907909
include("precompile.jl")
908910
_precompile_()
909911

src/map.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
4+
@generated function vmap!(f::F, dest::AbstractArray{T}, args::Vararg{<:AbstractArray,N}) where {F,T,N}
5+
W, Wshift = VectorizationBase.pick_vector_width_shift(T)
6+
val = Expr(:curly, :Val, W)
7+
q = Expr(:block, Expr(:(=), :M, Expr(:call, :length, :dest)), Expr(:(=), :vdest, Expr(:call, :vectorizable, :dest)), Expr(:(=), :m, 0))
8+
fcall = Expr(:call, :f)
9+
loopbody = Expr(:block, Expr(:call, :vstore!, :vdest, fcall, :m), Expr(:(+=), :m, W))
10+
fcallmask = Expr(:call, :f)
11+
bodymask = Expr(:block, Expr(:(=), :__mask__, Expr(:call, :mask, val, Expr(:call, :&, :M, W-1))), Expr(:call, :vstore!, :vdest, fcallmask, :m, :__mask__))
12+
for n 1:N
13+
arg_n = Symbol(:varg_,n)
14+
push!(q.args, Expr(:(=), arg_n, Expr(:call, :vectorizable, Expr(:ref, :args, n))))
15+
push!(fcall.args, Expr(:call, :vload, Expr(:call, val), arg_n, :m))
16+
push!(fcallmask.args, Expr(:call, :vload, Expr(:call, val), arg_n, :m, :__mask__))
17+
end
18+
loop = Expr(:for, Expr(:(=), :_, Expr(:call, :(:), 0, Expr(:call, :-, Expr(:call, :(>>>), :M, Wshift), 1))), loopbody)
19+
push!(q.args, loop)
20+
ifmask = Expr(:if, Expr(:call, :(!=), :m, :M), bodymask)
21+
push!(q.args, ifmask)
22+
push!(q.args, :dest)
23+
q
24+
end
25+
function vmap(f::F, args...) where {F}
26+
T = Base._return_type(f, Base.Broadcast.eltypes(args))
27+
dest = similar(first(args), T)
28+
vmap!(f, dest, args...)
29+
end
30+
31+

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,16 @@ end
358358
@test D1 D2
359359
end
360360
end
361+
362+
@testset "map" begin
363+
foo(x, y) = exp(x) - sin(y)
364+
N = 37
365+
for T (Float32,Float64)
366+
a = rand(T, N); b = rand(T, N)
367+
c1 = map(foo, a, b)
368+
c2 = vmap(foo, a, b)
369+
@test c1 c2
370+
end
371+
end
372+
361373
end

0 commit comments

Comments
 (0)