intel向量化指令在矩陣乘應用中的評估
隨著機器學習等人工智慧技術的飛速發展,矩陣乘法的應用越來越多,intel晶片先後提供了不同系列的向量指令,包括mmx、sse、avx等,支援simd操作。後來為了更好地支援矩陣乘法,又增加了fma(Fused Multiply-Add)指令。fma指令需要三個向量引數
例子主要計算了一個矩陣
#include <stdio.h>
#include <time.h>
#include <x86intrin.h>
int main() {
const int col = 1024, row = 64, num_trails = 1000000;
float w[row][col];
float x[col];
float y[row];
float scratchpad[8];
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
w[i][j]=(float)(rand()%1000)/800.0f;
}
}
for (int j=0; j<col; j++) {
x[j]=(float)(rand()%1000)/800.0f;
}
clock_t t1, t2;
// The original matrix multiplication version
t1 = clock();
for (int r = 0; r < num_trails; r++)
for(int j = 0; j < row; j++)
{
float sum = 0;
float *wj = w[j];
for(int i = 0; i < col; i++)
sum += wj[i] * x[i];
y[j] = sum;
}
t2 = clock();
float diff = ((float)t2 - (float)t1) / CLOCKS_PER_SEC;
printf("\nTime taken: %.2f second.\n", diff);
for (int i=0; i<row; i++) {
printf("%.4f, ", y[i]);
}
printf("\n");
// The avx matrix multiplication version.
const int col_reduced_8 = col - col % 8;
__m256 op0, op1, tgt, tmp_vec;
t1 = clock();
for (int r = 0; r < num_trails; r++)
for (int i=0; i<row; i++) {
float res = 0;
tgt = _mm256_setzero_ps();
for (int j = 0; j < col_reduced_8; j += 8) {
op0 = __builtin_ia32_loadups256(&x[j]);
op1 = __builtin_ia32_loadups256(&w[i][j]);
tmp_vec = __builtin_ia32_mulps256(op0, op1);
tgt = __builtin_ia32_addps256(tmp_vec, tgt);
}
__builtin_ia32_storeups256(scratchpad, tgt);
for (int k=0; k<8; k++)
res += scratchpad[k];
for (int l=col_reduced_8; l<col; l++) {
res += w[i][l] * x[l];
}
y[i] = res;
}
t2 = clock();
diff = ((float)t2 - (float)t1) / CLOCKS_PER_SEC;
printf("\nTime taken: %.2f second.\n", diff);
for (int i=0; i<row; i++) {
printf("%.4f, ", y[i]);
}
printf("\n");
// The fma matrix multiplication version.
t1 = clock();
for(int r = 0; r < num_trails; r++)
for(int i = 0; i < row; i++)
{
float rlt = 0;
tgt = _mm256_setzero_ps();
for(int j = 0; j < col_reduced_8; j += 8)
{
op0 = __builtin_ia32_loadups256(&x[j]);
op1 = __builtin_ia32_loadups256(&w[i][j]);
tgt = _mm256_fmadd_ps(op0, op1, tgt);
}
__builtin_ia32_storeups256(scratchpad, tgt);
for(int k = 0; k < 8; k++)
{
rlt += scratchpad[k];
}
for(int l = col_reduced_8; l < col; l++)
{
rlt += w[i][l] * x[l];
}
y[i] = rlt;
}
t2 = clock();
diff = ((float)t2 - (float)t1) / CLOCKS_PER_SEC ;
printf("\nTime taken: %.2f second.\n", diff);
for(int i=0; i<row; i++)
{
printf("%.4f, ", y[i]);
}
printf("\n");
在ubuntu系統中,程式的編譯命令是:
gcc -O2 -mfma test.c -o test
需要注意的是,只有在支援fma的晶片結構下,程式才能夠執行。可以通過命令:
cat /proc/cpuinfo | grep fma
來判斷晶片是否支援fma。
其執行結果為:
Time taken: 93.56 second.
409.8341, 413.4546, 398.7332, 399.8303, 404.1195, 402.3861, 394.6979, 412.6429, 409.0014, 390.9019, 400.3911, 392.7900, 400.5019, 418.6781, 399.3336, 404.0719, 414.9839, 411.6887, 396.0086, 406.6972, 384.5781, 399.3724, 400.0473, 391.6383, 401.3511, 400.8543, 418.4066, 406.6425, 405.5102, 408.4534, 403.0285, 406.3510, 410.2005, 414.9617, 417.3602, 406.4511, 397.1705, 406.1265, 393.3314, 407.1777, 389.9053, 397.3145, 401.7866, 413.3134, 415.7482, 414.2341, 403.3439, 405.4922, 395.4076, 399.6389, 409.6675, 419.8184, 412.3336, 399.8252, 403.3434, 387.4861, 402.2747, 399.8241, 414.1568, 405.4861, 406.6151, 410.4040, 408.9755, 398.9610,
Time taken: 10.94 second.
409.8341, 413.4549, 398.7335, 399.8304, 404.1191, 402.3860, 394.6979, 412.6424, 409.0016, 390.9022, 400.3909, 392.7900, 400.5020, 418.6781, 399.3336, 404.0718, 414.9842, 411.6884, 396.0087, 406.6971, 384.5780, 399.3723, 400.0472, 391.6382, 401.3510, 400.8541, 418.4067, 406.6424, 405.5103, 408.4536, 403.0287, 406.3513, 410.2007, 414.9618, 417.3603, 406.4513, 397.1708, 406.1266, 393.3315, 407.1776, 389.9049, 397.3150, 401.7864, 413.3134, 415.7483, 414.2341, 403.3439, 405.4922, 395.4075, 399.6392, 409.6674, 419.8183, 412.3336, 399.8253, 403.3433, 387.4865, 402.2746, 399.8239, 414.1567, 405.4861, 406.6153, 410.4034, 408.9752, 398.9612,
Time taken: 12.08 second.
409.8341, 413.4549, 398.7335, 399.8304, 404.1191, 402.3860, 394.6979, 412.6424, 409.0016, 390.9022, 400.3909, 392.7900, 400.5021, 418.6781, 399.3336, 404.0718, 414.9842, 411.6884, 396.0087, 406.6971, 384.5780, 399.3722, 400.0472, 391.6382, 401.3510, 400.8541, 418.4067, 406.6424, 405.5102, 408.4536, 403.0287, 406.3513, 410.2007, 414.9618, 417.3603, 406.4513, 397.1708, 406.1266, 393.3315, 407.1776, 389.9050, 397.3150, 401.7864, 413.3134, 415.7483, 414.2341, 403.3439, 405.4922, 395.4075, 399.6392, 409.6674, 419.8183, 412.3336, 399.8253, 403.3433, 387.4865, 402.2746, 399.8239, 414.1568, 405.4861, 406.6153, 410.4034, 408.9752, 398.9612,
可見,avx對乘加的組合實現效能還略高於fma指令。而精度兩者相似,略低於原始的運算。