Answered Struct of arrays

  • Wednesday, May 09, 2012 10:24 AM
     
      Has Code

    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