Skip to content
test_loopy.py 92.4 KiB
Newer Older
            data
            )

        knl = lp.split_iname(knl, 'i', 4, inner_tag='vec')
        knl = lp.split_array_axis(knl, names, 0, 4)
        knl = lp.tag_array_axes(knl, names, 'N0,vec')

        queue = cl.CommandQueue(ctx)
        if exception is not None:
            with pytest.raises(exception):
                knl(queue, **kwargs)
        else:
            result = knl(queue, **kwargs)[1][0]
            assert np.array_equal(result.flatten('C'), answer)

    ans = np.zeros(12, dtype=np.int32)
    ans[7:] = 1
Nick Curtis's avatar
Nick Curtis committed
    from loopy.diagnostic import LoopyError
    # 1) test a conditional on a vector iname -- currently unimplemented as it
    # would require creating a 'shadow' vector iname temporary
    create_and_test('a[i] = 1', 'i > 6', ans, exception=LoopyError)
Nick Curtis's avatar
Nick Curtis committed
    create_and_test('a[i] = 1', 'b[i] > 6', ans, b=np.arange(
        12, dtype=np.int32).reshape((3, 4)))
    # 3) condition on a vector temporary
    create_and_test('a[i] = 1', '1', ans, extra_insns='<> c = i < 6')


def test_vectorizability():
    # check new vectorizability conditions
    from loopy.kernel.array import VectorArrayDimTag
    from loopy.kernel.data import VectorizeTag

    def create_and_test(insn, exception=None, a=None, b=None):
        a = np.zeros((3, 4), dtype=np.int32) if a is None else a
        data = [lp.GlobalArg('a', shape=(12,), dtype=a.dtype)]
        kwargs = dict(a=a)
        if b is not None:
            data += [lp.GlobalArg('b', shape=(12,), dtype=b.dtype)]
            kwargs['b'] = b
        names = [d.name for d in data]

        knl = lp.make_kernel(['{[i]: 0 <= i < 12}'],
            """
            for i
                %(insn)s
            end
            """ % dict(insn=insn),
            data
            )

        knl = lp.split_iname(knl, 'i', 4, inner_tag='vec')
        knl = lp.split_array_axis(knl, names, 0, 4)
        knl = lp.tag_array_axes(knl, names, 'N0,vec')
        knl = lp.preprocess_kernel(knl)
        lp.generate_code_v2(knl).device_code()
        assert knl.instructions[0].within_inames & set(['i_inner'])
        assert isinstance(knl.args[0].dim_tags[-1], VectorArrayDimTag)
        assert isinstance(knl.args[0].dim_tags[-1], VectorArrayDimTag)
        assert isinstance(knl.iname_to_tag['i_inner'], VectorizeTag)

    def run(op_list=[], unary_operators=[], func_list=[], unary_funcs=[]):
        for op in op_list:
            template = 'a[i] = a[i] %(op)s %(rval)s' \
                if op not in unary_operators else 'a[i] = %(op)s a[i]'

            create_and_test(template % dict(op=op, rval='1'))
            create_and_test(template % dict(op=op, rval='a[i]'))
        for func in func_list:
            template = 'a[i] = %(func)s(a[i], %(rval)s)' \
                if func not in unary_funcs else 'a[i] = %(func)s(a[i])'
            create_and_test(template % dict(func=func, rval='1'))
            create_and_test(template % dict(func=func, rval='a[i]'))

    # 1) comparisons
    run(['>', '>=', '<', '<=', '==', '!='])

    # 2) logical operators
    run(['and', 'or', 'not'], ['not'])

    # 3) bitwize operators
    # bitwize and '&' is broken in parsing currently (#139)
    # bitwize xor '^' not not implemented in codegen
    run(['~', '|'], ['~'])

    # 4) functions -- a random selection of the enabled math functions in opencl
    run(func_list=['acos', 'exp10', 'atan2', 'round'],
        unary_funcs=['round', 'acos', 'exp10'])
def test_check_for_variable_access_ordering():
    knl = lp.make_kernel(
            "{[i]: 0<=i<n}",
            """
            a[i] = 12
            a[i+1] = 13
            """)

    knl = lp.preprocess_kernel(knl)

    from loopy.diagnostic import VariableAccessNotOrdered
    with pytest.raises(VariableAccessNotOrdered):
        lp.get_one_scheduled_kernel(knl)


def test_check_for_variable_access_ordering_with_aliasing():
    knl = lp.make_kernel(
            "{[i]: 0<=i<n}",
            """
            a[i] = 12
            b[i+1] = 13
            """,
            [
                lp.TemporaryVariable("a", shape="n+1", base_storage="tmp"),
                lp.TemporaryVariable("b", shape="n+1", base_storage="tmp"),
                ])

    knl = lp.preprocess_kernel(knl)

    from loopy.diagnostic import VariableAccessNotOrdered
    with pytest.raises(VariableAccessNotOrdered):
        lp.get_one_scheduled_kernel(knl)


@pytest.mark.parametrize(("second_index", "expect_barrier"),
        [
            ("2*i", True),
            ("2*i+1", False),
            ])
def test_no_barriers_for_nonoverlapping_access(second_index, expect_barrier):
    knl = lp.make_kernel(
            "{[i]: 0<=i<128}",
            """
            a[2*i] = 12  {id=first}
            a[%s] = 13  {id=second,dep=first}
            """ % second_index,
            [
                lp.TemporaryVariable("a", lp.auto, shape=(256,),
                    scope=lp.temp_var_scope.LOCAL),
                ])

    knl = lp.tag_inames(knl, "i:l.0")

    knl = lp.preprocess_kernel(knl)
    knl = lp.get_one_scheduled_kernel(knl)

    assert barrier_between(knl, "first", "second") == expect_barrier


def test_half_complex_conditional(ctx_factory):
    ctx = ctx_factory()
    queue = cl.CommandQueue(ctx)

    knl = lp.make_kernel(
            "{[i]: 0 <= i < 10}",
            """
           tmp[i] = if(i < 5, 0, 0j)
           """)

    knl(queue)


if __name__ == "__main__":
    if len(sys.argv) > 1:
        exec(sys.argv[1])
    else:
        from pytest import main
Andreas Klöckner's avatar
Andreas Klöckner committed
# vim: foldmethod=marker