Skip to content
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

[async] Add customized container for async state to node map #2070

Merged
merged 1 commit into from
Dec 1, 2020

Conversation

k-ye
Copy link
Member

@k-ye k-ye commented Nov 29, 2020

Add a customized StateToNodesMap data structure for fast SFG edge insertion and lookup.

This PR probably would make the performance even worse for now, because deduping during the edge insertion is slow..

[Click here for the format server]


@k-ye k-ye requested a review from yuanming-hu November 29, 2020 16:41
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the delayed reply. The interface part looks great! I was actually expecting NewStateToNodesMap to be a light-weight wrapper of the original llvm::SmallVector<std::pair<AsyncState, llvm::SmallSet<Node *, 8>>, 4> structure at the first step, and then we discuss how to implement these APIs efficiently on our own, to replace the llvm containers.

Sorry about not stating the plan clearly, but the current implementation of NewStateToNodesMap seems to be on the right track already! I think it's perfectly fine to split this migration into multiple PRs. Let me know when you are free for a chat on the design of the data structure internals, and I'll be happy to contribute to performance tuning!

(It's also nice that many edge operations are now much simpler with the new API!)

Copy link
Member Author

@k-ye k-ye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually expecting NewStateToNodesMap to be a light-weight wrapper.

Yeah, I thought about that. But I was too lazy to create a fully customized iterator type with a proper abstraction, and just borrowed the underlying container's. If we just wrap the existing container, we probably still need to deal with a large amount of refactoring due to the iterator type change..

Let me know when you are free for a chat on the design of the data structure internals, and I'll be happy to contribute to performance tuning!

Awesome! I think this PR should lay a good foundation for switching to mask, or other more efficient implementation. Note that once we make the switch and add a sort stage, many methods here should just assume that data_ is already sorted, instead of calling maybe_sort() on their own.

taichi/program/state_flow_graph.cpp Show resolved Hide resolved
taichi/program/state_flow_graph.cpp Show resolved Hide resolved
@k-ye k-ye requested a review from taichi-gardener November 30, 2020 13:25
@k-ye k-ye marked this pull request as ready for review November 30, 2020 13:38
taichi/program/state_flow_graph.h Show resolved Hide resolved
void StateToNodesMap::insert_edge(const AsyncState &as, Node *n) {
TI_ASSERT(!sorted_);
const Edge e = std::make_pair(as, n);
if (has_edge(e))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be problematic if has_edge is supposed to be used only after sorting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, hopefully, once we have the mask, most has_edge queries can quickly return false.

@k-ye k-ye requested a review from yuanming-hu November 30, 2020 13:38
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I took a pass and now I have a rough feeling on the use cases and the current internals of the customized container. A few things to discuss tomorrow:

  • We may need to codesign the SFG optimizations and the customized container for performance and simplicity. For example, is disconnect_all (for node deletion) really necessary? It seems that if we make rebuild_graph fast, we can simply remove the node and then rebuild the graph once we need to delete nodes. Then we no longer need to support the remove_node function in StateToNodesMap.
  • How to deal with LatestStateReaders
  • High-performance implementation of has_edge (probably need to tune on site after this PR)
  • ...

}
void StateToNodesMap::clear() {
sorted_ = false;
// It seems that once we are beyond the small buffer threshold, there's no
Copy link
Member

@yuanming-hu yuanming-hu Nov 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine :-) IIRC this is only used on the initial_node_ - if that is the case we can directly re-initialize the whole container.

void StateToNodesMap::insert_edge(const AsyncState &as, Node *n) {
TI_ASSERT(!sorted_);
const Edge e = std::make_pair(as, n);
if (has_edge(e))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, hopefully, once we have the mask, most has_edge queries can quickly return false.

Comment on lines +98 to +101
// This should be used only after |data_| is sorted.
// 1) disconnect_with
// 2) fuse_range
// So adding the mask shouldn't affect this implementation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't seem to be called very frequently. We can likely use some correct but slow solution.

Comment on lines +113 to +134
bool StateToNodesMap::replace_node_in_edge(const AsyncState &as,
Node *old_nd,
Node *new_nd) {
maybe_sort();

const Edge oe = std::make_pair(as, old_nd);
auto iter = std::lower_bound(data_.begin(), data_.end(), oe);
if (!matches(iter, oe)) {
return false;
}
if (has_edge(as, new_nd)) {
data_.erase(iter);
return true;
}
iter->second = new_nd;

// TODO: Is getting the range an overkill? Just sort the entire data range?
auto rn = (*this)[as];
TI_ASSERT(rn.begin() <= iter && iter < rn.end());
std::sort(rn.begin(), rn.end());
return true;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is troublesome, we may consider not supporting this API at all.

@k-ye k-ye merged commit af73ddd into taichi-dev:master Dec 1, 2020
k-ye added a commit that referenced this pull request Dec 1, 2020
This reverts commit af73ddd.
k-ye added a commit that referenced this pull request Dec 1, 2020
k-ye added a commit that referenced this pull request Dec 1, 2020
k-ye added a commit that referenced this pull request Dec 2, 2020
@k-ye k-ye mentioned this pull request Dec 13, 2020
@k-ye k-ye deleted the refactor branch December 20, 2020 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants