Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically trigger __disown__() when Python hostfns are bound to models. #975

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions swig/python/flamegpu.i
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,34 @@ class FLAMEGPURuntimeException : public std::exception {
%feature("director") flamegpu::HostFunctionCallback;
%feature("director") flamegpu::HostFunctionConditionCallback;


// Automatically disown all passed host functions, to prevent them going out of scope too early
// This still leaves a potential race condition if stateful information is stored in a host function instance
%feature("pythonprepend") flamegpu::LayerDescription::addHostFunctionCallback(HostFunctionCallback*) %{
try:
func_callback = func_callback.__disown__()
except:
pass
%}
%feature("pythonprepend") flamegpu::ModelDescription::addInitFunctionCallback(HostFunctionCallback*) %{
try:
func_callback = func_callback.__disown__()
except:
pass
%}
%feature("pythonprepend") flamegpu::ModelDescription::addStepFunctionCallback(HostFunctionCallback*) %{
try:
func_callback = func_callback.__disown__()
except:
pass
%}
%feature("pythonprepend") flamegpu::ModelDescription::addExitFunctionCallback(HostFunctionCallback*) %{
try:
func_callback = func_callback.__disown__()
except:
pass
%}

// Apply type mappings go before %includes.
// -----------------

Expand Down
2 changes: 1 addition & 1 deletion tests/swig/python/gpu/test_cuda_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def test_setEnvironmentProperty(self):
m.Environment().newPropertyInt("int", 2);
m.Environment().newPropertyArrayInt("int2", [ 12, 13 ]);
m.Environment().newPropertyArrayInt("int3", [ 56, 57, 58 ]);
m.newLayer().addHostFunctionCallback(Check_setEnvironmentProperty().__disown__());
m.newLayer().addHostFunctionCallback(Check_setEnvironmentProperty());
s = pyflamegpu.CUDASimulation(m);
s.SimulationConfig().steps = 1;
# Test the getters work
Expand Down
10 changes: 5 additions & 5 deletions tests/swig/python/runtime/test_host_macro_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def test_ReadTest(self):
agent.newVariableUInt("a");
t = agent.newRTCFunction("agentwrite", AgentWrite);
model.newLayer().addAgentFunction(t);
model.newLayer().addHostFunctionCallback(HostRead().__disown__());
model.newLayer().addHostFunctionCallback(HostRead());
total_agents = TEST_DIMS[0] * TEST_DIMS[1] * TEST_DIMS[2] * TEST_DIMS[3];
population = pyflamegpu.AgentVector(agent, total_agents);
a = 0;
Expand Down Expand Up @@ -476,7 +476,7 @@ def test_WriteTest(self):
agent.newVariableUInt("w");
agent.newVariableUInt("a");
t = agent.newRTCFunction("agentread", AgentRead);
model.newLayer().addHostFunctionCallback(HostWrite().__disown__());
model.newLayer().addHostFunctionCallback(HostWrite());
model.newLayer().addAgentFunction(t);
total_agents = TEST_DIMS[0] * TEST_DIMS[1] * TEST_DIMS[2] * TEST_DIMS[3];
population = pyflamegpu.AgentVector(agent, total_agents);
Expand Down Expand Up @@ -523,7 +523,7 @@ def test_ZeroTest(self):
t1 = agent.newRTCFunction("agentwrite", AgentWrite);
t2 = agent.newRTCFunction("agentread", AgentReadZero);
model.newLayer().addAgentFunction(t1);
model.newLayer().addHostFunctionCallback(HostZero().__disown__());
model.newLayer().addHostFunctionCallback(HostZero());
model.newLayer().addAgentFunction(t2);
total_agents = TEST_DIMS[0] * TEST_DIMS[1] * TEST_DIMS[2] * TEST_DIMS[3];
population = pyflamegpu.AgentVector(agent, total_agents);
Expand Down Expand Up @@ -569,8 +569,8 @@ def test_ArithmeticTest(self):
model.Environment().newMacroPropertyDouble("double");
# Setup agent fn
model.newAgent("agent");
model.newLayer().addHostFunctionCallback(HostArithmeticInit().__disown__());
model.newLayer().addHostFunctionCallback(HostArithmetic().__disown__());
model.newLayer().addHostFunctionCallback(HostArithmeticInit());
model.newLayer().addHostFunctionCallback(HostArithmetic());
# Do Sim
cudaSimulation = pyflamegpu.CUDASimulation(model);
cudaSimulation.SimulationConfig().steps = 1;
Expand Down