From d30c5f73b4dc93cc7c15e7d92e8f9c2d4020ef69 Mon Sep 17 00:00:00 2001
From: Martin Bauer <martin.bauer@fau.de>
Date: Tue, 23 Jan 2018 12:16:29 +0100
Subject: [PATCH] Changed parameter bind caching for CPU and GPU kernels

- previously all objects where cached by id()
- for waLBerla simulations in each time step a new np.array view
  is created from the waLBerla field. Each of these views has a
  different id -> caching did not work for waLBerla setups
- changed hash for numpy arrays: instead of id, a tuple of
  (dataPtr, strides, shapes) is used as hash input
---
 cpu/cpujit.py      | 4 +++-
 gpucuda/cudajit.py | 3 ++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/cpu/cpujit.py b/cpu/cpujit.py
index 7f36c88c3..fa04eda77 100644
--- a/cpu/cpujit.py
+++ b/cpu/cpujit.py
@@ -68,6 +68,7 @@ import platform
 import glob
 import atexit
 import shutil
+import numpy as np
 from appdirs import user_config_dir, user_cache_dir
 from ctypes import cdll
 from pystencils.backends.cbackend import generateC, getHeaders
@@ -428,7 +429,8 @@ def makePythonFunctionIncompleteParams(kernelFunctionNode, argumentDict, func):
     cacheValues = []
 
     def wrapper(**kwargs):
-        key = hash(tuple((k, id(v)) for k, v in kwargs.items()))
+        key = hash(tuple((k, v.ctypes.data, v.strides, v.shape) if isinstance(v, np.ndarray) else (k, id(v))
+                         for k, v in kwargs.items()))
         try:
             args = cache[key]
             func(*args)
diff --git a/gpucuda/cudajit.py b/gpucuda/cudajit.py
index 6f5633d5e..2cfafb0ee 100644
--- a/gpucuda/cudajit.py
+++ b/gpucuda/cudajit.py
@@ -33,7 +33,8 @@ def makePythonFunction(kernelFunctionNode, argumentDict={}):
     cacheValues = []
 
     def wrapper(**kwargs):
-        key = hash(tuple((k, id(v)) for k, v in kwargs.items()))
+        key = hash(tuple((k, v.ctypes.data, v.strides, v.shape) if isinstance(v, np.ndarray) else (k, id(v))
+                         for k, v in kwargs.items()))
         try:
             args, dictWithBlockAndThreadNumbers = cache[key]
             func(*args, **dictWithBlockAndThreadNumbers)
-- 
GitLab