File: conformance_global_control.cpp

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (433 lines) | stat: -rw-r--r-- 16,275 bytes parent folder | download | duplicates (4)
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
/*
    Copyright (c) 2005-2024 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.
*/
#include "common/test.h"
#include "common/concurrency_tracker.h"
#include "common/spin_barrier.h"
#include "common/utils.h"
#include "common/utils_concurrency_limit.h"
#include "common/cpu_usertime.h"

#include "oneapi/tbb/global_control.h"
#include "oneapi/tbb/parallel_for.h"

#include <limits.h>
#include <thread>

//! \file conformance_global_control.cpp
//! \brief Test for [sched.global_control] specification

const std::size_t MB = 1024*1024;

void TestStackSizeSimpleControl() {
    oneapi::tbb::global_control s0(oneapi::tbb::global_control::thread_stack_size, 1*MB);

    {
        oneapi::tbb::global_control s1(oneapi::tbb::global_control::thread_stack_size, 8*MB);

        CHECK(8*MB == oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::thread_stack_size));
    }
    CHECK(1*MB == oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::thread_stack_size));
}

struct StackSizeRun : utils::NoAssign {

    int num_threads;
    utils::SpinBarrier *barr1, *barr2;

    StackSizeRun(int threads, utils::SpinBarrier *b1, utils::SpinBarrier *b2) :
        num_threads(threads), barr1(b1), barr2(b2) {}
    void operator()( int id ) const {
        oneapi::tbb::global_control s1(oneapi::tbb::global_control::thread_stack_size, (1+id)*MB);

        barr1->wait();

        REQUIRE(num_threads*MB == oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::thread_stack_size));
        barr2->wait();
    }
};

void TestStackSizeThreadsControl() {
    int threads = 4;
    utils::SpinBarrier barr1(threads), barr2(threads);
    utils::NativeParallelFor( threads, StackSizeRun(threads, &barr1, &barr2) );
}

void RunWorkersLimited(size_t parallelism)
{
    oneapi::tbb::global_control s(oneapi::tbb::global_control::max_allowed_parallelism, parallelism);
    // TODO: consider better testing approach
    // Sleep is required because after destruction global_control on the previous iteration,
    // it recalls the maximum concurrency and excessive worker threads might populate the arena.
    // So, we need to wait when arena becomes empty but it is unreliable and might sporadically fail.
    utils::Sleep(100);
    const std::size_t expected_threads = (utils::get_platform_max_threads()==1)? 1 : parallelism;
    utils::ExactConcurrencyLevel::check(expected_threads);
}

void TestWorkersConstraints()
{
    const size_t max_parallelism =
        oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism);
    if (max_parallelism > 3) {
        oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism, max_parallelism-1);
        CHECK_MESSAGE(max_parallelism-1 ==
               oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism),
               "Allowed parallelism must be decreasable.");
        oneapi::tbb::global_control c1(oneapi::tbb::global_control::max_allowed_parallelism, max_parallelism-2);
        CHECK_MESSAGE(max_parallelism-2 ==
               oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism),
               "Allowed parallelism must be decreasable.");
    }
    const size_t limit_par = utils::min(max_parallelism, 4U);
    // check that constrains are really met
    for (size_t num=2; num<limit_par; num++)
        RunWorkersLimited(num);
    for (size_t num=limit_par; num>1; num--)
        RunWorkersLimited(num);
}

struct SetUseRun: utils::NoAssign {
    utils::SpinBarrier &barr;

    SetUseRun(utils::SpinBarrier& b) : barr(b) {}
    void operator()( int id ) const {
        if (id == 0) {
            for (int i=0; i<10; i++) {
                oneapi::tbb::parallel_for(0, 1000, utils::DummyBody(10), oneapi::tbb::simple_partitioner());
                barr.wait();
            }
        } else {
            for (int i=0; i<10; i++) {
                oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism, 8);
                barr.wait();
            }
        }
    }
};

void TestConcurrentSetUseConcurrency()
{
    utils::SpinBarrier barr(2);
    NativeParallelFor( 2, SetUseRun(barr) );
}

// check number of workers after autoinitialization
void TestAutoInit()
{
    const size_t max_parallelism =
        oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism);
    const unsigned expected_threads = utils::get_platform_max_threads()==1?
        1 : (unsigned)max_parallelism;
    utils::ExactConcurrencyLevel::check(expected_threads);
    CHECK_MESSAGE(oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism)
           == max_parallelism, "max_allowed_parallelism must not be changed after auto init");
    if (max_parallelism > 2) {
        // after autoinit it's possible to decrease workers number
        oneapi::tbb::global_control s(oneapi::tbb::global_control::max_allowed_parallelism, max_parallelism-1);
        // TODO: consider better testing approach
        // Sleep is required because after previous concurrency check, the arena is still populated with workers.
        // So, we need to wait when arena becomes empty but it is unreliable and might sporadically fail.
        utils::Sleep(100);
        utils::ExactConcurrencyLevel::check(max_parallelism-1);
    }
}

class TestMultipleControlsRun {
    utils::SpinBarrier &barrier;
public:
    TestMultipleControlsRun(utils::SpinBarrier& b) : barrier(b) {}
    void operator()( int id ) const {
        barrier.wait();
        if (id) {
            {
                oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism, 1);
                utils::ExactConcurrencyLevel::check(1);
                barrier.wait();
            }
            utils::ExactConcurrencyLevel::check(1);
            barrier.wait();
            {
                oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism, 2);
                utils::ExactConcurrencyLevel::check(1);
                barrier.wait();
                utils::ExactConcurrencyLevel::check(2);
                barrier.wait();
            }
        } else {
            {
                utils::ExactConcurrencyLevel::check(1);
                oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism, 1);
                barrier.wait();
                utils::ExactConcurrencyLevel::check(1);
                barrier.wait();
                utils::ExactConcurrencyLevel::check(1);
                barrier.wait();
            }
            utils::ExactConcurrencyLevel::check(2);
            barrier.wait();
        }
    }
};

// test that global controls from different thread with overlapping lifetime
// still keep parallelism under control
void TestMultipleControls()
{
    utils::SpinBarrier barrier(2);
    utils::NativeParallelFor( 2, TestMultipleControlsRun(barrier) );
}

#if !(__TBB_WIN8UI_SUPPORT && (_WIN32_WINNT < 0x0A00))
//! Testing setting stack size
//! \brief \ref interface \ref requirement
TEST_CASE("setting stack size") {
    std::size_t default_ss = oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::thread_stack_size);
    CHECK(default_ss > 0);
    TestStackSizeSimpleControl();
    TestStackSizeThreadsControl();
    CHECK(default_ss == oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::thread_stack_size));
}
#endif

//! Testing setting max number of threads
//! \brief \ref interface \ref requirement
TEST_CASE("setting max number of threads") {
    TestWorkersConstraints();
}
//! Testing concurrenct setting concurrency
//! \brief \ref interface \ref requirement
TEST_CASE("concurrenct setting concurrency") {
    TestConcurrentSetUseConcurrency();
}

//! Testing auto initialization
//! \brief \ref interface \ref requirement
TEST_CASE("auto initialization") {
    TestAutoInit();
}

//! Test terminate_on_exception default value
//! \brief \ref interface \ref requirement
TEST_CASE("terminate_on_exception: default") {
    std::size_t default_toe = oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::terminate_on_exception);
    CHECK(default_toe == 0);
}

//! Test terminate_on_exception in a nested case
//! \brief \ref interface \ref requirement
TEST_CASE("terminate_on_exception: nested") {
    oneapi::tbb::global_control* c0;
    {
        oneapi::tbb::global_control c1(oneapi::tbb::global_control::terminate_on_exception, 1);
        CHECK(oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::terminate_on_exception) == 1);
        {
            oneapi::tbb::global_control c2(oneapi::tbb::global_control::terminate_on_exception, 0);
            CHECK(oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::terminate_on_exception) == 1);
        }
        CHECK(oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::terminate_on_exception) == 1);
        c0 = new oneapi::tbb::global_control(oneapi::tbb::global_control::terminate_on_exception, 0);
    }
    CHECK(oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::terminate_on_exception) == 0);
    delete c0;
}

//! Testing setting the same value but different objects
//! \brief \ref interface \ref error_guessing
TEST_CASE("setting same value") {
    const std::size_t value = 2;

    oneapi::tbb::global_control* ctl1 = new oneapi::tbb::global_control(oneapi::tbb::global_control::max_allowed_parallelism, value);
    oneapi::tbb::global_control* ctl2 = new oneapi::tbb::global_control(oneapi::tbb::global_control::max_allowed_parallelism, value);

    std::size_t active = oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism);
    REQUIRE(active == value);
    delete ctl2;

    active = oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism);
    REQUIRE_MESSAGE(active == value, "Active value should not change, because of value duplication");
    delete ctl1;
}

//! Testing lifetime control conformance
//! \brief \ref interface \ref requirement
TEST_CASE("prolong lifetime simple") {
    tbb::task_scheduler_handle hdl1{ tbb::attach{} };
    {
        tbb::parallel_for(0, 10, utils::DummyBody());

        tbb::task_scheduler_handle hdl2;
        hdl2 = tbb::task_scheduler_handle{ tbb::attach{} };
        hdl2.release();
    }
    bool ok = tbb::finalize(hdl1, std::nothrow);
    REQUIRE(ok);
}

//! Testing handle check for emptiness
//! \brief \ref interface \ref requirement
TEST_CASE("null handle check") {
    tbb::task_scheduler_handle hndl;
    REQUIRE_FALSE(hndl);
}

//! Testing handle check for emptiness
//! \brief \ref interface \ref requirement
TEST_CASE("null handle check 2") {
    tbb::task_scheduler_handle hndl{ tbb::attach{} };
    bool not_empty = (bool)hndl;

    tbb::finalize(hndl, std::nothrow);

    REQUIRE(not_empty);
    REQUIRE_FALSE(hndl);
}

//! Testing handle check for emptiness
//! \brief \ref interface \ref requirement
TEST_CASE("null handle check 3") {
    tbb::task_scheduler_handle handle1{ tbb::attach{} };
    tbb::task_scheduler_handle handle2(std::move(handle1));

    bool handle1_empty = !handle1;
    bool handle2_not_empty = (bool)handle2;

    tbb::finalize(handle2, std::nothrow);

    REQUIRE(handle1_empty);
    REQUIRE(handle2_not_empty);
}

//! Testing  task_scheduler_handle is created on one thread and destroyed on another.
//! \brief \ref interface \ref requirement
TEST_CASE("cross thread 1") {
    // created task_scheduler_handle, parallel_for on another thread - finalize
    tbb::task_scheduler_handle handle{ tbb::attach{} };
    utils::NativeParallelFor(1, [&](int) {
        tbb::parallel_for(0, 10, utils::DummyBody());
        bool res = tbb::finalize(handle, std::nothrow);
        REQUIRE(res);
    });
}

//! Testing  task_scheduler_handle is created on one thread and destroyed on another.
//! \brief \ref interface \ref requirement
TEST_CASE("cross thread 2") {
    // created task_scheduler_handle, called parallel_for on this thread, killed the thread - and finalize on another thread
    tbb::task_scheduler_handle handle;
    utils::NativeParallelFor(1, [&](int) {
        handle = tbb::task_scheduler_handle{ tbb::attach{} };
        tbb::parallel_for(0, 10, utils::DummyBody());
    });
    bool res = tbb::finalize(handle, std::nothrow);
    REQUIRE(res);
}

//! Testing multiple wait
//! \brief \ref interface \ref requirement
TEST_CASE("simple prolong lifetime 3") {
    // Parallel region
    tbb::parallel_for(0, 10, utils::DummyBody());
    // Termination
    tbb::task_scheduler_handle handle = tbb::task_scheduler_handle{ tbb::attach{} };
    bool res = tbb::finalize(handle, std::nothrow);
    REQUIRE(res);
    // New parallel region
    tbb::parallel_for(0, 10, utils::DummyBody());
}

//! \brief \ref regression \ref interface \ref requirement
TEST_CASE("Test worker threads remain inactive in enforced serial execution mode") {
    auto num_threads = utils::get_platform_max_threads();
    utils::SpinBarrier barrier{num_threads};

    // Warm-up threads
    tbb::parallel_for(std::size_t(0), num_threads, [&] (std::size_t) {
        barrier.wait();
    });

    tbb::global_control control(tbb::global_control::max_allowed_parallelism, 1);

    std::thread thr([&] {
        tbb::parallel_for(0, 100000, [&] (int) {
            utils::doDummyWork(100);
        });
    });

    // Workers should sleep because of global_control enforced serial execution of tasks
    TestCPUUserTime(utils::get_platform_max_threads() - 1);

    thr.join();
}

// The test cannot work correctly with statically linked runtime.
// TODO: investigate a failure in debug with MSVC
#if (!_MSC_VER || (defined(_DLL) && !defined(_DEBUG))) && !EMSCRIPTEN
#include <csetjmp>

// Overall, the test case is not safe because the dtors might not be called during long jump.
// Therefore, it makes sense to run the test case after all other test cases.
//! Test terminate_on_exception behavior
//! \brief \ref interface \ref requirement
TEST_CASE("terminate_on_exception: enabled") {
    oneapi::tbb::global_control c(oneapi::tbb::global_control::terminate_on_exception, 1);
    static bool terminate_handler_called;
    terminate_handler_called = false;

#if TBB_USE_EXCEPTIONS
    try {
#endif
        static std::jmp_buf buffer;
        std::terminate_handler prev = std::set_terminate([] {
            CHECK(!terminate_handler_called);
            terminate_handler_called = true;
            std::longjmp(buffer, 1);
        });
#if _MSC_VER
#pragma warning(push)
#pragma warning(disable:4611) // interaction between '_setjmp' and C++ object destruction is non - portable
#endif
        SUBCASE("internal exception") {
            if (setjmp(buffer) == 0) {
                oneapi::tbb::parallel_for(0, 1, -1, [](int) {});
                FAIL("Unreachable code");
            }
        }
#if TBB_USE_EXCEPTIONS
        SUBCASE("user exception") {
            if (setjmp(buffer) == 0) {
                oneapi::tbb::parallel_for(0, 1, [](int) {
                    volatile bool suppress_unreachable_code_warning = true;
                    if (suppress_unreachable_code_warning) {
                        throw std::exception();
                    }
                });
                FAIL("Unreachable code");
            }
        }
#endif
#if _MSC_VER
#pragma warning(pop)
#endif
        std::set_terminate(prev);
        terminate_handler_called = true;
#if TBB_USE_EXCEPTIONS
    } catch (...) {
        FAIL("The exception is not expected");
    }
#endif
    CHECK(terminate_handler_called);
}
#endif