Limit work items to available threads, upgrade checks from assert to ORT_ENFORCE (#8495)

This commit is contained in:
Tim Harris 2021-07-28 03:25:12 +01:00 committed by GitHub
parent 686f9b530b
commit 56441dcd88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 1 deletions

View file

@ -1192,6 +1192,7 @@ void RunInParallelSection(ThreadPoolParallelSection &ps,
std::function<void(unsigned idx)> fn,
unsigned n,
std::ptrdiff_t block_size) override {
ORT_ENFORCE(n <= num_threads_+1, "More work items than threads");
profiler_.LogStartAndCoreAndBlock(block_size);
PerThread* pt = GetPerThread();
assert(pt->leading_par_section && "RunInParallel, but not in parallel section");
@ -1249,6 +1250,7 @@ void RunInParallelSection(ThreadPoolParallelSection &ps,
// For all other threads:
// 1. run fn(...);
void RunInParallel(std::function<void(unsigned idx)> fn, unsigned n, std::ptrdiff_t block_size) override {
ORT_ENFORCE(n <= num_threads_+1, "More work items than threads");
profiler_.LogStartAndCoreAndBlock(block_size);
PerThread* pt = GetPerThread();
ThreadPoolParallelSection ps;

View file

@ -408,7 +408,8 @@ void ThreadPool::ParallelForFixedBlockSizeScheduling(const std::ptrdiff_t total,
// hence we need at most one for each thread, even if the numberof blocks of iterations is larger.
auto d_of_p = DegreeOfParallelism(this);
auto num_blocks = total / block_size;
int num_work_items = static_cast<int>(std::min(static_cast<std::ptrdiff_t>(d_of_p), num_blocks));
auto num_threads_inc_main = NumThreads() + 1;
int num_work_items = static_cast<int>(std::min(static_cast<std::ptrdiff_t>(num_threads_inc_main), num_blocks));
assert(num_work_items > 0);
LoopCounter lc(total, d_of_p, block_size);