并行求和

  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 }

 

posted @ 2015-06-01 15:14  -学以致用-  阅读(408)  评论(0)    收藏  举报