{"action":"created","comment":{"url":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/comments/102118944","pull_request_review_id":22864561,"id":102118944,"diff_hunk":"@@ -413,3 +480,565 @@ def aggressive_unroll(expression):\n     expression, = unroll_indexsum((expression,), max_extent=numpy.inf)\n     expression, = remove_componenttensors((expression,))\n     return expression\n+\n+\n+# ---------------------------------------\n+# Count flop of expression, \"as it is\", no reordering etc\n+@singledispatch\n+def _count_flop(node, self):\n+    raise AssertionError(\"cannot handle type %s\" % type(node))\n+\n+\n+@_count_flop.register(IndexSum)\n+def _count_flop_single(node, self):\n+    return self(node.children[0])\n+\n+\n+@_count_flop.register(MathFunction)\n+@_count_flop.register(LogicalNot)\n+@_count_flop.register(LogicalAnd)\n+@_count_flop.register(LogicalOr)\n+def _count_flop_func(node, self):\n+    return self(node.children[0]) + 1\n+\n+\n+@_count_flop.register(Conditional)  # this is not quite right\n+@_count_flop.register(Power)\n+@_count_flop.register(Comparison)\n+@_count_flop.register(Sum)\n+@_count_flop.register(Product)\n+@_count_flop.register(Division)\n+def _count_flop_common(node, self):\n+    flop = numpy.product([i.extent for i in node.free_indices])\n+    # Hoisting the factors\n+    for child in node.children:\n+        flop += self(child)\n+    return flop\n+\n+\n+@_count_flop.register(Constant)\n+@_count_flop.register(Terminal)\n+@_count_flop.register(Indexed)\n+@_count_flop.register(FlexiblyIndexed)\n+def _count_flop_const(node, self):\n+    return 0\n+\n+\n+def count_flop(node):\n+    mapper = Memoizer_cf(_count_flop)\n+    return mapper(node)\n+\n+\n+@singledispatch\n+def _expand_all_product(node, self, index):\n+    # ---------------------------------------\n+    # Expand all products recursively if free index of node include index\n+    # from :param:`index`\n+    # e.g (a+(b+c)d)e = ae + bde + cde\n+\n+    raise AssertionError(\"cannot handle type %s\" % type(node))\n+\n+\n+_expand_all_product.register(Node)(reuse_if_untouched_arg)\n+\n+\n+@_expand_all_product.register(Product)\n+def _expand_all_product_common(node, self, index):\n+    a = self(node.children[0], index)\n+    b = self(node.children[1], index)\n+    if isinstance(b, Sum) and any([i in b.free_indices for i in index]):\n+        return Sum(self(Product(a, b.children[0]), index),\n+                   self(Product(a, b.children[1]), index))\n+    elif isinstance(a, Sum) and any([i in a.free_indices for i in index]):\n+        return Sum(self(Product(a.children[0], b), index),\n+                   self(Product(a.children[1], b), index))\n+    else:\n+        return node\n+\n+\n+def expand_all_product(node, index):\n+    mapper = MemoizerArg(_expand_all_product)\n+    return mapper(node, index)\n+\n+\n+def collect_terms(node, node_type):\n+    \"\"\"Recursively collect all children into a list from :param:`node`\n+    and its children of class :param:`node_type`.\n+\n+    :param node: root of expression\n+    :param node_type: class of node (e.g. Sum or Product)\n+    :return: list of all terms\n+    \"\"\"\n+    from collections import deque\n+    terms = []  # collected terms\n+    queue = deque([node])  # queue of children nodes to process","path":"gem/optimise.py","position":183,"original_position":186,"commit_id":"8e057397856ecb2883f2eccfe6bed43eabe5153a","original_commit_id":"e1348336d7f7e1cf0bd9fd82857059d806abfa9d","user":{"login":"tj-sun","id":9328006,"avatar_url":"https://avatars.githubusercontent.com/u/9328006?v=3","gravatar_id":"","url":"https://api.github.com/users/tj-sun","html_url":"https://github.com/tj-sun","followers_url":"https://api.github.com/users/tj-sun/followers","following_url":"https://api.github.com/users/tj-sun/following{/other_user}","gists_url":"https://api.github.com/users/tj-sun/gists{/gist_id}","starred_url":"https://api.github.com/users/tj-sun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tj-sun/subscriptions","organizations_url":"https://api.github.com/users/tj-sun/orgs","repos_url":"https://api.github.com/users/tj-sun/repos","events_url":"https://api.github.com/users/tj-sun/events{/privacy}","received_events_url":"https://api.github.com/users/tj-sun/received_events","type":"User","site_admin":false},"body":"Amended.\r\nSo I'm processing the queue from right to left now (should have the same results as before)","created_at":"2017-02-21T01:55:05Z","updated_at":"2017-02-21T01:55:05Z","html_url":"https://github.com/firedrakeproject/tsfc/pull/98#discussion_r102118944","pull_request_url":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/98","_links":{"self":{"href":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/comments/102118944"},"html":{"href":"https://github.com/firedrakeproject/tsfc/pull/98#discussion_r102118944"},"pull_request":{"href":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/98"}}},"pull_request":{"url":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/98","id":106887629,"html_url":"https://github.com/firedrakeproject/tsfc/pull/98","diff_url":"https://github.com/firedrakeproject/tsfc/pull/98.diff","patch_url":"https://github.com/firedrakeproject/tsfc/pull/98.patch","issue_url":"https://api.github.com/repos/firedrakeproject/tsfc/issues/98","number":98,"state":"open","locked":false,"title":"Coffee migration","user":{"login":"tj-sun","id":9328006,"avatar_url":"https://avatars.githubusercontent.com/u/9328006?v=3","gravatar_id":"","url":"https://api.github.com/users/tj-sun","html_url":"https://github.com/tj-sun","followers_url":"https://api.github.com/users/tj-sun/followers","following_url":"https://api.github.com/users/tj-sun/following{/other_user}","gists_url":"https://api.github.com/users/tj-sun/gists{/gist_id}","starred_url":"https://api.github.com/users/tj-sun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tj-sun/subscriptions","organizations_url":"https://api.github.com/users/tj-sun/orgs","repos_url":"https://api.github.com/users/tj-sun/repos","events_url":"https://api.github.com/users/tj-sun/events{/privacy}","received_events_url":"https://api.github.com/users/tj-sun/received_events","type":"User","site_admin":false},"body":"This commit migrates loop optimisations in coffee to gem.\r\n\r\n### Test if functionalities are migrated\r\n\r\nResults of flops comparison are in the attached file result.txt (actually it is a csv file, which cannot be uploaded).\r\n\r\nThe result shows that gem.optimise() matches or betters coffee level 2 by and large. It also demonstrates that coffee level 2 cannot further optimise ASTs generated by gem.optimise(). There are some cases for which gem.optimise() are slightly worse. These can be solved by checking if factorisation is profitable at each step. As it tends to only affect simple forms with small number of flops, and the differences are not large, I have commented out the code for now. Will revisit this later.\r\n\r\n### Major problem\r\npre-evaluations of tensors reduces numerical precision in some cases, as a result, these tests fails in the firedrake test suits\r\n\r\nregression/test_geometric_strong_bcs.py::test_dg_advection[left-1-False]\r\nregression/test_hybridisation.py::test_hybridisation[2]\r\nregression/test_manifolds.py::test_manifold_parallel\r\nregression/test_poisson_strong_bcs.py\r\nregression/test_poisson_strong_bcs_nitsche.py::test_poisson_nitsche[1-False]\r\nregression/test_python_parloop.py::test_python_parloop_vector\r\nregression/test_slate_hybridization.py::test_slate_hybridization[2]\r\n\r\nIn addition, several others which use errornorm() fails due to taking square root of (tiny) negative numbers. Not sure if these can be eliminated by some clever tricks in choosing an order to contract the tensors, or purely bad luck with floating point numbers.\r\n\r\n### Minor problems:\r\n\r\n- Performance can be improved further. Currently 3D hyperelasticity takes ~20 seconds. There are significant scope for code optimisation to get that time down, such as reusing the internal data structure of list of factors.\r\n- Several subtle cases to refine further, e.g. Power nodes are not analysed yet (might be beneficial to expand the product).\r\n- Pre-evaluation only works when the inner IndexSum is expanded already.\r\n- latex printing not completed yet.\r\n\r\n\r\n[result.txt](https://github.com/firedrakeproject/tsfc/files/785516/result.txt)\r\n","created_at":"2017-02-19T04:01:43Z","updated_at":"2017-02-21T01:55:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"dd2c7e473b174971a915f32bedca5751f5986e18","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/98/commits","review_comments_url":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/98/comments","review_comment_url":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/comments{/number}","comments_url":"https://api.github.com/repos/firedrakeproject/tsfc/issues/98/comments","statuses_url":"https://api.github.com/repos/firedrakeproject/tsfc/statuses/8e057397856ecb2883f2eccfe6bed43eabe5153a","head":{"label":"firedrakeproject:coffee-migration","ref":"coffee-migration","sha":"8e057397856ecb2883f2eccfe6bed43eabe5153a","user":{"login":"firedrakeproject","id":5558005,"avatar_url":"https://avatars.githubusercontent.com/u/5558005?v=3","gravatar_id":"","url":"https://api.github.com/users/firedrakeproject","html_url":"https://github.com/firedrakeproject","followers_url":"https://api.github.com/users/firedrakeproject/followers","following_url":"https://api.github.com/users/firedrakeproject/following{/other_user}","gists_url":"https://api.github.com/users/firedrakeproject/gists{/gist_id}","starred_url":"https://api.github.com/users/firedrakeproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/firedrakeproject/subscriptions","organizations_url":"https://api.github.com/users/firedrakeproject/orgs","repos_url":"https://api.github.com/users/firedrakeproject/repos","events_url":"https://api.github.com/users/firedrakeproject/events{/privacy}","received_events_url":"https://api.github.com/users/firedrakeproject/received_events","type":"Organization","site_admin":false},"repo":{"id":50843402,"name":"tsfc","full_name":"firedrakeproject/tsfc","owner":{"login":"firedrakeproject","id":5558005,"avatar_url":"https://avatars.githubusercontent.com/u/5558005?v=3","gravatar_id":"","url":"https://api.github.com/users/firedrakeproject","html_url":"https://github.com/firedrakeproject","followers_url":"https://api.github.com/users/firedrakeproject/followers","following_url":"https://api.github.com/users/firedrakeproject/following{/other_user}","gists_url":"https://api.github.com/users/firedrakeproject/gists{/gist_id}","starred_url":"https://api.github.com/users/firedrakeproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/firedrakeproject/subscriptions","organizations_url":"https://api.github.com/users/firedrakeproject/orgs","repos_url":"https://api.github.com/users/firedrakeproject/repos","events_url":"https://api.github.com/users/firedrakeproject/events{/privacy}","received_events_url":"https://api.github.com/users/firedrakeproject/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/firedrakeproject/tsfc","description":"Two-stage form compiler","fork":false,"url":"https://api.github.com/repos/firedrakeproject/tsfc","forks_url":"https://api.github.com/repos/firedrakeproject/tsfc/forks","keys_url":"https://api.github.com/repos/firedrakeproject/tsfc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/firedrakeproject/tsfc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/firedrakeproject/tsfc/teams","hooks_url":"https://api.github.com/repos/firedrakeproject/tsfc/hooks","issue_events_url":"https://api.github.com/repos/firedrakeproject/tsfc/issues/events{/number}","events_url":"https://api.github.com/repos/firedrakeproject/tsfc/events","assignees_url":"https://api.github.com/repos/firedrakeproject/tsfc/assignees{/user}","branches_url":"https://api.github.com/repos/firedrakeproject/tsfc/branches{/branch}","tags_url":"https://api.github.com/repos/firedrakeproject/tsfc/tags","blobs_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/refs{/sha}","trees_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/firedrakeproject/tsfc/statuses/{sha}","languages_url":"https://api.github.com/repos/firedrakeproject/tsfc/languages","stargazers_url":"https://api.github.com/repos/firedrakeproject/tsfc/stargazers","contributors_url":"https://api.github.com/repos/firedrakeproject/tsfc/contributors","subscribers_url":"https://api.github.com/repos/firedrakeproject/tsfc/subscribers","subscription_url":"https://api.github.com/repos/firedrakeproject/tsfc/subscription","commits_url":"https://api.github.com/repos/firedrakeproject/tsfc/commits{/sha}","git_commits_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/commits{/sha}","comments_url":"https://api.github.com/repos/firedrakeproject/tsfc/comments{/number}","issue_comment_url":"https://api.github.com/repos/firedrakeproject/tsfc/issues/comments{/number}","contents_url":"https://api.github.com/repos/firedrakeproject/tsfc/contents/{+path}","compare_url":"https://api.github.com/repos/firedrakeproject/tsfc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/firedrakeproject/tsfc/merges","archive_url":"https://api.github.com/repos/firedrakeproject/tsfc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/firedrakeproject/tsfc/downloads","issues_url":"https://api.github.com/repos/firedrakeproject/tsfc/issues{/number}","pulls_url":"https://api.github.com/repos/firedrakeproject/tsfc/pulls{/number}","milestones_url":"https://api.github.com/repos/firedrakeproject/tsfc/milestones{/number}","notifications_url":"https://api.github.com/repos/firedrakeproject/tsfc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/firedrakeproject/tsfc/labels{/name}","releases_url":"https://api.github.com/repos/firedrakeproject/tsfc/releases{/id}","deployments_url":"https://api.github.com/repos/firedrakeproject/tsfc/deployments","created_at":"2016-02-01T14:07:32Z","updated_at":"2016-11-04T12:54:29Z","pushed_at":"2017-02-21T01:47:21Z","git_url":"git://github.com/firedrakeproject/tsfc.git","ssh_url":"git@github.com:firedrakeproject/tsfc.git","clone_url":"https://github.com/firedrakeproject/tsfc.git","svn_url":"https://github.com/firedrakeproject/tsfc","homepage":null,"size":655,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":7,"forks":2,"open_issues":7,"watchers":1,"default_branch":"master"}},"base":{"label":"firedrakeproject:master","ref":"master","sha":"15160e6565f78683e90bbf3e309ab0b6660fae1a","user":{"login":"firedrakeproject","id":5558005,"avatar_url":"https://avatars.githubusercontent.com/u/5558005?v=3","gravatar_id":"","url":"https://api.github.com/users/firedrakeproject","html_url":"https://github.com/firedrakeproject","followers_url":"https://api.github.com/users/firedrakeproject/followers","following_url":"https://api.github.com/users/firedrakeproject/following{/other_user}","gists_url":"https://api.github.com/users/firedrakeproject/gists{/gist_id}","starred_url":"https://api.github.com/users/firedrakeproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/firedrakeproject/subscriptions","organizations_url":"https://api.github.com/users/firedrakeproject/orgs","repos_url":"https://api.github.com/users/firedrakeproject/repos","events_url":"https://api.github.com/users/firedrakeproject/events{/privacy}","received_events_url":"https://api.github.com/users/firedrakeproject/received_events","type":"Organization","site_admin":false},"repo":{"id":50843402,"name":"tsfc","full_name":"firedrakeproject/tsfc","owner":{"login":"firedrakeproject","id":5558005,"avatar_url":"https://avatars.githubusercontent.com/u/5558005?v=3","gravatar_id":"","url":"https://api.github.com/users/firedrakeproject","html_url":"https://github.com/firedrakeproject","followers_url":"https://api.github.com/users/firedrakeproject/followers","following_url":"https://api.github.com/users/firedrakeproject/following{/other_user}","gists_url":"https://api.github.com/users/firedrakeproject/gists{/gist_id}","starred_url":"https://api.github.com/users/firedrakeproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/firedrakeproject/subscriptions","organizations_url":"https://api.github.com/users/firedrakeproject/orgs","repos_url":"https://api.github.com/users/firedrakeproject/repos","events_url":"https://api.github.com/users/firedrakeproject/events{/privacy}","received_events_url":"https://api.github.com/users/firedrakeproject/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/firedrakeproject/tsfc","description":"Two-stage form compiler","fork":false,"url":"https://api.github.com/repos/firedrakeproject/tsfc","forks_url":"https://api.github.com/repos/firedrakeproject/tsfc/forks","keys_url":"https://api.github.com/repos/firedrakeproject/tsfc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/firedrakeproject/tsfc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/firedrakeproject/tsfc/teams","hooks_url":"https://api.github.com/repos/firedrakeproject/tsfc/hooks","issue_events_url":"https://api.github.com/repos/firedrakeproject/tsfc/issues/events{/number}","events_url":"https://api.github.com/repos/firedrakeproject/tsfc/events","assignees_url":"https://api.github.com/repos/firedrakeproject/tsfc/assignees{/user}","branches_url":"https://api.github.com/repos/firedrakeproject/tsfc/branches{/branch}","tags_url":"https://api.github.com/repos/firedrakeproject/tsfc/tags","blobs_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/refs{/sha}","trees_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/firedrakeproject/tsfc/statuses/{sha}","languages_url":"https://api.github.com/repos/firedrakeproject/tsfc/languages","stargazers_url":"https://api.github.com/repos/firedrakeproject/tsfc/stargazers","contributors_url":"https://api.github.com/repos/firedrakeproject/tsfc/contributors","subscribers_url":"https://api.github.com/repos/firedrakeproject/tsfc/subscribers","subscription_url":"https://api.github.com/repos/firedrakeproject/tsfc/subscription","commits_url":"https://api.github.com/repos/firedrakeproject/tsfc/commits{/sha}","git_commits_url":"https://api.github.com/repos/firedrakeproject/tsfc/git/commits{/sha}","comments_url":"https://api.github.com/repos/firedrakeproject/tsfc/comments{/number}","issue_comment_url":"https://api.github.com/repos/firedrakeproject/tsfc/issues/comments{/number}","contents_url":"https://api.github.com/repos/firedrakeproject/tsfc/contents/{+path}","compare_url":"https://api.github.com/repos/firedrakeproject/tsfc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/firedrakeproject/tsfc/merges","archive_url":"https://api.github.com/repos/firedrakeproject/tsfc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/firedrakeproject/tsfc/downloads","issues_url":"https://api.github.com/repos/firedrakeproject/tsfc/issues{/number}","pulls_url":"https://api.github.com/repos/firedrakeproject/tsfc/pulls{/number}","milestones_url":"https://api.github.com/repos/firedrakeproject/tsfc/milestones{/number}","notifications_url":"https://api.github.com/repos/firedrakeproject/tsfc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/firedrakeproject/tsfc/labels{/name}","releases_url":"https://api.github.com/repos/firedrakeproject/tsfc/releases{/id}","deployments_url":"https://api.github.com/repos/firedrakeproject/tsfc/deployments","created_at":"2016-02-01T14:07:32Z","updated_at":"2016-11-04T12:54:29Z","pushed_at":"2017-02-21T01:47:21Z","git_url":"git://github.com/firedrakeproject/tsfc.git","ssh_url":"git@github.com:firedrakeproject/tsfc.git","clone_url":"https://github.com/firedrakeproject/tsfc.git","svn_url":"https://github.com/firedrakeproject/tsfc","homepage":null,"size":655,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":7,"forks":2,"open_issues":7,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/98"},"html":{"href":"https://github.com/firedrakeproject/tsfc/pull/98"},"issue":{"href":"https://api.github.com/repos/firedrakeproject/tsfc/issues/98"},"comments":{"href":"https://api.github.com/repos/firedrakeproject/tsfc/issues/98/comments"},"review_comments":{"href":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/98/comments"},"review_comment":{"href":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/firedrakeproject/tsfc/pulls/98/commits"},"statuses":{"href":"https://api.github.com/repos/firedrakeproject/tsfc/statuses/8e057397856ecb2883f2eccfe6bed43eabe5153a"}}}}
{"actor":{"display_login":"tj-sun"}}