Skip to content

Commit 702f348

Browse files
committed
Fix timing with TBB and a minor GCC bug
1 parent 96e5355 commit 702f348

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

examples/TimeTBB.cpp

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
* @date November 6, 2013
1616
*/
1717

18-
#include <gtsam/global_includes.h>
1918
#include <gtsam/base/Matrix.h>
19+
#include <gtsam/global_includes.h>
2020

21-
#include <map>
21+
#include <algorithm>
2222
#include <iostream>
23+
#include <map>
24+
#include <random>
2325

2426
using namespace std;
2527
using namespace gtsam;
@@ -53,18 +55,30 @@ struct WorkerWithoutAllocation
5355
{
5456
vector<double>& results;
5557

56-
WorkerWithoutAllocation(vector<double>& results) : results(results) {}
58+
WorkerWithoutAllocation(vector<double>& results)
59+
: results(results), gen(rd()), dis(-1.0, 1.0) {}
60+
61+
// Copy constructor for TBB
62+
WorkerWithoutAllocation(const WorkerWithoutAllocation& other)
63+
: results(other.results), gen(rd()), dis(-1.0, 1.0) {}
5764

5865
void operator()(const tbb::blocked_range<size_t>& r) const
5966
{
6067
for(size_t i = r.begin(); i != r.end(); ++i)
6168
{
62-
FixedMatrix m1 = FixedMatrix::Random();
63-
FixedMatrix m2 = FixedMatrix::Random();
69+
FixedMatrix m1;
70+
FixedMatrix m2;
71+
std::generate(m1.data(), m1.data() + m1.size(),
72+
[&]() { return dis(gen); });
73+
std::generate(m2.data(), m2.data() + m2.size(),
74+
[&]() { return dis(gen); });
6475
FixedMatrix prod = m1 * m2;
6576
results[i] = prod.norm();
6677
}
6778
}
79+
std::random_device rd;
80+
mutable std::minstd_rand gen;
81+
mutable std::uniform_real_distribution<double> dis;
6882
};
6983

7084
/* ************************************************************************* */
@@ -86,9 +100,11 @@ map<int, double> testWithoutMemoryAllocation(int num_threads)
86100
tbb::tick_count t0 = tbb::tick_count::now();
87101

88102
// Run parallel code (as a task group) inside of task arena
89-
arena.execute([&]{
90-
tg.run_and_wait([&]{
91-
tbb::parallel_for(tbb::blocked_range<size_t>(0, numberOfProblems), WorkerWithoutAllocation(results));
103+
arena.execute([&] {
104+
tg.run_and_wait([&] {
105+
tbb::parallel_for(
106+
tbb::blocked_range<size_t>(0, numberOfProblems, grainSize),
107+
WorkerWithoutAllocation(results));
92108
});
93109
});
94110

@@ -105,7 +121,12 @@ struct WorkerWithAllocation
105121
{
106122
vector<double>& results;
107123

108-
WorkerWithAllocation(vector<double>& results) : results(results) {}
124+
WorkerWithAllocation(vector<double>& results)
125+
: results(results), gen(rd()), dis(-1.0, 1.0) {}
126+
127+
// Copy constructor for TBB
128+
WorkerWithAllocation(const WorkerWithAllocation& other)
129+
: results(other.results), gen(rd()), dis(-1.0, 1.0) {}
109130

110131
void operator()(const tbb::blocked_range<size_t>& r) const
111132
{
@@ -119,8 +140,10 @@ struct WorkerWithAllocation
119140
double *proddata = allocator.allocate(problemSize * problemSize);
120141
Eigen::Map<Matrix> prod(proddata, problemSize, problemSize);
121142

122-
m1 = Eigen::Matrix4d::Random(problemSize, problemSize);
123-
m2 = Eigen::Matrix4d::Random(problemSize, problemSize);
143+
std::generate(m1.data(), m1.data() + m1.size(),
144+
[&]() { return dis(gen); });
145+
std::generate(m2.data(), m2.data() + m2.size(),
146+
[&]() { return dis(gen); });
124147
prod = m1 * m2;
125148
results[i] = prod.norm();
126149

@@ -129,6 +152,10 @@ struct WorkerWithAllocation
129152
allocator.deallocate(proddata, problemSize * problemSize);
130153
}
131154
}
155+
156+
std::random_device rd;
157+
mutable std::minstd_rand gen;
158+
mutable std::uniform_real_distribution<double> dis;
132159
};
133160

134161
/* ************************************************************************* */
@@ -150,9 +177,11 @@ map<int, double> testWithMemoryAllocation(int num_threads)
150177
tbb::tick_count t0 = tbb::tick_count::now();
151178

152179
// Run parallel code (as a task group) inside of task arena
153-
arena.execute([&]{
154-
tg.run_and_wait([&]{
155-
tbb::parallel_for(tbb::blocked_range<size_t>(0, numberOfProblems), WorkerWithAllocation(results));
180+
arena.execute([&] {
181+
tg.run_and_wait([&] {
182+
tbb::parallel_for(
183+
tbb::blocked_range<size_t>(0, numberOfProblems, grainSize),
184+
WorkerWithAllocation(results));
156185
});
157186
});
158187

gtsam/geometry/Gal3.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
* refer to the aforementioned paper.
2121
*/
2222

23+
// GCC bug workaround
24+
#if defined(__GNUC__) && __GNUC__ == 15
25+
#pragma GCC diagnostic push
26+
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
27+
#endif
28+
2329
#include <gtsam/base/Matrix.h>
2430
#include <gtsam/base/numericalDerivative.h>
2531
#include <gtsam/geometry/Event.h>

0 commit comments

Comments
 (0)