Skip to content

Commit f0fab62

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

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

examples/TimeTBB.cpp

Lines changed: 33 additions & 8 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
/* ************************************************************************* */
@@ -105,7 +119,12 @@ struct WorkerWithAllocation
105119
{
106120
vector<double>& results;
107121

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

110129
void operator()(const tbb::blocked_range<size_t>& r) const
111130
{
@@ -119,8 +138,10 @@ struct WorkerWithAllocation
119138
double *proddata = allocator.allocate(problemSize * problemSize);
120139
Eigen::Map<Matrix> prod(proddata, problemSize, problemSize);
121140

122-
m1 = Eigen::Matrix4d::Random(problemSize, problemSize);
123-
m2 = Eigen::Matrix4d::Random(problemSize, problemSize);
141+
std::generate(m1.data(), m1.data() + m1.size(),
142+
[&]() { return dis(gen); });
143+
std::generate(m2.data(), m2.data() + m2.size(),
144+
[&]() { return dis(gen); });
124145
prod = m1 * m2;
125146
results[i] = prod.norm();
126147

@@ -129,6 +150,10 @@ struct WorkerWithAllocation
129150
allocator.deallocate(proddata, problemSize * problemSize);
130151
}
131152
}
153+
154+
std::random_device rd;
155+
mutable std::minstd_rand gen;
156+
mutable std::uniform_real_distribution<double> dis;
132157
};
133158

134159
/* ************************************************************************* */

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)