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

Taichi if statement bug that causes performance issue #8406

Open
FieryAery opened this issue Nov 9, 2023 · 0 comments
Open

Taichi if statement bug that causes performance issue #8406

FieryAery opened this issue Nov 9, 2023 · 0 comments

Comments

@FieryAery
Copy link

FieryAery commented Nov 9, 2023

Describe the bug
The idea is to bypass particle that has 0 mass using if statement, but it turns out that the code will still evaluate the content inside the if statement even when mass is 0. Also, different content inside if statement is generating different execution time when it should have been bypassed by the false condition.

To Reproduce

import taichi as ti
from taichi.math import vec3,vec2
import time
ti.init(arch = ti.gpu, debug=False)
Particle = ti.types.struct(mass=ti.uint32,pos=vec3,vel=vec3,force=vec3,density=float)
bufferP0,bufferP1 = Particle.field(shape=(128,64,32)),Particle.field(shape=(128,64,32))

@ti.kernel
def test_Main_exec():
    for pos in ti.grouped(ti.ndrange(128,64,32)):
        p0,p1 = Particle(),Particle()
        for offset in ti.grouped(ti.ndrange((-1,10),(-1,10),(-1,10))):
            NPos = pos + offset
            incoming0,incoming1 = bufferP0[NPos],bufferP1[NPos]
            if incoming0.mass > 0: #deleting content inside this statement generate different execution time result
                p0.pos = p0.pos*p0.mass + incoming0.pos*incoming0.mass
                p0.mass = incoming0.mass
            if incoming1.mass > 0: #same here
                p1.pos = p1.pos*p1.mass + incoming1.pos*incoming1.mass
                p1.mass = incoming1.mass
        bufferP0[pos],bufferP1[pos]=p0,p1

if __name__ == "__main__":
    start_time = time.time()
    prev_time = 0
    while True:
        test_Main_exec()
        ti.sync()
        end_time = time.time()
        execution_time = (end_time - start_time)*1000
        print(f"Execution time: {execution_time-prev_time:.1f} ms per frame")
        prev_time = execution_time

deleting

p0.mass = incoming0.mass
p1.mass = incoming1.mass

will generate different execution time (60ms to 8ms), which means the content inside if statement is not bypassed. Also 8ms to 60ms is waaaay too much for just a simple p0.mass = incoming0.mass and p1.mass = incoming1.mass.

@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Nov 9, 2023
@FieryAery FieryAery changed the title [perf] Taichi ignoring "if" statement and create devastating performance drop (from 1ms to 200 ms), with a lot of weird behavior Taichi ignoring "if" statement and create devastating performance drop (from 1ms to 200 ms), with a lot of weird behavior Nov 9, 2023
@github-project-automation github-project-automation bot moved this from Untriaged to Done in Taichi Lang Nov 9, 2023
@FieryAery FieryAery reopened this Nov 10, 2023
@FieryAery FieryAery changed the title Taichi ignoring "if" statement and create devastating performance drop (from 1ms to 200 ms), with a lot of weird behavior Taichi if statement bug that causes performance issue Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

1 participant