Browse Source

Optimize accesses to globals: cache for each task individually

Yentl Van Tendeloo 7 years ago
parent
commit
313e8f94c1

+ 1 - 0
kernel/modelverse_kernel/jit.py

@@ -21,6 +21,7 @@ class ModelverseJit(object):
         self.jitted_function_aliases = defaultdict(set)
         self.jit_count = 0
         self.compilation_dependencies = {}
+        self.cache = {}
 
     def mark_entry_point(self, body_id):
         """Marks the node with the given identifier as a function entry point."""

+ 11 - 4
kernel/modelverse_kernel/main.py

@@ -40,6 +40,7 @@ class ModelverseKernel(object):
         self.taskname = taskname
         if taskname not in self.request_handlers:
             self.request_handlers[taskname] = {}
+            self.jit.cache.setdefault(taskname, {})
         if operation not in self.request_handlers[taskname]:
             # Create the generator for the function to execute
             self.request_handlers[taskname][operation] = RequestHandler()
@@ -221,9 +222,13 @@ class ModelverseKernel(object):
             if str_value:
                 # Is a global
                 prev = \
-                       "  " * nested_indent + "%s, = yield [('RD', [_globs, '%s'])]\n" % (str_value, str_value) + \
-                       "  " * nested_indent + "%s, = yield [('RD', [%s, 'value'])]\n" % (str_value, str_value) + \
-                       "  " * nested_indent + "%s = {'id': %s}\n" % (str_value, str_value)
+                       "  " * nested_indent + "%s = _mvk.jit.cache[_taskname].get('%s', None)\n" % (str_value, str_value) + \
+                       "  " * nested_indent + "if %s is None:\n" % str_value + \
+                       "  " * (nested_indent + 1) + "print('cache miss for %s')\n" % str_value + \
+                       "  " * (nested_indent + 1) + "%s, = yield [('RD', [_globs, '%s'])]\n" % (str_value, str_value) + \
+                       "  " * (nested_indent + 1) + "%s, = yield [('RD', [%s, 'value'])]\n" % (str_value, str_value) + \
+                       "  " * (nested_indent + 1) + "%s = {'id': %s}\n" % (str_value, str_value) + \
+                       "  " * (nested_indent + 1) + "_mvk.jit.cache[_taskname]['%s'] = %s\n" % (str_value, str_value)
                 instruction = str_value
 
                 if self.jit.get_global_body_id(str_value) is None:
@@ -259,7 +264,8 @@ class ModelverseKernel(object):
                        "  " * nested_indent + "if 'id' not in %s:\n" % instruction_var + \
                        "  " * (nested_indent + 1) + "%s['id'], = yield [('CNV', [%s['value']])]\n" % (instruction_var, instruction_var) + \
                        "  " * nested_indent + "yield [('CD', [_var, 'value', %s['id']]), ('DE', [_old_edge])]\n" % instruction_var + \
-                       "  " * nested_indent + "_mvk.jit.register_global('%s', %s['id'])\n" % (instruction_var, instruction_var)
+                       "  " * nested_indent + "_mvk.jit.cache[_taskname]['%s'] = %s\n" % (instruction_var, instruction_var)
+                       #"  " * nested_indent + "_mvk.jit.register_global('%s', %s['id'])\n" % (instruction_var, instruction_var) + \
 
         elif inst_type["value"] == "call":
             func_name, = yield [("RD", [inst, "func"])]
@@ -422,6 +428,7 @@ class ModelverseKernel(object):
         if result is None:
             result = {'id': None, 'value': None}
 
+        # TODO this can move inside "store" branch?
         if "id" not in result:
             result['id'], = yield [("CNV", [result['value']])]
 

+ 1 - 0
state/modelverse_state/main.py

@@ -220,6 +220,7 @@ class ModelverseState(object):
             return [None, None]
 
     def read_dict(self, node, value):
+        #print("RD -- " + str(value))
         try:
             first = self.cache[node][value]
             # Got hit, so validate

+ 1 - 1
wrappers/modelverse_SCCD.py

@@ -1,7 +1,7 @@
 """
 Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
 
-Date:   Thu Apr 26 16:40:53 2018
+Date:   Fri Apr 27 08:54:20 2018
 
 Model author: Yentl Van Tendeloo
 Model name:   MvK Server