Find max value with ppl.h

ε祈祈猫儿з 提交于 2019-12-01 23:24:48

There is nothing built in, but it's straightforward with combinable (a reduction variable) and a parallel loop (here parallel_for_each). However if the work you are doing is only 'max' of numbers unless the amount of numbers you are looking at is very large, it may be difficult to see speedups.

You can read more about it on msdn:

#include <ppl.h>
#include <climits>
#include <vector>
#include <numeric>
#include <iostream>
using namespace Concurrency;
int main(int argc, _TCHAR* argv[])
{
    std::vector<int> vec(10);
    std::iota( begin(vec), end(vec), 1);
    combinable<int> locals([]{ return INT_MIN; });
    parallel_for_each( begin(vec), end(vec), [&locals](int cur){
        auto & localMax = locals.local();
        localMax = std::max(cur, localMax);
    });
    std::cout << "max is " << locals.combine([](int left, int right){ return std::max<int>(left, right);}) << std::endl;
    return 0;
}

Is it just a std::vector? If so you can use max_element.

auto it = std::max_element(std::begin(vec), std::end(vec));

The iterator it then points at the vector element that has the maximum value.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!