Find max value with ppl.h

2019-02-27 15:25发布

问题:

Is there a simple function in the ppl library for C++ where you can do something like Concurrency::max(vec) where vec is a vector of numbers? I can write my own, but I was hoping I could save myself the work.

Edit: Sorry I was not clear enough maybe. I need the max function to utilize parallelization.

回答1:

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;
}


回答2:

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.



标签: c++ max ppl