Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor set of soft_limit to fix a bug #1418

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/tbb/thread_request_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,12 @@ void thread_request_serializer_proxy::set_active_num_workers(int soft_limit) {

if (soft_limit != 0) {
my_is_mandatory_concurrency_enabled = false;
my_serializer.set_active_num_workers(soft_limit);
} else {
if (my_num_mandatory_requests > 0 && !my_is_mandatory_concurrency_enabled) {
my_is_mandatory_concurrency_enabled = true;
my_serializer.set_active_num_workers(1);
}
} else if (my_num_mandatory_requests > 0) {
my_is_mandatory_concurrency_enabled = true;
soft_limit = 1;
}

my_serializer.set_active_num_workers(soft_limit);
}

int thread_request_serializer_proxy::num_workers_requested() { return my_serializer.num_workers_requested(); }
Expand Down
28 changes: 28 additions & 0 deletions test/tbb/test_global_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "common/utils.h"
#include "common/spin_barrier.h"
#include "common/utils_concurrency_limit.h"
#include "common/cpu_usertime.h"

#include "tbb/global_control.h"
#include "tbb/parallel_for.h"
Expand Down Expand Up @@ -272,3 +273,30 @@ TEST_CASE("test concurrent task_scheduler_handle destruction") {
stop = true;
thr1.join();
}

//! \brief \ref regression
TEST_CASE("Thread should sleep when soft_limit is zero") {
pavelkumbrasev marked this conversation as resolved.
Show resolved Hide resolved
isaevil marked this conversation as resolved.
Show resolved Hide resolved
int num_threads = int(utils::get_platform_max_threads());
std::atomic<int> barrier{num_threads};

// Warm-up threads
tbb::parallel_for(0, num_threads, [&] (int) {
--barrier;
while (barrier > 0) {
std::this_thread::yield();
}
pavelkumbrasev marked this conversation as resolved.
Show resolved Hide resolved
});

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 the soft_limit
pavelkumbrasev marked this conversation as resolved.
Show resolved Hide resolved
TestCPUUserTime(utils::get_platform_max_threads() - 1);

thr.join();
}
Loading