Tbb :: parallel_reduce and std :: accumulate results are different

I am looking into the Intel TBB library . When all values ​​are summed up, the std::vector

result tbb::parallel_reduce

differs from std::accumulate

in the case of more than 16,777,220 elements in the vector (errors in 16,777,320 elements). Here's my minimal working example:

#include <iostream>
#include <vector>
#include <numeric>
#include <limits>
#include "tbb/tbb.h"

int main(int argc, const char * argv[]) {

    int count = std::numeric_limits<int>::max() * 0.0079 - 187800; // - 187900 works

    std::vector<float> heights(size);
    std::fill(heights.begin(), heights.end(), 1.0f);

    float ssum = std::accumulate(heights.begin(), heights.end(), 0);
    float psum = tbb::parallel_reduce(tbb::blocked_range<std::vector<float>::iterator>(heights.begin(), heights.end()), 0,
                                      [](tbb::blocked_range<std::vector<float>::iterator> const& range, float init) {
                                          return std::accumulate(range.begin(), range.end(), init);
                                      }, std::plus<float>()
                                      );

    std::cout << std::endl << " Heights serial sum: " << ssum << "   parallel sum: " << psum;
    return 0;
}

      

which outputs on my OSX 10.10.3 with XCode 6.3.1 and stable tbb 4.3-20141023 (spilled from Brew ):

Heights serial sum: 1.67772e+07   parallel sum: 1.67773e+07

      

Why? Should I report a bug to the TBB developers?


Additional testing applying your answers:

 correct value is: 1949700403
 cause we add 1.0f to zero 1949700403 times

 using (int) init values:
 Runtime: 17.407 sec. Heights serial   sum: 16777216.000, wrong
 Runtime:  8.482 sec. Heights parallel sum: 131127368.000, wrong

 using (float) init values:
 Runtime: 12.594 sec. Heights serial   sum: 16777216.000, wrong
 Runtime:  5.044 sec. Heights parallel sum: 303073632.000, wrong

 using (double) initial values:
 Runtime: 13.671 sec. Heights serial   sum: 1949700352.000, wrong
 Runtime:  5.343 sec. Heights parallel sum: 263690016.000, wrong

 using (double) initial values and tbb::parallel_deterministic_reduce:
 Runtime: 13.463 sec. Heights serial   sum: 1949700352.000, wrong
 Runtime: 99.031 sec. Heights parallel sum: 1949700352.000, wrong >>> almost 10x slower !

      

Why do all call cuts cause the wrong amount? Not enough (double)

?
Here's my test code:

    #include <iostream>
    #include <vector>
    #include <numeric>
    #include <limits>
    #include <sys/time.h>
    #include <iomanip>
    #include "tbb/tbb.h"
    #include <cmath>

    class StopWatch {
    private:
        double elapsedTime;
        timeval startTime, endTime;
    public:
        StopWatch () : elapsedTime(0) {}
        void startTimer() {
            elapsedTime = 0;
            gettimeofday(&startTime, 0);
        }
        void stopNprintTimer() {
            gettimeofday(&endTime, 0);
            elapsedTime = (endTime.tv_sec - startTime.tv_sec) * 1000.0;             // compute sec to ms
            elapsedTime += (endTime.tv_usec - startTime.tv_usec) / 1000.0;          // compute us to ms and add
            std::cout << " Runtime: " << std::right << std::setw(6) << elapsedTime / 1000 << " sec.";             // show in sec
        }
    };

    int main(int argc, const char * argv[]) {

        StopWatch watch;
        std::cout << std::fixed << std::setprecision(3) << "" << std::endl;
        size_t count = std::numeric_limits<int>::max() * 0.9079;

        std::vector<float> heights(count);
        std::cout << " Vector size: " << count << std::endl;
        std::fill(heights.begin(), heights.end(), 1.0f);

        watch.startTimer();
        float ssum = std::accumulate(heights.begin(), heights.end(), 0.0); // change type of initial value here
        watch.stopNprintTimer();
        std::cout << " Heights serial   sum: " << std::right << std::setw(8) << ssum << std::endl;

        watch.startTimer();
        float psum = tbb::parallel_reduce(tbb::blocked_range<std::vector<float>::iterator>(heights.begin(), heights.end()), 0.0, // change type of initial value here
                                          [](tbb::blocked_range<std::vector<float>::iterator> const& range, float init) {
                                              return std::accumulate(range.begin(), range.end(), init);
                                          }, std::plus<float>()
                                          );
        watch.stopNprintTimer();
        std::cout << " Heights parallel sum: " << std::right << std::setw(8) << psum << std::endl;

        return 0;
    }

      


Answer my last question: they all produce wrong results because they are not made for integer addition with large numbers. Switching to int resolves that:

[...]
std::vector<int> heights(count);
std::cout << " Vector size: " << count << std::endl;
std::fill(heights.begin(), heights.end(), 1);

watch.startTimer();
int ssum = std::accumulate(heights.begin(), heights.end(), (int)0);
watch.stopNprintTimer();
std::cout << " Heights serial   sum: " << std::right << std::setw(8) << ssum << std::endl;

watch.startTimer();
int psum = tbb::parallel_reduce(tbb::blocked_range<std::vector<int>::iterator>(heights.begin(), heights.end()), (int)0,
                                  [](tbb::blocked_range<std::vector<int>::iterator> const& range, int init) {
                                      return std::accumulate(range.begin(), range.end(), init);
                                  }, std::plus<int>()
                                  );
watch.stopNprintTimer();
std::cout << " Heights parallel sum: " << std::right << std::setw(8) << psum << std::endl;
[...]

      

leads to:

Vector size: 1949700403
Runtime: 13.041 sec. Heights serial   sum: 1949700403, correct
Runtime:  4.728 sec. Heights parallel sum: 1949700403, correct and almost 4x faster

      

+3


source to share


3 answers


Your call std::accumulate

does an integer addition and then converts the result to float

at the end of the calculation. To accumulate over floating point numbers, the accumulator must be float

* .

float ssum = std::accumulate(heights.begin(), heights.end(), 0.0f);
                                                             ^^^^

      




* Or any other type that can accumulate float

correctly.

+8


source


This might fix this problem for you:

Your std :: accumulate call does integer addition and then converts the result to float at the end of the calculation.

BUT floating point addition is NOT an associative operation:



  • Accumulated: (... ((s + a1) + a2) + ...) + an
  • With parralel_reduce: Any parenthesis permutation is possible.

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

+3


source


Other correct answers for "why?" Also, I would add that TBB provides parallel_deterministic_reduce

one that guarantees reproducible results between two or more runs on the same data (but it may still differ in that std :: accumulate). See the blog for the problem and the deterministic algorithm.

So, "Should I report a bug to the TBB developers?" part, the answer is clearly no (unless you find something insufficient on the TBB side).

+1


source







All Articles