From 0e56c1e3533606202b61505989d4253cd4ace4fb Mon Sep 17 00:00:00 2001
From: Stephan Seitz <stephan.seitz@fau.de>
Date: Wed, 13 Nov 2019 11:01:39 +0100
Subject: [PATCH] Make SerialDataHandling.serialization_function work with
 OpenCL

---
 pystencils/datahandling/serial_datahandling.py |  9 ++++++++-
 pystencils/gpucuda/periodicity.py              | 15 +++++++++++----
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/pystencils/datahandling/serial_datahandling.py b/pystencils/datahandling/serial_datahandling.py
index a3e6820f0..931a7df30 100644
--- a/pystencils/datahandling/serial_datahandling.py
+++ b/pystencils/datahandling/serial_datahandling.py
@@ -23,6 +23,7 @@ class SerialDataHandling(DataHandling):
                  periodicity: Union[bool, Sequence[bool]] = False,
                  default_target: str = 'cpu',
                  opencl_queue=None,
+                 opencl_ctx=None,
                  array_handler=None) -> None:
         """
         Creates a data handling for single node simulations.
@@ -44,6 +45,8 @@ class SerialDataHandling(DataHandling):
         self.custom_data_cpu = DotDict()
         self.custom_data_gpu = DotDict()
         self._custom_data_transfer_functions = {}
+        self._opencl_queue = opencl_queue
+        self._opencl_ctx = opencl_ctx
 
         if array_handler:
             self.array_handler = array_handler
@@ -315,11 +318,15 @@ class SerialDataHandling(DataHandling):
                     result.append(get_periodic_boundary_functor(filtered_stencil, ghost_layers=gls))
                 else:
                     from pystencils.gpucuda.periodicity import get_periodic_boundary_functor as boundary_func
+                    target = 'gpu' if not isinstance(self.array_handler, PyOpenClArrayHandler) else 'opencl'
                     result.append(boundary_func(filtered_stencil, self._domainSize,
                                                 index_dimensions=self.fields[name].index_dimensions,
                                                 index_dim_shape=values_per_cell,
                                                 dtype=self.fields[name].dtype.numpy_dtype,
-                                                ghost_layers=gls))
+                                                ghost_layers=gls,
+                                                target=target,
+                                                opencl_queue=self._opencl_queue,
+                                                opencl_ctx=self._opencl_ctx))
 
         if target == 'cpu':
             def result_functor():
diff --git a/pystencils/gpucuda/periodicity.py b/pystencils/gpucuda/periodicity.py
index e94a1796e..080ef44eb 100644
--- a/pystencils/gpucuda/periodicity.py
+++ b/pystencils/gpucuda/periodicity.py
@@ -1,7 +1,8 @@
 import numpy as np
 
+import pystencils.gpucuda
+import pystencils.opencl
 from pystencils import Assignment, Field
-from pystencils.gpucuda import make_python_function
 from pystencils.gpucuda.kernelcreation import create_cuda_kernel
 from pystencils.slicing import get_periodic_boundary_src_dst_slices, normalize_slice
 
@@ -25,16 +26,22 @@ def create_copy_kernel(domain_size, from_slice, to_slice, index_dimensions=0, in
         update_eqs.append(eq)
 
     ast = create_cuda_kernel(update_eqs, iteration_slice=to_slice, skip_independence_check=True)
-    return make_python_function(ast)
+    return ast
 
 
 def get_periodic_boundary_functor(stencil, domain_size, index_dimensions=0, index_dim_shape=1, ghost_layers=1,
-                                  thickness=None, dtype=float):
+                                  thickness=None, dtype=float, target='gpu', opencl_queue=None, opencl_ctx=None):
+    assert target in ['gpu', 'opencl']
     src_dst_slice_tuples = get_periodic_boundary_src_dst_slices(stencil, ghost_layers, thickness)
     kernels = []
     index_dimensions = index_dimensions
+
     for src_slice, dst_slice in src_dst_slice_tuples:
-        kernels.append(create_copy_kernel(domain_size, src_slice, dst_slice, index_dimensions, index_dim_shape, dtype))
+        ast = create_copy_kernel(domain_size, src_slice, dst_slice, index_dimensions, index_dim_shape, dtype)
+        if target == 'gpu':
+            kernels.append(pystencils.gpucuda.make_python_function(ast))
+        else:
+            kernels.append(pystencils.opencl.make_python_function(ast, opencl_queue, opencl_ctx))
 
     def functor(pdfs, **_):
         for kernel in kernels:
-- 
GitLab