次のコードを使用して、配列の要素をスケーリングおよび合計するルーチンのタイミングを計ることにより、L1 キャッシュとメモリに配列を持つことの影響を把握しようとしています (結果を 'ポイントは、ループ内で乗算と加算の両方を実行することです-これまでのところ、コンパイラは「a」を因数分解することを理解していません):
double sum(double a,double* X,int size)
{
double total = 0.0;
for(int i = 0; i < size; ++i)
{
total += a*X[i];
}
return total;
}
#define KB 1024
int main()
{
//Approximately half the L1 cache size of my machine
int operand_size = (32*KB)/(sizeof(double)*2);
printf("Operand size: %d\n", operand_size);
double* X = new double[operand_size];
fill(X,operand_size);
double seconds = timer();
double result;
int n_iterations = 100000;
for(int i = 0; i < n_iterations; ++i)
{
result = sum(3.5,X,operand_size);
//result += rand();
}
seconds = timer() - seconds;
double mflops = 2e-6*double(n_iterations*operand_size)/seconds;
printf("Vector size %d: mflops=%.1f, result=%.1f\n",operand_size,mflops,result);
return 0;
}
timer() および fill() ルーチンは簡潔にするために含まれていないことに注意してください。コードを実行する場合は、完全なソースをここで見つけることができます。
さて、ここからが興味深いところです。これは出力です:
Operand size: 2048
Vector size 2048: mflops=588.8, result=-67.8
X のすべての要素をループ反復間でキャッシュに保持する必要があるにもかかわらず、これは完全にキャッシュされていないパフォーマンスです。によって生成されたアセンブリ コードを見ると、次のようになります。
g++ -O3 -S -fno-asynchronous-unwind-tables register_opt_example.cpp
sum 関数のループに奇妙な点が 1 つあります。
L55:
movsd (%r12,%rax,8), %xmm0
mulsd %xmm1, %xmm0
addsd -72(%rbp), %xmm0
movsd %xmm0, -72(%rbp)
incq %rax
cmpq $2048, %rax
jne L55
説明書:
addsd -72(%rbp), %xmm0
movsd %xmm0, -72(%rbp)
"total" の値をスタックの sum() に格納し、ループの反復ごとに読み書きしていることを示します。このオペランドがレジスタに保持されるようにアセンブリを変更しました。
...
addsd %xmm0, %xmm3
...
この小さな変更により、パフォーマンスが大幅に向上します。
Operand size: 2048
Vector size 2048: mflops=1958.9, result=-67.8
tl;dr 私の質問は、単一のメモリ ロケーション アクセスをレジスタに置き換えると、コードが大幅に高速化されるのはなぜですか? これを可能にするアーキテクチャ上の要因は何ですか? 1 つのスタック位置を繰り返し書き込むと、キャッシュの有効性が完全に失われるというのは非常に奇妙に思えます。
付録
私のgccバージョンは次のとおりです。
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5646.1~2/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5646) (dot 1)
私のCPUは:
インテル Xeon X5650