Newer
Older
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
#ifndef THREADPOOLTEST_H_
#define THREADPOOLTEST_H_
#include <cxxtest/TestSuite.h>
#include <MantidKernel/Timer.h>
#include "MantidKernel/MultiThreaded.h"
#include <MantidKernel/ThreadPool.h>
#include "Poco/Thread.h"
#include "Poco/ThreadPool.h"
#include "Poco/Runnable.h"
#include "Poco/RunnableAdapter.h"
#include <iostream>
#include <iomanip>
using Poco::Mutex;
using namespace Mantid::Kernel;
#include <boost/thread.hpp>
size_t waste_time(double seconds)
{
// Waste time, but use up the CPU!
std::size_t num = 0;
Mantid::Kernel::Timer time;
while (time.elapsed_no_reset() < seconds)
{
double x = 1.1;
for (int j=0; j < 100000; j++)
{
x = x * x;
x = x + x;
x = x / 1.1;
}
num += 1;
}
return num;
}
Mutex stdout_mutex;
class HelloRunnable: public Poco::Runnable
{
public:
double m_n;
HelloRunnable(double n) : m_n(n)
{
}
void run()
{
{
Mutex::ScopedLock lock(stdout_mutex);
std::cout << "Starting " << m_n << " secs. " << std::endl;
}
size_t num = waste_time(m_n);
{
Mutex::ScopedLock lock(stdout_mutex);
std::cout << "Done! " << m_n << " secs. Ran " <<( num/m_n) << " times/sec." << std::endl;
}
}
};
class DoNothingRunnable: public Poco::Runnable
{
public:
void run()
{
}
};
class DoNothingBoost
{
public:
void operator()()
{
}
};
class ThreadPoolTest : public CxxTest::TestSuite
{
public:
void setUp()
{
}
void tearDown()
{
}
void test_Constructor()
{
Mantid::Kernel::Timer overall;
for (int i=0; i<1000000; i++)
{
ThreadPool::Instance().test();
}
//std::cout << std::setw(15) << overall.elapsed() << " secs total" << std::endl;
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
}
/** Test that shows that OPENMP does not use a thread pool idea to optimally allocate threads */
void xtestOpenMP()
{
Timer overall;
int num = 16;
PARALLEL_FOR_NO_WSP_CHECK()
for (int i=0; i<num; i++)
{
double delay = i;
PARALLEL_CRITICAL(test1)
std::cout << std::setw(5) << i << ": Thread " << PARALLEL_THREAD_NUMBER << " will delay for " << delay << " seconds." << std::endl;
waste_time(delay);
PARALLEL_CRITICAL(test1)
std::cout << std::setw(5) << i << ": is done." << std::endl;
}
std::cout << overall.elapsed() << " secs total.\n";
}
void xtest_Poco_pool()
{
Poco::ThreadPool myPool(2,32);
Mantid::Kernel::Timer overall;
std::cout << std::endl;
for (int i=32; i>0; i--)
{
HelloRunnable * thisOne = new HelloRunnable(i);
myPool.start(*thisOne);
}
myPool.joinAll();
std::cout << overall.elapsed() << " secs total." << std::endl;
return;
}
void xtest_Poco_single_threads()
{
std::cout << "\n";
Mantid::Kernel::Timer overall;
double time;
size_t num = 10000;
for (size_t i=0; i < num; i++)
{
DoNothingRunnable donothing;
Poco::Thread thread;
thread.start(donothing);
thread.join();
}
time = overall.elapsed();
std::cout << "POCO: " << std::setw(15) << time << " secs total = " << std::setw(15) << (num*1.0/time) << " per second" << std::endl;
for (size_t i=0; i < num; i++)
{
DoNothingRunnable donothing;
donothing.run();
}
time = overall.elapsed();
std::cout << "NO THREADS:" << std::setw(15) << time << " secs total = " << std::setw(15) << (num*1.0/time) << " per second" << std::endl;
}
void xtest_Boost_single_threads()
{
Mantid::Kernel::Timer overall;
double time;
size_t num = 10000;
for (size_t i=0; i < num; i++)
{
DoNothingBoost myDoNothing;
boost::thread workerThread(myDoNothing);
workerThread.join();
}
time = overall.elapsed();
std::cout << "Boost: " <<std::setw(15) << time << " secs total = " << std::setw(15) << (num*1.0/time) << " per second" << std::endl;
}
};
#endif