Я закодировал умножение вектора на матрицу двумя способами: один с помощью 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;
}
std::async
довольно ужасна. Каждая нить шагает наN
. В версии OpenMP вы позволяете OpenMP управлять распределением работы. И, вероятно, он использует блочное распределение вместо пошагового распределения. - person Mysticial   schedule 24.10.2017out[i]
. Они чередуются между потоками, поэтому существует реальная возможность конфликта. - person Mysticial   schedule 24.10.2017