Skip to content

Commit

Permalink
performance optimization: applying mutations in an extra step
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Aug 10, 2024
1 parent db4f279 commit 8cfcb65
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
47 changes: 23 additions & 24 deletions source/EngineGpuKernels/ConstructorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ private:
__inline__ __device__ static ConstructionData readConstructionData(Cell* cell);
__inline__ __device__ static bool isConstructionTriggered(SimulationData const& data, Cell* cell, Activity const& activity);

__inline__ __device__ static bool tryConstructCell(SimulationData& data, SimulationStatistics& statistics, Cell* hostCell, ConstructionData const& constructionData);
__inline__ __device__ static Cell* tryConstructCell(SimulationData& data, SimulationStatistics& statistics, Cell* hostCell, ConstructionData const& constructionData);

__inline__ __device__ static Cell* getLastConstructedCell(Cell* hostCell);
__inline__ __device__ static bool startNewConstruction(SimulationData& data, SimulationStatistics& statistics, Cell* hostCell, ConstructionData const& constructionData);
__inline__ __device__ static bool continueConstruction(SimulationData& data, SimulationStatistics& statistics, Cell* hostCell, ConstructionData const& constructionData);
__inline__ __device__ static Cell* startNewConstruction(SimulationData& data, SimulationStatistics& statistics, Cell* hostCell, ConstructionData const& constructionData);
__inline__ __device__ static Cell* continueConstruction(SimulationData& data, SimulationStatistics& statistics, Cell* hostCell, ConstructionData const& constructionData);

__inline__ __device__ static bool isConnectable(int numConnections, int maxConnections, bool adaptMaxConnections);

Expand Down Expand Up @@ -279,27 +279,27 @@ ConstructorProcessor::isConstructionTriggered(SimulationData const& data, Cell*
return true;
}

__inline__ __device__ bool
__inline__ __device__ Cell*
ConstructorProcessor::tryConstructCell(SimulationData& data, SimulationStatistics& statistics, Cell* hostCell, ConstructionData const& constructionData)
{
if (!hostCell->tryLock()) {
return false;
return nullptr;
}
if (constructionData.lastConstructionCell) {
if (!constructionData.lastConstructionCell->tryLock()) {
hostCell->releaseLock();
return false;
return nullptr;
}
auto success = continueConstruction(data, statistics, hostCell, constructionData);
auto newCell = continueConstruction(data, statistics, hostCell, constructionData);

constructionData.lastConstructionCell->releaseLock();
hostCell->releaseLock();
return success;
return newCell;
} else {
auto success = startNewConstruction(data, statistics, hostCell, constructionData);
auto newCell = startNewConstruction(data, statistics, hostCell, constructionData);

hostCell->releaseLock();
return success;
return newCell;
}
}

Expand Down Expand Up @@ -333,29 +333,29 @@ __inline__ __device__ Cell* ConstructorProcessor::getLastConstructedCell(Cell* h
return nullptr;
}

__inline__ __device__ bool
__inline__ __device__ Cell*
ConstructorProcessor::startNewConstruction(SimulationData& data, SimulationStatistics& statistics, Cell* hostCell, ConstructionData const& constructionData)
{
auto& constructor = hostCell->cellFunctionData.constructor;

if (!isConnectable(hostCell->numConnections, hostCell->maxConnections, true)) {
return false;
return nullptr;
}
auto anglesForNewConnection = CellFunctionProcessor::calcLargestGapReferenceAndActualAngle(data, hostCell, constructionData.angle);

auto newCellDirection = Math::unitVectorOfAngle(anglesForNewConnection.actualAngle);
float2 newCellPos = hostCell->pos + newCellDirection;

if (CellConnectionProcessor::existCrossingConnections(data, hostCell->pos, newCellPos, hostCell->detached, hostCell->color)) {
return false;
return nullptr;
}

if (cudaSimulationParameters.cellFunctionConstructorCheckCompletenessForSelfReplication && !constructor.isComplete) {
return false;
return nullptr;
}

if (!checkAndReduceHostEnergy(data, hostCell, constructionData)) {
return false;
return nullptr;
}

if (GenomeDecoder::containsSelfReplication(constructor)) {
Expand All @@ -370,7 +370,7 @@ ConstructorProcessor::startNewConstruction(SimulationData& data, SimulationStati
Cell* newCell = constructCellIntern(data, statistics, cellPointerIndex, hostCell, newCellPos, 0, constructionData);

if (!newCell->tryLock()) {
return false;
return nullptr;
}

if (!constructionData.isLastNodeOfLastRepetition || !constructionData.genomeHeader.separateConstruction) {
Expand All @@ -388,10 +388,10 @@ ConstructorProcessor::startNewConstruction(SimulationData& data, SimulationStati
newCell->maxConnections = max(newCell->numConnections, newCell->maxConnections);

newCell->releaseLock();
return true;
return newCell;
}

__inline__ __device__ bool ConstructorProcessor::continueConstruction(
__inline__ __device__ Cell* ConstructorProcessor::continueConstruction(
SimulationData& data,
SimulationStatistics& statistics,
Cell* hostCell,
Expand All @@ -406,7 +406,7 @@ __inline__ __device__ bool ConstructorProcessor::continueConstruction(

if (Math::length(posDelta) <= cudaSimulationParameters.cellMinDistance
|| constructionSiteDistance - desiredDistance < cudaSimulationParameters.cellMinDistance) {
return false;
return nullptr;
}

auto newCellPos = hostCell->pos + posDelta;
Expand Down Expand Up @@ -447,18 +447,18 @@ __inline__ __device__ bool ConstructorProcessor::continueConstruction(
}
if (constructionData.numRequiredAdditionalConnections != -1) {
if (numOtherCells < constructionData.numRequiredAdditionalConnections) {
return false;
return nullptr;
}
}

if (!checkAndReduceHostEnergy(data, hostCell, constructionData)) {
return false;
return nullptr;
}
uint64_t cellPointerIndex;
Cell* newCell = constructCellIntern(data, statistics, cellPointerIndex, hostCell, newCellPos, 0, constructionData);

if (!newCell->tryLock()) {
return false;
return nullptr;
}
if (constructionData.lastConstructionCell->livingState == LivingState_Dying) {
newCell->livingState = LivingState_Dying;
Expand Down Expand Up @@ -590,7 +590,7 @@ __inline__ __device__ bool ConstructorProcessor::continueConstruction(
newCell->maxConnections = max(newCell->numConnections, newCell->maxConnections);

newCell->releaseLock();
return true;
return newCell;
}

__inline__ __device__ bool ConstructorProcessor::isConnectable(int numConnections, int maxConnections, bool adaptMaxConnections)
Expand Down Expand Up @@ -679,7 +679,6 @@ ConstructorProcessor::constructCellIntern(
newConstructor.offspringMutationId = constructor.offspringMutationId;
if (GenomeDecoder::containsSelfReplication(newConstructor)) {
statistics.incNumCreatedReplicators(hostCell->color);
MutationProcessor::applyRandomMutation(data, result);
}
} break;
case CellFunction_Sensor: {
Expand Down
25 changes: 20 additions & 5 deletions source/EngineGpuKernels/MutationProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class MutationProcessor
{
public:

__inline__ __device__ static void applyRandomMutation(SimulationData& data, Cell* cell);
__inline__ __device__ static void applyRandomMutations(SimulationData& data);
__inline__ __device__ static void applyRandomMutationsForCell(SimulationData& data, Cell* cell);

__inline__ __device__ static void neuronDataMutation(SimulationData& data, Cell* cell);
__inline__ __device__ static void propertiesMutation(SimulationData& data, Cell* cell);
Expand All @@ -37,7 +38,20 @@ private:
/************************************************************************/
/* Implementation */
/************************************************************************/
__inline__ __device__ void MutationProcessor::applyRandomMutation(SimulationData& data, Cell* cell)
__inline__ __device__ void MutationProcessor::applyRandomMutations(SimulationData& data)
{
auto& cells = data.objects.cellPointers;
auto partition = calcAllThreadsPartition(cells.getNumEntries());

for (int index = partition.startIndex; index <= partition.endIndex; ++index) {
auto& cell = cells.at(index);
if (cell->livingState== LivingState_Activating && cell->cellFunction == CellFunction_Constructor) {
MutationProcessor::applyRandomMutationsForCell(data, cell);
}
}
}

__inline__ __device__ void MutationProcessor::applyRandomMutationsForCell(SimulationData& data, Cell* cell)
{
auto& constructor = cell->cellFunctionData.constructor;
auto numNodes = toFloat(GenomeDecoder::getNumNodesRecursively(constructor.genome, constructor.genomeSize, false, true));
Expand Down Expand Up @@ -836,9 +850,10 @@ __inline__ __device__ void MutationProcessor::genomeColorMutation(SimulationData
template <typename Func>
__inline__ __device__ void MutationProcessor::executeEvent(SimulationData& data, float probability, Func eventFunc)
{
for (int i = 0, j = toInt(probability); i < j; ++i) {
eventFunc();
}
//TODO avoid performance deterioration
//for (int i = 0, j = toInt(probability); i < j; ++i) {
// eventFunc();
//}
if (isRandomEvent(data, probability)) {
eventFunc();
}
Expand Down
1 change: 1 addition & 0 deletions source/EngineGpuKernels/SimulationKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ __global__ void cudaNextTimestep_physics_verletVelocityUpdate(SimulationData dat
__global__ void cudaNextTimestep_cellFunction_prepare_substep1(SimulationData data)
{
CellProcessor::aging(data);
MutationProcessor::applyRandomMutations(data);
}

__global__ void cudaNextTimestep_cellFunction_prepare_substep2(SimulationData data)
Expand Down

0 comments on commit 8cfcb65

Please sign in to comment.