Skip to content

Commit

Permalink
WIP: AgentPython capture external constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robadob committed Nov 8, 2023
1 parent e52194a commit 4d4e017
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 15 additions & 1 deletion swig/python/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
8 changes: 7 additions & 1 deletion swig/python/flamegpu.i
Original file line number Diff line number Diff line change
Expand Up @@ -1182,4 +1182,10 @@ TEMPLATE_VARIABLE_INSTANTIATE_INTS(poisson, flamegpu::HostRandom::poisson)
#define GLM true
#else
#define GLM false
#endif
#endif

// Declare an empty type we can use as an attribute for constants to be pulled in by codegen
%pythoncode {
class constant:
pass;
}

0 comments on commit 4d4e017

Please sign in to comment.