-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Async mmap population #36668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
olpipi
wants to merge
5
commits into
openvinotoolkit:master
Choose a base branch
from
olpipi:async_mmap_population
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+569
−139
Open
Async mmap population #36668
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
|
|
||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <filesystem> | ||
|
|
||
|
|
@@ -25,6 +26,15 @@ inline constexpr size_t default_parallel_io_min_chunk = 2UL * 1024 * 1024; ///< | |
| */ | ||
| inline constexpr size_t default_parallel_io_prefetch_cap = 32UL * 1024 * 1024; | ||
|
|
||
| /** | ||
| * @brief Number of parallel chunks a @p size byte job should be split into: at least one, at most | ||
| * @p max_chunks, targeting roughly one chunk per @p min_chunk bytes. | ||
| */ | ||
| constexpr size_t split_chunk_count(size_t size, size_t min_chunk, size_t max_chunks) noexcept { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets keep as internal util function. |
||
| const size_t by_size = (min_chunk == 0) ? max_chunks : size / min_chunk; | ||
| return std::max<size_t>(1, std::min<size_t>(max_chunks, by_size)); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Open a file for reading and retrieve its size. | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <future> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| #include "openvino/util/memory.hpp" | ||
| #include "openvino/util/mmap_object.hpp" | ||
| #include "openvino/util/parallel_io.hpp" | ||
|
|
||
| namespace ov::util { | ||
|
|
||
| // Touches one byte per page over [m_begin, m_end) to force the pages resident. The volatile | ||
| // accumulator keeps the compiler from eliminating the read loop. | ||
| struct PageToucher { | ||
| const uint8_t* m_begin; | ||
| const uint8_t* m_end; | ||
| const size_t m_page_size; | ||
|
|
||
| void operator()() const noexcept { | ||
| volatile uint8_t local = 0; | ||
| for (auto begin = m_begin; begin < m_end; begin += m_page_size) { | ||
| local += *begin; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| class PrefetchToken; | ||
|
|
||
| /** | ||
| * @brief Pre-fetches a page-aligned, committed VM range into physical memory, blocking until every | ||
| * page is resident. | ||
| * | ||
| * @param ptr Page-aligned base address of the range. | ||
| * @param size Multiple of the system page size. | ||
| * @param num_threads Number of population jobs to split the range into; @c 0 requests only a | ||
| * lightweight advisory OS hint instead of touching pages. | ||
| */ | ||
| void vm_prefetch(void* ptr, size_t size, size_t num_threads) noexcept; | ||
|
|
||
| /** | ||
| * @brief Asynchronous variant of @ref vm_prefetch: submits page-population to the shared pool and | ||
| * returns immediately with a @ref PrefetchToken to wait on. Returns an empty token if the work | ||
| * could not be scheduled. | ||
| */ | ||
| PrefetchToken vm_prefetch_async(void* ptr, size_t size) noexcept; | ||
|
|
||
| /** | ||
| * @brief Move-only RAII handle for background page-population started by @ref vm_prefetch_async. | ||
| * | ||
| * Destruction (or an explicit @ref wait) joins the outstanding work, so nothing is ever left | ||
| * running uncontrolled. The token does not keep the populated memory alive: the caller must keep | ||
| * that memory valid until the token completes, is destroyed, or its futures are @ref detach "detached". | ||
| */ | ||
| class PrefetchToken { | ||
| public: | ||
| PrefetchToken() noexcept = default; | ||
| explicit PrefetchToken(std::vector<std::future<void>>&& tasks) noexcept : m_tasks(std::move(tasks)) {} | ||
|
|
||
| PrefetchToken(const PrefetchToken&) = delete; | ||
| PrefetchToken& operator=(const PrefetchToken&) = delete; | ||
| PrefetchToken(PrefetchToken&&) noexcept = default; | ||
|
|
||
| PrefetchToken& operator=(PrefetchToken&& other) noexcept { | ||
| if (this != &other) { | ||
| wait(); | ||
| m_tasks = std::move(other.m_tasks); | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| ~PrefetchToken() { | ||
| wait(); | ||
| } | ||
|
|
||
| void wait() noexcept { | ||
| for (auto& task : m_tasks) { | ||
| if (task.valid()) { | ||
| task.wait(); | ||
| } | ||
| } | ||
| m_tasks.clear(); | ||
| } | ||
|
|
||
| std::vector<std::future<void>> detach() noexcept { | ||
| auto tasks = std::move(m_tasks); | ||
| m_tasks.clear(); | ||
| return tasks; | ||
| } | ||
|
|
||
| bool valid() const noexcept { | ||
| return !m_tasks.empty(); | ||
| } | ||
|
|
||
| explicit operator bool() const noexcept { | ||
| return valid(); | ||
| } | ||
|
|
||
| private: | ||
| std::vector<std::future<void>> m_tasks; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Clamps [offset, offset + size) to [0, mapping_size) and page-aligns the result. Returns an | ||
| * empty region (m_length == 0) for a null/empty mapping, an offset at or past the end, or a | ||
| * sub-page request. | ||
| */ | ||
| inline AlignedRegion clamp_align_region(const void* data, size_t mapping_size, size_t offset, size_t size) noexcept { | ||
| const auto page_size = static_cast<size_t>(get_system_page_size()); | ||
| if (data == nullptr || mapping_size == 0 || offset >= mapping_size || size < page_size) { | ||
| return {}; | ||
| } | ||
| const auto available = mapping_size - offset; | ||
| const auto raw_len = (size == auto_size) ? available : std::min(size, available); | ||
| return align_region(reinterpret_cast<uintptr_t>(data) + offset, raw_len, page_size); | ||
| } | ||
|
|
||
| /** @brief Aligned region and page-aligned size shared between the sync and async hint_prefetch(). */ | ||
| struct PrefetchPlan { | ||
| uintptr_t m_address = 0; | ||
| size_t m_aligned_size = 0; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Computes the region and page-aligned size for a hint_prefetch()/hint_prefetch_async() | ||
| * call, or an empty plan (m_aligned_size == 0) when the region is below the parallel-I/O threshold | ||
| * (a real population pass would not be worth it). | ||
| */ | ||
| inline PrefetchPlan make_prefetch_plan(const void* data, size_t mapping_size, size_t offset, size_t size) noexcept { | ||
| if (const auto region = clamp_align_region(data, mapping_size, offset, size); | ||
| region.m_length > default_parallel_io_threshold) { | ||
| return {region.m_address, align_size_up(region.m_length, static_cast<size_t>(get_system_page_size()))}; | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| /** @brief Upper bound on the shared page-population pool's worker threads. */ | ||
| inline constexpr size_t max_prefetch_threads = 8; | ||
|
|
||
| /** | ||
| * @brief Number of page-population jobs a @p size byte region is split into, honoring the shared | ||
| * parallel-I/O minimum chunk size and the pool worker cap. | ||
| */ | ||
| inline size_t prefetch_thread_count(size_t size) noexcept { | ||
| const auto pool_cap = | ||
| std::max<size_t>(1, std::min<size_t>(max_prefetch_threads, std::thread::hardware_concurrency())); | ||
| return split_chunk_count(size, default_parallel_io_min_chunk, pool_cap); | ||
| } | ||
|
|
||
| } // namespace ov::util |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Detail:
Just expand line above with PRIVATE section instead add
target_include_directoriesnew entry