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, logging variable mean for an empty agent pop #734

Merged
merged 1 commit into from
Nov 17, 2021
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
4 changes: 3 additions & 1 deletion include/flamegpu/sim/AgentLoggingConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ template <typename T> struct sum_input_t { typedef T result_t; };
*/
template<typename T>
util::Any getAgentVariableMeanFunc(HostAgentAPI &ai, const std::string &variable_name) {
return util::Any(ai.sum<T, typename sum_input_t<T>::result_t>(variable_name) / static_cast<double>(ai.count()));
if (ai.count() > 0)
return util::Any(ai.sum<T, typename sum_input_t<T>::result_t>(variable_name) / static_cast<double>(ai.count()));
return util::Any(static_cast<double>(0));
}
template<typename T>
util::Any getAgentVariableSumFunc(HostAgentAPI &ai, const std::string &variable_name) {
Expand Down
4 changes: 3 additions & 1 deletion src/flamegpu/gpu/CUDAEnsemble.cu
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ void CUDAEnsemble::simulate(const RunPlanVector &plans) {

// Init with placement new
{
if (!config.quiet)
if (!config.quiet) {
printf("\rCUDAEnsemble progress: %u/%u", 0, static_cast<unsigned int>(plans.size()));
fflush(stdout);
}
unsigned int i = 0;
for (auto &d : devices) {
for (unsigned int j = 0; j < config.concurrent_runs; ++j) {
Expand Down
36 changes: 36 additions & 0 deletions tests/test_cases/io/test_logging.cu
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,42 @@ FLAMEGPU_INIT_FUNCTION(logging_ensemble_init) {
instance.setVariable<unsigned int>("uint_var", static_cast<unsigned int>(i+2));
}
}
TEST(LoggingTest, EmptyMean) {
/**
* There was a bug, where if an agent population died out
* Reductions would log in the form `{"mean":}`, rather than `{"mean":0}`
* This led to parsing of the JSON to fail.
* This was due to mean dividing by 0, producing an output of NaN
* Hence, this test simply checks that mean of empty pop does not return 0
* A line can be uncommented to generate the json output file
*/
// Define model
ModelDescription m(MODEL_NAME);
AgentDescription& a = m.newAgent(AGENT_NAME1);
a.newVariable<float>("float_var");
AgentFunctionDescription& f1 = a.newFunction(FUNCTION_NAME1, agent_fn1);
m.newLayer().addAgentFunction(f1);

// Define logging configs
LoggingConfig lcfg(m);
AgentLoggingConfig alcfg = lcfg.agent(AGENT_NAME1);
alcfg.logCount();
alcfg.logMean<float>("float_var");

StepLoggingConfig slcfg(lcfg);
slcfg.setFrequency(1);

// Run model (note there are no agents, so no agent functions will actually run, but the logging will still occur)
CUDASimulation sim(m);
sim.setStepLog(slcfg);
sim.SimulationConfig().steps = 5;
// sim.SimulationConfig().step_log_file = "valid.json"; // Enabling this produces the bugged output file
sim.simulate();
auto& sl = sim.getRunLog().getStepLog();
for (auto& step : sl) { // Check step log doesn't contain NaN
EXPECT_EQ(step.getAgent(AGENT_NAME1).getMean("float_var"), 0.0);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, I didn't put test at the end of the file, in hopes of avoiding a conflict with my other logging pr.

TEST(LoggingTest, CUDAEnsembleSimulate) {
/**
* Ensure the expected data is logged when CUDAEnsemble::simulate() is called
Expand Down