Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Little Trouble in Big Data - Part 2

improving CPU utilization across cores

const std::vector colIndices = {0, 1, 2, 3, ... };
const std::vector markerIndices = randomise(colIndices);

for (i = 0; i < maxIterations; ++i) {
    for (j = 0; j < numCols; ++j) {
        const unsigned int marker = markerIndices[j];
        const auto col = data.mappedZ.col(marker);

        output += doStuff(col);
    }

    if (i % numIterations == 0)
        writeOutput(output);
}
const size_t grainSize = 10000;

double parallelDotProduct(const VectorXf &Cx, const VectorXd &y_tilde)
{
    const unsigned long startIndex = 0;
    const unsigned long endIndex = static_cast(y_tilde.size());

    auto apply = [&](const blocked_range& r, double initialValue) {
        const long start = static_cast(r.begin());
        const long count = static_cast(r.end() - r.begin());
        const auto sum = initialValue + (Cx.segment(start, count).cast() *
                                          y_tilde.segment(start, count)).sum();
        return sum;
    };

    auto combine = [](double a, double b) { return a + b; };

    return parallel_reduce(blocked_range(startIndex, endIndex, grainSize), 0.0,
                            apply, combine);
}

SeanHarmer

Sean Harmer

Managing Director KDAB UK

Learn Modern C++

Learn more