Skip to content

[Executorch][threadpool] Add "private" method to reset num threads in threadpool #2351

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions backends/xnnpack/threadpool/threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ size_t ThreadPool::get_thread_count() const {
return pthreadpool_get_threads_count(threadpool_.get());
}

bool ThreadPool::_unsafe_reset_threadpool(uint32_t new_thread_count) {
// No need to do anything if the count is same or 0
if (new_thread_count == get_thread_count() || new_thread_count == 0) {
return true;
}

std::lock_guard<std::mutex> lock{mutex_};

threadpool_.reset(pthreadpool_create(new_thread_count));
return true;
}

void ThreadPool::run(
const std::function<void(size_t)>& fn,
const size_t range) {
Expand Down
11 changes: 11 additions & 0 deletions backends/xnnpack/threadpool/threadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class ThreadPool final {

size_t get_thread_count() const;

/*
* Resets the threadpool by creating a new threadpool with requested # of
* threads. This is not a thread safe call. When calling this method, threads
* of the threadpool might be doing some work. Some other code may also be
* holding on to the threadpool pointer, that is no longer valid. This is a
* private API, which will later be replaced by something that allows creating
* of threadpool with requested size and use such a threadpool with backend
* delegates, custom ops or optimized lib.
*/
bool _unsafe_reset_threadpool(uint32_t num_threads);

// Run, in parallel, function fn(task_id) over task_id in range [0, range).
// This function is blocking. All input is processed by the time it returns.
// NoThreadPoolGuard (see threadpool_guard.h) can used to disable
Expand Down