Advertisement
Guest User

Untitled

a guest
Oct 13th, 2020
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. // реализация функции mapreduce
  2. template<typename It, typename UnFunc, typename BiFunc>
  3. auto map_reduce(It p, It q, UnFunc f1, BiFunc f2, size_t threads)->decltype(f2(f1(*p), f1(*p)))
  4. {
  5.     using ResType = decltype(f2(f1(*p), f1(*p)));
  6.     size_t block_size = std::distance(p, q) / threads;
  7.    
  8.     std::vector<It> first(threads);
  9.     std::vector<It> last(threads);
  10.     int c = 0;
  11.     for (int i = 0; i < threads; i++)
  12.     {
  13.         first[i] = p;
  14.         while (c < (i+1)*block_size)
  15.         {
  16.             last[i] = ++p;
  17.             c++;
  18.         }
  19.     }
  20.     last[threads-1] = q;
  21.  
  22.     std::vector <ResType> VectResult(threads);
  23.     for (int i = 0; i < threads; i++)
  24.     {
  25.         ResType res;
  26.         std::thread t([&res]() {res = map<It, UnFunc, BiFunc>(p, q, f1, f2);});
  27.         t.join();
  28.         VectResult[i] = res;
  29.     }
  30.  
  31.     /*std::vector<std::future<ResType>> VectFuture(threads);
  32.    
  33.    
  34.     for (int i = 0; i < threads; i++)
  35.     {
  36.         VectFuture[i] = std::async(std::launch::async, map <It, UnFunc, BiFunc>, first[i], last[i], f1, f2);
  37.     }
  38.     ResType res = VectFuture[0].get();
  39.     for (int i = 1; i < threads; i++)
  40.     {
  41.         res = f2(res, VectFuture[i].get());
  42.     }*/
  43.  
  44.     return res;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement