Skip to content

Commit

Permalink
show created self-replicators per second
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Aug 2, 2024
1 parent 6750c40 commit 9baa012
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
14 changes: 8 additions & 6 deletions source/EngineGpuKernels/ConstructorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private:

__inline__ __device__ static Cell* constructCellIntern(
SimulationData& data,
SimulationStatistics& statistics,
uint64_t& cellPointerIndex,
Cell* hostCell,
float2 const& newCellPos,
Expand Down Expand Up @@ -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;
Expand All @@ -389,8 +390,6 @@ ConstructorProcessor::startNewConstruction(SimulationData& data, SimulationStati
newCell->maxConnections = max(newCell->numConnections, newCell->maxConnections);

newCell->releaseLock();

statistics.incNumCreatedCells(hostCell->color);
return true;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -593,8 +592,6 @@ __inline__ __device__ bool ConstructorProcessor::continueConstruction(
newCell->maxConnections = max(newCell->numConnections, newCell->maxConnections);

newCell->releaseLock();

statistics.incNumCreatedCells(hostCell->color);
return true;
}

Expand All @@ -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,
Expand Down Expand Up @@ -681,6 +679,9 @@ ConstructorProcessor::constructCellIntern(
newConstructor.numInheritedGenomeNodes = static_cast<uint16_t>(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;
Expand Down Expand Up @@ -726,6 +727,7 @@ ConstructorProcessor::constructCellIntern(
} break;
}

statistics.incNumCreatedCells(hostCell->color);
return result;
}

Expand Down
1 change: 1 addition & 0 deletions source/EngineGpuKernels/SimulationStatistics.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -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)); }
Expand Down
1 change: 1 addition & 0 deletions source/EngineInterface/RawStatisticsData.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct TimestepStatistics
struct AccumulatedStatistics
{
ColorVector<uint64_t> numCreatedCells = {0, 0, 0, 0, 0, 0, 0};
ColorVector<uint64_t> numCreatedReplicators = {0, 0, 0, 0, 0, 0, 0};
ColorVector<uint64_t> numAttacks = {0, 0, 0, 0, 0, 0, 0};
ColorVector<uint64_t> numMuscleActivities = {0, 0, 0, 0, 0, 0, 0};
ColorVector<uint64_t> numDefenderActivities = {0, 0, 0, 0, 0, 0, 0};
Expand Down
13 changes: 10 additions & 3 deletions source/Gui/StatisticsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
32 changes: 19 additions & 13 deletions source/Gui/TableLiveStatistics.cpp
Original file line number Diff line number Diff line change
@@ -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<uint64_t> const& valueByColor)
{
uint64_t result = 0;
Expand All @@ -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<std::chrono::milliseconds>(*_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)
Expand All @@ -45,11 +43,19 @@ void TableLiveStatistics::update(TimelineStatistics const& data)
}

auto duration = static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(timepoint - *_currentDataTimepoint).count());
if (duration > 5000) {
if (duration > TimeInterval) {
_lastData = _currentData;
_lastDataTimepoint = _currentDataTimepoint;

_currentData = data;
_currentDataTimepoint = timepoint;
}
}

float TableLiveStatistics::calcObjectsPerSecond(ColorVector<uint64_t> const& lastCount, ColorVector<uint64_t> const& currentCount) const
{
auto currentCreatedObjects = sum(currentCount);
auto lastCreatedObjects = sum(lastCount);
return (toFloat(currentCreatedObjects) - toFloat(lastCreatedObjects))
/ toFloat(std::chrono::duration_cast<std::chrono::milliseconds>(*_currentDataTimepoint - *_lastDataTimepoint).count()) * TimeInterval;
}
4 changes: 3 additions & 1 deletion source/Gui/TableLiveStatistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t> const& lastCount, ColorVector<uint64_t> const& currentCount) const;

std::optional<TimelineStatistics> _currentData;
std::optional<std::chrono::steady_clock::time_point> _currentDataTimepoint;

Expand Down

0 comments on commit 9baa012

Please sign in to comment.