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.
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.
来源:https://stackoverflow.com/questions/22939121/find-max-value-with-ppl-h