[英]Poor performance with Intel Threading Building Blocks using task_group (new user)
我最近對英特爾線程構建模塊感興趣。 我想利用tbb::task_group
類來管理線程池。
我的第一個嘗試是構建一個測試,在該測試中將向量復制到另一個向量中:我創建第n個任務,每個任務都要復制向量的連續切片。
但是,性能隨線程數而降低。 我在另一個線程池實現中也得到了相同的結果。 在TBB 2018 Update 5中,在8 i7核心盒上的debian strecth上使用gcc 6.3,我得到以下圖來復制1'000'000元素的向量:
第n個真實用戶
1 0.808秒0.807秒
2 1.068秒2.105秒
4 1.109秒4.282秒
也許有些人會幫助我理解這個問題。 這是代碼:
#include<iostream>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include "tbb/task_group.h"
#include "tbb/task_scheduler_init.h"
namespace mgis{
using real = double;
using size_type = size_t;
}
void my_copy(std::vector<mgis::real>& d,
const std::vector<mgis::real>& s,
const mgis::size_type b,
const mgis::size_type e){
const auto pb = s.begin()+b;
const auto pe = s.begin()+e;
const auto po = d.begin()+b;
std::copy(pb,pe,po);
}
int main(const int argc, const char* const* argv) {
using namespace mgis;
if (argc != 3) {
std::cerr << "invalid number of arguments\n";
std::exit(-1);
}
const auto ng = std::stoi(argv[1]);
const auto nth = std::stoi(argv[2]);
tbb::task_scheduler_init init(nth);
tbb::task_group g;
std::vector<real> v(ng,0);
std::vector<real> v2(ng);
for(auto i =0; i!=2000;++i){
const auto d = ng / nth;
const auto r = ng % nth;
size_type b = 0;
for (size_type i = 0; i != r; ++i) {
g.run([&v2, &v, b, d] { my_copy(v2, v, b, b + d + 1); });
b += d+1;
}
for (size_type i = r; i != nth; ++i) {
g.run([&v2, &v, b, d] { my_copy(v2, v, b, b + d); });
b += d ;
}
g.wait();
}
return EXIT_SUCCESS;
}
tbb::parallel_for
,您在那里不需要task_group
。 此外,一一調用任務具有線性復雜度, parallel_for
具有對數復雜度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.