Skip to content
Snippets Groups Projects

Fixed kernel_decorator with config parameter

Closed Jan Hönig requested to merge hoenig/pystencils:master into master
Viewing commit d21f6f8c
Show latest version
2 files
+ 44
28
Preferences
Compare changes
Files
2
@@ -7,11 +7,12 @@ import sympy as sp
@@ -7,11 +7,12 @@ import sympy as sp
from pystencils.assignment import Assignment
from pystencils.assignment import Assignment
from pystencils.sympyextensions import SymbolCreator
from pystencils.sympyextensions import SymbolCreator
 
from pystencils.kernelcreation import CreateKernelConfig
__all__ = ['kernel']
__all__ = ['kernel']
def kernel(func: Callable[..., None], return_config: bool = False, **kwargs) -> Union[List[Assignment], Dict]:
def kernel(config: CreateKernelConfig = None, **kwargs) -> Callable[..., Union[List[Assignment], Dict]]:
"""Decorator to simplify generation of pystencils Assignments.
"""Decorator to simplify generation of pystencils Assignments.
Changes the meaning of the '@=' operator. Each line containing this operator gives a symbolic assignment
Changes the meaning of the '@=' operator. Each line containing this operator gives a symbolic assignment
@@ -21,8 +22,8 @@ def kernel(func: Callable[..., None], return_config: bool = False, **kwargs) ->
@@ -21,8 +22,8 @@ def kernel(func: Callable[..., None], return_config: bool = False, **kwargs) ->
The decorated function may not receive any arguments, with exception of an argument called 's' that specifies
The decorated function may not receive any arguments, with exception of an argument called 's' that specifies
a SymbolCreator()
a SymbolCreator()
func: the decorated function
func: the decorated function
return_config: Specify whether to return the list with assignments, or a dictionary containing additional settings
config: Specify whether to return the list with assignments, or a dictionary containing additional settings
like func_name
like func_name
Examples:
Examples:
>>> import pystencils as ps
>>> import pystencils as ps
@@ -34,31 +35,34 @@ def kernel(func: Callable[..., None], return_config: bool = False, **kwargs) ->
@@ -34,31 +35,34 @@ def kernel(func: Callable[..., None], return_config: bool = False, **kwargs) ->
>>> f, g = ps.fields('f, g: [2D]')
>>> f, g = ps.fields('f, g: [2D]')
>>> assert my_kernel[0].rhs == f[0,1] + f[1,0]
>>> assert my_kernel[0].rhs == f[0,1] + f[1,0]
"""
"""
source = inspect.getsource(func)
def decorator(func: Callable[..., None]) -> Union[List[Assignment], Dict]:
source = textwrap.dedent(source)
source = inspect.getsource(func)
a = ast.parse(source)
source = textwrap.dedent(source)
KernelFunctionRewrite().visit(a)
a = ast.parse(source)
ast.fix_missing_locations(a)
KernelFunctionRewrite().visit(a)
gl = func.__globals__.copy()
ast.fix_missing_locations(a)
gl = func.__globals__.copy()
assignments = []
assignments = []
def assignment_adder(lhs, rhs):
assignments.append(Assignment(lhs, rhs))
def assignment_adder(lhs, rhs):
assignments.append(Assignment(lhs, rhs))
gl['_add_assignment'] = assignment_adder
gl['_Piecewise'] = sp.Piecewise
gl['_add_assignment'] = assignment_adder
gl.update(inspect.getclosurevars(func).nonlocals)
gl['_Piecewise'] = sp.Piecewise
exec(compile(a, filename="<ast>", mode="exec"), gl)
gl.update(inspect.getclosurevars(func).nonlocals)
func = gl[func.__name__]
exec(compile(a, filename="<ast>", mode="exec"), gl)
args = inspect.getfullargspec(func).args
func = gl[func.__name__]
if 's' in args and 's' not in kwargs:
args = inspect.getfullargspec(func).args
kwargs['s'] = SymbolCreator()
if 's' in args and 's' not in kwargs:
func(**kwargs)
kwargs['s'] = SymbolCreator()
if return_config:
func(**kwargs)
return {'assignments': assignments, 'function_name': func.__name__}
if config:
else:
config.function_name = func.__name__
return assignments
return {'assignments': assignments, 'config': config}
 
else:
 
return assignments
 
return decorator
# noinspection PyMethodMayBeStatic
# noinspection PyMethodMayBeStatic