当前位置:   article > 正文

异步调用std::async实现并行计算_std::async() get()

std::async() get()

一 std::async

  1. template< class Function, class... Args>
  2. std::future<std::result_of_t<std::decay_t<Function>(std::decay_t<Args>...)>>
  3. async( Function&& f, Args&&... args );

std::async返回一个future对象,可通过get方法异步获取执行结果,如下所示:

  1. #include <iostream>
  2. #include <string>
  3. #include <future>
  4. std::string helloFunction(const std::string& s) {
  5. return "Hello C++11 from " + s + ".";
  6. }
  7. class HelloFunctionObject {
  8. public:
  9. std::string operator()(const std::string& s) const {
  10. return "Hello C++11 from " + s + ".";
  11. }
  12. };
  13. int main() {
  14. // 带函数的future
  15. auto futureFunction = std::async(helloFunction, "function");
  16. // 带函数对象的future
  17. HelloFunctionObject helloFunctionObject;
  18. auto futureFunctionObject = std::async(helloFunctionObject, "function object");
  19. // 带匿名函数的future
  20. auto futureLambda = std::async([](const std::string& s) {return "Hello C++11 from " + s + "."; }, "lambda function");
  21. std::cout << futureFunction.get() << std::endl;
  22. std::cout << futureFunctionObject.get() << std::endl;
  23. std::cout << futureLambda.get() << std::endl;
  24. }

二 std::async实现并行计算

1.主程序:

  1. #include <iostream>
  2. #include <vector>
  3. #include <future>
  4. #include <random>
  5. #include <numeric>
  6. #include <cassert>
  7. #include "time_elapse.hpp"
  8. static const int NUM = 100000000;
  9. long long parallel_inner_product(std::vector<int>&v, std::vector<int>&w) {
  10. if (v.size() != w.size()) {
  11. return 0;
  12. }
  13. if (v.size() < 100) {
  14. return std::inner_product(begin(v), end(v), begin(w), 0LL);
  15. }
  16. auto future1 = std::async([&] { return std::inner_product(&v[0], &v[v.size() / 4], &w[0], 0LL); });
  17. auto future2 = std::async([&] { return std::inner_product(&v[v.size() / 4], &v[v.size() / 2], &w[v.size() / 4], 0LL); });
  18. auto future3 = std::async([&] { return std::inner_product(&v[v.size() / 2], &v[v.size() * 3 / 4], &w[v.size() / 2], 0LL); });
  19. auto future4 = std::async([&] { return std::inner_product(&v[v.size() * 3 / 4], &v[v.size()], &w[v.size() * 3 / 4], 0LL); });
  20. return future1.get() + future2.get() + future3.get() + future4.get();
  21. }
  22. long long serial_inner_product(std::vector<int>&v, std::vector<int>&w) {
  23. if (v.size() != w.size()) {
  24. return 0;
  25. }
  26. return std::inner_product(begin(v), end(v), begin(w), 0LL);
  27. }
  28. int main() {
  29. std::random_device seed;
  30. std::mt19937 engine(seed());
  31. std::uniform_int_distribution<int> dist(0, 100);
  32. std::vector<int> v, w;
  33. v.reserve(NUM);
  34. w.reserve(NUM);
  35. time_elapse te;
  36. te.start();
  37. for (int i = 0; i < NUM; ++i) {
  38. v.push_back(dist(engine));
  39. w.push_back(dist(engine));
  40. }
  41. std::cout << "load vector elapse = " << te.end() << "s" << std::endl;
  42. te.start();
  43. auto res1 = parallel_inner_product(v, w);
  44. std::cout << "parallel inner_product elapse = " << te.end() << "s" << std::endl;
  45. te.start();
  46. auto res2 = serial_inner_product(v, w);
  47. std::cout << "serial inner_product elapse = " << te.end() << "s" << std::endl;
  48. assert(res1 == res2);
  49. return 0;
  50. }

2.time_elapse.hpp

  1. #include <chrono>
  2. struct time_elapse {
  3. void start() {
  4. start_ = std::chrono::steady_clock::now();
  5. }
  6. double end() {
  7. std::chrono::duration<double>dura = std::chrono::steady_clock::now() - start_;
  8. return dura.count();
  9. }
  10. private:
  11. std::chrono::time_point<std::chrono::steady_clock> start_ = std::chrono::steady_clock::now();
  12. };

3.编译脚本make.sh

g++ -std=c++11 -g -o Test test.cpp -pthread

三 程序性能测试

1.测试环境:

(1)国产环境(实体机)

CPU:

OS:

(2)Intel环境(虚拟机)

CPU:

 OS:

2.测试结果对比:

(1)国产环境测试结果(执行两次)

 

(2)Interl环境测试结果(执行两次)

 

3.测试结论

(1)使用std::async优化后的并行计算比串行更节省时间;

(2)国产化环境并没有太多优势,反而调用C++相关库更慢。

https://github.com/wangzhicheng2013/async_parallel

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/602507
推荐阅读
相关标签
  

闽ICP备14008679号