From 964420bec5d3127835c6e802a4e5e922224144ad Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Mon, 4 Jul 2011 09:45:41 -0400
Subject: [PATCH] Provide advice if user passes bare array to kernel.

---
 examples/demo_array.py | 28 ++++++++++++++++++++++++++++
 pyopencl/__init__.py   |  9 +++++++--
 2 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 examples/demo_array.py

diff --git a/examples/demo_array.py b/examples/demo_array.py
new file mode 100644
index 00000000..d8afb594
--- /dev/null
+++ b/examples/demo_array.py
@@ -0,0 +1,28 @@
+import pyopencl as cl
+import pyopencl.array as cl_array
+import numpy
+import numpy.linalg as la
+
+a = numpy.random.rand(50000).astype(numpy.float32)
+b = numpy.random.rand(50000).astype(numpy.float32)
+
+ctx = cl.create_some_context()
+queue = cl.CommandQueue(ctx)
+
+mf = cl.mem_flags
+a_dev = cl_array.to_device(queue, a)
+b_dev = cl_array.to_device(queue, b)
+dest_dev = cl_array.empty_like(a_dev)
+
+prg = cl.Program(ctx, """
+    __kernel void sum(__global const float *a,
+    __global const float *b, __global float *c)
+    {
+      int gid = get_global_id(0);
+      c[gid] = a[gid] + b[gid];
+    }
+    """).build()
+
+prg.sum(queue, a.shape, None, a_dev.data, b_dev.data, dest_dev.data)
+
+print(la.norm((dest_dev - (a_dev+b_dev)).get()))
diff --git a/pyopencl/__init__.py b/pyopencl/__init__.py
index da075dbc..a74adfcb 100644
--- a/pyopencl/__init__.py
+++ b/pyopencl/__init__.py
@@ -272,9 +272,14 @@ def _add_functionality():
                         self.set_arg(i, arg)
         except LogicError, e:
             if i is not None:
+                advice = ""
+                from pyopencl.array import Array
+                if isinstance(args[i], Array):
+                    advice = " (perhaps you meant to pass 'array.data' instead of the array itself?)"
+
                 raise LogicError(
-                        "when processing argument #%d (1-based): %s"
-                        % (i+1, str(e)))
+                        "when processing argument #%d (1-based): %s%s"
+                        % (i+1, str(e), advice))
             else:
                 raise
 
-- 
GitLab