File tree Expand file tree Collapse file tree 4 files changed +44
-4
lines changed
Expand file tree Collapse file tree 4 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,10 @@ include("cmake/fmt.cmake")
7777include ("cmake/spdlog.cmake" )
7878include ("cmake/toml.cmake" )
7979
80+ if (SVS_ENABLE_OMP)
81+ include ("cmake/openmp.cmake" )
82+ endif ()
83+
8084add_library (svs_x86_options_base INTERFACE )
8185add_library (svs::x86_options_base ALIAS svs_x86_options_base)
8286if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)" )
Original file line number Diff line number Diff line change 1313# limitations under the License.
1414
1515find_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)
2018else ()
2119 message (FATAL_ERROR "no OpenMP support" )
2220endif ()
Original file line number Diff line number Diff 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)
141146endif ()
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#####
Original file line number Diff line number Diff line change 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// ///
You can’t perform that action at this time.
0 commit comments