If you accidentally pass a numpy integer type as the shape
argument to GPUarray, memory allocation fails:
import pycuda.autoinit
import pycuda.gpuarray as gpuarray
import numpy as np
gpuarray.empty(shape=np.prod(10), dtype='double')
ArgumentError: Python argument types in
pycuda._driver.mem_alloc(numpy.int64)
did not match C++ signature:
mem_alloc(unsigned long)
Here's a patch:
-- >8 --
Subject: [PATCH] Accept numpy scalar types in shape when creating GPUarray
If shape contained a numpy int, allocation of the data would fail,
because we'd pass a numpy int not a python int into the allocator.
Fix this by explicitly converting s to a python scalar type.
This means we can now write:
a = gpuarray.empty(shape=np.prod(some_shape), ...)
In addition raise an assertion error if, for some reason, we
were passed a non-integral shape.
---
pycuda/gpuarray.py | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/pycuda/gpuarray.py b/pycuda/gpuarray.py
index 3f37016..ceb9cb3 100644
--- a/pycuda/gpuarray.py
+++ b/pycuda/gpuarray.py
@@ -162,6 +162,10 @@ class GPUArray(object):
s = shape
shape = (shape,)
+ if isinstance(s, np.integer):
+ s = np.asscalar(s)
+ assert isinstance(s, (int, long))
+
if strides is None:
if order == "F":
strides = _f_contiguous_strides(
_______________________________________________
PyCUDA mailing list
[email protected]
http://lists.tiker.net/listinfo/pycuda