cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

arsenm
Adept III

Array of struct vs. Struct of array

I'm a bit unsure of the most efficient way to handle a large collection of structs. There are 2 different ways of do it:

Struct of arrays - create a separate buffer for each field

Array of structs - one buffer with the struct as elements

 

Looking around I've seen some people (mostly talking about Nvidia) saying that it's almost always better to use a struct of arrays. Would this not be true if your struct is one of the fetch sizes (e.g. 128 bits)?

Suppose you have a small struct with a few fields that you will need in close succession and have the same access pattern, and is either 64 or 128 bits wide. Successive threads are reading neighboring structs. For memory coalescing, which approach would be better?


0 Likes
1 Reply
rick_weber
Adept II

I think the argument is that you generally don't access every field in a struct in a tight loop so you're wasting bandwidth loading lesser used elements. With a struct of arrays, you only load the elements you need as you need them.

If this assumption isn't true and you use every element in a struct in the critical path, then it doesn't really matter which way you do it.

0 Likes