.. _program_listing_file_include_flamegpu_simulation_AgentLoggingConfig.h: Program Listing for File AgentLoggingConfig.h ============================================= |exhale_lsh| :ref:`Return to documentation for file ` (``include/flamegpu/simulation/AgentLoggingConfig.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #ifndef INCLUDE_FLAMEGPU_SIMULATION_AGENTLOGGINGCONFIG_H_ #define INCLUDE_FLAMEGPU_SIMULATION_AGENTLOGGINGCONFIG_H_ #include #include #include #include #include #include "flamegpu/simulation/LoggingConfig.h" #include "flamegpu/simulation/AgentLoggingConfig_Reductions.cuh" #include "flamegpu/simulation/AgentLoggingConfig_SumReturn.h" #include "flamegpu/runtime/agent/HostAgentAPI.cuh" namespace flamegpu { struct ModelData; class AgentLoggingConfig { friend AgentLoggingConfig LoggingConfig::agent(const std::string &, const std::string &); AgentLoggingConfig(std::shared_ptr agent, std::pair>, bool> &agent_set); public: void logCount() { log_count = true; } template void logMean(const std::string &variable_name); template void logStandardDev(const std::string &variable_name); template void logMin(const std::string &variable_name); template void logMax(const std::string &variable_name); template void logSum(const std::string &variable_name); private: void log(const LoggingConfig::NameReductionFn &name, const std::type_index &variable_type, const std::string &method_name); const std::shared_ptr agent; const std::shared_ptr> agent_set; bool &log_count; }; template detail::Any getAgentVariableMeanFunc(HostAgentAPI &ai, const std::string &variable_name) { if (ai.count() > 0) return detail::Any(ai.sum::result_t>(variable_name) / static_cast(ai.count())); return detail::Any(static_cast(0)); } template detail::Any getAgentVariableSumFunc(HostAgentAPI &ai, const std::string &variable_name) { return detail::Any(ai.sum::result_t>(variable_name)); } template detail::Any getAgentVariableMinFunc(HostAgentAPI &ai, const std::string &variable_name) { return detail::Any(ai.min(variable_name)); } template detail::Any getAgentVariableMaxFunc(HostAgentAPI &ai, const std::string &variable_name) { return detail::Any(ai.max(variable_name)); } template detail::Any getAgentVariableStandardDevFunc(HostAgentAPI &ai, const std::string &variable_name) { // Todo, workout how to make this more multi-thread/deviceable. // Todo, streams for the memcpy? if (ai.count() == 0) return detail::Any(0.0); // Work out the Mean const double mean = ai.sum::result_t>(variable_name) / static_cast(ai.count()); // Then for each number: subtract the Mean and square the result // Then work out the mean of those squared differences. auto lock = std::unique_lock(detail::STANDARD_DEVIATION_MEAN_mutex); gpuErrchk(cudaMemcpyToSymbol(detail::STANDARD_DEVIATION_MEAN, &mean, sizeof(double))); const double variance = ai.transformReduce(variable_name, detail::standard_deviation_subtract_mean, detail::standard_deviation_add, 0) / static_cast(ai.count()); lock.unlock(); // Take the square root of that and we are done! return detail::Any(sqrt(variance)); } template void AgentLoggingConfig::logMean(const std::string &variable_name) { // Instantiate the template function for calculating the mean LoggingConfig::ReductionFn *fn = getAgentVariableMeanFunc; // Log the property (validation occurs in this common log method) log({variable_name, LoggingConfig::Mean, fn}, std::type_index(typeid(T)), "Mean"); } template void AgentLoggingConfig::logStandardDev(const std::string &variable_name) { // Instantiate the template function for calculating the mean LoggingConfig::ReductionFn *fn = getAgentVariableStandardDevFunc; // Log the property (validation occurs in this common log method) log({variable_name, LoggingConfig::StandardDev, fn}, std::type_index(typeid(T)), "StandardDev"); } template void AgentLoggingConfig::logMin(const std::string &variable_name) { // Instantiate the template function for calculating the mean LoggingConfig::ReductionFn *fn = getAgentVariableMinFunc; // Log the property (validation occurs in this common log method) log({variable_name, LoggingConfig::Min, fn}, std::type_index(typeid(T)), "Min"); } template void AgentLoggingConfig::logMax(const std::string &variable_name) { // Instantiate the template function for calculating the mean LoggingConfig::ReductionFn *fn = getAgentVariableMaxFunc; // Log the property (validation occurs in this common log method) log({variable_name, LoggingConfig::Max, fn}, std::type_index(typeid(T)), "Max"); } template void AgentLoggingConfig::logSum(const std::string &variable_name) { // Instantiate the template function for calculating the mean LoggingConfig::ReductionFn *fn = getAgentVariableSumFunc; // Log the property (validation occurs in this common log method) log({variable_name, LoggingConfig::Sum, fn}, std::type_index(typeid(T)), "Sum"); } } // namespace flamegpu #endif // INCLUDE_FLAMEGPU_SIMULATION_AGENTLOGGINGCONFIG_H_