使用PPL在数组中查找max元素

时间:2015-12-16 17:27:43

标签: c++ parallel-processing ppl

我需要实现一个函数,它可以使用ppl.h找到浮点数组中的最大元素。

我有这段代码,基于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;
}

但是,此代码存在问题:

  • 在执行之前抛出以下异常:
  

错误C2780&#39; void Concurrency :: _ Parallel_for_each_impl(const   _Random_iterator&amp;,const _Random_iterator&amp;,const _Function&amp;,_ Partitioner&amp;&amp;,std :: random_access_iterator_tag)&#39;:预计5   参数 - 4提供了parp D:\ Microsoft Visual Studio   14.0 \ VC \ include \ ppl.h 2987

     

错误C2780&#39; void Concurrency :: _ Parallel_for_each_impl(_Forward_iterator,const   _Forward_iterator&amp;,const _Function&amp;,const Concurrency :: auto_partitioner&amp;,std :: forward_iterator_tag)&#39;:预计5   参数 - 4提供了parp D:\ Microsoft Visual Studio   14.0 \ VC \ include \ ppl.h 2987

     

错误C2893无法专门化功能模板&#39; iterator_traits&lt; _Iter&gt; :: iterator_category   std :: _ Iter_cat(const _Iter&amp;)&#39; parp D:\ Microsoft Visual Studio   14.0 \ VC \ include \ ppl.h 2987

  1. 你能帮我解决一下这个问题吗?

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

1 个答案:

答案 0 :(得分:1)

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

也许会起作用