diff --git a/doc/design.rst b/doc/design.rst index a569511ec98e9971a8b358a3821d76e66c8ae81e..c21422d9e3750407b1e7f065ba0a594a7a45c195 100644 --- a/doc/design.rst +++ b/doc/design.rst @@ -121,6 +121,8 @@ Reserved Identifiers - ``_pt_out``: The default name of an unnamed output argument + - ``_pt_in``: The default name of an unnamed placeholder argument + - ``_pt_data``: Used to automatically generate identifiers for names of :class:`~pytato.array.DataWrapper` arguments that are not supplied by the user. diff --git a/examples/advection.py b/examples/advection.py index a599b057a27669073143268156ac1c2fafbe7c48..69b4ebe48837f89a9d7953e25fa9083a6bb9c86c 100755 --- a/examples/advection.py +++ b/examples/advection.py @@ -149,7 +149,7 @@ def test_advection_convergence(order, flux_type): pt.make_size_param(ns, "nelements") pt.make_size_param(ns, "nnodes") u = pt.make_placeholder(ns, - "u", shape="(nelements, nnodes)", dtype=np.float64) + name="u", shape="(nelements, nnodes)", dtype=np.float64) op = AdvectionOperator(discr, c=1, flux_type=flux_type, dg_ops=DGOps1D(discr, ns)) result = op.apply(u) @@ -182,7 +182,7 @@ def main(): pt.make_size_param(ns, "nelements") pt.make_size_param(ns, "nnodes") u = pt.make_placeholder(ns, - "u", shape="(nelements, nnodes)", dtype=np.float64) + name="u", shape="(nelements, nnodes)", dtype=np.float64) op = AdvectionOperator(discr, c=1, flux_type="central", dg_ops=DGOps1D(discr, ns)) result = op.apply(u) diff --git a/examples/visualization.py b/examples/visualization.py index f41f33b93c38dc0fdfbaae47a3e1338f30502177..56a72e66152b1dd4a024c8f99b07c7ff177d1ef0 100755 --- a/examples/visualization.py +++ b/examples/visualization.py @@ -20,7 +20,7 @@ def main(): ns = pt.Namespace() pt.make_size_param(ns, "n") - array = pt.make_placeholder(ns, "array", shape="n", dtype=np.float) + array = pt.make_placeholder(ns, name="array", shape="n", dtype=np.float) stack = pt.stack([array, 2*array, array + 6]) ns.assign("stack", stack) result = stack @ stack.T diff --git a/pytato/array.py b/pytato/array.py index 1b5406f163b444070f0eaf1435e4a899f29470eb..33cc4788df084f47650d9abd7048a2653c614bef 100644 --- a/pytato/array.py +++ b/pytato/array.py @@ -1537,21 +1537,22 @@ def make_dict_of_named_arrays(data: Dict[str, Array]) -> DictOfNamedArrays: def make_placeholder(namespace: Namespace, - name: str, shape: ConvertibleToShape, dtype: Any, + name: Optional[str] = None, tags: Optional[TagsType] = None) -> Placeholder: """Make a :class:`Placeholder` object. :param namespace: namespace of the placeholder array - :param name: name of the placeholder array + :param name: name of the placeholder array, generated automatically + if not given :param shape: shape of the placeholder array :param dtype: dtype of the placeholder array (must be convertible to :class:`numpy.dtype`) :param tags: implementation tags """ if name is None: - raise ValueError("Placeholder instances must have a name") + name = namespace.name_gen("_pt_in") if not name.isidentifier(): raise ValueError(f"'{name}' is not a Python identifier") diff --git a/test/test_codegen.py b/test/test_codegen.py index 4d901b27d1e34864e4765d47bbec38c2279f266c..53721f2a620fd277a0e9a869206b13dd4f0da16e 100755 --- a/test/test_codegen.py +++ b/test/test_codegen.py @@ -69,8 +69,8 @@ def test_size_param(ctx_factory): queue = cl.CommandQueue(ctx) namespace = pt.Namespace() - n = pt.make_size_param(namespace, "n") - pt.make_placeholder(namespace, "x", "(n,)", np.int) + n = pt.make_size_param(namespace, name="n") + pt.make_placeholder(namespace, name="x", shape="(n,)", dtype=np.int) prog = pt.generate_loopy(n, target=pt.PyOpenCLTarget(queue)) x_in = np.array([1, 2, 3, 4, 5]) _, (n_out,) = prog(x=x_in) @@ -157,7 +157,7 @@ def test_roll(ctx_factory, shift, axis): namespace = pt.Namespace() pt.make_size_param(namespace, "n") - x = pt.make_placeholder(namespace, "x", shape=("n", "n"), dtype=np.float) + x = pt.make_placeholder(namespace, name="x", shape=("n", "n"), dtype=np.float) prog = pt.generate_loopy( pt.roll(x, shift=shift, axis=axis), diff --git a/test/test_pytato.py b/test/test_pytato.py index 49444fa599e3a8940c195020d20203f24e1d3598..76e11efaee25c8c77e915644e45c0842b6b62e0f 100755 --- a/test/test_pytato.py +++ b/test/test_pytato.py @@ -33,25 +33,25 @@ import pytato as pt def test_matmul_input_validation(): namespace = pt.Namespace() - a = pt.make_placeholder(namespace, "a", (10, 10), np.float) - b = pt.make_placeholder(namespace, "b", (20, 10), np.float) + a = pt.make_placeholder(namespace, name="a", shape=(10, 10), dtype=np.float) + b = pt.make_placeholder(namespace, name="b", shape=(20, 10), dtype=np.float) with pytest.raises(ValueError): a @ b - c = pt.make_placeholder(namespace, "c", (), np.float) + c = pt.make_placeholder(namespace, name="c", shape=(), dtype=np.float) with pytest.raises(ValueError): c @ c pt.make_size_param(namespace, "n") - d = pt.make_placeholder(namespace, "d", "(n, n)", np.float) + d = pt.make_placeholder(namespace, name="d", shape="(n, n)", dtype=np.float) d @ d def test_roll_input_validation(): namespace = pt.Namespace() - a = pt.make_placeholder(namespace, "a", (10, 10), np.float) + a = pt.make_placeholder(namespace, name="a", shape=(10, 10), dtype=np.float) pt.roll(a, 1, axis=0) with pytest.raises(ValueError): @@ -64,7 +64,7 @@ def test_roll_input_validation(): def test_transpose_input_validation(): namespace = pt.Namespace() - a = pt.make_placeholder(namespace, "a", (10, 10), np.float) + a = pt.make_placeholder(namespace, name="a", shape=(10, 10), dtype=np.float) pt.transpose(a) with pytest.raises(ValueError): @@ -80,7 +80,7 @@ def test_transpose_input_validation(): def test_slice_input_validation(): namespace = pt.Namespace() - a = pt.make_placeholder(namespace, "a", (10, 10, 10), np.float) + a = pt.make_placeholder(namespace, name="a", shape=(10, 10, 10), dtype=np.float) a[0] a[0, 0] @@ -96,8 +96,8 @@ def test_slice_input_validation(): def test_stack_input_validation(): namespace = pt.Namespace() - x = pt.make_placeholder(namespace, "x", (10, 10), np.float) - y = pt.make_placeholder(namespace, "y", (1, 10), np.float) + 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) @@ -114,6 +114,17 @@ def test_stack_input_validation(): pt.stack((x, x), axis=3) +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() + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1])