def arange(context, queue, *args, **kwargs): """Create an array filled with numbers spaced `step` apart, starting from `start` and ending at `stop`. For floating point arguments, the length of the result is `ceil((stop - start)/step)`. This rule may result in the last element of the result being greater than stop. """ # argument processing ----------------------------------------------------- # Yuck. Thanks, numpy developers. ;) from pytools import Record class Info(Record): pass explicit_dtype = False inf = Info() inf.start = None inf.stop = None inf.step = None inf.dtype = None if isinstance(args[-1], numpy.dtype): dtype = args[-1] args = args[:-1] explicit_dtype = True argc = len(args) if argc == 0: raise ValueError, "stop argument required" elif argc == 1: inf.stop = args[0] elif argc == 2: inf.start = args[0] inf.stop = args[1] elif argc == 3: inf.start = args[0] inf.stop = args[1] inf.step = args[2] else: raise ValueError, "too many arguments" admissible_names = ["start", "stop", "step", "dtype"] for k, v in kwargs.iteritems(): if k in admissible_names: if getattr(inf, k) is None: setattr(inf, k, v) if k == "dtype": explicit_dtype = True else: raise ValueError, "may not specify '%s' by position and keyword" % k else: raise ValueError, "unexpected keyword argument '%s'" % k if inf.start is None: inf.start = 0 if inf.step is None: inf.step = 1 if inf.dtype is None: inf.dtype = numpy.array([inf.start, inf.stop, inf.step]).dtype # actual functionality ---------------------------------------------------- dtype = numpy.dtype(inf.dtype) start = dtype.type(inf.start) step = dtype.type(inf.step) stop = dtype.type(inf.stop) if not explicit_dtype: raise TypeError("arange requires dtype argument") from math import ceil size = int(ceil((stop-start)/step)) result = Array(context, (size,), dtype, queue=queue) knl = elementwise.get_arange_kernel(context, dtype) knl(queue, result._global_size, result._local_size, result.data, start, step, size) return result
def _arange_knl(result, start, step): return elementwise.get_arange_kernel(result.context, result.dtype)
def _arange(result, start, step): return elementwise.get_arange_kernel( result.context, result.dtype)
def arange(context, queue, *args, **kwargs): """Create an array filled with numbers spaced `step` apart, starting from `start` and ending at `stop`. For floating point arguments, the length of the result is `ceil((stop - start)/step)`. This rule may result in the last element of the result being greater than stop. """ # argument processing ----------------------------------------------------- # Yuck. Thanks, numpy developers. ;) from pytools import Record class Info(Record): pass explicit_dtype = False inf = Info() inf.start = None inf.stop = None inf.step = None inf.dtype = None if isinstance(args[-1], numpy.dtype): dtype = args[-1] args = args[:-1] explicit_dtype = True argc = len(args) if argc == 0: raise ValueError, "stop argument required" elif argc == 1: inf.stop = args[0] elif argc == 2: inf.start = args[0] inf.stop = args[1] elif argc == 3: inf.start = args[0] inf.stop = args[1] inf.step = args[2] else: raise ValueError, "too many arguments" admissible_names = ["start", "stop", "step", "dtype"] for k, v in kwargs.iteritems(): if k in admissible_names: if getattr(inf, k) is None: setattr(inf, k, v) if k == "dtype": explicit_dtype = True else: raise ValueError, "may not specify '%s' by position and keyword" % k else: raise ValueError, "unexpected keyword argument '%s'" % k if inf.start is None: inf.start = 0 if inf.step is None: inf.step = 1 if inf.dtype is None: inf.dtype = numpy.array([inf.start, inf.stop, inf.step]).dtype # actual functionality ---------------------------------------------------- dtype = numpy.dtype(inf.dtype) start = dtype.type(inf.start) step = dtype.type(inf.step) stop = dtype.type(inf.stop) if not explicit_dtype: raise TypeError("arange requires dtype argument") from math import ceil size = int(ceil((stop - start) / step)) result = Array(context, (size, ), dtype, queue=queue) knl = elementwise.get_arange_kernel(context, dtype) knl(queue, result._global_size, result._local_size, result.data, start, step, size) return result