File: gtbbexecutor_internal_tests.cpp

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (178 lines) | stat: -rw-r--r-- 6,176 bytes parent folder | download
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
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2020 Intel Corporation

// Deliberately include .cpp file instead of header as we use non exported function (execute)
#include <executor/gtbbexecutor.cpp>

#ifdef HAVE_TBB
#include <tbb/tbb.h>
#include <tbb/task.h>
#if TBB_INTERFACE_VERSION < 12000

#include <tbb/task_arena.h>

#include "../test_precomp.hpp"
#include <thread>

namespace {
    tbb::task_arena create_task_arena(int max_concurrency  = tbb::task_arena::automatic /* set to 1 for single thread */) {
        unsigned int reserved_for_master_threads = 1;
        if (max_concurrency == 1) {
            // Leave no room for TBB worker threads, by reserving all to masters.
            // TBB runtime guarantees that no worker threads will join the arena
            // if max_concurrency is equal to reserved_for_master_threads
            // except 1:1 + use of enqueued tasks for safety guarantee.
            // So deliberately make it 2:2 to force TBB not to create extra thread.
            //
            // N.B. one slot will left empty as only one master thread(one that
            // calls root->wait_for_all()) will join the arena.

            // FIXME: strictly speaking master can take any free slot, not the first one.
            // However at the moment master seems to pick 0 slot all the time.
            max_concurrency = 2;
            reserved_for_master_threads = 2;
        }
        return tbb::task_arena{max_concurrency, reserved_for_master_threads};
    }
}

namespace opencv_test {

TEST(TBBExecutor, Basic) {
    using namespace cv::gimpl::parallel;
    bool executed = false;
    prio_items_queue_t q;
    tile_node n([&]() {
        executed = true;
    });
    q.push(&n);
    execute(q);
    EXPECT_TRUE(executed);
}

TEST(TBBExecutor, SerialExecution) {
    using namespace cv::gimpl::parallel;
    const int n = 10;
    prio_items_queue_t q;
    std::vector<tile_node> nodes; nodes.reserve(n+1);
    std::vector<std::thread::id> thread_id(n);
    for (int i=0; i <n; i++) {
        nodes.push_back(tile_node([&, i]() {
                thread_id[i] = std::this_thread::get_id();
                std::this_thread::sleep_for(std::chrono::milliseconds(10));

        }));
        q.push(&nodes.back());
    }

    auto serial_arena = create_task_arena(1);
    execute(q, serial_arena);
    auto print_thread_ids = [&] {
        std::stringstream str;
        for (auto& i : thread_id) { str << i <<" ";}
        return str.str();
    };
    EXPECT_NE(thread_id[0], std::thread::id{}) << print_thread_ids();
    EXPECT_EQ(thread_id.size(), static_cast<size_t>(std::count(thread_id.begin(), thread_id.end(), thread_id[0])))
        << print_thread_ids();
}

TEST(TBBExecutor, AsyncBasic) {
    using namespace cv::gimpl::parallel;

    std::atomic<bool> callback_ready {false};
    std::function<void()> callback;

    std::atomic<bool> callback_called   {false};
    std::atomic<bool> master_is_waiting {true};
    std::atomic<bool> master_was_blocked_until_callback_called {false};

    auto async_thread = std::thread([&] {
            bool slept = false;
            while (!callback_ready) {
                std::this_thread::sleep_for(std::chrono::milliseconds(1));
                slept = true;
            }
            if (!slept) {
                std::this_thread::sleep_for(std::chrono::milliseconds(1));
            }
            callback_called = true;
            master_was_blocked_until_callback_called = (master_is_waiting == true);
            callback();
    });

    auto async_task_body = [&](std::function<void()>&& cb, size_t /*total_order_index*/) {
        callback = std::move(cb);
        callback_ready = true;
    };
    tile_node n(async, std::move(async_task_body));

    prio_items_queue_t q;
    q.push(&n);
    execute(q);
    master_is_waiting = false;

    async_thread.join();

    EXPECT_TRUE(callback_called);
    EXPECT_TRUE(master_was_blocked_until_callback_called);
}

TEST(TBBExecutor, Dependencies) {
    using namespace cv::gimpl::parallel;
    const int n = 10;
    bool serial = true;
    std::atomic<int> counter {0};
    prio_items_queue_t q;
    std::vector<tile_node> nodes; nodes.reserve(n+1);
    const int invalid_order = -10;
    std::vector<int> tiles_exec_order(n, invalid_order);

    auto add_dependency_to = [](tile_node& node, tile_node& dependency) {
        dependency.dependants.push_back(&node);
        node.dependencies++;
        node.dependency_count.fetch_add(1);
    };
    for (int i=0; i <n; i++) {
        nodes.push_back(tile_node([&, i]() {
                tiles_exec_order[i] = counter++;
                if (!serial) {
                    //sleep gives a better chance for other threads to take part in the execution
                    std::this_thread::sleep_for(std::chrono::milliseconds(10));
                }
        }));
        if (i >0) {
            auto last_node = nodes.end() - 1;
            add_dependency_to(*last_node, *(last_node -1));
        }
    }

    q.push(&nodes.front());

    auto arena = serial ? create_task_arena(1) : create_task_arena();
    execute(q, arena);
    auto print_execution_order = [&] {
        std::stringstream str;
        for (auto& i : tiles_exec_order) { str << i <<" ";}
        return str.str();
    };
    ASSERT_EQ(0, std::count(tiles_exec_order.begin(), tiles_exec_order.end(), invalid_order))
        << "Not all " << n << " task executed ?\n"
        <<" execution order : " << print_execution_order();

    for (size_t i=0; i <nodes.size(); i++) {
        auto node_exec_order = tiles_exec_order[i];
        for (auto* dependee : nodes[i].dependants) {
            auto index = std::distance(&nodes.front(), dependee);
            auto dependee_execution_order = tiles_exec_order[index];
            ASSERT_LT(node_exec_order, dependee_execution_order) << "node number " << index << " is executed earlier than it's dependency " << i;
        }
    }
}
} // namespace opencv_test

#endif //TBB_INTERFACE_VERSION
#endif //HAVE_TBB