为什么并行版本的累积会慢得多?

时间:2016-03-28 20:54:39

标签: c++ c++11 concurrency

Antony Williams' "C++ Concurrency in Action"的启发,我仔细研究了std::accumulate的并行版本。我从书中复制了它的代码并添加了一些输出用于调试目的,这就是我最终得到的结果:

#include <algorithm>
#include <future>
#include <iostream>
#include <thread>

template <typename Iterator, typename T>
struct accumulate_block
{
  T operator()(Iterator first, Iterator last)
  {
    return std::accumulate(first, last, T());
  }
};

template <typename Iterator, typename T>
T parallel_accumulate(Iterator first, Iterator last, T init)
{
  const unsigned long length = std::distance(first, last);

  if (!length) return init;

  const unsigned long min_per_thread = 25;
  const unsigned long max_threads    = (length) / min_per_thread;
  const unsigned long hardware_conc  = std::thread::hardware_concurrency();
  const unsigned long num_threads    = std::min(hardware_conc != 0 ? hardware_conc : 2, max_threads);
  const unsigned long block_size     = length / num_threads;

  std::vector<std::future<T>> futures(num_threads - 1);
  std::vector<std::thread> threads(num_threads - 1);

  Iterator block_start = first;
  for (unsigned long i = 0; i < (num_threads - 1); ++i)
  {
    Iterator block_end = block_start;
    std::advance(block_end, block_size);

    std::packaged_task<T(Iterator, Iterator)> task{accumulate_block<Iterator, T>()};
    futures[i] = task.get_future();
    threads[i] = std::thread(std::move(task), block_start, block_end);
    block_start = block_end;
  }

  T last_result = accumulate_block<Iterator, T>()(block_start, last);

  for (auto& t : threads) t.join();

  T result = init;
  for (unsigned long i = 0; i < (num_threads - 1); ++i) {
    result += futures[i].get();
  }
  result += last_result;
  return result;
}

template <typename TimeT = std::chrono::microseconds>
struct measure
{
  template <typename F, typename... Args>
  static typename TimeT::rep execution(F func, Args&&... args)
  {
    using namespace std::chrono;
    auto start = system_clock::now();
    func(std::forward<Args>(args)...);
    auto duration = duration_cast<TimeT>(system_clock::now() - start);
    return duration.count();
  }
};

template <typename T>
T parallel(const std::vector<T>& v)
{
  return parallel_accumulate(v.begin(), v.end(), 0);
}

template <typename T>
T stdaccumulate(const std::vector<T>& v)
{
  return std::accumulate(v.begin(), v.end(), 0);
}

int main()
{
  constexpr unsigned int COUNT = 200000000;
  std::vector<int> v(COUNT);

  // optional randomising vector contents - std::accumulate also gives 0us
  // but custom parallel accumulate gives longer times with randomised input
  std::mt19937 mersenne_engine;
  std::uniform_int_distribution<int> dist(1, 100);
  auto gen = std::bind(dist, mersenne_engine);
  std::generate(v.begin(), v.end(), gen);
  std::fill(v.begin(), v.end(), 1);

  auto v2 = v; // copy to work on the same data

  std::cout << "starting ... " << '\n';
  std::cout << "std::accumulate : \t" << measure<>::execution(stdaccumulate<int>, v) << "us" << '\n';
  std::cout << "parallel: \t" << measure<>::execution(parallel<int>, v2) << "us" << '\n';
}

最有趣的是,我几乎总是从std::accumulate获得0长度的时间。

示例输出:

starting ... 
std::accumulate :       0us
parallel: 
inside1 54us

inside2 81830us

inside3 89082us
89770us

这是什么问题?

http://cpp.sh/6jbt

1 个答案:

答案 0 :(得分:10)

与微基准测试一样,您需要确保您的代码实际上是做某事。你正在做一个0,但你实际上并没有把结果存储在任何地方或用它做任何事情。所以你真的需要完成任何工作吗?编译器只是在正常情况下删除了所有逻辑。这就是你得到int s, s2; std::cout << "starting ... " << '\n'; std::cout << "std::accumulate : \t" << measure<>::execution([&]{s = std::accumulate(v.begin(), v.end(), 0);}) << "us\n"; std::cout << "parallel: \t" << measure<>::execution([&]{s2 = parallel_accumulate(v2.begin(), v2.end(), 0);}) << "us\n"; std::cout << s << ',' << s2 << std::endl; 的原因。

只需更改代码即可确保需要完成工作。例如:

private void ButtonBgColor()
{
string qryBgColor = "select Name from lookup where code in (select VALUE from qSettings where name='BUTTON_BG_COLOR') and type='BgColor'";
try
{
sqlConnection.Open();
sqlCommand = new SqlCommand(qryBgColor, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.Read())
{
string BUTTON_BG_COLOR = sqlDataReader["Name"].ToString();
Button[] b = new Button[9];
for (int i = 0; i < b.Length; i++)
{
var textBlock08 = (TextBlock)b[i].Template.FindName("myTextBlock", b[i]);
textBlock08.Background = (System.Windows.Media.SolidColorBrush)new System.Windows.Media.BrushConverter().ConvertFromString(BUTTON_BG_COLOR);
}
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString(), "Button Background Color Exception");
}