|
13 | 13 | #include <cstdlib> // for malloc, free
|
14 | 14 | #include <functional> // for function
|
15 | 15 | #include <new> // for bad_alloc
|
16 |
| -#include <type_traits> // for is_signed, conditional_t |
| 16 | +#include <type_traits> // for is_signed, conditional_t, is_integral_v, invoke_result_t |
17 | 17 | #include <vector> // for vector
|
18 | 18 |
|
19 | 19 | #include "xgboost/logging.h"
|
@@ -87,8 +87,9 @@ class BlockedSpace2d {
|
87 | 87 | // dim1 - size of the first dimension in the space
|
88 | 88 | // getter_size_dim2 - functor to get the second dimensions for each 'row' by row-index
|
89 | 89 | // grain_size - max size of produced blocks
|
90 |
| - BlockedSpace2d(std::size_t dim1, std::function<std::size_t(std::size_t)> getter_size_dim2, |
91 |
| - std::size_t grain_size) { |
| 90 | + template <typename Getter> |
| 91 | + BlockedSpace2d(std::size_t dim1, Getter&& getter_size_dim2, std::size_t grain_size) { |
| 92 | + static_assert(std::is_integral_v<std::invoke_result_t<Getter, std::size_t>>); |
92 | 93 | for (std::size_t i = 0; i < dim1; ++i) {
|
93 | 94 | std::size_t size = getter_size_dim2(i);
|
94 | 95 | // Each row (second dim) is divided into n_blocks
|
@@ -137,8 +138,9 @@ class BlockedSpace2d {
|
137 | 138 |
|
138 | 139 |
|
139 | 140 | // Wrapper to implement nested parallelism with simple omp parallel for
|
140 |
| -inline void ParallelFor2d(BlockedSpace2d const& space, std::int32_t n_threads, |
141 |
| - std::function<void(std::size_t, Range1d)> func) { |
| 141 | +template <typename Func> |
| 142 | +void ParallelFor2d(const BlockedSpace2d& space, int n_threads, Func&& func) { |
| 143 | + static_assert(std::is_void_v<std::invoke_result_t<Func, std::size_t, Range1d>>); |
142 | 144 | std::size_t n_blocks_in_space = space.Size();
|
143 | 145 | CHECK_GE(n_threads, 1);
|
144 | 146 |
|
|
0 commit comments