23 #include <system_error>
24 #include <type_traits>
28 #include <kvikio/defaults.hpp>
29 #include <kvikio/error.hpp>
30 #include <kvikio/nvtx.hpp>
31 #include <kvikio/utils.hpp>
52 auto make_copyable_lambda(F op)
55 auto sp = std::make_shared<F>(std::move(op));
59 [sp](
auto&&... args) -> decltype(
auto) {
return (*sp)(std::forward<decltype(args)>(args)...); };
72 inline const std::pair<const nvtx_color_type&, std::uint64_t> get_next_color_and_call_idx() noexcept
74 static std::atomic_uint64_t call_counter{1ull};
75 auto call_idx = call_counter.fetch_add(1ull, std::memory_order_relaxed);
77 return {nvtx_color, call_idx};
94 template <
typename F,
typename T>
95 std::future<std::size_t> submit_task(F op,
98 std::size_t file_offset,
99 std::size_t devPtr_offset,
100 std::uint64_t nvtx_payload = 0ull,
103 static_assert(std::is_invocable_r_v<std::size_t,
107 decltype(file_offset),
108 decltype(devPtr_offset)>);
111 KVIKIO_NVTX_SCOPED_RANGE(
"task", nvtx_payload, nvtx_color);
112 return op(buf, size, file_offset, devPtr_offset);
129 template <
typename F>
130 std::future<std::size_t> submit_move_only_task(
132 std::uint64_t nvtx_payload = 0ull,
135 static_assert(std::is_invocable_r_v<std::size_t, F>);
136 auto op_copyable = make_copyable_lambda(std::move(op_move_only));
138 KVIKIO_NVTX_SCOPED_RANGE(
"task", nvtx_payload, nvtx_color);
139 return op_copyable();
158 template <
typename F,
typename T>
162 std::size_t file_offset,
163 std::size_t task_size,
164 std::size_t devPtr_offset,
165 std::uint64_t call_idx = 0,
168 KVIKIO_EXPECT(task_size > 0,
"`task_size` 必须为正数", std::invalid_argument);
169 static_assert(std::is_invocable_r_v<std::size_t,
173 decltype(file_offset),
174 decltype(devPtr_offset)>);
177 if (task_size >= size || page_size >= size) {
178 return detail::submit_task(op, buf, size, file_offset, devPtr_offset, call_idx, nvtx_color);
181 std::vector<std::future<std::size_t>> tasks;
182 tasks.reserve(size / task_size);
185 while (size > task_size) {