threadpool_wrapper.hpp
1 /*
2  * 版权所有 (c) 2025, NVIDIA CORPORATION.
3  *
4  * 根据 Apache 许可证 2.0 版(“许可证”)获得许可;
5  * 您不得使用此文件,除非符合许可证的规定。
6  * 您可以在以下位置获取许可证的副本:
7  *
8  * https://apache.ac.cn/licenses/LICENSE-2.0
9  *
10  * 除非适用法律要求或书面同意,根据许可证分发的软件是基于“原样”分发的,
11  * 不附带任何明示或默示的保证或条件。
12  * 请参阅许可证,了解许可证下特定语言的权限和限制。
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <functional>
20 
21 #include <BS_thread_pool.hpp>
22 
23 #include <kvikio/nvtx.hpp>
24 
25 namespace kvikio {
26 
27 template <typename pool_type>
28 class thread_pool_wrapper : public pool_type {
29  public
36  thread_pool_wrapper(unsigned int nthreads) : pool_type{nthreads, worker_thread_init_func} {}
37 
44  void reset(unsigned int nthreads) { pool_type::reset(nthreads, worker_thread_init_func); }
45 
46  private
47  inline static std::function<void()> worker_thread_init_func{[] {
48  KVIKIO_NVTX_SCOPED_RANGE("worker thread init", 0, NvtxManager::default_color());
49  // 重命名线程池中的工作线程,以提高 nsys-ui 的清晰度。
50  // 注意:nsys-ui 目前不支持此 NVTX 功能。
52  }};
53 };
54 
55 using BS_thread_pool = thread_pool_wrapper<BS::thread_pool>;
56 
57 } // namespace kvikio
static void rename_current_thread(std::string_view new_name) noexcept
在 KvikIO NVTX 域下重命名当前线程。
static const nvtx_color_type & default_color() noexcept
返回默认颜色。
thread_pool_wrapper(unsigned int nthreads)
构造一个新的线程池包装器,并在每个工作线程中调用一个预定义初始化函数...。
void reset(unsigned int nthreads)
重置线程池中的线程数,并在每个工作线程中调用一个预定义初始化函数...。
KvikIO 命名空间。
定义: batch.hpp:27