-
Notifications
You must be signed in to change notification settings - Fork 773
/
TimeTBB.cpp
218 lines (176 loc) · 7 KB
/
TimeTBB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
/**
* @file TimeTBB.cpp
* @brief Measure task scheduling overhead in TBB
* @author Richard Roberts
* @date November 6, 2013
*/
#include <gtsam/global_includes.h>
#include <gtsam/base/Matrix.h>
#include <map>
#include <iostream>
using namespace std;
using namespace gtsam;
#ifdef GTSAM_USE_TBB
#include <tbb/blocked_range.h> // tbb::blocked_range
#include <tbb/tick_count.h> // tbb::tick_count
#include <tbb/parallel_for.h> // tbb::parallel_for
#include <tbb/cache_aligned_allocator.h> // tbb::cache_aligned_allocator
#include <tbb/task_arena.h> // tbb::task_arena
#include <tbb/task_group.h> // tbb::task_group
static const DenseIndex numberOfProblems = 1000000;
static const DenseIndex problemSize = 4;
typedef Eigen::Matrix<double, problemSize, problemSize> FixedMatrix;
/* ************************************************************************* */
struct ResultWithThreads
{
typedef map<int, double>::value_type value_type;
map<int, double> grainSizesWithoutAllocation;
map<int, double> grainSizesWithAllocation;
};
typedef map<int, ResultWithThreads> Results;
/* ************************************************************************* */
struct WorkerWithoutAllocation
{
vector<double>& results;
WorkerWithoutAllocation(vector<double>& results) : results(results) {}
void operator()(const tbb::blocked_range<size_t>& 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<int, double> testWithoutMemoryAllocation(int num_threads)
{
// A function to do some matrix operations without allocating any memory
// Create task_arena and task_group
tbb::task_arena arena(num_threads);
tbb::task_group tg;
// Now call it
vector<double> results(numberOfProblems);
const vector<size_t> grainSizes = {1, 10, 100, 1000};
map<int, double> timingResults;
for(size_t grainSize: grainSizes)
{
tbb::tick_count t0 = tbb::tick_count::now();
// Run parallel code (as a task group) inside of task arena
arena.execute([&]{
tg.run_and_wait([&]{
tbb::parallel_for(tbb::blocked_range<size_t>(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[(int)grainSize] = (t1 - t0).seconds();
}
return timingResults;
}
/* ************************************************************************* */
struct WorkerWithAllocation
{
vector<double>& results;
WorkerWithAllocation(vector<double>& results) : results(results) {}
void operator()(const tbb::blocked_range<size_t>& r) const
{
tbb::cache_aligned_allocator<double> allocator;
for(size_t i = r.begin(); i != r.end(); ++i)
{
double *m1data = allocator.allocate(problemSize * problemSize);
Eigen::Map<Matrix> m1(m1data, problemSize, problemSize);
double *m2data = allocator.allocate(problemSize * problemSize);
Eigen::Map<Matrix> m2(m2data, problemSize, problemSize);
double *proddata = allocator.allocate(problemSize * problemSize);
Eigen::Map<Matrix> 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<int, double> testWithMemoryAllocation(int num_threads)
{
// A function to do some matrix operations with allocating memory
// Create task_arena and task_group
tbb::task_arena arena(num_threads);
tbb::task_group tg;
// Now call it
vector<double> results(numberOfProblems);
const vector<size_t> grainSizes = {1, 10, 100, 1000};
map<int, double> timingResults;
for(size_t grainSize: grainSizes)
{
tbb::tick_count t0 = tbb::tick_count::now();
// Run parallel code (as a task group) inside of task arena
arena.execute([&]{
tg.run_and_wait([&]{
tbb::parallel_for(tbb::blocked_range<size_t>(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[(int)grainSize] = (t1 - t0).seconds();
}
return timingResults;
}
/* ************************************************************************* */
int main(int argc, char* argv[])
{
cout << "numberOfProblems = " << numberOfProblems << endl;
cout << "problemSize = " << problemSize << endl;
const vector<int> numThreads = {1, 4, 8};
Results results;
for(size_t n: numThreads)
{
cout << "With " << n << " threads:" << endl;
results[(int)n].grainSizesWithoutAllocation = testWithoutMemoryAllocation((int)n);
results[(int)n].grainSizesWithAllocation = testWithMemoryAllocation((int)n);
cout << endl;
}
cout << "Summary of results:" << endl;
for(const Results::value_type& threads_result: results)
{
const int threads = threads_result.first;
const ResultWithThreads& result = threads_result.second;
if(threads != 1)
{
for(const ResultWithThreads::value_type& grainsize_time: result.grainSizesWithoutAllocation)
{
const int grainsize = grainsize_time.first;
const double speedup = results[1].grainSizesWithoutAllocation[grainsize] / grainsize_time.second;
cout << threads << " threads, without allocation, grain size = " << grainsize << ", speedup = " << speedup << endl;
}
for(const ResultWithThreads::value_type& grainsize_time: result.grainSizesWithAllocation)
{
const int grainsize = grainsize_time.first;
const double speedup = results[1].grainSizesWithAllocation[grainsize] / grainsize_time.second;
cout << threads << " threads, with allocation, grain size = " << grainsize << ", speedup = " << speedup << endl;
}
}
}
return 0;
}
#else
/* ************************************************************************* */
int main(int argc, char* argv [])
{
cout << "GTSAM is compiled without TBB, please compile with TBB to use this program." << endl;
return 0;
}
#endif