c++parallel-processingppl

Find max element in array using PPL


I need to implement a function that would find the largest element in the array of floats using ppl.h.

I have this code, based on 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;
}

However, there's an issue with this code:

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. Could you please help me with resolving that issue?

  2. How can I rewrite the code to make use of parallel_for? (I cannot reference the array argument passed to the function in the parallel_for block)


Solution

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

    Maybe it will work