forked from CaptGreg/SenecaOOP345-attic
/
threading101i.cpp
219 lines (179 loc) · 6.49 KB
/
threading101i.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
// 1. how to thread a for loop
// 2. how do you start up a thread with a class member function?
// 3. sometimes threading makes things worse
// threading includes
#include <thread>
#include <mutex>
// application includes
#include <vector>
#include <chrono>
#include <iostream>
#include <exception>
// using namespace std;
class Timer {
std::chrono::time_point<std::chrono::high_resolution_clock> start;
std::chrono::time_point<std::chrono::high_resolution_clock> stop;
public:
Timer() {}
void Start() { start = std::chrono::high_resolution_clock::now(); }
void Stop() { stop = std::chrono::high_resolution_clock::now(); }
uint64_t usecs() {
typedef std::chrono::duration<int,std::micro> microsecs_t ;
microsecs_t duration_get( std::chrono::duration_cast<microsecs_t>(stop-start) ) ;
uint64_t tWait = duration_get.count();
return tWait;
}
uint64_t nsecs() {
typedef std::chrono::duration<int,std::nano> nanosecs_t ;
nanosecs_t duration_get( std::chrono::duration_cast<nanosecs_t>(stop-start) ) ;
uint64_t tWait = duration_get.count();
return tWait;
}
};
class Application {
std::vector<uint64_t> v;
std::mutex vLock;
public:
void Reset() { v.clear(); }
uint64_t Size() { return v.size(); }
uint64_t Capacity() { return v.capacity(); }
void calcItem(size_t item, bool needLock)
{
uint64_t itemValue;
itemValue = 123; // ... (a lengthy calculation) // 123 to make it compile
if(needLock)
{ // acquire smartLock
std::unique_lock<std::mutex> smartLock (vLock);
v.push_back(itemValue);
} // release smartLock
else
{
v.push_back(itemValue);
}
}
void doWork(size_t start, size_t end, bool needLock)
{
for(size_t item = start; item < end; item++)
calcItem(item,needLock);
}
void runProcess() // executes on one thread
{
size_t start = 0;
size_t end = 50LL * 1000LL * 1000LL;
bool needLock = false;
v.reserve(end);
doWork(start, end, needLock);
}
void runProcessWithThreads()
{
const int NUM_THREADS = std::thread::hardware_concurrency();
std::cout << "running with " << NUM_THREADS << " threads\n";
std::vector<std::thread> t(NUM_THREADS);
size_t start = 0;
size_t end = 50LL * 1000LL * 1000LL;
v.reserve(end);
size_t chunk = (end - start + (NUM_THREADS-1)) / NUM_THREADS;
bool needLock = true;
for(int i = 0; i < NUM_THREADS; i++) {
size_t s = start + i * chunk;
size_t e = s + chunk;
if(i == NUM_THREADS - 1) e = end;
#if 1 // call thread directly
// Note how to start a thread with a class member function.
// if any doWork parameters are call-by-ref, say parm 'foo', then need 'ref(foo)'
t[i] = std::thread (&Application::doWork, this, s, e, needLock);
#else // call thread with a function pointer
// if any doWork parameters are call-by-ref, say parm 'foo', then need 'ref(foo)'
std::function<void(void)> b = std::bind( &Application::doWork, this, s, e, needLock);
// if any doWork parameters are call-by-ref, say parm 'foo', then need 'ref(foo)'
// auto b = std::bind( &Application::doWork, this, s, e, needLock); // also works...
t[i] = std::thread ( b );
#endif
}
for( auto& e : t ) e.join();
}
};
int main()
{
#ifdef __GNUC__
#ifdef __clang__
std::cout << "clang++ compiler version " << __VERSION__ << "\n";
#else
std::cout << "g++ compiler version " << __VERSION__ << "\n";
#endif
#endif
try {
Application app;
Timer t;
t.Start();
app.runProcess();
t.Stop();
uint64_t tNoThreads = t.usecs();
std::cout << "no threading " << tNoThreads << " usecs"
<< ", " << tNoThreads * 1e-6 << " secs\n";
std::cout << "vector v size/capacity=" << app.Size() << "/" << app.Capacity() << "\n";
app.Reset(); // run adds things to vector v with push_back, reset vector v for the threaded run
t.Start();
app.runProcessWithThreads();
t.Stop();
uint64_t tThreads = t.usecs();
std::cout << "threading " << tThreads << " usecs"
<< ", " << tThreads * 1e-6 << " secs\n";
std::cout << "vector v size/capacity=" << app.Size() << "/" << app.Capacity() << "\n";
double speedup = double(tNoThreads) / double(tThreads);
if(speedup >= 1.)
std::cout << "Threading speed up = " << speedup << " times\n";
else
std::cout << "Threading slow down = " << 1./speedup << " times\n";
} catch (std::exception& e) {
std::cerr << e.what() << "\n";
}
return 0;
}
// timings on 2011 vintage laptop with a 4 core Intel i5-460M mobile CPU, 4GB DDRIII mem
// uname -a
// Linux msi 3.13.0-57-generic #95-Ubuntu SMP Fri Jun 19 09:28:15 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
// lsb_release -a
// No LSB modules are available.
// Distributor ID: Ubuntu
// Description: Ubuntu 14.04.2 LTS
// Release: 14.04
// Codename: trusty
// clang++ --version
// Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)
// Target: x86_64-pc-linux-gnu
// Thread model: posix
// clang++ -std=c++11 threading101i.cpp -o threading101i -pthread && ./threading101i
// no threading 1821157 usecs
// running with 4 threads
// threading 26723729 usecs
// Threading slow down = 14.674 times
// clang++ -std=c++11 -Ofast threading101i.cpp -o threading101i -pthread && ./threading101i
// no threading 640690 usecs
// running with 4 threads
// threading 13486095 usecs
// Threading slow down = 21.0493 times
// g++ --version
// g++ (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
// g++ -std=c++11 threading101i.cpp -o threading101i -pthread && ./threading101i
// no threading 3393726 usecs
// running with 4 threads
// threading 26701773 usecs
// Threading slow down = 7.86798 times
// g++ -std=c++11 -Ofast threading101i.cpp -o threading101i -pthread && ./threading101i
// no threading 463719 usecs
// running with 4 threads
// threading 11403303 usecs
// Threading slow down = 24.591 times
// g++-5 --version
// g++-5 (Ubuntu 5.1.0-0ubuntu11~14.04.1) 5.1.0
// g++-5 -std=c++11 threading101i.cpp -o threading101i -pthread && ./threading101i
// no threading 1814138 usecs
// running with 4 threads
// threading 25927255 usecs
// Threading slow down = 14.2918 times
// g++-5 -std=c++11 -Ofast threading101i.cpp -o threading101i -pthread && ./threading101i
// no threading 517545 usecs
// running with 4 threads
// threading 14106871 usecs
// Threading slow down = 27.2573 times