为什么 openMP 的 VC++ 矩阵乘以向量比异步更快?
Why is VC++ matrix times vector faster with openMP than async?
我用两种方式编写向量与矩阵的乘法,一种使用 openMP,另一种使用 std::async。我预计性能几乎相同。 OpenMP 在第一次调用时很慢,可能是因为它推迟了创建线程池。在那之后,异步版本一直慢 40%。 (我有 Intel Core i5,是 4 核。)
怎么回事? VC++ 不使用线程池进行异步吗?我在做傻事吗? (最有可能。)我认为对输出向量的访问间隔足够大以避免错误共享。
#include "stdafx.h"
# include <iostream>
# include <vector>
# include <omp.h>
# include <ctime>
#include <numeric>
#include <thread>
#include <chrono>
#include <future>
// Matrix multiplication of vector using omp
template<class Mat, class Vec>
void mult_mat_vec_omp
(const Mat &mat, const Vec &inp, Vec &out) {
const int steps = static_cast<int>(std::size(mat));
using std::begin; using std::end;
auto N = std::thread::hardware_concurrency();
omp_set_num_threads(N);
#pragma omp parallel for
for (int i=0; i < steps; ++i) {
out[i] = std::inner_product(begin(mat[i]), end(mat[i]), begin(inp), 0.0);
}
}
// Matrix multiplication of vector using async
template<class Mat, class Vec>
void mult_mat_vec_async
(const Mat &mat, const Vec &inp, Vec &out) {
using std::begin; using std::end;
auto N = std::thread::hardware_concurrency();
typedef decltype(N) usigned;
const unsigned steps = static_cast<unsigned>(std::size(mat));
auto f = [&](unsigned id) {
for (unsigned i=id; i < steps; i+= N) {
out[i] = std::inner_product(begin(mat[i]), end(mat[i]), begin(inp), 0.0);
}
};
std::vector<std::future<void>> threads;
for (unsigned i = 1; i<N; ++i) {
threads.push_back(std::async(std::launch::async, f, i));
}
f(0);
for (auto &x: threads) {
x.get();
}
}
double test() {
using std::vector;
using clock=std::chrono::high_resolution_clock;
vector<double> a;
vector<double> b;
vector<double> c;
vector<vector<double>> mat;
vector<double> v;
int rows = 350;
int cols = 350;
for (int i = 0; i< cols; ++i) {
a.push_back(i/10.0);
b.push_back(-999);
c.push_back(8888);
}
for (int i=0; i<rows; ++i) {
v.clear();
for (int j=0; j<cols; ++j) {
v.push_back (((i+.5)*j)/100.0);
}
mat.push_back(v);
}
clock::time_point start = clock::now();
int N = 10000;
for (int i=0; i< N/10; ++i) {
mult_mat_vec_omp(mat, a, b) ;
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
};
long long duration =
std::chrono::duration_cast<std::chrono::milliseconds>(clock::now()-start).count();
start = clock::now();
size_t cutoff = 0; // 2*rows*cols;
for (int i=0; i< N/10; ++i) {
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
};
long long duration2 = std::chrono::duration_cast<std::chrono::milliseconds>(clock::now()-start).count();
//cout << mat[0][5] << " " << b[0] << " " << c[0] << endl;
bool good = (b==c);
std::cout << duration*(1.0/N) << ' ' << duration2*(1.0/N) << " " << good << std::endl;
return 0;
}
int main ()
{
for(int i=0; i<15; ++i) test();
return 0;
}
在英特尔酷睿 i7-2600 上,禁用 HT,使用 gcc 7.2 / Linux,数字有些不同,异步版本大约慢 10%。
现在关于缓存效率和错误共享的讨论步入正轨。您应该尝试通过同一线程访问连续的元素,至少达到缓存行的大小(例如 64 字节)。对于读取,您只需通过更高效地使用缓存/数据局部性来节省内存访问 - 对于写入,情况更糟,因为错误共享会在内核之间的缓存行周围反弹。但是,重要的是要认识到这与实际数据访问无关——发生在 std::inner_product
内并且两个版本都相同。如果实际数据访问采用这种线程交错模式,性能将比 40% off 差得多。
现在很容易避免并测试它是否有帮助:
const unsigned steps = static_cast<unsigned>(std::size(mat));
auto f = [&](unsigned id) {
const auto chunk_size = 1 + ((steps - 1) / N);
const auto max = std::min(chunk_size * (id + 1), steps);
for (unsigned i = chunk_size * id; i < max; i++)
{
out[i] = std::inner_product(begin(mat[i]), end(mat[i]), begin(inp), 0.0);
}
};
在我的配置中消除了版本之间的所有性能差异。
如果您仍然发现系统性能存在差异,我建议您使用合适的性能分析工具。我不熟悉您的生态系统,因此无法提供任何建议 - 但重要的是不要猜测性能。
请注意,std::vector<std::vector<>>
不是用于高性能数据访问/矩阵乘法的良好数据结构。您不会接近为矩阵使用连续内存的高度优化库的性能。
我用两种方式编写向量与矩阵的乘法,一种使用 openMP,另一种使用 std::async。我预计性能几乎相同。 OpenMP 在第一次调用时很慢,可能是因为它推迟了创建线程池。在那之后,异步版本一直慢 40%。 (我有 Intel Core i5,是 4 核。)
怎么回事? VC++ 不使用线程池进行异步吗?我在做傻事吗? (最有可能。)我认为对输出向量的访问间隔足够大以避免错误共享。
#include "stdafx.h"
# include <iostream>
# include <vector>
# include <omp.h>
# include <ctime>
#include <numeric>
#include <thread>
#include <chrono>
#include <future>
// Matrix multiplication of vector using omp
template<class Mat, class Vec>
void mult_mat_vec_omp
(const Mat &mat, const Vec &inp, Vec &out) {
const int steps = static_cast<int>(std::size(mat));
using std::begin; using std::end;
auto N = std::thread::hardware_concurrency();
omp_set_num_threads(N);
#pragma omp parallel for
for (int i=0; i < steps; ++i) {
out[i] = std::inner_product(begin(mat[i]), end(mat[i]), begin(inp), 0.0);
}
}
// Matrix multiplication of vector using async
template<class Mat, class Vec>
void mult_mat_vec_async
(const Mat &mat, const Vec &inp, Vec &out) {
using std::begin; using std::end;
auto N = std::thread::hardware_concurrency();
typedef decltype(N) usigned;
const unsigned steps = static_cast<unsigned>(std::size(mat));
auto f = [&](unsigned id) {
for (unsigned i=id; i < steps; i+= N) {
out[i] = std::inner_product(begin(mat[i]), end(mat[i]), begin(inp), 0.0);
}
};
std::vector<std::future<void>> threads;
for (unsigned i = 1; i<N; ++i) {
threads.push_back(std::async(std::launch::async, f, i));
}
f(0);
for (auto &x: threads) {
x.get();
}
}
double test() {
using std::vector;
using clock=std::chrono::high_resolution_clock;
vector<double> a;
vector<double> b;
vector<double> c;
vector<vector<double>> mat;
vector<double> v;
int rows = 350;
int cols = 350;
for (int i = 0; i< cols; ++i) {
a.push_back(i/10.0);
b.push_back(-999);
c.push_back(8888);
}
for (int i=0; i<rows; ++i) {
v.clear();
for (int j=0; j<cols; ++j) {
v.push_back (((i+.5)*j)/100.0);
}
mat.push_back(v);
}
clock::time_point start = clock::now();
int N = 10000;
for (int i=0; i< N/10; ++i) {
mult_mat_vec_omp(mat, a, b) ;
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
mult_mat_vec_omp(mat, a, b);
};
long long duration =
std::chrono::duration_cast<std::chrono::milliseconds>(clock::now()-start).count();
start = clock::now();
size_t cutoff = 0; // 2*rows*cols;
for (int i=0; i< N/10; ++i) {
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
mult_mat_vec_async(mat, a, c);
};
long long duration2 = std::chrono::duration_cast<std::chrono::milliseconds>(clock::now()-start).count();
//cout << mat[0][5] << " " << b[0] << " " << c[0] << endl;
bool good = (b==c);
std::cout << duration*(1.0/N) << ' ' << duration2*(1.0/N) << " " << good << std::endl;
return 0;
}
int main ()
{
for(int i=0; i<15; ++i) test();
return 0;
}
在英特尔酷睿 i7-2600 上,禁用 HT,使用 gcc 7.2 / Linux,数字有些不同,异步版本大约慢 10%。
现在关于缓存效率和错误共享的讨论步入正轨。您应该尝试通过同一线程访问连续的元素,至少达到缓存行的大小(例如 64 字节)。对于读取,您只需通过更高效地使用缓存/数据局部性来节省内存访问 - 对于写入,情况更糟,因为错误共享会在内核之间的缓存行周围反弹。但是,重要的是要认识到这与实际数据访问无关——发生在 std::inner_product
内并且两个版本都相同。如果实际数据访问采用这种线程交错模式,性能将比 40% off 差得多。
现在很容易避免并测试它是否有帮助:
const unsigned steps = static_cast<unsigned>(std::size(mat));
auto f = [&](unsigned id) {
const auto chunk_size = 1 + ((steps - 1) / N);
const auto max = std::min(chunk_size * (id + 1), steps);
for (unsigned i = chunk_size * id; i < max; i++)
{
out[i] = std::inner_product(begin(mat[i]), end(mat[i]), begin(inp), 0.0);
}
};
在我的配置中消除了版本之间的所有性能差异。
如果您仍然发现系统性能存在差异,我建议您使用合适的性能分析工具。我不熟悉您的生态系统,因此无法提供任何建议 - 但重要的是不要猜测性能。
请注意,std::vector<std::vector<>>
不是用于高性能数据访问/矩阵乘法的良好数据结构。您不会接近为矩阵使用连续内存的高度优化库的性能。