Skip to content

Commit cd8aed6

Browse files
authored
Use a separate threadpool library
Differential Revision: D61940492 Pull Request resolved: #4967
1 parent 1d6662d commit cd8aed6

File tree

11 files changed

+61
-38
lines changed

11 files changed

+61
-38
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,13 @@ if(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL)
637637
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/runner_util)
638638
endif()
639639

640+
if(EXECUTORCH_BUILD_PTHREADPOOL
641+
AND EXECUTORCH_BUILD_CPUINFO
642+
AND CMAKE_CXX_STANDARD GREATER_EQUAL 14
643+
)
644+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/threadpool)
645+
endif()
646+
640647
if(EXECUTORCH_BUILD_PYBIND)
641648
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/pybind11)
642649

backends/xnnpack/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ et_cxx_test(
3434
SOURCES
3535
${_test_srcs}
3636
EXTRA_LIBS
37+
extension_threadpool
3738
xnnpack_backend
3839
XNNPACK
3940
pthreadpool

build/Test.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ find_package(executorch CONFIG REQUIRED)
2525
enable_testing()
2626
find_package(GTest CONFIG REQUIRED)
2727

28+
target_link_options_shared_lib(cpuinfo)
2829
target_link_options_shared_lib(extension_data_loader)
2930
target_link_options_shared_lib(portable_kernels)
3031
target_link_options_shared_lib(portable_ops_lib)
32+
target_link_options_shared_lib(pthreadpool)
3133
target_link_options_shared_lib(quantized_ops_lib)
3234

3335
# Add code coverage flags to supported compilers

build/executorch-config.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ set(lib_list
4646
extension_module
4747
extension_module_static
4848
extension_runner_util
49+
extension_threadpool
4950
xnnpack_backend
5051
XNNPACK
5152
cpuinfo

examples/models/llama2/CMakeLists.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,15 @@ set(XNNPACK_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack)
125125
# Extra compile option and include dir for pthreadpool
126126
if(EXECUTORCH_BUILD_PTHREADPOOL)
127127
list(APPEND _common_compile_options -DET_USE_THREADPOOL)
128-
list(APPEND link_libraries pthreadpool)
129-
# These 2 source files are included in xnnpack_backend
130-
if(NOT TARGET xnnpack_backend)
131-
list(APPEND _srcs ${EXECUTORCH_ROOT}/extension/threadpool/threadpool.cpp
132-
${EXECUTORCH_ROOT}/extension/threadpool/threadpool_guard.cpp
133-
)
134-
endif()
128+
list(APPEND link_libraries extension_threadpool pthreadpool)
135129
list(APPEND _common_include_directories
136130
${XNNPACK_ROOT}/third-party/pthreadpool/include
137131
)
138132
endif()
139133

140134
# Extra sources for cpuinfo
141135
if(EXECUTORCH_BUILD_CPUINFO)
142-
list(APPEND link_libraries cpuinfo)
143-
list(APPEND _srcs ${EXECUTORCH_ROOT}/extension/threadpool/cpuinfo_utils.cpp)
136+
list(APPEND link_libraries extension_threadpool cpuinfo)
144137
list(APPEND _common_include_directories
145138
${XNNPACK_ROOT}/third-party/cpuinfo/include
146139
)

examples/models/llava/CMakeLists.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,15 @@ set(XNNPACK_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack)
127127
# Extra compile option and include dir for pthreadpool
128128
if(EXECUTORCH_BUILD_PTHREADPOOL)
129129
list(APPEND _common_compile_options -DET_USE_THREADPOOL)
130-
list(APPEND link_libraries pthreadpool)
131-
# These 2 source files are included in xnnpack_backend
132-
if(NOT TARGET xnnpack_backend)
133-
list(APPEND _srcs ${EXECUTORCH_ROOT}/extension/threadpool/threadpool.cpp
134-
${EXECUTORCH_ROOT}/extension/threadpool/threadpool_guard.cpp
135-
)
136-
endif()
130+
list(APPEND link_libraries extension_threadpool pthreadpool)
137131
list(APPEND _common_include_directories
138132
${XNNPACK_ROOT}/third-party/pthreadpool/include
139133
)
140134
endif()
141135

142136
# Extra sources for cpuinfo
143137
if(EXECUTORCH_BUILD_CPUINFO)
144-
list(APPEND link_libraries cpuinfo)
145-
list(APPEND _srcs ${EXECUTORCH_ROOT}/extension/threadpool/cpuinfo_utils.cpp)
138+
list(APPEND link_libraries extension_threadpool cpuinfo)
146139
list(APPEND _common_include_directories
147140
${XNNPACK_ROOT}/third-party/cpuinfo/include
148141
)

extension/android/CMakeLists.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ find_package(executorch CONFIG REQUIRED)
3232
target_link_options_shared_lib(executorch)
3333

3434
set(link_libraries)
35-
list(APPEND link_libraries extension_data_loader extension_module executorch
35+
list(APPEND link_libraries extension_data_loader extension_module extension_threadpool executorch
3636
fbjni
3737
)
3838

@@ -93,13 +93,7 @@ if(EXECUTORCH_BUILD_LLAMA_JNI)
9393

9494
target_link_options_shared_lib(quantized_ops_lib)
9595

96-
if(TARGET pthreadpool)
97-
set(LLAMA_JNI_SRCS jni/jni_layer_llama.cpp
98-
../../extension/threadpool/cpuinfo_utils.cpp
99-
)
100-
else()
101-
set(LLAMA_JNI_SRCS jni/jni_layer_llama.cpp)
102-
endif()
96+
set(LLAMA_JNI_SRCS jni/jni_layer_llama.cpp)
10397
add_library(executorch_llama_jni SHARED ${LLAMA_JNI_SRCS})
10498
if(TARGET pthreadpool)
10599
target_compile_definitions(executorch_llama_jni PRIVATE ET_USE_THREADPOOL=1)

extension/llm/custom_ops/CMakeLists.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,10 @@ list(APPEND custom_ops_libs eigen_blas)
4747

4848
list(TRANSFORM _custom_ops__srcs PREPEND "${EXECUTORCH_ROOT}/")
4949

50-
# TODO: Consider moving xnnpack/threadpool in a separate lib since it's now used
51-
# by custom ops too.
5250
if(NOT EXECUTORCH_BUILD_XNNPACK)
53-
list(
54-
APPEND
55-
_custom_ops__srcs
56-
"${CMAKE_CURRENT_SOURCE_DIR}/../../../extension/threadpool/threadpool.cpp"
57-
"${CMAKE_CURRENT_SOURCE_DIR}/../../../extension/threadpool/threadpool_guard.cpp"
58-
)
51+
list(APPEND custom_ops_libs extension_threadpool)
5952
else()
60-
list(APPEND custom_ops_libs xnnpack_backend)
53+
list(APPEND custom_ops_libs extension_threadpool xnnpack_backend)
6154
endif()
6255

6356
add_library(custom_ops ${_custom_ops__srcs})

extension/parallel/test/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ include(${EXECUTORCH_ROOT}/build/Test.cmake)
2323

2424
set(_test_srcs
2525
thread_parallel_test.cpp ../thread_parallel.cpp
26-
${EXECUTORCH_ROOT}/extension/threadpool/threadpool.cpp
27-
${EXECUTORCH_ROOT}/extension/threadpool/threadpool_guard.cpp
2826
)
2927

3028
et_cxx_test(
31-
extension_parallel_test SOURCES ${_test_srcs} EXTRA_LIBS pthreadpool cpuinfo
29+
extension_parallel_test SOURCES ${_test_srcs} EXTRA_LIBS pthreadpool cpuinfo extension_threadpool
3230
)
3331
target_include_directories(
3432
extension_parallel_test

extension/threadpool/CMakeLists.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# Please this file formatted by running:
8+
# ~~~
9+
# cmake-format -i CMakeLists.txt
10+
# ~~~
11+
12+
cmake_minimum_required(VERSION 3.19)
13+
14+
# Source root directory for executorch.
15+
if(NOT EXECUTORCH_ROOT)
16+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
17+
endif()
18+
19+
if(NOT CMAKE_CXX_STANDARD)
20+
set(CMAKE_CXX_STANDARD 17)
21+
endif()
22+
23+
add_library(extension_threadpool threadpool.cpp threadpool_guard.cpp cpuinfo_utils.cpp)
24+
target_link_libraries(extension_threadpool PUBLIC executorch cpuinfo pthreadpool)
25+
target_include_directories(extension_threadpool PUBLIC ${EXECUTORCH_ROOT}/..)
26+
target_include_directories(
27+
extension_threadpool
28+
PUBLIC
29+
${EXECUTORCH_ROOT}/backends/xnnpack/third-party/cpuinfo/include
30+
${EXECUTORCH_ROOT}/backends/xnnpack/third-party/pthreadpool/include
31+
)
32+
target_compile_options(extension_threadpool PUBLIC ${_common_compile_options})
33+
34+
# Install libraries
35+
install(
36+
TARGETS extension_threadpool
37+
DESTINATION lib
38+
INCLUDES
39+
DESTINATION ${_common_include_directories}
40+
)

extension/threadpool/threadpool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cpuinfo.h>
1515

1616
#include <atomic>
17+
#include <memory>
1718

1819
namespace torch {
1920
namespace executorch {

0 commit comments

Comments
 (0)