From 9baa012b31cdbf5dcc6a216d20c2595feacf7f05 Mon Sep 17 00:00:00 2001 From: Christian Heinemann Date: Fri, 2 Aug 2024 18:16:40 +0200 Subject: [PATCH] show created self-replicators per second --- .../EngineGpuKernels/ConstructorProcessor.cuh | 14 ++++---- .../EngineGpuKernels/SimulationStatistics.cuh | 1 + source/EngineInterface/RawStatisticsData.h | 1 + source/Gui/StatisticsWindow.cpp | 13 ++++++-- source/Gui/TableLiveStatistics.cpp | 32 +++++++++++-------- source/Gui/TableLiveStatistics.h | 4 ++- 6 files changed, 42 insertions(+), 23 deletions(-) diff --git a/source/EngineGpuKernels/ConstructorProcessor.cuh b/source/EngineGpuKernels/ConstructorProcessor.cuh index 66227b15c..94a01ccad 100644 --- a/source/EngineGpuKernels/ConstructorProcessor.cuh +++ b/source/EngineGpuKernels/ConstructorProcessor.cuh @@ -58,6 +58,7 @@ private: __inline__ __device__ static Cell* constructCellIntern( SimulationData& data, + SimulationStatistics& statistics, uint64_t& cellPointerIndex, Cell* hostCell, float2 const& newCellPos, @@ -368,7 +369,7 @@ ConstructorProcessor::startNewConstruction(SimulationData& data, SimulationStati } uint64_t cellPointerIndex; - Cell* newCell = constructCellIntern(data, cellPointerIndex, hostCell, newCellPos, 0, constructionData); + Cell* newCell = constructCellIntern(data, statistics, cellPointerIndex, hostCell, newCellPos, 0, constructionData); if (!newCell->tryLock()) { return false; @@ -389,8 +390,6 @@ ConstructorProcessor::startNewConstruction(SimulationData& data, SimulationStati newCell->maxConnections = max(newCell->numConnections, newCell->maxConnections); newCell->releaseLock(); - - statistics.incNumCreatedCells(hostCell->color); return true; } @@ -458,7 +457,7 @@ __inline__ __device__ bool ConstructorProcessor::continueConstruction( return false; } uint64_t cellPointerIndex; - Cell* newCell = constructCellIntern(data, cellPointerIndex, hostCell, newCellPos, 0, constructionData); + Cell* newCell = constructCellIntern(data, statistics, cellPointerIndex, hostCell, newCellPos, 0, constructionData); if (!newCell->tryLock()) { return false; @@ -593,8 +592,6 @@ __inline__ __device__ bool ConstructorProcessor::continueConstruction( newCell->maxConnections = max(newCell->numConnections, newCell->maxConnections); newCell->releaseLock(); - - statistics.incNumCreatedCells(hostCell->color); return true; } @@ -615,6 +612,7 @@ __inline__ __device__ bool ConstructorProcessor::isConnectable(int numConnection __inline__ __device__ Cell* ConstructorProcessor::constructCellIntern( SimulationData& data, + SimulationStatistics& statistics, uint64_t& cellPointerIndex, Cell* hostCell, float2 const& posOfNewCell, @@ -681,6 +679,9 @@ ConstructorProcessor::constructCellIntern( newConstructor.numInheritedGenomeNodes = static_cast(min(NPP_MAX_16U, numInheritedGenomeNodes)); newConstructor.genomeGeneration = constructor.genomeGeneration + 1; newConstructor.offspringMutationId = constructor.offspringMutationId; + if (GenomeDecoder::containsSelfReplication(newConstructor)) { + statistics.incNumCreatedReplicators(hostCell->color); + } } break; case CellFunction_Sensor: { result->cellFunctionData.sensor.mode = GenomeDecoder::readByte(constructor, genomeCurrentBytePosition) % SensorMode_Count; @@ -726,6 +727,7 @@ ConstructorProcessor::constructCellIntern( } break; } + statistics.incNumCreatedCells(hostCell->color); return result; } diff --git a/source/EngineGpuKernels/SimulationStatistics.cuh b/source/EngineGpuKernels/SimulationStatistics.cuh index 0a7fedf7d..a43401ad9 100644 --- a/source/EngineGpuKernels/SimulationStatistics.cuh +++ b/source/EngineGpuKernels/SimulationStatistics.cuh @@ -82,6 +82,7 @@ public: } __inline__ __device__ void incNumCreatedCells(int color) { alienAtomicAdd64(&_data->timeline.accumulated.numCreatedCells[color], uint64_t(1)); } + __inline__ __device__ void incNumCreatedReplicators(int color) { alienAtomicAdd64(&_data->timeline.accumulated.numCreatedReplicators[color], uint64_t(1)); } __inline__ __device__ void incNumAttacks(int color) { alienAtomicAdd64(&_data->timeline.accumulated.numAttacks[color], uint64_t(1)); } __inline__ __device__ void incNumMuscleActivities(int color) { alienAtomicAdd64(&_data->timeline.accumulated.numMuscleActivities[color], uint64_t(1)); } __inline__ __device__ void incNumDefenderActivities(int color) { alienAtomicAdd64(&_data->timeline.accumulated.numDefenderActivities[color], uint64_t(1)); } diff --git a/source/EngineInterface/RawStatisticsData.h b/source/EngineInterface/RawStatisticsData.h index 7c66c4c67..3f2dff387 100644 --- a/source/EngineInterface/RawStatisticsData.h +++ b/source/EngineInterface/RawStatisticsData.h @@ -19,6 +19,7 @@ struct TimestepStatistics struct AccumulatedStatistics { ColorVector numCreatedCells = {0, 0, 0, 0, 0, 0, 0}; + ColorVector numCreatedReplicators = {0, 0, 0, 0, 0, 0, 0}; ColorVector numAttacks = {0, 0, 0, 0, 0, 0, 0}; ColorVector numMuscleActivities = {0, 0, 0, 0, 0, 0, 0}; ColorVector numDefenderActivities = {0, 0, 0, 0, 0, 0, 0}; diff --git a/source/Gui/StatisticsWindow.cpp b/source/Gui/StatisticsWindow.cpp index 18dce63aa..a84b6f211 100644 --- a/source/Gui/StatisticsWindow.cpp +++ b/source/Gui/StatisticsWindow.cpp @@ -25,7 +25,8 @@ namespace { auto constexpr RightColumnWidth = 175.0f; - auto constexpr RightColumnWidthTable = 150.0f; + auto constexpr RightColumnWidthTimeline = 150.0f; + auto constexpr RightColumnWidthTable = 200.0f; auto constexpr LiveStatisticsDeltaTime = 50; //in millisec } @@ -247,6 +248,12 @@ void _StatisticsWindow::processTablesTab() ImGui::TableSetColumnIndex(1); AlienImGui::Text("Created cells / sec"); + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); + AlienImGui::Text(StringHelper::format(_tableLiveStatistics.getCreatedReplicatorsPerSecond())); + + ImGui::TableSetColumnIndex(1); + AlienImGui::Text("Created self-replicators / sec"); ImGui::EndTable(); } @@ -261,7 +268,7 @@ void _StatisticsWindow::processTimelineStatistics() int row = 0; if (ImGui::BeginTable("##", 2, ImGuiTableFlags_BordersInnerH, ImVec2(-1, 0))) { ImGui::TableSetupColumn("##"); - ImGui::TableSetupColumn("##", ImGuiTableColumnFlags_WidthFixed, scale(RightColumnWidthTable)); + ImGui::TableSetupColumn("##", ImGuiTableColumnFlags_WidthFixed, scale(RightColumnWidthTimeline)); ImPlot::PushColormap(ImPlotColormap_Cool); @@ -326,7 +333,7 @@ void _StatisticsWindow::processTimelineStatistics() ImGui::PushID(2); if (ImGui::BeginTable("##", 2, ImGuiTableFlags_BordersInnerH, ImVec2(-1, 0))) { ImGui::TableSetupColumn("##"); - ImGui::TableSetupColumn("##", ImGuiTableColumnFlags_WidthFixed, scale(RightColumnWidthTable)); + ImGui::TableSetupColumn("##", ImGuiTableColumnFlags_WidthFixed, scale(RightColumnWidthTimeline)); ImPlot::PushColormap(ImPlotColormap_Cool); ImGui::TableNextRow(); diff --git a/source/Gui/TableLiveStatistics.cpp b/source/Gui/TableLiveStatistics.cpp index 977a3acea..684bbff1c 100644 --- a/source/Gui/TableLiveStatistics.cpp +++ b/source/Gui/TableLiveStatistics.cpp @@ -1,12 +1,9 @@ #include "TableLiveStatistics.h" -bool TableLiveStatistics::isDataAvailable() const -{ - return _currentData.has_value(); -} - namespace { + auto constexpr TimeInterval = 5000; //in millisec + uint64_t sum(ColorVector const& valueByColor) { uint64_t result = 0; @@ -17,21 +14,22 @@ namespace } } +bool TableLiveStatistics::isDataAvailable() const +{ + return _currentData.has_value(); +} + float TableLiveStatistics::getCreatedCellsPerSecond() const { if (!_lastDataTimepoint.has_value()) { return 0; } - - auto currentCreatedCells = sum(_currentData->accumulated.numCreatedCells); - auto lastCreatedCells = sum(_lastData->accumulated.numCreatedCells); - return (toFloat(currentCreatedCells) - toFloat(lastCreatedCells)) - / toFloat(std::chrono::duration_cast(*_currentDataTimepoint - *_lastDataTimepoint).count()) * 5000; + return calcObjectsPerSecond(_lastData->accumulated.numCreatedCells, _currentData->accumulated.numCreatedCells); } -float TableLiveStatistics::getReplicatorsPerSecond() const +float TableLiveStatistics::getCreatedReplicatorsPerSecond() const { - return 0; + return calcObjectsPerSecond(_lastData->accumulated.numCreatedReplicators, _currentData->accumulated.numCreatedReplicators); } void TableLiveStatistics::update(TimelineStatistics const& data) @@ -45,7 +43,7 @@ void TableLiveStatistics::update(TimelineStatistics const& data) } auto duration = static_cast(std::chrono::duration_cast(timepoint - *_currentDataTimepoint).count()); - if (duration > 5000) { + if (duration > TimeInterval) { _lastData = _currentData; _lastDataTimepoint = _currentDataTimepoint; @@ -53,3 +51,11 @@ void TableLiveStatistics::update(TimelineStatistics const& data) _currentDataTimepoint = timepoint; } } + +float TableLiveStatistics::calcObjectsPerSecond(ColorVector const& lastCount, ColorVector const& currentCount) const +{ + auto currentCreatedObjects = sum(currentCount); + auto lastCreatedObjects = sum(lastCount); + return (toFloat(currentCreatedObjects) - toFloat(lastCreatedObjects)) + / toFloat(std::chrono::duration_cast(*_currentDataTimepoint - *_lastDataTimepoint).count()) * TimeInterval; +} diff --git a/source/Gui/TableLiveStatistics.h b/source/Gui/TableLiveStatistics.h index 070e1e7dd..0a6ec7208 100644 --- a/source/Gui/TableLiveStatistics.h +++ b/source/Gui/TableLiveStatistics.h @@ -11,11 +11,13 @@ class TableLiveStatistics bool isDataAvailable() const; float getCreatedCellsPerSecond() const; - float getReplicatorsPerSecond() const; + float getCreatedReplicatorsPerSecond() const; void update(TimelineStatistics const& data); private: + float calcObjectsPerSecond(ColorVector const& lastCount, ColorVector const& currentCount) const; + std::optional _currentData; std::optional _currentDataTimepoint;