-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtime_hash_map.cpp
More file actions
261 lines (224 loc) · 8.19 KB
/
time_hash_map.cpp
File metadata and controls
261 lines (224 loc) · 8.19 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Copyright (c) 2005-2017 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.
*/
// configuration:
#define TBB_USE_THREADING_TOOLS 0
//! enable/disable std::map tests
#define STDTABLE 0
//! enable/disable old implementation tests (correct include file also)
#define OLDTABLE 0
#define OLDTABLEHEADER "tbb/concurrent_hash_map-5468.h"//-4329
//! enable/disable experimental implementation tests (correct include file also)
#define TESTTABLE 0
#define TESTTABLEHEADER "tbb/concurrent_unordered_map.h"
//! avoid erase()
#define TEST_ERASE 1
//////////////////////////////////////////////////////////////////////////////////
#include <cstdlib>
#include <math.h>
#include "tbb/tbb_stddef.h"
#include <vector>
#include <map>
// needed by hash_maps
#include <stdexcept>
#include <iterator>
#include <algorithm> // Need std::swap
#include <utility> // Need std::pair
#include "tbb/cache_aligned_allocator.h"
#include "tbb/tbb_allocator.h"
#include "tbb/spin_rw_mutex.h"
#include "tbb/aligned_space.h"
#include "tbb/atomic.h"
#define __TBB_concurrent_unordered_set_H
#include "tbb/internal/_concurrent_unordered_impl.h"
#undef __TBB_concurrent_unordered_set_H
// for test
#include "tbb/spin_mutex.h"
#include "time_framework.h"
using namespace tbb;
using namespace tbb::internal;
struct IntHashCompare {
size_t operator() ( int x ) const { return x; }
bool operator() ( int x, int y ) const { return x==y; }
static long hash( int x ) { return x; }
bool equal( int x, int y ) const { return x==y; }
};
namespace version_current {
namespace tbb { using namespace ::tbb; namespace internal { using namespace ::tbb::internal; } }
namespace tbb { namespace interface5 { using namespace ::tbb::interface5; namespace internal { using namespace ::tbb::interface5::internal; } } }
#include "tbb/concurrent_hash_map.h"
}
typedef version_current::tbb::concurrent_hash_map<int,int> IntTable;
#if OLDTABLE
#undef __TBB_concurrent_hash_map_H
namespace version_base {
namespace tbb { using namespace ::tbb; namespace internal { using namespace ::tbb::internal; } }
namespace tbb { namespace interface5 { using namespace ::tbb::interface5; namespace internal { using namespace ::tbb::interface5::internal; } } }
#include OLDTABLEHEADER
}
typedef version_base::tbb::concurrent_hash_map<int,int> OldTable;
#endif
#if TESTTABLE
#undef __TBB_concurrent_hash_map_H
namespace version_new {
namespace tbb { using namespace ::tbb; namespace internal { using namespace ::tbb::internal; } }
namespace tbb { namespace interface5 { using namespace ::tbb::interface5; namespace internal { using namespace ::tbb::interface5::internal; } } }
#include TESTTABLEHEADER
}
typedef version_new::tbb::concurrent_unordered_map<int,int> TestTable;
#define TESTTABLE 1
#endif
///////////////////////////////////////
static const char *map_testnames[] = {
"1.insert", "2.count1st", "3.count2nd", "4.insert-exists", "5.erase "
};
template<typename TableType>
struct TestTBBMap : TesterBase {
TableType Table;
int n_items;
TestTBBMap() : TesterBase(4+TEST_ERASE), Table(MaxThread*4) {}
void init() { n_items = value/threads_count; }
std::string get_name(int testn) {
return std::string(map_testnames[testn]);
}
double test(int test, int t)
{
switch(test) {
case 0: // fill
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
Table.insert( std::make_pair(i,i) );
}
break;
case 1: // work1
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
size_t c = Table.count( i );
ASSERT( c == 1, NULL);
}
break;
case 2: // work2
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
Table.count( i );
}
break;
case 3: // work3
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
Table.insert( std::make_pair(i,i) );
}
break;
#if TEST_ERASE
case 4: // clean
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
ASSERT( Table.erase( i ), NULL);
}
#endif
}
return 0;
}
};
template<typename M>
struct TestSTLMap : TesterBase {
std::map<int, int> Table;
M mutex;
int n_items;
TestSTLMap() : TesterBase(4+TEST_ERASE) {}
void init() { n_items = value/threads_count; }
std::string get_name(int testn) {
return std::string(map_testnames[testn]);
}
double test(int test, int t)
{
switch(test) {
case 0: // fill
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
typename M::scoped_lock with(mutex);
Table[i] = 0;
}
break;
case 1: // work1
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
typename M::scoped_lock with(mutex);
size_t c = Table.count(i);
ASSERT( c == 1, NULL);
}
break;
case 2: // work2
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
typename M::scoped_lock with(mutex);
Table.count(i);
}
break;
case 3: // work3
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
typename M::scoped_lock with(mutex);
Table.insert(std::make_pair(i,i));
}
break;
case 4: // clean
for(int i = t*n_items, e = (t+1)*n_items; i < e; i++) {
typename M::scoped_lock with(mutex);
Table.erase(i);
}
}
return 0;
}
};
class fake_mutex {
public:
class scoped_lock {
fake_mutex *p;
public:
scoped_lock() {}
scoped_lock( fake_mutex &m ) { p = &m; }
~scoped_lock() { }
void acquire( fake_mutex &m ) { p = &m; }
void release() { }
};
};
class test_hash_map : public TestProcessor {
public:
test_hash_map() : TestProcessor("time_hash_map") {}
void factory(int value, int threads) {
if(Verbose) printf("Processing with %d threads: %d...\n", threads, value);
process( value, threads,
#if STDTABLE
run("std::map ", new NanosecPerValue<TestSTLMap<spin_mutex> >() ),
#endif
#if OLDTABLE
run("old::hmap", new NanosecPerValue<TestTBBMap<OldTable> >() ),
#endif
run("tbb::hmap", new NanosecPerValue<TestTBBMap<IntTable> >() ),
#if TESTTABLE
run("new::hmap", new NanosecPerValue<TestTBBMap<TestTable> >() ),
#endif
end );
//stat->Print(StatisticsCollector::Stdout);
//if(value >= 2097152) stat->Print(StatisticsCollector::HTMLFile);
}
};
/////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]) {
if(argc>1) Verbose = true;
//if(argc>2) ExtraVerbose = true;
MinThread = 1; MaxThread = task_scheduler_init::default_num_threads();
ParseCommandLine( argc, argv );
ASSERT(tbb_allocator<int>::allocator_type() == tbb_allocator<int>::scalable, "expecting scalable allocator library to be loaded. Please build it by:\n\t\tmake tbbmalloc");
{
test_hash_map the_test;
for( int t=MinThread; t <= MaxThread; t++)
for( int o=/*2048*/(1<<8)*8; o<2200000; o*=2 )
the_test.factory(o, t);
the_test.report.SetTitle("Nanoseconds per operation of (Mode) for N items in container (Name)");
the_test.report.SetStatisticFormula("1AVG per size", "=AVERAGE(ROUNDS)");
the_test.report.Print(StatisticsCollector::HTMLFile|StatisticsCollector::ExcelXML);
}
return 0;
}