taichi icon indicating copy to clipboard operation
taichi copied to clipboard

Do not the Kernel arguments support the class 'bool'?

Open 17150934 opened this issue 2 years ago • 2 comments

When I set the type-hinted of my Kernel arguments as 'bool', there existed an error: TaichiSyntaxError: Invalid type annotation (argument 0) of Taichi kernel: <class 'bool'> So, do not the Kernel arguments support the class 'bool'?

17150934 avatar Sep 11 '22 09:09 17150934

Thanks for the issue. Currently bool type is not (explicitly) one of the primitive types in Taichi. There is a u1 type, which is used as bool. A workaround is to use int (or other integer types) to mimic its behavior.

@strongoier I noticed that the u1 type is defined in C++ but not exposed to the Python frontend (used in export_lang.cpp but not referenced in primitive_types.py). Should we use it when encountering the bool type during compilation?

AD1024 avatar Sep 12 '22 17:09 AD1024

u1 type has not been fully supported yet (https://github.com/taichi-dev/taichi/issues/577), and we plan to support it in the future. For now we need to use int as a workaround.

strongoier avatar Sep 15 '22 06:09 strongoier

As a first step towards adding the proper bool type, we're going to add bool as an alias of i32.

Specifically:

  • x: bool is equivalent to x: i32
  • -> bool is equivalent to -> i32
  • bool(x) is equivalent to ti.cast(x, i32).

This is only a temporary workaround as we work on a more permanent solution.

re-xyr avatar Nov 26 '22 07:11 re-xyr