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  {
38  KVIKIO_NVTX_FUNC_RANGE();
39  }
40 
47  void reset(unsigned int nthreads)
48  {
49  KVIKIO_NVTX_FUNC_RANGE();
50  pool_type::reset(nthreads, worker_thread_init_func);
51  }
52 
53  private:
54  inline static std::function<void()> worker_thread_init_func{[] {
55  KVIKIO_NVTX_FUNC_RANGE();
56  // Rename the worker thread in the thread pool to improve clarity from nsys-ui.
57  // Note: This NVTX feature is currently not supported by nsys-ui.
59  }};
60 };
61 
62 using BS_thread_pool = thread_pool_wrapper<BS::thread_pool>;
63 
64 } // namespace kvikio
static void rename_current_thread(std::string_view new_name) noexcept
Rename the current thread under the KvikIO NVTX domain.
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