-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtest_halt.cpp
More file actions
109 lines (96 loc) · 3.25 KB
/
test_halt.cpp
File metadata and controls
109 lines (96 loc) · 3.25 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
/*
Copyright (c) 2005-2019 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define HARNESS_DEFAULT_MIN_THREADS 4
#define HARNESS_DEFAULT_MAX_THREADS 8
#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;
}