簡體   English   中英

使用task_group的英特爾線程構建模塊的性能不佳(新用戶)

[英]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;
}
  1. 這么短的基准測試沒有意義,因為TBB需要創建線程並將其啟動,由於它是惰性異步進程,因此在第一次調用TBB時不會立即發生。 但是,您的用戶時間建議線程已啟動並正在運行,但可能沒有工作要做。
  2. 內存復制不利於可伸縮性研究,因為它的規模不會超過內存控制器/通道的數量。 因此,有4個CPU或24個CPU都沒關系,即使有好的硬件,也不太可能獲得超過x4的加速。 您的頻道可能較少。
  3. 而不是手動分割范圍,請使用tbb::parallel_for ,您在那里不需要task_group 此外,一一調用任務具有線性復雜度, parallel_for具有對數復雜度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM