并行求和

  1 #include <vector>
  2 #include <iostream>
  3 #include <thread>
  4 #include <mutex>
  5 
  6 using namespace std;
  7 
  8 int psum[256];
  9 std::mutex g_mtx;
 10 int g_sum = 0;
 11 
 12 void add0(int vargp, int var_first, int var_end)
 13 {
 14     for (int i = var_first; i <= var_end; i++)
 15     {
 16         psum[vargp] += i;
 17     }
 18 }
 19 
 20 void add1(int var_first, int var_end)
 21 {
 22     int result = 0;
 23     for (int i = var_first; i <= var_end; i++)
 24     {
 25         result += i;
 26     }
 27 
 28     g_mtx.lock();
 29     g_sum += result;
 30     g_mtx.unlock();
 31 }
 32 
 33 void test0(const int num)
 34 {
 35     auto start_comp = std::chrono::high_resolution_clock::now();
 36     int sum = 0;
 37     for (int i = 0; i < num; i++)
 38     {
 39         sum += i;
 40     }
 41     auto end_comp = std::chrono::high_resolution_clock::now();
 42 
 43     printf("sum = %d, %llu microseconds. \n", sum, std::chrono::duration_cast<std::chrono::microseconds>(end_comp - start_comp).count());
 44 }
 45 
 46 void test1(const int num)
 47 {
 48     unsigned long const hardware_threads = std::thread::hardware_concurrency();
 49     unsigned long const num_threads = hardware_threads != 0 ? hardware_threads : 2;
 50     
 51     std::vector<std::thread> threads(num_threads);
 52     auto start_comp = std::chrono::high_resolution_clock::now();
 53     for (int i = 0; i < (int)num_threads; i++)
 54     {
 55         long long first = (num / num_threads) * i;
 56         long long end = 0;
 57         if (i != num_threads - 1)
 58         {
 59             end = (num / num_threads) * (i + 1) - 1;
 60         }
 61         else
 62         {
 63             end = num - 1;
 64         }
 65 
 66         threads[i] = std::thread(add0, i, first, end);
 67     }
 68 
 69     for (auto& th : threads)
 70     {
 71         th.join();
 72     }
 73 
 74     int sum = 0;
 75     for (int i = 0; i < (int)num_threads; i++)
 76     {
 77         sum += psum[i];
 78     }
 79     auto end_comp = std::chrono::high_resolution_clock::now();
 80 
 81     printf("sum = %d, %llu microseconds. \n", sum, std::chrono::duration_cast<std::chrono::microseconds>(end_comp - start_comp).count());
 82 }
 83 
 84 void test2(const int num)
 85 {
 86     unsigned long const hardware_threads = std::thread::hardware_concurrency();
 87     unsigned long const num_threads = hardware_threads != 0 ? hardware_threads : 2;
 88 
 89     std::vector<std::thread> threads(num_threads);
 90     auto start_comp = std::chrono::high_resolution_clock::now();
 91     for (int i = 0; i < (int)num_threads; i++)
 92     {
 93         int first = (num / num_threads) * i;
 94         int end = 0;
 95         if (i != num_threads - 1)
 96         {
 97             end = (num / num_threads) * (i + 1) - 1;
 98         }
 99         else
100         {
101             end = num - 1;
102         }
103         threads[i] = std::thread(add1, first, end);
104     }
105 
106     for (auto& th : threads)
107     {
108         th.join();
109     }
110     auto end_comp = std::chrono::high_resolution_clock::now();
111 
112     printf("sum = %d, %llu microseconds. \n", g_sum, std::chrono::duration_cast<std::chrono::microseconds>(end_comp - start_comp).count());
113 }
114 
115 int main()
116 {
117     int num = 10001;
118 
119     test0(num);
120     test1(num);
121     test2(num);
122 
123     getchar();
124 
125     return 0;
126 }

 

转载于:https://www.cnblogs.com/WuhanLiukai/p/4544013.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值