Hi I have run into some problem on parameter types but luckily I got it solved. Here I want to ask about how to actively avoid similar issues
@ti.func
def distance(x0, y0, x1, y1, x2, y2):
dist = ((x2-x1)**2+(y2-y1)**2)**0.5
res = ((x2-x0)*(y1-y0)-(y2-y0)*(x1-x0))/dist
return res
@ti.kernel
dist = distance(0,2,3,4,5,6)
In this function why shouldn’t I use ti.sqr & ti.sqrt?
I suppose the ti.sqr should work on Taichi variables, and can 0, 2, … be viewed as ti.var or some Expression in intermediate representation?
The problem is fixed in recent versions (https://github.com/taichi-dev/taichi/pull/539). This is because @ti.func not translating function properly. Please update taichi and see if your problem is gone.
In fact, when you type x * 2, it’s not evaluated immediately. That’s why we need x to be Expr.
What Expr express is not a value, but an instruction.
When we type x * 2, it will call to Expr.__mul__ (operator override), which generates coresponding codes for different backend, take x86_64 for example:
mov eax, 2 # when Expr(2)
mov ebx, [x] # when Expr(x)
mul ebx, eax # when Expr.__mul__ called
Then these codes will be executed directly on the CPU, and thus higher performance than python.
That’s why we need @ti.kernel, it translates python codes to native machine codes.