With reference to http://blogs.msdn.com/b/xiangfan/archive/2009/04/28/optimize-your-code-matrix-multiplication.aspx.
template<>
void SeqMatrixMult4(int size, float** m1, float** m2, float** result)
{
Transpose(size, m2);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
__m128 c = _mm_setzero_ps();
for (int k = 0; k < size; k += 4) {
c = _mm_add_ps(c, _mm_mul_ps(_mm_load_ps(&m1[i][k]), _mm_load_ps(&m2[j][k])));
}
c = _mm_hadd_ps(c, c);
c = _mm_hadd_ps(c, c);
_mm_store_ss(&result[i][j], c);
}
}
Transpose(size, m2);
}
Why is there 2 more _mm_hadd_ps(c, c) after the inner most for loop? To verify my understanding: this code loads 4 floats from m1 and another 4 from m2, then multiplies them resulting in 4 floats (__m128). Then I sum them into c (at this point, its still 4 floats?). Then after the for loop I hadd this result twice? What does that do?
My code slightly re-written produces the wrong result it appears
long long start, end;
__m128 v1, v2, vMul, vRes;
vRes = _mm_setzero_ps();
start = wall_clock_time();
transpose_matrix(m2);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
float tmp = 0;
for (int k = 0; k < SIZE; k+=4) {
v1 = _mm_load_ps(&m1[i][k]);
v2 = _mm_load_ps(&m2[j][k]);
vMul = _mm_mul_ps(v1, v2);
vRes = _mm_add_ps(vRes, vMul);
}
vRes = _mm_hadd_ps(vRes, vRes);
_mm_store_ss(&result[i][j], vRes);
}
}
end = wall_clock_time();
fprintf(stderr, "Optimized Matrix multiplication took %1.2f seconds\n", ((float)(end - start))/1000000000);
// reverse the transposition
transpose_matrix(m2);
haddpsdoesn’t sum all four elements in a vector. Twohaddpsinstructions are needed to get the full horizontal sum.If we number the elements of the vector
{c0,c1,c2,c3}, the firsthaddpsproduces{c0+c1, c2+c3, c0+c1, c2+c3}. The second produces{c0+c1+c2+c3, <same thing in the other lanes>}.