Skip to content

Instantly share code, notes, and snippets.

@SimonDanisch
Created December 4, 2014 12:39
Show Gist options
  • Save SimonDanisch/736c19a1071be0f77bad to your computer and use it in GitHub Desktop.
Save SimonDanisch/736c19a1071be0f77bad to your computer and use it in GitHub Desktop.
immutable Functor{FuncName} end
#with staged functions available in 0.4:
#=
stagedfunction evaluate{NumArgs,FuncName}(::Functor{FuncName}, args::NTuple{NumArgs})
args_expr = map(1:NumArgs) do x
:(args[$x])
end
:($FuncName($(args_expr...)))
end
=#
#without:
for numargs=1:8, funcname=[:testfunction]
args_expr = map(1:numargs) do x
:(args[$x])
end
@eval evaluate(::Functor{$(parse(":$funcname"))}, args::NTuple{$numargs}) = $funcname($(args_expr...))
end
testfunction(a) = sum(a)
testfunction(a,b) = sum(a+b)
testfunction(a,b,c) = sum(a+b+c)
function broadcast_devec(m, n, f, arr_out, args::NTuple)
for j=1:n
for i=1:m
@inbounds arr_out[i, j] = evaluate(f, args)
end
end
end
#Your version!?
for numargs=1:8, funcname=[:testfunction]
args_expr = map(1:numargs) do x
:($(symbol(":a$x")))
end
@eval evaluate2(::Functor{$(parse(":$funcname"))}, $(args_expr...)) = $funcname($(args_expr...))
end
function broadcast_devec2(
num_args, m, n, f, arr_out,
a, b, c)
if num_args == 1
for j=1:n
for i=1:m
@inbounds arr_out[i, j] = evaluate2(f, a)
end
end
elseif num_args == 2
for j=1:n
for i=1:m
@inbounds arr_out[i, j] = evaluate2(f, a, b)
end
end
elseif num_args == 3
for j=1:n
for i=1:m
@inbounds arr_out[i, j] = evaluate2(f, a, b, c)
end
end
end
end
const myfunc = Functor{:testfunction}()
const N = 500
const m,n = (N,N)
const arr_out = rand(m,n)
const a,b,c = (rand(N), rand(N), rand(N))
@time broadcast_devec(m,n,myfunc, arr_out, (a,))
@time broadcast_devec(m,n,myfunc, arr_out, (a,b))
@time broadcast_devec(m,n,myfunc, arr_out, (a,b,c))
@time broadcast_devec2(1,m,n,myfunc, arr_out, a,b,c)
@time broadcast_devec2(2,m,n,myfunc, arr_out, a,b,c)
@time broadcast_devec2(3,m,n,myfunc, arr_out, a,b,c)
@time broadcast_devec(m,n,myfunc, arr_out, (a,)) #0.033829109 seconds (104 bytes allocated)
@time broadcast_devec2(1,m,n,myfunc, arr_out, a,b,c) #0.032053038 seconds (80 bytes allocated)
@time broadcast_devec(m,n,myfunc, arr_out, (a,b)) #0.425748692 seconds (1034000112 bytes allocated, 58.94% gc time)
@time broadcast_devec2(2,m,n,myfunc, arr_out, a,b,c) #0.497402536 seconds (1034000080 bytes allocated, 57.61% gc time)
@time broadcast_devec(m,n,myfunc, arr_out, (a,b,c)) #0.898736742 seconds (2068000120 bytes allocated, 58.89% gc time)
@time broadcast_devec2(3,m,n,myfunc, arr_out, a,b,c) #0.830133583 seconds (2068000080 bytes allocated, 60.20% gc time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment