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

[Lang] Support printing lists and tuples in Taichi-scope #1796

Merged
merged 6 commits into from
Aug 29, 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
18 changes: 15 additions & 3 deletions python/taichi/lang/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,26 @@ def entry2content(var):
else:
return Expr(var).ptr

def list_ti_repr(var):
yield '[' # distinguishing tuple & list will increase maintainance cost
for i, v in enumerate(var):
if i:
yield ', '
yield v
yield ']'

def vars2entries(vars):
for var in vars:
if hasattr(var, '__ti_repr__'):
repr = var.__ti_repr__()
for v in vars2entries(repr):
yield v
res = var.__ti_repr__()
elif isinstance(var, (list, tuple)):
res = list_ti_repr(var)
else:
yield var
continue

for v in vars2entries(res):
yield v

def add_separators(vars):
for i, var in enumerate(vars):
Expand Down
1 change: 1 addition & 0 deletions python/taichi/lang/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def mul(a, b):
@binary
def mod(a, b):
def expr_python_mod(a, b):
# a % b = (a // b) * b - a
quotient = Expr(ti_core.expr_floordiv(a, b))
multiply = Expr(ti_core.expr_mul(b, quotient.ptr))
return ti_core.expr_sub(a, multiply.ptr)
Expand Down
10 changes: 2 additions & 8 deletions taichi/python/export_lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,14 +574,8 @@ void export_lang(py::module &m) {

m.def("host_arch", host_arch);

m.def("set_lib_dir", [&](const std::string &dir) {
TI_INFO("set_lib_dir: [{}]", dir);
compiled_lib_dir = dir;
});
m.def("set_tmp_dir", [&](const std::string &dir) {
TI_INFO("set_tmp_dir: [{}]", dir);
runtime_tmp_dir = dir;
});
m.def("set_lib_dir", [&](const std::string &dir) { compiled_lib_dir = dir; });
m.def("set_tmp_dir", [&](const std::string &dir) { runtime_tmp_dir = dir; });
m.def("get_runtime_dir", get_runtime_dir);

m.def("get_commit_hash", get_commit_hash);
Expand Down
5 changes: 4 additions & 1 deletion taichi/transforms/type_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ class TypeCheck : public IRVisitor {

void visit(IfStmt *if_stmt) {
// TODO: use DataType::u1 when it's supported
TI_ASSERT(if_stmt->cond->ret_type.data_type == DataType::i32);
TI_ASSERT_INFO(
if_stmt->cond->ret_type.data_type == DataType::i32,
"`if` conditions must be of type int32, consider using `if x != 0:` "
"instead of `if x:` for float values.");
if (if_stmt->true_statements)
if_stmt->true_statements->accept(this);
if (if_stmt->false_statements) {
Expand Down
21 changes: 21 additions & 0 deletions tests/python/test_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ def func(k: ti.f32):
ti.sync()


@ti.test()
def test_print_list():
x = ti.Matrix.field(2, 3, dtype=ti.f32, shape=(2, 3))
y = ti.Vector.field(3, dtype=ti.f32, shape=())

@ti.kernel
def func(k: ti.f32):
w = [k, x.shape]
print(w + [y.n]) # [233.3, [2, 3], 3]
print(x.shape) # [2, 3]
print(y.shape) # []
z = (1, )
print([1, k**2, k + 1]) # [1, 233.3, 234.3]
print(z) # [1]
print([y[None], z]) # [[0, 0, 0], [1]]
print([]) # []

func(233.3)
ti.sync()


@ti.test(arch=ti.cpu)
def test_python_scope_print_field():
x = ti.Matrix.field(2, 3, dtype=ti.f32, shape=())
Expand Down