Struct of arrays
-
Wednesday, May 09, 2012 10:24 AM
I have read several places that implementing a memory layout with a struct of arrays is much more efficient than using an array of struct.
Im implementing the lattice boltzmann method for cfd using c++ amp and have some issues with this.
My current layout is as follows
struct grid_cell { // int blocked; // Define if blocked float n; // North float ne; // North-East float e; // East float se; // South-East float s; float sw; float w; float nw; float c; // Center }; array<struct grid_cell, 1> amp_node(n_nodes, node.begin(), node.end(), av); array<struct grid_cell, 1> amp_tmp(n_nodes, tmp_node.begin(), tmp_node.end(), av); array<int, 1> amp_obst(n_nodes, obstacle.begin(), obstacle.end(), av);The amp_obst was moved from inside the grid_cell structure to its own array, and this had a speed improvement. The most obious way i see is to create an array for each of the elements in struct, but this results in 19 arrays, and i remember there was limit of the number of arrays that could be used by the accelerator. This also creates ugly code, and if i were to implement this for 3 dimensions it would result in 39 arrays.
Any suggestions to how i can implement this as a struct of arrays instead of the current array of structs?
All Replies
-
Thursday, May 10, 2012 3:05 AM
Hi kristoffer_t,
Please take a look at this blog post http://blogs.msdn.com/b/nativeconcurrency/archive/2012/04/16/data-under-the-covers-in-c-amp.aspx. On a device that supports DX feature level 11.1, there could is 64 UAV and 128 SRV. For a device that supports DX feature level 11.0, there is 8 UAV and 128 SRV. I'd assume that you do not need to write to all 39 arrays. For most of them, you probably only need to read from. For these array that you only read from, you could consider capturing them by const reference, so they will be mapped to SRV, thus you have plenty of them. The blog post I metioned has more details on the mapping.
Thanks,
Weirong
- Proposed As Answer by DanielMothMicrosoft Employee, Owner Friday, May 11, 2012 6:07 AM
- Marked As Answer by DanielMothMicrosoft Employee, Owner Thursday, May 17, 2012 3:58 AM

