コード例 #1
0
ファイル: test_pytato.py プロジェクト: matthiasdiener/pytato
def test_make_placeholder_noname():
    ns = pt.Namespace()
    x = pt.make_placeholder(ns, shape=(10, 4), dtype=float)
    y = 2 * x

    knl = pt.generate_loopy(y).program

    assert x.name in knl.arg_dict
    assert x.name in knl.get_read_variables()
コード例 #2
0
def test_roll_input_validation():
    namespace = pt.Namespace()

    a = pt.make_placeholder(namespace, name="a", shape=(10, 10), dtype=np.float)
    pt.roll(a, 1, axis=0)

    with pytest.raises(ValueError):
        pt.roll(a, 1, axis=2)

    with pytest.raises(ValueError):
        pt.roll(a, 1, axis=-1)
コード例 #3
0
ファイル: array_context.py プロジェクト: kaushikcfd/meshmode
    def __init__(self, ctx, queue=None):
        import pytato as pt
        import pyopencl as cl
        super().__init__()
        self.ns = pt.Namespace()
        self.opencl_ctx = ctx
        if queue is None:
            queue = cl.CommandQueue(ctx)

        self.queue = queue
        self.np = self._get_fake_numpy_namespace()
コード例 #4
0
def test_slice_input_validation():
    namespace = pt.Namespace()

    a = pt.make_placeholder(namespace, name="a", shape=(10, 10, 10), dtype=np.float)

    a[0]
    a[0, 0]
    a[0, 0, 0]

    with pytest.raises(ValueError):
        a[0, 0, 0, 0]

    with pytest.raises(ValueError):
        a[10]
コード例 #5
0
def test_transpose_input_validation():
    namespace = pt.Namespace()

    a = pt.make_placeholder(namespace, name="a", shape=(10, 10), dtype=np.float)
    pt.transpose(a)

    with pytest.raises(ValueError):
        pt.transpose(a, (2, 0, 1))

    with pytest.raises(ValueError):
        pt.transpose(a, (1, 1))

    with pytest.raises(ValueError):
        pt.transpose(a, (0,))
コード例 #6
0
def test_transpose(ctx_factory):
    cl_ctx = ctx_factory()
    queue = cl.CommandQueue(cl_ctx)

    shape = (2, 8)

    from numpy.random import default_rng
    rng = default_rng()
    x_in = rng.random(size=shape)

    namespace = pt.Namespace()
    x = pt.make_data_wrapper(namespace, x_in)
    prog = pt.generate_loopy(x.T, target=pt.PyOpenCLTarget(queue))

    _, (x_out,) = prog()
    assert (x_out == x_in.T).all()
コード例 #7
0
def test_roll(ctx_factory, shift, axis):
    cl_ctx = ctx_factory()
    queue = cl.CommandQueue(cl_ctx)

    namespace = pt.Namespace()
    pt.make_size_param(namespace, "n")
    x = pt.make_placeholder(namespace, name="x", shape=("n", "n"), dtype=np.float)

    prog = pt.generate_loopy(
            pt.roll(x, shift=shift, axis=axis),
            target=pt.PyOpenCLTarget(queue))

    x_in = np.arange(1., 10.).reshape(3, 3)

    _, (x_out,) = prog(x=x_in)

    assert (x_out == np.roll(x_in, shift=shift, axis=axis)).all()
コード例 #8
0
def test_reshape_input_validation():
    ns = pt.Namespace()

    x = pt.make_placeholder(ns, shape=(3, 3, 4), dtype=np.float)

    assert pt.reshape(x, (-1,)).shape == (36,)
    assert pt.reshape(x, (-1, 6)).shape == (6, 6)
    assert pt.reshape(x, (4, -1)).shape == (4, 9)
    assert pt.reshape(x, (36, -1)).shape == (36, 1)

    with pytest.raises(ValueError):
        # 36 not a multiple of 25
        pt.reshape(x, (5, 5))

    with pytest.raises(ValueError):
        # 2 unknown dimensions
        pt.reshape(x, (-1, -1, 3))
コード例 #9
0
def test_stack_input_validation():
    namespace = pt.Namespace()

    x = pt.make_placeholder(namespace, name="x", shape=(10, 10), dtype=np.float)
    y = pt.make_placeholder(namespace, name="y", shape=(1, 10), dtype=np.float)

    assert pt.stack((x, x, x), axis=0).shape == (3, 10, 10)

    pt.stack((x,), axis=0)
    pt.stack((x,), axis=1)

    with pytest.raises(ValueError):
        pt.stack(())

    with pytest.raises(ValueError):
        pt.stack((x, y))

    with pytest.raises(ValueError):
        pt.stack((x, x), axis=3)
コード例 #10
0
def test_axis_permutation(ctx_factory, axes):
    cl_ctx = ctx_factory()
    queue = cl.CommandQueue(cl_ctx)

    ndim = len(axes)
    shape = (3, 4, 5)[:ndim]

    from numpy.random import default_rng
    rng = default_rng()

    x_in = rng.random(size=shape)

    namespace = pt.Namespace()
    x = pt.make_data_wrapper(namespace, x_in)
    prog = pt.generate_loopy(
            pt.transpose(x, axes),
            target=pt.PyOpenCLTarget(queue))

    _, (x_out,) = prog()
    assert (x_out == np.transpose(x_in, axes)).all()