def cli(): # default behavior if len(sys.argv) == 1: import numpy as np rand = NumPyRVG(limits=(0, 1)) print(rand(np.float32)) return args = parser.parse_args() if args.numpy is None: sys.stderr.write( 'Please provide a generator flag, like --numpy <dtype>\n') exit(1) if args.numpy: import numpy as np if len(args.limits) == 1: rand = NumPyRVG(limit=args.limits[0]) else: rand = NumPyRVG(limits=args.limits) try: vals = rand(eval('np.' + args.numpy), shape=args.samples) except AttributeError: sys.stderr.write('numpy does not have the type `' + args.numpy + '`\n') exit(1) if args.samples is None: print(vals) else: for val in vals: print(val)
def test_nested_structured_dtypes_simple(): rand = NumPyRVG(limits=(0, 100)) dtypes = [[randtype(), randtype()], [randtype(), randtype()], [randtype(), randtype()]] name = randname() struct1 = np.dtype([(name + '1', dtypes[0][0]), (name + '2', dtypes[0][1])]) struct2 = np.dtype([(name + '3', dtypes[1][0]), (name + '4', dtypes[1][1])]) struct3 = np.dtype([(name + '5', dtypes[2][0]), (name + '6', dtypes[2][1])]) nested_struct_dtype_simple = np.dtype([ ('struct1', struct1), ('struct2', struct2), ('struct3', struct3), ]) val = rand(nested_struct_dtype_simple) # recursive structural equality check assert val.dtype == nested_struct_dtype_simple and len(val) == 3
def test_a_b_limits_proper_usage(): a, b = -17, 42 rand = NumPyRVG(limits=(a, b)) vals = rand(np.int32, shape=100) assert (vals >= a).all() and (vals <= b).all()
def test_a_b_limits_improper_usage(): a, b = 1, 1000 rand = NumPyRVG(limits=(a, b)) with pytest.raises(ValueError) as e: rand(np.int8, shape=100, type_limits=False) assert 'high is out of bounds for int8' in str(e)
def test_array_dtypes(): for dtype, lims in zip(intdtypes + floatdtypes, intlimits + floatlimits): rand = NumPyRVG(limits=lims) samples = np.random.randint(10, 100) arr = rand(dtype, shape=samples) assert isinstance(arr, np.ndarray) assert len(arr) == samples assert arr.dtype == dtype
def test_a_b_limits_errors(): a, b = 2, 1 with pytest.raises(ValueError) as e: NumPyRVG(limits=(a, b)) assert str( e.value ) == 'the lower limit must be strictly less than the upper limit' a, b = -10, -5 with pytest.warns( Warning, match='value ' + str(b) + ' as the upper limit will cause a runtime error if generation of values of unsigned type is attempted' ): rand = NumPyRVG(limits=(a, b)) with pytest.raises(ValueError) as e: rand(np.uint32) assert str(e.value).startswith('unproper limits')
def test_nested_structured_dtypes(): rand = NumPyRVG(limits=(0, 100)) for _ in range(10): members = np.random.randint(2, 100) samples = np.random.randint(10, 100) nested_struct_dtype = np.dtype([ (''.join(np.random.choice(letters) for _ in range(5)), create_struct_dtype()) for _ in range(members) ]) # TODO: a more complete test rand(nested_struct_dtype, shape=samples)
def test_structured_dtypes(): rand = NumPyRVG(limits=(0, 100)) for _ in range(10): samples = np.random.randint(10, 100) struct_dtype = create_struct_dtype() scl = rand(struct_dtype) assert scl.dtype == struct_dtype arr = rand(struct_dtype, shape=samples) assert isinstance(arr, np.ndarray) assert len(arr) == samples assert arr.dtype == struct_dtype
def init_kernel_arguments(context, args, arg_types, gsize): arg_bufs, which_are_scalar = [], [] hidden_global_hostbuf, hidden_global_buf = None, None mem_flags = cl.mem_flags.READ_WRITE | cl.mem_flags.COPY_HOST_PTR rand = NumPyRVG(limit=gsize) for (argname, argtypename, argaddrqual), argtype in zip(args, arg_types.values()): # special handling of oclude hidden buffers if argname == hidden_counter_name_local: which_are_scalar.append(None) arg_bufs.append(cl.LocalMemory(len(llvm_instructions) * argtype(0).itemsize)) continue if argname == hidden_counter_name_global: which_are_scalar.append(None) hidden_global_hostbuf = np.zeros(len(llvm_instructions), dtype=argtype) hidden_global_buf = cl.Buffer(context, mem_flags, hostbuf=hidden_global_hostbuf) arg_bufs.append(hidden_global_buf) continue argtypename_split = argtypename.split('*') argtypename_base = argtypename_split[0] arg_is_local = argaddrqual == 'local' # argument is scalar if len(argtypename_split) == 1: which_are_scalar.append(argtype) val = rand(argtype) arg_bufs.append(val if not arg_is_local else cl.LocalMemory(val.itemsize)) # argument is buffer else: which_are_scalar.append(None) val = rand(argtype, gsize) arg_bufs.append( cl.Buffer(context, mem_flags, hostbuf=val) if not arg_is_local else cl.LocalMemory(len(val) * val.dtype.itemsize) ) return arg_bufs, which_are_scalar, hidden_global_hostbuf, hidden_global_buf
def test_scalar_dtypes(): for dtype, lims in zip(intdtypes + floatdtypes, intlimits + floatlimits): rand = NumPyRVG(limits=lims) for _ in range(50): val = rand(dtype) assert isinstance(val, dtype)
def test_negative_limit(): neglim = np.random.randint(-100, 0) with pytest.raises(ValueError) as e: NumPyRVG(limit=neglim) assert str(e.value) == 'argument `limit` must be a number greater than 0'