Exemplo n.º 1
0
bool ParallelComputeAverages(Numbers const &numbers, Averages &averages)
{
    if (numbers.empty())
    {
        return false;
    }


    // sort numbers for mode and median
    Numbers sorted = numbers;
    Averages avgResult;
    int modeResult;
    double medianResult;

    // sort numbers for median / mode
    tbb::parallel_sort(sorted);

    auto medianFunc = [&medianResult, &sorted]()
    {
        medianResult = ComputeMedian(sorted);
    };

    auto avgFunc = [&avgResult, &numbers]()
    {
        auto grainSize = numbers.size() / thread::hardware_concurrency();
        Averages avg{0, 0, 0, 0, 0, 0};
        avgResult = tbb::parallel_reduce(
                Range(numbers.begin(), numbers.end(), grainSize), avg,
                CalcIntermediateAverages,
                AddIntermediateAverages);
    };

    auto modeFunc = [&modeResult, &sorted]()
    {
        modeResult = ComputeMode(sorted);
    };

    // invoke parallel execution of calculations
    tbb::parallel_invoke(avgFunc, modeFunc, medianFunc);

    // set result
    double size = (double) numbers.size();
    averages.arithmeticMean = avgResult.arithmeticMean / size;
    averages.geometricMean = exp(avgResult.geometricMean / size);
    averages.harmonicMean = size / avgResult.harmonicMean;
    averages.quadraticMean = sqrt(avgResult.quadraticMean / size);
    averages.mode = modeResult;
    averages.median = medianResult;

    return true;
}
Exemplo n.º 2
0
bool ComputeAverages(Numbers const &numbers, Averages &averages)
{
    if (numbers.empty())
    {
        return false;
    }
    Numbers sortedNumbers = numbers;
    sort(sortedNumbers.begin(),
         sortedNumbers.end());  // sorting required for mode and median
    averages.mode = ComputeMode(sortedNumbers);
    averages.median = ComputeMedian(sortedNumbers);
    ComputeMeans(numbers, averages);
    return true;
}