You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AVX2点积函数多次运行性能波动的原因排查

AVX2点积计算器性能波动排查问题

问题概述

开发基于AVX2的点积计算器时,发现同一程序多次运行的耗时存在显著差异。初始怀疑数据对齐问题,但将数据对齐到4K边界后问题仍存在;且数据量极小,完全适配L1缓存。测试程序单次运行耗时在2.3-3.0秒,将迭代次数提升10倍后,耗时波动范围变为23-28秒,远超预期。

测试Intel MKL的cblas_sdot函数时,同样存在耗时波动(2.7-3.3秒)。

硬件与运行环境

  • CPU:禁用E核的Core i7-12800H(仅运行Golden Cove P核)
  • 系统:Linux kernel 6.11
  • CPU governor:设置为performance模式,运行时CPU稳定在4.7-4.8GHz,排除CPU节流可能;此前其他代码的基准测试波动极小。

perf stat 结果对比

同一可执行文件的两组perf stat结果显示,指令数相同但周期数存在明显差异:

快速运行(耗时2.3秒)

Performance counter stats for './t':

           2328.22 msec task-clock                       #    1.000 CPUs utilized
                32      context-switches                 #   13.744 /sec
                 6      cpu-migrations                   #    2.577 /sec
                65      page-faults                      #   27.918 /sec
       10783895775      cpu_core/cycles/                 #    4.632 GHz
       33906242851      cpu_core/instructions/           #   14.563 G/sec
        1701192939      cpu_core/branches/               #  730.685 M/sec
             18621      cpu_core/branch-misses/          #    7.998 K/sec

       2.329005509 seconds time elapsed

       2.328711000 seconds user
       0.000000000 seconds sys

慢速运行(耗时2.9秒)

Performance counter stats for './t':

           2855.76 msec task-clock                       #    1.000 CPUs utilized
                26      context-switches                 #    9.104 /sec
                 0      cpu-migrations                   #    0.000 /sec
                66      page-faults                      #   23.111 /sec
       13620027356      cpu_core/cycles/                 #    4.769 GHz
       33907381511      cpu_core/instructions/           #   11.873 G/sec
        1701481151      cpu_core/branches/               #  595.806 M/sec
             19152      cpu_core/branch-misses/          #    6.706 K/sec

       2.856294128 seconds time elapsed

       2.856142000 seconds user
       0.000000000 seconds sys

编译环境

分别使用以下命令编译,问题均存在:

  • Clang:clang++-20 t.cpp -ot -O3 -march=haswell
  • GCC:g++-14 t.cpp -ot -O3 -march=haswell

测试程序代码

(注:仅当length为64的倍数时,dot_product()结果正确)

#include <cstdio>
#include <cstdlib>
#include <immintrin.h>

inline __m128 horizontal_sum_8(__m256 v) {
    __m128 t = _mm_add_ps(_mm256_castps256_ps128(v), _mm256_extractf128_ps(v, 1));
    t = _mm_add_ps(t, _mm_movehl_ps(t, t));
    return _mm_add_ps(t, _mm_movehdup_ps(t));
}

float dot_product(int length, const float *x, const float *y) {
    __m256 sum0 = _mm256_setzero_ps();
    __m256 sum1 = _mm256_setzero_ps();
    __m256 sum2 = _mm256_setzero_ps();
    __m256 sum3 = _mm256_setzero_ps();
    int i = 0;
    while (i <= length - 64) {
        const __m256 x0 = _mm256_loadu_ps(x + i);
        const __m256 x1 = _mm256_loadu_ps(x + i + 8);
        const __m256 x2 = _mm256_loadu_ps(x + i + 16);
        const __m256 x3 = _mm256_loadu_ps(x + i + 24);
        const __m256 x4 = _mm256_loadu_ps(x + i + 32);
        const __m256 x5 = _mm256_loadu_ps(x + i + 40);
        const __m256 x6 = _mm256_loadu_ps(x + i + 48);
        const __m256 x7 = _mm256_loadu_ps(x + i + 56);
        const __m256 y0 = _mm256_loadu_ps(y + i);
        const __m256 y1 = _mm256_loadu_ps(y + i + 8);
        const __m256 y2 = _mm256_loadu_ps(y + i + 16);
        const __m256 y3 = _mm256_loadu_ps(y + i + 24);
        const __m256 y4 = _mm256_loadu_ps(y + i + 32);
        const __m256 y5 = _mm256_loadu_ps(y + i + 40);
        const __m256 y6 = _mm256_loadu_ps(y + i + 48);
        const __m256 y7 = _mm256_loadu_ps(y + i + 56);

        sum0 = _mm256_fmadd_ps(x0, y0, sum0);
        sum1 = _mm256_fmadd_ps(x1, y1, sum1);
        sum2 = _mm256_fmadd_ps(x2, y2, sum2);
        sum3 = _mm256_fmadd_ps(x3, y3, sum3);
        sum0 = _mm256_fmadd_ps(x4, y4, sum0);
        sum1 = _mm256_fmadd_ps(x5, y5, sum1);
        sum2 = _mm256_fmadd_ps(x6, y6, sum2);
        sum3 = _mm256_fmadd_ps(x7, y7, sum3);
        i += 64;
    }

    __m256 sum = _mm256_add_ps(_mm256_add_ps(sum0, sum1), _mm256_add_ps(sum2, sum3));

    return _mm_cvtss_f32(horizontal_sum_8(sum));
}

int main() {
    const int N = 1024;
    alignas(4096) float x[N];
    alignas(4096) float y[N];

    for (int i = 0; i < N; i++) {
        x[i] = 0;
        y[i] = 0;
    }

    float s = 0;
    for (int iter = 0; iter < 100'000'000; iter++) {
        s += dot_product(N, x, y);
    }

    printf("%f\n", s);
}

理论性能分析

Alder Lake P核的FMA延迟为4周期,吞吐量为2次/周期(支持8个并行FMA)。每个FMA需要2次加载操作,P核加载吞吐量为3次/周期,理论峰值为1.5 FMA/周期;但由于程序仅使用4条依赖链,平均预期达到1 FMA/周期。目前无法解释为何性能波动如此显著。

后续测试补充

编辑1:固定CPU频率后的结果

手动将CPU频率限制为3.4GHz以排除自动节流可能,运行程序约600次并生成散点图,发现耗时存在两个典型值:3.4秒和4.2秒,约60%的运行结果为这两个值,其余40%为中间值。

编辑2:perf record采样对比

使用命令perf record -e cycles:ppp(采样率4KHz)进行采样,慢速运行的总采样数为16130,快速运行为12750;采样结果显示指令分布一致,采样数差异对应周期数的差异。

疑问

  1. 该性能波动的可能原因是什么?
  2. 哪些CPU性能计数器可用于进一步排查该问题?

内容的提问来源于stack exchange,提问作者geza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 07:29:54