Skip to content

Commit

Permalink
BugFix: Agent ID's were not static during host functions.
Browse files Browse the repository at this point in the history
If agents were created via host agent creation, then accessed via DeviceAgentVector, their HostAgentCreation assigned IDs would be replaced.
  • Loading branch information
Robadob committed Dec 10, 2024
1 parent 12811d4 commit c3f3574
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/flamegpu/runtime/agent/DeviceAgentVector_impl.cu
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ void DeviceAgentVector_impl::_insert(size_type pos, size_type count) {
_require(ID_VARIABLE_NAME);
id_t *h_ptr = static_cast<id_t*>(d->second->getDataPtr());
for (unsigned int i = pos; i < pos + count; ++i) {
// Always assign ID, as AgentVector should reset these to unset, but this saves us checking
// if (h_ptr[i] == ID_NOT_SET) {
// Do not reassign ID if it has been set during HostAgentCreation
if (h_ptr[i] == ID_NOT_SET) {
h_ptr[i] = cuda_agent.nextID();
// }
}
}
_changedAfter(ID_VARIABLE_NAME, pos);
} else {
Expand Down
33 changes: 33 additions & 0 deletions tests/test_cases/runtime/agent/test_host_agent_creation.cu
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,39 @@ TEST(HostAgentCreationTest, AgentID_MultipleAgents) {
}
ASSERT_EQ(ids_b.size(), 2 * POP_SIZE); // No collisions
}
FLAMEGPU_INIT_FUNCTION(AgentID_Consistent1) {
for (unsigned int i = 0; i < 10; ++i) {
auto a = FLAMEGPU->agent("agent").newAgent();
a.setVariable<id_t>("id_copy", a.getID());
}
}
FLAMEGPU_INIT_FUNCTION(AgentID_Consistent2) {
DeviceAgentVector v = FLAMEGPU->agent("agent").getPopulationData();
for (const auto &a : v) {
id_t t1 = a.getID();
id_t t2 = a.getVariable<id_t>("id_copy");
EXPECT_EQ(t1, t2);
}
}
TEST(HostAgentCreationTest, AgentID_Consistent) {
// PTHeywood found that when agents were created in a host function they would be given an ID
// This would then be reassigned, if they were accessed via a DeviceAgentVector in the same host function
// This was also present with multiple init functions, as shown in this test.
// Pull request #1270

ModelDescription model("test_agentid");
AgentDescription agent = model.newAgent("agent");
agent.newVariable<id_t>("id_copy", ID_NOT_SET);

model.addInitFunction(AgentID_Consistent1);
model.addInitFunction(AgentID_Consistent2);

CUDASimulation sim(model);

sim.SimulationConfig().steps = 10;

sim.simulate();
}
#ifdef FLAMEGPU_USE_GLM
FLAMEGPU_STEP_FUNCTION(ArrayVarHostBirthSetGet_glm) {
auto t = FLAMEGPU->agent("agent_name");
Expand Down

0 comments on commit c3f3574

Please sign in to comment.