Replies: 4 comments 7 replies
-
Wow, thank you for this very detailed report!
Not really. While using
Note that by default, these augassignments on global fields are atomic (see https://docs.taichi-lang.org/lang/articles/operator#supported-atomic-operations). So by rewriting them into local variables, you can avoid this problem. Taichi does try to demote unnecessary atomics during JIT if it can prove that it is safe to do so. This might need to take a look.
That is an interesting idea! cc @lin-hitonami who is working on the real function support.
Ouch. Do you have a concrete repro :-( We will look into this. |
Beta Was this translation helpful? Give feedback.
-
BTW if you want a succinct issue (perhaps I should open this if you think it's valid). THIS IS VERY SLOW But passing the deconstructed struct is much faster. I'm not sure if this is expected or not @k-ye
|
Beta Was this translation helpful? Give feedback.
-
Also regarding performance. I see significantly better performance in macOS with https://github.com/taichi-dev/taichi/blob/master/python/taichi/examples/rendering/cornell_box.py in vulkan vs metal. |
Beta Was this translation helpful? Give feedback.
-
So back to one of my original questions. Is a field of a compound struct type stored internally as a structure of arrays? I.e
|
Beta Was this translation helpful? Give feedback.
-
Hi there. First of all congrats on the 1.0 release! I was inspired to take another look at my Ray Tracing in one Weekend repo and had some thoughts on optimizing kernel code. In fact it would be interesting to post a longer post on doing RTOW in taichi if I had the time, but wanted to at least write down some thoughts and get feedback.
(Of note I mainly test on a MacBook Pro, using metal - more on that later. I have not tested on CPU much and CUDA less so, but more so on Vulkan)
Object Oriented code
This is more of style thing. If you like for your code to be object oriented using the
@ti.data_oriented
is an insignificant overhead. Of course you'll want to store some data in your objects, so let's take the case of an array of Spheres:Representing this optimally in Taichi - Array of structs or struct of arrays
Now there are few ways we could move the above data structure to taichi. Basically it comes down to which you prefer. I found little difference in performance. One is to use two arrays:
This last part is important. If you are going to use the center and radius of the sphere together (probably a good bet in this case) this will put the center[I] and radius[I] next to each other.
The other alternative I've found is using structs:
Of course if you want to associate functions with the struct above you can wrap it in a class:
Optimizing memory usage of arrays of structs
Both of the above ways, Structs of arrays and an Array of Structs are roughly equivalent performance! Why is this? Well if you look closely at the array of structs
spheres = sphere_type.field(shape(n,))
it appears that this is internally just a structure of arrays! (Devs is this true?). For example I can do bulk operations on one array in the struct.spheres.center.from_numpy(...)
Caveat on performance
There is a large BUT here though. I have found that dereferencing a struct object after getting it is slower in multiple parts of the code. Take for example.
Catch what is happening here? The above code is clean but doesn't behave nicely from a memory standpoint. Multiple threads will be addressing different points in the spheres array at the same time. This is a trivial example but in complex code it matters more. A better solution is something like:
Again, developers please correct me if I'm wrong but this seems to be what's happening of why this would affect performance.
Wrapping up the object oriented design
This is more a matter of style but here's how I like to keep my object oriented data structures somewhat pythonic and performing well. The key thing to note here is that you can override python "magic methods" with ti.funcs!
A couple random notes on performance
The latter two points are probably worth a developer commenting on! Let me know if you have other questions.
Beta Was this translation helpful? Give feedback.
All reactions