diff --git a/docs/hello.rst b/docs/hello.rst index c463e3faaeccc..54a98a59d8522 100644 --- a/docs/hello.rst +++ b/docs/hello.rst @@ -55,9 +55,9 @@ Let's dive into this simple Taichi program. import taichi as ti ------------------- -Taichi is a domain-specific language (DSL) embedded in Python. To make Taichi as easy to use as a Python package, -we have done heavy engineering with this goal in mind - letting every Python programmer write Taichi codes with -minimal learning effort. You can even use your favorite Python package management system, Python IDEs and other +Taichi is a domain-specific language (DSL) embedded in Python. To make Taichi as easy to use as a Python package, +we have done heavy engineering with this goal in mind - letting every Python programmer write Taichi codes with +minimal learning effort. You can even use your favorite Python package management system, Python IDEs and other Python packages in conjunction with Taichi. Portability diff --git a/docs/utilities.rst b/docs/utilities.rst index 69bf8692e82f6..84c1fd98057cc 100644 --- a/docs/utilities.rst +++ b/docs/utilities.rst @@ -58,7 +58,7 @@ For example, this is part of the output by ``ti regression`` after enabling cons The suggested workflow for **the PR author** to run the regression tests is: * When a performance related PR is ready, checkout that PR locally. - + * Run ``ti benchmark && ti regression`` to obtain the result. * Decide wheater to approve or request change, depends on the result. diff --git a/python/taichi/main.py b/python/taichi/main.py index 4e18d3b8235d1..22111b7ba6558 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -85,8 +85,10 @@ def display_benchmark_regression(xd, yd): def parse_dat(file): dict = {} for line in open(file).readlines(): - try: a, b = line.strip().split(':') - except: continue + try: + a, b = line.strip().split(':') + except: + continue dict[a.strip()] = int(float(b)) return dict @@ -267,7 +269,9 @@ def main(debug=False): os.chdir(baseline_dir) print('[benchmark] pushing baseline data...') os.system('git add .') - os.system(f"git commit -m 'update baseline for taichi@{ti.core.get_commit_hash()}'") + os.system( + f"git commit -m 'update baseline for taichi@{ti.core.get_commit_hash()}'" + ) os.system('git push') os.chdir(old_cwd) print('[benchmark] baseline data uploaded') @@ -279,9 +283,7 @@ def main(debug=False): print('[benchmark] fetching latest baseline...') os.system('git pull') os.chdir(old_cwd) - display_benchmark_regression( - baseline_dir, - output_dir) + display_benchmark_regression(baseline_dir, output_dir) elif mode == "build": ti.core.build() elif mode == "format": diff --git a/taichi/analysis/verify.cpp b/taichi/analysis/verify.cpp index be02ed1e4f18f..3e56285b03bd8 100644 --- a/taichi/analysis/verify.cpp +++ b/taichi/analysis/verify.cpp @@ -78,12 +78,12 @@ class IRVerifier : public BasicStmtVisitor { TI_ASSERT(stmt->loop); if (stmt->loop->is()) { TI_ASSERT(stmt->loop->as()->task_type == - OffloadedStmt::TaskType::struct_for || - stmt->loop->as()->task_type == - OffloadedStmt::TaskType::range_for); + OffloadedStmt::TaskType::struct_for || + stmt->loop->as()->task_type == + OffloadedStmt::TaskType::range_for); } else { TI_ASSERT(stmt->loop->is() || - stmt->loop->is()); + stmt->loop->is()); } } @@ -92,16 +92,18 @@ class IRVerifier : public BasicStmtVisitor { if (for_stmt->loop_var) { TI_ASSERT(for_stmt->loop_var->is()); TI_ASSERT_INFO(irpass::analysis::gather_statements( - for_stmt->loop_var->parent, - [&](Stmt *s) { - if (auto store = s->cast()) - return store->ptr == for_stmt->loop_var; - else if (auto atomic = s->cast()) { - return atomic->dest == for_stmt->loop_var; - } else { - return false; - } - }).empty(), "loop_var of {} modified", for_stmt->id); + for_stmt->loop_var->parent, + [&](Stmt *s) { + if (auto store = s->cast()) + return store->ptr == for_stmt->loop_var; + else if (auto atomic = s->cast()) { + return atomic->dest == for_stmt->loop_var; + } else { + return false; + } + }) + .empty(), + "loop_var of {} modified", for_stmt->id); } for_stmt->body->accept(this); } diff --git a/taichi/backends/metal/codegen_metal.cpp b/taichi/backends/metal/codegen_metal.cpp index e928264ab04c6..924050d647519 100644 --- a/taichi/backends/metal/codegen_metal.cpp +++ b/taichi/backends/metal/codegen_metal.cpp @@ -454,17 +454,15 @@ class KernelCodegen : public IRVisitor { TI_ASSERT(for_stmt->width() == 1); auto loop_var_name = for_stmt->raw_name(); if (!for_stmt->reversed) { - emit("for (int {}_ = {}; {}_ < {}; {}_ = {}_ + {}) {{", - loop_var_name, for_stmt->begin->raw_name(), - loop_var_name, for_stmt->end->raw_name(), - loop_var_name, loop_var_name, 1); + emit("for (int {}_ = {}; {}_ < {}; {}_ = {}_ + {}) {{", loop_var_name, + for_stmt->begin->raw_name(), loop_var_name, + for_stmt->end->raw_name(), loop_var_name, loop_var_name, 1); emit(" int {} = {}_;", loop_var_name, loop_var_name); } else { // reversed for loop emit("for (int {}_ = {} - 1; {}_ >= {}; {}_ = {}_ - {}) {{", - loop_var_name, for_stmt->end->raw_name(), - loop_var_name, for_stmt->begin->raw_name(), - loop_var_name, loop_var_name, 1); + loop_var_name, for_stmt->end->raw_name(), loop_var_name, + for_stmt->begin->raw_name(), loop_var_name, loop_var_name, 1); emit(" int {} = {}_;", loop_var_name, loop_var_name); } for_stmt->body->accept(this); diff --git a/taichi/backends/metal/shaders/helpers.metal.h b/taichi/backends/metal/shaders/helpers.metal.h index 41cf774576e7f..d897cf12008f1 100644 --- a/taichi/backends/metal/shaders/helpers.metal.h +++ b/taichi/backends/metal/shaders/helpers.metal.h @@ -33,8 +33,9 @@ STR( inline int ifloordiv(int lhs, int rhs) { const int intm = (lhs / rhs); - return (((lhs < 0) != (rhs < 0) && lhs && - (rhs * intm != lhs)) ? (intm - 1) : intm); + return (((lhs < 0) != (rhs < 0) && lhs && (rhs * intm != lhs)) + ? (intm - 1) + : intm); } int32_t pow_i32(int32_t x, int32_t n) { diff --git a/taichi/backends/opengl/codegen_opengl.cpp b/taichi/backends/opengl/codegen_opengl.cpp index 3530be00464d3..b709b7968af3e 100644 --- a/taichi/backends/opengl/codegen_opengl.cpp +++ b/taichi/backends/opengl/codegen_opengl.cpp @@ -414,14 +414,13 @@ class KernelGen : public IRVisitor { emit("{} {} = atan({}, {});", dt_name, bin_name, lhs_name, rhs_name); } return; - } else if (bin->op_type == BinaryOpType::pow - && is_integral(bin->rhs->element_type())) { - // The GLSL `pow` is not so percise for `int`... e.g.: `pow(5, 3)` obtains 124 - // So that we have to use some hack to make it percise. - // Discussion: https://github.com/taichi-dev/taichi/pull/943#issuecomment-626354902 + } else if (bin->op_type == BinaryOpType::pow && + is_integral(bin->rhs->element_type())) { + // The GLSL `pow` is not so percise for `int`... e.g.: `pow(5, 3)` obtains + // 124 So that we have to use some hack to make it percise. Discussion: + // https://github.com/taichi-dev/taichi/pull/943#issuecomment-626354902 emit("{} {} = {}(fast_pow_{}({}, {}));", dt_name, bin_name, dt_name, - data_type_short_name(bin->lhs->element_type()), - lhs_name, rhs_name); + data_type_short_name(bin->lhs->element_type()), lhs_name, rhs_name); used.fast_pow = true; return; } @@ -603,9 +602,9 @@ class KernelGen : public IRVisitor { void visit(LoopIndexStmt *stmt) override { TI_ASSERT(stmt->loop->is() || - (stmt->loop->is() && - stmt->loop->as()->task_type == - OffloadedStmt::TaskType::range_for)); + (stmt->loop->is() && + stmt->loop->as()->task_type == + OffloadedStmt::TaskType::range_for)); TI_ASSERT(stmt->index == 0); // TODO: multiple indices emit("int {} = _itv;", stmt->short_name()); } @@ -614,17 +613,15 @@ class KernelGen : public IRVisitor { TI_ASSERT(for_stmt->width() == 1); auto loop_var_name = for_stmt->raw_name(); if (!for_stmt->reversed) { - emit("for (int {}_ = {}; {}_ < {}; {}_ = {}_ + {}) {{", - loop_var_name, for_stmt->begin->raw_name(), - loop_var_name, for_stmt->end->raw_name(), - loop_var_name, loop_var_name, 1); + emit("for (int {}_ = {}; {}_ < {}; {}_ = {}_ + {}) {{", loop_var_name, + for_stmt->begin->raw_name(), loop_var_name, + for_stmt->end->raw_name(), loop_var_name, loop_var_name, 1); emit(" int {} = {}_;", loop_var_name, loop_var_name); } else { // reversed for loop emit("for (int {}_ = {} - 1; {}_ >= {}; {}_ = {}_ - {}) {{", - loop_var_name, for_stmt->end->raw_name(), - loop_var_name, for_stmt->begin->raw_name(), - loop_var_name, loop_var_name, 1); + loop_var_name, for_stmt->end->raw_name(), loop_var_name, + for_stmt->begin->raw_name(), loop_var_name, loop_var_name, 1); emit(" int {} = {}_;", loop_var_name, loop_var_name); } for_stmt->body->accept(this); @@ -722,12 +719,14 @@ void OpenglCodeGen::lower() { auto ir = kernel_->ir; auto &config = kernel_->program.config; config.demote_dense_struct_fors = true; - auto res = irpass::compile_to_offloads(ir, config, - /*vectorize=*/false, kernel_->grad, - /*ad_use_stack=*/false, config.print_ir, - /*lower_global_access*/true); + auto res = + irpass::compile_to_offloads(ir, config, + /*vectorize=*/false, kernel_->grad, + /*ad_use_stack=*/false, config.print_ir, + /*lower_global_access*/ true); global_tmps_buffer_size_ = res.total_size; - TI_TRACE("[glsl] Global temporary buffer size {} B", global_tmps_buffer_size_); + TI_TRACE("[glsl] Global temporary buffer size {} B", + global_tmps_buffer_size_); #ifdef _GLSL_DEBUG irpass::print(ir); #endif diff --git a/taichi/codegen/codegen_llvm.cpp b/taichi/codegen/codegen_llvm.cpp index 991132c847e36..af4ca90a287ee 100644 --- a/taichi/codegen/codegen_llvm.cpp +++ b/taichi/codegen/codegen_llvm.cpp @@ -808,15 +808,13 @@ void CodeGenLLVM::create_naive_range_for(RangeForStmt *for_stmt) { builder->SetInsertPoint(loop_test); llvm::Value *cond; if (!for_stmt->reversed) { - cond = - builder->CreateICmp(llvm::CmpInst::Predicate::ICMP_SLT, - builder->CreateLoad(loop_var), - llvm_val[for_stmt->end]); + cond = builder->CreateICmp(llvm::CmpInst::Predicate::ICMP_SLT, + builder->CreateLoad(loop_var), + llvm_val[for_stmt->end]); } else { - cond = - builder->CreateICmp(llvm::CmpInst::Predicate::ICMP_SGE, - builder->CreateLoad(loop_var), - llvm_val[for_stmt->begin]); + cond = builder->CreateICmp(llvm::CmpInst::Predicate::ICMP_SGE, + builder->CreateLoad(loop_var), + llvm_val[for_stmt->begin]); } builder->CreateCondBr(cond, body, after_loop); } @@ -1413,13 +1411,13 @@ void CodeGenLLVM::visit(LoopIndexStmt *stmt) { TI_ASSERT(&module->getContext() == tlctx->get_this_thread_context()); if (stmt->loop->is() && stmt->loop->as()->task_type == - OffloadedStmt::TaskType::struct_for) { + OffloadedStmt::TaskType::struct_for) { llvm_val[stmt] = builder->CreateLoad(builder->CreateGEP( current_coordinates, {tlctx->get_constant(0), tlctx->get_constant(0), tlctx->get_constant(stmt->index)})); } else { - llvm_val[stmt] = builder->CreateLoad( - loop_vars_llvm[stmt->loop][stmt->index]); + llvm_val[stmt] = + builder->CreateLoad(loop_vars_llvm[stmt->loop][stmt->index]); } } diff --git a/taichi/ir/ir.h b/taichi/ir/ir.h index 76006210da23b..4810b1de1de87 100644 --- a/taichi/ir/ir.h +++ b/taichi/ir/ir.h @@ -835,7 +835,8 @@ class Block : public IRNode { void insert(VecStatement &&stmt, int location = -1); void replace_statements_in_range(int start, int end, VecStatement &&stmts); void set_statements(VecStatement &&stmts); - void replace_with(Stmt *old_statement, std::unique_ptr &&new_statement, + void replace_with(Stmt *old_statement, + std::unique_ptr &&new_statement, bool replace_usages = true); void insert_before(Stmt *old_statement, VecStatement &&new_statements); void replace_with(Stmt *old_statement, diff --git a/taichi/ir/transforms.h b/taichi/ir/transforms.h index a29cd59f9b1bf..50fcd3862a7e9 100644 --- a/taichi/ir/transforms.h +++ b/taichi/ir/transforms.h @@ -49,14 +49,13 @@ void demote_dense_struct_fors(IRNode *root); void demote_atomics(IRNode *root); void reverse_segments(IRNode *root); // for autograd std::unique_ptr initialize_scratch_pad(StructForStmt *root); -OffloadedResult compile_to_offloads( - IRNode *ir, - const CompileConfig &config, - bool vectorize, - bool grad, - bool ad_use_stack, - bool verbose, - bool lower_global_access = true); +OffloadedResult compile_to_offloads(IRNode *ir, + const CompileConfig &config, + bool vectorize, + bool grad, + bool ad_use_stack, + bool verbose, + bool lower_global_access = true); } // namespace irpass diff --git a/taichi/transforms/compile_to_offloads.cpp b/taichi/transforms/compile_to_offloads.cpp index 263a631d31a34..5339b06646ed2 100644 --- a/taichi/transforms/compile_to_offloads.cpp +++ b/taichi/transforms/compile_to_offloads.cpp @@ -7,14 +7,13 @@ TLANG_NAMESPACE_BEGIN namespace irpass { -OffloadedResult compile_to_offloads( - IRNode *ir, - const CompileConfig &config, - bool vectorize, - bool grad, - bool ad_use_stack, - bool verbose, - bool lower_global_access) { +OffloadedResult compile_to_offloads(IRNode *ir, + const CompileConfig &config, + bool vectorize, + bool grad, + bool ad_use_stack, + bool verbose, + bool lower_global_access) { TI_AUTO_PROF; auto print = [&](const std::string &name) { diff --git a/taichi/transforms/convert_into_loop_index.cpp b/taichi/transforms/convert_into_loop_index.cpp index 142534bd29ffc..ce2a60a51823e 100644 --- a/taichi/transforms/convert_into_loop_index.cpp +++ b/taichi/transforms/convert_into_loop_index.cpp @@ -19,12 +19,14 @@ class ConvertIntoLoopIndexStmt : public BasicStmtVisitor { [&](Stmt *load) { if (auto local_load = load->cast()) { return local_load->width() == 1 && - local_load->ptr[0].var == loop_var && - local_load->ptr[0].offset == 0; + local_load->ptr[0].var == loop_var && + local_load->ptr[0].offset == 0; } return false; }, - [&]() { return Stmt::make(loop, index, is_struct_for); }); + [&]() { + return Stmt::make(loop, index, is_struct_for); + }); } void preprocess_container_stmt(Stmt *stmt) override { @@ -35,7 +37,7 @@ class ConvertIntoLoopIndexStmt : public BasicStmtVisitor { auto leaf = struct_for->snode; for (int i = 0; i < (int)struct_for->loop_vars.size(); i++) { convert(struct_for, struct_for->loop_vars[i], - leaf->physical_index_position[i], true); + leaf->physical_index_position[i], true); struct_for->loop_vars[i] = nullptr; } } diff --git a/taichi/transforms/demote_dense_struct_fors.cpp b/taichi/transforms/demote_dense_struct_fors.cpp index ea459b8907bdb..50c5a70f8c4ca 100644 --- a/taichi/transforms/demote_dense_struct_fors.cpp +++ b/taichi/transforms/demote_dense_struct_fors.cpp @@ -84,7 +84,8 @@ VecStatement convert_to_range_for(StructForStmt *struct_for) { [&](Stmt *s) { if (auto loop_index = s->cast()) { return loop_index->loop == struct_for && - loop_index->index == snodes.back()->physical_index_position[i]; + loop_index->index == + snodes.back()->physical_index_position[i]; } return false; }, diff --git a/taichi/transforms/ir_printer.cpp b/taichi/transforms/ir_printer.cpp index a84076cb0586f..79533f9f8635f 100644 --- a/taichi/transforms/ir_printer.cpp +++ b/taichi/transforms/ir_printer.cpp @@ -259,17 +259,18 @@ class IRPrinter : public IRVisitor { void visit(RangeForStmt *for_stmt) override { print("{} : {}for {} in range({}, {}, step {}) {{", for_stmt->name(), - for_stmt->reversed ? "reversed " : "", for_stmt->loop_var ? - for_stmt->loop_var->name() : "nullptr", + for_stmt->reversed ? "reversed " : "", + for_stmt->loop_var ? for_stmt->loop_var->name() : "nullptr", for_stmt->begin->name(), for_stmt->end->name(), for_stmt->vectorize); for_stmt->body->accept(this); print("}}"); } void visit(StructForStmt *for_stmt) override { - auto loop_vars = make_list( - for_stmt->loop_vars, - [](Stmt *const &stmt) -> std::string { return stmt ? stmt->name() : "nullptr"; }); + auto loop_vars = make_list(for_stmt->loop_vars, + [](Stmt *const &stmt) -> std::string { + return stmt ? stmt->name() : "nullptr"; + }); print("{} : for {} where {} active, step {} {{", for_stmt->name(), loop_vars, for_stmt->snode->get_node_type_name_hinted(), for_stmt->vectorize); @@ -459,7 +460,8 @@ class IRPrinter : public IRVisitor { } void visit(LoopIndexStmt *stmt) override { - print("{}{} = loop {} index {}", stmt->type_hint(), stmt->name(), stmt->loop->name(), stmt->index); + print("{}{} = loop {} index {}", stmt->type_hint(), stmt->name(), + stmt->loop->name(), stmt->index); } void visit(GlobalTemporaryStmt *stmt) override {