# Forward Dependency Example

This example demonstrates the declaration of a Structure of Arrays (SoA) interacting with a forward dependency.

    #include <stdio.h>
    #include <sdlt/primitive.h>
    #include <sdlt/soa1d_container.h>

    #define N 1024

    typedef struct RGBs {
        float r;
        float g;
        float b;
    } RGBTy;

    SDLT_PRIMITIVE(RGBTy, r, g, b)

    void main()
    {
        // RGBTy a[N];  // AOS data layout

        sdlt::soa1d_container<RGBTy> aContainer(N);
        auto a = aContainer.access();    // SOA data layout

        // use SDLT access method to access struct members r, g, and b.
        // with unit-stride access  after vectorization
        #pragma omp simd
        for (int k = 0; k<N; k++) {
            a[k].r() = k*1.5;
            a[k].g() = k*2.5;
            a[k].b() = k*3.5;
        }

        // Test forward-dependency on SOA memory access
        #pragma omp simd
        for (int i = 0; i<N - 1; i++) {
            sdlt::linear_index k(i);
            a[k].r() = a[k + 1].r() + k*1.5;
            a[k].g() = a[k + 1].g() + k*2.5;
            a[k].b() = a[k + 1].b() + k*3.5;
        }
        std::cout << "k =" << 10 <<
            ", a[k].r =" << a[10].r() <<
            ", a[k].g =" << a[10].g() <<
            ", a[k].b =" << a[10].b() << std::endl;
    }

Parent topic: Examples