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

[lang] Return None when parent() exceeds its maximum depth #1319

Merged
merged 4 commits into from
Jun 27, 2020

Conversation

k-ye
Copy link
Member

@k-ye k-ye commented Jun 24, 2020

Seems like https://github.com/taichi-dev/taichi/pull/1214/files#r440768343 wasn't addressed. I'd suggest us not to make root circular and keep the parent chain linear. Following parent until null is a fairly common op, e.g.

auto leaf = for_stmt->snode;
// make a list of nodes, from the leaf block (instead of 'place') to root
std::vector<SNode *> path;
// leaf is the place (scalar)
// leaf->parent is the leaf block
// so listgen should be invoked from the root to leaf->parent
for (auto p = leaf; p; p = p->parent) {
path.push_back(p);
}

This PR also handles the case where n exceeds the hierarchy depth.

Related issue = #1214

[Click here for the format server]


@k-ye k-ye requested review from archibate and yuanming-hu June 24, 2020 10:15
@codecov
Copy link

codecov bot commented Jun 24, 2020

Codecov Report

Merging #1319 into master will increase coverage by 0.08%.
The diff coverage is 90.24%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1319      +/-   ##
==========================================
+ Coverage   66.41%   66.49%   +0.08%     
==========================================
  Files          37       37              
  Lines        5133     5146      +13     
  Branches      931      932       +1     
==========================================
+ Hits         3409     3422      +13     
  Misses       1562     1562              
  Partials      162      162              
Impacted Files Coverage Δ
python/taichi/lang/transformer.py 93.63% <ø> (ø)
python/taichi/misc/gui.py 24.65% <0.00%> (ø)
python/taichi/lang/matrix.py 90.70% <88.88%> (+0.03%) ⬆️
python/taichi/lang/expr.py 89.82% <91.66%> (ø)
python/taichi/lang/snode.py 87.50% <94.73%> (+1.47%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 477b1e1...e6bc6db. Read the comment docs.

Copy link
Collaborator

@archibate archibate left a comment

Choose a reason for hiding this comment

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

Took a quick look, lgtmig!

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.

Thank you! LGTM.

@yuanming-hu
Copy link
Member

Offtopic discussion: maybe we should make ti.root a real SNode instead of ti.Root?

@archibate
Copy link
Collaborator

Offtopic discussion: maybe we should make ti.root a real SNode instead of ti.Root?

Agree.
If make ti.root a SNode too hard for you, you may at least make SNode a base class of Root.

@k-ye
Copy link
Member Author

k-ye commented Jun 25, 2020

Offtopic discussion: maybe we should make ti.root a real SNode instead of ti.Root?

Actually, probably not. I think root was implemented this way to do lazy-evaluation, see

def __getattribute__(self, item):
import taichi as ti
ti.get_runtime().create_program()
root = SNode(ti.get_runtime().prog.get_root())

While we can say one should only access ti.root after ti.init(), we have to define root before ti.init(). This initialization requires one level of indirection.. OTOH, changing to ti.root() may solve the problem.

As a side effect, I found it useless to define parent() on root, because it's not called anyway.

@yuanming-hu
Copy link
Member

Offtopic discussion: maybe we should make ti.root a real SNode instead of ti.Root?

Actually, probably not. I think root was implemented this way to do lazy-evaluation, see

def __getattribute__(self, item):
import taichi as ti
ti.get_runtime().create_program()
root = SNode(ti.get_runtime().prog.get_root())

While we can say one should only access ti.root after ti.init(), we have to define root before ti.init(). This initialization requires one level of indirection.. OTOH, changing to ti.root() may solve the problem.

As a side effect, I found it useless to define parent() on root, because it's not called anyway.

Good point. How about just setting ti.root in ti.init()? We are enforcing users to call ti.init in the very beginning of every taichi program anyway. It's fine to let ti.root be attribute not found before ti.init.

@k-ye
Copy link
Member Author

k-ye commented Jun 26, 2020

Allow me to merge this one first. I've encountered some problems when modifying global root in the module level. Seems like I've encountered the same problem in https://stackoverflow.com/questions/3536620/how-to-change-a-module-variable-from-another-module.

@yuanming-hu
Copy link
Member

Allow me to merge this one first. I've encountered some problems when modifying global root in the module level. Seems like I've encountered the same problem in https://stackoverflow.com/questions/3536620/how-to-change-a-module-variable-from-another-module.

Ah you are right... I think that's the reason why I did a Root class a few months ago... (Sorry about my terrible memory...)

@k-ye k-ye merged commit 32e68ab into taichi-dev:master Jun 27, 2020
@k-ye k-ye deleted the par branch June 27, 2020 02:59
@yuanming-hu yuanming-hu mentioned this pull request Jun 28, 2020
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.

3 participants