-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #522 from willow-ahrens/wma/cleanup-caching2
Wma/cleanup caching2
- Loading branch information
Showing
7 changed files
with
194 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" | ||
defer_tables(root::LogicNode) | ||
Replace immediate tensors with deferred expressions assuming the original program structure | ||
is given as input to the program. | ||
""" | ||
function defer_tables(ex, node::LogicNode) | ||
if @capture node table(~tns::isimmediate, ~idxs...) | ||
table(deferred(:($ex.tns.val), typeof(tns.val)), map(enumerate(node.idxs)) do (i, idx) | ||
defer_tables(:($ex.idxs[$i]), idx) | ||
end) | ||
elseif istree(node) | ||
similarterm(node, operation(node), map(enumerate(node.children)) do (i, child) | ||
defer_tables(:($ex.children[$i]), child) | ||
end) | ||
else | ||
node | ||
end | ||
end | ||
|
||
""" | ||
cache_deferred(ctx, root::LogicNode, seen) | ||
Replace deferred expressions with simpler expressions, and cache their evaluation in the preamble. | ||
""" | ||
function cache_deferred!(ctx, root::LogicNode) | ||
seen::Dict{Any, LogicNode} = Dict{Any, LogicNode}() | ||
return Rewrite(Postwalk(node -> if isdeferred(node) | ||
get!(seen, node.val) do | ||
var = freshen(ctx, :V) | ||
push!(ctx.preamble, :($var = $(node.ex)::$(node.type))) | ||
deferred(var, node.type) | ||
end | ||
end))(root) | ||
end | ||
|
||
function logic_executor_code(ctx, prgm) | ||
ctx_2 = JuliaContext() | ||
freshen(ctx_2, :prgm) | ||
code = contain(ctx_2) do ctx_3 | ||
prgm = defer_tables(:prgm, prgm) | ||
prgm = cache_deferred!(ctx_3, prgm) | ||
ctx(prgm) | ||
end | ||
code = pretty(code) | ||
fname = gensym(:compute) | ||
return :(function $fname(prgm) | ||
$code | ||
end) |> striplines | ||
end | ||
|
||
""" | ||
LogicExecutor(ctx) | ||
Executes a logic program by compiling it with the given compiler `ctx`. Compiled | ||
codes are cached, and are only compiled once for each program with the same | ||
structure. | ||
""" | ||
struct LogicExecutor | ||
ctx | ||
end | ||
|
||
codes = Dict() | ||
function (ctx::LogicExecutor)(prgm) | ||
f = get!(codes, get_structure(prgm)) do | ||
eval(logic_executor_code(ctx.ctx, prgm)) | ||
end | ||
return Base.invokelatest(f, prgm) | ||
end | ||
|
||
""" | ||
LogicExecutorCode(ctx) | ||
Return the code that would normally be used by the LogicExecutor to run a program. | ||
""" | ||
struct LogicExecutorCode | ||
ctx | ||
end | ||
|
||
function (ctx::LogicExecutorCode)(prgm) | ||
return logic_executor_code(ctx.ctx, prgm) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.