diff --git a/examples/TimeTBB.cpp b/examples/TimeTBB.cpp index 036fabd1b..a513916b7 100644 --- a/examples/TimeTBB.cpp +++ b/examples/TimeTBB.cpp @@ -46,27 +46,29 @@ struct ResultWithThreads typedef map Results; +/* ************************************************************************* */ +struct WorkerWithoutAllocation +{ + vector& results; + + WorkerWithoutAllocation(vector& results) : results(results) {} + + void operator()(const tbb::blocked_range& r) const + { + for(size_t i = r.begin(); i != r.end(); ++i) + { + FixedMatrix m1 = FixedMatrix::Random(); + FixedMatrix m2 = FixedMatrix::Random(); + FixedMatrix prod = m1 * m2; + results[i] = prod.norm(); + } + } +}; + /* ************************************************************************* */ map testWithoutMemoryAllocation() { // A function to do some matrix operations without allocating any memory - struct Worker - { - vector& results; - - Worker(vector& results) : results(results) {} - - void operator()(const tbb::blocked_range& r) const - { - for(size_t i = r.begin(); i != r.end(); ++i) - { - FixedMatrix m1 = FixedMatrix::Random(); - FixedMatrix m2 = FixedMatrix::Random(); - FixedMatrix prod = m1 * m2; - results[i] = prod.norm(); - } - } - }; // Now call it vector results(numberOfProblems); @@ -76,7 +78,7 @@ map testWithoutMemoryAllocation() BOOST_FOREACH(size_t grainSize, grainSizes) { tbb::tick_count t0 = tbb::tick_count::now(); - tbb::parallel_for(tbb::blocked_range(0, numberOfProblems), Worker(results)); + tbb::parallel_for(tbb::blocked_range(0, numberOfProblems), WorkerWithoutAllocation(results)); tbb::tick_count t1 = tbb::tick_count::now(); cout << "Without memory allocation, grain size = " << grainSize << ", time = " << (t1 - t0).seconds() << endl; timingResults[grainSize] = (t1 - t0).seconds(); @@ -85,39 +87,41 @@ map testWithoutMemoryAllocation() return timingResults; } +/* ************************************************************************* */ +struct WorkerWithAllocation +{ + vector& results; + + WorkerWithAllocation(vector& results) : results(results) {} + + void operator()(const tbb::blocked_range& r) const + { + tbb::cache_aligned_allocator allocator; + for(size_t i = r.begin(); i != r.end(); ++i) + { + double *m1data = allocator.allocate(problemSize * problemSize); + Eigen::Map m1(m1data, problemSize, problemSize); + double *m2data = allocator.allocate(problemSize * problemSize); + Eigen::Map m2(m2data, problemSize, problemSize); + double *proddata = allocator.allocate(problemSize * problemSize); + Eigen::Map prod(proddata, problemSize, problemSize); + + m1 = Eigen::Matrix4d::Random(problemSize, problemSize); + m2 = Eigen::Matrix4d::Random(problemSize, problemSize); + prod = m1 * m2; + results[i] = prod.norm(); + + allocator.deallocate(m1data, problemSize * problemSize); + allocator.deallocate(m2data, problemSize * problemSize); + allocator.deallocate(proddata, problemSize * problemSize); + } + } +}; + /* ************************************************************************* */ map testWithMemoryAllocation() { // A function to do some matrix operations with allocating memory - struct Worker - { - vector& results; - - Worker(vector& results) : results(results) {} - - void operator()(const tbb::blocked_range& r) const - { - tbb::cache_aligned_allocator allocator; - for(size_t i = r.begin(); i != r.end(); ++i) - { - double *m1data = allocator.allocate(problemSize * problemSize); - Eigen::Map m1(m1data, problemSize, problemSize); - double *m2data = allocator.allocate(problemSize * problemSize); - Eigen::Map m2(m2data, problemSize, problemSize); - double *proddata = allocator.allocate(problemSize * problemSize); - Eigen::Map prod(proddata, problemSize, problemSize); - - m1 = Eigen::Matrix4d::Random(problemSize, problemSize); - m2 = Eigen::Matrix4d::Random(problemSize, problemSize); - prod = m1 * m2; - results[i] = prod.norm(); - - allocator.deallocate(m1data, problemSize * problemSize); - allocator.deallocate(m2data, problemSize * problemSize); - allocator.deallocate(proddata, problemSize * problemSize); - } - } - }; // Now call it vector results(numberOfProblems); @@ -127,7 +131,7 @@ map testWithMemoryAllocation() BOOST_FOREACH(size_t grainSize, grainSizes) { tbb::tick_count t0 = tbb::tick_count::now(); - tbb::parallel_for(tbb::blocked_range(0, numberOfProblems), Worker(results)); + tbb::parallel_for(tbb::blocked_range(0, numberOfProblems), WorkerWithAllocation(results)); tbb::tick_count t1 = tbb::tick_count::now(); cout << "With memory allocation, grain size = " << grainSize << ", time = " << (t1 - t0).seconds() << endl; timingResults[grainSize] = (t1 - t0).seconds();