-
Notifications
You must be signed in to change notification settings - Fork 99
std::tread and joint to remove threads to load #3582
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
jijoongmoon
wants to merge
1
commit into
nnstreamer:main
Choose a base branch
from
jijoongmoon:threads
base: main
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.
+37
−31
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -737,21 +737,23 @@ void NeuralNetwork::load(const std::string &file_path, | |
| NNTR_THROW_IF((model_file_fd == -1), std::invalid_argument) | ||
| << "Cannot open file : " << f_path; | ||
| } | ||
| std::vector<std::future<void>> futures; | ||
| // std::vector<std::future<void>> futures; | ||
| std::vector<std::thread> threads; | ||
| threads.reserve(model_graph.size()); | ||
| for (auto iter = model_graph.cbegin(); iter != model_graph.cend(); | ||
| ++iter) { | ||
| auto node = *iter; | ||
| auto exec_order = std::get<0>((*iter)->getExecutionOrder()); | ||
|
|
||
| futures.emplace_back(std::async(std::launch::async, [&, node] { | ||
| threads.emplace_back([&, node]() { | ||
| if (!MMAP_READ) { | ||
| auto local_model_file = checkedOpenStream<std::ifstream>( | ||
| (v.size() == 2) ? v[1] : v[0], std::ios::in | std::ios::binary); | ||
| node->read(local_model_file, false, exec_mode, fsu_mode, | ||
| std::numeric_limits<size_t>::max(), true, model_file_fd); | ||
| } else { | ||
| #if defined(_WIN32) | ||
| // Map per-task, then unmap immediately after: enables early release | ||
| // Map per-ask, then unmap immediately after: enables early release | ||
|
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. Maybe typo? 'per-ask' -> 'per-task' |
||
| // of pages | ||
| HANDLE hFile = | ||
| CreateFileA(f_path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, | ||
|
|
@@ -777,39 +779,43 @@ void NeuralNetwork::load(const std::string &file_path, | |
| CloseHandle(hMap); | ||
| CloseHandle(hFile); | ||
| #else | ||
| // POSIX: map per-task, advise kernel, drop pages, unmap | ||
| int fd = ::open(f_path.c_str(), O_RDONLY); | ||
| NNTR_THROW_IF((fd == -1), std::invalid_argument) | ||
| << "Cannot open file : " << f_path; | ||
|
|
||
| struct stat st {}; | ||
| NNTR_THROW_IF((::fstat(fd, &st) == -1), std::invalid_argument) | ||
| << "Cannot get file info (fstat): " << f_path; | ||
|
|
||
| size_t f_size = static_cast<size_t>(st.st_size); | ||
| void *mmap_ptr = ::mmap(nullptr, f_size, PROT_READ, MAP_PRIVATE, fd, 0); | ||
| ::close(fd); // fd not needed after mmap | ||
| NNTR_THROW_IF((mmap_ptr == MAP_FAILED), std::runtime_error) | ||
| << "mmap failed"; | ||
|
|
||
| // Hint: many model loads touch scattered regions -> RANDOM helps reduce readahead | ||
| (void)::posix_madvise(mmap_ptr, f_size, POSIX_MADV_RANDOM); | ||
|
|
||
| char *view = static_cast<char *>(mmap_ptr); | ||
| node->read(view, false, exec_mode, fsu_mode, | ||
| std::numeric_limits<size_t>::max(), true); | ||
| // POSIX: map per-task, advise kernel, drop pages, unmap | ||
| int fd = ::open(f_path.c_str(), O_RDONLY); | ||
| NNTR_THROW_IF((fd == -1), std::invalid_argument) | ||
| << "Cannot open file : " << f_path; | ||
|
|
||
| struct stat st {}; | ||
| NNTR_THROW_IF((::fstat(fd, &st) == -1), std::invalid_argument) | ||
| << "Cannot get file info (fstat): " << f_path; | ||
|
|
||
| size_t f_size = static_cast<size_t>(st.st_size); | ||
| void *mmap_ptr = | ||
| ::mmap(nullptr, f_size, PROT_READ, MAP_PRIVATE, fd, 0); | ||
| ::close(fd); // fd not needed after mmap | ||
| NNTR_THROW_IF((mmap_ptr == MAP_FAILED), std::runtime_error) | ||
| << "mmap failed"; | ||
|
|
||
| // Hint: many model loads touch scattered regions -> RANDOM helps | ||
| // reduce readahead | ||
| (void)::posix_madvise(mmap_ptr, f_size, POSIX_MADV_RANDOM); | ||
|
|
||
| char *view = static_cast<char *>(mmap_ptr); | ||
| node->read(view, false, exec_mode, fsu_mode, | ||
| std::numeric_limits<size_t>::max(), true); | ||
|
|
||
| // Early drop: pages no longer needed; helps lower peak RSS during overlap | ||
| (void)::posix_madvise(mmap_ptr, f_size, POSIX_MADV_DONTNEED); | ||
| // Early drop: pages no longer needed; helps lower peak RSS during | ||
| // overlap | ||
| (void)::posix_madvise(mmap_ptr, f_size, POSIX_MADV_DONTNEED); | ||
|
|
||
| ::munmap(mmap_ptr, f_size); | ||
| ::munmap(mmap_ptr, f_size); | ||
| #endif | ||
| } | ||
| })); | ||
| }); | ||
| } | ||
| for (auto &t : threads) { | ||
| if (t.joinable()) | ||
| t.join(); | ||
| } | ||
|
|
||
| for (auto &f : futures) | ||
| f.get(); | ||
| } else { | ||
| for (auto iter = model_graph.cbegin(); iter != model_graph.cend(); | ||
| ++iter) { | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.