【发布时间】:2014-02-08 00:43:45
【问题描述】:
我正在使用 _mm_stream_ps 内在函数,但在理解它的性能方面遇到了一些麻烦。
这是我正在使用的代码 sn-p... 流版本:
#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
真正让我感到困惑的是,如果我使用非对齐的加载和存储(即 storeu_ps/loadu_ps),我会看到更好的性能 - 在 Xeon CPU(不是在我的笔记本电脑上):
305.176 MB allocated
Bandwidth (non-temporal): 8.30105 GB/s
Bandwidth: 12.7056 GB/s
由于 y 的冗余负载,我希望流版本比非流版本更快。但是,测量表明流版本实际上比非流版本慢两倍。
你对此有什么解释吗?
使用的编译器:Intel 14.0.1; 编译器标志:-O3 -restrict -xAVX; 使用的 CPU:Intel Xeon E5-2650;
谢谢。
【问题讨论】:
-
无需展开循环。循环展开只对依赖链有用,没有依赖链。 CPU 可以为您解决这个问题。但我有个问题。您的带宽计算中的 3 系数是多少?
-
两次读取+一次写入。尽管非临时版本只读取一次,但我保留了 3 的因子以使比较更简单。
标签: assembly vectorization sse intrinsics avx