diff --git a/pystencils/astnodes.py b/pystencils/astnodes.py index b081f752945133d56a4d32407e335b26d13614ec..cc58ebd301886db63bdf4e37d86285643768e9bd 100644 --- a/pystencils/astnodes.py +++ b/pystencils/astnodes.py @@ -518,12 +518,13 @@ class LoopOverCoordinate(Node): class SympyAssignment(Node): - def __init__(self, lhs_symbol, rhs_expr, is_const=True): + def __init__(self, lhs_symbol, rhs_expr, is_const=True, use_auto=False): super(SympyAssignment, self).__init__(parent=None) self._lhs_symbol = lhs_symbol self.rhs = sp.sympify(rhs_expr) self._is_const = is_const self._is_declaration = self.__is_declaration() + self.use_auto = use_auto def __is_declaration(self): if isinstance(self._lhs_symbol, cast_func): diff --git a/pystencils/backends/cbackend.py b/pystencils/backends/cbackend.py index ff96581b091ab3e2a21bc3c3964c1b0030de2266..6367807f9fb8c0632053504196d3486a0b6a3174 100644 --- a/pystencils/backends/cbackend.py +++ b/pystencils/backends/cbackend.py @@ -33,9 +33,9 @@ def generate_c(ast_node: Node, with_globals=True) -> str: """Prints an abstract syntax tree node as C or CUDA code. - This function does not need to distinguish between C, C++ or CUDA code, it just prints 'C-like' code as encoded - in the abstract syntax tree (AST). The AST is built differently for C or CUDA by calling different create_kernel - functions. + This function does not need to distinguish for most AST nodes between C, C++ or CUDA code, it just prints 'C-like' + code as encoded in the abstract syntax tree (AST). The AST is built differently for C or CUDA by calling different + create_kernel functions. Args: ast_node: @@ -230,11 +230,15 @@ class CBackend: def _print_SympyAssignment(self, node): if node.is_declaration: - if node.is_const: - prefix = 'const ' + if node.use_auto: + data_type = 'auto ' else: - prefix = '' - data_type = prefix + self._print(node.lhs.dtype).replace(' const', '') + " " + if node.is_const: + prefix = 'const ' + else: + prefix = '' + data_type = prefix + self._print(node.lhs.dtype).replace(' const', '') + " " + return "%s%s = %s;" % (data_type, self.sympy_printer.doprint(node.lhs), self.sympy_printer.doprint(node.rhs)) diff --git a/pystencils/kernelcreation.py b/pystencils/kernelcreation.py index e0c635e06220f10198383e3c1fbbe0de7561280d..408cc506b56f1da5c42522d24e28b7a902f391ad 100644 --- a/pystencils/kernelcreation.py +++ b/pystencils/kernelcreation.py @@ -26,7 +26,8 @@ def create_kernel(assignments, gpu_indexing='block', gpu_indexing_params=MappingProxyType({}), use_textures_for_interpolation=True, - cpu_prepend_optimizations=[]): + cpu_prepend_optimizations=[], + use_auto_for_assignments=False): """ Creates abstract syntax tree (AST) of kernel, using a list of update equations. @@ -102,12 +103,10 @@ def create_kernel(assignments, vectorize(ast, **cpu_vectorize_info) else: raise ValueError("Invalid value for cpu_vectorize_info") - return ast elif target == 'llvm': from pystencils.llvm import create_kernel ast = create_kernel(assignments, type_info=data_type, split_groups=split_groups, iteration_slice=iteration_slice, ghost_layers=ghost_layers) - return ast elif target == 'gpu': from pystencils.gpucuda import create_cuda_kernel ast = create_cuda_kernel(assignments, type_info=data_type, @@ -115,10 +114,15 @@ def create_kernel(assignments, iteration_slice=iteration_slice, ghost_layers=ghost_layers, skip_independence_check=skip_independence_check, use_textures_for_interpolation=use_textures_for_interpolation) - return ast else: raise ValueError("Unknown target %s. Has to be one of 'cpu', 'gpu' or 'llvm' " % (target,)) + if use_auto_for_assignments: + for a in ast.atoms(SympyAssignment): + a.use_auto = True + + return ast + def create_indexed_kernel(assignments, index_fields,