-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtest_halt.cpp
More file actions
110 lines (99 loc) · 3.93 KB
/
test_halt.cpp
File metadata and controls
110 lines (99 loc) · 3.93 KB
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
/*
Copyright 2005-2014 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks. Threading Building Blocks is free software;
you can redistribute it and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation. Threading Building Blocks is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details. You should have received a copy of
the GNU General Public License along with Threading Building Blocks; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software library without
restriction. Specifically, if other files instantiate templates or use macros or inline
functions from this file, or you compile this file and link it with other files to produce
an executable, this file does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however invalidate any other
reasons why the executable file might be covered by the GNU General Public License.
*/
#include "harness_defs.h"
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <utility>
#include "tbb/task.h"
#include "tbb/task_scheduler_init.h"
#include "tbb/tick_count.h"
#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"
#include "tbb/mutex.h"
#include "tbb/spin_mutex.h"
#include "tbb/queuing_mutex.h"
#include "harness.h"
using namespace std;
using namespace tbb;
///////////////////// Parallel methods ////////////////////////
// *** Serial shared by mutexes *** //
int SharedI = 1, SharedN;
template<typename M>
class SharedSerialFibBody: NoAssign {
M &mutex;
public:
SharedSerialFibBody( M &m ) : mutex( m ) {}
//! main loop
void operator()( const blocked_range<int>& /*range*/ ) const {
for(;;) {
typename M::scoped_lock lock( mutex );
if(SharedI >= SharedN) break;
volatile double sum = 7.3;
sum *= 11.17;
++SharedI;
}
}
};
//! Root function
template<class M>
void SharedSerialFib(int n)
{
SharedI = 1;
SharedN = n;
M mutex;
parallel_for( blocked_range<int>(0,4,1), SharedSerialFibBody<M>( mutex ) );
}
/////////////////////////// Main ////////////////////////////////////////////////////
double Tsum = 0; int Tnum = 0;
typedef void (*MeasureFunc)(int);
//! Measure ticks count in loop [2..n]
void Measure(const char *name, MeasureFunc func, int n)
{
tick_count t0;
tick_count::interval_t T;
REMARK("%s",name);
t0 = tick_count::now();
for(int number = 2; number <= n; number++)
func(number);
T = tick_count::now() - t0;
double avg = Tnum? Tsum/Tnum : 1;
if (avg == 0.0) avg = 1;
if(avg * 100 < T.seconds()) {
REPORT("Warning: halting detected (%g sec, av: %g)\n", T.seconds(), avg);
ASSERT(avg * 1000 > T.seconds(), "Too long halting period");
} else {
Tsum += T.seconds(); Tnum++;
}
REMARK("\t- in %f msec\n", T.seconds()*1000);
}
int TestMain () {
MinThread = max(2, MinThread);
int NumbersCount = 100;
short recycle = 100;
do {
for(int threads = MinThread; threads <= MaxThread; threads++) {
task_scheduler_init scheduler_init(threads);
REMARK("Threads number is %d\t", threads);
Measure("Shared serial (wrapper mutex)\t", SharedSerialFib<mutex>, NumbersCount);
//sum = Measure("Shared serial (spin_mutex)", SharedSerialFib<tbb::spin_mutex>, NumbersCount);
//sum = Measure("Shared serial (queuing_mutex)", SharedSerialFib<tbb::queuing_mutex>, NumbersCount);
}
} while(--recycle);
return Harness::Done;
}