taichi icon indicating copy to clipboard operation
taichi copied to clipboard

Question on euler2d example.

Open jorge-ortega opened this issue 8 months ago • 4 comments

I'm going through euler2d example, and I'm having trouble understanding how accessing elements in the field works. There doesn't seem to be any checks to ensure correct access when i or j equal 0, or the dimension length. But the example works without error. How does the example ensure these access work?

https://github.com/taichi-dev/taichi/blob/19672ccace254409f2bd4698b436df8d78e5b80a/python/taichi/examples/simulation/eulerfluid2d.py#L103-L106

jorge-ortega avatar Apr 27 '25 22:04 jorge-ortega

Maybe it's a bug? If you change the init to:

ti.init(arch=ti.cpu, debug=True)

then the result is:

  File "/Users/hugh/git/taichi-play/euler_fluid.py", line 303, in <module>
    voricity_step()
  File "/Users/hugh/git/taichi-play/euler_fluid.py", line 276, in voricity_step
    curl(velocities_pair.cur, curlField)
  File "/Users/hugh/git/taichi/python/taichi/lang/kernel_impl.py", line 1117, in wrapped
    raise type(e)("\n" + str(e)) from None
taichi.lang.exception.TaichiAssertionError:
(kernel=curl_c84_0) Accessing field (S5place<f32>) of size (512, 512) with indices (-1, 0)

Note that on gpu there is NO bounds checking. The bounds checking is: did the program crash? 😅

hughperkins avatar Apr 28 '25 09:04 hughperkins

Thanks for the response. Wasn't aware of the debug flag. However, I did try the same access pattern without the debug flag in the python console and got cuda illegal memory access errors. This does sound like a logic bug with UB when running without any debug assertions.

jorge-ortega avatar Apr 28 '25 19:04 jorge-ortega

My best guess is these accesses should have gone through the _with_boundary functions. The pressure_projection kernel does this already.

jorge-ortega avatar Apr 28 '25 20:04 jorge-ortega

Yeah, e.g. something like c_with_boundary, https://github.com/taichi-dev/taichi/blob/19672ccace254409f2bd4698b436df8d78e5b80a/python/taichi/examples/simulation/eulerfluid2d.py#L194 perhaps ?

hughperkins avatar Apr 28 '25 22:04 hughperkins