使用SIMD的SOA和AOS不会提高性能

时间:2019-04-21 18:19:26

标签: c++ performance visual-c++ sse simd

我正在关注SOA与AOS的本教程,并尝试在Visual Studio 2017上编写测试用例。我关闭了所有编译器方面的优化。我确保数据是与__declspec属性对齐的16个字节。以下代码有什么问题?

#include<xmmintrin.h>
#include<iostream>

constexpr int SIZE = 1000000;
//AOS
struct AOS
{
    float a, b, c;
};

//SOA
__declspec(align(16))
struct SOA
{
    float a[SIZE];
    float b[SIZE];
    float c[SIZE];
};

void AddSIMD(float *a, float *b, float * c, float * results, int count)
{
    for (int i = 0; i < count; i += 4)
    {
        __m128 result, t1,t2; 
        result = _mm_load_ps(&a[i]); 
        t1 = _mm_load_ps(&b[i]);
        t2 = _mm_load_ps(&c[i]);
        result = _mm_add_ps(result, t1);
        result = _mm_add_ps(result, t2);
        _mm_store_ps(&results[i], result);
    }
}

void Add(AOS* data, int count,float* results)
{
    for (int i = 0; i < count; ++i)
    {
        results[i] = data[i].a + data[i].b + data[i].c;
    }
}


int main()
{
    SOA soa;
    AOS aos[SIZE];

    for (int i = 0; i < SIZE; ++i)
    {
        soa.a[i] = aos[i].a = rand() % 100;
        soa.b[i] = aos[i].b = rand() % 100;
        soa.c[i] = aos[i].c = rand() % 100;
    }
    float resultsSIMD[SIZE];
    float results[SIZE];

    AddSIMD(soa.a,soa.b,soa.c,resultsSIMD,SIZE);
    Add(aos,SIZE,results);

    return 0;
}

0 个答案:

没有答案