gpt4 book ai didi

arrays - 是否可以使用 SIMD 对 C 中的非平凡循环进行矢量化? (复用一个输入的多长度 5 double 点积)

转载 作者:行者123 更新时间:2023-12-02 16:01:10 28 4
gpt4 key购买 nike

我有一个性能关键的 C 代码,其中 > 90% 的时间都花在了一个基本操作上:

Operation

我使用的C代码是:

static void function(double *X1, double *Y1, double *X2, double *Y2, double *output) {
double Z1, Z2;
int i, j, k;
for (i = 0, j = 0; i < 25; j++) { // sweep Y
Z1 = 0;
Z2 = 0;
for (k = 0; k < 5; k++, i++) { // sweep X
Z1 += X1[k] * Y1[i];
Z2 += X2[k] * Y2[i];
}
output[j] = Z1*Z2;
}
}

长度是固定的(X 为 5;Y 为 25;输出为 5)。我已经尝试了我所知道的一切来使它更快。当我使用带有 -O3 -march=native -Rpass-analysis=loop-vectorize -Rpass=loop-vectorize -Rpass-missed=loop-vectorize 的 clang 编译此代码时,我收到此消息:

remark: the cost-model indicates that vectorization is not beneficial [-Rpass-missed=loop-vectorize]

但我认为加快速度的方法是以某种方式使用 SIMD。如有任何建议,我们将不胜感激。

最佳答案

尝试以下版本,它需要 SSE2 和 FMA3。未经测试。

void function_fma( const double* X1, const double* Y1, const double* X2, const double* Y2, double* output )
{
// Load X1 and X2 vectors into 6 registers; the instruction set has 16 of them available, BTW.
const __m128d x1_0 = _mm_loadu_pd( X1 );
const __m128d x1_1 = _mm_loadu_pd( X1 + 2 );
const __m128d x1_2 = _mm_load_sd( X1 + 4 );

const __m128d x2_0 = _mm_loadu_pd( X2 );
const __m128d x2_1 = _mm_loadu_pd( X2 + 2 );
const __m128d x2_2 = _mm_load_sd( X2 + 4 );

// 5 iterations of the outer loop
const double* const y1End = Y1 + 25;
while( Y1 < y1End )
{
// Multiply first 2 values
__m128d z1 = _mm_mul_pd( x1_0, _mm_loadu_pd( Y1 ) );
__m128d z2 = _mm_mul_pd( x2_0, _mm_loadu_pd( Y2 ) );

// Multiply + accumulate next 2 values
z1 = _mm_fmadd_pd( x1_1, _mm_loadu_pd( Y1 + 2 ), z1 );
z2 = _mm_fmadd_pd( x2_1, _mm_loadu_pd( Y2 + 2 ), z2 );

// Horizontal sum both vectors
z1 = _mm_add_sd( z1, _mm_unpackhi_pd( z1, z1 ) );
z2 = _mm_add_sd( z2, _mm_unpackhi_pd( z2, z2 ) );

// Multiply + accumulate the last 5-th value
z1 = _mm_fmadd_sd( x1_2, _mm_load_sd( Y1 + 4 ), z1 );
z2 = _mm_fmadd_sd( x2_2, _mm_load_sd( Y2 + 4 ), z2 );

// Advance Y pointers
Y1 += 5;
Y2 += 5;

// Compute and store z1 * z2
z1 = _mm_mul_sd( z1, z2 );
_mm_store_sd( output, z1 );

// Advance output pointer
output++;
}
}

可以通过使用 AVX 进一步微优化,但我不确定它会有多大帮助,因为内循环太短了。我认为这两个额外的 FMA 指令比计算 32 字节 AVX vector 的水平和的开销更小。

更新:这是另一个版本,它总体上需要更少的指令,但代价是几次随机播放。对于您的用例,可能不会更快。需要 SSE 4.1,但我认为所有具有 FMA3 的 CPU 也都具有 SSE 4.1。

void function_fma_v2( const double* X1, const double* Y1, const double* X2, const double* Y2, double* output )
{
// Load X1 and X2 vectors into 5 registers
const __m128d x1_0 = _mm_loadu_pd( X1 );
const __m128d x1_1 = _mm_loadu_pd( X1 + 2 );
__m128d xLast = _mm_load_sd( X1 + 4 );

const __m128d x2_0 = _mm_loadu_pd( X2 );
const __m128d x2_1 = _mm_loadu_pd( X2 + 2 );
xLast = _mm_loadh_pd( xLast, X2 + 4 );

// 5 iterations of the outer loop
const double* const y1End = Y1 + 25;
while( Y1 < y1End )
{
// Multiply first 2 values
__m128d z1 = _mm_mul_pd( x1_0, _mm_loadu_pd( Y1 ) );
__m128d z2 = _mm_mul_pd( x2_0, _mm_loadu_pd( Y2 ) );

// Multiply + accumulate next 2 values
z1 = _mm_fmadd_pd( x1_1, _mm_loadu_pd( Y1 + 2 ), z1 );
z2 = _mm_fmadd_pd( x2_1, _mm_loadu_pd( Y2 + 2 ), z2 );

// Horizontal sum both vectors while transposing
__m128d res = _mm_shuffle_pd( z1, z2, _MM_SHUFFLE2( 0, 1 ) ); // [ z1.y, z2.x ]
// On Intel CPUs that blend SSE4 instruction doesn't use shuffle port,
// throughput is 3x better than shuffle or unpack. On AMD they're equal.
res = _mm_add_pd( res, _mm_blend_pd( z1, z2, 0b10 ) ); // [ z1.x + z1.y, z2.x + z2.y ]

// Load the last 5-th Y values into a single vector
__m128d yLast = _mm_load_sd( Y1 + 4 );
yLast = _mm_loadh_pd( yLast, Y2 + 4 );

// Advance Y pointers
Y1 += 5;
Y2 += 5;

// Multiply + accumulate the last 5-th value
res = _mm_fmadd_pd( xLast, yLast, res );

// Compute and store z1 * z2
res = _mm_mul_sd( res, _mm_unpackhi_pd( res, res ) );
_mm_store_sd( output, res );
// Advance output pointer
output++;
}
}

关于arrays - 是否可以使用 SIMD 对 C 中的非平凡循环进行矢量化? (复用一个输入的多长度 5 double 点积),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70652936/

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