forked from RcppCore/RcppParallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_buffer_node.cpp
More file actions
444 lines (379 loc) · 12.4 KB
/
test_buffer_node.cpp
File metadata and controls
444 lines (379 loc) · 12.4 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
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.h"
#include "tbb/flow_graph.h"
#include "tbb/task_scheduler_init.h"
#include "tbb/tick_count.h"
#if TBB_PREVIEW_FLOW_GRAPH_FEATURES
#include "harness_graph.h"
#include <vector>
#include <algorithm>
#endif
#define N 1000
#define C 10
template< typename T >
void spin_try_get( tbb::flow::buffer_node<T> &b, T &value ) {
while ( b.try_get(value) != true ) {}
}
template< typename T >
void check_item( T* count_value, T &value ) {
count_value[value / N] += value % N;
}
template< typename T >
struct parallel_puts : NoAssign {
tbb::flow::buffer_node<T> &my_b;
parallel_puts( tbb::flow::buffer_node<T> &b ) : my_b(b) {}
void operator()(int i) const {
for (int j = 0; j < N; ++j) {
bool msg = my_b.try_put( T(N*i + j) );
ASSERT( msg == true, NULL );
}
}
};
template< typename T >
struct touches {
bool **my_touches;
int my_num_threads;
touches( int num_threads ) : my_num_threads(num_threads) {
my_touches = new bool* [my_num_threads];
for ( int p = 0; p < my_num_threads; ++p) {
my_touches[p] = new bool[N];
for ( int n = 0; n < N; ++n)
my_touches[p][n] = false;
}
}
~touches() {
for ( int p = 0; p < my_num_threads; ++p) {
delete [] my_touches[p];
}
delete [] my_touches;
}
bool check( T v ) {
ASSERT ( my_touches[v/N][v%N] == false, NULL);
my_touches[v/N][v%N] = true;
return true;
}
bool validate_touches() {
for ( int p = 0; p < my_num_threads; ++p) {
for ( int n = 0; n < N; ++n) {
ASSERT ( my_touches[p][n] == true, NULL);
}
}
return true;
}
};
template< typename T >
struct parallel_gets : NoAssign {
tbb::flow::buffer_node<T> &my_b;
touches<T> &my_touches;
parallel_gets( tbb::flow::buffer_node<T> &b, touches<T> &t) : my_b(b), my_touches(t) {}
void operator()(int) const {
for (int j = 0; j < N; ++j) {
T v;
spin_try_get( my_b, v );
my_touches.check( v );
}
}
};
template< typename T >
struct parallel_put_get : NoAssign {
tbb::flow::buffer_node<T> &my_b;
touches<T> &my_touches;
parallel_put_get( tbb::flow::buffer_node<T> &b, touches<T> &t ) : my_b(b), my_touches(t) {}
void operator()(int tid) const {
for ( int i = 0; i < N; i+=C ) {
int j_end = ( N < i + C ) ? N : i + C;
// dump about C values into the buffer
for ( int j = i; j < j_end; ++j ) {
ASSERT( my_b.try_put( T (N*tid + j ) ) == true, NULL );
}
// receiver about C values from the buffer
for ( int j = i; j < j_end; ++j ) {
T v;
spin_try_get( my_b, v );
my_touches.check( v );
}
}
}
};
//
// Tests
//
// Item can be reserved, released, consumed ( single serial receiver )
//
template< typename T >
int test_reservation() {
tbb::flow::graph g;
T bogus_value(-1);
// Simple tests
tbb::flow::buffer_node<T> b(g);
b.try_put(T(1));
b.try_put(T(2));
b.try_put(T(3));
T v, vsum;
ASSERT( b.try_reserve(v) == true, NULL );
ASSERT( b.try_release() == true, NULL );
v = bogus_value;
g.wait_for_all();
ASSERT( b.try_reserve(v) == true, NULL );
ASSERT( b.try_consume() == true, NULL );
vsum += v;
v = bogus_value;
g.wait_for_all();
ASSERT( b.try_get(v) == true, NULL );
vsum += v;
v = bogus_value;
g.wait_for_all();
ASSERT( b.try_reserve(v) == true, NULL );
ASSERT( b.try_release() == true, NULL );
v = bogus_value;
g.wait_for_all();
ASSERT( b.try_reserve(v) == true, NULL );
ASSERT( b.try_consume() == true, NULL );
vsum += v;
ASSERT( vsum == T(6), NULL);
v = bogus_value;
g.wait_for_all();
return 0;
}
//
// Tests
//
// multilpe parallel senders, items in arbitrary order
// multilpe parallel senders, multiple parallel receivers, items in arbitrary order and all items received
// * overlapped puts / gets
// * all puts finished before any getS
//
template< typename T >
int test_parallel(int num_threads) {
tbb::flow::graph g;
tbb::flow::buffer_node<T> b(g);
tbb::flow::buffer_node<T> b2(g);
tbb::flow::buffer_node<T> b3(g);
T bogus_value(-1);
T j = bogus_value;
NativeParallelFor( num_threads, parallel_puts<T>(b) );
T *next_value = new T[num_threads];
for (int tid = 0; tid < num_threads; ++tid) next_value[tid] = T(0);
for (int i = 0; i < num_threads * N; ++i ) {
spin_try_get( b, j );
check_item( next_value, j );
j = bogus_value;
}
for (int tid = 0; tid < num_threads; ++tid) {
ASSERT( next_value[tid] == T((N*(N-1))/2), NULL );
}
j = bogus_value;
g.wait_for_all();
ASSERT( b.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
NativeParallelFor( num_threads, parallel_puts<T>(b) );
{
touches< T > t( num_threads );
NativeParallelFor( num_threads, parallel_gets<T>(b, t) );
g.wait_for_all();
ASSERT( t.validate_touches(), NULL );
}
j = bogus_value;
ASSERT( b.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
g.wait_for_all();
{
touches< T > t( num_threads );
NativeParallelFor( num_threads, parallel_put_get<T>(b, t) );
g.wait_for_all();
ASSERT( t.validate_touches(), NULL );
}
j = bogus_value;
ASSERT( b.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
tbb::flow::make_edge( b, b2 );
tbb::flow::make_edge( b2, b3 );
NativeParallelFor( num_threads, parallel_puts<T>(b) );
{
touches< T > t( num_threads );
NativeParallelFor( num_threads, parallel_gets<T>(b3, t) );
g.wait_for_all();
ASSERT( t.validate_touches(), NULL );
}
j = bogus_value;
g.wait_for_all();
ASSERT( b.try_get( j ) == false, NULL );
g.wait_for_all();
ASSERT( b2.try_get( j ) == false, NULL );
g.wait_for_all();
ASSERT( b3.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
// test copy constructor
ASSERT( b.remove_successor( b2 ), NULL );
// fill up b:
NativeParallelFor( num_threads, parallel_puts<T>(b) );
// copy b:
tbb::flow::buffer_node<T> b_copy(b);
// b_copy should be empty
j = bogus_value;
g.wait_for_all();
ASSERT( b_copy.try_get( j ) == false, NULL );
// hook them together:
ASSERT( b.register_successor(b_copy) == true, NULL );
// try to get content from b_copy
{
touches< T > t( num_threads );
NativeParallelFor( num_threads, parallel_gets<T>(b_copy, t) );
g.wait_for_all();
ASSERT( t.validate_touches(), NULL );
}
// now both should be empty
j = bogus_value;
g.wait_for_all();
ASSERT( b.try_get( j ) == false, NULL );
g.wait_for_all();
ASSERT( b_copy.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
return 0;
}
//
// Tests
//
// Predecessors cannot be registered
// Empty buffer rejects item requests
// Single serial sender, items in arbitrary order
// Chained buffers ( 2 & 3 ), single sender, items at last buffer in arbitrary order
//
template< typename T >
int test_serial() {
tbb::flow::graph g;
T bogus_value(-1);
tbb::flow::buffer_node<T> b(g);
tbb::flow::buffer_node<T> b2(g);
T j = bogus_value;
//
// Rejects attempts to add / remove predecessor
// Rejects request from empty buffer
//
ASSERT( b.register_predecessor( b2 ) == false, NULL );
ASSERT( b.remove_predecessor( b2 ) == false, NULL );
ASSERT( b.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
//
// Simple puts and gets
//
for (int i = 0; i < N; ++i) {
bool msg = b.try_put( T(i) );
ASSERT( msg == true, NULL );
}
T vsum = T(0);
for (int i = 0; i < N; ++i) {
j = bogus_value;
spin_try_get( b, j );
vsum += j;
}
ASSERT( vsum == (N*(N-1))/2, NULL);
j = bogus_value;
g.wait_for_all();
ASSERT( b.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
tbb::flow::make_edge(b, b2);
#if TBB_PREVIEW_FLOW_GRAPH_FEATURES
ASSERT( b.successor_count() == 1, NULL);
ASSERT( b.predecessor_count() == 0, NULL);
ASSERT( b2.successor_count() == 0, NULL);
ASSERT( b2.predecessor_count() == 1, NULL);
typename tbb::flow::buffer_node<T>::successor_vector_type my_succs;
b.copy_successors(my_succs);
ASSERT(my_succs.size() == 1, NULL);
typename tbb::flow::buffer_node<T>::predecessor_vector_type my_preds;
b.copy_predecessors(my_preds);
ASSERT(my_preds.size() == 0, NULL);
#endif
vsum = T(0);
for (int i = 0; i < N; ++i) {
bool msg = b.try_put( T(i) );
ASSERT( msg == true, NULL );
}
for (int i = 0; i < N; ++i) {
j = bogus_value;
spin_try_get( b2, j );
vsum += j;
}
ASSERT( vsum == (N*(N-1))/2, NULL);
j = bogus_value;
g.wait_for_all();
ASSERT( b.try_get( j ) == false, NULL );
g.wait_for_all();
ASSERT( b2.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
tbb::flow::remove_edge(b, b2);
ASSERT( b.try_put( 1 ) == true, NULL );
g.wait_for_all();
ASSERT( b2.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
g.wait_for_all();
ASSERT( b.try_get( j ) == true, NULL );
ASSERT( j == 1, NULL );
tbb::flow::buffer_node<T> b3(g);
tbb::flow::make_edge( b, b2 );
tbb::flow::make_edge( b2, b3 );
vsum = T(0);
for (int i = 0; i < N; ++i) {
bool msg = b.try_put( T(i) );
ASSERT( msg == true, NULL );
}
for (int i = 0; i < N; ++i) {
j = bogus_value;
spin_try_get( b3, j );
vsum += j;
}
ASSERT( vsum == (N*(N-1))/2, NULL);
j = bogus_value;
g.wait_for_all();
ASSERT( b.try_get( j ) == false, NULL );
g.wait_for_all();
ASSERT( b2.try_get( j ) == false, NULL );
g.wait_for_all();
ASSERT( b3.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
tbb::flow::remove_edge(b, b2);
ASSERT( b.try_put( 1 ) == true, NULL );
g.wait_for_all();
ASSERT( b2.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
g.wait_for_all();
ASSERT( b3.try_get( j ) == false, NULL );
ASSERT( j == bogus_value, NULL );
g.wait_for_all();
ASSERT( b.try_get( j ) == true, NULL );
ASSERT( j == 1, NULL );
return 0;
}
int TestMain() {
tbb::tick_count start = tbb::tick_count::now(), stop;
for (int p = 2; p <= 4; ++p) {
tbb::task_scheduler_init init(p);
test_serial<int>();
test_parallel<int>(p);
}
stop = tbb::tick_count::now();
REMARK("Buffer_Node Time=%6.6f\n", (stop-start).seconds());
#if TBB_PREVIEW_FLOW_GRAPH_FEATURES
test_resets<int,tbb::flow::buffer_node<int> >();
test_resets<float,tbb::flow::buffer_node<float> >();
test_buffer_extract<tbb::flow::buffer_node<int> >().run_tests();
#endif
return Harness::Done;
}