Skip to content

Commit

Permalink
Add build-time config for threadpool size
Browse files Browse the repository at this point in the history
Summary:
Add a preprocessor flag (ET_THREADPOOL_SIZE) and corresponding buck config ("executorch.threadpool_size") to set the default ET threadpool size.

The intent of this change is to allow users in very memory-constrained environments to opt in to a smaller threadpool to shave a small amount of additional memory. Even when measuring resident set size, the addition threads can add more than a Mb of peak memory.

I considered making this a parameter to get_threadpool, but it is not consistently usable, as it depends on who first instantiates the threadpool. As it's a shared resource, it seemed more prudent to make it a build-time configuration.

Differential Revision: D68401795
  • Loading branch information
GregoryComer authored and facebook-github-bot committed Jan 20, 2025
1 parent fedb035 commit 786b93d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions extension/threadpool/targets.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
load("@fbsource//xplat/executorch/backends/xnnpack/third-party:third_party_libs.bzl", "third_party_dep")
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

def get_threadpool_size():
return native.read_config("executorch", "threadpool_size")

def get_threadpool_flags():
flags = []
threadpool_size = get_threadpool_size()
if threadpool_size != None:
flags += ["-DET_THREADPOOL_SIZE=" + threadpool_size]
return flags

def define_common_targets():
"""Defines targets that should be shared between fbcode and xplat.
Expand Down Expand Up @@ -32,6 +42,7 @@ def define_common_targets():
exported_preprocessor_flags = [
"-DET_USE_THREADPOOL",
],
preprocessor_flags = get_threadpool_flags(),
visibility = [
"//executorch/...",
"//executorch/backends/...",
Expand Down
6 changes: 6 additions & 0 deletions extension/threadpool/threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ void ThreadPool::run(
// Make this part threadsafe: TODO(kimishpatel)
ThreadPool* get_threadpool() {
ET_CHECK_MSG(cpuinfo_initialize(), "cpuinfo initialization failed");

#ifdef ET_THREADPOOL_SIZE
int num_threads = ET_THREADPOOL_SIZE;
#else
int num_threads = cpuinfo_get_processors_count();
#endif

/*
* For llvm-tsan, holding limit for the number of locks for a single thread
* is 63 (because of comparison < 64 instead of <=). pthreadpool's worst
Expand Down

0 comments on commit 786b93d

Please sign in to comment.