From bbf0817ea667bfca767ac43d898629266a216c39 Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Wed, 9 Sep 2020 17:24:00 -0500
Subject: [PATCH] Warn about array arithmetic with inconsistent queues (gh-358)

---
 pyopencl/array.py | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/pyopencl/array.py b/pyopencl/array.py
index 8d5cad90..bf58c965 100644
--- a/pyopencl/array.py
+++ b/pyopencl/array.py
@@ -82,6 +82,10 @@ except Exception:
         return False
 
 
+class InconsistentOpenCLQueueWarning(UserWarning):
+    pass
+
+
 class VecLookupWarner:
     def __getattr__(self, name):
         from warnings import warn
@@ -144,7 +148,11 @@ def elwise_kernel_runner(kernel_getter):
 
     def kernel_runner(*args, **kwargs):
         repr_ary = args[0]
-        queue = kwargs.pop("queue", None) or repr_ary.queue
+        queue = kwargs.pop("queue", None)
+        implicit_queue = queue is None
+        if implicit_queue:
+            queue = repr_ary.queue
+
         wait_for = kwargs.pop("wait_for", None)
 
         # wait_for must be a copy, because we modify it in-place below
@@ -171,6 +179,16 @@ def elwise_kernel_runner(kernel_getter):
                 actual_args.append(arg.base_data)
                 actual_args.append(arg.offset)
                 wait_for.extend(arg.events)
+
+                if (implicit_queue
+                        and arg.queue is not None
+                        and arg.queue != queue):
+                    from warnings import warn
+
+                    warn("Implicit queue in elementwise operation does not match "
+                            "queue of a provided argument. This will become an "
+                            "error in 2021.",
+                            type=InconsistentOpenCLQueueWarning)
             else:
                 actual_args.append(arg)
         actual_args.append(repr_ary.size)
-- 
GitLab