def run(self): vecls = ['', '2', '4', '8', '16'] cltypes = ['float', 'double', 'real', 'char', 'short', 'int', 'long', 'uchar', 'ushort', 'uint', 'ulong'] shapes = [(1,), (10,), (3,4), (4,2,5)] numbers = [1, 1., 123, 123.2, 123.7, 0.12, 0.78, -1, -1., -123, -123.2, -123.7, -0.12, -0.78] for cltype in cltypes: for vecl in vecls: for shape in shapes: a_cl = CLVar(addspc='global', cltype=cltype+vecl, shape=shape) a_np = np.random.random(shape + (int(vecl),)) if not(vecl == '') else np.random.random(shape + (1,)) a_np *= 100 a_np = np.array(a_np, dtype=a_cl._value.dtype) a_cl.value = a_np b_cl = CLVar(addspc='global', cltype=cltype+vecl, shape=shape) b_np = np.random.random(shape + (int(vecl),)) if not(vecl == '') else np.random.random(shape + (1,)) b_np *= 100 b_np = np.array(b_np, dtype=b_cl._value.dtype) b_cl.value = b_np check((a_cl + b_cl).value, (a_np + b_np), 'add ' + str(cltype) + vecl) check((a_cl - b_cl).value, (a_np - b_np), 'sub ' + str(cltype) + vecl) check((a_cl * b_cl).value, (a_np * b_np), 'mul ' + str(cltype) + vecl) check((a_cl / b_cl).value, (a_np / b_np), 'div ' + str(cltype) + vecl) for num in numbers: a_cl = CLVar(addspc='global', cltype=cltype+vecl, shape=shape) a_np = np.random.random(shape + (int(vecl),)) if not(vecl == '') else np.random.random(shape + (1,)) a_np *= 100 a_np = np.array(a_np, dtype=a_cl._value.dtype) a_cl.value = a_np b_cl = CLVar(addspc='global', cltype=cltype+vecl, shape=shape) b_np = np.random.random(shape + (int(vecl),)) if not(vecl == '') else np.random.random(shape + (1,)) b_np *= 100 b_np = np.array(b_np, dtype=b_cl._value.dtype) b_cl.value = b_np check((a_cl + num).value, (a_np + num), 'add ' + str(num) + ' ' + str(cltype) + vecl) check((a_cl - num).value, (a_np - num), 'sub ' + str(num) + ' ' + str(cltype) + vecl) check((a_cl * num).value, (a_np * num), 'mul ' + str(num) + ' ' + str(cltype) + vecl) check((a_cl / num).value, (a_np / num), 'div ' + str(num) + ' ' + str(cltype) + vecl) a_cl += num a_np += num check(a_cl.value, a_np, 'iadd ' + str(num) + ' ' + str(cltype) + vecl) a_cl -= num a_np -= num check(a_cl.value, a_np, 'isub ' + str(num) + ' ' + str(cltype) + vecl) a_cl *= num a_np *= num check(a_cl.value, a_np, 'imul ' + str(num) + ' ' + str(cltype) + vecl) a_cl /= num a_np /= num check(a_cl.value, a_np, 'idiv ' + str(num) + ' ' + str(cltype) + vecl)
def clarray(inpt, dtype_cl=None, name=None): from CLVar import CLVar import numpy as np import typeconv val = None if isinstance(inpt, list): val = np.array(inpt) if isinstance(inpt, np.ndarray): val = np.array(inpt) vl = val.shape[-1] if vl in [1,2,4,8,16] and len(val.shape) > 1 and len(val.shape) <= 4: if not dtype_cl: dtype_cl = typeconv._t_np_cl[val.dtype] cltype = dtype_cl + str(vl) shape = val.shape[:-1] ret = CLVar(shape=shape, cltype=cltype) ret.value = inpt return ret else: raise ValueError('Input value must have correct shape to match a cl vector type.')
def clarray_like(var): from CLVar import CLVar return CLVar.var_like(var)