Since stephan wanted an example. Here is a generated function that unrolls
a matrix multiplication.
@gen_code function matmul!{N, T}(A::Matrix{T}, B::Matrix{T}, C::Matrix{T},
::Type{Val{N}})
@code :(fill!(A, 0.0))
for i in 1:N, j in 1:N, k in 1:N
@code :(A[$i,$j] += B[$i,$k] * C[$k,$j])
end
@code :(return A)
end
Benchmark comparison:
function bench{N}(::Type{Val{N}})
A = rand(N,N)
B = rand(N,N)
C = rand(N,N)
@time for i = 1:10^5 matmul!(A, B, C, Val{N}) end
@assert isapprox(A, B * C)
@time for i = 1:10^5 A_mul_B!(A, B, C) end
end
julia> bench(Val{2})
0.001262 seconds
0.002567 seconds
julia> bench(Val{3})
0.002427 seconds
0.003310 seconds
julia> bench(Val{4})
0.004748 seconds
0.015123 seconds
julia> bench(Val{5})
0.008624 seconds
0.023559 seconds
julia> bench(Val{6})
0.014464 seconds
0.022126 seconds
On Saturday, February 13, 2016 at 2:05:11 PM UTC+1, Stefan Karpinski wrote:
>
> Could you post an example usage?
>
> On Friday, February 12, 2016, Isaac Yonemoto <[email protected]
> <javascript:>> wrote:
>
>> As generated functions are right now, if you have a type-variadic section
>> of code, you have two options:
>>
>> 1) generate varying segments at the top, assign them to a variable and
>> perform code insertion using $
>>
>> This has obvious difficulties with readability.
>>
>> 2) create a variable, say "code" that holds the current, cumulative code,
>> and append to that variable by doing something like:
>>
>> code = quote
>> $code
>> #more code here
>> end
>>
>> This adds a lot of boilerplate overhead.
>>
>> I quickly wrote a macro system that reduces this to a single @code
>> macro....
>>
>> Don't know if anyone else would find this useful, but I'll take comments
>> before pushing this to a package.
>>
>> https://gist.github.com/ityonemo/65230126faddab4c6021
>>
>> Isaac
>>
>