def __rdivmod__(self, other): if not isinstance(other, array.GpuArray): other = np.asarray(other) odtype = get_common_dtype(other, self, True) a_arg = as_argument(other, 'a') b_arg = as_argument(self, 'b') args = [ArrayArg(odtype, 'div'), ArrayArg(odtype, 'mod'), a_arg, b_arg] div = self._empty_like_me(dtype=odtype) mod = self._empty_like_me(dtype=odtype) if odtype.kind == 'f': tmpl = "div[i] = floor((%(out_t)s)%(a)s / (%(out_t)s)%(b)s)," \ "mod[i] = fmod((%(out_t)s)%(a)s, (%(out_t)s)%(b)s)" else: tmpl = "div[i] = (%(out_t)s)%(a)s / (%(out_t)s)%(b)s," \ "mod[i] = %(a)s %% %(b)s" ksrc = tmpl % {'a': a_arg.expr(), 'b': b_arg.expr(), 'out_t': dtype_to_ctype(odtype)} k = ElemwiseKernel(self.context, args, ksrc) k(div, mod, other, self, broadcast=True) return (div, mod)
def __rdivmod__(self, other): if not isinstance(other, array.GpuArray): other = np.asarray(other) odtype = get_common_dtype(other, self, True) a_arg = as_argument(other, 'a') b_arg = as_argument(self, 'b') args = [ArrayArg(odtype, 'div'), ArrayArg(odtype, 'mod'), a_arg, b_arg] div = self._empty_like_me(dtype=odtype) mod = self._empty_like_me(dtype=odtype) if odtype.kind == 'f': tmpl = "div[i] = floor((%(out_t)s)%(a)s / (%(out_t)s)%(b)s)," \ "mod[i] = fmod((%(out_t)s)%(a)s, (%(out_t)s)%(b)s)" else: tmpl = "div[i] = (%(out_t)s)%(a)s / (%(out_t)s)%(b)s," \ "mod[i] = %(a)s %% %(b)s" ksrc = tmpl % { 'a': a_arg.expr(), 'b': b_arg.expr(), 'out_t': dtype_to_ctype(odtype) } k = ElemwiseKernel(self.context, args, ksrc) k(div, mod, other, self, broadcast=True) return (div, mod)
def __imod__(self, other): out_dtype = get_common_dtype(self, other, self.dtype == np.float64) kw = {'broadcast': True} if out_dtype == np.float32: kw['op_tmpl'] = "a[i] = fmod((float)a[i], (float)%(b)s)" if out_dtype == np.float64: kw['op_tmpl'] = "a[i] = fmod((double)a[i], (double)%(b)s)" return ielemwise2(self, '%', other, **kw)
def __imod__(self, other): out_dtype = get_common_dtype(self, other, self.dtype == np.float64) kw = {'broadcast': True} if out_dtype == np.float32: kw['op_tmpl'] = "a[i] = fmod((float)a[i], (float)%(b)s)" if out_dtype == np.float64: kw['op_tmpl'] = "a[i] = fmod((double)a[i], (double)%(b)s)" return ielemwise2(self, '%', other, **kw)
def elemwise2(a, op, b, ary, odtype=None, oper=None, op_tmpl="res[i] = (%(out_t)s)%(a)s %(op)s (%(out_t)s)%(b)s", broadcast=False): ndim_extend = True if not isinstance(a, gpuarray.GpuArray): a = numpy.asarray(a) ndim_extend = False if not isinstance(b, gpuarray.GpuArray): b = numpy.asarray(b) ndim_extend = False if odtype is None: odtype = get_common_dtype(a, b, True) a_arg = as_argument(a, 'a') b_arg = as_argument(b, 'b') args = [ArrayArg(odtype, 'res'), a_arg, b_arg] if ndim_extend: if a.ndim != b.ndim: nd = max(a.ndim, b.ndim) if a.ndim < nd: a = a.reshape(((1, ) * (nd - a.ndim)) + a.shape) if b.ndim < nd: b = b.reshape(((1, ) * (nd - b.ndim)) + b.shape) out_shape = tuple(max(sa, sb) for sa, sb in zip(a.shape, b.shape)) res = gpuarray.empty(out_shape, dtype=odtype, context=ary.context, cls=ary.__class__) else: res = ary._empty_like_me(dtype=odtype) if oper is None: oper = op_tmpl % { 'a': a_arg.expr(), 'op': op, 'b': b_arg.expr(), 'out_t': dtype_to_ctype(odtype) } k = ElemwiseKernel(ary.context, args, oper) k(res, a, b, broadcast=broadcast) return res
def elemwise2(a, op, b, ary, odtype=None, oper=None, op_tmpl="res[i] = (%(out_t)s)%(a)s %(op)s (%(out_t)s)%(b)s", broadcast=False): ndim_extend = True if not isinstance(a, gpuarray.GpuArray): a = numpy.asarray(a) ndim_extend = False if not isinstance(b, gpuarray.GpuArray): b = numpy.asarray(b) ndim_extend = False if odtype is None: odtype = get_common_dtype(a, b, True) a_arg = as_argument(a, 'a') b_arg = as_argument(b, 'b') args = [ArrayArg(odtype, 'res'), a_arg, b_arg] if ndim_extend: if a.ndim != b.ndim: nd = max(a.ndim, b.ndim) if a.ndim < nd: a = a.reshape(((1,) * (nd - a.ndim))+a.shape) if b.ndim < nd: b = b.reshape(((1,) * (nd - b.ndim))+b.shape) out_shape = tuple(max(sa, sb) for sa, sb in zip(a.shape, b.shape)) res = gpuarray.empty(out_shape, dtype=odtype, context=ary.context, cls=ary.__class__) else: res = ary._empty_like_me(dtype=odtype) if oper is None: oper = op_tmpl % {'a': a_arg.expr(), 'op': op, 'b': b_arg.expr(), 'out_t': dtype_to_ctype(odtype)} k = ElemwiseKernel(ary.context, args, oper) k(res, a, b, broadcast=broadcast) return res
def __rfloordiv__(self, other): out_dtype = get_common_dtype(other, self, True) kw = {'broadcast': True} if out_dtype.kind == 'f': kw['op_tmpl'] = "res[i] = floor((%(out_t)s)%(a)s / (%(out_t)s)%(b)s)" return elemwise2(other, '/', self, self, odtype=out_dtype, **kw)
def __mod__(self, other): out_dtype = get_common_dtype(self, other, True) kw = {'broadcast': True} if out_dtype.kind == 'f': kw['op_tmpl'] = "res[i] = fmod((%(out_t)s)%(a)s, (%(out_t)s)%(b)s)" return elemwise2(self, '%', other, self, odtype=out_dtype, **kw)
def __rfloordiv__(self, other): out_dtype = get_common_dtype(other, self, True) kw = {'broadcast': True} if out_dtype.kind == 'f': kw['op_tmpl'] = "res[i] = floor((%(out_t)s)%(a)s / (%(out_t)s)%(b)s)" return elemwise2(other, '/', self, self, odtype=out_dtype, **kw)
def __mod__(self, other): out_dtype = get_common_dtype(self, other, True) kw = {'broadcast': True} if out_dtype.kind == 'f': kw['op_tmpl'] = "res[i] = fmod((%(out_t)s)%(a)s, (%(out_t)s)%(b)s)" return elemwise2(self, '%', other, self, odtype=out_dtype, **kw)