1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #include "common/parallel_for_each_common.h"
18 
19 //! \file test_parallel_for_each.cpp
20 //! \brief Test for [algorithms.parallel_for_each]
21 
22 //! Test forward access iterator support
23 //! \brief \ref error_guessing \ref interface
24 TEST_CASE("Forward iterator support") {
25     for ( auto concurrency_level : utils::concurrency_range() ) {
26         tbb::global_control control(tbb::global_control::max_allowed_parallelism, concurrency_level);
27         for(size_t depth = 0; depth <= depths_nubmer; ++depth) {
28             g_tasks_expected = 0;
29             for (size_t i=0; i < depth; ++i)
30                 g_tasks_expected += FindNumOfTasks(g_depths[i].value());
31             TestIterator_Modifiable<utils::ForwardIterator<value_t>>(depth);
32         }
33     }
34 }
35 
36 //! Test random access iterator support
37 //! \brief \ref error_guessing \ref interface
38 TEST_CASE("Random access iterator support") {
39     for ( auto concurrency_level : utils::concurrency_range() ) {
40         tbb::global_control control(tbb::global_control::max_allowed_parallelism, concurrency_level);
41         for(size_t depth = 0; depth <= depths_nubmer; ++depth) {
42             g_tasks_expected = 0;
43             for (size_t i=0; i < depth; ++i)
44                 g_tasks_expected += FindNumOfTasks(g_depths[i].value());
45             TestIterator_Modifiable<value_t*>(depth);
46         }
47     }
48 }
49 
50 //! Test const random access iterator support
51 //! \brief \ref error_guessing \ref interface
52 TEST_CASE("Const random access iterator support") {
53     for ( auto concurrency_level : utils::concurrency_range() ) {
54         tbb::global_control control(tbb::global_control::max_allowed_parallelism, concurrency_level);
55         for(size_t depth = 0; depth <= depths_nubmer; ++depth) {
56             g_tasks_expected = 0;
57             for (size_t i=0; i < depth; ++i)
58                 g_tasks_expected += FindNumOfTasks(g_depths[i].value());
59             TestIterator_Const<utils::ConstRandomIterator<value_t>>(depth);
60         }
61     }
62 
63 }
64 
65 //! Test container based overload
66 //! \brief \ref error_guessing \ref interface
67 TEST_CASE("Container based overload - forward iterator based container") {
68     container_based_overload_test_case<utils::ForwardIterator>(/*expected_value*/1);
69 }
70 
71 //! Test container based overload
72 //! \brief \ref error_guessing \ref interface
73 TEST_CASE("Container based overload - random access iterator based container") {
74     container_based_overload_test_case<utils::RandomIterator>(/*expected_value*/1);
75 }
76 
77 // Test for iterators over values convertible to work item type
78 //! \brief \ref error_guessing \ref interface
79 TEST_CASE("Using with values convertible to work item type") {
80     for ( auto concurrency_level : utils::concurrency_range() ) {
81         tbb::global_control control(tbb::global_control::max_allowed_parallelism, concurrency_level);
82         using Iterator = size_t*;
83         for(size_t depth = 0; depth <= depths_nubmer; ++depth) {
84             g_tasks_expected = 0;
85             for (size_t i=0; i < depth; ++i)
86                 g_tasks_expected += FindNumOfTasks(g_depths[i].value());
87             // Test for iterators over values convertible to work item type
88             TestIterator_Common<Iterator>(depth);
89             TestBody<FakeTaskGeneratorBody_RvalueRefVersion, Iterator>(depth);
90             TestBody<TaskGeneratorBody_RvalueRefVersion, Iterator>(depth);
91         }
92     }
93 }
94 
95 //! Testing workers going to sleep
96 //! \brief \ref resource_usage \ref stress
97 TEST_CASE("That all workers sleep when no work") {
98     const std::size_t N = 100000;
99     std::vector<std::size_t> vec(N, 0);
100 
101     tbb::parallel_for_each(vec.begin(), vec.end(), [&](std::size_t& in) {
102         for (volatile int i = 0; i < 1000; ++i) {
103             ++in;
104         }
105     });
106     TestCPUUserTime(utils::get_platform_max_threads());
107 }
108