Esempio n. 1
0
    def test_random_id(self):
        from eve.utils import UIDGenerator

        a = UIDGenerator.random_id()
        b = UIDGenerator.random_id()
        c = UIDGenerator.random_id()
        assert a != b and a != c and b != c
        assert UIDGenerator.random_id(prefix="abcde").startswith("abcde")
        assert len(UIDGenerator.random_id(width=10)) == 10
        with pytest.raises(ValueError, match="Width"):
            UIDGenerator.random_id(width=-1)
        with pytest.raises(ValueError, match="Width"):
            UIDGenerator.random_id(width=4)
Esempio n. 2
0
    def test_sequential_id(self):
        from eve.utils import UIDGenerator

        i = UIDGenerator.sequential_id()
        assert UIDGenerator.sequential_id() != i
        assert UIDGenerator.sequential_id(prefix="abcde").startswith("abcde")
        assert len(UIDGenerator.sequential_id(width=10)) == 10
        assert not UIDGenerator.sequential_id().startswith("0")
        with pytest.raises(ValueError, match="Width"):
            UIDGenerator.sequential_id(width=-1)
Esempio n. 3
0
    def test_reset_sequence(self):
        from eve.utils import UIDGenerator

        i = UIDGenerator.sequential_id()
        counter = int(i)
        UIDGenerator.reset_sequence(counter + 1)
        assert int(UIDGenerator.sequential_id()) == counter + 1
        with pytest.warns(RuntimeWarning, match="Unsafe reset"):
            UIDGenerator.reset_sequence(counter)
Esempio n. 4
0
    def visit_Assign(
        self, node: Assign, *, symtable, location_stack, **kwargs
    ) -> Union[gtir.AssignStmt, gtir.NeighborAssignStmt]:
        right_type = deduce_type(symtable, node.value)
        sparse_assign = issubclass(right_type, LocalField)
        if sparse_assign:
            assert isinstance(node.target, SymbolRef)
            location_type = symtable[node.target.name].type_.args[0]
            if location_type != right_type.args[0]:
                raise ValueError()
            # gtir expects the name of the connectivity instead of its type, hence search for the name
            connectivity_name = None
            for symbol in symtable.values():
                if (
                    isinstance(symbol, Argument)
                    and issubclass(symbol.type_, Connectivity)
                    and location_type == symbol.type_
                ):
                    assert connectivity_name is None
                    connectivity_name = symbol.name
            assert connectivity_name is not None
            location_name = UIDGenerator.sequential_id(prefix="location")

            if isinstance(node.value, Generator):
                # TODO(tehrengruber) hacky, visit the generator
                new_location_stack = location_stack + [
                    (
                        node.value.generators[0].target,
                        symtable[
                            node.value.generators[0].iterable.value.name
                        ].type_.secondary_location(),
                    )
                ]
                assert node.value.generators[0].iterable.value.name == connectivity_name
                right = self.visit(
                    node.value.elt,
                    symtable={**symtable, **node.value.symtable_},
                    location_stack=location_stack,
                    **kwargs,
                )
                location_name = node.value.generators[0].target

            else:
                right = self.visit(
                    node.value,
                    symtable=symtable,
                    location_stack=location_stack,
                    inside_sparse_assign=sparse_assign,
                    neighbor_vector_access_expr_location_name=location_name,
                    **kwargs,
                )

            return gtir.NeighborAssignStmt(
                left=self.visit(
                    node.target, symtable=symtable, location_stack=location_stack, **kwargs
                ),
                right=right,
                location_type=location_stack[-1][1],
                neighbors=gtir.LocationComprehension(
                    name=location_name, of=gtir.ConnectivityRef(name=connectivity_name)
                ),
            )
        else:
            return gtir.AssignStmt(
                left=self.visit(
                    node.target,
                    **{"symtable": symtable, "location_stack": location_stack, **kwargs},
                ),
                right=self.visit(
                    node.value,
                    **{
                        "symtable": symtable,
                        "location_stack": location_stack,
                        "inside_sparse_assign": sparse_assign,
                        **kwargs,
                    },
                ),
                location_type=location_stack[-1][1],
            )
Esempio n. 5
0
    class Patterns:
        """
        Stores the pattern nodes / templates to be used extracting information from the Python ast.

        Patterns are a 1-to-1 mapping from context and Python ast node to GTScript ast node. Context is encoded in the
        field types and all understood sementic is encoded in the structure.
        """

        SymbolName = ast.Name(id=Capture(0))

        SymbolRef = ast.Name(id=Capture("name"))

        IterationOrder = ast.withitem(
            context_expr=ast.Call(func=ast.Name(id="computation"),
                                  args=[ast.Name(id=Capture("order"))]))

        Constant = ast.Constant(value=Capture("value"))

        Interval = ast.withitem(context_expr=ast.Call(
            func=ast.Name(id="interval"),
            args=[Capture("start"), Capture("stop")]))

        LocationSpecification = ast.withitem(
            context_expr=ast.Call(func=ast.Name(id="location"),
                                  args=[Capture("location_type")]),
            optional_vars=Capture(
                "name",
                default=lambda: ast.Name(id=UIDGenerator.sequential_id(
                    prefix="location"))),
        )

        Subscript = ast.Subscript(
            value=Capture("value"),
            slice=ast.Index(
                Capture("indices", transformer=SubscriptTransformer)),
        )

        BinaryOp = ast.BinOp(op=Capture("op"),
                             left=Capture("left"),
                             right=Capture("right"))

        Call = ast.Call(args=Capture("args"),
                        func=Capture("func", expected_type=ast.Name))

        SubscriptCall = ast.Call(args=Capture("args"),
                                 func=Capture("func",
                                              expected_type=ast.Subscript))

        List_ = ast.List(elts=Capture("elts"))

        LocationComprehension = ast.comprehension(target=Capture("target"),
                                                  iter=Capture("iterable"))

        Generator = ast.GeneratorExp(generators=Capture("generators"),
                                     elt=Capture("elt"))

        Assign = ast.Assign(targets=[Capture("target")],
                            value=Capture("value"))

        Stencil = ast.With(items=Capture("iteration_spec"),
                           body=Capture("body"))

        Pass = ast.Pass()

        Argument = ast.arg(arg=Capture("name",
                                       transformer=StrToSymbolTransformer),
                           annotation=Capture("type_"))

        Computation = ast.FunctionDef(
            # args=ast.arguments(args=Capture("arguments")), # noqa E800
            body=Capture("stencils"),
            name=Capture("name"),
        )

        UnaryOp = ast.UnaryOp(op=Capture("op"), operand=Capture("operand"))
Esempio n. 6
0
    class Patterns:
        """
        Stores the pattern nodes / templates to be used extracting information from the Python ast.

        Patterns are a 1-to-1 mapping from context and Python ast node to GTScript ast node. Context is encoded in the
        field types and all understood sementic is encoded in the structure.
        """

        Symbol = ast.Name(id=Capture("name"))

        IterationOrder = ast.withitem(
            context_expr=ast.Call(func=ast.Name(id="computation"),
                                  args=[ast.Name(id=Capture("order"))]))

        Constant = ast.Constant(value=Capture("value"))

        Interval = ast.withitem(context_expr=ast.Call(
            func=ast.Name(id="interval"),
            args=[Capture("start"), Capture("stop")]))

        # TODO(tehrengruber): this needs to be a function, since the uid must be generated each time
        LocationSpecification = ast.withitem(
            context_expr=ast.Call(func=ast.Name(id="location"),
                                  args=[ast.Name(id=Capture("location_type"))
                                        ]),
            optional_vars=Capture(
                "name",
                default=ast.Name(id=UIDGenerator.sequential_id(
                    prefix="location"))),
        )

        SubscriptSingle = ast.Subscript(
            value=Capture("value"),
            slice=ast.Index(value=ast.Name(id=Capture("index"))))

        SubscriptMultiple = ast.Subscript(
            value=Capture("value"),
            slice=ast.Index(value=ast.Tuple(elts=Capture("indices"))))

        BinaryOp = ast.BinOp(op=Capture("op"),
                             left=Capture("left"),
                             right=Capture("right"))

        Call = ast.Call(args=Capture("args"),
                        func=ast.Name(id=Capture("func")))

        LocationComprehension = ast.comprehension(target=Capture("target"),
                                                  iter=Capture("iterator"))

        Generator = ast.GeneratorExp(generators=Capture("generators"),
                                     elt=Capture("elt"))

        Assign = ast.Assign(targets=[Capture("target")],
                            value=Capture("value"))

        Stencil = ast.With(items=Capture("iteration_spec"),
                           body=Capture("body"))

        Pass = ast.Pass()

        Argument = ast.arg(arg=Capture("name"), annotation=Capture("type_"))

        Computation = ast.FunctionDef(
            args=ast.arguments(args=Capture("arguments")),
            body=Capture("stencils"),
            name=Capture("name"),
        )