Skip to content

Commit

Permalink
Fix current_thread_index for masterless arena(1,0) (#1128)
Browse files Browse the repository at this point in the history
Signed-off-by: pavelkumbrasev <pavel.kumbrasev@intel.com>
  • Loading branch information
pavelkumbrasev authored Jun 13, 2023
1 parent fc18473 commit f8f7f73
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/tbb/arena.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2005-2022 Intel Corporation
Copyright (c) 2005-2023 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -110,7 +110,7 @@ std::uintptr_t arena::calculate_stealing_threshold() {
void arena::process(thread_data& tls) {
governor::set_thread_data(tls); // TODO: consider moving to create_one_job.
__TBB_ASSERT( is_alive(my_guard), nullptr);
__TBB_ASSERT( my_num_slots > 1, nullptr);
__TBB_ASSERT( my_num_slots >= 1, nullptr);

std::size_t index = occupy_free_slot</*as_worker*/true>(tls);
if (index == out_of_arena) {
Expand Down Expand Up @@ -171,7 +171,7 @@ arena::arena ( market& m, unsigned num_slots, unsigned num_reserved_slots, unsig
my_market = &m;
my_limit = 1;
// Two slots are mandatory: for the external thread, and for 1 worker (required to support starvation resistant tasks).
my_num_slots = num_arena_slots(num_slots);
my_num_slots = num_arena_slots(num_slots, num_reserved_slots);
my_num_reserved_slots = num_reserved_slots;
my_max_num_workers = num_slots-num_reserved_slots;
my_priority_level = priority_level;
Expand Down Expand Up @@ -212,11 +212,11 @@ arena& arena::allocate_arena( market& m, unsigned num_slots, unsigned num_reserv
__TBB_ASSERT( sizeof(base_type) + sizeof(arena_slot) == sizeof(arena), "All arena data fields must go to arena_base" );
__TBB_ASSERT( sizeof(base_type) % cache_line_size() == 0, "arena slots area misaligned: wrong padding" );
__TBB_ASSERT( sizeof(mail_outbox) == max_nfs_size, "Mailbox padding is wrong" );
std::size_t n = allocation_size(num_arena_slots(num_slots));
std::size_t n = allocation_size(num_arena_slots(num_slots, num_reserved_slots));
unsigned char* storage = (unsigned char*)cache_aligned_allocate(n);
// Zero all slots to indicate that they are empty
std::memset( storage, 0, n );
return *new( storage + num_arena_slots(num_slots) * sizeof(mail_outbox) )
return *new( storage + num_arena_slots(num_slots, num_reserved_slots) * sizeof(mail_outbox) )
arena(m, num_slots, num_reserved_slots, priority_level);
}

Expand Down Expand Up @@ -478,7 +478,7 @@ bool task_arena_impl::attach(d1::task_arena_base& ta) {
ta.my_num_reserved_slots = a->my_num_reserved_slots;
ta.my_priority = arena_priority(a->my_priority_level);
ta.my_max_concurrency = ta.my_num_reserved_slots + a->my_max_num_workers;
__TBB_ASSERT(arena::num_arena_slots(ta.my_max_concurrency) == a->my_num_slots, nullptr);
__TBB_ASSERT(arena::num_arena_slots(ta.my_max_concurrency, ta.my_num_reserved_slots) == a->my_num_slots, nullptr);
ta.my_arena.store(a, std::memory_order_release);
// increases market's ref count for task_arena
market::global_market( /*is_public=*/true );
Expand Down
6 changes: 3 additions & 3 deletions src/tbb/arena.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2005-2022 Intel Corporation
Copyright (c) 2005-2023 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -312,8 +312,8 @@ class arena: public padded<arena_base>
static arena& allocate_arena( market& m, unsigned num_slots, unsigned num_reserved_slots,
unsigned priority_level );

static int unsigned num_arena_slots ( unsigned num_slots ) {
return max(2u, num_slots);
static int unsigned num_arena_slots ( unsigned num_slots, unsigned num_reserved_slots ) {
return num_reserved_slots == 0 ? num_slots : max(2u, num_slots);
}

static int allocation_size ( unsigned num_slots ) {
Expand Down
21 changes: 20 additions & 1 deletion test/tbb/test_task_arena.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2005-2022 Intel Corporation
Copyright (c) 2005-2023 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1979,3 +1979,22 @@ TEST_CASE("is_inside_task in arena::execute") {
});
}
#endif //__TBB_PREVIEW_TASK_GROUP_EXTENSIONS

//! \brief \ref interface \ref requirement \ref regression
TEST_CASE("worker threads occupy slots in correct range") {
std::vector<tbb::task_arena> arenas(42);
for (auto& arena : arenas) {
arena.initialize(1, 0);
}

tbb::task_group tg;
for (auto& arena : arenas) {
arena.execute([&] {
tg.run([] {
CHECK(tbb::this_task_arena::current_thread_index() == 0);
});
});
}

tg.wait();
}

0 comments on commit f8f7f73

Please sign in to comment.