gpt4 book ai didi

c++ - 使用 PPL 查找数组中的最大元素

转载 作者:行者123 更新时间:2023-12-03 12:50:21 24 4
gpt4 key购买 nike

我需要实现一个函数,使用 ppl.h 查找 float 组中的最大元素。

我有这个代码,基于 this answer :

float find_largest_element_in_matrix_PPL(float* m, size_t dims)
{
float max_element;
int row, col;
concurrency::combinable<float> locals([] { return INT_MIN + 0.f; });
concurrency::parallel_for_each(size_t(0), dims * dims, [&locals](int curr)
{
float & localMax = locals.local();
localMax = max<float>(localMax, curr);
});

max_element = locals.combine([](float left, float right) { return max<float>(left, right); });
cout << max_element << endl;
return max_element;
}

但是,此代码有一个问题:

  • 执行前抛出以下异常:

Error C2780 'void Concurrency::_Parallel_for_each_impl(const _Random_iterator &,const _Random_iterator &,const _Function &,_Partitioner &&,std::random_access_iterator_tag)': expects 5 arguments - 4 provided parp D:\Microsoft Visual Studio 14.0\VC\include\ppl.h 2987

Error C2780 'void Concurrency::_Parallel_for_each_impl(_Forward_iterator,const _Forward_iterator &,const _Function &,const Concurrency::auto_partitioner &,std::forward_iterator_tag)': expects 5 arguments - 4 provided parp D:\Microsoft Visual Studio 14.0\VC\include\ppl.h 2987

Error C2893 Failed to specialize function template 'iterator_traits<_Iter>::iterator_category std::_Iter_cat(const _Iter &)' parp D:\Microsoft Visual Studio 14.0\VC\include\ppl.h 2987

<小时/>
  1. 您能帮我解决这个问题吗?

  2. 如何重写代码以使用 parallel_for? (我无法引用传递给 parallel_for block 中的函数的数组参数)

最佳答案

float SimpleTest::SimpleTestfind_largest_element_in_matrix_PPL(float* m, size_t dims)
{
float max_element;
concurrency::combinable<float> locals([&]{ return INT_MIN + 0.f; });
int last= dims*dims;
concurrency::parallel_for(0, last, [&](int curr)
{
float & localMax = locals.local();
localMax = max<float>(localMax, curr);
});

max_element = locals.combine([](float left, float right) { return max<float>(left, right); });
std::cout << max_element << endl;
return max_element;
}

也许会起作用

关于c++ - 使用 PPL 查找数组中的最大元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34318366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com