Skip to content

Commit

Permalink
Automatically trigger __disown__() when Python hostfns are bound to m…
Browse files Browse the repository at this point in the history
…odels. (#975)

Co-authored-by: Peter Heywood <peethwd@gmail.com>
  • Loading branch information
Robadob and ptheywood authored Nov 23, 2022
1 parent 85e61bb commit 7d38299
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
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

0 comments on commit 7d38299

Please sign in to comment.