desempenho de SSE e AVX quando ambos largura de banda de memória limitada
No código abaixo mudei o "dataLen" e obtive eficiência diferente.
dataLen = 400 SSE tempo: 758000 nos AVX tempo: 483000 nos SSE> AVX
dataLen = 2400 tempo SSE: 4212000 nos Tempo AVX: 2636000 us SSE> AVX
dataLen = 2864 tempo SSE: 6115000 nos AVX time: 6146000 us SSE ~ = AVX
dataLen = 3200 tempo SSE: 8049000 nos Tempo AVX: 9297000 us SSE <AVX
dataLen = 4000 SSE tempo: 10170000us tempo AVX: 11690000us SSE <AVX
O código SSE e AVX pode ser simplificado para isso: buf3 [i] + = buf1 [1] * buf2 [i];
#include "testfun.h"
#include <iostream>
#include <chrono>
#include <malloc.h>
#include "immintrin.h"
using namespace std::chrono;
void testfun()
{
int dataLen = 4000;
int N = 10000000;
float *buf1 = reinterpret_cast<float*>(_aligned_malloc(sizeof(float)*dataLen, 32));
float *buf2 = reinterpret_cast<float*>(_aligned_malloc(sizeof(float)*dataLen, 32));
float *buf3 = reinterpret_cast<float*>(_aligned_malloc(sizeof(float)*dataLen, 32));
for(int i=0; i<dataLen; i++)
{
buf1[i] = 1;
buf2[i] = 1;
buf3[i] = 0;
}
//=========================SSE CODE=====================================
system_clock::time_point SSEStart = system_clock::now();
__m128 p1, p2, p3;
for(int j=0; j<N; j++)
for(int i=0; i<dataLen; i=i+4)
{
p1 = _mm_load_ps(&buf1[i]);
p2 = _mm_load_ps(&buf2[i]);
p3 = _mm_load_ps(&buf3[i]);
p3 = _mm_add_ps(_mm_mul_ps(p1, p2), p3);
_mm_store_ps(&buf3[i], p3);
}
microseconds SSEtimeUsed = duration_cast<milliseconds>(system_clock::now() - SSEStart);
std::cout << "SSE time used: " << SSEtimeUsed.count() << " us, " <<std::endl;
//=========================AVX CODE=====================================
for(int i=0; i<dataLen; i++) buf3[i] = 0;
system_clock::time_point AVXstart = system_clock::now();
__m256 pp1, pp2, pp3;
for(int j=0; j<N; j++)
for(int i=0; i<dataLen; i=i+8)
{
pp1 = _mm256_load_ps(&buf1[i]);
pp2 = _mm256_load_ps(&buf2[i]);
pp3 = _mm256_load_ps(&buf3[i]);
pp3 = _mm256_add_ps(_mm256_mul_ps(pp1, pp2), pp3);
_mm256_store_ps(&buf3[i], pp3);
}
microseconds AVXtimeUsed = duration_cast<milliseconds>(system_clock::now() - AVXstart);
std::cout << "AVX time used: " << AVXtimeUsed.count() << " us, " <<std::endl;
_aligned_free(buf1);
_aligned_free(buf2);
}
meu cpu é Intel Xeon E3-1225 v2, que tem um cache L1 32KB * 4 (4 core), ao executar este código, ele usa apenas 1 núcleo, então o cache L1 usado é 32KB.
buf1 buf2 e buf3 é pequeno o suficiente para ser localizado em cache L1 e cache L2 (cache L2 1MB). Ambas as opções SSE e AVX têm largura de banda limitada, mas com o aumento de dataLen, por que o AVX precisa de mais tempo que SSE?