From 047846863ed71c7a13633b90aaaaf972dc83397d Mon Sep 17 00:00:00 2001
From: Martin Bauer <martin.bauer@fau.de>
Date: Thu, 11 Jan 2018 14:33:51 +0100
Subject: [PATCH] KernelFunction node stores now backend string - compiled
 functions have ast attribute

---
 astnodes.py                        | 3 ++-
 cpu/cpujit.py                      | 1 +
 cpu/kernelcreation.py              | 2 +-
 gpucuda/cudajit.py                 | 1 +
 gpucuda/kernelcreation.py          | 4 ++--
 llvm/kernelcreation.py             | 2 +-
 parallel/datahandling.py           | 8 ++++++++
 transformations/transformations.py | 2 +-
 8 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/astnodes.py b/astnodes.py
index d87b45555..cd39d26c1 100644
--- a/astnodes.py
+++ b/astnodes.py
@@ -155,7 +155,7 @@ class KernelFunction(Node):
         def __repr__(self):
             return '<{0} {1}>'.format(self.dtype, self.name)
 
-    def __init__(self, body, ghostLayers=None, functionName="kernel"):
+    def __init__(self, body, ghostLayers=None, functionName="kernel", backend=""):
         super(KernelFunction, self).__init__()
         self._body = body
         body.parent = self
@@ -166,6 +166,7 @@ class KernelFunction(Node):
         self.ghostLayers = ghostLayers
         # these variables are assumed to be global, so no automatic parameter is generated for them
         self.globalVariables = set()
+        self.backend = ""
 
     @property
     def symbolsDefined(self):
diff --git a/cpu/cpujit.py b/cpu/cpujit.py
index 28184d6f3..7fcfab610 100644
--- a/cpu/cpujit.py
+++ b/cpu/cpujit.py
@@ -439,4 +439,5 @@ def makePythonFunctionIncompleteParams(kernelFunctionNode, argumentDict, func):
             cache[key] = args
             cacheValues.append(kwargs)  # keep objects alive such that ids remain unique
             func(*args)
+    wrapper.ast = kernelFunctionNode
     return wrapper
diff --git a/cpu/kernelcreation.py b/cpu/kernelcreation.py
index 91b6dc3eb..eb5e48511 100644
--- a/cpu/kernelcreation.py
+++ b/cpu/kernelcreation.py
@@ -139,7 +139,7 @@ def createIndexedKernel(listOfEquations, indexFields, functionName="kernel", typ
         loopBody.append(assignment)
 
     functionBody = Block([loopNode])
-    ast = KernelFunction(functionBody, functionName=functionName)
+    ast = KernelFunction(functionBody, "cpu", functionName=functionName)
 
     fixedCoordinateMapping = {f.name: coordinateTypedSymbols for f in nonIndexFields}
     resolveFieldAccesses(ast, set(['indexField']), fieldToFixedCoordinates=fixedCoordinateMapping)
diff --git a/gpucuda/cudajit.py b/gpucuda/cudajit.py
index 3a0fe85f4..96087f1a4 100644
--- a/gpucuda/cudajit.py
+++ b/gpucuda/cudajit.py
@@ -52,6 +52,7 @@ def makePythonFunction(kernelFunctionNode, argumentDict={}):
             cacheValues.append(kwargs)  # keep objects alive such that ids remain unique
             func(*args, **dictWithBlockAndThreadNumbers)
         #cuda.Context.synchronize() # useful for debugging, to get errors right after kernel was called
+    wrapper.ast = kernelFunctionNode
     return wrapper
 
 
diff --git a/gpucuda/kernelcreation.py b/gpucuda/kernelcreation.py
index 5bd2f5d7d..7797b46a3 100644
--- a/gpucuda/kernelcreation.py
+++ b/gpucuda/kernelcreation.py
@@ -45,7 +45,7 @@ def createCUDAKernel(listOfEquations, functionName="kernel", typeForSymbol=None,
 
     block = Block(assignments)
     block = indexing.guard(block, commonShape)
-    ast = KernelFunction(block, functionName=functionName, ghostLayers=ghostLayers)
+    ast = KernelFunction(block, functionName=functionName, ghostLayers=ghostLayers, backend='gpucuda')
     ast.globalVariables.update(indexing.indexVariables)
 
     coordMapping = indexing.coordinates
@@ -118,7 +118,7 @@ def createdIndexedCUDAKernel(listOfEquations, indexFields, functionName="kernel"
 
     functionBody = Block(coordinateSymbolAssignments + assignments)
     functionBody = indexing.guard(functionBody, getCommonShape(indexFields))
-    ast = KernelFunction(functionBody, functionName=functionName)
+    ast = KernelFunction(functionBody, functionName=functionName, backend='gpucuda')
     ast.globalVariables.update(indexing.indexVariables)
 
     coordMapping = indexing.coordinates
diff --git a/llvm/kernelcreation.py b/llvm/kernelcreation.py
index 121dcdd92..aa690b957 100644
--- a/llvm/kernelcreation.py
+++ b/llvm/kernelcreation.py
@@ -89,7 +89,7 @@ def createIndexedKernel(listOfEquations, indexFields, functionName="kernel", typ
         loopBody.append(assignment)
 
     functionBody = Block([loopNode])
-    ast = KernelFunction(functionBody, allFields, functionName)
+    ast = KernelFunction(functionBody, None, functionName, backend='llvm')
 
     fixedCoordinateMapping = {f.name: coordinateTypedSymbols for f in nonIndexFields}
     resolveFieldAccesses(ast, set(['indexField']), fieldToFixedCoordinates=fixedCoordinateMapping)
diff --git a/parallel/datahandling.py b/parallel/datahandling.py
index 675c47a2c..f05539816 100644
--- a/parallel/datahandling.py
+++ b/parallel/datahandling.py
@@ -141,6 +141,14 @@ class ParallelDataHandling(DataHandling):
                     array = array[:, :, 0]
                 yield array
 
+    def runKernel(self, kernelFunc, *args, **kwargs):
+        fieldArguments = [p.fieldName for p in kernelFunc.ast.parameters if p.isFieldPtrArgument]
+        for block in self.blocks:
+            fieldArgs = {fieldName: wlb.field.toArray(block[fieldName], withGhostLayers=True)
+                         for fieldName in fieldArguments}
+            fieldArgs.update(kwargs)
+            kernelFunc(*args, **kwargs)
+
     def toCpu(self, name):
         if name in self._customDataTransferFunctions:
             transferFunc = self._customDataTransferFunctions[name][1]
diff --git a/transformations/transformations.py b/transformations/transformations.py
index bf9426f32..73bc8361d 100644
--- a/transformations/transformations.py
+++ b/transformations/transformations.py
@@ -121,7 +121,7 @@ def makeLoopOverDomain(body, functionName, iterationSlice=None, ghostLayers=None
                 currentBody.insertFront(assignment)
 
     loopVars = [numBufferAccesses * var for var in loopVars]
-    astNode = ast.KernelFunction(currentBody, ghostLayers=ghostLayers, functionName=functionName)
+    astNode = ast.KernelFunction(currentBody, ghostLayers=ghostLayers, functionName=functionName, backend='cpu')
     return (astNode, loopStrides, loopVars)
 
 
-- 
GitLab