用ppl.h查找最大值 [英] Find max value with ppl.h
问题描述
在C ++的ppl库中是否有一个简单的函数,您可以在其中执行Concurrency::max(vec)
之类的操作,其中vec是数字的向量?我可以自己写,但我希望自己可以省下工作.
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.
对不起,我可能还不够清楚.我需要使用max函数来利用并行化.
Sorry I was not clear enough maybe. I need the max function to utilize parallelization.
推荐答案
没有内置任何内容,但是使用可组合(归约变量)和并行循环(此处为parallel_for_each)很简单.但是,如果您正在做的工作只是数字的最大值",除非您正在看的数字量很大,那么可能很难看到速度的提高.
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.
您可以在 msdn 上了解有关此内容的更多信息
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;
}
这篇关于用ppl.h查找最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!