我了興趣,這一點,並編寫了一段代碼來測試假設自己,它似乎是比「標準模式」 SIMD給出了不同的結果。
以下片段與ICC 13.0.2在Mac OS X 10.8.3使用icpc -std=c++11 -O3 -ip -xAVX -fp-model source -fp-model precise -mkl=parallel -openmp
編譯。
#include <cmath>
#include <cstring>
#include <iostream>
#include <random>
#include <utility>
#include <immintrin.h>
#include <mkl.h>
template <typename type, size_t rows, size_t cols>
class matrix
{
private:
void *_data;
public:
matrix() :
_data (_mm_malloc(sizeof(type) * rows * cols, 64))
{
if (_data == nullptr) throw std::bad_alloc();
else memset(_data, 0, sizeof(type) * rows * cols);
}
matrix(matrix<type, rows, cols> const& other) :
_data (_mm_malloc(sizeof(type) * rows * cols, 64))
{
if (_data == nullptr) throw std::bad_alloc();
else memcpy(_data, other._data, sizeof(type) * rows * cols);
}
~matrix()
{
if (_data != nullptr) _mm_free(_data);
}
typedef type array_type[cols];
array_type& operator[](size_t i)
{
return static_cast<array_type*>(_data)[i];
}
typedef type const_array_type[cols];
const_array_type& operator[](size_t i) const
{
return static_cast<const_array_type*>(_data)[i];
}
};
template <typename type, size_t m, size_t n>
type max_diff(matrix<type, m, n> const& a, matrix<type, m, n> const& b)
{
type value = static_cast<type>(0);
for (size_t i = 0; i < m; ++i)
{
#pragma novector
for (size_t j = 0; j < n; ++j)
{
const type diff = a[i][j] - b[i][j];
if (std::abs(diff) > value) value = std::abs(diff);
}
}
return value;
}
template <typename type, size_t m, size_t n, size_t k>
matrix<type, m, n> matmul_loop(matrix<type, m, k> const& a, matrix<type, n, k> const& b)
{
matrix<type, m, n> out;
#pragma omp parallel for
for (size_t i = 0; i < m; ++i)
{
for (size_t j = 0; j < n; ++j)
{
for (size_t l = 0; l < k; ++l)
{
out[i][j] += a[i][l] * b[j][l];
}
}
}
return out;
}
template <typename type, size_t m, size_t n, size_t k>
matrix<type, m, n> matmul_simd(matrix<type, m, k> const& a, matrix<type, n, k> const& b)
{
matrix<type, m, n> out;
type *temp = static_cast<type*>(_mm_malloc(sizeof(type) * k, 64));
#pragma omp parallel for
for (size_t i = 0; i < m; ++i)
{
for (size_t j = 0; j < n; ++j)
{
type temp = 0.;
#pragma vector aligned
#pragma ivdep
#pragma simd vectorlengthfor(type)
for (size_t l = 0; l < k; ++l)
{
temp += a[i][l] * b[j][l];
}
out[i][j] = temp;
}
}
return out;
}
template <size_t m, size_t n, size_t k>
matrix<float, m, n> matmul_sgemm(matrix<float, m, k> const& a, matrix<float, n, k> const& b)
{
matrix<float, m, n> out;
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, m, n, k, 1., &a[0][0], m, &b[0][0], n, 0., &out[0][0], m);
return out;
}
int main()
{
std::mt19937_64 generator;
std::uniform_real_distribution<float> rand_dist(-1000.0,1000.0);
const size_t size = 4096;
matrix<float, size, size> mat;
for (size_t i = 0; i < size; ++i)
{
for (size_t j = 0; j < size; ++j)
{
mat[i][j] = rand_dist(generator);
}
}
matrix<float, size, size> result_loop = matmul_loop(mat, mat);
matrix<float, size, size> result_simd = matmul_simd(mat, mat);
matrix<float, size, size> result_sgemm = matmul_sgemm(mat, mat);
std::cout << "SIMD differs from LOOP by a maximum of " << max_diff(result_loop, result_simd) << std::endl;
std::cout << "SGEMM differs from LOOP by a maximum of " << max_diff(result_loop, result_sgemm) << std::endl;
std::cout << "SGEMM differs from SIMD by a maximum of " << max_diff(result_simd, result_sgemm) << std::endl;
return 0;
}
請注意,「隨機」矩陣是使用標準種子生成的,因此結果應該是完全可重現的。基本上,給定一個4096x4096
矩陣A,代碼使用三種不同的方法計算AA T,然後比較結果,打印出相差最大的成分。在我的機器,輸出如下:
$ ./matmul
SIMD differs from LOOP by a maximum of 6016
SGEMM differs from LOOP by a maximum of 6016
SGEMM differs from SIMD by a maximum of 512
的編譯器標誌-fp-model source -fp-model precise
防止matmul_loop
被矢量化,但在matmul_simd
循環顯然被迫矢量化#pragma simd
。矩陣轉置只是爲了簡化SIMD代碼。
假設你未優化的SGEMM涉及嵌套循環?如果您使用的是英特爾編譯器,請嘗試#pragma simd最內層的循環並查看結果是否更改。此頁http://software.intel.com/sites/products/documentation/doclib/iss/2013/compiler/cpp-lin/GUID-1EA04294-988E-4152-B584-B028FD6FAC48.htm似乎暗示英特爾的SIMD指令不一定尊重嚴格的浮點標準。 MKL SGEMM絕對使用SSE/AVX指令。 – Saran
不幸的是,我現在沒有使用英特爾的編譯器......我將提取代碼並使用英特爾編譯器運行它,以查看SIMD是否有責任。 – Alex
你可以和Eigen比較。這是非常容易使用和免費的。它使用SSE,並且在僅SSE的機器上與MKL差不多。(MKL對於帶有AVX的機器速度更快。 – 2013-06-25 11:04:08