From f8a8c30ed7535bf363e2dc8b7ca3456207ad7705 Mon Sep 17 00:00:00 2001
From: Dominic Kempf <dominic.r.kempf@gmail.com>
Date: Thu, 10 Sep 2015 14:33:01 +0200
Subject: [PATCH] Use caching mechanisms for the loopy arguments

---
 python/dune/perftool/pdelab/localoperator.py | 26 +++-----------------
 python/dune/perftool/transformer.py          | 14 +++++++++--
 2 files changed, 16 insertions(+), 24 deletions(-)

diff --git a/python/dune/perftool/pdelab/localoperator.py b/python/dune/perftool/pdelab/localoperator.py
index 357711b7..ebb08e04 100644
--- a/python/dune/perftool/pdelab/localoperator.py
+++ b/python/dune/perftool/pdelab/localoperator.py
@@ -90,30 +90,12 @@ def generate_term(integrand=None, measure=None):
     instructions = [i for i in retrieve_cache_items("instruction")]
     temporaries = {i.name:i for i in retrieve_cache_items("temporary")}
     preambles = [i for i in retrieve_cache_items("preamble")]
+    arguments = [i for i in retrieve_cache_items("argument")]
 
-# TODO it might be necessary to list the arguments and their shape manually to avoid automatic shape detection!
-    import loopy
-    import numpy
+    # Create the kernel
     from loopy import make_kernel, preprocess_kernel
-    from dune.perftool.target import DuneTarget
-    k = make_kernel(domains, instructions,
-                [loopy.GlobalArg("grad_a0", numpy.float64, shape=("argi_n", "dim")),
-                 loopy.GlobalArg("grad_a1", numpy.float64, shape=("argj_n", "dim")),
-                 loopy.GlobalArg("grad_c0", numpy.float64, shape=("dim")),
-                 loopy.ValueArg("dim", numpy.int32),
-                 loopy.ValueArg("q_n", numpy.int32),
-                 loopy.ValueArg("argi_n", numpy.int32),
-                 loopy.ValueArg("argj_n", numpy.int32)],
-                    temporary_variables=temporaries, target=DuneTarget())
-    k = preprocess_kernel(k)
-    kernel = k
-
-#     # Create the kernel
-#     from loopy import make_kernel, preprocess_kernel, add_dtypes
-#     kernel = make_kernel(domains, instructions, temporary_variables=temporaries, preambles=preambles, target=DuneTarget())
-#     import numpy
-#     kernel = add_dtypes(kernel, dict(grad_a0=numpy.float64, grad_c0=numpy.float64))
-#     kernel = preprocess_kernel(kernel)
+    kernel = make_kernel(domains, instructions, arguments, temporary_variables=temporaries, preambles=preambles, target=DuneTarget())
+    kernel = preprocess_kernel(kernel)
 
     # Return the actual code (might instead return kernels...)
     from loopy import generate_code
diff --git a/python/dune/perftool/transformer.py b/python/dune/perftool/transformer.py
index ab065db3..05da1205 100644
--- a/python/dune/perftool/transformer.py
+++ b/python/dune/perftool/transformer.py
@@ -15,9 +15,17 @@ loopy_iname = generator_factory(item_tags=("loopy", "kernel", "iname"))
 loopy_expr_instruction = generator_factory(item_tags=("loopy", "kernel", "instruction", "exprinstruction"), no_deco=True)
 loopy_temporary_variable = generator_factory(item_tags=("loopy", "kernel", "temporary"), on_store=lambda n: loopy.TemporaryVariable(n, dtype=numpy.float64), no_deco=True)
 loopy_c_instruction = generator_factory(item_tags=("loopy", "kernel", "instruction", "cinstruction"), no_deco=True)
+loopy_valuearg = generator_factory(item_tags=("loopy", "kernel", "argument", "valuearg"), on_store=lambda n: loopy.ValueArg(n), no_deco=True)
+
+@generator_factory(item_tags=("loopy", "kernel", "argument", "globalarg"))
+def loopy_globalarg(name, shape=loopy.auto):
+    if isinstance(shape, str):
+        shape = (shape,)
+    return loopy.GlobalArg(name, numpy.float64, shape)
 
 @generator_factory(item_tags=("loopy", "kernel", "domain"))
 def loopy_domain(iname, shape):
+    loopy_valuearg(shape)
     return "{{ [{0}] : 0<={0}<{1} }}".format(iname, shape)
 
 @loopy_iname
@@ -77,8 +85,8 @@ class UFLVisitor(MultiFunction):
         from dune.perftool.pdelab.quadrature import name_factor
         loopyexpr = Product((loopyexpr, Variable(name_factor())))
         # TODO: Some unique name mangling on this temporary variable is necessary!
-        expr_tv = loopy_temporary_variable("xyz")
-        loopy_expr_instruction(loopy.ExpressionInstruction(assignee=Variable("xyz"), expression=loopyexpr))
+        expr_tv = loopy_temporary_variable("expr")
+        loopy_expr_instruction(loopy.ExpressionInstruction(assignee=Variable("expr"), expression=loopyexpr))
 
         from dune.perftool.pdelab.argument import name_residual
         residual = name_residual()
@@ -105,6 +113,7 @@ class UFLVisitor(MultiFunction):
         name = name_testfunction(o, self.index, self.grad, self.restriction)
         index = argument_iname(o)
         self.inames.append(index)
+        loopy_globalarg(name)
 
         return Subscript(Variable(name), (Variable(index),))
 
@@ -117,6 +126,7 @@ class UFLVisitor(MultiFunction):
         from dune.perftool.pdelab.argument import name_trialfunction
         from dune.perftool.pdelab import name_index
         name = name_trialfunction(o, self.index, self.grad, self.restriction)
+        loopy_globalarg(name)
 
         return Variable(name)
 
-- 
GitLab