forked from RcppCore/RcppParallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_eh_algorithms.cpp
More file actions
1580 lines (1415 loc) · 65.8 KB
/
test_eh_algorithms.cpp
File metadata and controls
1580 lines (1415 loc) · 65.8 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.h"
#if __TBB_TASK_GROUP_CONTEXT
#include <limits.h> // for INT_MAX
#include "tbb/task_scheduler_init.h"
#include "tbb/tbb_exception.h"
#include "tbb/task.h"
#include "tbb/atomic.h"
#include "tbb/parallel_for.h"
#include "tbb/parallel_reduce.h"
#include "tbb/parallel_do.h"
#include "tbb/pipeline.h"
#include "tbb/parallel_scan.h"
#include "tbb/blocked_range.h"
#include "harness_assert.h"
#define FLAT_RANGE 100000
#define FLAT_GRAIN 100
#define OUTER_RANGE 100
#define OUTER_GRAIN 10
#define INNER_RANGE (FLAT_RANGE / OUTER_RANGE)
#define INNER_GRAIN (FLAT_GRAIN / OUTER_GRAIN)
tbb::atomic<intptr_t> g_FedTasksCount; // number of tasks added by parallel_do feeder
tbb::atomic<intptr_t> g_OuterParCalls; // number of actual invocations of the outer construct executed.
tbb::atomic<intptr_t> g_TGCCancelled; // Number of times a task sees its group cancelled at start
inline intptr_t Existed () { return INT_MAX; }
#include "harness_eh.h"
/********************************
Variables in test
__ Test control variables
g_ExceptionInMaster -- only the master thread is allowed to throw. If false, the master cannot throw
g_SolitaryException -- only one throw may be executed.
-- controls for ThrowTestException for pipeline tests
g_NestedPipelines -- are inner pipelines being run?
g_PipelinesStarted -- how many pipelines have run their first filter at least once.
-- Information variables
g_Master -- Thread ID of the "master" thread
In pipelines sometimes the master thread does not participate, so the tests have to be resilient to this.
-- Measurement variables
g_OuterParCalls -- how many outer parallel ranges or filters started
g_TGCCancelled -- how many inner parallel ranges or filters saw task::self().is_cancelled()
g_ExceptionsThrown -- number of throws executed (counted in ThrowTestException)
g_MasterExecutedThrow -- number of times master thread actually executed a throw
g_NonMasterExecutedThrow -- number of times non-master thread actually executed a throw
g_ExceptionCaught -- one of PropagatedException or unknown exception was caught. (Other exceptions cause assertions.)
-- Tallies for the task bodies which have executed (counted in each inner body, sampled in ThrowTestException)
g_CurExecuted -- total number of inner ranges or filters which executed
g_ExecutedAtLastCatch -- value of g_CurExecuted when last catch was made, 0 if none.
g_ExecutedAtFirstCatch -- value of g_CurExecuted when first catch is made, 0 if none.
*********************************/
inline void ResetGlobals ( bool throwException = true, bool flog = false ) {
ResetEhGlobals( throwException, flog );
g_FedTasksCount = 0;
g_OuterParCalls = 0;
g_NestedPipelines = false;
g_TGCCancelled = 0;
}
////////////////////////////////////////////////////////////////////////////////
// Tests for tbb::parallel_for and tbb::parallel_reduce
typedef size_t count_type;
typedef tbb::blocked_range<count_type> range_type;
inline intptr_t CountSubranges(range_type r) {
if(!r.is_divisible()) return intptr_t(1);
range_type r2(r,tbb::split());
return CountSubranges(r) + CountSubranges(r2);
}
inline intptr_t NumSubranges ( intptr_t length, intptr_t grain ) {
return CountSubranges(range_type(0,length,grain));
}
template<class Body>
intptr_t TestNumSubrangesCalculation ( intptr_t length, intptr_t grain, intptr_t inner_length, intptr_t inner_grain ) {
ResetGlobals();
g_ThrowException = false;
intptr_t outerCalls = NumSubranges(length, grain),
innerCalls = NumSubranges(inner_length, inner_grain),
maxExecuted = outerCalls * (innerCalls + 1);
tbb::parallel_for( range_type(0, length, grain), Body() );
ASSERT (g_CurExecuted == maxExecuted, "Wrong estimation of bodies invocation count");
return maxExecuted;
}
class NoThrowParForBody {
public:
void operator()( const range_type& r ) const {
volatile count_type x = 0;
if(g_Master == Harness::CurrentTid()) g_MasterExecuted = true;
else g_NonMasterExecuted = true;
if( tbb::task::self().is_cancelled() ) ++g_TGCCancelled;
count_type end = r.end();
for( count_type i=r.begin(); i<end; ++i )
x += i;
}
};
#if TBB_USE_EXCEPTIONS
void Test0 () {
ResetGlobals();
tbb::simple_partitioner p;
for( size_t i=0; i<10; ++i ) {
tbb::parallel_for( range_type(0, 0, 1), NoThrowParForBody() );
tbb::parallel_for( range_type(0, 0, 1), NoThrowParForBody(), p );
tbb::parallel_for( range_type(0, 128, 8), NoThrowParForBody() );
tbb::parallel_for( range_type(0, 128, 8), NoThrowParForBody(), p );
}
} // void Test0 ()
//! Template that creates a functor suitable for parallel_reduce from a functor for parallel_for.
template<typename ParForBody>
class SimpleParReduceBody: NoAssign {
ParForBody m_Body;
public:
void operator()( const range_type& r ) const { m_Body(r); }
SimpleParReduceBody() {}
SimpleParReduceBody( SimpleParReduceBody& left, tbb::split ) : m_Body(left.m_Body) {}
void join( SimpleParReduceBody& /*right*/ ) {}
}; // SimpleParReduceBody
//! Test parallel_for and parallel_reduce for a given partitioner.
/** The Body need only be suitable for a parallel_for. */
template<typename ParForBody, typename Partitioner>
void TestParallelLoopAux() {
Partitioner partitioner;
for( int i=0; i<2; ++i ) {
ResetGlobals();
TRY();
if( i==0 )
tbb::parallel_for( range_type(0, FLAT_RANGE, FLAT_GRAIN), ParForBody(), partitioner );
else {
SimpleParReduceBody<ParForBody> rb;
tbb::parallel_reduce( range_type(0, FLAT_RANGE, FLAT_GRAIN), rb, partitioner );
}
CATCH_AND_ASSERT();
// two cases: g_SolitaryException and !g_SolitaryException
// 1) g_SolitaryException: only one thread actually threw. There is only one context, so the exception
// (when caught) will cause that context to be cancelled. After this event, there may be one or
// more threads which are "in-flight", up to g_NumThreads, but no more will be started. The threads,
// when they start, if they see they are cancelled, TGCCancelled is incremented.
// 2) !g_SolitaryException: more than one thread can throw. The number of threads that actually
// threw is g_MasterExecutedThrow if only the master is allowed, else g_NonMasterExecutedThrow.
// Only one context, so TGCCancelled should be <= g_NumThreads.
//
// the reasoning is similar for nested algorithms in a single context (Test2).
//
// If a thread throws in a context, more than one subsequent task body may see the
// cancelled state (if they are scheduled before the state is propagated.) this is
// infrequent, but it occurs. So what was to be an assertion must be a remark.
ASSERT( g_TGCCancelled <= g_NumThreads, "Too many tasks ran after exception thrown");
if( g_TGCCancelled > g_NumThreads) REMARK( "Too many tasks ran after exception thrown (%d vs. %d)\n",
(int)g_TGCCancelled, (int)g_NumThreads);
ASSERT(g_CurExecuted <= g_ExecutedAtLastCatch + g_NumThreads, "Too many tasks survived exception");
if ( g_SolitaryException ) {
ASSERT(g_NumExceptionsCaught == 1, "No try_blocks in any body expected in this test");
ASSERT(g_NumExceptionsCaught == (g_ExceptionInMaster ? g_MasterExecutedThrow : g_NonMasterExecutedThrow),
"Not all throws were caught");
ASSERT(g_ExecutedAtFirstCatch == g_ExecutedAtLastCatch, "Too many exceptions occurred");
}
else {
ASSERT(g_NumExceptionsCaught >= 1, "No try blocks in any body expected in this test");
}
}
} // TestParallelLoopAux
//! Test with parallel_for and parallel_reduce, over all three kinds of partitioners.
/** The Body only needs to be suitable for tbb::parallel_for. */
template<typename Body>
void TestParallelLoop() {
// The simple and auto partitioners should be const, but not the affinity partitioner.
TestParallelLoopAux<Body, const tbb::simple_partitioner >();
TestParallelLoopAux<Body, const tbb::auto_partitioner >();
#define __TBB_TEMPORARILY_DISABLED 1
#if !__TBB_TEMPORARILY_DISABLED
// TODO: Improve the test so that it tolerates delayed start of tasks with affinity_partitioner
TestParallelLoopAux<Body, /***/ tbb::affinity_partitioner>();
#endif
#undef __TBB_TEMPORARILY_DISABLED
}
class SimpleParForBody: NoAssign {
public:
void operator()( const range_type& r ) const {
Harness::ConcurrencyTracker ct;
volatile long x = 0;
++g_CurExecuted;
if(g_Master == Harness::CurrentTid()) g_MasterExecuted = true;
else g_NonMasterExecuted = true;
if( tbb::task::self().is_cancelled() ) ++g_TGCCancelled;
for( count_type i = r.begin(); i != r.end(); ++i )
x += 0;
WaitUntilConcurrencyPeaks();
ThrowTestException(1);
}
};
void Test1() {
// non-nested parallel_for/reduce with throwing body, one context
TestParallelLoop<SimpleParForBody>();
} // void Test1 ()
class OuterParForBody: NoAssign {
public:
void operator()( const range_type& ) const {
Harness::ConcurrencyTracker ct;
++g_OuterParCalls;
tbb::parallel_for( tbb::blocked_range<size_t>(0, INNER_RANGE, INNER_GRAIN), SimpleParForBody() );
}
};
//! Uses parallel_for body containing an inner parallel_for with the default context not wrapped by a try-block.
/** Inner algorithms are spawned inside the new bound context by default. Since
exceptions thrown from the inner parallel_for are not handled by the caller
(outer parallel_for body) in this test, they will cancel all the sibling inner
algorithms. **/
void Test2 () {
TestParallelLoop<OuterParForBody>();
} // void Test2 ()
class OuterParForBodyWithIsolatedCtx {
public:
void operator()( const range_type& ) const {
tbb::task_group_context ctx(tbb::task_group_context::isolated);
++g_OuterParCalls;
tbb::parallel_for( tbb::blocked_range<size_t>(0, INNER_RANGE, INNER_GRAIN), SimpleParForBody(), tbb::simple_partitioner(), ctx );
}
};
//! Uses parallel_for body invoking an inner parallel_for with an isolated context without a try-block.
/** Even though exceptions thrown from the inner parallel_for are not handled
by the caller in this test, they will not affect sibling inner algorithms
already running because of the isolated contexts. However because the first
exception cancels the root parallel_for only the first g_NumThreads subranges
will be processed (which launch inner parallel_fors) **/
void Test3 () {
ResetGlobals();
typedef OuterParForBodyWithIsolatedCtx body_type;
intptr_t innerCalls = NumSubranges(INNER_RANGE, INNER_GRAIN),
// we expect one thread to throw without counting, the rest to run to completion
// this formula assumes g_numThreads outer pfor ranges will be started, but that is not the
// case; the SimpleParFor subranges are started up as part of the outer ones, and when
// the amount of concurrency reaches g_NumThreads no more outer Pfor ranges are started.
// so we have to count the number of outer Pfors actually started.
minExecuted = (g_NumThreads - 1) * innerCalls;
TRY();
tbb::parallel_for( range_type(0, OUTER_RANGE, OUTER_GRAIN), body_type() );
CATCH_AND_ASSERT();
minExecuted = (g_OuterParCalls - 1) * innerCalls; // see above
// The first formula above assumes all ranges of the outer parallel for are executed, and one
// cancels. In the event, we have a smaller number of ranges that start before the exception
// is caught.
//
// g_SolitaryException:One inner range throws. Outer parallel_For is cancelled, but sibling
// parallel_fors continue to completion (unless the threads that execute
// are not allowed to throw, in which case we will not see any exceptions).
// !g_SolitaryException:multiple inner ranges may throw. Any which throws will stop, and the
// corresponding range of the outer pfor will stop also.
//
// In either case, once the outer pfor gets the exception it will stop executing further ranges.
// if the only threads executing were not allowed to throw, then not seeing an exception is okay.
bool okayNoExceptionsCaught = (g_ExceptionInMaster && !g_MasterExecuted) || (!g_ExceptionInMaster && !g_NonMasterExecuted);
if ( g_SolitaryException ) {
ASSERT( g_TGCCancelled <= g_NumThreads, "Too many tasks survived exception");
ASSERT (g_CurExecuted > minExecuted, "Too few tasks survived exception");
ASSERT (g_CurExecuted <= minExecuted + (g_ExecutedAtLastCatch + g_NumThreads), "Too many tasks survived exception");
ASSERT (g_NumExceptionsCaught == 1 || okayNoExceptionsCaught, "No try_blocks in any body expected in this test");
}
else {
ASSERT (g_CurExecuted <= g_ExecutedAtLastCatch + g_NumThreads, "Too many tasks survived exception");
ASSERT (g_NumExceptionsCaught >= 1 || okayNoExceptionsCaught, "No try_blocks in any body expected in this test");
}
} // void Test3 ()
class OuterParForExceptionSafeBody {
public:
void operator()( const range_type& ) const {
tbb::task_group_context ctx(tbb::task_group_context::isolated);
++g_OuterParCalls;
TRY();
tbb::parallel_for( tbb::blocked_range<size_t>(0, INNER_RANGE, INNER_GRAIN), SimpleParForBody(), tbb::simple_partitioner(), ctx );
CATCH(); // this macro sets g_ExceptionCaught
}
};
//! Uses parallel_for body invoking an inner parallel_for (with isolated context) inside a try-block.
/** Since exception(s) thrown from the inner parallel_for are handled by the caller
in this test, they do not affect neither other tasks of the the root parallel_for
nor sibling inner algorithms. **/
void Test4 () {
ResetGlobals( true, true );
intptr_t innerCalls = NumSubranges(INNER_RANGE, INNER_GRAIN),
outerCalls = NumSubranges(OUTER_RANGE, OUTER_GRAIN);
TRY();
tbb::parallel_for( range_type(0, OUTER_RANGE, OUTER_GRAIN), OuterParForExceptionSafeBody() );
CATCH();
// g_SolitaryException : one inner pfor will throw, the rest will execute to completion.
// so the count should be (outerCalls -1) * innerCalls, if a throw happened.
// !g_SolitaryException : possible multiple inner pfor throws. Should be approximately
// (outerCalls - g_NumExceptionsCaught) * innerCalls, give or take a few
intptr_t minExecuted = (outerCalls - g_NumExceptionsCaught) * innerCalls;
bool okayNoExceptionsCaught = (g_ExceptionInMaster && !g_MasterExecuted) || (!g_ExceptionInMaster && !g_NonMasterExecuted);
if ( g_SolitaryException ) {
// only one task had exception thrown. That task had at least one execution (the one that threw).
// There may be an arbitrary number of ranges executed after the throw but before the exception
// is caught in the scheduler and cancellation is signaled. (seen 9, 11 and 62 (!) for 8 threads)
ASSERT (g_NumExceptionsCaught == 1 || okayNoExceptionsCaught, "No exception registered");
ASSERT (g_CurExecuted >= minExecuted, "Too few tasks executed");
ASSERT( g_TGCCancelled <= g_NumThreads, "Too many tasks survived exception");
// a small number of threads can execute in a throwing sub-pfor, if the task which is
// to do the solitary throw swaps out after registering its intent to throw but before it
// actually does so. (Or is this caused by the extra threads participating? No, the
// number of extra tasks is sometimes far greater than the number of extra threads.)
ASSERT (g_CurExecuted <= minExecuted + g_NumThreads, "Too many tasks survived exception");
if(g_CurExecuted > minExecuted + g_NumThreads) REMARK("Unusual number of tasks executed after signal (%d vs. %d)\n",
(int)g_CurExecuted, minExecuted + g_NumThreads);
}
else {
ASSERT ((g_NumExceptionsCaught >= 1 && g_NumExceptionsCaught <= outerCalls) || okayNoExceptionsCaught, "Unexpected actual number of exceptions");
ASSERT (g_CurExecuted >= minExecuted, "Too few executed tasks reported");
ASSERT (g_CurExecuted <= g_ExecutedAtLastCatch + g_NumThreads, "Too many tasks survived multiple exceptions");
if(g_CurExecuted > g_ExecutedAtLastCatch + g_NumThreads) REMARK("Unusual number of tasks executed after signal (%d vs. %d)\n",
(int)g_CurExecuted, g_ExecutedAtLastCatch + g_NumThreads);
ASSERT (g_CurExecuted <= outerCalls * (1 + g_NumThreads), "Too many tasks survived exception");
}
} // void Test4 ()
#endif /* TBB_USE_EXCEPTIONS */
class ParForBodyToCancel {
public:
void operator()( const range_type& ) const {
++g_CurExecuted;
CancellatorTask::WaitUntilReady();
}
};
template<class B>
class ParForLauncherTask : public tbb::task {
tbb::task_group_context &my_ctx;
tbb::task* execute () {
tbb::parallel_for( range_type(0, FLAT_RANGE, FLAT_GRAIN), B(), tbb::simple_partitioner(), my_ctx );
return NULL;
}
public:
ParForLauncherTask ( tbb::task_group_context& ctx ) : my_ctx(ctx) {}
};
//! Test for cancelling an algorithm from outside (from a task running in parallel with the algorithm).
void TestCancelation1 () {
ResetGlobals( false );
RunCancellationTest<ParForLauncherTask<ParForBodyToCancel>, CancellatorTask>( NumSubranges(FLAT_RANGE, FLAT_GRAIN) / 4 );
}
class CancellatorTask2 : public tbb::task {
tbb::task_group_context &m_GroupToCancel;
tbb::task* execute () {
Harness::ConcurrencyTracker ct;
WaitUntilConcurrencyPeaks();
m_GroupToCancel.cancel_group_execution();
g_ExecutedAtLastCatch = g_CurExecuted;
return NULL;
}
public:
CancellatorTask2 ( tbb::task_group_context& ctx, intptr_t ) : m_GroupToCancel(ctx) {}
};
class ParForBodyToCancel2 {
public:
void operator()( const range_type& ) const {
++g_CurExecuted;
Harness::ConcurrencyTracker ct;
// The test will hang (and be timed out by the test system) if is_cancelled() is broken
while( !tbb::task::self().is_cancelled() )
__TBB_Yield();
}
};
//! Test for cancelling an algorithm from outside (from a task running in parallel with the algorithm).
/** This version also tests task::is_cancelled() method. **/
void TestCancelation2 () {
ResetGlobals();
RunCancellationTest<ParForLauncherTask<ParForBodyToCancel2>, CancellatorTask2>();
ASSERT (g_ExecutedAtLastCatch < g_NumThreads, "Somehow worker tasks started their execution before the cancellator task");
ASSERT( g_TGCCancelled <= g_NumThreads, "Too many tasks survived cancellation");
ASSERT (g_CurExecuted <= g_ExecutedAtLastCatch + g_NumThreads, "Some tasks were executed after cancellation");
}
////////////////////////////////////////////////////////////////////////////////
// Regression test based on the contribution by the author of the following forum post:
// http://softwarecommunity.intel.com/isn/Community/en-US/forums/thread/30254959.aspx
class Worker {
static const int max_nesting = 3;
static const int reduce_range = 1024;
static const int reduce_grain = 256;
public:
int DoWork (int level);
int Validate (int start_level) {
int expected = 1; // identity for multiplication
for(int i=start_level+1; i<max_nesting; ++i)
expected *= reduce_range;
return expected;
}
};
class RecursiveParReduceBodyWithSharedWorker {
Worker * m_SharedWorker;
int m_NestingLevel;
int m_Result;
public:
RecursiveParReduceBodyWithSharedWorker ( RecursiveParReduceBodyWithSharedWorker& src, tbb::split )
: m_SharedWorker(src.m_SharedWorker)
, m_NestingLevel(src.m_NestingLevel)
, m_Result(0)
{}
RecursiveParReduceBodyWithSharedWorker ( Worker *w, int outer )
: m_SharedWorker(w)
, m_NestingLevel(outer)
, m_Result(0)
{}
void operator() ( const tbb::blocked_range<size_t>& r ) {
if(g_Master == Harness::CurrentTid()) g_MasterExecuted = true;
else g_NonMasterExecuted = true;
if( tbb::task::self().is_cancelled() ) ++g_TGCCancelled;
for (size_t i = r.begin (); i != r.end (); ++i) {
m_Result += m_SharedWorker->DoWork (m_NestingLevel);
}
}
void join (const RecursiveParReduceBodyWithSharedWorker & x) {
m_Result += x.m_Result;
}
int result () { return m_Result; }
};
int Worker::DoWork ( int level ) {
++level;
if ( level < max_nesting ) {
RecursiveParReduceBodyWithSharedWorker rt (this, level);
tbb::parallel_reduce (tbb::blocked_range<size_t>(0, reduce_range, reduce_grain), rt);
return rt.result();
}
else
return 1;
}
//! Regression test for hanging that occurred with the first version of cancellation propagation
void TestCancelation3 () {
Worker w;
int result = w.DoWork (0);
int expected = w.Validate(0);
ASSERT ( result == expected, "Wrong calculation result");
}
struct StatsCounters {
tbb::atomic<size_t> my_total_created;
tbb::atomic<size_t> my_total_deleted;
StatsCounters() {
my_total_created = 0;
my_total_deleted = 0;
}
};
class ParReduceBody {
StatsCounters* my_stats;
size_t my_id;
bool my_exception;
public:
ParReduceBody( StatsCounters& s_, bool e_ ) : my_stats(&s_), my_exception(e_) {
my_id = my_stats->my_total_created++;
}
ParReduceBody( const ParReduceBody& lhs ) {
my_stats = lhs.my_stats;
my_id = my_stats->my_total_created++;
}
ParReduceBody( ParReduceBody& lhs, tbb::split ) {
my_stats = lhs.my_stats;
my_id = my_stats->my_total_created++;
}
~ParReduceBody(){ ++my_stats->my_total_deleted; }
void operator()( const tbb::blocked_range<std::size_t>& /*range*/ ) const {
//Do nothing, except for one task (chosen arbitrarily)
if( my_id >= 12 ) {
if( my_exception )
ThrowTestException(1);
else
tbb::task::self().cancel_group_execution();
}
}
void join( ParReduceBody& /*rhs*/ ) {}
};
void TestCancelation4() {
StatsCounters statsObj;
__TBB_TRY {
tbb::task_group_context tgc1, tgc2;
ParReduceBody body_for_cancellation(statsObj, false), body_for_exception(statsObj, true);
tbb::parallel_reduce( tbb::blocked_range<std::size_t>(0,100000000,100), body_for_cancellation, tbb::simple_partitioner(), tgc1 );
tbb::parallel_reduce( tbb::blocked_range<std::size_t>(0,100000000,100), body_for_exception, tbb::simple_partitioner(), tgc2 );
} __TBB_CATCH(...) {}
ASSERT ( statsObj.my_total_created==statsObj.my_total_deleted, "Not all parallel_reduce body objects created were reclaimed");
}
void RunParForAndReduceTests () {
REMARK( "parallel for and reduce tests\n" );
tbb::task_scheduler_init init (g_NumThreads);
g_Master = Harness::CurrentTid();
#if TBB_USE_EXCEPTIONS && !__TBB_THROW_ACROSS_MODULE_BOUNDARY_BROKEN
Test0();
Test1();
Test2();
Test3();
Test4();
#endif /* TBB_USE_EXCEPTIONS && !__TBB_THROW_ACROSS_MODULE_BOUNDARY_BROKEN */
TestCancelation1();
TestCancelation2();
TestCancelation3();
TestCancelation4();
}
////////////////////////////////////////////////////////////////////////////////
// Tests for tbb::parallel_do
#define ITER_RANGE 1000
#define ITEMS_TO_FEED 50
#define INNER_ITER_RANGE 100
#define OUTER_ITER_RANGE 50
#define PREPARE_RANGE(Iterator, rangeSize) \
size_t test_vector[rangeSize + 1]; \
for (int i =0; i < rangeSize; i++) \
test_vector[i] = i; \
Iterator begin(&test_vector[0]); \
Iterator end(&test_vector[rangeSize])
void Feed ( tbb::parallel_do_feeder<size_t> &feeder, size_t val ) {
if (g_FedTasksCount < ITEMS_TO_FEED) {
++g_FedTasksCount;
feeder.add(val);
}
}
#include "harness_iterator.h"
#if TBB_USE_EXCEPTIONS
// Simple functor object with exception
class SimpleParDoBody {
public:
void operator() ( size_t &value ) const {
++g_CurExecuted;
if(g_Master == Harness::CurrentTid()) g_MasterExecuted = true;
else g_NonMasterExecuted = true;
if( tbb::task::self().is_cancelled() ) ++g_TGCCancelled;
Harness::ConcurrencyTracker ct;
value += 1000;
WaitUntilConcurrencyPeaks();
ThrowTestException(1);
}
};
// Simple functor object with exception and feeder
class SimpleParDoBodyWithFeeder : SimpleParDoBody {
public:
void operator() ( size_t &value, tbb::parallel_do_feeder<size_t> &feeder ) const {
Feed(feeder, 0);
SimpleParDoBody::operator()(value);
}
};
// Tests exceptions without nesting
template <class Iterator, class simple_body>
void Test1_parallel_do () {
ResetGlobals();
PREPARE_RANGE(Iterator, ITER_RANGE);
TRY();
tbb::parallel_do<Iterator, simple_body>(begin, end, simple_body() );
CATCH_AND_ASSERT();
ASSERT (g_CurExecuted <= g_ExecutedAtLastCatch + g_NumThreads, "Too many tasks survived exception");
ASSERT( g_TGCCancelled <= g_NumThreads, "Too many tasks survived cancellation");
ASSERT (g_NumExceptionsCaught == 1, "No try_blocks in any body expected in this test");
if ( !g_SolitaryException )
ASSERT (g_CurExecuted <= g_ExecutedAtLastCatch + g_NumThreads, "Too many tasks survived exception");
} // void Test1_parallel_do ()
template <class Iterator>
class OuterParDoBody {
public:
void operator()( size_t& /*value*/ ) const {
++g_OuterParCalls;
PREPARE_RANGE(Iterator, INNER_ITER_RANGE);
tbb::parallel_do<Iterator, SimpleParDoBody>(begin, end, SimpleParDoBody());
}
};
template <class Iterator>
class OuterParDoBodyWithFeeder : OuterParDoBody<Iterator> {
public:
void operator()( size_t& value, tbb::parallel_do_feeder<size_t>& feeder ) const {
Feed(feeder, 0);
OuterParDoBody<Iterator>::operator()(value);
}
};
//! Uses parallel_do body containing an inner parallel_do with the default context not wrapped by a try-block.
/** Inner algorithms are spawned inside the new bound context by default. Since
exceptions thrown from the inner parallel_do are not handled by the caller
(outer parallel_do body) in this test, they will cancel all the sibling inner
algorithms. **/
template <class Iterator, class outer_body>
void Test2_parallel_do () {
ResetGlobals();
PREPARE_RANGE(Iterator, ITER_RANGE);
TRY();
tbb::parallel_do<Iterator, outer_body >(begin, end, outer_body() );
CATCH_AND_ASSERT();
//if ( g_SolitaryException )
ASSERT (g_CurExecuted <= g_ExecutedAtLastCatch + g_NumThreads, "Too many tasks survived exception");
ASSERT( g_TGCCancelled <= g_NumThreads, "Too many tasks survived cancellation");
ASSERT (g_NumExceptionsCaught == 1, "No try_blocks in any body expected in this test");
if ( !g_SolitaryException )
ASSERT (g_CurExecuted <= g_ExecutedAtLastCatch + g_NumThreads, "Too many tasks survived exception");
} // void Test2_parallel_do ()
template <class Iterator>
class OuterParDoBodyWithIsolatedCtx {
public:
void operator()( size_t& /*value*/ ) const {
tbb::task_group_context ctx(tbb::task_group_context::isolated);
++g_OuterParCalls;
PREPARE_RANGE(Iterator, INNER_ITER_RANGE);
tbb::parallel_do<Iterator, SimpleParDoBody>(begin, end, SimpleParDoBody(), ctx);
}
};
template <class Iterator>
class OuterParDoBodyWithIsolatedCtxWithFeeder : OuterParDoBodyWithIsolatedCtx<Iterator> {
public:
void operator()( size_t& value, tbb::parallel_do_feeder<size_t> &feeder ) const {
Feed(feeder, 0);
OuterParDoBodyWithIsolatedCtx<Iterator>::operator()(value);
}
};
//! Uses parallel_do body invoking an inner parallel_do with an isolated context without a try-block.
/** Even though exceptions thrown from the inner parallel_do are not handled
by the caller in this test, they will not affect sibling inner algorithms
already running because of the isolated contexts. However because the first
exception cancels the root parallel_do, at most the first g_NumThreads subranges
will be processed (which launch inner parallel_dos) **/
template <class Iterator, class outer_body>
void Test3_parallel_do () {
ResetGlobals();
PREPARE_RANGE(Iterator, OUTER_ITER_RANGE);
intptr_t innerCalls = INNER_ITER_RANGE,
// The assumption here is the same as in outer parallel fors.
minExecuted = (g_NumThreads - 1) * innerCalls;
g_Master = Harness::CurrentTid();
TRY();
tbb::parallel_do<Iterator, outer_body >(begin, end, outer_body());
CATCH_AND_ASSERT();
// figure actual number of expected executions given the number of outer PDos started.
minExecuted = (g_OuterParCalls - 1) * innerCalls;
// one extra thread may run a task that sees cancellation. Infrequent but possible
ASSERT( g_TGCCancelled <= g_NumThreads, "Too many tasks survived exception");
if(g_TGCCancelled > g_NumThreads) REMARK("Extra thread(s) executed after cancel (%d vs. %d)\n",
(int)g_TGCCancelled, (int)g_NumThreads);
if ( g_SolitaryException ) {
ASSERT (g_CurExecuted > minExecuted, "Too few tasks survived exception");
ASSERT (g_CurExecuted <= minExecuted + (g_ExecutedAtLastCatch + g_NumThreads), "Too many tasks survived exception");
}
ASSERT (g_NumExceptionsCaught == 1, "No try_blocks in any body expected in this test");
if ( !g_SolitaryException )
ASSERT (g_CurExecuted <= g_ExecutedAtLastCatch + g_NumThreads, "Too many tasks survived exception");
} // void Test3_parallel_do ()
template <class Iterator>
class OuterParDoWithEhBody {
public:
void operator()( size_t& /*value*/ ) const {
tbb::task_group_context ctx(tbb::task_group_context::isolated);
++g_OuterParCalls;
PREPARE_RANGE(Iterator, INNER_ITER_RANGE);
TRY();
tbb::parallel_do<Iterator, SimpleParDoBody>(begin, end, SimpleParDoBody(), ctx);
CATCH();
}
};
template <class Iterator>
class OuterParDoWithEhBodyWithFeeder : NoAssign, OuterParDoWithEhBody<Iterator> {
public:
void operator()( size_t &value, tbb::parallel_do_feeder<size_t> &feeder ) const {
Feed(feeder, 0);
OuterParDoWithEhBody<Iterator>::operator()(value);
}
};
//! Uses parallel_for body invoking an inner parallel_for (with default bound context) inside a try-block.
/** Since exception(s) thrown from the inner parallel_for are handled by the caller
in this test, they do not affect neither other tasks of the the root parallel_for
nor sibling inner algorithms. **/
template <class Iterator, class outer_body_with_eh>
void Test4_parallel_do () {
ResetGlobals( true, true );
PREPARE_RANGE(Iterator, OUTER_ITER_RANGE);
g_Master = Harness::CurrentTid();
TRY();
tbb::parallel_do<Iterator, outer_body_with_eh>(begin, end, outer_body_with_eh());
CATCH();
ASSERT (!l_ExceptionCaughtAtCurrentLevel, "All exceptions must have been handled in the parallel_do body");
intptr_t innerCalls = INNER_ITER_RANGE,
outerCalls = OUTER_ITER_RANGE + g_FedTasksCount,
maxExecuted = outerCalls * innerCalls,
minExecuted = 0;
ASSERT( g_TGCCancelled <= g_NumThreads, "Too many tasks survived exception");
if ( g_SolitaryException ) {
minExecuted = maxExecuted - innerCalls;
ASSERT (g_NumExceptionsCaught == 1, "No exception registered");
ASSERT (g_CurExecuted >= minExecuted, "Too few tasks executed");
// This test has the same property as Test4 (parallel_for); the exception can be
// thrown, but some number of tasks from the outer Pdo can execute after the throw but
// before the cancellation is signaled (have seen 36).
ASSERT_WARNING(g_CurExecuted < maxExecuted || g_TGCCancelled, "All tasks survived exception. Oversubscription?");
}
else {
minExecuted = g_NumExceptionsCaught;
ASSERT (g_NumExceptionsCaught > 1 && g_NumExceptionsCaught <= outerCalls, "Unexpected actual number of exceptions");
ASSERT (g_CurExecuted >= minExecuted, "Too many executed tasks reported");
ASSERT (g_CurExecuted < g_ExecutedAtLastCatch + g_NumThreads + outerCalls, "Too many tasks survived multiple exceptions");
ASSERT (g_CurExecuted <= outerCalls * (1 + g_NumThreads), "Too many tasks survived exception");
}
} // void Test4_parallel_do ()
// This body throws an exception only if the task was added by feeder
class ParDoBodyWithThrowingFeederTasks {
public:
//! This form of the function call operator can be used when the body needs to add more work during the processing
void operator() ( size_t &value, tbb::parallel_do_feeder<size_t> &feeder ) const {
++g_CurExecuted;
if(g_Master == Harness::CurrentTid()) g_MasterExecuted = true;
else g_NonMasterExecuted = true;
if( tbb::task::self().is_cancelled() ) ++g_TGCCancelled;
Feed(feeder, 1);
if (value == 1)
ThrowTestException(1);
}
}; // class ParDoBodyWithThrowingFeederTasks
// Test exception in task, which was added by feeder.
template <class Iterator>
void Test5_parallel_do () {
ResetGlobals();
PREPARE_RANGE(Iterator, ITER_RANGE);
g_Master = Harness::CurrentTid();
TRY();
tbb::parallel_do<Iterator, ParDoBodyWithThrowingFeederTasks>(begin, end, ParDoBodyWithThrowingFeederTasks());
CATCH();
if (g_SolitaryException) {
// Failure occurs when g_ExceptionInMaster is false, but all the 1 values in the range
// are handled by the master thread. In this case no throw occurs.
ASSERT (l_ExceptionCaughtAtCurrentLevel // we saw an exception
|| (!g_ExceptionInMaster && !g_NonMasterExecutedThrow) // non-master throws but none tried
|| (g_ExceptionInMaster && !g_MasterExecutedThrow) // master throws but master didn't try
, "At least one exception should occur");
if(!g_ExceptionCaught) {
if(g_ExceptionInMaster)
REMARK("PDo exception not thrown; non-masters handled all throwing values.\n");
else
REMARK("PDo exception not thrown; master handled all throwing values.\n");
}
}
} // void Test5_parallel_do ()
#endif /* TBB_USE_EXCEPTIONS */
class ParDoBodyToCancel {
public:
void operator()( size_t& /*value*/ ) const {
++g_CurExecuted;
CancellatorTask::WaitUntilReady();
}
};
class ParDoBodyToCancelWithFeeder : ParDoBodyToCancel {
public:
void operator()( size_t& value, tbb::parallel_do_feeder<size_t> &feeder ) const {
Feed(feeder, 0);
ParDoBodyToCancel::operator()(value);
}
};
template<class B, class Iterator>
class ParDoWorkerTask : public tbb::task {
tbb::task_group_context &my_ctx;
tbb::task* execute () {
PREPARE_RANGE(Iterator, INNER_ITER_RANGE);
tbb::parallel_do<Iterator, B>( begin, end, B(), my_ctx );
return NULL;
}
public:
ParDoWorkerTask ( tbb::task_group_context& ctx ) : my_ctx(ctx) {}
};
//! Test for cancelling an algorithm from outside (from a task running in parallel with the algorithm).
template <class Iterator, class body_to_cancel>
void TestCancelation1_parallel_do () {
ResetGlobals( false );
intptr_t threshold = 10;
tbb::task_group_context ctx;
ctx.reset();
tbb::empty_task &r = *new( tbb::task::allocate_root() ) tbb::empty_task;
r.set_ref_count(3);
r.spawn( *new( r.allocate_child() ) CancellatorTask(ctx, threshold) );
__TBB_Yield();
r.spawn( *new( r.allocate_child() ) ParDoWorkerTask<body_to_cancel, Iterator>(ctx) );
TRY();
r.wait_for_all();
CATCH_AND_FAIL();
ASSERT (g_CurExecuted < g_ExecutedAtLastCatch + g_NumThreads, "Too many tasks were executed after cancellation");
r.destroy(r);
}
class ParDoBodyToCancel2 {
public:
void operator()( size_t& /*value*/ ) const {
++g_CurExecuted;
Harness::ConcurrencyTracker ct;
// The test will hang (and be timed out by the test system) if is_cancelled() is broken
while( !tbb::task::self().is_cancelled() )
__TBB_Yield();
}
};
class ParDoBodyToCancel2WithFeeder : ParDoBodyToCancel2 {
public:
void operator()( size_t& value, tbb::parallel_do_feeder<size_t> &feeder ) const {
Feed(feeder, 0);
ParDoBodyToCancel2::operator()(value);
}
};
//! Test for cancelling an algorithm from outside (from a task running in parallel with the algorithm).
/** This version also tests task::is_cancelled() method. **/
template <class Iterator, class body_to_cancel>
void TestCancelation2_parallel_do () {
ResetGlobals();
RunCancellationTest<ParDoWorkerTask<body_to_cancel, Iterator>, CancellatorTask2>();
}
#define RunWithSimpleBody(func, body) \
func<Harness::RandomIterator<size_t>, body>(); \
func<Harness::RandomIterator<size_t>, body##WithFeeder>(); \
func<Harness::ForwardIterator<size_t>, body>(); \
func<Harness::ForwardIterator<size_t>, body##WithFeeder>()
#define RunWithTemplatedBody(func, body) \
func<Harness::RandomIterator<size_t>, body<Harness::RandomIterator<size_t> > >(); \
func<Harness::RandomIterator<size_t>, body##WithFeeder<Harness::RandomIterator<size_t> > >(); \
func<Harness::ForwardIterator<size_t>, body<Harness::ForwardIterator<size_t> > >(); \
func<Harness::ForwardIterator<size_t>, body##WithFeeder<Harness::ForwardIterator<size_t> > >()
void RunParDoTests() {
REMARK( "parallel do tests\n" );
tbb::task_scheduler_init init (g_NumThreads);
g_Master = Harness::CurrentTid();
#if TBB_USE_EXCEPTIONS && !__TBB_THROW_ACROSS_MODULE_BOUNDARY_BROKEN
RunWithSimpleBody(Test1_parallel_do, SimpleParDoBody);
RunWithTemplatedBody(Test2_parallel_do, OuterParDoBody);
RunWithTemplatedBody(Test3_parallel_do, OuterParDoBodyWithIsolatedCtx);
RunWithTemplatedBody(Test4_parallel_do, OuterParDoWithEhBody);
Test5_parallel_do<Harness::ForwardIterator<size_t> >();
Test5_parallel_do<Harness::RandomIterator<size_t> >();
#endif /* TBB_USE_EXCEPTIONS && !__TBB_THROW_ACROSS_MODULE_BOUNDARY_BROKEN */
RunWithSimpleBody(TestCancelation1_parallel_do, ParDoBodyToCancel);
RunWithSimpleBody(TestCancelation2_parallel_do, ParDoBodyToCancel2);
}
////////////////////////////////////////////////////////////////////////////////
// Tests for tbb::pipeline
#define NUM_ITEMS 100
const size_t c_DataEndTag = size_t(~0);
int g_NumTokens = 0;
// Simple input filter class, it assigns 1 to all array members
// It stops when it receives item equal to -1
class InputFilter: public tbb::filter {
tbb::atomic<size_t> m_Item;
size_t m_Buffer[NUM_ITEMS + 1];
public:
InputFilter() : tbb::filter(parallel) {
m_Item = 0;
for (size_t i = 0; i < NUM_ITEMS; ++i )
m_Buffer[i] = 1;
m_Buffer[NUM_ITEMS] = c_DataEndTag;
}
void* operator()( void* ) {
size_t item = m_Item.fetch_and_increment();
if(g_Master == Harness::CurrentTid()) g_MasterExecuted = true;
else g_NonMasterExecuted = true;
if( tbb::task::self().is_cancelled() ) ++g_TGCCancelled;
if(item == 1) {
++g_PipelinesStarted; // count on emitting the first item.
}
if ( item >= NUM_ITEMS )
return NULL;
m_Buffer[item] = 1;
return &m_Buffer[item];
}
size_t* buffer() { return m_Buffer; }
}; // class InputFilter
// Pipeline filter, without exceptions throwing
class NoThrowFilter : public tbb::filter {
size_t m_Value;
public:
enum operation {
addition,
subtraction,
multiplication
} m_Operation;
NoThrowFilter(operation _operation, size_t value, bool is_parallel)
: filter(is_parallel? tbb::filter::parallel : tbb::filter::serial_in_order),
m_Value(value), m_Operation(_operation)
{}
void* operator()(void* item) {
size_t &value = *(size_t*)item;
if(g_Master == Harness::CurrentTid()) g_MasterExecuted = true;
else g_NonMasterExecuted = true;
if( tbb::task::self().is_cancelled() ) ++g_TGCCancelled;
ASSERT(value != c_DataEndTag, "terminator element is being processed");
switch (m_Operation){
case addition:
value += m_Value;
break;
case subtraction:
value -= m_Value;
break;
case multiplication:
value *= m_Value;
break;
default:
ASSERT(0, "Wrong operation parameter passed to NoThrowFilter");
} // switch (m_Operation)
return item;
}