forked from RcppCore/RcppParallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_overwrite_node.cpp
More file actions
159 lines (137 loc) · 5.4 KB
/
test_overwrite_node.cpp
File metadata and controls
159 lines (137 loc) · 5.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
/*
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_graph.h"
#include "tbb/task_scheduler_init.h"
#define N 300
#define T 4
#define M 5
template< typename R >
void simple_read_write_tests() {
tbb::flow::graph g;
tbb::flow::overwrite_node<R> n(g);
for ( int t = 0; t < T; ++t ) {
R v0(N+1);
harness_counting_receiver<R> r[M];
ASSERT( n.is_valid() == false, NULL );
ASSERT( n.try_get( v0 ) == false, NULL );
if ( t % 2 ) {
ASSERT( n.try_put( static_cast<R>(N) ), NULL );
ASSERT( n.is_valid() == true, NULL );
ASSERT( n.try_get( v0 ) == true, NULL );
ASSERT( v0 == R(N), NULL );
}
for (int i = 0; i < M; ++i) {
tbb::flow::make_edge( n, r[i] );
}
#if TBB_PREVIEW_FLOW_GRAPH_FEATURES
ASSERT(n.successor_count() == M, NULL);
typename tbb::flow::overwrite_node<R>::successor_vector_type my_succs;
n.copy_successors(my_succs);
ASSERT(my_succs.size() == M, NULL);
ASSERT(n.predecessor_count() == 0, NULL);
#endif
for (int i = 0; i < N; ++i ) {
R v1(static_cast<R>(i));
ASSERT( n.try_put( v1 ), NULL );
ASSERT( n.is_valid() == true, NULL );
for (int j = 0; j < N; ++j ) {
R v2(0);
ASSERT( n.try_get( v2 ), NULL );
ASSERT( v1 == v2, NULL );
}
}
for (int i = 0; i < M; ++i) {
size_t c = r[i].my_count;
ASSERT( int(c) == N+t%2, NULL );
}
for (int i = 0; i < M; ++i) {
tbb::flow::remove_edge( n, r[i] );
}
ASSERT( n.try_put( R(0) ), NULL );
for (int i = 0; i < M; ++i) {
size_t c = r[i].my_count;
ASSERT( int(c) == N+t%2, NULL );
}
n.clear();
ASSERT( n.is_valid() == false, NULL );
ASSERT( n.try_get( v0 ) == false, NULL );
}
}
template< typename R >
class native_body : NoAssign {
tbb::flow::overwrite_node<R> &my_node;
public:
native_body( tbb::flow::overwrite_node<R> &n ) : my_node(n) {}
void operator()( int i ) const {
R v1(static_cast<R>(i));
ASSERT( my_node.try_put( v1 ), NULL );
ASSERT( my_node.is_valid() == true, NULL );
}
};
template< typename R >
void parallel_read_write_tests() {
tbb::flow::graph g;
tbb::flow::overwrite_node<R> n(g);
//Create a vector of identical nodes
std::vector< tbb::flow::overwrite_node<R> > ow_vec(2, n);
for (size_t node_idx=0; node_idx<ow_vec.size(); ++node_idx) {
for ( int t = 0; t < T; ++t ) {
harness_counting_receiver<R> r[M];
for (int i = 0; i < M; ++i) {
tbb::flow::make_edge( ow_vec[node_idx], r[i] );
}
R v0;
ASSERT( ow_vec[node_idx].is_valid() == false, NULL );
ASSERT( ow_vec[node_idx].try_get( v0 ) == false, NULL );
NativeParallelFor( N, native_body<R>( ow_vec[node_idx] ) );
for (int i = 0; i < M; ++i) {
size_t c = r[i].my_count;
ASSERT( int(c) == N, NULL );
}
for (int i = 0; i < M; ++i) {
tbb::flow::remove_edge( ow_vec[node_idx], r[i] );
}
ASSERT( ow_vec[node_idx].try_put( R(0) ), NULL );
for (int i = 0; i < M; ++i) {
size_t c = r[i].my_count;
ASSERT( int(c) == N, NULL );
}
ow_vec[node_idx].clear();
ASSERT( ow_vec[node_idx].is_valid() == false, NULL );
ASSERT( ow_vec[node_idx].try_get( v0 ) == false, NULL );
}
}
}
int TestMain() {
if( MinThread<1 ) {
REPORT("number of threads must be positive\n");
exit(1);
}
simple_read_write_tests<int>();
simple_read_write_tests<float>();
for( int p=MinThread; p<=MaxThread; ++p ) {
tbb::task_scheduler_init init(p);
parallel_read_write_tests<int>();
parallel_read_write_tests<float>();
}
#if TBB_PREVIEW_FLOW_GRAPH_FEATURES
test_extract_on_node<tbb::flow::overwrite_node, int>();
test_extract_on_node<tbb::flow::overwrite_node, float>();
#endif
return Harness::Done;
}