Skip to content

Instantly share code, notes, and snippets.

@KristofferC
Last active September 5, 2015 14:29
Show Gist options
  • Save KristofferC/ec880377299b2f04836e to your computer and use it in GitHub Desktop.
Save KristofferC/ec880377299b2f04836e to your computer and use it in GitHub Desktop.
using MacroTools
symchar(symb::Symbol) = symb, 'N'
function symchar(ex::Expr)
if ex.head == symbol("'")
return ex.args[1], 'C'
elseif ex.head == :(.')
return ex.args[1], 'T'
else
throw(Error("invalid macro usage"))
end
end
macro into_plus!(ex)
α = 1.0
last_match = nothing
matched = false
match = @match ex begin
(C_ += A_ * B_) => (C, A, B)
end
if match != nothing
last_match = match
matched = true
end
match = @match ex begin
(C_ += α_ * A_ * B_) => (C, A, B, α)
end
if match != nothing
last_match = match
matched = true
α = match[4]
end
if last_match != nothing
C, A_s, B_s = last_match[1], last_match[2], last_match[3]
A, A_char = symchar(A_s)
B, B_char = symchar(B_s)
quote BLAS.gemm!($A_char, $B_char, $α, $A, $B, 1.0, $C) end
else
throw(ArgumentError("invalid macro usage"))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment