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

标签 c++ parallel-processing ppl

我需要实现一个函数,使用 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/

相关文章:

c++ - 从文件中读取对象时 undefined symbol

使用信号量和共享变量进行 Java 编程

C++ 线程池中线程的执行顺序

r - 有没有办法跟踪并行随机森林构建过程中的进度?

java - 如何对 hadoop 中的所有键/值对进行一般化简

c++ - 并行化 for 循环不会带来性能提升

c++ - 终止 PPL 线程池中的线程

c++ - 从 8 位转换为 1 字节

c++ - 如何在C++中绘制函数

java - C++ 或 Java 中的 Linux 守护进程?