#include <iostream>
#include <future>
#include <numeric>
#include <vector>
#include <list>
template <typename It, typename Func>
void map(It first, It last, Func f)
{
while (first != last)
{
f(*first++);
}
}
template <typename It, typename MapFunc, typename FoldFunc>
auto map_reduce(It first, It last, MapFunc f1, FoldFunc f2, size_t num_threads)
-> decltype(f2(f1(*first), f1(*first)))
{
size_t block_size = std::distance(first, last) / num_threads;
std::vector<std::future<int>> results(num_threads);
for (size_t i = 0; i < num_threads; ++i)
{
results[i] = std::async(map, first, last, f1);
}
return 0;
}
int main()
{
std::list<int> ls = { 1, 2, 3 };
auto sum = map_reduce(ls.begin(), ls.end(),
[](int i) {return i; },
std::plus<int>(),
3);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnV0dXJlPgojaW5jbHVkZSA8bnVtZXJpYz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGxpc3Q+Cgp0ZW1wbGF0ZSA8dHlwZW5hbWUgSXQsIHR5cGVuYW1lIEZ1bmM+CnZvaWQgbWFwKEl0IGZpcnN0LCBJdCBsYXN0LCBGdW5jIGYpCnsKCXdoaWxlIChmaXJzdCAhPSBsYXN0KQoJewoJCWYoKmZpcnN0KyspOwoJfQp9Cgp0ZW1wbGF0ZSA8dHlwZW5hbWUgSXQsIHR5cGVuYW1lIE1hcEZ1bmMsIHR5cGVuYW1lIEZvbGRGdW5jPgphdXRvIG1hcF9yZWR1Y2UoSXQgZmlyc3QsIEl0IGxhc3QsIE1hcEZ1bmMgZjEsIEZvbGRGdW5jIGYyLCBzaXplX3QgbnVtX3RocmVhZHMpCgktPiBkZWNsdHlwZShmMihmMSgqZmlyc3QpLCBmMSgqZmlyc3QpKSkKewoJc2l6ZV90IGJsb2NrX3NpemUgPSBzdGQ6OmRpc3RhbmNlKGZpcnN0LCBsYXN0KSAvIG51bV90aHJlYWRzOwoKCXN0ZDo6dmVjdG9yPHN0ZDo6ZnV0dXJlPGludD4+IHJlc3VsdHMobnVtX3RocmVhZHMpOwoJZm9yIChzaXplX3QgaSA9IDA7IGkgPCBudW1fdGhyZWFkczsgKytpKQoJewoJCXJlc3VsdHNbaV0gPSBzdGQ6OmFzeW5jKG1hcCwgZmlyc3QsIGxhc3QsIGYxKTsKCX0KCglyZXR1cm4gMDsKfQoKCmludCBtYWluKCkKewoJc3RkOjpsaXN0PGludD4gbHMgPSB7IDEsIDIsIDMgfTsKCWF1dG8gc3VtID0gbWFwX3JlZHVjZShscy5iZWdpbigpLCBscy5lbmQoKSwKCQlbXShpbnQgaSkge3JldHVybiBpOyB9LAoJCXN0ZDo6cGx1czxpbnQ+KCksCgkJMyk7CgoJcmV0dXJuIDA7Cn0=
prog.cpp: In instantiation of ‘decltype (f2(f1((* first)), f1((* first)))) map_reduce(It, It, MapFunc, FoldFunc, size_t) [with It = std::_List_iterator<int>; MapFunc = main()::<lambda(int)>; FoldFunc = std::plus<int>; decltype (f2(f1((* first)), f1((* first)))) = int; size_t = long unsigned int]’:
prog.cpp:38:4: required from here
prog.cpp:25:26: error: no matching function for call to ‘async(<unresolved overloaded function type>, std::_List_iterator<int>&, std::_List_iterator<int>&, main()::<lambda(int)>&)’
results[i] = std::async(map, first, last, f1);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from prog.cpp:2:0:
/usr/include/c++/6/future:1709:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_BoundArgs>::type ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)
async(launch __policy, _Fn&& __fn, _Args&&... __args)
^~~~~
/usr/include/c++/6/future:1709:5: note: template argument deduction/substitution failed:
prog.cpp:25:26: note: cannot convert ‘map’ (type ‘<unresolved overloaded function type>’) to type ‘std::launch’
results[i] = std::async(map, first, last, f1);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from prog.cpp:2:0:
/usr/include/c++/6/future:1739:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_BoundArgs>::type ...)>::type> std::async(_Fn&&, _Args&& ...)
async(_Fn&& __fn, _Args&&... __args)
^~~~~
/usr/include/c++/6/future:1739:5: note: template argument deduction/substitution failed:
prog.cpp:25:26: note: couldn't deduce template parameter ‘_Fn’
results[i] = std::async(map, first, last, f1);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~