From 0d78c5844023a765aae67a585a60682e78d433a2 Mon Sep 17 00:00:00 2001
From: Dominic Kempf <dominic.kempf@iwr.uni-heidelberg.de>
Date: Thu, 23 Aug 2018 15:53:07 +0200
Subject: [PATCH] Beautify the implementation a small bit

---
 python/dune/perftool/sumfact/vectorization.py | 47 ++++++++++---------
 1 file changed, 24 insertions(+), 23 deletions(-)

diff --git a/python/dune/perftool/sumfact/vectorization.py b/python/dune/perftool/sumfact/vectorization.py
index cd0f7ffa..d98ba5e7 100644
--- a/python/dune/perftool/sumfact/vectorization.py
+++ b/python/dune/perftool/sumfact/vectorization.py
@@ -85,13 +85,15 @@ def explicit_costfunction(sf):
         return 1000000000000
 
 
-_global_kernel_amount = 0
-
-
 def target_costfunction(sf):
+    # The cost of a kernel is given by the difference to the desired target cost.
+    # Pitfall: The target cost needs to be weighed to account for this being called
+    # on subsets and not on a full vectorization strategy!
+    all_sf, _ = filter_active_inactive_sumfacts()
+    total = len(all_sf)
     target = float(get_form_option("vectorization_target"))
     realcost = costmodel(sf)
-    ratio = sf.horizontal_width / _global_kernel_amount
+    ratio = sf.horizontal_width / total
     return abs(realcost - ratio * target)
 
 
@@ -110,8 +112,7 @@ def strategy_cost(strat_tuple):
         raise NotImplementedError("Vectorization strategy '{}' unknown!".format(s))
 
     keys = set(sf.cache_key for sf in strategy.values())
-    if qp is not None:
-        set_quadrature_points(qp)
+    set_quadrature_points(qp)
 
     # Sum over all the sum factorization kernels in the realization
     score = 0.0
@@ -154,13 +155,7 @@ def stringify_vectorization_strategy(strategy):
     return result
 
 
-def decide_vectorization_strategy():
-    """ Decide how to vectorize!
-    Note that the vectorization of the quadrature loop is independent of this,
-    as it is implemented through a post-processing (== loopy transformation) step.
-    """
-    logger = logging.getLogger(__name__)
-
+def filter_active_inactive_sumfacts():
     # Retrieve all sum factorization kernels for stage 1 and 3
     from dune.perftool.generation import retrieve_cache_items
     all_sumfacts = [i for i in retrieve_cache_items("kernel_default and sumfactnodes")]
@@ -174,6 +169,18 @@ def decide_vectorization_strategy():
     # All sum factorization kernels that get used
     active_sumfacts = [i for i in all_sumfacts if i.stage == 3 or i in basis_sumfacts]
 
+    return active_sumfacts, inactive_sumfacts
+
+
+def decide_vectorization_strategy():
+    """ Decide how to vectorize!
+    Note that the vectorization of the quadrature loop is independent of this,
+    as it is implemented through a post-processing (== loopy transformation) step.
+    """
+    logger = logging.getLogger(__name__)
+
+    active_sumfacts, inactive_sumfacts = filter_active_inactive_sumfacts()
+
     # If no vectorization is needed, abort now
     if get_form_option("vectorization_strategy") == "none" or (get_global_context_value("form_type") == "jacobian" and not get_form_option("vectorization_jacobians")):
         for sf in all_sumfacts:
@@ -208,18 +215,12 @@ def decide_vectorization_strategy():
     sfdict = add_to_frozendict(sfdict, {sf: 0 for sf in inactive_sumfacts})
 
     # Register the results
-    for sf in all_sumfacts:
+    from itertools import chain
+    for sf in chain(active_sumfacts, inactive_sumfacts):
         _cache_vectorization_info(sf, sfdict[sf])
 
 
 def level1_optimal_vectorization_strategy(sumfacts, width):
-    # If this uses the 'target' cost model, we need to store information on how many
-    # sum factorization kernels need to be implemented. This will be used to correctly
-    # weight the cost target in the cost function.
-    if get_form_option("vectorization_strategy") == "target":
-        global _global_kernel_amount
-        _global_kernel_amount = len(sumfacts)
-
     # Gather a list of possible quadrature point tuples
     quad_points = [quadrature_points_per_direction()]
     if get_form_option("vectorization_allow_quadrature_changes"):
@@ -255,11 +256,11 @@ def level1_optimal_vectorization_strategy(sumfacts, width):
 
         # Write an entry into a csvfile which connects the given measuring identifier with a cost
         from dune.testtools.parametertree.parser import parse_ini_file
-        inifile = parse_ini_file(get_option("inifile"))
+        inifile = parse_ini_file(get_option("ini_file"))
         identifier = inifile["identifier"]
         filename = join(get_option("project_basedir"), "costmodel-verification", "mapping.csv")
 
-        #TODO: Depending on the number of samples, we might need a file lock here.
+        # TODO: Depending on the number of samples, we might need a file lock here.
         with open(filename, 'a') as f:
             f.write("{} {}".format(identifier, cost))
 
-- 
GitLab