Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[opt] [async] Fix constant folding in async mode #2034

Merged
merged 9 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions taichi/program/async_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ void AsyncEngine::enqueue(const TaskLaunchRecord &t) {
void AsyncEngine::synchronize() {
TI_AUTO_PROF
bool modified = true;
TI_TRACE("Synchronizing SFG of {} nodes", sfg->size());
debug_sfg("initial");
sfg->reid_nodes();
sfg->reid_pending_nodes();
TI_TRACE("Synchronizing SFG of {} nodes ({} pending)", sfg->size(),
sfg->num_pending_tasks());
debug_sfg("initial");
Comment on lines +215 to +217
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off-topic change: num_pending_tasks() matches the following Ended up with {} nodes better.

if (program->config.debug) {
sfg->verify();
}
Expand Down
7 changes: 5 additions & 2 deletions taichi/program/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class CurrentKernelGuard {
} // namespace

Kernel::Kernel(Program &program,
std::function<void()> func,
std::string primal_name,
const std::function<void()> &func,
const std::string &primal_name,
bool grad)
: program(program), lowered(false), grad(grad) {
program.initialize_device_llvm_context();
Expand All @@ -43,6 +43,9 @@ Kernel::Kernel(Program &program,
ir = taichi::lang::context->get_root();

{
// Note: this is NOT a mutex. If we want to call Kernel::Kernel()
// concurrently, we need to lock this block of code together with
// taichi::lang::context with a mutex.
CurrentKernelGuard _(program, this);
program.start_function_definition(this);
func();
Expand Down
4 changes: 2 additions & 2 deletions taichi/program/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class Kernel {
};

Kernel(Program &program,
std::function<void()> func,
std::string name = "",
const std::function<void()> &func,
const std::string &name = "",
bool grad = false);

void compile();
Expand Down
22 changes: 8 additions & 14 deletions taichi/transforms/constant_fold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ class ConstantFold : public BasicStmtVisitor {

static Kernel *get_jit_evaluator_kernel(JITEvaluatorId const &id) {
auto &cache = get_current_program().jit_evaluator_cache;
{
// Discussion:
// https://github.com/taichi-dev/taichi/pull/954#discussion_r423442606
std::lock_guard<std::mutex> _(
get_current_program().jit_evaluator_cache_mut);
auto it = cache.find(id);
if (it != cache.end()) // cached?
return it->second.get();
}
// Discussion:
// https://github.com/taichi-dev/taichi/pull/954#discussion_r423442606
std::lock_guard<std::mutex> _(
get_current_program().jit_evaluator_cache_mut);
auto it = cache.find(id);
if (it != cache.end()) // cached?
return it->second.get();

auto kernel_name = fmt::format("jit_evaluator_{}", cache.size());
auto func = [&id]() {
Expand Down Expand Up @@ -65,11 +63,7 @@ class ConstantFold : public BasicStmtVisitor {
auto *ker_ptr = ker.get();
TI_TRACE("Saving JIT evaluator cache entry id={}",
std::hash<JITEvaluatorId>{}(id));
{
std::lock_guard<std::mutex> _(
get_current_program().jit_evaluator_cache_mut);
cache[id] = std::move(ker);
}
cache[id] = std::move(ker);
return ker_ptr;
}

Expand Down