Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // реализация функции mapreduce
- template<typename It, typename UnFunc, typename BiFunc>
- auto map_reduce(It p, It q, UnFunc f1, BiFunc f2, size_t threads)->decltype(f2(f1(*p), f1(*p)))
- {
- using ResType = decltype(f2(f1(*p), f1(*p)));
- size_t block_size = std::distance(p, q) / threads;
- std::vector<It> first(threads);
- std::vector<It> last(threads);
- int c = 0;
- for (int i = 0; i < threads; i++)
- {
- first[i] = p;
- while (c < (i+1)*block_size)
- {
- last[i] = ++p;
- c++;
- }
- }
- last[threads-1] = q;
- std::vector <ResType> VectResult(threads);
- for (int i = 0; i < threads; i++)
- {
- ResType res;
- std::thread t([&res]() {res = map<It, UnFunc, BiFunc>(p, q, f1, f2);});
- t.join();
- VectResult[i] = res;
- }
- /*std::vector<std::future<ResType>> VectFuture(threads);
- for (int i = 0; i < threads; i++)
- {
- VectFuture[i] = std::async(std::launch::async, map <It, UnFunc, BiFunc>, first[i], last[i], f1, f2);
- }
- ResType res = VectFuture[0].get();
- for (int i = 1; i < threads; i++)
- {
- res = f2(res, VectFuture[i].get());
- }*/
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement