From e3195d96abbf51ec46f338fe43fed07fa239f223 Mon Sep 17 00:00:00 2001
From: Dominic Kempf <dominic.kempf@iwr.uni-heidelberg.de>
Date: Tue, 25 Oct 2016 17:38:44 +0200
Subject: [PATCH] Reorganize imports to have all backend implentations included

The backend selecting mechanism needs all modules to be imported.
This commit restructures the package to be able to do that. Placement
of some functions felt weird, but this can still be changed.
No more placements in __init__.py anymore though, as this will
result in cyclic dependencies.
---
 python/dune/perftool/__init__.py              | 10 +++--
 python/dune/perftool/pdelab/__init__.py       | 39 ++++++-------------
 python/dune/perftool/pdelab/argument.py       | 18 ++-------
 python/dune/perftool/pdelab/basis.py          |  3 +-
 python/dune/perftool/pdelab/geometry.py       |  4 +-
 python/dune/perftool/pdelab/index.py          | 17 ++++++++
 python/dune/perftool/pdelab/localoperator.py  |  4 +-
 python/dune/perftool/pdelab/quadrature.py     |  5 ++-
 python/dune/perftool/pdelab/restriction.py    | 10 +++++
 python/dune/perftool/pdelab/signatures.py     |  2 +-
 python/dune/perftool/pdelab/spaces.py         |  2 +-
 python/dune/perftool/sumfact/__init__.py      |  6 +++
 python/dune/perftool/sumfact/amatrix.py       |  2 +-
 .../dune/perftool/ufl/modified_terminals.py   |  7 +++-
 python/dune/perftool/ufl/visitor.py           |  7 ++--
 15 files changed, 74 insertions(+), 62 deletions(-)
 create mode 100644 python/dune/perftool/pdelab/index.py
 create mode 100644 python/dune/perftool/pdelab/restriction.py

diff --git a/python/dune/perftool/__init__.py b/python/dune/perftool/__init__.py
index 02ef874f..b907a800 100644
--- a/python/dune/perftool/__init__.py
+++ b/python/dune/perftool/__init__.py
@@ -1,4 +1,6 @@
-class Restriction:
-    NONE = 0
-    NEGATIVE = 1
-    POSITIVE = 2
+from dune.perftool.options import get_option
+
+# Trigger some imports that are needed to have all backend implementations visible
+# to the selection mechanisms
+import dune.perftool.pdelab
+import dune.perftool.sumfact
diff --git a/python/dune/perftool/pdelab/__init__.py b/python/dune/perftool/pdelab/__init__.py
index abbc10ff..d8d6ab1d 100644
--- a/python/dune/perftool/pdelab/__init__.py
+++ b/python/dune/perftool/pdelab/__init__.py
@@ -1,30 +1,13 @@
 """ The pdelab specific parts of the code generation process """
 
-from dune.perftool.generation import (preamble,
-                                      cached,
-                                      )
-
-
-# Now define some commonly used generators that do not fall into a specific category
-@cached
-def name_index(index):
-    from ufl.classes import MultiIndex, Index
-    if isinstance(index, Index):
-        # This failed for index > 9 because ufl placed curly brackets around
-        # return str(index)
-        return "i_{}".format(index.count())
-    if isinstance(index, MultiIndex):
-        assert len(index) == 1
-        # return str(index._indices[0])
-        return "i_{}".format(index._indices[0].count())
-    raise NotImplementedError
-
-
-def restricted_name(name, restriction):
-    from dune.perftool import Restriction
-    if restriction == Restriction.NONE:
-        return name
-    if restriction == Restriction.POSITIVE:
-        return name + '_n'
-    if restriction == Restriction.NEGATIVE:
-        return name + '_s'
+# Trigger some imports that are needed to have all backend implementations visible
+# to the selection mechanisms
+import dune.perftool.pdelab.argument
+import dune.perftool.pdelab.basis
+import dune.perftool.pdelab.driver
+import dune.perftool.pdelab.geometry
+import dune.perftool.pdelab.localoperator
+import dune.perftool.pdelab.parameter
+import dune.perftool.pdelab.quadrature
+import dune.perftool.pdelab.signatures
+import dune.perftool.pdelab.spaces
\ No newline at end of file
diff --git a/python/dune/perftool/pdelab/argument.py b/python/dune/perftool/pdelab/argument.py
index 06d4a75d..0e81542c 100644
--- a/python/dune/perftool/pdelab/argument.py
+++ b/python/dune/perftool/pdelab/argument.py
@@ -8,15 +8,14 @@ Namely:
 from dune.perftool.options import get_option
 from dune.perftool.generation import (domain,
                                       function_mangler,
+                                      get_backend,
                                       iname,
                                       pymbolic_expr,
                                       globalarg,
                                       valuearg,
                                       get_global_context_value
                                       )
-from dune.perftool.pdelab import (name_index,
-                                  restricted_name,
-                                  )
+from dune.perftool.pdelab.index import name_index
 from dune.perftool.pdelab.basis import (evaluate_coefficient,
                                         evaluate_coefficient_gradient,
                                         name_basis,
@@ -24,7 +23,8 @@ from dune.perftool.pdelab.basis import (evaluate_coefficient,
 from dune.perftool.pdelab.spaces import (lfs_iname,
                                          name_lfs_bound,
                                          )
-from dune.perftool import Restriction
+from dune.perftool.pdelab.restriction import restricted_name
+from dune.perftool.ufl.modified_terminals import Restriction
 
 from pymbolic.primitives import Call, Subscript, Variable
 
@@ -95,16 +95,6 @@ def name_trialfunction_gradient(element, restriction, component):
     rawname = "gradu" + "_".join(str(c) for c in component)
     name = restricted_name(rawname, restriction)
     container = name_coefficientcontainer(restriction)
-
-    # TODO
-    #
-    # This is just a temporary test used to create an A-matrix as
-    # local operator class member. Right now it doesn't evaluate
-    # anything.
-    if get_option("sumfact") and restriction == Restriction.NONE:
-        from dune.perftool.sumfact import start_sumfactorization
-        start_sumfactorization(element, container, restriction, component)
-
     evaluate_coefficient_gradient(element, name, container, restriction, component)
     return name
 
diff --git a/python/dune/perftool/pdelab/basis.py b/python/dune/perftool/pdelab/basis.py
index ced7d2da..09ecddb0 100644
--- a/python/dune/perftool/pdelab/basis.py
+++ b/python/dune/perftool/pdelab/basis.py
@@ -27,8 +27,7 @@ from dune.perftool.pdelab.localoperator import (lop_template_ansatz_gfs,
                                                 lop_template_test_gfs,
                                                 )
 from dune.perftool.pdelab.driver import FEM_name_mangling
-
-from dune.perftool.pdelab import restricted_name
+from dune.perftool.pdelab.restriction import restricted_name
 from pymbolic.primitives import Product, Subscript, Variable
 from loopy import Reduction
 
diff --git a/python/dune/perftool/pdelab/geometry.py b/python/dune/perftool/pdelab/geometry.py
index f4d476bc..8dd41c12 100644
--- a/python/dune/perftool/pdelab/geometry.py
+++ b/python/dune/perftool/pdelab/geometry.py
@@ -1,5 +1,5 @@
-from dune.perftool import Restriction
-from dune.perftool.pdelab import restricted_name
+from dune.perftool.ufl.modified_terminals import Restriction
+from dune.perftool.pdelab.restriction import restricted_name
 from dune.perftool.generation import (cached,
                                       domain,
                                       get_global_context_value,
diff --git a/python/dune/perftool/pdelab/index.py b/python/dune/perftool/pdelab/index.py
new file mode 100644
index 00000000..cc87edfd
--- /dev/null
+++ b/python/dune/perftool/pdelab/index.py
@@ -0,0 +1,17 @@
+from dune.perftool.generation import cached
+
+from ufl.classes import MultiIndex, Index
+
+
+# Now define some commonly used generators that do not fall into a specific category
+@cached
+def name_index(index):
+    if isinstance(index, Index):
+        # This failed for index > 9 because ufl placed curly brackets around
+        # return str(index)
+        return "i_{}".format(index.count())
+    if isinstance(index, MultiIndex):
+        assert len(index) == 1
+        # return str(index._indices[0])
+        return "i_{}".format(index._indices[0].count())
+    raise NotImplementedError
diff --git a/python/dune/perftool/pdelab/localoperator.py b/python/dune/perftool/pdelab/localoperator.py
index 3c008a17..7a299ab7 100644
--- a/python/dune/perftool/pdelab/localoperator.py
+++ b/python/dune/perftool/pdelab/localoperator.py
@@ -20,7 +20,7 @@ from dune.perftool.cgen.clazz import (AccessModifier,
                                       BaseClass,
                                       ClassMember,
                                       )
-from dune.perftool import Restriction
+from dune.perftool.ufl.modified_terminals import Restriction
 from pymbolic.primitives import Variable
 from pytools import Record
 
@@ -355,7 +355,7 @@ def boundary_predicates(expr, measure, subdomain_id):
 
 @iname
 def grad_iname(index, dim):
-    from dune.perftool.pdelab import name_index
+    from dune.perftool.pdelab.index import name_index
     name = name_index(index)
     domain(name, dim)
     return name
diff --git a/python/dune/perftool/pdelab/quadrature.py b/python/dune/perftool/pdelab/quadrature.py
index bf943177..b778de54 100644
--- a/python/dune/perftool/pdelab/quadrature.py
+++ b/python/dune/perftool/pdelab/quadrature.py
@@ -1,5 +1,5 @@
-from dune.perftool import Restriction
-from dune.perftool.generation import (cached,
+from dune.perftool.generation import (backend,
+                                      cached,
                                       domain,
                                       get_global_context_value,
                                       iname,
@@ -8,6 +8,7 @@ from dune.perftool.generation import (cached,
                                       temporary_variable,
                                       )
 from dune.perftool.options import get_option
+from dune.perftool.ufl.modified_terminals import Restriction
 
 
 @iname
diff --git a/python/dune/perftool/pdelab/restriction.py b/python/dune/perftool/pdelab/restriction.py
new file mode 100644
index 00000000..e889fcd6
--- /dev/null
+++ b/python/dune/perftool/pdelab/restriction.py
@@ -0,0 +1,10 @@
+from dune.perftool.ufl.modified_terminals import Restriction
+
+
+def restricted_name(name, restriction):
+    if restriction == Restriction.NONE:
+        return name
+    if restriction == Restriction.POSITIVE:
+        return name + '_n'
+    if restriction == Restriction.NEGATIVE:
+        return name + '_s'
\ No newline at end of file
diff --git a/python/dune/perftool/pdelab/signatures.py b/python/dune/perftool/pdelab/signatures.py
index 3a40e636..b329245d 100644
--- a/python/dune/perftool/pdelab/signatures.py
+++ b/python/dune/perftool/pdelab/signatures.py
@@ -1,6 +1,6 @@
 """ Signatures for PDELab local opreator assembly functions """
 
-from dune.perftool import Restriction
+from dune.perftool.ufl.modified_terminals import Restriction
 from dune.perftool.pdelab.geometry import (name_geometry_wrapper,
                                            type_geometry_wrapper,
                                            )
diff --git a/python/dune/perftool/pdelab/spaces.py b/python/dune/perftool/pdelab/spaces.py
index e439425f..07403a86 100644
--- a/python/dune/perftool/pdelab/spaces.py
+++ b/python/dune/perftool/pdelab/spaces.py
@@ -6,7 +6,7 @@ from dune.perftool.generation import (domain,
                                       include_file,
                                       preamble,
                                       )
-from dune.perftool.pdelab import restricted_name
+from dune.perftool.pdelab.restriction import restricted_name
 
 from loopy import CallMangleInfo
 from loopy.symbolic import FunctionIdentifier
diff --git a/python/dune/perftool/sumfact/__init__.py b/python/dune/perftool/sumfact/__init__.py
index 793cbde0..4f1ce7c9 100644
--- a/python/dune/perftool/sumfact/__init__.py
+++ b/python/dune/perftool/sumfact/__init__.py
@@ -1 +1,7 @@
+# Trigger some imports that are needed to have all backend implementations visible
+# to the selection mechanisms
+import dune.perftool.sumfact.amatrix
+import dune.perftool.sumfact.quadrature
+import dune.perftool.sumfact.sumfact
+
 from dune.perftool.sumfact.sumfact import start_sumfactorization
diff --git a/python/dune/perftool/sumfact/amatrix.py b/python/dune/perftool/sumfact/amatrix.py
index 9d2e2498..968f4f16 100644
--- a/python/dune/perftool/sumfact/amatrix.py
+++ b/python/dune/perftool/sumfact/amatrix.py
@@ -1,4 +1,4 @@
-from dune.perftool import Restriction
+from dune.perftool.ufl.modified_terminals import Restriction
 
 from dune.perftool.options import get_option
 
diff --git a/python/dune/perftool/ufl/modified_terminals.py b/python/dune/perftool/ufl/modified_terminals.py
index 33319b27..4ba1d0a3 100644
--- a/python/dune/perftool/ufl/modified_terminals.py
+++ b/python/dune/perftool/ufl/modified_terminals.py
@@ -1,11 +1,16 @@
 """ A module mimicking some functionality of uflacs' modified terminals """
 
 from ufl.algorithms import MultiFunction
-from dune.perftool import Restriction
 from ufl.classes import MultiIndex
 from pytools import Record
 
 
+class Restriction:
+    NONE = 0
+    NEGATIVE = 1
+    POSITIVE = 2
+
+
 class ModifiedArgument(Record):
     def __init__(self,
                  expr=None,
diff --git a/python/dune/perftool/ufl/visitor.py b/python/dune/perftool/ufl/visitor.py
index 70d77358..ba3d98c5 100644
--- a/python/dune/perftool/ufl/visitor.py
+++ b/python/dune/perftool/ufl/visitor.py
@@ -3,8 +3,7 @@ This module defines the main visitor algorithm transforming ufl expressions
 to pymbolic and loopy.
 """
 
-from dune.perftool import Restriction
-from dune.perftool.ufl.modified_terminals import ModifiedTerminalTracker
+from dune.perftool.ufl.modified_terminals import ModifiedTerminalTracker, Restriction
 from dune.perftool.generation import (domain,
                                       get_temporary_name,
                                       global_context,
@@ -185,7 +184,7 @@ class UFL2LoopyVisitor(ModifiedTerminalTracker):
         ind = o.ufl_operands[1][0]
         redinames = additional_inames + (ind,)
         shape = o.ufl_operands[0].ufl_index_dimensions[0]
-        from dune.perftool.pdelab import name_index
+        from dune.perftool.pdelab.index import name_index
         domain(name_index(ind), shape)
 
         # If the left operand is an index sum to, we do it in one reduction
@@ -211,7 +210,7 @@ class UFL2LoopyVisitor(ModifiedTerminalTracker):
             return index._value
         else:
             from pymbolic.primitives import Variable
-            from dune.perftool.pdelab import name_index
+            from dune.perftool.pdelab.index import name_index
             if index in self.dimension_indices:
                 from dune.perftool.pdelab.geometry import dimension_iname
                 self.inames.append(self.dimension_indices[index])
-- 
GitLab