gpt4 book ai didi

assembly - 流内在会降低性能

转载 作者:行者123 更新时间:2023-12-04 20:08:56 25 4
gpt4 key购买 nike

我正在尝试使用 _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

真正让我困惑的是,如果我使用非对齐的加载和存储(即 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;

谢谢你。

最佳答案

流变化创建直接到 DRAM 的流水线突发写入。速度应该与您的 DRAM 的速度大致匹配。标准存储写入缓存(但如果数据不在缓存中,它首先将其读入缓存)。如果数据已经在缓存中,标准存储会以缓存写入的速度运行。通常,使用流方法,大小远大于最后一级缓存大小的写入要快得多。使用标准存储,小型写入通常更快。尝试使用几个 GB 的缓冲区大小运行测试。流方法应该更快。

这里有一个基准来证明:

#define __USE_MINGW_ANSI_STDIO 1
#include <stdlib.h>
#include <intrin.h>
#include <windows.h>
#include <stdio.h>
#include <stdint.h>

//-----------------------------------------------------------------------------
//
// queryPerformanceCounter - similar to QueryPerformanceCounter, but returns
// count directly.

uint64_t queryPerformanceCounter (void)
{
LARGE_INTEGER int64;
QueryPerformanceCounter (&int64);
return int64.QuadPart;
}

//-----------------------------------------------------------------------------
//
// queryPerformanceFrequency - same as QueryPerformanceFrequency, but returns count direcly.

uint64_t queryPerformanceFrequency (void)
{
LARGE_INTEGER int64;

QueryPerformanceFrequency (&int64);
return int64.QuadPart;
}

//---------------------------------------------------------------------------

static void testNontemporal (float *x, float *y, uint64_t numberOfVectors)
{
uint64_t i;
for(i = 0; i < numberOfVectors / 2; ++i)
{
_mm_stream_ps(y,_mm_load_ps(x));
_mm_stream_ps(y+4,_mm_load_ps(x+4));
y+=8; x+=8;
}
}

//---------------------------------------------------------------------------

static void testTemporal (float *x, float *y, uint64_t numberOfVectors)
{
uint64_t i;
for(i = 0; i < numberOfVectors / 2; ++i)
{
_mm_store_ps(y,_mm_load_ps(x));
_mm_store_ps(y+4,_mm_load_ps(x+4));
y+=8; x+=8;
}
}

//---------------------------------------------------------------------------

static void runtests (int nonTemporal)
{
uint64_t startCount, elapsed, index;
float *x, *y;
uint64_t numberOfBytes = 400 * 0x100000ull;
uint64_t numberOfFloats = numberOfBytes / sizeof *x;
uint64_t numberOfVectors = numberOfFloats / 4;
double gbPerSecond;

x = _mm_malloc (numberOfBytes, 32);
y = _mm_malloc (numberOfBytes, 32);
if (x == NULL || y == NULL) exit (1);

// put valid floating point data into the source buffer
// to avoid performance penalty
for (index = 0; index < numberOfFloats; index++)
x [index] = (float) index, y [index] = 0;

startCount = queryPerformanceCounter ();
if (nonTemporal)
testNontemporal (x, y, numberOfVectors);
else
testTemporal (x, y, numberOfVectors);
elapsed = queryPerformanceCounter () - startCount;
gbPerSecond = (double) numberOfBytes / 0x40000000 * queryPerformanceFrequency () / elapsed;
printf ("%.2f GB/s\n", gbPerSecond);
_mm_free (x);
_mm_free (y);
}

//---------------------------------------------------------------------------

int main (void)
{
// raise our priority to increase measurement accuracy
SetPriorityClass (GetCurrentProcess (), REALTIME_PRIORITY_CLASS);

printf ("using temporal stores\n");
runtests (0);
printf ("using non-temporal stores\n");
runtests (1);
return 0;
}

//---------------------------------------------------------------------------

英特尔酷睿 i7-2600K 的输出:
using temporal stores
5.57 GB/s
using non-temporal stores
8.35 GB/s

关于assembly - 流内在会降低性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21207023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com