我正在玩_mm_stream_ps内在,我在理解其性能方面遇到了一些麻烦。流内在降低性能
这里是我工作的代码片段... 流版本:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <omp.h>
#include <immintrin.h>
#define NUM_ELEMENTS 10000000L
static void copy_temporal(float* restrict x, float* restrict y)
{
for(uint64_t i = 0; i < NUM_ELEMENTS/2; ++i){
_mm_store_ps(y,_mm_load_ps(x));
_mm_store_ps(y+4,_mm_load_ps(x+4));
x+=8;
y+=8;
}
}
static void copy_nontemporal(float* restrict x, float* restrict y)
{
for(uint64_t i = 0; i < NUM_ELEMENTS/2; ++i){
_mm_stream_ps(y,_mm_load_ps(x));
_mm_stream_ps(y+4,_mm_load_ps(x+4));
x+=8;
y+=8;
}
}
int main(int argc, char** argv)
{
uint64_t sizeX = sizeof(float) * 4 * NUM_ELEMENTS;
float *x = (float*) _mm_malloc(sizeX,32);
float *y = (float*) _mm_malloc(sizeX,32);
//initialization
for(uint64_t i = 0 ; i < 4 * NUM_ELEMENTS; ++i){
x[i] = (float)rand()/RAND_MAX;
y[i] = 0;
}
printf("%g MB allocated\n",(2 * sizeX)/1024.0/1024.0);
double start = omp_get_wtime();
copy_nontemporal(x, y);
double time = omp_get_wtime() - start;
printf("Bandwidth (non-temporal): %g GB/s\n",((3 * sizeX)/1024.0/1024.0/1024.0)/time);
start = omp_get_wtime();
copy_temporal(x, y);
time = omp_get_wtime() - start;
printf("Bandwidth: %g GB/s\n",((3 * sizeX)/1024.0/1024.0/1024.0)/time);
_mm_free(x);
_mm_free(y);
return 0;
}
性能测试结果:
2.3 GHz Core i7 (I7-3615QM) (Laptop):
305.176 MB allocated
Bandwidth (non-temporal): 24.2242 GB/s
Bandwidth: 21.4136 GB/s
Xeon(R) CPU E5-2650 0 @ 2.00GHz (cluster (exclusive job)):
305.176 MB allocated
Bandwidth (non-temporal): 8.33133 GB/s
Bandwidth: 8.20684 GB/s
真正困扰我的是,我看到更好的性能 - - 在Xeon CPU上(不在我的笔记本电脑上) - 如果我使用非对齐的加载和存储(即storeu_ps/loadu_ps):
305.176 MB allocated
Bandwidth (non-temporal): 8.30105 GB/s
Bandwidth: 12.7056 GB/s
由于y的冗余负载,我期望流版本比非流版本更快。但是,测量结果显示,流版本实际上比非流版本慢两倍。
你对此有任何解释吗?使用编译器:Intel 14.0.1;使用编译器:Intel 14.0.2;使用编译器:Intel 14.0.1;使用编译器:Intel 14.0.1;使用编译器:Intel 14.0.1; 编译器标志:-O3 -restrict -xAVX; 使用的CPU:Intel Xeon E5-2650;
谢谢。
没有必要展开循环。循环展开只在依赖关系链中有用,并且没有依赖关系链。 CPU可以帮你处理这个问题。但我有个问题。在你的带宽计算中,3是什么因素? –
两次读取+一次写入。即使非时间版本只做一次阅读,我仍然保留三个因子来简化比较。 – user1829358