Structure of Arrays vs Array of Structures in CUDA

前端 未结 3 984
野性不改
野性不改 2020-11-27 11:25

From some comments that I have read in here, for some reason it is preferable to have Structure of Arrays (SoA) over Array of Structures

3条回答
  •  我在风中等你
    2020-11-27 12:09

    SoA is effectly good for SIMD processing. For several reason, but basically it's more efficient to load 4 consecutive floats in a register. With something like:

     float v [4] = {0};
     __m128 reg = _mm_load_ps( v );
    

    than using:

     struct vec { float x; float, y; ....} ;
     vec v = {0, 0, 0, 0};
    

    and create an __m128 data by accessing all member:

     __m128 reg = _mm_set_ps(v.x, ....);
    

    if your arrays are 16-byte aligned data load/store are faster and some op can be perform directly in memory.

提交回复
热议问题