-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
PoC: For Each (Range) Index #552
Draft
samayala22
wants to merge
3
commits into
taskflow:dev
Choose a base branch
from
samayala22:range
base: dev
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -75,36 +75,50 @@ TF_FORCE_INLINE auto make_for_each_task(B b, E e, C c, P part = P()) { | |||||
}; | ||||||
} | ||||||
|
||||||
template<typename T, typename C> | ||||||
using is_index_func = std::is_invocable_r<void, C, T>; | ||||||
|
||||||
template<typename T, typename C> | ||||||
using is_range_func = std::is_invocable_r<void, C, T, T>; | ||||||
|
||||||
// Function: make_for_each_index_task | ||||||
template <typename B, typename E, typename S, typename C, typename P = DefaultPartitioner> | ||||||
TF_FORCE_INLINE auto make_for_each_index_task(B b, E e, S s, C c, P part = P()){ | ||||||
template <typename T, typename C, typename P = DefaultPartitioner> | ||||||
TF_FORCE_INLINE auto make_for_each_index_task(T b, T e, C c, P part = P()){ | ||||||
using T_t = std::decay_t<unwrap_ref_decay_t<T>>; | ||||||
|
||||||
static_assert(std::is_integral<T_t>::value, "Begin and end values must be an integral type."); | ||||||
static_assert( | ||||||
std::disjunction<is_index_func<T_t, C>, is_range_func<T_t, C>>::value, | ||||||
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. This might be easier to read as follows :-)
Suggested change
|
||||||
"C must be either a void function taking one int or two ints" | ||||||
); | ||||||
constexpr bool is_index_callable = is_index_func<T, C>::value; | ||||||
|
||||||
using namespace std::string_literals; | ||||||
|
||||||
using B_t = std::decay_t<unwrap_ref_decay_t<B>>; | ||||||
using E_t = std::decay_t<unwrap_ref_decay_t<E>>; | ||||||
using S_t = std::decay_t<unwrap_ref_decay_t<S>>; | ||||||
|
||||||
return [=] (Runtime& rt) mutable { | ||||||
|
||||||
// fetch the iterator values | ||||||
B_t beg = b; | ||||||
E_t end = e; | ||||||
S_t inc = s; | ||||||
T_t beg = b; | ||||||
T_t end = e; | ||||||
|
||||||
// nothing to be done if the range is invalid | ||||||
if(is_range_invalid(beg, end, inc)) { | ||||||
if(is_range_invalid(beg, end, 1)) { | ||||||
return; | ||||||
} | ||||||
|
||||||
size_t W = rt.executor().num_workers(); | ||||||
size_t N = distance(beg, end, inc); | ||||||
T_t n = end-beg; | ||||||
size_t N = static_cast<size_t>(n); | ||||||
|
||||||
// only myself - no need to spawn another graph | ||||||
if(W <= 1 || N <= part.chunk_size()) { | ||||||
TF_MAKE_LOOP_TASK( | ||||||
for(size_t x=0; x<N; x++, beg+=inc) { | ||||||
c(beg); | ||||||
if constexpr(is_index_callable) { | ||||||
for(T_t i=0; i<n; i++) { | ||||||
c(i); | ||||||
} | ||||||
} else { | ||||||
c(0, n); | ||||||
} | ||||||
); | ||||||
return; | ||||||
|
@@ -123,9 +137,12 @@ TF_FORCE_INLINE auto make_for_each_index_task(B b, E e, S s, C c, P part = P()){ | |||||
TF_MAKE_LOOP_TASK( | ||||||
part.loop(N, W, curr_b, chunk_size, | ||||||
[&](size_t part_b, size_t part_e) { | ||||||
auto idx = static_cast<B_t>(part_b) * inc + beg; | ||||||
for(size_t x=part_b; x<part_e; x++, idx += inc) { | ||||||
c(idx); | ||||||
if constexpr(is_index_callable) { | ||||||
for(size_t i=part_b; i<part_e; i++) { | ||||||
c(static_cast<T_t>(i)); | ||||||
} | ||||||
} else { | ||||||
c(static_cast<T_t>(part_b), static_cast<T_t>(part_e)); | ||||||
} | ||||||
} | ||||||
); | ||||||
|
@@ -142,9 +159,12 @@ TF_FORCE_INLINE auto make_for_each_index_task(B b, E e, S s, C c, P part = P()){ | |||||
TF_MAKE_LOOP_TASK( | ||||||
part.loop(N, W, next, | ||||||
[&](size_t part_b, size_t part_e) { | ||||||
auto idx = static_cast<B_t>(part_b) * inc + beg; | ||||||
for(size_t x=part_b; x<part_e; x++, idx += inc) { | ||||||
c(idx); | ||||||
if constexpr(is_index_callable) { | ||||||
for(size_t i=part_b; i<part_e; i++) { | ||||||
c(static_cast<T_t>(i)); | ||||||
} | ||||||
} else { | ||||||
c(static_cast<T_t>(part_b), static_cast<T_t>(part_e)); | ||||||
} | ||||||
} | ||||||
); | ||||||
|
@@ -171,13 +191,12 @@ Task FlowBuilder::for_each(B beg, E end, C c, P part) { | |||||
// ---------------------------------------------------------------------------- | ||||||
|
||||||
// Function: for_each_index | ||||||
template <typename B, typename E, typename S, typename C, typename P> | ||||||
Task FlowBuilder::for_each_index(B beg, E end, S inc, C c, P part){ | ||||||
template <typename T, typename C, typename P> | ||||||
Task FlowBuilder::for_each_index(T beg, T end, C c, P part){ | ||||||
return emplace( | ||||||
make_for_each_index_task(beg, end, inc, c, part) | ||||||
make_for_each_index_task(beg, end, c, part) | ||||||
); | ||||||
} | ||||||
|
||||||
|
||||||
} // end of namespace tf ----------------------------------------------------- | ||||||
|
This file contains 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
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.
The comment in the PR is specifically about this part of the patch. The begin and end iterators,
b
ande
may sometimes be of different types and I think it is useful to retain the ability to have different types as was there before.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.
Separately, though, I think it is important to keep the original interface for backward compatibility. Perhaps you want to overload the function by providing a second version that takes the two-argument function (instead of modifying the existing function)? You can use
std::enable_if_t
to ensure the right one gets called.