Taichi: [Lang] Make tensor indicing automatically cast to int32

Created on 30 Jun 2020  路  2Comments  路  Source: taichi-dev/taichi

Concisely describe the proposed feature
It seems that we can automatically cast range into int32?

import taichi as ti

@ti.kernel
def func():
    n = 2.6  # float!!!
    for i in range(n):  # range(f32) = range(int(f32))
        print(i)

func()

```
[Taichi] mode=release
[Taichi] version 0.6.14, llvm 10.0.0, commit c83a7e3d, python 3.8.3
0
1

It's really useful! ...according to my experience in working with Taichi THREE.

But would it also be great to **automatically cast tensor index into int32**? e.g., the GAMES 201 advocated example:
```py
@ti.kernel
def semi_lagragian():
  for pos in ti.grouped(img):
    bt_pos = pos * dx - vel[pos]  # now bt_pos is vector of float32!
    new_img[pos] = img[int(round(bt_pos / dx))]  # we have to convert `int` since round returns float32!

See? It would be useful to simply cast all indices into int32.

Describe the solution you'd like (if any)
Either in python/lang/transform.py or impl.subscript, add a expr_group.cast(ti.i32).

Additional comments
Or we may give a warning when non-int range/index is used?

feature request good first issue python simplification

Most helpful comment

SGTM, let's give a warning for both them but not crash, so that user can rid these warning when they're final-polishing their program and seek possible bugs.

All 2 comments

Or we may give a warning when non-int range/index is used?

I actually think this is better. We'd better not make the system too smart. Explicit is better than implicit.

SGTM, let's give a warning for both them but not crash, so that user can rid these warning when they're final-polishing their program and seek possible bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yuanming-hu picture yuanming-hu  路  4Comments

kazimuth picture kazimuth  路  4Comments

yuanming-hu picture yuanming-hu  路  3Comments

zdxpan picture zdxpan  路  3Comments

liaopeiyuan picture liaopeiyuan  路  3Comments