From f5d3f8d952da937c2ec7470137a06c6d65472691 Mon Sep 17 00:00:00 2001
From: Stephan Seitz <stephan.seitz@fau.de>
Date: Thu, 9 Jul 2020 12:52:08 +0200
Subject: [PATCH] Throw an error when performing GPU operations with
 SerialDataHandling when pycuda is not available

---
 pystencils/datahandling/pycuda.py              | 6 ++++++
 pystencils/datahandling/serial_datahandling.py | 4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/pystencils/datahandling/pycuda.py b/pystencils/datahandling/pycuda.py
index 75967c1cc..14e255c85 100644
--- a/pystencils/datahandling/pycuda.py
+++ b/pystencils/datahandling/pycuda.py
@@ -39,3 +39,9 @@ class PyCudaArrayHandler:
         return self.to_gpu(cpu_array)
 
     from_numpy = to_gpu
+
+
+class PyCudaNotAvailableHandler:
+    def __getattribute__(self, name):
+        raise NotImplementedError("Unable to initiaize PyCuda! "
+                                  "Try to run `import pycuda.autoinit` to check whether PyCuda is working correctly!")
diff --git a/pystencils/datahandling/serial_datahandling.py b/pystencils/datahandling/serial_datahandling.py
index e5244c05a..7352951d2 100644
--- a/pystencils/datahandling/serial_datahandling.py
+++ b/pystencils/datahandling/serial_datahandling.py
@@ -6,7 +6,7 @@ import numpy as np
 
 from pystencils.datahandling.blockiteration import SerialBlock
 from pystencils.datahandling.datahandling_interface import DataHandling
-from pystencils.datahandling.pycuda import PyCudaArrayHandler
+from pystencils.datahandling.pycuda import PyCudaArrayHandler, PyCudaNotAvailableHandler
 from pystencils.datahandling.pyopencl import PyOpenClArrayHandler
 from pystencils.field import (
     Field, FieldType, create_numpy_array_with_layout, layout_string_to_tuple,
@@ -53,7 +53,7 @@ class SerialDataHandling(DataHandling):
             try:
                 self.array_handler = PyCudaArrayHandler()
             except Exception:
-                self.array_handler = None
+                self.array_handler = PyCudaNotAvailableHandler()
 
             if default_target == 'opencl' or opencl_queue:
                 self.array_handler = PyOpenClArrayHandler(opencl_queue)
-- 
GitLab