Skip to content

Commit

Permalink
FIX: no more explicit padding required for vectors
Browse files Browse the repository at this point in the history
Additionally, relying on default values for initialization triggers a
deprecation warning. Added three more vector constructors: filled_ which fills
the entire vector with a given value, zeros_ and ones_.

Closes #28
  • Loading branch information
pohlt committed Mar 11, 2014
1 parent cccf153 commit 878a998
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
15 changes: 15 additions & 0 deletions doc/array.rst
Expand Up @@ -29,6 +29,21 @@ Vector Types
Python code. For each type, a `make_type` function is also provided (e.g.
`make_float3(x,y,z)`).
If you want to construct a pre-initialized vector type you have three new
functions to choose from:
* `zeros_type()`
* `ones_type()`
* `filled_type(fill_value)`
.. versionadded:: 2013.3
.. versionchanged:: 2013.3
The `make_type` functions have a default value (0) for each component.
Relying on the default values has been deprecated. Either specify all
components or use one of th new flavors mentioned above for constructing
a vector.
Custom data types
^^^^^^^^^^^^^^^^^

Expand Down
32 changes: 24 additions & 8 deletions pyopencl/array.py
Expand Up @@ -28,6 +28,7 @@
"""


import warnings
import numpy as np
import pyopencl.elementwise as elementwise
import pyopencl as cl
Expand Down Expand Up @@ -99,14 +100,29 @@ def _create_vector_types():

setattr(vec, name, dtype)

my_field_names = ",".join(field_names[:count])
my_field_names_defaulted = ",".join(
"%s=0" % fn for fn in field_names[:count])
setattr(vec, "make_"+name,
staticmethod(eval(
"lambda %s: array((%s), dtype=my_dtype)"
% (my_field_names_defaulted, my_field_names),
dict(array=np.array, my_dtype=dtype))))
def create_array(dtype, count, padded_count, *args, **kwargs):
if len(args) < count:
warnings.warn("default values for make_xxx are deprecated;"+
" instead specify all parameters or use"+
" array.vec.zeros_xxx", DeprecationWarning)
padded_args = tuple(list(args)+[0]*(padded_count-len(args)))
array = eval("array(padded_args, dtype=dtype)",
dict(array=np.array, padded_args=padded_args,
dtype=dtype))
for key, val in kwargs.items():
array[key] = val
return array

setattr(vec, "make_"+name, staticmethod(eval(
"lambda *args, **kwargs: create_array(dtype, %i, %i, "+
"*args, **kwargs)" % (count, padded_count),
dict(create_array=create_array, dtype=dtype))))
setattr(vec, "filled_"+name, staticmethod(eval(
"lambda val: vec.make_%s(*[val]*%i)" % (name, count))))
setattr(vec, "zeros_"+name,
staticmethod(eval("lambda: vec.filled_%s(0)" % (name))))
setattr(vec, "ones_"+name,
staticmethod(eval("lambda: vec.filled_%s(1)" % (name))))

vec.types[np.dtype(base_type), count] = dtype
vec.type_to_scalar_and_count[dtype] = np.dtype(base_type), count
Expand Down

0 comments on commit 878a998

Please sign in to comment.