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

BugFix: DeviceAgentVector::setVariable() array element did not sync #1266

Merged
merged 2 commits into from
Dec 4, 2024
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
2 changes: 2 additions & 0 deletions include/flamegpu/simulation/AgentVector_Agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ void AgentVector_Agent::setVariable(const std::string &variable_name, const unsi
}
_parent->_require(variable_name);
static_cast<T*>(v_buff->getDataPtr())[(index * (v_buff->getElements() / detail::type_decode<T>::len_t)) + array_index] = value;
// Notify (_data was locked above)
_parent->_changed(variable_name, index);
}
#ifdef SWIG
template <typename T>
Expand Down
6 changes: 2 additions & 4 deletions src/flamegpu/io/Telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ std::string Telemetry::getUserId() {
return cached_id;
else
return userId;
}
else {
} else {
throw std::runtime_error("Unable to open user ID file for reading");
}
}
Expand All @@ -382,8 +381,7 @@ std::string Telemetry::getUserId() {
if (file.is_open()) {
file << userId;
file.close();
}
else {
} else {
throw std::runtime_error("Unable to create user ID file");
}
} catch (const std::exception&) {
Expand Down
97 changes: 97 additions & 0 deletions tests/test_cases/runtime/agent/test_device_agent_vector.cu
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ FLAMEGPU_STEP_FUNCTION(SetGet) {
ai.setVariable<int>("int", ai.getVariable<int>("int") + 12);
}
}
FLAMEGPU_STEP_FUNCTION(SetGetArray) {
// Accessing DeviceAgentVector like this would previously lead to an access violation (Issue #522, PR #751)
DeviceAgentVector av = FLAMEGPU->agent(AGENT_NAME).getPopulationData();
for (AgentVector::Agent ai : av) {
auto t = ai.getVariable<int, 3>("int");
t[0] += 11;
t[1] += 12;
t[2] += 13;
ai.setVariable<int, 3>("int", t);
}
}
FLAMEGPU_STEP_FUNCTION(SetGetArrayElement) {
// Accessing DeviceAgentVector like this would previously lead to an access violation (Issue #522, PR #751)
DeviceAgentVector av = FLAMEGPU->agent(AGENT_NAME).getPopulationData();
for (AgentVector::Agent ai : av) {
ai.setVariable<int, 3>("int", 1, ai.getVariable<int, 3>("int", 1) + 12);
}
}
FLAMEGPU_STEP_FUNCTION(SetGetHalf) {
HostAgentAPI agent = FLAMEGPU->agent(AGENT_NAME);
DeviceAgentVector av = agent.getPopulationData();
Expand Down Expand Up @@ -77,6 +95,85 @@ TEST(DeviceAgentVectorTest, SetGet) {
ASSERT_EQ(av[i].getVariable<int>("int"), static_cast<int>(i) + 24);
}
}
TEST(DeviceAgentVectorTest, SetGetArray) {
// Initialise an agent population with values in a variable [0,1,2..N]
// Inside a step function, retrieve the agent population as a DeviceAgentVector
// Update all agents by adding 12 to their value
// After model completion, retrieve the agent population and check their values are [12,13,14..N+12]
ModelDescription model(MODEL_NAME);
AgentDescription agent = model.newAgent(AGENT_NAME);
agent.newVariable<int, 3>("int", { 0, 0, 0 });
model.addStepFunction(SetGetArray);

// Init agent pop
AgentVector av(agent, AGENT_COUNT);
for (unsigned int i = 0; i < AGENT_COUNT; ++i)
av[i].setVariable<int, 3>("int", { 0, static_cast<int>(i), 0 });

// Create and step simulation
CUDASimulation sim(model);
sim.setPopulationData(av);
sim.step();

// Retrieve and validate agents match
sim.getPopulationData(av);
for (unsigned int i = 0; i < AGENT_COUNT; ++i) {
const std::array<int, 3> t1 = {11, static_cast<int>(i) + 12, 13 };
const std::array<int, 3> t2 = av[i].getVariable<int, 3>("int");
ASSERT_EQ(t1, t2);
}

// Step again
sim.step();

// Retrieve and validate agents match
sim.getPopulationData(av);
for (unsigned int i = 0; i < AGENT_COUNT; ++i) {
const std::array<int, 3> t1 = { 22, static_cast<int>(i) + 24, 26 };
const std::array<int, 3> t2 = av[i].getVariable<int, 3>("int");
ASSERT_EQ(t1, t2);
}
}
TEST(DeviceAgentVectorTest, SetGetArrayElement) {
// Covers former bug, where updating an array element would not trigger sync
// Initialise an agent population with values in a variable [0,1,2..N]
// Inside a step function, retrieve the agent population as a DeviceAgentVector
// Update all agents by adding 12 to their value
// After model completion, retrieve the agent population and check their values are [12,13,14..N+12]
ModelDescription model(MODEL_NAME);
AgentDescription agent = model.newAgent(AGENT_NAME);
agent.newVariable<int, 3>("int", {0, 0, 0});
model.addStepFunction(SetGetArrayElement);

// Init agent pop
AgentVector av(agent, AGENT_COUNT);
for (unsigned int i = 0; i < AGENT_COUNT; ++i)
av[i].setVariable<int, 3>("int", { 0, static_cast<int>(i), 0 });

// Create and step simulation
CUDASimulation sim(model);
sim.setPopulationData(av);
sim.step();

// Retrieve and validate agents match
sim.getPopulationData(av);
for (unsigned int i = 0; i < AGENT_COUNT; ++i) {
const std::array<int, 3> t1 = { 0, static_cast<int>(i) + 12, 0 };
const std::array<int, 3> t2 = av[i].getVariable<int, 3>("int");
ASSERT_EQ(t1, t2);
}

// Step again
sim.step();

// Retrieve and validate agents match
sim.getPopulationData(av);
for (unsigned int i = 0; i < AGENT_COUNT; ++i) {
const std::array<int, 3> t1 = { 0, static_cast<int>(i) + 24, 0 };
const std::array<int, 3> t2 = av[i].getVariable<int, 3>("int");
ASSERT_EQ(t1, t2);
}
}
TEST(DeviceAgentVectorTest, SetGetHalf) {
// Initialise an agent population with values in a variable [0,1,2..N]
// Inside a step function, retrieve the agent population as a DeviceAgentVector
Expand Down