forked from RcppCore/RcppParallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_eh_flow_graph.cpp
More file actions
2035 lines (1856 loc) · 88.7 KB
/
test_eh_flow_graph.cpp
File metadata and controls
2035 lines (1856 loc) · 88.7 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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"
#if _MSC_VER
#pragma warning (disable: 4503) // Suppress "decorated name length exceeded, name was truncated" warning
#if !TBB_USE_EXCEPTIONS
// Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
#pragma warning (disable: 4530)
#endif
#endif
#if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
// Suppress "unreachable code" warning by VC++ 17.0-18.0 (VS 2012 or newer)
#pragma warning (disable: 4702)
#endif
#include "harness.h"
// global task_scheduler_observer is an imperfect tool to find how many threads are really
// participating. That was the hope, but it counts the entries into the marketplace,
// not the arena.
// #define USE_TASK_SCHEDULER_OBSERVER 1
#if _MSC_VER && defined(__INTEL_COMPILER) && !TBB_USE_DEBUG
#define TBB_RUN_BUFFERING_TEST __INTEL_COMPILER > 1210
#else
#define TBB_RUN_BUFFERING_TEST 1
#endif
#if TBB_USE_EXCEPTIONS
#if USE_TASK_SCHEDULER_OBSERVER
#include "tbb/task_scheduler_observer.h"
#endif
#include "tbb/flow_graph.h"
#include "tbb/task_scheduler_init.h"
#include <iostream>
#include <vector>
#include "harness_assert.h"
#include "harness_checktype.h"
inline intptr_t Existed() { return INT_MAX; } // resolve Existed in harness_eh.h
#include "harness_eh.h"
#include <stdexcept>
#define NUM_ITEMS 15
int g_NumItems;
tbb::atomic<unsigned> nExceptions;
tbb::atomic<intptr_t> g_TGCCancelled;
enum TestNodeTypeEnum { nonThrowing, isThrowing };
static const size_t unlimited_type = 0;
static const size_t serial_type = 1;
static const size_t limited_type = 4;
template<TestNodeTypeEnum T> struct TestNodeTypeName;
template<> struct TestNodeTypeName<nonThrowing> { static const char *name() { return "nonThrowing"; } };
template<> struct TestNodeTypeName<isThrowing> { static const char *name() { return "isThrowing"; } };
template<size_t Conc> struct concurrencyName;
template<> struct concurrencyName<serial_type>{ static const char *name() { return "serial"; } };
template<> struct concurrencyName<unlimited_type>{ static const char *name() { return "unlimited"; } };
template<> struct concurrencyName<limited_type>{ static const char *name() { return "limited"; } };
// Class that provides waiting and throwing behavior. If we are not throwing, do nothing
// If serial, we can't wait for concurrency to peak; we may be the bottleneck and will
// stop further processing. We will execute g_NumThreads + 10 times (the "10" is somewhat
// arbitrary, and just makes sure there are enough items in the graph to keep it flowing),
// If parallel or serial and throwing, use Harness::ConcurrencyTracker to wait.
template<size_t Conc, TestNodeTypeEnum t = nonThrowing>
class WaitThrow;
template<>
class WaitThrow<serial_type,nonThrowing> {
protected:
void WaitAndThrow(int cnt, const char * /*name*/) {
if(cnt > g_NumThreads + 10) {
Harness::ConcurrencyTracker ct;
WaitUntilConcurrencyPeaks();
}
}
};
template<>
class WaitThrow<serial_type,isThrowing> {
protected:
void WaitAndThrow(int cnt, const char * /*name*/) {
if(cnt > g_NumThreads + 10) {
Harness::ConcurrencyTracker ct;
WaitUntilConcurrencyPeaks();
ThrowTestException(1);
}
}
};
// for nodes with limited concurrency, if that concurrency is < g_NumThreads, we need
// to make sure enough other nodes wait for concurrency to peak. If we are attached to
// N successors, for each item we pass to a successor, we will get N executions of the
// "absorbers" (because we broadcast to successors.) for an odd number of threads we
// need (g_NumThreads - limited + 1) / 2 items (that will give us one extra execution
// of an "absorber", but we can't change that without changing the behavior of the node.)
template<>
class WaitThrow<limited_type,nonThrowing> {
protected:
void WaitAndThrow(int cnt, const char * /*name*/) {
if(cnt <= (g_NumThreads - (int)limited_type + 1)/2) {
return;
}
Harness::ConcurrencyTracker ct;
WaitUntilConcurrencyPeaks();
}
};
template<>
class WaitThrow<limited_type,isThrowing> {
protected:
void WaitAndThrow(int cnt, const char * /*name*/) {
Harness::ConcurrencyTracker ct;
if(cnt <= (g_NumThreads - (int)limited_type + 1)/2) {
return;
}
WaitUntilConcurrencyPeaks();
ThrowTestException(1);
}
};
template<>
class WaitThrow<unlimited_type,nonThrowing> {
protected:
void WaitAndThrow(int /*cnt*/, const char * /*name*/) {
Harness::ConcurrencyTracker ct;
WaitUntilConcurrencyPeaks();
}
};
template<>
class WaitThrow<unlimited_type,isThrowing> {
protected:
void WaitAndThrow(int /*cnt*/, const char * /*name*/) {
Harness::ConcurrencyTracker ct;
WaitUntilConcurrencyPeaks();
ThrowTestException(1);
}
};
void
ResetGlobals(bool throwException = true, bool flog = false) {
nExceptions = 0;
g_TGCCancelled = 0;
ResetEhGlobals(throwException, flog);
}
// -------source_node body ------------------
template <class OutputType, TestNodeTypeEnum TType>
class test_source_body : WaitThrow<serial_type, TType> {
using WaitThrow<serial_type, TType>::WaitAndThrow;
tbb::atomic<int> *my_current_val;
int my_mult;
public:
test_source_body(tbb::atomic<int> &my_cnt, int multiplier = 1) : my_current_val(&my_cnt), my_mult(multiplier) { }
bool operator()(OutputType & out) {
UPDATE_COUNTS();
out = OutputType(my_mult * ++(*my_current_val));
if(*my_current_val > g_NumItems) {
*my_current_val = g_NumItems;
return false;
}
WaitAndThrow((int)out,"test_source_body");
return true;
}
int count_value() { return (int)*my_current_val; }
};
template <TestNodeTypeEnum TType>
class test_source_body<tbb::flow::continue_msg, TType> : WaitThrow<serial_type, TType> {
using WaitThrow<serial_type, TType>::WaitAndThrow;
tbb::atomic<int> *my_current_val;
public:
test_source_body(tbb::atomic<int> &my_cnt) : my_current_val(&my_cnt) { }
bool operator()(tbb::flow::continue_msg & out) {
UPDATE_COUNTS();
int outint = ++(*my_current_val);
out = tbb::flow::continue_msg();
if(*my_current_val > g_NumItems) {
*my_current_val = g_NumItems;
return false;
}
WaitAndThrow(outint,"test_source_body");
return true;
}
int count_value() { return (int)*my_current_val; }
};
// -------{function/continue}_node body ------------------
template<class InputType, class OutputType, TestNodeTypeEnum T, size_t Conc>
class absorber_body : WaitThrow<Conc,T> {
using WaitThrow<Conc,T>::WaitAndThrow;
tbb::atomic<int> *my_count;
public:
absorber_body(tbb::atomic<int> &my_cnt) : my_count(&my_cnt) { }
OutputType operator()(const InputType &/*p_in*/) {
UPDATE_COUNTS();
int out = ++(*my_count);
WaitAndThrow(out,"absorber_body");
return OutputType();
}
int count_value() { return *my_count; }
};
// -------multifunction_node body ------------------
// helper classes
template<int N,class PortsType>
struct IssueOutput {
typedef typename tbb::flow::tuple_element<N-1,PortsType>::type::output_type my_type;
static void issue_tuple_element( PortsType &my_ports) {
ASSERT(tbb::flow::get<N-1>(my_ports).try_put(my_type()), "Error putting to successor");
IssueOutput<N-1,PortsType>::issue_tuple_element(my_ports);
}
};
template<class PortsType>
struct IssueOutput<1,PortsType> {
typedef typename tbb::flow::tuple_element<0,PortsType>::type::output_type my_type;
static void issue_tuple_element( PortsType &my_ports) {
ASSERT(tbb::flow::get<0>(my_ports).try_put(my_type()), "Error putting to successor");
}
};
template<class InputType, class OutputTupleType, TestNodeTypeEnum T, size_t Conc>
class multifunction_node_body : WaitThrow<Conc,T> {
using WaitThrow<Conc,T>::WaitAndThrow;
static const int N = tbb::flow::tuple_size<OutputTupleType>::value;
typedef typename tbb::flow::multifunction_node<InputType,OutputTupleType> NodeType;
typedef typename NodeType::output_ports_type PortsType;
tbb::atomic<int> *my_count;
public:
multifunction_node_body(tbb::atomic<int> &my_cnt) : my_count(&my_cnt) { }
void operator()(const InputType& /*in*/, PortsType &my_ports) {
UPDATE_COUNTS();
int out = ++(*my_count);
WaitAndThrow(out,"multifunction_node_body");
// issue an item to each output port.
IssueOutput<N,PortsType>::issue_tuple_element(my_ports);
}
int count_value() { return *my_count; }
};
// --------- body to sort items in sequencer_node
template<class BufferItemType>
struct sequencer_body {
size_t operator()(const BufferItemType &s) {
return size_t(s) - 1;
}
};
// --------- body to compare the "priorities" of objects for priority_queue_node five priority levels 0-4.
template<class T>
struct myLess {
bool operator()(const T &t1, const T &t2) {
return (int(t1) % 5) < (int(t2) % 5);
}
};
// --------- type for < comparison in priority_queue_node.
template<class ItemType>
struct less_body : public std::binary_function<ItemType,ItemType,bool> {
bool operator()(const ItemType &lhs, const ItemType &rhs) {
return ((int)lhs % 3) < ((int)rhs % 3);
}
};
// --------- tag methods for tag_matching join_node
template<typename TT>
class tag_func {
TT my_mult;
public:
tag_func(TT multiplier) : my_mult(multiplier) { }
void operator=( const tag_func& other){my_mult = other.my_mult;}
// operator() will return [0 .. Count)
tbb::flow::tag_value operator()( TT v) {
tbb::flow::tag_value t = tbb::flow::tag_value(v / my_mult);
return t;
}
};
// --------- Source body for split_node test.
template <class OutputTuple, TestNodeTypeEnum TType>
class tuple_test_source_body : WaitThrow<serial_type, TType> {
typedef typename tbb::flow::tuple_element<0,OutputTuple>::type ItemType0;
typedef typename tbb::flow::tuple_element<1,OutputTuple>::type ItemType1;
using WaitThrow<serial_type, TType>::WaitAndThrow;
tbb::atomic<int> *my_current_val;
public:
tuple_test_source_body(tbb::atomic<int> &my_cnt) : my_current_val(&my_cnt) { }
bool operator()(OutputTuple & out) {
UPDATE_COUNTS();
int ival = ++(*my_current_val);
out = OutputTuple(ItemType0(ival),ItemType1(ival));
if(*my_current_val > g_NumItems) {
*my_current_val = g_NumItems; // jam the final value; we assert on it later.
return false;
}
WaitAndThrow(ival,"tuple_test_source_body");
return true;
}
int count_value() { return (int)*my_current_val; }
};
// ------- end of node bodies
// source_node is only-serial. source_node can throw, or the function_node can throw.
// graph being tested is
//
// source_node+---+parallel function_node
//
// After each run the graph is reset(), to test the reset functionality.
//
template<class ItemType, TestNodeTypeEnum srcThrowType, TestNodeTypeEnum absorbThrowType>
void run_one_source_node_test(bool throwException, bool flog) {
typedef test_source_body<ItemType,srcThrowType> src_body_type;
typedef absorber_body<ItemType, tbb::flow::continue_msg, absorbThrowType, unlimited_type> parallel_absorb_body_type;
tbb::atomic<int> source_body_count;
tbb::atomic<int> absorber_body_count;
source_body_count = 0;
absorber_body_count = 0;
tbb::flow::graph g;
g_Master = Harness::CurrentTid();
#if USE_TASK_SCHEDULER_OBSERVER
eh_test_observer o;
o.observe(true);
#endif
tbb::flow::source_node<ItemType> sn(g, src_body_type(source_body_count),/*is_active*/false);
parallel_absorb_body_type ab2(absorber_body_count);
tbb::flow::function_node<ItemType> parallel_fn(g,tbb::flow::unlimited,ab2);
make_edge(sn, parallel_fn);
for(int runcnt = 0; runcnt < 2; ++runcnt) {
ResetGlobals(throwException,flog);
if(throwException) {
TRY();
sn.activate();
g.wait_for_all();
CATCH_AND_ASSERT();
}
else {
TRY();
sn.activate();
g.wait_for_all();
CATCH_AND_FAIL();
}
bool okayNoExceptionsCaught = (g_ExceptionInMaster && !g_MasterExecutedThrow) || (!g_ExceptionInMaster && !g_NonMasterExecutedThrow) || !throwException;
int src_cnt = tbb::flow::copy_body<src_body_type>(sn).count_value();
int sink_cnt = tbb::flow::copy_body<parallel_absorb_body_type>(parallel_fn).count_value();
if(throwException) {
ASSERT(g.exception_thrown() || okayNoExceptionsCaught, "Exception flag in flow::graph not set");
ASSERT(g.is_cancelled() || okayNoExceptionsCaught, "canceled flag not set");
ASSERT(src_cnt <= g_NumItems, "Too many source_node items emitted");
ASSERT(sink_cnt <= src_cnt, "Too many source_node items received");
}
else {
ASSERT(!g.exception_thrown(), "Exception flag in flow::graph set but no throw occurred");
ASSERT(!g.is_cancelled(), "canceled flag set but no throw occurred");
ASSERT(src_cnt == g_NumItems, "Incorrect # source_node items emitted");
ASSERT(sink_cnt == src_cnt, "Incorrect # source_node items received");
}
g.reset(); // resets the body of the source_node and the absorb_nodes.
source_body_count = 0;
absorber_body_count = 0;
ASSERT(!g.exception_thrown(), "Reset didn't clear exception_thrown()");
ASSERT(!g.is_cancelled(), "Reset didn't clear is_cancelled()");
src_cnt = tbb::flow::copy_body<src_body_type>(sn).count_value();
sink_cnt = tbb::flow::copy_body<parallel_absorb_body_type>(parallel_fn).count_value();
ASSERT(src_cnt == 0, "source_node count not reset");
ASSERT(sink_cnt == 0, "sink_node count not reset");
}
#if USE_TASK_SCHEDULER_OBSERVER
o.observe(false);
#endif
} // run_one_source_node_test
template<class ItemType, TestNodeTypeEnum srcThrowType, TestNodeTypeEnum absorbThrowType>
void run_source_node_test() {
run_one_source_node_test<ItemType,srcThrowType,absorbThrowType>(false,false);
run_one_source_node_test<ItemType,srcThrowType,absorbThrowType>(true,false);
run_one_source_node_test<ItemType,srcThrowType,absorbThrowType>(true,true);
} // run_source_node_test
void test_source_node() {
REMARK("Testing source_node\n");
check_type<int>::check_type_counter = 0;
g_Wakeup_Msg = "source_node(1): Missed wakeup or machine is overloaded?";
run_source_node_test<check_type<int>, isThrowing, nonThrowing>();
ASSERT(!check_type<int>::check_type_counter, "Some items leaked in test");
g_Wakeup_Msg = "source_node(2): Missed wakeup or machine is overloaded?";
run_source_node_test<int, isThrowing, nonThrowing>();
g_Wakeup_Msg = "source_node(3): Missed wakeup or machine is overloaded?";
run_source_node_test<int, nonThrowing, isThrowing>();
g_Wakeup_Msg = "source_node(4): Missed wakeup or machine is overloaded?";
run_source_node_test<int, isThrowing, isThrowing>();
g_Wakeup_Msg = "source_node(5): Missed wakeup or machine is overloaded?";
run_source_node_test<check_type<int>, isThrowing, isThrowing>();
g_Wakeup_Msg = g_Orig_Wakeup_Msg;
ASSERT(!check_type<int>::check_type_counter, "Some items leaked in test");
}
// -------- utilities & types to test function_node and multifunction_node.
// need to tell the template which node type I am using so it attaches successors correctly.
enum NodeFetchType { func_node_type, multifunc_node_type };
template<class NodeType, class ItemType, int indx, NodeFetchType NFT>
struct AttachPoint;
template<class NodeType, class ItemType, int indx>
struct AttachPoint<NodeType,ItemType,indx,multifunc_node_type> {
static tbb::flow::sender<ItemType> &GetSender(NodeType &n) {
return tbb::flow::output_port<indx>(n);
}
};
template<class NodeType, class ItemType, int indx>
struct AttachPoint<NodeType,ItemType,indx,func_node_type> {
static tbb::flow::sender<ItemType> &GetSender(NodeType &n) {
return n;
}
};
// common template for running function_node, multifunction_node. continue_node
// has different firing requirements, so it needs a different graph topology.
template<
class SourceNodeType,
class SourceNodeBodyType0,
class SourceNodeBodyType1,
NodeFetchType NFT,
class TestNodeType,
class TestNodeBodyType,
class TypeToSink0, // what kind of item are we sending to sink0
class TypeToSink1, // what kind of item are we sending to sink1
class SinkNodeType0, // will be same for function;
class SinkNodeType1, // may differ for multifunction_node
class SinkNodeBodyType0,
class SinkNodeBodyType1,
size_t Conc
>
void
run_one_functype_node_test(bool throwException, bool flog, const char * /*name*/) {
char mymsg[132];
char *saved_msg = const_cast<char *>(g_Wakeup_Msg);
tbb::flow::graph g;
tbb::atomic<int> source0_count;
tbb::atomic<int> source1_count;
tbb::atomic<int> sink0_count;
tbb::atomic<int> sink1_count;
tbb::atomic<int> test_count;
source0_count = source1_count = sink0_count = sink1_count = test_count = 0;
#if USE_TASK_SCHEDULER_OBSERVER
eh_test_observer o;
o.observe(true);
#endif
g_Master = Harness::CurrentTid();
SourceNodeType source0(g, SourceNodeBodyType0(source0_count),/*is_active*/false);
SourceNodeType source1(g, SourceNodeBodyType1(source1_count),/*is_active*/false);
TestNodeType node_to_test(g, Conc, TestNodeBodyType(test_count));
SinkNodeType0 sink0(g,tbb::flow::unlimited,SinkNodeBodyType0(sink0_count));
SinkNodeType1 sink1(g,tbb::flow::unlimited,SinkNodeBodyType1(sink1_count));
make_edge(source0, node_to_test);
make_edge(source1, node_to_test);
make_edge(AttachPoint<TestNodeType, TypeToSink0, 0, NFT>::GetSender(node_to_test), sink0);
make_edge(AttachPoint<TestNodeType, TypeToSink1, 1, NFT>::GetSender(node_to_test), sink1);
for(int iter = 0; iter < 2; ++iter) { // run, reset, run again
sprintf(mymsg, "%s iter=%d, threads=%d, throw=%s, flog=%s", saved_msg, iter, g_NumThreads,
throwException?"T":"F", flog?"T":"F");
g_Wakeup_Msg = mymsg;
ResetGlobals(throwException,flog);
if(throwException) {
TRY();
source0.activate();
source1.activate();
g.wait_for_all();
CATCH_AND_ASSERT();
}
else {
TRY();
source0.activate();
source1.activate();
g.wait_for_all();
CATCH_AND_FAIL();
}
bool okayNoExceptionsCaught = (g_ExceptionInMaster && !g_MasterExecutedThrow) || (!g_ExceptionInMaster && !g_NonMasterExecutedThrow) || !throwException;
int sb0_cnt = tbb::flow::copy_body<SourceNodeBodyType0>(source0).count_value();
int sb1_cnt = tbb::flow::copy_body<SourceNodeBodyType1>(source1).count_value();
int t_cnt = tbb::flow::copy_body<TestNodeBodyType>(node_to_test).count_value();
int nb0_cnt = tbb::flow::copy_body<SinkNodeBodyType0>(sink0).count_value();
int nb1_cnt = tbb::flow::copy_body<SinkNodeBodyType1>(sink1).count_value();
if(throwException) {
ASSERT(g.exception_thrown() || okayNoExceptionsCaught, "Exception not caught by graph");
ASSERT(g.is_cancelled() || okayNoExceptionsCaught, "Cancellation not signalled in graph");
ASSERT(sb0_cnt + sb1_cnt <= 2*g_NumItems, "Too many items sent by sources");
ASSERT(sb0_cnt + sb1_cnt >= t_cnt, "Too many items received by test node");
ASSERT(nb0_cnt + nb1_cnt <= t_cnt*2, "Too many items received by sink nodes");
}
else {
ASSERT(!g.exception_thrown(), "Exception flag in flow::graph set but no throw occurred");
ASSERT(!g.is_cancelled(), "canceled flag set but no throw occurred");
ASSERT(sb0_cnt + sb1_cnt == 2*g_NumItems, "Missing invocations of source_nodes");
ASSERT(t_cnt == 2*g_NumItems, "Not all items reached test node");
ASSERT(nb0_cnt == 2*g_NumItems && nb1_cnt == 2*g_NumItems, "Missing items in absorbers");
}
g.reset(); // resets the body of the source_nodes, test_node and the absorb_nodes.
source0_count = source1_count = sink0_count = sink1_count = test_count = 0;
ASSERT(0 == tbb::flow::copy_body<SourceNodeBodyType0>(source0).count_value(),"Reset source 0 failed");
ASSERT(0 == tbb::flow::copy_body<SourceNodeBodyType1>(source1).count_value(),"Reset source 1 failed");
ASSERT(0 == tbb::flow::copy_body<TestNodeBodyType>(node_to_test).count_value(),"Reset test_node failed");
ASSERT(0 == tbb::flow::copy_body<SinkNodeBodyType0>(sink0).count_value(),"Reset sink 0 failed");
ASSERT(0 == tbb::flow::copy_body<SinkNodeBodyType1>(sink1).count_value(),"Reset sink 1 failed");
g_Wakeup_Msg = saved_msg;
}
#if USE_TASK_SCHEDULER_OBSERVER
o.observe(false);
#endif
}
// Test function_node
//
// graph being tested is
//
// source_node -\ /- parallel function_node
// \ /
// +function_node+
// / \ x
// source_node -/ \- parallel function_node
//
// After each run the graph is reset(), to test the reset functionality.
//
template<
TestNodeTypeEnum SType1, // does source node 1 throw?
TestNodeTypeEnum SType2, // does source node 2 throw?
class Item12, // type of item passed between sources and test node
TestNodeTypeEnum FType, // does function node throw?
class Item23, // type passed from function_node to sink nodes
TestNodeTypeEnum NType1, // does sink node 1 throw?
TestNodeTypeEnum NType2, // does sink node 1 throw?
tbb::flow::graph_buffer_policy NodePolicy, // rejecting,queueing
size_t Conc // is node concurrent? {serial | limited | unlimited}
>
void run_function_node_test() {
typedef test_source_body<Item12,SType1> SBodyType1;
typedef test_source_body<Item12,SType2> SBodyType2;
typedef absorber_body<Item12, Item23, FType, Conc> TestBodyType;
typedef absorber_body<Item23,tbb::flow::continue_msg, NType1, unlimited_type> SinkBodyType1;
typedef absorber_body<Item23,tbb::flow::continue_msg, NType2, unlimited_type> SinkBodyType2;
typedef tbb::flow::source_node<Item12> SrcType;
typedef tbb::flow::function_node<Item12, Item23, NodePolicy> TestType;
typedef tbb::flow::function_node<Item23,tbb::flow::continue_msg> SnkType;
for(int i = 0; i < 4; ++i ) {
if(i != 2) { // doesn't make sense to flog a non-throwing test
bool doThrow = (i & 0x1) != 0;
bool doFlog = (i & 0x2) != 0;
run_one_functype_node_test<
/*SourceNodeType*/ SrcType,
/*SourceNodeBodyType0*/ SBodyType1,
/*SourceNodeBodyType1*/ SBodyType2,
/* NFT */ func_node_type,
/*TestNodeType*/ TestType,
/*TestNodeBodyType*/ TestBodyType,
/*TypeToSink0 */ Item23,
/*TypeToSink1 */ Item23,
/*SinkNodeType0*/ SnkType,
/*SinkNodeType1*/ SnkType,
/*SinkNodeBodyType1*/ SinkBodyType1,
/*SinkNodeBodyType2*/ SinkBodyType2,
/*Conc*/ Conc>
(doThrow,doFlog,"function_node");
}
}
} // run_function_node_test
void test_function_node() {
REMARK("Testing function_node\n");
// serial rejecting
g_Wakeup_Msg = "function_node(1a): Missed wakeup or machine is overloaded?";
run_function_node_test<isThrowing, nonThrowing, int, nonThrowing, int, nonThrowing, nonThrowing, tbb::flow::rejecting, serial_type>();
g_Wakeup_Msg = "function_node(1b): Missed wakeup or machine is overloaded?";
run_function_node_test<nonThrowing, nonThrowing, int, isThrowing, int, nonThrowing, nonThrowing, tbb::flow::rejecting, serial_type>();
g_Wakeup_Msg = "function_node(1c): Missed wakeup or machine is overloaded?";
run_function_node_test<nonThrowing, nonThrowing, int, nonThrowing, int, isThrowing, nonThrowing, tbb::flow::rejecting, serial_type>();
// serial queueing
g_Wakeup_Msg = "function_node(2): Missed wakeup or machine is overloaded?";
run_function_node_test<isThrowing, nonThrowing, int, nonThrowing, int, nonThrowing, nonThrowing, tbb::flow::queueing, serial_type>();
run_function_node_test<nonThrowing, nonThrowing, int, isThrowing, int, nonThrowing, nonThrowing, tbb::flow::queueing, serial_type>();
run_function_node_test<nonThrowing, nonThrowing, int, nonThrowing, int, isThrowing, nonThrowing, tbb::flow::queueing, serial_type>();
check_type<int>::check_type_counter = 0;
run_function_node_test<nonThrowing, nonThrowing, check_type<int>, nonThrowing, check_type<int>, isThrowing, nonThrowing, tbb::flow::queueing, serial_type>();
ASSERT(!check_type<int>::check_type_counter, "Some items leaked in test");
// unlimited parallel rejecting
g_Wakeup_Msg = "function_node(3): Missed wakeup or machine is overloaded?";
run_function_node_test<isThrowing, nonThrowing, int, nonThrowing, int, nonThrowing, nonThrowing, tbb::flow::rejecting, unlimited_type>();
run_function_node_test<nonThrowing, nonThrowing, int, isThrowing, int, nonThrowing, nonThrowing, tbb::flow::rejecting, unlimited_type>();
run_function_node_test<nonThrowing, nonThrowing, int, nonThrowing, int, nonThrowing, isThrowing, tbb::flow::rejecting, unlimited_type>();
// limited parallel rejecting
g_Wakeup_Msg = "function_node(4): Missed wakeup or machine is overloaded?";
run_function_node_test<isThrowing, nonThrowing, int, nonThrowing, int, nonThrowing, nonThrowing, tbb::flow::rejecting, limited_type>();
run_function_node_test<nonThrowing, nonThrowing, int, isThrowing, int, nonThrowing, nonThrowing, tbb::flow::rejecting, (size_t)limited_type>();
run_function_node_test<nonThrowing, nonThrowing, int, nonThrowing, int, nonThrowing, isThrowing, tbb::flow::rejecting, (size_t)limited_type>();
// limited parallel queueing
g_Wakeup_Msg = "function_node(5): Missed wakeup or machine is overloaded?";
run_function_node_test<isThrowing, nonThrowing, int, nonThrowing, int, nonThrowing, nonThrowing, tbb::flow::queueing, (size_t)limited_type>();
run_function_node_test<nonThrowing, nonThrowing, int, isThrowing, int, nonThrowing, nonThrowing, tbb::flow::queueing, (size_t)limited_type>();
run_function_node_test<nonThrowing, nonThrowing, int, nonThrowing, int, nonThrowing, isThrowing, tbb::flow::queueing, (size_t)limited_type>();
// everyone throwing
g_Wakeup_Msg = "function_node(6): Missed wakeup or machine is overloaded?";
run_function_node_test<isThrowing, isThrowing, int, isThrowing, int, isThrowing, isThrowing, tbb::flow::rejecting, unlimited_type>();
g_Wakeup_Msg = g_Orig_Wakeup_Msg;
}
// ----------------------------------- multifunction_node ----------------------------------
// Test multifunction_node.
//
// graph being tested is
//
// source_node -\ /- parallel function_node
// \ /
// +multifunction_node+
// / \ x
// source_node -/ \- parallel function_node
//
// After each run the graph is reset(), to test the reset functionality. The
// multifunction_node will put an item to each successor for every item
// received.
//
template<
TestNodeTypeEnum SType0, // does source node 1 throw?
TestNodeTypeEnum SType1, // does source node 2 thorw?
class Item12, // type of item passed between sources and test node
TestNodeTypeEnum FType, // does multifunction node throw?
class ItemTuple, // tuple of types passed from multifunction_node to sink nodes
TestNodeTypeEnum NType1, // does sink node 1 throw?
TestNodeTypeEnum NType2, // does sink node 2 throw?
tbb::flow::graph_buffer_policy NodePolicy, // rejecting,queueing
size_t Conc // is node concurrent? {serial | limited | unlimited}
>
void run_multifunction_node_test() {
typedef typename tbb::flow::tuple_element<0,ItemTuple>::type Item23Type0;
typedef typename tbb::flow::tuple_element<1,ItemTuple>::type Item23Type1;
typedef test_source_body<Item12,SType0> SBodyType1;
typedef test_source_body<Item12,SType1> SBodyType2;
typedef multifunction_node_body<Item12, ItemTuple, FType, Conc> TestBodyType;
typedef absorber_body<Item23Type0,tbb::flow::continue_msg, NType1, unlimited_type> SinkBodyType1;
typedef absorber_body<Item23Type1,tbb::flow::continue_msg, NType2, unlimited_type> SinkBodyType2;
typedef tbb::flow::source_node<Item12> SrcType;
typedef tbb::flow::multifunction_node<Item12, ItemTuple, NodePolicy> TestType;
typedef tbb::flow::function_node<Item23Type0,tbb::flow::continue_msg> SnkType0;
typedef tbb::flow::function_node<Item23Type1,tbb::flow::continue_msg> SnkType1;
for(int i = 0; i < 4; ++i ) {
if(i != 2) { // doesn't make sense to flog a non-throwing test
bool doThrow = (i & 0x1) != 0;
bool doFlog = (i & 0x2) != 0;
run_one_functype_node_test<
/*SourceNodeType*/ SrcType,
/*SourceNodeBodyType0*/ SBodyType1,
/*SourceNodeBodyType1*/ SBodyType2,
/*NFT*/ multifunc_node_type,
/*TestNodeType*/ TestType,
/*TestNodeBodyType*/ TestBodyType,
/*TypeToSink0*/ Item23Type0,
/*TypeToSink1*/ Item23Type1,
/*SinkNodeType0*/ SnkType0,
/*SinkNodeType1*/ SnkType1,
/*SinkNodeBodyType0*/ SinkBodyType1,
/*SinkNodeBodyType1*/ SinkBodyType2,
/*Conc*/ Conc>
(doThrow,doFlog,"multifunction_node");
}
}
} // run_multifunction_node_test
void test_multifunction_node() {
REMARK("Testing multifunction_node\n");
g_Wakeup_Msg = "multifunction_node(source throws,rejecting,serial): Missed wakeup or machine is overloaded?";
// serial rejecting
run_multifunction_node_test<isThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,float>, nonThrowing, nonThrowing, tbb::flow::rejecting, serial_type>();
g_Wakeup_Msg = "multifunction_node(test throws,rejecting,serial): Missed wakeup or machine is overloaded?";
run_multifunction_node_test<nonThrowing, nonThrowing, int, isThrowing, tbb::flow::tuple<int,int>, nonThrowing, nonThrowing, tbb::flow::rejecting, serial_type>();
g_Wakeup_Msg = "multifunction_node(sink throws,rejecting,serial): Missed wakeup or machine is overloaded?";
run_multifunction_node_test<nonThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,int>, isThrowing, nonThrowing, tbb::flow::rejecting, serial_type>();
g_Wakeup_Msg = "multifunction_node(2): Missed wakeup or machine is overloaded?";
// serial queueing
run_multifunction_node_test<isThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,int>, nonThrowing, nonThrowing, tbb::flow::queueing, serial_type>();
run_multifunction_node_test<nonThrowing, nonThrowing, int, isThrowing, tbb::flow::tuple<int,int>, nonThrowing, nonThrowing, tbb::flow::queueing, serial_type>();
run_multifunction_node_test<nonThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,int>, isThrowing, nonThrowing, tbb::flow::queueing, serial_type>();
check_type<int>::check_type_counter = 0;
run_multifunction_node_test<nonThrowing, nonThrowing, check_type<int>, nonThrowing, tbb::flow::tuple<check_type<int>, check_type<int> >, isThrowing, nonThrowing, tbb::flow::queueing, serial_type>();
ASSERT(!check_type<int>::check_type_counter, "Some items leaked in test");
g_Wakeup_Msg = "multifunction_node(3): Missed wakeup or machine is overloaded?";
// unlimited parallel rejecting
run_multifunction_node_test<isThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,int>, nonThrowing, nonThrowing, tbb::flow::rejecting, unlimited_type>();
run_multifunction_node_test<nonThrowing, nonThrowing, int, isThrowing, tbb::flow::tuple<int,int>, nonThrowing, nonThrowing, tbb::flow::rejecting, unlimited_type>();
run_multifunction_node_test<nonThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,int>, nonThrowing, isThrowing, tbb::flow::rejecting, unlimited_type>();
g_Wakeup_Msg = "multifunction_node(4): Missed wakeup or machine is overloaded?";
// limited parallel rejecting
run_multifunction_node_test<isThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,int>, nonThrowing, nonThrowing, tbb::flow::rejecting, limited_type>();
run_multifunction_node_test<nonThrowing, nonThrowing, int, isThrowing, tbb::flow::tuple<int,int>, nonThrowing, nonThrowing, tbb::flow::rejecting, (size_t)limited_type>();
run_multifunction_node_test<nonThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,int>, nonThrowing, isThrowing, tbb::flow::rejecting, (size_t)limited_type>();
g_Wakeup_Msg = "multifunction_node(5): Missed wakeup or machine is overloaded?";
// limited parallel queueing
run_multifunction_node_test<isThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,int>, nonThrowing, nonThrowing, tbb::flow::queueing, (size_t)limited_type>();
run_multifunction_node_test<nonThrowing, nonThrowing, int, isThrowing, tbb::flow::tuple<int,int>, nonThrowing, nonThrowing, tbb::flow::queueing, (size_t)limited_type>();
run_multifunction_node_test<nonThrowing, nonThrowing, int, nonThrowing, tbb::flow::tuple<int,int>, nonThrowing, isThrowing, tbb::flow::queueing, (size_t)limited_type>();
g_Wakeup_Msg = "multifunction_node(6): Missed wakeup or machine is overloaded?";
// everyone throwing
run_multifunction_node_test<isThrowing, isThrowing, int, isThrowing, tbb::flow::tuple<int,int>, isThrowing, isThrowing, tbb::flow::rejecting, unlimited_type>();
g_Wakeup_Msg = g_Orig_Wakeup_Msg;
}
//
// Continue node has T predecessors. when it receives messages (continue_msg) on T predecessors
// it executes the body of the node, and forwards a continue_msg to its successors.
// However many predecessors the continue_node has, that's how many continue_msgs it receives
// on input before forwarding a message.
//
// The graph will look like
//
// +broadcast_node+
// / \ ___
// source_node+------>+broadcast_node+ +continue_node+--->+absorber
// \ /
// +broadcast_node+
//
// The continue_node has unlimited parallelism, no input buffering, and broadcasts to successors.
// The absorber is parallel, so each item emitted by the source will result in one thread
// spinning. So for N threads we pass N-1 continue_messages, then spin wait and then throw if
// we are allowed to.
template < class SourceNodeType, class SourceNodeBodyType, class TTestNodeType, class TestNodeBodyType,
class SinkNodeType, class SinkNodeBodyType>
void run_one_continue_node_test (bool throwException, bool flog) {
tbb::flow::graph g;
tbb::atomic<int> source_count;
tbb::atomic<int> test_count;
tbb::atomic<int> sink_count;
source_count = test_count = sink_count = 0;
#if USE_TASK_SCHEDULER_OBSERVER
eh_test_observer o;
o.observe(true);
#endif
g_Master = Harness::CurrentTid();
SourceNodeType source(g, SourceNodeBodyType(source_count),/*is_active*/false);
TTestNodeType node_to_test(g, TestNodeBodyType(test_count));
SinkNodeType sink(g,tbb::flow::unlimited,SinkNodeBodyType(sink_count));
tbb::flow::broadcast_node<tbb::flow::continue_msg> b1(g), b2(g), b3(g);
make_edge(source, b1);
make_edge(b1,b2);
make_edge(b1,b3);
make_edge(b2,node_to_test);
make_edge(b3,node_to_test);
make_edge(node_to_test, sink);
for(int iter = 0; iter < 2; ++iter) {
ResetGlobals(throwException,flog);
if(throwException) {
TRY();
source.activate();
g.wait_for_all();
CATCH_AND_ASSERT();
}
else {
TRY();
source.activate();
g.wait_for_all();
CATCH_AND_FAIL();
}
bool okayNoExceptionsCaught = (g_ExceptionInMaster && !g_MasterExecutedThrow) || (!g_ExceptionInMaster && !g_NonMasterExecutedThrow) || !throwException;
int sb_cnt = tbb::flow::copy_body<SourceNodeBodyType>(source).count_value();
int t_cnt = tbb::flow::copy_body<TestNodeBodyType>(node_to_test).count_value();
int nb_cnt = tbb::flow::copy_body<SinkNodeBodyType>(sink).count_value();
if(throwException) {
ASSERT(g.exception_thrown() || okayNoExceptionsCaught, "Exception not caught by graph");
ASSERT(g.is_cancelled() || okayNoExceptionsCaught, "Cancellation not signalled in graph");
ASSERT(sb_cnt <= g_NumItems, "Too many items sent by sources");
ASSERT(sb_cnt >= t_cnt, "Too many items received by test node");
ASSERT(nb_cnt <= t_cnt, "Too many items received by sink nodes");
}
else {
ASSERT(!g.exception_thrown(), "Exception flag in flow::graph set but no throw occurred");
ASSERT(!g.is_cancelled(), "canceled flag set but no throw occurred");
ASSERT(sb_cnt == g_NumItems, "Missing invocations of source_node");
ASSERT(t_cnt == g_NumItems, "Not all items reached test node");
ASSERT(nb_cnt == g_NumItems, "Missing items in absorbers");
}
g.reset(); // resets the body of the source_nodes, test_node and the absorb_nodes.
source_count = test_count = sink_count = 0;
ASSERT(0 == (int)test_count, "Atomic wasn't reset properly");
ASSERT(0 == tbb::flow::copy_body<SourceNodeBodyType>(source).count_value(),"Reset source failed");
ASSERT(0 == tbb::flow::copy_body<TestNodeBodyType>(node_to_test).count_value(),"Reset test_node failed");
ASSERT(0 == tbb::flow::copy_body<SinkNodeBodyType>(sink).count_value(),"Reset sink failed");
}
#if USE_TASK_SCHEDULER_OBSERVER
o.observe(false);
#endif
}
template<
class ItemType,
TestNodeTypeEnum SType, // does source node throw?
TestNodeTypeEnum CType, // does continue_node throw?
TestNodeTypeEnum AType> // does absorber throw
void run_continue_node_test() {
typedef test_source_body<tbb::flow::continue_msg,SType> SBodyType;
typedef absorber_body<tbb::flow::continue_msg,ItemType,CType,unlimited_type> ContBodyType;
typedef absorber_body<ItemType,tbb::flow::continue_msg, AType, unlimited_type> SinkBodyType;
typedef tbb::flow::source_node<tbb::flow::continue_msg> SrcType;
typedef tbb::flow::continue_node<ItemType> TestType;
typedef tbb::flow::function_node<ItemType,tbb::flow::continue_msg> SnkType;
for(int i = 0; i < 4; ++i ) {
if(i == 2) continue; // don't run (false,true); it doesn't make sense.
bool doThrow = (i & 0x1) != 0;
bool doFlog = (i & 0x2) != 0;
run_one_continue_node_test<
/*SourceNodeType*/ SrcType,
/*SourceNodeBodyType*/ SBodyType,
/*TestNodeType*/ TestType,
/*TestNodeBodyType*/ ContBodyType,
/*SinkNodeType*/ SnkType,
/*SinkNodeBodyType*/ SinkBodyType>
(doThrow,doFlog);
}
}
//
void test_continue_node() {
REMARK("Testing continue_node\n");
g_Wakeup_Msg = "buffer_node(non,is,non): Missed wakeup or machine is overloaded?";
run_continue_node_test<int,nonThrowing,isThrowing,nonThrowing>();
g_Wakeup_Msg = "buffer_node(non,non,is): Missed wakeup or machine is overloaded?";
run_continue_node_test<int,nonThrowing,nonThrowing,isThrowing>();
g_Wakeup_Msg = "buffer_node(is,non,non): Missed wakeup or machine is overloaded?";
run_continue_node_test<int,isThrowing,nonThrowing,nonThrowing>();
g_Wakeup_Msg = "buffer_node(is,is,is): Missed wakeup or machine is overloaded?";
run_continue_node_test<int,isThrowing,isThrowing,isThrowing>();
check_type<double>::check_type_counter = 0;
run_continue_node_test<check_type<double>,isThrowing,isThrowing,isThrowing>();
ASSERT(!check_type<double>::check_type_counter, "Dropped objects in continue_node test");
g_Wakeup_Msg = g_Orig_Wakeup_Msg;
}
// ---------- buffer_node queue_node overwrite_node --------------
template<
class BufferItemType, //
class SourceNodeType,
class SourceNodeBodyType,
class TestNodeType,
class SinkNodeType,
class SinkNodeBodyType >
void run_one_buffer_node_test(bool throwException,bool flog) {
tbb::flow::graph g;
tbb::atomic<int> source_count;
tbb::atomic<int> sink_count;
source_count = sink_count = 0;
#if USE_TASK_SCHEDULER_OBSERVER
eh_test_observer o;
o.observe(true);
#endif
g_Master = Harness::CurrentTid();
SourceNodeType source(g, SourceNodeBodyType(source_count),/*is_active*/false);
TestNodeType node_to_test(g);
SinkNodeType sink(g,tbb::flow::unlimited,SinkNodeBodyType(sink_count));
make_edge(source,node_to_test);
make_edge(node_to_test, sink);
for(int iter = 0; iter < 2; ++iter) {
ResetGlobals(throwException,flog);
if(throwException) {
TRY();
source.activate();
g.wait_for_all();
CATCH_AND_ASSERT();
}
else {
TRY();
source.activate();
g.wait_for_all();
CATCH_AND_FAIL();
}
bool okayNoExceptionsCaught = (g_ExceptionInMaster && !g_MasterExecutedThrow) || (!g_ExceptionInMaster && !g_NonMasterExecutedThrow) || !throwException;
int sb_cnt = tbb::flow::copy_body<SourceNodeBodyType>(source).count_value();
int nb_cnt = tbb::flow::copy_body<SinkNodeBodyType>(sink).count_value();
if(throwException) {
ASSERT(g.exception_thrown() || okayNoExceptionsCaught, "Exception not caught by graph");
ASSERT(g.is_cancelled() || okayNoExceptionsCaught, "Cancellation not signalled in graph");
ASSERT(sb_cnt <= g_NumItems, "Too many items sent by sources");
ASSERT(nb_cnt <= sb_cnt, "Too many items received by sink nodes");
}
else {
ASSERT(!g.exception_thrown(), "Exception flag in flow::graph set but no throw occurred");
ASSERT(!g.is_cancelled(), "canceled flag set but no throw occurred");
ASSERT(sb_cnt == g_NumItems, "Missing invocations of source_node");
ASSERT(nb_cnt == g_NumItems, "Missing items in absorbers");
}
if(iter == 0) {
remove_edge(node_to_test, sink);
node_to_test.try_put(BufferItemType());
g.wait_for_all();
g.reset();
source_count = sink_count = 0;
BufferItemType tmp;
ASSERT(!node_to_test.try_get(tmp), "node not empty");
make_edge(node_to_test, sink);
g.wait_for_all();
}
else {
g.reset();
source_count = sink_count = 0;
}
ASSERT(0 == tbb::flow::copy_body<SourceNodeBodyType>(source).count_value(),"Reset source failed");
ASSERT(0 == tbb::flow::copy_body<SinkNodeBodyType>(sink).count_value(),"Reset sink failed");
}
#if USE_TASK_SCHEDULER_OBSERVER
o.observe(false);
#endif
}
template<class BufferItemType,
TestNodeTypeEnum SourceThrowType,
TestNodeTypeEnum SinkThrowType>
void run_buffer_queue_and_overwrite_node_test() {
typedef test_source_body<BufferItemType,SourceThrowType> SourceBodyType;
typedef absorber_body<BufferItemType,tbb::flow::continue_msg,SinkThrowType,unlimited_type> SinkBodyType;
typedef tbb::flow::source_node<BufferItemType> SrcType;
typedef tbb::flow::buffer_node<BufferItemType> BufType;
typedef tbb::flow::queue_node<BufferItemType> QueType;
typedef tbb::flow::overwrite_node<BufferItemType> OvrType;
typedef tbb::flow::function_node<BufferItemType,tbb::flow::continue_msg> SnkType;
for(int i = 0; i < 4; ++i) {