Pregunta

parallel_reduce offers 2 interfaces, the one for the lambdas can be used like showed in the previous link

#include "tbb/parallel_reduce.h"
#include "tbb/blocked_range.h"

using namespace tbb;

float ParallelSum( float array[], size_t n ) {
    return parallel_reduce( 
        blocked_range<float*>( array, array+n ), 
        0.f, 
        [](const blocked_range<float*>& r, float init)->float {
            for( float* a=r.begin(); a!=r.end(); ++a ) 
                init += *a;
            return init;
        },
        []( float x, float y )->float { // what this lambda does ?
            return x+y;
        }
    );                    
}

the second lambda fits in the const Reduction& reduction signature, I'm studying this lambda trying to change the body or the values, but it does exactly nothing ( even if I put a cout inside, nothing happens ), looks like it's there for no reason at all.

What is the purpose of this Reduction ? Has something to do with the 0f ? For what I got that 0f is a predicate used to detect what are the values that can be skipped, but I still can't figure out what that lambda does ...

¿Fue útil?

Solución

The first lambda computes the expression over the range, giving a result for that range.

The second lambda combines the results from two range computations, giving a result for the combination of the two ranges.

The second lambda will only get called if the parallel_reduce breaks up the operation into multiple ranges.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top