-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathtest_enumerable_thread_specific.cpp
1380 lines (1149 loc) · 52 KB
/
test_enumerable_thread_specific.cpp
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 (c) 2005-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define HARNESS_DEFAULT_MIN_THREADS 0
#define HARNESS_DEFAULT_MAX_THREADS 4
#include "tbb/enumerable_thread_specific.h"
#include "tbb/task_scheduler_init.h"
#include "tbb/parallel_for.h"
#include "tbb/parallel_reduce.h"
#include "tbb/blocked_range.h"
#include "tbb/tick_count.h"
#include "tbb/tbb_allocator.h"
#include "tbb/tbb_thread.h"
#include "tbb/atomic.h"
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <utility>
#include "harness_assert.h"
#include "harness.h"
#include "harness_checktype.h"
#include "../tbbmalloc/shared_utils.h"
using rml::internal::estimatedCacheLineSize;
#if __TBB_GCC_WARNING_SUPPRESSION_PRESENT
#pragma GCC diagnostic ignored "-Wuninitialized"
#endif
static tbb::atomic<int> construction_counter;
static tbb::atomic<int> destruction_counter;
#if TBB_USE_DEBUG
const int REPETITIONS = 4;
const int N = 10000;
const int RANGE_MIN=1000;
#else
const int REPETITIONS = 10;
const int N = 100000;
const int RANGE_MIN=10000;
#endif
const int VALID_NUMBER_OF_KEYS = 100;
const double EXPECTED_SUM = (REPETITIONS + 1) * N;
//! A minimal class that occupies N bytes.
/** Defines default and copy constructor, and allows implicit operator&.
Hides operator=. */
template<size_t N=tbb::internal::NFS_MaxLineSize>
class minimal: NoAssign {
private:
int my_value;
bool is_constructed;
char pad[N-sizeof(int) - sizeof(bool)];
public:
minimal() : NoAssign(), my_value(0) { ++construction_counter; is_constructed = true; }
minimal( const minimal &m ) : NoAssign(), my_value(m.my_value) { ++construction_counter; is_constructed = true; }
~minimal() { ++destruction_counter; ASSERT(is_constructed, NULL); is_constructed = false; }
void set_value( const int i ) { ASSERT(is_constructed, NULL); my_value = i; }
int value( ) const { ASSERT(is_constructed, NULL); return my_value; }
};
static size_t AlignMask = 0; // set to cache-line-size - 1
template<typename T>
T& check_alignment(T& t, const char *aname) {
if( !tbb::internal::is_aligned(&t, AlignMask)) {
REPORT_ONCE("alignment error with %s allocator (%x)\n", aname, (int)size_t(&t) & (AlignMask-1));
}
return t;
}
template<typename T>
const T& check_alignment(const T& t, const char *aname) {
if( !tbb::internal::is_aligned(&t, AlignMask)) {
REPORT_ONCE("alignment error with %s allocator (%x)\n", aname, (int)size_t(&t) & (AlignMask-1));
}
return t;
}
// Test constructors which throw. If an ETS constructor throws before completion,
// the already-built objects are un-constructed. Do not call the destructor if
// this occurs.
static tbb::atomic<int> gThrowValue;
static int targetThrowValue = 3;
class Thrower {
public:
Thrower() {
#if TBB_USE_EXCEPTIONS
if(++gThrowValue == targetThrowValue) {
throw std::bad_alloc();
}
#endif
}
};
// MyThrower field of ThrowingConstructor will throw after a certain number of
// construction calls. The constructor unwinder wshould unconstruct the instance
// of check_type<int> that was constructed just before.
class ThrowingConstructor {
check_type<int> m_checktype;
Thrower m_throwing_field;
public:
int m_cnt;
ThrowingConstructor() : m_checktype(), m_throwing_field() { m_cnt = 0;}
private:
};
//
// A helper class that simplifies writing the tests since minimal does not
// define = or + operators.
//
template< typename T >
struct test_helper {
static inline void init(T &e) { e = static_cast<T>(0); }
static inline void sum(T &e, const int addend ) { e += static_cast<T>(addend); }
static inline void sum(T &e, const double addend ) { e += static_cast<T>(addend); }
static inline void set(T &e, const int value ) { e = static_cast<T>(value); }
static inline double get(const T &e ) { return static_cast<double>(e); }
};
template<size_t N>
struct test_helper<minimal<N> > {
static inline void init(minimal<N> &sum) { sum.set_value( 0 ); }
static inline void sum(minimal<N> &sum, const int addend ) { sum.set_value( sum.value() + addend); }
static inline void sum(minimal<N> &sum, const double addend ) { sum.set_value( sum.value() + static_cast<int>(addend)); }
static inline void sum(minimal<N> &sum, const minimal<N> &addend ) { sum.set_value( sum.value() + addend.value()); }
static inline void set(minimal<N> &v, const int value ) { v.set_value( static_cast<int>(value) ); }
static inline double get(const minimal<N> &sum ) { return static_cast<double>(sum.value()); }
};
template<>
struct test_helper<ThrowingConstructor> {
static inline void init(ThrowingConstructor &sum) { sum.m_cnt = 0; }
static inline void sum(ThrowingConstructor &sum, const int addend ) { sum.m_cnt += addend; }
static inline void sum(ThrowingConstructor &sum, const double addend ) { sum.m_cnt += static_cast<int>(addend); }
static inline void sum(ThrowingConstructor &sum, const ThrowingConstructor &addend ) { sum.m_cnt += addend.m_cnt; }
static inline void set(ThrowingConstructor &v, const int value ) { v.m_cnt = static_cast<int>(value); }
static inline double get(const ThrowingConstructor &sum ) { return static_cast<double>(sum.m_cnt); }
};
//! Tag class used to make certain constructors hard to invoke accidentally.
struct SecretTagType {} SecretTag;
//// functors and routines for initialization and combine
//! Counts instances of FunctorFinit
static tbb::atomic<int> FinitCounter;
template <typename T, int Value>
struct FunctorFinit {
FunctorFinit( const FunctorFinit& ) {++FinitCounter;}
FunctorFinit( SecretTagType ) {++FinitCounter;}
~FunctorFinit() {--FinitCounter;}
T operator()() { return Value; }
};
template <int Value>
struct FunctorFinit<ThrowingConstructor,Value> {
FunctorFinit( const FunctorFinit& ) {++FinitCounter;}
FunctorFinit( SecretTagType ) {++FinitCounter;}
~FunctorFinit() {--FinitCounter;}
ThrowingConstructor operator()() { ThrowingConstructor temp; temp.m_cnt = Value; return temp; }
};
template <size_t N, int Value>
struct FunctorFinit<minimal<N>,Value> {
FunctorFinit( const FunctorFinit& ) {++FinitCounter;}
FunctorFinit( SecretTagType ) {++FinitCounter;}
~FunctorFinit() {--FinitCounter;}
minimal<N> operator()() {
minimal<N> result;
result.set_value( Value );
return result;
}
};
// Addition
template <typename T>
struct FunctorAddCombineRef {
T operator()(const T& left, const T& right) const {
return left+right;
}
};
template <size_t N>
struct FunctorAddCombineRef<minimal<N> > {
minimal<N> operator()(const minimal<N>& left, const minimal<N>& right) const {
minimal<N> result;
result.set_value( left.value() + right.value() );
return result;
}
};
template <>
struct FunctorAddCombineRef<ThrowingConstructor> {
ThrowingConstructor operator()(const ThrowingConstructor& left, const ThrowingConstructor& right) const {
ThrowingConstructor result;
result.m_cnt = ( left.m_cnt + right.m_cnt );
return result;
}
};
template <typename T>
struct FunctorAddCombine {
T operator()(T left, T right ) const {
return FunctorAddCombineRef<T>()( left, right );
}
};
template <typename T>
T FunctionAddByRef( const T &left, const T &right) {
return FunctorAddCombineRef<T>()( left, right );
}
template <typename T>
T FunctionAdd( T left, T right) { return FunctionAddByRef(left,right); }
template <typename T>
class Accumulator {
public:
Accumulator(T& _result) : my_result(_result) {}
Accumulator& operator=(const Accumulator& other) {
test_helper<T>::set(my_result, test_helper<T>::get(other));
return *this;
}
void operator()(const T& new_bit) { test_helper<T>::sum(my_result, new_bit); }
private:
T& my_result;
};
template <typename T>
class ClearingAccumulator {
public:
ClearingAccumulator(T& _result) : my_result(_result) {}
ClearingAccumulator& operator=(const ClearingAccumulator& other) {
test_helper<T>::set(my_result, test_helper<T>::get(other));
return *this;
}
void operator()(T& new_bit) {
test_helper<T>::sum(my_result, new_bit);
test_helper<T>::init(new_bit);
}
static void AssertClean(const T& thread_local_value) {
T zero;
test_helper<T>::init(zero);
ASSERT(test_helper<T>::get(thread_local_value)==test_helper<T>::get(zero),
"combine_each does not allow to modify thread local values?");
}
private:
T& my_result;
};
//// end functors and routines
template< typename T >
void run_serial_scalar_tests(const char *test_name) {
tbb::tick_count t0;
T sum;
test_helper<T>::init(sum);
REMARK("Testing serial %s... ", test_name);
for (int t = -1; t < REPETITIONS; ++t) {
if (Verbose && t == 0) t0 = tbb::tick_count::now();
for (int i = 0; i < N; ++i) {
test_helper<T>::sum(sum,1);
}
}
double result_value = test_helper<T>::get(sum);
ASSERT( EXPECTED_SUM == result_value, NULL);
REMARK("done\nserial %s, 0, %g, %g\n", test_name, result_value, ( tbb::tick_count::now() - t0).seconds());
}
template <typename T, template<class> class Allocator>
class parallel_scalar_body: NoAssign {
typedef tbb::enumerable_thread_specific<T, Allocator<T> > ets_type;
ets_type &sums;
const char* allocator_name;
public:
parallel_scalar_body ( ets_type &_sums, const char *alloc_name ) : sums(_sums), allocator_name(alloc_name) { }
void operator()( const tbb::blocked_range<int> &r ) const {
for (int i = r.begin(); i != r.end(); ++i)
test_helper<T>::sum( check_alignment(sums.local(),allocator_name), 1 );
}
};
template< typename T, template<class> class Allocator>
void run_parallel_scalar_tests_nocombine(const char *test_name, const char *allocator_name) {
typedef tbb::enumerable_thread_specific<T, Allocator<T> > ets_type;
Check<T> my_check;
gThrowValue = 0;
{
// We assume that static_sums zero-initialized or has a default constructor that zeros it.
static ets_type static_sums = ets_type( T() );
T exemplar;
test_helper<T>::init(exemplar);
for (int p = MinThread; p <= MaxThread; ++p) {
REMARK("Testing parallel %s with allocator %s on %d thread(s)... ", test_name, allocator_name, p);
tbb::task_scheduler_init init(p);
tbb::tick_count t0;
T iterator_sum;
test_helper<T>::init(iterator_sum);
T finit_ets_sum;
test_helper<T>::init(finit_ets_sum);
T const_iterator_sum;
test_helper<T>::init(const_iterator_sum);
T range_sum;
test_helper<T>::init(range_sum);
T const_range_sum;
test_helper<T>::init(const_range_sum);
T cconst_sum;
test_helper<T>::init(cconst_sum);
T assign_sum;
test_helper<T>::init(assign_sum);
T cassgn_sum;
test_helper<T>::init(cassgn_sum);
T non_cassgn_sum;
test_helper<T>::init(non_cassgn_sum);
T static_sum;
test_helper<T>::init(static_sum);
for (int t = -1; t < REPETITIONS; ++t) {
if (Verbose && t == 0) t0 = tbb::tick_count::now();
static_sums.clear();
ets_type sums(exemplar);
FunctorFinit<T,0> my_finit(SecretTag);
ets_type finit_ets(my_finit);
ASSERT( sums.empty(), NULL);
tbb::parallel_for( tbb::blocked_range<int>( 0, N, RANGE_MIN ), parallel_scalar_body<T,Allocator>( sums, allocator_name ) );
ASSERT( !sums.empty(), NULL);
ASSERT( finit_ets.empty(), NULL);
tbb::parallel_for( tbb::blocked_range<int>( 0, N, RANGE_MIN ), parallel_scalar_body<T,Allocator>( finit_ets, allocator_name ) );
ASSERT( !finit_ets.empty(), NULL);
ASSERT(static_sums.empty(), NULL);
tbb::parallel_for( tbb::blocked_range<int>( 0, N, RANGE_MIN ), parallel_scalar_body<T,Allocator>( static_sums, allocator_name ) );
ASSERT( !static_sums.empty(), NULL);
// use iterator
typename ets_type::size_type size = 0;
for ( typename ets_type::iterator i = sums.begin(); i != sums.end(); ++i ) {
++size;
test_helper<T>::sum(iterator_sum, *i);
}
ASSERT( sums.size() == size, NULL);
// use const_iterator
for ( typename ets_type::const_iterator i = sums.begin(); i != sums.end(); ++i ) {
test_helper<T>::sum(const_iterator_sum, *i);
}
// use range_type
typename ets_type::range_type r = sums.range();
for ( typename ets_type::range_type::const_iterator i = r.begin(); i != r.end(); ++i ) {
test_helper<T>::sum(range_sum, *i);
}
// use const_range_type
typename ets_type::const_range_type cr = sums.range();
for ( typename ets_type::const_range_type::iterator i = cr.begin(); i != cr.end(); ++i ) {
test_helper<T>::sum(const_range_sum, *i);
}
// test copy constructor, with TLS-cached locals
typedef typename tbb::enumerable_thread_specific<T, Allocator<T>, tbb::ets_key_per_instance> cached_ets_type;
cached_ets_type cconst(sums);
for ( typename cached_ets_type::const_iterator i = cconst.begin(); i != cconst.end(); ++i ) {
test_helper<T>::sum(cconst_sum, *i);
}
// test assignment
ets_type assigned;
assigned = sums;
for ( typename ets_type::const_iterator i = assigned.begin(); i != assigned.end(); ++i ) {
test_helper<T>::sum(assign_sum, *i);
}
// test assign to and from cached locals
cached_ets_type cassgn;
cassgn = sums;
for ( typename cached_ets_type::const_iterator i = cassgn.begin(); i != cassgn.end(); ++i ) {
test_helper<T>::sum(cassgn_sum, *i);
}
ets_type non_cassgn;
non_cassgn = cassgn;
for ( typename ets_type::const_iterator i = non_cassgn.begin(); i != non_cassgn.end(); ++i ) {
test_helper<T>::sum(non_cassgn_sum, *i);
}
// test finit-initialized ets
for(typename ets_type::const_iterator i = finit_ets.begin(); i != finit_ets.end(); ++i) {
test_helper<T>::sum(finit_ets_sum, *i);
}
// test static ets
for(typename ets_type::const_iterator i = static_sums.begin(); i != static_sums.end(); ++i) {
test_helper<T>::sum(static_sum, *i);
}
}
ASSERT( EXPECTED_SUM == test_helper<T>::get(iterator_sum), NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(const_iterator_sum), NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(range_sum), NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(const_range_sum), NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(cconst_sum), NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(assign_sum), NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(cassgn_sum), NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(non_cassgn_sum), NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(finit_ets_sum), NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(static_sum), NULL);
REMARK("done\nparallel %s, %d, %g, %g\n", test_name, p, test_helper<T>::get(iterator_sum),
( tbb::tick_count::now() - t0).seconds());
}
} // Check block
}
template< typename T, template<class> class Allocator>
void run_parallel_scalar_tests(const char *test_name, const char *allocator_name) {
typedef tbb::enumerable_thread_specific<T, Allocator<T> > ets_type;
bool exception_caught = false;
// We assume that static_sums zero-initialized or has a default constructor that zeros it.
static ets_type static_sums = ets_type( T() );
T exemplar;
test_helper<T>::init(exemplar);
int test_throw_count = 10;
// the test will be performed repeatedly until it does not throw. For non-throwing types
// this means once; for the throwing type test it may loop two or three times. The
// value of targetThrowValue will determine when and if the test will throw.
do {
targetThrowValue = test_throw_count; // keep testing until we get no exception
exception_caught = false;
#if TBB_USE_EXCEPTIONS
try {
#endif
run_parallel_scalar_tests_nocombine<T,Allocator>(test_name, allocator_name);
#if TBB_USE_EXCEPTIONS
}
catch(...) {
REMARK("Exception caught %d\n", targetThrowValue);
}
#endif
for (int p = MinThread; p <= MaxThread; ++p) {
REMARK("Testing parallel %s with allocator %s on %d thread(s)... ", test_name, allocator_name, p);
tbb::task_scheduler_init init(p);
tbb::tick_count t0;
gThrowValue = 0;
T combine_sum;
test_helper<T>::init(combine_sum);
T combine_ref_sum;
test_helper<T>::init(combine_ref_sum);
T accumulator_sum;
test_helper<T>::init(accumulator_sum);
T static_sum;
test_helper<T>::init(static_sum);
T clearing_accumulator_sum;
test_helper<T>::init(clearing_accumulator_sum);
{
Check<T> my_check;
#if TBB_USE_EXCEPTIONS
try
#endif
{
for (int t = -1; t < REPETITIONS; ++t) {
if (Verbose && t == 0) t0 = tbb::tick_count::now();
static_sums.clear();
ets_type sums(exemplar);
ASSERT( sums.empty(), NULL);
tbb::parallel_for( tbb::blocked_range<int>( 0, N, RANGE_MIN ),
parallel_scalar_body<T,Allocator>( sums, allocator_name ) );
ASSERT( !sums.empty(), NULL);
ASSERT(static_sums.empty(), NULL);
tbb::parallel_for( tbb::blocked_range<int>( 0, N, RANGE_MIN ),
parallel_scalar_body<T,Allocator>( static_sums, allocator_name ) );
ASSERT( !static_sums.empty(), NULL);
// Use combine
test_helper<T>::sum(combine_sum, sums.combine(FunctionAdd<T>));
test_helper<T>::sum(combine_ref_sum, sums.combine(FunctionAddByRef<T>));
test_helper<T>::sum(static_sum, static_sums.combine(FunctionAdd<T>));
// Accumulate with combine_each
sums.combine_each(Accumulator<T>(accumulator_sum));
// Accumulate and clear thread-local values
sums.combine_each(ClearingAccumulator<T>(clearing_accumulator_sum));
// Check that the values were cleared
sums.combine_each(ClearingAccumulator<T>::AssertClean);
}
}
#if TBB_USE_EXCEPTIONS
catch(...) {
REMARK("Exception caught %d\n", targetThrowValue);
exception_caught = true;
}
#endif
}
ASSERT( EXPECTED_SUM == test_helper<T>::get(combine_sum) || exception_caught, NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(combine_ref_sum) || exception_caught, NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(static_sum) || exception_caught, NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(accumulator_sum) || exception_caught, NULL);
ASSERT( EXPECTED_SUM == test_helper<T>::get(clearing_accumulator_sum) || exception_caught, NULL);
REMARK("done\nparallel combine %s, %d, %g, %g\n", test_name, p, test_helper<T>::get(combine_sum),
( tbb::tick_count::now() - t0).seconds());
} // MinThread .. MaxThread
test_throw_count += 10; // keep testing until we don't get an exception
} while (exception_caught && test_throw_count < 200);
ASSERT(!exception_caught, "No non-exception test completed");
}
template <typename T, template<class> class Allocator>
class parallel_vector_for_body: NoAssign {
typedef std::vector<T, tbb::tbb_allocator<T> > container_type;
typedef tbb::enumerable_thread_specific< container_type, Allocator<container_type> > ets_type;
ets_type &locals;
const char *allocator_name;
public:
parallel_vector_for_body ( ets_type &_locals, const char *aname ) : locals(_locals), allocator_name(aname) { }
void operator()( const tbb::blocked_range<int> &r ) const {
T one;
test_helper<T>::set(one, 1);
for (int i = r.begin(); i < r.end(); ++i) {
check_alignment(locals.local(),allocator_name).push_back( one );
}
}
};
template <typename R, typename T>
struct parallel_vector_reduce_body {
T sum;
size_t count;
typedef std::vector<T, tbb::tbb_allocator<T> > container_type;
parallel_vector_reduce_body ( ) : count(0) { test_helper<T>::init(sum); }
parallel_vector_reduce_body ( parallel_vector_reduce_body<R, T> &, tbb::split ) : count(0) { test_helper<T>::init(sum); }
void operator()( const R &r ) {
for (typename R::iterator ri = r.begin(); ri != r.end(); ++ri) {
const container_type &v = *ri;
++count;
for (typename container_type::const_iterator vi = v.begin(); vi != v.end(); ++vi) {
test_helper<T>::sum(sum, *vi);
}
}
}
void join( const parallel_vector_reduce_body &b ) {
test_helper<T>::sum(sum,b.sum);
count += b.count;
}
};
template< typename T, template<class> class Allocator>
void run_parallel_vector_tests(const char *test_name, const char *allocator_name) {
tbb::tick_count t0;
typedef std::vector<T, tbb::tbb_allocator<T> > container_type;
typedef tbb::enumerable_thread_specific< container_type, Allocator<container_type> > ets_type;
for (int p = MinThread; p <= MaxThread; ++p) {
REMARK("Testing parallel %s with allocator %s on %d thread(s)... ", test_name, allocator_name, p);
tbb::task_scheduler_init init(p);
T sum;
test_helper<T>::init(sum);
for (int t = -1; t < REPETITIONS; ++t) {
if (Verbose && t == 0) t0 = tbb::tick_count::now();
ets_type vs;
ASSERT( vs.empty(), NULL );
tbb::parallel_for( tbb::blocked_range<int> (0, N, RANGE_MIN),
parallel_vector_for_body<T,Allocator>( vs, allocator_name ) );
ASSERT( !vs.empty(), NULL );
// copy construct
ets_type vs2(vs); // this causes an assertion failure, related to allocators...
// assign
ets_type vs3;
vs3 = vs;
parallel_vector_reduce_body< typename ets_type::const_range_type, T > pvrb;
tbb::parallel_reduce ( vs.range(1), pvrb );
test_helper<T>::sum(sum, pvrb.sum);
ASSERT( vs.size() == pvrb.count, NULL );
ASSERT( vs2.size() == pvrb.count, NULL );
ASSERT( vs3.size() == pvrb.count, NULL );
tbb::flattened2d<ets_type> fvs = flatten2d(vs);
size_t ccount = fvs.size();
ASSERT( ccount == size_t(N), NULL );
size_t elem_cnt = 0;
for(typename tbb::flattened2d<ets_type>::const_iterator i = fvs.begin(); i != fvs.end(); ++i) {
++elem_cnt;
};
ASSERT( ccount == elem_cnt, NULL );
elem_cnt = 0;
for(typename tbb::flattened2d<ets_type>::iterator i = fvs.begin(); i != fvs.end(); ++i) {
++elem_cnt;
};
ASSERT( ccount == elem_cnt, NULL );
#if __TBB_ETS_USE_CPP11
// Test the ETS constructor with multiple args
T minus_one;
test_helper<T>::set(minus_one, -1);
// Set ETS to construct "local" vectors pre-occupied with 25 "minus_one"s
// Cast 25 to size_type to prevent Intel Compiler SFINAE compilation issues with gcc 5.
ets_type vvs( typename container_type::size_type(25), minus_one, tbb::tbb_allocator<T>() );
ASSERT( vvs.empty(), NULL );
tbb::parallel_for ( tbb::blocked_range<int> (0, N, RANGE_MIN), parallel_vector_for_body<T,Allocator>( vvs, allocator_name ) );
ASSERT( !vvs.empty(), NULL );
parallel_vector_reduce_body< typename ets_type::const_range_type, T > pvrb2;
tbb::parallel_reduce ( vvs.range(1), pvrb2 );
ASSERT( pvrb2.count == vvs.size(), NULL );
ASSERT( test_helper<T>::get(pvrb2.sum) == N-pvrb2.count*25, NULL );
tbb::flattened2d<ets_type> fvvs = flatten2d(vvs);
ccount = fvvs.size();
ASSERT( ccount == N+pvrb2.count*25, NULL );
#endif
}
double result_value = test_helper<T>::get(sum);
ASSERT( EXPECTED_SUM == result_value, NULL);
REMARK("done\nparallel %s, %d, %g, %g\n", test_name, p, result_value, ( tbb::tick_count::now() - t0).seconds());
}
}
template<typename T, template<class> class Allocator>
void run_cross_type_vector_tests(const char *test_name) {
tbb::tick_count t0;
const char* allocator_name = "default";
typedef std::vector<T, tbb::tbb_allocator<T> > container_type;
for (int p = MinThread; p <= MaxThread; ++p) {
REMARK("Testing parallel %s on %d thread(s)... ", test_name, p);
tbb::task_scheduler_init init(p);
T sum;
test_helper<T>::init(sum);
for (int t = -1; t < REPETITIONS; ++t) {
if (Verbose && t == 0) t0 = tbb::tick_count::now();
typedef typename tbb::enumerable_thread_specific< container_type, Allocator<container_type>, tbb::ets_no_key > ets_nokey_type;
typedef typename tbb::enumerable_thread_specific< container_type, Allocator<container_type>, tbb::ets_key_per_instance > ets_tlskey_type;
ets_nokey_type vs;
ASSERT( vs.empty(), NULL);
tbb::parallel_for ( tbb::blocked_range<int> (0, N, RANGE_MIN), parallel_vector_for_body<T, Allocator>( vs, allocator_name ) );
ASSERT( !vs.empty(), NULL);
// copy construct
ets_tlskey_type vs2(vs);
// assign
ets_nokey_type vs3;
vs3 = vs2;
parallel_vector_reduce_body< typename ets_nokey_type::const_range_type, T > pvrb;
tbb::parallel_reduce ( vs3.range(1), pvrb );
test_helper<T>::sum(sum, pvrb.sum);
ASSERT( vs3.size() == pvrb.count, NULL);
tbb::flattened2d<ets_nokey_type> fvs = flatten2d(vs3);
size_t ccount = fvs.size();
size_t elem_cnt = 0;
for(typename tbb::flattened2d<ets_nokey_type>::const_iterator i = fvs.begin(); i != fvs.end(); ++i) {
++elem_cnt;
};
ASSERT(ccount == elem_cnt, NULL);
elem_cnt = 0;
for(typename tbb::flattened2d<ets_nokey_type>::iterator i = fvs.begin(); i != fvs.end(); ++i) {
++elem_cnt;
};
ASSERT(ccount == elem_cnt, NULL);
}
double result_value = test_helper<T>::get(sum);
ASSERT( EXPECTED_SUM == result_value, NULL);
REMARK("done\nparallel %s, %d, %g, %g\n", test_name, p, result_value, ( tbb::tick_count::now() - t0).seconds());
}
}
template< typename T >
void run_serial_vector_tests(const char *test_name) {
tbb::tick_count t0;
T sum;
test_helper<T>::init(sum);
T one;
test_helper<T>::set(one, 1);
REMARK("Testing serial %s... ", test_name);
for (int t = -1; t < REPETITIONS; ++t) {
if (Verbose && t == 0) t0 = tbb::tick_count::now();
std::vector<T, tbb::tbb_allocator<T> > v;
for (int i = 0; i < N; ++i) {
v.push_back( one );
}
for (typename std::vector<T, tbb::tbb_allocator<T> >::const_iterator i = v.begin(); i != v.end(); ++i)
test_helper<T>::sum(sum, *i);
}
double result_value = test_helper<T>::get(sum);
ASSERT( EXPECTED_SUM == result_value, NULL);
REMARK("done\nserial %s, 0, %g, %g\n", test_name, result_value, ( tbb::tick_count::now() - t0).seconds());
}
const size_t line_size = tbb::internal::NFS_MaxLineSize;
void run_serial_tests() {
run_serial_scalar_tests<int>("int");
run_serial_scalar_tests<double>("double");
run_serial_scalar_tests<minimal<> >("minimal<>");
run_serial_vector_tests<int>("std::vector<int, tbb::tbb_allocator<int> >");
run_serial_vector_tests<double>("std::vector<double, tbb::tbb_allocator<double> >");
}
template<template<class>class Allocator>
void run_parallel_tests(const char *allocator_name) {
run_parallel_scalar_tests<int, Allocator>("int",allocator_name);
run_parallel_scalar_tests<double, Allocator>("double",allocator_name);
run_parallel_scalar_tests_nocombine<minimal<>,Allocator>("minimal<>",allocator_name);
run_parallel_scalar_tests<ThrowingConstructor, Allocator>("ThrowingConstructor", allocator_name);
run_parallel_vector_tests<int, Allocator>("std::vector<int, tbb::tbb_allocator<int> >",allocator_name);
run_parallel_vector_tests<double, Allocator>("std::vector<double, tbb::tbb_allocator<double> >",allocator_name);
}
void run_cross_type_tests() {
// cross-type scalar tests are part of run_parallel_scalar_tests_nocombine
run_cross_type_vector_tests<int, tbb::tbb_allocator>("std::vector<int, tbb::tbb_allocator<int> >");
run_cross_type_vector_tests<double, tbb::tbb_allocator>("std::vector<double, tbb::tbb_allocator<double> >");
}
typedef tbb::enumerable_thread_specific<minimal<line_size> > flogged_ets;
class set_body {
flogged_ets *a;
public:
set_body( flogged_ets*_a ) : a(_a) { }
void operator() ( ) const {
for (int i = 0; i < VALID_NUMBER_OF_KEYS; ++i) {
check_alignment(a[i].local(), "default").set_value(i + 1);
}
}
};
void do_tbb_threads( int max_threads, flogged_ets a[] ) {
std::vector< tbb::tbb_thread * > threads;
for (int p = 0; p < max_threads; ++p) {
threads.push_back( new tbb::tbb_thread ( set_body( a ) ) );
}
for (int p = 0; p < max_threads; ++p) {
threads[p]->join();
}
for(int p = 0; p < max_threads; ++p) {
delete threads[p];
}
}
void flog_key_creation_and_deletion() {
const int FLOG_REPETITIONS = 100;
for (int p = MinThread; p <= MaxThread; ++p) {
REMARK("Testing repeated deletes on %d threads... ", p);
for (int j = 0; j < FLOG_REPETITIONS; ++j) {
construction_counter = 0;
destruction_counter = 0;
// causes VALID_NUMBER_OF_KEYS exemplar instances to be constructed
flogged_ets* a = new flogged_ets[VALID_NUMBER_OF_KEYS];
ASSERT(int(construction_counter) == 0, NULL); // no exemplars or actual locals have been constructed
ASSERT(int(destruction_counter) == 0, NULL); // and none have been destroyed
// causes p * VALID_NUMBER_OF_KEYS minimals to be created
do_tbb_threads(p, a);
for (int i = 0; i < VALID_NUMBER_OF_KEYS; ++i) {
int pcnt = 0;
for ( flogged_ets::iterator tli = a[i].begin(); tli != a[i].end(); ++tli ) {
ASSERT( (*tli).value() == i+1, NULL );
++pcnt;
}
ASSERT( pcnt == p, NULL); // should be one local per thread.
}
delete[] a;
}
ASSERT( int(construction_counter) == (p)*VALID_NUMBER_OF_KEYS, NULL );
ASSERT( int(destruction_counter) == (p)*VALID_NUMBER_OF_KEYS, NULL );
REMARK("done\nTesting repeated clears on %d threads... ", p);
construction_counter = 0;
destruction_counter = 0;
// causes VALID_NUMBER_OF_KEYS exemplar instances to be constructed
flogged_ets* a = new flogged_ets[VALID_NUMBER_OF_KEYS];
for (int j = 0; j < FLOG_REPETITIONS; ++j) {
// causes p * VALID_NUMBER_OF_KEYS minimals to be created
do_tbb_threads(p, a);
for (int i = 0; i < VALID_NUMBER_OF_KEYS; ++i) {
for ( flogged_ets::iterator tli = a[i].begin(); tli != a[i].end(); ++tli ) {
ASSERT( (*tli).value() == i+1, NULL );
}
a[i].clear();
ASSERT( static_cast<int>(a[i].end() - a[i].begin()) == 0, NULL );
}
}
delete[] a;
ASSERT( int(construction_counter) == (FLOG_REPETITIONS*p)*VALID_NUMBER_OF_KEYS, NULL );
ASSERT( int(destruction_counter) == (FLOG_REPETITIONS*p)*VALID_NUMBER_OF_KEYS, NULL );
REMARK("done\n");
}
}
template <typename inner_container>
void flog_segmented_interator() {
bool found_error = false;
typedef typename inner_container::value_type T;
typedef std::vector< inner_container > nested_vec;
inner_container my_inner_container;
my_inner_container.clear();
nested_vec my_vec;
// simple nested vector (neither level empty)
const int maxval = 10;
for(int i=0; i < maxval; i++) {
my_vec.push_back(my_inner_container);
for(int j = 0; j < maxval; j++) {
my_vec.at(i).push_back((T)(maxval * i + j));
}
}
tbb::internal::segmented_iterator<nested_vec, T> my_si(my_vec);
T ii;
for(my_si=my_vec.begin(), ii=0; my_si != my_vec.end(); ++my_si, ++ii) {
if((*my_si) != ii) {
found_error = true;
REMARK( "*my_si=%d\n", int(*my_si));
}
}
// outer level empty
my_vec.clear();
for(my_si=my_vec.begin(); my_si != my_vec.end(); ++my_si) {
found_error = true;
}
// inner levels empty
my_vec.clear();
for(int i =0; i < maxval; ++i) {
my_vec.push_back(my_inner_container);
}
for(my_si = my_vec.begin(); my_si != my_vec.end(); ++my_si) {
found_error = true;
}
// every other inner container is empty
my_vec.clear();
for(int i=0; i < maxval; ++i) {
my_vec.push_back(my_inner_container);
if(i%2) {
for(int j = 0; j < maxval; ++j) {
my_vec.at(i).push_back((T)(maxval * (i/2) + j));
}
}
}
for(my_si = my_vec.begin(), ii=0; my_si != my_vec.end(); ++my_si, ++ii) {
if((*my_si) != ii) {
found_error = true;
REMARK("*my_si=%d, ii=%d\n", (int)(*my_si), (int)ii);
}
}
tbb::internal::segmented_iterator<nested_vec, const T> my_csi(my_vec);
for(my_csi=my_vec.begin(), ii=0; my_csi != my_vec.end(); ++my_csi, ++ii) {
if((*my_csi) != ii) {
found_error = true;
REMARK( "*my_csi=%d\n", int(*my_csi));
}
}
// outer level empty
my_vec.clear();
for(my_csi=my_vec.begin(); my_csi != my_vec.end(); ++my_csi) {
found_error = true;
}
// inner levels empty
my_vec.clear();
for(int i =0; i < maxval; ++i) {
my_vec.push_back(my_inner_container);
}
for(my_csi = my_vec.begin(); my_csi != my_vec.end(); ++my_csi) {
found_error = true;
}
// every other inner container is empty
my_vec.clear();
for(int i=0; i < maxval; ++i) {
my_vec.push_back(my_inner_container);
if(i%2) {