Re: [julia-users] @generated function cumulative definition
Very nice example, showing both the benefits of Isaac's macros, and of using generated functions in general. I was wondering, because of the column major nature of Julia, should the order of the loops be changed? i.e. for A and B, it is changing the columns more rapidly than the rows, only matrix C is accessed in the right order for column major. On Saturday, February 20, 2016 at 8:54:47 AM UTC-5, Kristoffer Carlsson wrote: > > 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>> 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 >>> >>
Re: [julia-users] @generated function cumulative definition
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> 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 >> >
[julia-users] @generated function cumulative definition
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