Skip to content

Commit 6ee506d

Browse files
committed
std::tread and joint to remove threads to load
This PR fixes load function using std::thread rather than futures. For windows, the threads created by futures are not removed and the total number of threads are increase. We fixes this by using std::thread and join. signed-off-by jijoong.moon<[email protected]>
1 parent b332f3f commit 6ee506d

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

nntrainer/models/neuralnet.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -737,21 +737,23 @@ void NeuralNetwork::load(const std::string &file_path,
737737
NNTR_THROW_IF((model_file_fd == -1), std::invalid_argument)
738738
<< "Cannot open file : " << f_path;
739739
}
740-
std::vector<std::future<void>> futures;
740+
//std::vector<std::future<void>> futures;
741+
std::vector<std::thread> threads;
742+
threads.reserve(model_graph.size());
741743
for (auto iter = model_graph.cbegin(); iter != model_graph.cend();
742744
++iter) {
743745
auto node = *iter;
744746
auto exec_order = std::get<0>((*iter)->getExecutionOrder());
745747

746-
futures.emplace_back(std::async(std::launch::async, [&, node] {
748+
threads.emplace_back([&, node]() {
747749
if (!MMAP_READ) {
748750
auto local_model_file = checkedOpenStream<std::ifstream>(
749751
(v.size() == 2) ? v[1] : v[0], std::ios::in | std::ios::binary);
750752
node->read(local_model_file, false, exec_mode, fsu_mode,
751753
std::numeric_limits<size_t>::max(), true, model_file_fd);
752754
} else {
753755
#if defined(_WIN32)
754-
// Map per-task, then unmap immediately after: enables early release
756+
// Map per-ask, then unmap immediately after: enables early release
755757
// of pages
756758
HANDLE hFile =
757759
CreateFileA(f_path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
@@ -805,11 +807,10 @@ void NeuralNetwork::load(const std::string &file_path,
805807
::munmap(mmap_ptr, f_size);
806808
#endif
807809
}
808-
}));
810+
});
809811
}
810-
811-
for (auto &f : futures)
812-
f.get();
812+
for(auto &t : threads)
813+
t.join();
813814
} else {
814815
for (auto iter = model_graph.cbegin(); iter != model_graph.cend();
815816
++iter) {

0 commit comments

Comments
 (0)