diff --git a/assignment.py b/assignment.py index 251ff6966af05372abdf953f7e69291a2c151148..5eec6c027b366afd8eb9cb29d2e54a433212730a 100644 --- a/assignment.py +++ b/assignment.py @@ -1,7 +1,12 @@ # -*- coding: utf-8 -*- -from sympy.codegen.ast import Assignment +import sympy as sp from sympy.printing.latex import LatexPrinter +try: + from sympy.codegen.ast import Assignment +except ImportError: + Assignment = None + __all__ = ['Assignment'] @@ -9,12 +14,37 @@ def print_assignment_latex(printer, expr): """sympy cannot print Assignments as Latex. Thus, this function is added to the sympy Latex printer""" printed_lhs = printer.doprint(expr.lhs) printed_rhs = printer.doprint(expr.rhs) - return f"{printed_lhs} \leftarrow {printed_rhs}" + return "{printed_lhs} \leftarrow {printed_rhs}".format(printed_lhs=printed_lhs, printed_rhs=printed_rhs) def assignment_str(assignment): - return f"{assignment.lhs} ↠{assignment.rhs}" + return "{lhs} ↠{rhs}".format(lhs=assignment.lhs, rhs=assignment.rhs) + + +if Assignment: + + Assignment.__str__ = assignment_str + LatexPrinter._print_Assignment = print_assignment_latex + +else: + # back port for older sympy versions that don't have Assignment yet + + class Assignment(sp.Rel): + + rel_op = ':=' + __slots__ = [] + def __new__(cls, lhs, rhs=0, **assumptions): + from sympy.matrices.expressions.matexpr import ( + MatrixElement, MatrixSymbol) + from sympy.tensor.indexed import Indexed + lhs = sp.sympify(lhs) + rhs = sp.sympify(rhs) + # Tuple of things that can be on the lhs of an assignment + assignable = (sp.Symbol, MatrixSymbol, MatrixElement, Indexed) + if not isinstance(lhs, assignable): + raise TypeError("Cannot assign to lhs of type %s." % type(lhs)) + return sp.Rel.__new__(cls, lhs, rhs, **assumptions) -Assignment.__str__ = assignment_str -LatexPrinter._print_Assignment = print_assignment_latex + __str__ = assignment_str + _print_Assignment = print_assignment_latex diff --git a/astnodes.py b/astnodes.py index 7206624ef7d63d1f63ab75475952bd78e381707f..7ed51daac71bd4a6cbf812d8a800e1529d7cb611 100644 --- a/astnodes.py +++ b/astnodes.py @@ -494,7 +494,7 @@ class SympyAssignment(Node): def _repr_html_(self): printed_lhs = sp.latex(self.lhs) printed_rhs = sp.latex(self.rhs) - return f"${printed_lhs} \leftarrow {printed_rhs}$" + return "${printed_lhs} \leftarrow {printed_rhs}$".format(printed_lhs=printed_lhs, printed_rhs=printed_rhs) class ResolvedFieldAccess(sp.Indexed): diff --git a/boundaries/boundaryhandling.py b/boundaries/boundaryhandling.py index 73330c33a7e4e6dee9a2a2a7fee8d2af58645e66..f36cad5ff972e72264184ed9e4a4d6c8c2c775c6 100644 --- a/boundaries/boundaryhandling.py +++ b/boundaries/boundaryhandling.py @@ -51,13 +51,13 @@ class FlagInterface: self._used_flags.add(flag) assert self._is_power_of_2(flag) return flag - raise ValueError(f"All available {self.max_bits} flags are reserved") + raise ValueError("All available {} flags are reserved".format(self.max_bits)) def reserve_flag(self, flag): assert self._is_power_of_2(flag) flag = self.dtype(flag) if flag in self._used_flags: - raise ValueError(f"The flag {flag} is already reserved") + raise ValueError("The flag {flag} is already reserved".format(flag=flag)) self._used_flags.add(flag) return flag diff --git a/cpu/vectorization.py b/cpu/vectorization.py index f8870c52a30a842cd802ae7e5c99c15ef83f6c56..d841c50b93dc2900041c8b5080eef60e9a2b29fb 100644 --- a/cpu/vectorization.py +++ b/cpu/vectorization.py @@ -43,7 +43,8 @@ def vectorize_inner_loops_and_adapt_load_stores(ast_node, vector_width=4): successful = False break typed_symbol = base.label - assert type(typed_symbol.dtype) is PointerType, f"Type of access is {typed_symbol.dtype}, {indexed}" + assert type(typed_symbol.dtype) is PointerType, \ + "Type of access is {}, {}".format(typed_symbol.dtype, indexed) substitutions[indexed] = cast_func(indexed, VectorType(typed_symbol.dtype.base_type, vector_width)) if not successful: warnings.warn("Could not vectorize loop because of non-consecutive memory access") diff --git a/display_utils.py b/display_utils.py index d4c924bc4c71483e9840c007ef4b3f209f57aae7..612d69aa3ec43a83def693f27438313b2b2ab80a 100644 --- a/display_utils.py +++ b/display_utils.py @@ -27,7 +27,7 @@ def highlight_cpp(code: str): from pygments.lexers import CppLexer css = HtmlFormatter().get_style_defs('.highlight') - css_tag = f"<style>{css}</style>" + css_tag = "<style>{css}</style>".format(css=css) display(HTML(css_tag)) return HTML(highlight(code, CppLexer(), HtmlFormatter())) diff --git a/field.py b/field.py index 29112e605346e12b625f075c181c5a9102c38665..87b4785a27f3e14fce10829f47b0ff468bde3d21 100644 --- a/field.py +++ b/field.py @@ -269,7 +269,7 @@ class Field: self._layout = normalize_layout(layout) self.shape = shape self.strides = strides - self.latex_name: Optional[str] = None + self.latex_name = None # type: Optional[str] def new_field_with_different_name(self, new_name): return Field(new_name, self.field_type, self._dtype, self._layout, self.shape, self.strides) diff --git a/gpucuda/indexing.py b/gpucuda/indexing.py index 1d2a655bf89a90ff5fb2e4b254f834f5eacc9b9e..862143abb0dbdd0460c692ba6c1cd50eb5bd8286 100644 --- a/gpucuda/indexing.py +++ b/gpucuda/indexing.py @@ -97,8 +97,8 @@ class BlockIndexing(AbstractIndexing): _get_end_from_slice(self._iterationSlice, arr_shape))] widths = sp.Matrix(widths).subs(substitution_dict) - grid: Tuple[int, ...] = tuple(sp.ceiling(length / block_size) - for length, block_size in zip(widths, self._blockSize)) + grid = tuple(sp.ceiling(length / block_size) + for length, block_size in zip(widths, self._blockSize)) # type: : Tuple[int, ...] extend_bs = (1,) * (3 - len(self._blockSize)) extend_gr = (1,) * (3 - len(grid)) diff --git a/integer_set_analysis.py b/integer_set_analysis.py index 8d01c9ee7367ed682eb6cde0f7d3867b59f5f39a..fc4a60b130f2671907e938020527580f0078e280 100644 --- a/integer_set_analysis.py +++ b/integer_set_analysis.py @@ -36,11 +36,13 @@ def isl_iteration_set(node: ast.Node): loop_start_str = remove_brackets(str(loop.start)) loop_stop_str = remove_brackets(str(loop.stop)) ctr_name = loop.loop_counter_name - conditions.append(remove_brackets(f"{ctr_name} >= {loop_start_str} and {ctr_name} < {loop_stop_str}")) + set_string_description = "{} >= {} and {} < {}".format(ctr_name, loop_start_str, ctr_name, loop_stop_str) + conditions.append(remove_brackets(set_string_description)) symbol_names = ','.join(degrees_of_freedom) condition_str = ' and '.join(conditions) - set_description = f"{{ [{symbol_names}] : {condition_str} }}" + set_description = "{{ [{symbol_names}] : {condition_str} }}".format(symbol_names=symbol_names, + condition_str=condition_str) return degrees_of_freedom, isl.BasicSet(set_description) @@ -51,7 +53,8 @@ def simplify_loop_counter_dependent_conditional(conditional): if dofs_in_condition.issubset(dofs_in_loops): symbol_names = ','.join(dofs_in_loops) condition_str = remove_brackets(str(conditional.condition_expr)) - condition_set = isl.BasicSet(f"{{ [{symbol_names}] : {condition_str} }}") + condition_set = isl.BasicSet("{{ [{symbol_names}] : {condition_str} }}".format(symbol_names=symbol_names, + condition_str=condition_str)) if condition_set.is_empty(): conditional.replace_by_false_block() diff --git a/simp/assignment_collection.py b/simp/assignment_collection.py index 9b7e9948b40ab42e7850e413ae629f9974418679..5fb57a1ec380bd6bc2b8aae845b067ee3caa5ca7 100644 --- a/simp/assignment_collection.py +++ b/simp/assignment_collection.py @@ -326,10 +326,10 @@ class AssignmentCollection: def __str__(self): result = "Subexpressions:\n" for eq in self.subexpressions: - result += f"\t{eq}\n" + result += "\t{eq}\n".format(eq=eq) result += "Main Assignments:\n" for eq in self.main_assignments: - result += f"\t{eq}\n" + result += "\t{eq}\n".format(eq=eq) return result @@ -343,6 +343,6 @@ class SymbolGen: return self def __next__(self): - name = f"{self._symbol}_{self._ctr}" + name = "{}_{}".format(self._symbol, self._ctr) self._ctr += 1 return sp.Symbol(name) diff --git a/transformations.py b/transformations.py index 81dd22c2938feb611aba44d64bf3360c7d2e566b..62b78258d495c9b0d5ad57e00390d7916c103d63 100644 --- a/transformations.py +++ b/transformations.py @@ -713,12 +713,12 @@ class KernelConstraintsCheck: fai = self.FieldAndIndex(lhs.field, lhs.index) self._field_writes[fai].add(lhs.offsets) if len(self._field_writes[fai]) > 1: - raise ValueError(f"Field {lhs.field.name} is written at two different locations") + raise ValueError("Field {} is written at two different locations".format(lhs.field.name)) elif isinstance(lhs, sp.Symbol): if lhs in self._defined_pure_symbols: - raise ValueError(f"Assignments not in SSA form, multiple assignments to {lhs.name}") + raise ValueError("Assignments not in SSA form, multiple assignments to {}".format(lhs.name)) if lhs in self._accessed_pure_symbols: - raise ValueError(f"Symbol {lhs.name} is written, after it has been read") + raise ValueError("Symbol {} is written, after it has been read".format(lhs.name)) self._defined_pure_symbols.add(lhs) def _update_accesses_rhs(self, rhs): @@ -727,8 +727,8 @@ class KernelConstraintsCheck: for write_offset in writes: assert len(writes) == 1 if write_offset != rhs.offsets: - raise ValueError(f"Violation of loop independence condition. " - f"Field {rhs.field} is read at {rhs.offsets} and written at {write_offset}") + raise ValueError("Violation of loop independence condition. Field " + "{} is read at {} and written at {}".format(rhs.field, rhs.offsets, write_offset)) self.fields_read.add(rhs.field) elif isinstance(rhs, sp.Symbol): self._accessed_pure_symbols.add(rhs)