from __future__ import division, absolute_import, print_function __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import six from six.moves import range import sys import numpy as np import loopy as lp import pyopencl as cl import pyopencl.clmath # noqa import pyopencl.clrandom # noqa import pytest import logging logger = logging.getLogger(__name__) try: import faulthandler except ImportError: pass else: faulthandler.enable() from pyopencl.tools import pytest_generate_tests_for_pyopencl \ as pytest_generate_tests __all__ = [ "pytest_generate_tests", "cl" # 'cl.create_some_context' ] def test_complicated_subst(ctx_factory): #ctx = ctx_factory() knl = lp.make_kernel( "{[i]: 0<=i id:h and tag:two > id:g and tag:two") print(knl) sr_keys = list(knl.substitutions.keys()) for letter, how_many in [ ("f", 1), ("g", 1), ("h", 2) ]: substs_with_letter = sum(1 for k in sr_keys if k.startswith(letter)) assert substs_with_letter == how_many def test_type_inference_no_artificial_doubles(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[i]: 0<=i bb = a[i] - b[i] c[i] = bb """, [ lp.GlobalArg("a", np.float32, shape=("n",)), lp.GlobalArg("b", np.float32, shape=("n",)), lp.GlobalArg("c", np.float32, shape=("n",)), lp.ValueArg("n", np.int32), ], assumptions="n>=1") knl = lp.preprocess_kernel(knl, ctx.devices[0]) for k in lp.generate_loop_schedules(knl): code = lp.generate_code(k) assert "double" not in code def test_sized_and_complex_literals(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[i]: 0<=i aa = 5jf <> bb = 5j a[i] = imag(aa) b[i] = imag(bb) c[i] = 5f """, [ lp.GlobalArg("a", np.float32, shape=("n",)), lp.GlobalArg("b", np.float32, shape=("n",)), lp.GlobalArg("c", np.float32, shape=("n",)), lp.ValueArg("n", np.int32), ], assumptions="n>=1") lp.auto_test_vs_ref(knl, ctx, knl, parameters=dict(n=5)) def test_simple_side_effect(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[i,j]: 0<=i,j<100}", """ a[i] = a[i] + 1 """, [lp.GlobalArg("a", np.float32, shape=(100,))] ) knl = lp.preprocess_kernel(knl, ctx.devices[0]) kernel_gen = lp.generate_loop_schedules(knl) for gen_knl in kernel_gen: print(gen_knl) compiled = lp.CompiledKernel(ctx, gen_knl) print(compiled.get_code()) def test_owed_barriers(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[i]: 0<=i<100}", [ " z[i] = a[i]" ], [lp.GlobalArg("a", np.float32, shape=(100,))] ) knl = lp.tag_inames(knl, dict(i="l.0")) knl = lp.preprocess_kernel(knl, ctx.devices[0]) kernel_gen = lp.generate_loop_schedules(knl) for gen_knl in kernel_gen: compiled = lp.CompiledKernel(ctx, gen_knl) print(compiled.get_code()) def test_wg_too_small(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[i]: 0<=i<100}", [ " z[i] = a[i] {id=copy}" ], [lp.GlobalArg("a", np.float32, shape=(100,))], local_sizes={0: 16}) knl = lp.tag_inames(knl, dict(i="l.0")) knl = lp.preprocess_kernel(knl, ctx.devices[0]) kernel_gen = lp.generate_loop_schedules(knl) import pytest for gen_knl in kernel_gen: with pytest.raises(RuntimeError): lp.CompiledKernel(ctx, gen_knl).get_code() def test_multi_cse(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[i]: 0<=i<100}", [ " z[i] = a[i] + a[i]**2" ], [lp.GlobalArg("a", np.float32, shape=(100,))], local_sizes={0: 16}) knl = lp.split_iname(knl, "i", 16, inner_tag="l.0") knl = lp.add_prefetch(knl, "a", []) knl = lp.preprocess_kernel(knl, ctx.devices[0]) kernel_gen = lp.generate_loop_schedules(knl) for gen_knl in kernel_gen: compiled = lp.CompiledKernel(ctx, gen_knl) print(compiled.get_code()) # {{{ code generator fuzzing def make_random_value(): from random import randrange, uniform v = randrange(3) if v == 0: while True: z = randrange(-1000, 1000) if z: return z elif v == 1: return uniform(-10, 10) else: cval = uniform(-10, 10) + 1j*uniform(-10, 10) if randrange(0, 2) == 0: return np.complex128(cval) else: return np.complex128(cval) def make_random_expression(var_values, size): from random import randrange import pymbolic.primitives as p v = randrange(1500) size[0] += 1 if v < 500 and size[0] < 40: term_count = randrange(2, 5) if randrange(2) < 1: cls = p.Sum else: cls = p.Product return cls(tuple( make_random_expression(var_values, size) for i in range(term_count))) elif v < 750: return make_random_value() elif v < 1000: var_name = "var_%d" % len(var_values) assert var_name not in var_values var_values[var_name] = make_random_value() return p.Variable(var_name) elif v < 1250: # Cannot use '-' because that destroys numpy constants. return p.Sum(( make_random_expression(var_values, size), - make_random_expression(var_values, size))) elif v < 1500: # Cannot use '/' because that destroys numpy constants. return p.Quotient( make_random_expression(var_values, size), make_random_expression(var_values, size)) def generate_random_fuzz_examples(count): for i in range(count): size = [0] var_values = {} expr = make_random_expression(var_values, size) yield expr, var_values def test_fuzz_code_generator(ctx_factory): ctx = ctx_factory() queue = cl.CommandQueue(ctx) if ctx.devices[0].platform.vendor.startswith("Advanced Micro"): pytest.skip("crashes on AMD 15.12") #from expr_fuzz import get_fuzz_examples #for expr, var_values in get_fuzz_examples(): for expr, var_values in generate_random_fuzz_examples(50): from pymbolic import evaluate try: true_value = evaluate(expr, var_values) except ZeroDivisionError: continue def get_dtype(x): if isinstance(x, (complex, np.complexfloating)): return np.complex128 else: return np.float64 knl = lp.make_kernel("{ : }", [lp.Assignment("value", expr)], [lp.GlobalArg("value", np.complex128, shape=())] + [ lp.ValueArg(name, get_dtype(val)) for name, val in six.iteritems(var_values) ]) ck = lp.CompiledKernel(ctx, knl) evt, (lp_value,) = ck(queue, out_host=True, **var_values) err = abs(true_value-lp_value)/abs(true_value) if abs(err) > 1e-10: print(80*"-") print("WRONG: rel error=%g" % err) print("true=%r" % true_value) print("loopy=%r" % lp_value) print(80*"-") print(ck.get_code()) print(80*"-") print(var_values) print(80*"-") print(repr(expr)) print(80*"-") print(expr) print(80*"-") 1/0 # }}} def test_bare_data_dependency(ctx_factory): dtype = np.dtype(np.float32) ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( [ "[znirp] -> {[i]: 0<=i znirp = n", "a[i] = 1", ], [ lp.GlobalArg("a", dtype, shape=("n"), order="C"), lp.ValueArg("n", np.int32), ]) cknl = lp.CompiledKernel(ctx, knl) n = 20000 evt, (a,) = cknl(queue, n=n, out_host=True) assert a.shape == (n,) assert (a == 1).all() # {{{ test race detection @pytest.mark.skipif("sys.version_info < (2,6)") def test_ilp_write_race_detection_global(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "[n] -> {[i,j]: 0<=i,j a[i] = 5+i+j", ], []) knl = lp.tag_inames(knl, dict(i="l.0", j="ilp")) knl = lp.preprocess_kernel(knl, ctx.devices[0]) for k in lp.generate_loop_schedules(knl): assert k.temporary_variables["a"].shape == (16, 17) def test_ilp_write_race_avoidance_private(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[j]: 0<=j<16 }", [ "<> a = 5+j", ], []) knl = lp.tag_inames(knl, dict(j="ilp")) knl = lp.preprocess_kernel(knl, ctx.devices[0]) for k in lp.generate_loop_schedules(knl): assert k.temporary_variables["a"].shape == (16,) # }}} def test_write_parameter(ctx_factory): dtype = np.float32 ctx = ctx_factory() knl = lp.make_kernel( "{[i,j]: 0<=i,j src_ibox = source_boxes[isrc_box] <> isrc_start = box_source_starts[src_ibox] <> isrc_end = isrc_start+box_source_counts_nonchild[src_ibox] <> strength = strengths[isrc] {id=set_strength} """, [ lp.GlobalArg("box_source_starts,box_source_counts_nonchild", None, shape=None), lp.GlobalArg("strengths", None, shape="nsources"), "..."]) print(knl) assert "isrc_box" in knl.insn_inames("set_strength") print(lp.CompiledKernel(ctx, knl).get_highlighted_code( dict( source_boxes=np.int32, box_source_starts=np.int32, box_source_counts_nonchild=np.int32, strengths=np.float64, nsources=np.int32, ))) def test_inames_deps_from_write_subscript(ctx_factory): knl = lp.make_kernel( "{[i,j]: 0<=i,j src_ibox = source_boxes[i] something = 5 a[src_ibox] = sum(j, something) {id=myred} """, [ lp.GlobalArg("box_source_starts,box_source_counts_nonchild,a", None, shape=None), "..."]) print(knl) assert "i" in knl.insn_inames("myred") def test_modulo_indexing(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[i,j]: 0<=i my_a = a[i,j] {id=read_a} <> a_less_than_zero = my_a < 0 {dep=read_a,inames=i:j} my_a = 2*my_a {id=twice_a,dep=read_a,if=a_less_than_zero} my_a = my_a+1 {id=aplus,dep=twice_a,if=a_less_than_zero} out[i,j] = 2*my_a {dep=aplus} """, [ lp.GlobalArg("a", np.float32, shape=lp.auto), lp.GlobalArg("out", np.float32, shape=lp.auto), "..." ]) ref_knl = knl lp.auto_test_vs_ref(ref_knl, ctx, knl, parameters=dict( n=200 )) def test_ilp_loop_bound(ctx_factory): # The salient bit of this test is that a joint bound on (outer, inner) # from a split occurs in a setting where the inner loop has been ilp'ed. # In 'normal' parallel loops, the inner index is available for conditionals # throughout. In ILP'd loops, not so much. ctx = ctx_factory() knl = lp.make_kernel( "{ [i,j,k]: 0<=i,j,k temp[i, 0] = 17 temp[i, 1] = 15 """) knl = lp.tag_inames(knl, dict(i="l.0")) knl = lp.preprocess_kernel(knl) for k in lp.generate_loop_schedules(knl): code, _ = lp.generate_code(k) print(code) def test_make_copy_kernel(ctx_factory): ctx = ctx_factory() queue = cl.CommandQueue(ctx) intermediate_format = "f,f,sep" a1 = np.random.randn(1024, 4, 3) cknl1 = lp.make_copy_kernel(intermediate_format) cknl1 = lp.fix_parameters(cknl1, n2=3) cknl1 = lp.set_options(cknl1, write_cl=True) evt, a2 = cknl1(queue, input=a1) cknl2 = lp.make_copy_kernel("c,c,c", intermediate_format) cknl2 = lp.fix_parameters(cknl2, n2=3) evt, a3 = cknl2(queue, input=a2) assert (a1 == a3).all() def test_auto_test_can_detect_problems(ctx_factory): ctx = ctx_factory() ref_knl = lp.make_kernel( "{[i,j]: 0<=i,jt = i ... gbarrier out[i] = t end """, seq_dependencies=True) if hw_loop: knl = lp.tag_inames(knl, dict(i="g.0")) save_and_reload_temporaries_test(queue, knl, np.arange(8), debug) def test_save_of_private_array(ctx_factory, debug=False): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "{ [i]: 0<=i<8 }", """ for i <>t[i] = i ... gbarrier out[i] = t[i] end """, seq_dependencies=True) knl = lp.set_temporary_scope(knl, "t", "private") save_and_reload_temporaries_test(queue, knl, np.arange(8), debug) def test_save_of_private_array_in_hw_loop(ctx_factory, debug=False): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "{ [i,j,k]: 0<=i,j,k<8 }", """ for i for j <>t[j] = j end ... gbarrier for k out[i,k] = t[k] end end """, seq_dependencies=True) knl = lp.tag_inames(knl, dict(i="g.0")) knl = lp.set_temporary_scope(knl, "t", "private") save_and_reload_temporaries_test( queue, knl, np.vstack((8 * (np.arange(8),))), debug) def test_save_of_private_multidim_array(ctx_factory, debug=False): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "{ [i,j,k,l,m]: 0<=i,j,k,l,m<8 }", """ for i for j, k <>t[j,k] = k end ... gbarrier for l, m out[i,l,m] = t[l,m] end end """, seq_dependencies=True) knl = lp.set_temporary_scope(knl, "t", "private") result = np.array([np.vstack((8 * (np.arange(8),))) for i in range(8)]) save_and_reload_temporaries_test(queue, knl, result, debug) def test_save_of_private_multidim_array_in_hw_loop(ctx_factory, debug=False): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "{ [i,j,k,l,m]: 0<=i,j,k,l,m<8 }", """ for i for j, k <>t[j,k] = k end ... gbarrier for l, m out[i,l,m] = t[l,m] end end """, seq_dependencies=True) knl = lp.set_temporary_scope(knl, "t", "private") knl = lp.tag_inames(knl, dict(i="g.0")) result = np.array([np.vstack((8 * (np.arange(8),))) for i in range(8)]) save_and_reload_temporaries_test(queue, knl, result, debug) @pytest.mark.parametrize("hw_loop", [True, False]) def test_save_of_multiple_private_temporaries(ctx_factory, hw_loop, debug=False): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "{ [i,j,k]: 0<=i,j,k<10 }", """ for i for k <> t_arr[k] = k end <> t_scalar = 1 for j ... gbarrier out[j] = t_scalar ... gbarrier t_scalar = 10 end ... gbarrier <> flag = i == 9 out[i] = t_arr[i] {if=flag} end """, seq_dependencies=True) knl = lp.set_temporary_scope(knl, "t_arr", "private") if hw_loop: knl = lp.tag_inames(knl, dict(i="g.0")) result = np.array([1, 10, 10, 10, 10, 10, 10, 10, 10, 9]) save_and_reload_temporaries_test(queue, knl, result, debug) def test_save_of_local_array(ctx_factory, debug=False): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "{ [i,j]: 0<=i,j<8 }", """ for i, j <>t[2*j] = j t[2*j+1] = j ... gbarrier out[i] = t[2*i] end """, seq_dependencies=True) knl = lp.set_temporary_scope(knl, "t", "local") knl = lp.tag_inames(knl, dict(i="g.0", j="l.0")) save_and_reload_temporaries_test(queue, knl, np.arange(8), debug) def test_save_local_multidim_array(ctx_factory, debug=False): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "{ [i,j,k]: 0<=i<2 and 0<=k<3 and 0<=j<2}", """ for i, j, k ... gbarrier <> t_local[k,j] = 1 ... gbarrier out[k,i*2+j] = t_local[k,j] end """, seq_dependencies=True) knl = lp.set_temporary_scope(knl, "t_local", "local") knl = lp.tag_inames(knl, dict(j="l.0", i="g.0")) save_and_reload_temporaries_test(queue, knl, 1, debug) def test_missing_temporary_definition_detection(): knl = lp.make_kernel( "{ [i]: 0<=i<10 }", """ for i <> t = 1 ... gbarrier out[i] = t end """, seq_dependencies=True) from loopy.diagnostic import MissingDefinitionError with pytest.raises(MissingDefinitionError): lp.generate_code_v2(knl) def test_missing_definition_check_respects_aliases(): # Based on https://github.com/inducer/loopy/issues/69 knl = lp.make_kernel("{ [i] : 0<=i c[i] = a[i + 1] ... gbarrier out[i] = c[i] end """, seq_dependencies=True) knl = lp.add_and_infer_dtypes(knl, {"a": np.float32, "c": np.float32, "out": np.float32, "n": np.int32}) knl = lp.set_temporary_scope(knl, "c", "global") ref_knl = knl knl = lp.split_iname(knl, "i", 128, outer_tag="g.0", inner_tag="l.0") cgr = lp.generate_code_v2(knl) assert len(cgr.device_programs) == 2 #print(cgr.device_code()) #print(cgr.host_code()) lp.auto_test_vs_ref(ref_knl, ctx, knl, parameters=dict(n=5)) def test_assign_to_linear_subscript(ctx_factory): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl1 = lp.make_kernel( "{ [i]: 0<=i aa = 5jf <> bb = 5j a[i] = imag(aa) b[i] = imag(bb) c[i] = 5f end """, seq_dependencies=True) print(knl.stringify(with_dependencies=True)) lp.auto_test_vs_ref(knl, ctx, knl, parameters=dict(n=5)) def test_nop(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[i,itrip]: 0<=i z[i] = z[i+1] + z[i] {id=wr_z} <> v[i] = 11 {id=wr_v} ... nop {dep=wr_z:wr_v,id=yoink} z[i] = z[i] - z[i+1] + v[i] end """) print(knl) knl = lp.fix_parameters(knl, n=15) knl = lp.add_and_infer_dtypes(knl, {"z": np.float64}) lp.auto_test_vs_ref(knl, ctx, knl, parameters=dict(ntrips=5)) def test_global_barrier(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel( "{[i,itrip]: 0<=i z[i] = z[i+1] + z[i] {id=wr_z,dep=top} <> v[i] = 11 {id=wr_v,dep=top} ... gbarrier {dep=wr_z:wr_v,id=yoink} z[i] = z[i] - z[i+1] + v[i] {id=iupd} end ... gbarrier {dep=iupd,id=postloop} z[i] = z[i] - z[i+1] + v[i] {dep=postloop} end """) knl = lp.fix_parameters(knl, ntrips=3) knl = lp.add_and_infer_dtypes(knl, {"z": np.float64}) ref_knl = knl ref_knl = lp.set_temporary_scope(ref_knl, "z", "global") ref_knl = lp.set_temporary_scope(ref_knl, "v", "global") knl = lp.split_iname(knl, "i", 256, outer_tag="g.0", inner_tag="l.0") print(knl) knl = lp.preprocess_kernel(knl) assert knl.temporary_variables["z"].scope == lp.temp_var_scope.GLOBAL assert knl.temporary_variables["v"].scope == lp.temp_var_scope.GLOBAL print(knl) lp.auto_test_vs_ref(ref_knl, ctx, knl, parameters=dict(ntrips=5, n=10)) def test_missing_global_barrier(): knl = lp.make_kernel( "{[i,itrip]: 0<=i z[i] = z[i] - z[i+1] {id=iupd,dep=yoink} end # This is where the barrier should be z[i] = z[i] - z[i+1] + v[i] {dep=iupd} end """) knl = lp.set_temporary_scope(knl, "z", "global") knl = lp.split_iname(knl, "i", 256, outer_tag="g.0") knl = lp.preprocess_kernel(knl) from loopy.diagnostic import MissingBarrierError with pytest.raises(MissingBarrierError): lp.get_one_scheduled_kernel(knl) def test_index_cse(ctx_factory): knl = lp.make_kernel(["{[i,j,k,l,m]:0<=i,j,k,l,m Tcond = T[k] < 0.5 if Tcond cp[k] = 2 * T[k] + Tcond end end """) knl = lp.fix_parameters(knl, n=200) knl = lp.add_and_infer_dtypes(knl, {"T": np.float32}) ref_knl = knl knl = lp.split_iname(knl, 'k', 2, inner_tag='ilp') lp.auto_test_vs_ref(ref_knl, ctx, knl) def test_unr_and_conditionals(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel('{[k]: 0<=k Tcond[k] = T[k] < 0.5 if Tcond[k] cp[k] = 2 * T[k] + Tcond[k] end end """) knl = lp.fix_parameters(knl, n=200) knl = lp.add_and_infer_dtypes(knl, {"T": np.float32}) ref_knl = knl knl = lp.split_iname(knl, 'k', 2, inner_tag='unr') lp.auto_test_vs_ref(ref_knl, ctx, knl) def test_constant_array_args(ctx_factory): ctx = ctx_factory() knl = lp.make_kernel('{[k]: 0<=k Tcond[k] = T[k] < 0.5 if Tcond[k] cp[k] = 2 * T[k] + Tcond[k] end end """, [lp.ConstantArg('T', shape=(200,), dtype=np.float32), '...']) knl = lp.fix_parameters(knl, n=200) lp.auto_test_vs_ref(knl, ctx, knl) @pytest.mark.parametrize("src_order", ["C"]) @pytest.mark.parametrize("tmp_order", ["C", "F"]) def test_temp_initializer(ctx_factory, src_order, tmp_order): a = np.random.randn(3, 3).copy(order=src_order) ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "{[i,j]: 0<=i,j { [j] : 2 * i - 2 < j <= 2 * i and 0 <= j <= 9 }"], """ for i for j out[j] = j end end """, silenced_warnings="write_race(insn)") knl = lp.split_iname(knl, "i", 5, inner_tag="l.0", outer_tag="g.0") evt, (out,) = knl(queue, out_host=True) assert (out == np.arange(10)).all() def test_tight_loop_bounds_codegen(): knl = lp.make_kernel( ["{ [i] : 0 <= i <= 5 }", "[i] -> { [j] : 2 * i - 2 <= j <= 2 * i and 0 <= j <= 9 }"], """ for i for j out[j] = j end end """, silenced_warnings="write_race(insn)", target=lp.OpenCLTarget()) knl = lp.split_iname(knl, "i", 5, inner_tag="l.0", outer_tag="g.0") cgr = lp.generate_code_v2(knl) #print(cgr.device_code()) for_loop = \ "for (int j = " \ "(lid(0) == 0 && gid(0) == 0 ? 0 : -2 + 10 * gid(0) + 2 * lid(0)); " \ "j <= (lid(0) == 0 && -1 + gid(0) == 0 ? 9 : 2 * lid(0)); ++j)" assert for_loop in cgr.device_code() def test_unscheduled_insn_detection(): knl = lp.make_kernel( "{ [i]: 0 <= i < 10 }", """ out[i] = i {id=insn1} """, "...") knl = lp.get_one_scheduled_kernel(lp.preprocess_kernel(knl)) insn1, = lp.find_instructions(knl, "id:insn1") knl.instructions.append(insn1.copy(id="insn2")) from loopy.diagnostic import UnscheduledInstructionError with pytest.raises(UnscheduledInstructionError): lp.generate_code(knl) def test_integer_reduction(): n = 200 for vtype in [np.int32, np.int64]: var_int = np.random.randint(1000, size=n, dtype=vtype) var_lp = lp.TemporaryVariable('var', initializer=var_int, scope=scopes.PRIVATE, read_only=True, dtype=vtype, shape=lp.auto) reductions = [('max', lambda x: x == np.max(var_int)), ('min', lambda x: x == np.min(var_int)), ('sum', lambda x: x == np.sum(var_int)), ('product', lambda x: x == np.prod(var_int)), ('argmax', lambda x: (x[0] == np.max(var_int) and x[1] == np.argmax(var_int))), ('argmin', lambda x: (x[0] == np.min(var_int) and x[1] == np.argmin(var_int)))] for reduction, function in reductions: kstr = (("out" if not 'arg' in reduction else "out[0], out[1]") + ' = {}(k, var[k])'.format(reduction)) knl = lp.make_kernel('{[k]: 0<=k 1: exec(sys.argv[1]) else: from py.test.cmdline import main main([__file__]) # vim: foldmethod=marker