Skip to content

Commit cff1032

Browse files
Add a simple threadpool.
1 parent f5268f9 commit cff1032

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

cpp/benchmarks/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ function(kvikio_add_benchmark)
4444
add_executable(${_KVIKIO_NAME} ${_KVIKIO_SOURCES})
4545
set_target_properties(${_KVIKIO_NAME} PROPERTIES INSTALL_RPATH "\$ORIGIN/../../../lib")
4646

47+
<<<<<<< HEAD
48+
=======
49+
target_include_directories(${_KVIKIO_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
50+
>>>>>>> e54d507 (Add a simple threadpool.)
4751
target_link_libraries(${_KVIKIO_NAME} PUBLIC benchmark::benchmark kvikio::kvikio)
4852

4953
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
@@ -59,4 +63,10 @@ function(kvikio_add_benchmark)
5963
)
6064
endfunction()
6165

66+
<<<<<<< HEAD
6267
kvikio_add_benchmark(NAME THREADPOOL_BENCHMARK SOURCES "threadpool/threadpool_benchmark.cpp")
68+
=======
69+
kvikio_add_benchmark(
70+
NAME THREADPOOL_BENCHMARK SOURCES "threadpool/threadpool_benchmark.cpp" "utils/utils.cpp"
71+
)
72+
>>>>>>> e54d507 (Add a simple threadpool.)

cpp/benchmarks/threadpool/threadpool_benchmark.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <benchmark/benchmark.h>
3030
#include <kvikio/defaults.hpp>
31+
#include <utils/utils.hpp>
3132

3233
namespace kvikio {
3334
enum class ScalingType : uint8_t {
@@ -88,6 +89,8 @@ int main(int argc, char** argv)
8889
->Unit(benchmark::kMillisecond)
8990
->MinTime(2);
9091

92+
kvikio::utils::explain_default_metrics();
93+
9194
benchmark::RunSpecifiedBenchmarks();
9295
benchmark::Shutdown();
9396
}

cpp/benchmarks/utils/utils.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
#include <benchmark/benchmark.h>
18+
#include <utils/utils.hpp>
19+
20+
namespace kvikio::utils {
21+
void explain_default_metrics()
22+
{
23+
benchmark::AddCustomContext(
24+
"Time",
25+
"The average real time (i.e. wall-clock time) of the entire process per benchmark iteration.");
26+
benchmark::AddCustomContext(
27+
"CPU",
28+
"The average CPU time of the main thread per benchmark iteration. The timer is accumulated "
29+
"only when the main thread is being executed.");
30+
}
31+
} // namespace kvikio::utils

cpp/benchmarks/utils/utils.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
namespace kvikio::utils {
20+
void explain_default_metrics();
21+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 andc
14+
* limitations under the License.
15+
*/
16+
17+
#include <atomic>
18+
#include <functional>
19+
#include <future>
20+
#include <thread>
21+
#include <type_traits>
22+
#include <vector>
23+
24+
namespace kvikio {
25+
/**
26+
* @brief A simple thread pool that executes tasks in an embarrassingly parallel manner.
27+
*
28+
* The implementation is header-only.
29+
*/
30+
class ThreadPoolSimple {
31+
public:
32+
ThreadPoolSimple(
33+
unsigned int num_threads, const std::function<void()>& worker_thread_init_func = [] {})
34+
: _num_threads{num_threads}, _worker_thread_init_func{worker_thread_init_func}
35+
{
36+
}
37+
38+
void reset();
39+
40+
template <typename F, typename R = std::invoke_result_t<std::decay_t<F>>>
41+
[[nodiscard]] std::future<R> submit_task(F&& task)
42+
{
43+
}
44+
45+
private:
46+
void worker() {}
47+
48+
void create_threads()
49+
{
50+
for (unsigned int i = 0; i < _num_threads; ++i) {
51+
_thread_container.emplace_back(&ThreadPoolSimple::worker, _worker_thread_init_func);
52+
}
53+
}
54+
55+
void destroy_threads() {}
56+
57+
std::atomic_bool _done{false};
58+
unsigned int _num_threads{};
59+
std::function<void()> _worker_thread_init_func{};
60+
std::vector<std::thread> _thread_container{};
61+
};
62+
63+
} // namespace kvikio

0 commit comments

Comments
 (0)