0
votes

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:

  • It throws the following exception before executing:

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)

1
You are using parallel_for parameters and the parallel_for_each function. You should probably use parallel_for not parallel_for each in this case. - Rick
@Rick thanks, I changed the code to parallel_for and the error was gone. But now the function returns just the index of the last element in array. Any ideas on how to make it work correctly? - Denis Yakovenko
@Rick sorry for misleading, it doesn't return any index, it just returns zero. - Denis Yakovenko

1 Answers

1
votes
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