From 4d4e01734f02cf4fb8da6c8efb281814f34ed8f5 Mon Sep 17 00:00:00 2001 From: Robert Chisholm Date: Wed, 8 Nov 2023 11:08:24 +0000 Subject: [PATCH] WIP: AgentPython capture external constants. --- swig/python/codegen/__init__.py | 16 +++++++++++++++- swig/python/flamegpu.i | 8 +++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/swig/python/codegen/__init__.py b/swig/python/codegen/__init__.py index a51633e1f..fe7cc39d7 100644 --- a/swig/python/codegen/__init__.py +++ b/swig/python/codegen/__init__.py @@ -40,6 +40,20 @@ def translate(function: Union[str, Callable]) -> str: # get source for function and preprend device functions function_source = prepend_source + inspect.getsource(function) tree = ast.parse(function_source) - return codegen(tree) + # Filter constants + module_annontations = inspect.get_annotations(module) # requires python 3.10 + module_members = inspect.getmembers(module); + print(module_annontations) + print(module_members) + prepend_c_source = "" + # Find all annotated variables + for key, val in module_annontations.items(): + print(val.__name__) + if val.__name__ == "Final" or val.__name__ == "constant": + # Locate the literal for that variable (Python will precompute anything e.g. math.sqrt(12.5)) + for mem in module_members: + if key == mem[0]: + prepend_c_source += f"constexpr auto {mem[0]} = {mem[1]};\n" + return prepend_c_source + codegen(tree) else: raise CodeGenException(f"Error: translate function requires either a source string or Callable") \ No newline at end of file diff --git a/swig/python/flamegpu.i b/swig/python/flamegpu.i index 364995d91..8ed346388 100644 --- a/swig/python/flamegpu.i +++ b/swig/python/flamegpu.i @@ -1182,4 +1182,10 @@ TEMPLATE_VARIABLE_INSTANTIATE_INTS(poisson, flamegpu::HostRandom::poisson) #define GLM true #else #define GLM false -#endif \ No newline at end of file +#endif + +// Declare an empty type we can use as an attribute for constants to be pulled in by codegen +%pythoncode { +class constant: + pass; +}