From 8531286777aaaf31596012ed924e7f0d6189f8f0 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 30 Jan 2022 17:44:48 -0700 Subject: [PATCH] qbsp: replace `-nothreads` with `-threads 1` for consistency with light/vis --- common/CMakeLists.txt | 2 +- common/threads.cc | 17 +++++++++++++++++ include/common/threads.hh | 11 +++++++++++ include/qbsp/qbsp.hh | 2 +- qbsp/qbsp.cc | 16 ++++++++-------- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index ff161807..90d1a120 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -25,4 +25,4 @@ add_library(common STATIC ${CMAKE_SOURCE_DIR}/include/common/fs.hh ${CMAKE_SOURCE_DIR}/include/common/imglib.hh) -target_link_libraries(common ${CMAKE_THREAD_LIBS_INIT} fmt::fmt nlohmann_json::nlohmann_json) +target_link_libraries(common ${CMAKE_THREAD_LIBS_INIT} TBB::tbb fmt::fmt nlohmann_json::nlohmann_json) diff --git a/common/threads.cc b/common/threads.cc index e8d9a8bb..33df3ba3 100644 --- a/common/threads.cc +++ b/common/threads.cc @@ -169,3 +169,20 @@ void RunThreadsOn(int start, int workcnt, void *(func)(void *), void *arg) } #endif /* HAVE_THREADS */ + +/* + * ======================================================================= + * TBB + * ======================================================================= + */ + +std::unique_ptr ConfigureTBB(int maxthreads) +{ + auto tbbOptions = std::unique_ptr(); + + if (maxthreads > 0) { + tbbOptions = std::make_unique(tbb::global_control::max_allowed_parallelism, maxthreads); + } + + return tbbOptions; +} diff --git a/include/common/threads.hh b/include/common/threads.hh index bf94236d..5ef6ffc4 100644 --- a/include/common/threads.hh +++ b/include/common/threads.hh @@ -2,6 +2,10 @@ #pragma once +#include + +#include "tbb/global_control.h" + extern int numthreads; void LowerProcessPriority(void); @@ -15,3 +19,10 @@ void ThreadUnlock(void); /* Call if needing to print to stdout - should be called with lock held */ void InterruptThreadProgress__(void); + +/** + * Configures TBB to have the given max threads (specify 0 for unlimited). + * + * Call this from main() and keep the returned object until main() finishes. + */ +std::unique_ptr ConfigureTBB(int maxthreads); diff --git a/include/qbsp/qbsp.hh b/include/qbsp/qbsp.hh index edfeb647..d1d00d81 100644 --- a/include/qbsp/qbsp.hh +++ b/include/qbsp/qbsp.hh @@ -117,7 +117,7 @@ public: bool fLeakTest = false; bool fContentHack = false; vec_t worldExtent = 0.f; - bool fNoThreads = false; + int threads = 0; // 0 = let TBB auto select ideal number of threads bool includeSkip = false; bool fNoTJunc = false; }; diff --git a/qbsp/qbsp.cc b/qbsp/qbsp.cc index 950b520a..9262bdf1 100644 --- a/qbsp/qbsp.cc +++ b/qbsp/qbsp.cc @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -1059,7 +1060,7 @@ PrintOptions " -expand Write hull 1 expanded brushes to expanded.map for debugging\n" " -leaktest Make compilation fail if the map leaks\n" " -contenthack Hack to fix leaks through solids. Causes missing faces in some cases so disabled by default.\n" - " -nothreads Disable multithreading\n" + " -threads n Set the number of threads (1 to disable multithreading)\n" " sourcefile .MAP file to process\n" " destfile .BSP file to output\n"); @@ -1295,8 +1296,11 @@ static void ParseOptions(char *szOptions) options.fLeakTest = true; } else if (!Q_strcasecmp(szTok, "contenthack")) { options.fContentHack = true; - } else if (!Q_strcasecmp(szTok, "nothreads")) { - options.fNoThreads = true; + } else if (!Q_strcasecmp(szTok, "threads")) { + szTok2 = GetTok(szTok + strlen(szTok) + 1, szEnd); + if (!szTok2) + FError("Invalid argument to option {}", szTok); + options.threads = atoi(szTok2); } else if (!Q_strcasecmp(szTok, "?") || !Q_strcasecmp(szTok, "help")) { PrintOptions(); } else { @@ -1421,11 +1425,7 @@ int qbsp_main(int argc, const char **argv) InitQBSP(argc, argv); - // disable TBB if requested - auto tbbOptions = std::unique_ptr(); - if (options.fNoThreads) { - tbbOptions = std::make_unique(tbb::global_control::max_allowed_parallelism, 1); - } + auto tbbOptions = ConfigureTBB(options.threads); // do it! auto start = I_FloatTime();