vc2012並行執行的2種方法

lt發表於2016-11-06

一種是微軟的Parallel Patterns Library (PPL)庫,一種是c++11的std::async。 使用微軟的菲波那契數函式例子。

// parallel-fibonacci.cpp
// compile with: /EHsc
#include <windows.h>
#include <ppl.h>
#include <concurrent_vector.h>
#include <array>
#include <vector>
#include <tuple>
#include <algorithm>
#include <iostream>

using namespace concurrency;
using namespace std;

// Calls the provided work function and returns the number of milliseconds 
// that it takes to call that function.
template <class Function>
__int64 time_call(Function&& f)
{
   __int64 begin = GetTickCount();
   f();
   return GetTickCount() - begin;
}

// Computes the nth Fibonacci number.
int fibonacci(int n)
{
   if(n < 2)
      return n;
   return fibonacci(n-1) + fibonacci(n-2);
}

int wmain()
{
   __int64 elapsed;

   // An array of Fibonacci numbers to compute.
   array<int, 4> a = { 24, 26, 41, 42 };

   // The results of the serial computation.
   vector<tuple<int,int>> results1;

   // The results of the parallel computation.
   concurrent_vector<tuple<int,int>> results2;

   // Use the for_each algorithm to compute the results serially.
   elapsed = time_call([&] 
   {
      for_each (begin(a), end(a), [&](int n) {
         results1.push_back(make_tuple(n, fibonacci(n)));
      });
   });   
   wcout << L"serial time: " << elapsed << L" ms" << endl;

   // Use the parallel_for_each algorithm to perform the same task.
   elapsed = time_call([&] 
   {
      parallel_for_each (begin(a), end(a), [&](int n) {
         results2.push_back(make_tuple(n, fibonacci(n)));
      });

      // Because parallel_for_each acts concurrently, the results do not 
      // have a pre-determined order. Sort the concurrent_vector object
      // so that the results match the serial version.
      sort(begin(results2), end(results2));
   });   
   wcout << L"parallel time: " << elapsed << L" ms" << endl << endl;

   // Print the results.
   for_each (begin(results2), end(results2), [](tuple<int,int>& pair) {
      wcout << L"fib(" << get<0>(pair) << L"): " << get<1>(pair) << endl;
   });
}

可見2種並行的效率是差不多的。都比序列提高了大約1/2.

#include <iostream>                // std::cout
#include <future>                // std::async, std::future, std::launch
#include <array>
#include <vector>
#include <tuple>
#include <algorithm>
#include <ctime>
// Computes the nth Fibonacci number.
using namespace std;
int fibonacci(int n)
{
   if(n < 2)
      return n;
   return fibonacci(n-1) + fibonacci(n-2);
}
int main(int argc,char*argv[]) 
{ 
   // An array of Fibonacci numbers to compute.
   array<int, 4> a = { 24, 26, 41, 42 };

   // The results of the serial computation.
   vector<tuple<int,int>> results1,results2;

  int t=clock();
      for_each (begin(a), end(a), [&](int n) {
         results1.push_back(make_tuple(n, fibonacci(n)));
      });
  printf("(normal)time is %d ms\n",clock()-t);
  t=clock();
  std::future < int >fb[20];
  int i=0;
  for_each (begin(a), end(a), [&](int n) {
    fb[i]=std::async(std::launch::async, fibonacci,n);
    i++;
    });
    i=0;
  for_each (begin(a), end(a), [&](int n) {
    results2.push_back(make_tuple(n, fb[i].get()));
    i++;
    });
  //sort(begin(results2), end(results2));  
  printf("(mthred)time is %d ms\n",clock()-t);
     for_each (begin(results2), end(results2), [](tuple<int,int>& pair) {
      wcout << L"fib(" << get<0>(pair) << L"): " << get<1>(pair) << endl;
     });
  return 0;
}

結果

D:\>cl mtf.cpp -O2
用於 x86 的 Microsoft (R) C/C++ 優化編譯器 17.00.51106.1 版版權所有(C) Microsoft Corporation。保留所有權利。

mtf.cpp
D:\vs2012\vc\bin\..\include\xlocale(336) : warning C4530: 使用了 C++ 異常處理程式,但未啟用展開語義。請指定 /EHsc
Microsoft (R) Incremental Linker Version 11.00.51106.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:mtf.exe
mtf.obj

D:\>mtf
(normal)time is 4368 ms
(mthred)time is 2714 ms
fib(24): 46368
fib(26): 121393
fib(41): 165580141
fib(42): 267914296

D:\>cl ppl.cpp -O2
用於 x86 的 Microsoft (R) C/C++ 優化編譯器 17.00.51106.1 版版權所有(C) Microsoft Corporation。保留所有權利。

ppl.cpp
d:\vs2012\winsdk\v7.1a\include\sal_supp.h(57) : warning C4005: “__useHeader”: 巨集重定義
        D:\vs2012\vc\bin\..\include\sal.h(2872) : 參見“__useHeader”的前一個定義
d:\vs2012\winsdk\v7.1a\include\specstrings_supp.h(77) : warning C4005: “__on_failure”: 巨集重定義
        D:\vs2012\vc\bin\..\include\sal.h(2882) : 參見“__on_failure”的前一個定義
D:\vs2012\vc\bin\..\include\concrt.h(313) : warning C4530: 使用了 C++ 異常處理程式,但未啟用展開語義。請指定 /EHsc
Microsoft (R) Incremental Linker Version 11.00.51106.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:ppl.exe
ppl.obj
D:\>ppl
serial time: 4352 ms
parallel time: 2730 ms

fib(24): 46368
fib(26): 121393
fib(41): 165580141
fib(42): 267914296

相關文章