-
Notifications
You must be signed in to change notification settings - Fork 2
Adding different types of parallelism to the elementwise layer #222
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
Merged
Merged
Changes from 45 commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
f14195d
1
b19191f
fix
9efc295
Merge branch 'main' into AndreySorokin7/Add_parall_ew_layer
AndreySorokin7 30a33ff
fix
4a3d16e
fix
406387d
fix
6f82796
fix
3cb8263
fix
2d369b6
fix
0412b6a
fix
5521152
fix
4293356
fix
0ba7e1b
fix
f5f0f14
fix
8222f23
fix
0bb0d02
fix
66b3b93
fix
04c3815
fix
7430245
fix
56c6d89
fix
46bfe0b
fix
16e39bf
fix
4eac55a
fix
bbaa426
fix
3d7d56c
fix
4684d9c
fix
0d7e4bf
fix
83732e7
fix
a9dc8c3
fix
02b39ab
fix
5f921f1
fix
4a8c2f5
fix
a453ed9
fix
40a343b
fix
e1a1825
fix
15ee554
fix
a19776c
fix
f15530c
fix
14ab537
Merge branch 'main' into AndreySorokin7/Add_parall_ew_layer
AndreySorokin7 2232cd6
Update test_ewlayer.cpp
AndreySorokin7 e5c56ac
Update backends.hpp
AndreySorokin7 2a3bd2a
Update backends.hpp
AndreySorokin7 971aac0
Update backends.hpp
AndreySorokin7 70c83f1
fix
ba9ea84
fix
23db74a
fix
c656a85
fix
8e1c6f1
fix
0be51fa
fix
7f88c7a
fix
d9f3d13
link_libraries
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
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,120 @@ | ||
| #pragma once | ||
| #include <oneapi/tbb/blocked_range.h> | ||
| #include <oneapi/tbb/info.h> | ||
| #include <oneapi/tbb/parallel_for.h> | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <functional> | ||
| #include <limits> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| namespace it_lab_ai { | ||
| namespace parallel { | ||
|
|
||
| enum class Backend : std::uint8_t { | ||
| kSeq = 0, | ||
| kThreads = 1, | ||
| kTbb = 2, | ||
| kOmp = 3 | ||
| }; | ||
|
|
||
| struct Options { | ||
| Backend backend = Backend::kSeq; | ||
| int max_threads = 0; | ||
| std::size_t min_parallel_n = 1000; | ||
| std::size_t grain = 1024; | ||
| }; | ||
|
|
||
| inline void impl_seq(std::size_t count, | ||
| const std::function<void(std::size_t)>& func) { | ||
| for (std::size_t i = 0; i < count; ++i) { | ||
| func(i); | ||
| } | ||
| } | ||
|
|
||
| inline void impl_threads(std::size_t count, | ||
| const std::function<void(std::size_t)>& func, | ||
| const Options& opt) { | ||
| int num_threads = opt.max_threads > 0 | ||
| ? opt.max_threads | ||
| : static_cast<int>(std::thread::hardware_concurrency()); | ||
| if (num_threads == 0) num_threads = 4; | ||
|
|
||
| std::size_t min_chunk_size = std::max(opt.grain, count / (num_threads * 4)); | ||
| if (count / num_threads < min_chunk_size) { | ||
| num_threads = std::max(1, static_cast<int>(count / min_chunk_size)); | ||
| } | ||
|
|
||
| std::vector<std::thread> threads; | ||
| threads.reserve(num_threads); | ||
|
|
||
| std::size_t chunk_size = count / num_threads; | ||
| std::size_t remainder = count % num_threads; | ||
|
|
||
| std::size_t start = 0; | ||
| for (int t = 0; t < num_threads; ++t) { | ||
| std::size_t end = | ||
| start + chunk_size + (t < static_cast<int>(remainder) ? 1 : 0); | ||
| if (start >= end) break; | ||
|
|
||
| threads.emplace_back([start, end, &func]() { | ||
| for (std::size_t i = start; i < end; ++i) { | ||
| func(i); | ||
| } | ||
| }); | ||
|
|
||
| start = end; | ||
| } | ||
|
|
||
| for (auto& thread : threads) { | ||
| thread.join(); | ||
| } | ||
| } | ||
|
|
||
| inline void impl_tbb(std::size_t count, | ||
| const std::function<void(std::size_t)>& func, | ||
| const Options& opt) { | ||
| oneapi::tbb::parallel_for( | ||
| oneapi::tbb::blocked_range<std::size_t>(0, count, opt.grain), | ||
| [&](const oneapi::tbb::blocked_range<std::size_t>& range) { | ||
| for (std::size_t i = range.begin(); i < range.end(); ++i) { | ||
| func(i); | ||
| } | ||
| }, | ||
| oneapi::tbb::auto_partitioner()); | ||
| } | ||
|
|
||
| #ifdef HAS_OPENMP | ||
| inline void impl_omp(std::size_t count, | ||
| const std::function<void(std::size_t)>& func, | ||
| const Options& opt) { | ||
| if (count == 0) return; | ||
|
|
||
| int num_threads = opt.max_threads > 0 | ||
| ? opt.max_threads | ||
| : static_cast<int>(std::thread::hardware_concurrency()); | ||
|
|
||
| static_cast<void>(std::max(opt.grain, count / (num_threads * 8))); | ||
|
|
||
| int int_count = static_cast<int>(count); | ||
| if (int_count < 0 || static_cast<std::size_t>(int_count) != count) { | ||
| impl_seq(count, func); | ||
| return; | ||
| } | ||
| #pragma omp parallel for schedule(static) num_threads(num_threads) | ||
| for (int i = 0; i < int_count; ++i) { | ||
| func(static_cast<std::size_t>(i)); | ||
| } | ||
| } | ||
| #else | ||
| inline void impl_omp(std::size_t count, | ||
| const std::function<void(std::size_t)>& func, | ||
| const Options& opt) { | ||
| impl_seq(count, func); | ||
| } | ||
| #endif | ||
|
|
||
| } // namespace parallel | ||
| } // namespace it_lab_ai |
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.
Please, revert. Flags are still required
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.
70c83f1
I tried to uncomment them, but then I get errors