All Classes Namespaces Functions Enumerations Enumerator Modules Pages
threadpool_wrapper.hpp
1 /*
2  * Copyright (c) 2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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  // Rename the worker thread in the thread pool to improve clarity from nsys-ui.
50  // Note: This NVTX feature is currently not supported by nsys-ui.
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
Rename the current thread under the KvikIO NVTX domain.
static const nvtx_color_type & default_color() noexcept
Return the default color.
thread_pool_wrapper(unsigned int nthreads)
Construct a new thread pool wrapper, and invoke a pre-defined initialization function in each worker ...
void reset(unsigned int nthreads)
Reset the number of threads in the thread pool, and invoke a pre-defined initialization function in e...
KvikIO namespace.
Definition: batch.hpp:27