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
|
/*
Copyright (c) 2025 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.
*/
//! \file test_parallel_phase.cpp
//! \brief Test for [preview] functionality
#define TBB_PREVIEW_PARALLEL_PHASE 1
#include <chrono>
#include "common/test.h"
#include "common/utils.h"
#include "common/utils_concurrency_limit.h"
#include "common/spin_barrier.h"
#include "tbb/task_arena.h"
void active_wait_for(std::chrono::microseconds duration) {
for (auto t1 = std::chrono::steady_clock::now(), t2 = t1;
std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1) < duration;
t2 = std::chrono::steady_clock::now())
{
utils::doDummyWork(100);
}
}
struct dummy_func {
void operator()() const {
}
};
template <typename F1 = dummy_func, typename F2 = dummy_func>
std::size_t measure_median_start_time(tbb::task_arena* ta, const F1& start = F1{}, const F2& end = F2{}) {
std::size_t num_threads = ta ? ta->max_concurrency() : tbb::this_task_arena::max_concurrency();
// TODO: propagate worker threads blocking time to the test
std::size_t max_wait = 1000;
std::size_t num_runs = 100;
std::vector<std::size_t> longest_start_times;
longest_start_times.reserve(num_runs);
std::vector<std::chrono::steady_clock::time_point> start_times(num_threads);
utils::SpinBarrier barrier(num_threads);
auto measure_start_time = [&] {
start_times[tbb::this_task_arena::current_thread_index()] = std::chrono::steady_clock::now();
barrier.wait();
};
auto get_longest_start = [&] (std::chrono::steady_clock::time_point start_time) {
std::size_t longest_time = 0;
for (auto& time : start_times) {
auto diff = std::chrono::duration_cast<std::chrono::microseconds>(time - start_time);
longest_time = std::max(longest_time, std::size_t(diff.count()));
}
return longest_time;
};
auto work = [&] {
auto start_time = std::chrono::steady_clock::now();
start();
for(std::size_t thr = 0; thr < num_threads-1; ++thr) {
tbb::this_task_arena::enqueue(measure_start_time);
}
start_times[tbb::this_task_arena::current_thread_index()] = std::chrono::steady_clock::now();
barrier.wait();
end();
longest_start_times.push_back(get_longest_start(start_time));
};
std::size_t step = max_wait / num_runs;
for (std::size_t i = 0; i <= max_wait; i += step) {
if (ta) {
ta->execute(work);
} else {
work();
}
active_wait_for(std::chrono::microseconds(i));
}
return utils::median(longest_start_times.begin(), longest_start_times.end());
}
template <typename Impl>
class start_time_collection_base {
friend Impl;
public:
start_time_collection_base(tbb::task_arena& ta, std::size_t ntrials) :
arena(&ta), num_trials(ntrials), start_times(ntrials) {}
explicit start_time_collection_base(std::size_t ntrials) :
arena(nullptr), num_trials(ntrials), start_times(ntrials) {}
std::vector<std::size_t> measure() {
for (std::size_t i = 0; i < num_trials; ++i) {
std::size_t median_start_time = static_cast<Impl*>(this)->measure_impl();
start_times[i] = median_start_time;
}
return start_times;
}
protected:
tbb::task_arena* arena;
std::size_t num_trials;
std::vector<std::size_t> start_times;
};
class start_time_collection : public start_time_collection_base<start_time_collection> {
using base = start_time_collection_base<start_time_collection>;
using base::base;
friend base;
std::size_t measure_impl() {
return measure_median_start_time(arena);
}
};
class start_time_collection_phase_wrapped
: public start_time_collection_base<start_time_collection_phase_wrapped>
{
using base = start_time_collection_base<start_time_collection_phase_wrapped>;
using base::base;
friend base;
std::size_t measure_impl() {
arena->start_parallel_phase();
auto median_start_time = measure_median_start_time(arena);
arena->end_parallel_phase(/*with_fast_leave*/true);
return median_start_time;
}
};
class start_time_collection_scoped_phase_wrapped
: public start_time_collection_base<start_time_collection_scoped_phase_wrapped>
{
using base = start_time_collection_base<start_time_collection_scoped_phase_wrapped>;
using base::base;
friend base;
std::size_t measure_impl() {
tbb::task_arena::scoped_parallel_phase phase{*arena};
auto median_start_time = measure_median_start_time(arena);
return median_start_time;
}
};
class start_time_collection_sequenced_phases
: public start_time_collection_base<start_time_collection_sequenced_phases>
{
using base = start_time_collection_base<start_time_collection_sequenced_phases>;
friend base;
bool with_fast_leave;
std::size_t measure_impl() {
std::size_t median_start_time;
utils::SpinBarrier barrier;
auto body = [&] {
barrier.wait();
};
if (arena) {
barrier.initialize(arena->max_concurrency());
median_start_time = measure_median_start_time(arena,
[&] {
std::size_t num_threads = arena->max_concurrency();
arena->start_parallel_phase();
arena->execute([&] {
for(std::size_t thr = 0; thr < num_threads-1; ++thr) {
tbb::this_task_arena::enqueue(body);
}
barrier.wait();
});
arena->end_parallel_phase(with_fast_leave);
}
);
} else {
barrier.initialize(tbb::this_task_arena::max_concurrency());
median_start_time = measure_median_start_time(arena,
[&] {
std::size_t num_threads = tbb::this_task_arena::max_concurrency();
tbb::this_task_arena::start_parallel_phase();
for(std::size_t thr = 0; thr < num_threads-1; ++thr) {
tbb::this_task_arena::enqueue(body);
}
barrier.wait();
tbb::this_task_arena::end_parallel_phase(with_fast_leave);
}
);
}
return median_start_time;
}
public:
start_time_collection_sequenced_phases(tbb::task_arena& ta, std::size_t ntrials, bool fast_leave = false) :
base(ta, ntrials), with_fast_leave(fast_leave)
{}
explicit start_time_collection_sequenced_phases(std::size_t ntrials, bool fast_leave = false) :
base(ntrials), with_fast_leave(fast_leave)
{}
};
class start_time_collection_sequenced_scoped_phases
: public start_time_collection_base<start_time_collection_sequenced_scoped_phases>
{
using base = start_time_collection_base<start_time_collection_sequenced_scoped_phases>;
friend base;
bool with_fast_leave;
std::size_t measure_impl() {
utils::SpinBarrier barrier{static_cast<std::size_t>(arena->max_concurrency())};
auto body = [&] {
barrier.wait();
};
auto median_start_time = measure_median_start_time(arena,
[&] {
std::size_t num_threads = arena->max_concurrency();
{
tbb::task_arena::scoped_parallel_phase phase{*arena, with_fast_leave};
arena->execute([&] {
for(std::size_t thr = 0; thr < num_threads-1; ++thr) {
tbb::this_task_arena::enqueue(body);
}
barrier.wait();
});
}
}
);
return median_start_time;
}
public:
start_time_collection_sequenced_scoped_phases(tbb::task_arena& ta, std::size_t ntrials, bool fast_leave = false) :
base(ta, ntrials), with_fast_leave(fast_leave)
{}
explicit start_time_collection_sequenced_scoped_phases(std::size_t ntrials, bool fast_leave = false) :
base(ntrials), with_fast_leave(fast_leave)
{}
};
//! \brief \ref interface \ref requirement
TEST_CASE("Check that workers leave faster with leave_policy::fast") {
// Test measures workers start time, so no there is no point to
// measure it with workerless arena
if (utils::get_platform_max_threads() < 2) {
return;
}
tbb::task_arena ta_automatic_leave {
tbb::task_arena::automatic, 1,
tbb::task_arena::priority::normal,
tbb::task_arena::leave_policy::automatic
};
tbb::task_arena ta_fast_leave {
tbb::task_arena::automatic, 1,
tbb::task_arena::priority::normal,
tbb::task_arena::leave_policy::fast
};
start_time_collection st_collector1{ta_automatic_leave, /*num_trials=*/5};
start_time_collection st_collector2{ta_fast_leave, /*num_trials=*/5};
auto times_automatic = st_collector1.measure();
auto times_fast = st_collector2.measure();
auto median_automatic = utils::median(times_automatic.begin(), times_automatic.end());
auto median_fast = utils::median(times_fast.begin(), times_fast.end());
WARN_MESSAGE(median_automatic < median_fast,
"Expected workers to start new work slower with fast leave policy");
}
//! \brief \ref interface \ref requirement
TEST_CASE("Parallel Phase retains workers in task_arena") {
if (utils::get_platform_max_threads() < 2) {
return;
}
tbb::task_arena ta_fast1 {
tbb::task_arena::automatic, 1,
tbb::task_arena::priority::normal,
tbb::task_arena::leave_policy::fast
};
tbb::task_arena ta_fast2 {
tbb::task_arena::automatic, 1,
tbb::task_arena::priority::normal,
tbb::task_arena::leave_policy::fast
};
start_time_collection_phase_wrapped st_collector1{ta_fast1, /*num_trials=*/5};
start_time_collection_scoped_phase_wrapped st_collector_scoped{ta_fast1, /*num_trials=*/5};
start_time_collection st_collector2{ta_fast2, /*num_trials=*/5};
auto times1 = st_collector1.measure();
auto times2 = st_collector2.measure();
auto times_scoped = st_collector_scoped.measure();
auto median1 = utils::median(times1.begin(), times1.end());
auto median2 = utils::median(times2.begin(), times2.end());
auto median_scoped = utils::median(times_scoped.begin(), times_scoped.end());
WARN_MESSAGE(median1 < median2,
"Expected workers start new work faster when using parallel_phase");
WARN_MESSAGE(median_scoped < median2,
"Expected workers start new work faster when using scoped parallel_phase");
}
//! \brief \ref interface \ref requirement
TEST_CASE("Test one-time fast leave") {
if (utils::get_platform_max_threads() < 2) {
return;
}
tbb::task_arena ta1{};
tbb::task_arena ta2{};
start_time_collection_sequenced_phases st_collector1{ta1, /*num_trials=*/10};
start_time_collection_sequenced_phases st_collector2{ta2, /*num_trials=*/10, /*fast_leave*/true};
start_time_collection_sequenced_scoped_phases st_collector_scoped{ta2, /*num_trials=*/10, /*fast_leave*/true};
auto times1 = st_collector1.measure();
auto times2 = st_collector2.measure();
auto times_scoped = st_collector_scoped.measure();
auto median1 = utils::median(times1.begin(), times1.end());
auto median2 = utils::median(times2.begin(), times2.end());
auto median_scoped = utils::median(times_scoped.begin(), times_scoped.end());
WARN_MESSAGE(median1 < median2,
"Expected one-time fast leave setting to slow workers to start new work");
WARN_MESSAGE(median1 < median_scoped,
"Expected one-time fast leave setting to slow workers to start new work");
}
//! \brief \ref interface \ref requirement
TEST_CASE("Test parallel phase with this_task_arena") {
if (utils::get_platform_max_threads() < 2) {
return;
}
start_time_collection_sequenced_phases st_collector1{/*num_trials=*/10};
start_time_collection_sequenced_phases st_collector2{/*num_trials=*/10, /*fast_leave*/true};
auto times1 = st_collector1.measure();
auto times2 = st_collector2.measure();
auto median1 = utils::median(times1.begin(), times1.end());
auto median2 = utils::median(times2.begin(), times2.end());
WARN_MESSAGE(median1 < median2,
"Expected one-time fast leave setting to slow workers to start new work");
}
|