Skip to content

Commit e525ed2

Browse files
author
Dian-Lun (Aaron) Lin
authored
Support omp threadpool (#170)
This pull request introduces a new OpenMP thread pool, `OMPThreadPool`, designed for users who wish to utilize OpenMP threading.
1 parent bc690a4 commit e525ed2

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ include("cmake/fmt.cmake")
7777
include("cmake/spdlog.cmake")
7878
include("cmake/toml.cmake")
7979

80+
if(SVS_ENABLE_OMP)
81+
include("cmake/openmp.cmake")
82+
endif()
83+
8084
add_library(svs_x86_options_base INTERFACE)
8185
add_library(svs::x86_options_base ALIAS svs_x86_options_base)
8286
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")

cmake/openmp.cmake

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
# limitations under the License.
1414

1515
find_package(OpenMP)
16-
if (OPENMP_FOUND)
17-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
18-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
19-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
16+
if (OPENMP_CXX_FOUND)
17+
target_link_libraries(${SVS_LIB} INTERFACE OpenMP::OpenMP_CXX)
2018
else()
2119
message(FATAL_ERROR "no OpenMP support")
2220
endif()

cmake/options.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ option(SVS_INITIALIZE_LOGGER
7373
ON # enabled by default
7474
)
7575

76+
option(SVS_ENABLE_OMP
77+
"Support OpenMP threadpool."
78+
OFF # disable by default
79+
)
80+
7681
#####
7782
##### Experimental
7883
#####
@@ -140,6 +145,12 @@ else()
140145
target_compile_options(${SVS_LIB} INTERFACE -DSVS_INITIALIZE_LOGGER=0)
141146
endif()
142147

148+
if (SVS_ENABLE_OMP)
149+
target_compile_options(${SVS_LIB} INTERFACE -DSVS_ENABLE_OMP=1)
150+
else()
151+
target_compile_options(${SVS_LIB} INTERFACE -DSVS_ENABLE_OMP=0)
152+
endif()
153+
143154
#####
144155
##### Helper target to apply relevant compiler optimizations.
145156
#####

include/svs/lib/threads/threadpool.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#include <sstream>
2727
#include <vector>
2828

29+
SVS_VALIDATE_BOOL_ENV(SVS_ENABLE_OMP);
30+
#if SVS_ENABLE_OMP
31+
#include <omp.h>
32+
#endif
33+
2934
#include "svs/lib/numa.h"
3035
#include "svs/lib/threads/thread.h"
3136
#include "svs/lib/threads/thunks.h"
@@ -283,6 +288,28 @@ class SwitchNativeThreadPool {
283288
NativeThreadPool threadpool_;
284289
};
285290

291+
SVS_VALIDATE_BOOL_ENV(SVS_ENABLE_OMP);
292+
#if SVS_ENABLE_OMP
293+
/////
294+
///// A thread pool that utilizes OpenMP for multithreading
295+
/////
296+
class OMPThreadPool {
297+
public:
298+
explicit OMPThreadPool(size_t num_threads) { omp_set_num_threads(num_threads); }
299+
300+
size_t size() const { return omp_get_num_threads(); }
301+
302+
void parallel_for(std::function<void(size_t)> f, size_t n) {
303+
#pragma omp parallel for
304+
for (size_t i = 0; i < n; ++i) {
305+
f(i);
306+
}
307+
}
308+
309+
void resize(size_t num_threads) { omp_set_num_threads(num_threads); }
310+
};
311+
#endif
312+
286313
/////
287314
///// A handy reference wrapper for situations where we only want to share a thread pool
288315
/////

0 commit comments

Comments
 (0)