Skip to content

Commit

Permalink
+ implementation of reconnector cells completed
Browse files Browse the repository at this point in the history
+ test for establishing connection made working
+ set random creature id for creator tools
  • Loading branch information
chrxh committed Oct 20, 2023
1 parent 68c6c56 commit c1b7bd1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 32 deletions.
82 changes: 57 additions & 25 deletions source/EngineGpuKernels/ReconnectorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@
#include "SimulationStatistics.cuh"
#include "CellConnectionProcessor.cuh"

class ReconectorProcessor
class ReconnectorProcessor
{
public:
__inline__ __device__ static void process(SimulationData& data, SimulationStatistics& result);

private:
__inline__ __device__ static void processCell(SimulationData& data, SimulationStatistics& statistics, Cell* cell);

__inline__ __device__ static void tryEstablishConnection(SimulationData& data, SimulationStatistics& statistics, Cell* cell, Activity& activity);
__inline__ __device__ static void removeConnections(SimulationData& data, SimulationStatistics& statistics, Cell* cell, Activity& activity);
};

/************************************************************************/
/* Implementation */
/************************************************************************/

__device__ __inline__ void ReconectorProcessor::process(SimulationData& data, SimulationStatistics& result)
__device__ __inline__ void ReconnectorProcessor::process(SimulationData& data, SimulationStatistics& result)
{
auto& operations = data.cellFunctionOperations[CellFunction_Reconnector];
auto partition = calcAllThreadsPartition(operations.getNumEntries());
Expand All @@ -33,34 +36,63 @@ __device__ __inline__ void ReconectorProcessor::process(SimulationData& data, Si
}
}

__device__ __inline__ void ReconectorProcessor::processCell(SimulationData& data, SimulationStatistics& statistics, Cell* cell)
__device__ __inline__ void ReconnectorProcessor::processCell(SimulationData& data, SimulationStatistics& statistics, Cell* cell)
{
auto activity = CellFunctionProcessor::calcInputActivity(cell);
if (abs(activity.channels[0]) >= 0.1f) {
if (activity.channels[0] >= 0.1f) {
tryEstablishConnection(data, statistics, cell, activity);
} else if (activity.channels[0] <= -0.1f) {
removeConnections(data, statistics, cell, activity);
}
CellFunctionProcessor::setActivity(cell, activity);
}

Cell* closestCell = nullptr;
float closestDistance = 0;
data.cellMap.executeForEach(cell->pos, 2.0f, cell->detached, [&](auto const& otherCell) {
if (cell->creatureId != 0 && otherCell->creatureId == cell->creatureId) {
return;
}
if (CellConnectionProcessor::isConnectedConnected(cell, otherCell)) {
return;
}
if (otherCell->barrier) {
return;
}
auto distance = data.cellMap.getDistance(cell->pos, otherCell->pos);
if (!closestCell || distance < closestDistance) {
closestCell = otherCell;
closestDistance = distance;
}
});
__inline__ __device__ void
ReconnectorProcessor::tryEstablishConnection(SimulationData& data, SimulationStatistics& statistics, Cell* cell, Activity& activity)
{
Cell* closestCell = nullptr;
float closestDistance = 0;
data.cellMap.executeForEach(cell->pos, 2.0f, cell->detached, [&](auto const& otherCell) {
if (cell->creatureId != 0 && otherCell->creatureId == cell->creatureId) {
return;
}
if (CellConnectionProcessor::isConnectedConnected(cell, otherCell)) {
return;
}
if (otherCell->barrier) {
return;
}
auto distance = data.cellMap.getDistance(cell->pos, otherCell->pos);
if (!closestCell || distance < closestDistance) {
closestCell = otherCell;
closestDistance = distance;
}
});

if (closestCell) {
CellConnectionProcessor::scheduleAddConnectionPair(cell, closestCell);
activity.channels[1] = 0;
if (closestCell) {
SystemDoubleLock lock;
lock.init(&cell->locked, &closestCell->locked);
if (lock.tryLock()) {
closestCell->maxConnections = min(max(closestCell->maxConnections, closestCell->numConnections + 1), MAX_CELL_BONDS);
cell->maxConnections = min(max(cell->maxConnections, cell->numConnections + 1), MAX_CELL_BONDS);
CellConnectionProcessor::scheduleAddConnectionPair(data, cell, closestCell);
lock.releaseLock();
activity.channels[1] = 1;
}
}
CellFunctionProcessor::setActivity(cell, activity);
}

__inline__ __device__ void ReconnectorProcessor::removeConnections(SimulationData& data, SimulationStatistics& statistics, Cell* cell, Activity& activity)
{
if (cell->tryLock()) {
for (int i = 0; i < cell->numConnections; ++i) {
auto connectedCell = cell->connections[i].cell;
if (connectedCell->creatureId != cell->creatureId) {
CellConnectionProcessor::scheduleDeleteConnectionPair(data, cell, connectedCell);
}
}
cell->releaseLock();
}
}

3 changes: 2 additions & 1 deletion source/EngineGpuKernels/SimulationKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "SensorProcessor.cuh"
#include "CellProcessor.cuh"
#include "ParticleProcessor.cuh"
#include "ReconnectorProcessor.cuh"

__global__ void cudaNextTimestep_prepare(SimulationData data, SimulationStatistics statistics)
{
Expand Down Expand Up @@ -129,7 +130,7 @@ __global__ void cudaNextTimestep_cellFunction_sensor(SimulationData data, Simula

__global__ void cudaNextTimestep_cellFunction_reconector(SimulationData data, SimulationStatistics statistics)
{
ConstructorProcessor::process(data, statistics);
ReconnectorProcessor::process(data, statistics);
}

__global__ void cudaNextTimestep_physics_substep7_innerFriction(SimulationData data)
Expand Down
18 changes: 13 additions & 5 deletions source/EngineInterface/DescriptionHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DataDescription DescriptionHelper::createRect(CreateRectParameters const& parameters)
{
DataDescription result;
auto creatureId = parameters._randomCreatureId ? toInt(NumberGenerator::getInstance().getRandomInt(std::numeric_limits<int>::max())) : 0;
for (int i = 0; i < parameters._width; ++i) {
for (int j = 0; j < parameters._height; ++j) {
result.addCell(CellDescription()
Expand All @@ -22,7 +23,8 @@ DataDescription DescriptionHelper::createRect(CreateRectParameters const& parame
.setStiffness(parameters._stiffness)
.setMaxConnections(parameters._maxConnections)
.setColor(parameters._color)
.setBarrier(parameters._barrier));
.setBarrier(parameters._barrier)
.setCreatureId(creatureId));
}
}
reconnectCells(result, parameters._cellDistance * 1.1f);
Expand All @@ -36,6 +38,7 @@ DataDescription DescriptionHelper::createRect(CreateRectParameters const& parame
DataDescription DescriptionHelper::createHex(CreateHexParameters const& parameters)
{
DataDescription result;
auto creatureId = parameters._randomCreatureId ? toInt(NumberGenerator::getInstance().getRandomInt(std::numeric_limits<int>::max())) : 0;
auto incY = sqrt(3.0) * parameters._cellDistance / 2.0;
for (int j = 0; j < parameters._layers; ++j) {
for (int i = -(parameters._layers - 1); i < parameters._layers - j; ++i) {
Expand All @@ -48,7 +51,8 @@ DataDescription DescriptionHelper::createHex(CreateHexParameters const& paramete
.setPos({toFloat(i * parameters._cellDistance + j * parameters._cellDistance / 2.0), toFloat(-j * incY)})
.setMaxConnections(parameters._maxConnections)
.setColor(parameters._color)
.setBarrier(parameters._barrier));
.setBarrier(parameters._barrier)
.setCreatureId(creatureId));

//create cell: under layer (except for 0-layer)
if (j > 0) {
Expand All @@ -59,7 +63,8 @@ DataDescription DescriptionHelper::createHex(CreateHexParameters const& paramete
.setPos({toFloat(i * parameters._cellDistance + j * parameters._cellDistance / 2.0), toFloat(j * incY)})
.setMaxConnections(parameters._maxConnections)
.setColor(parameters._color)
.setBarrier(parameters._barrier));
.setBarrier(parameters._barrier)
.setCreatureId(creatureId));
}
}
}
Expand All @@ -76,6 +81,7 @@ DataDescription DescriptionHelper::createHex(CreateHexParameters const& paramete
DataDescription DescriptionHelper::createUnconnectedCircle(CreateUnconnectedCircleParameters const& parameters)
{
DataDescription result;
auto creatureId = parameters._randomCreatureId ? toInt(NumberGenerator::getInstance().getRandomInt(std::numeric_limits<int>::max())) : 0;

if (parameters._radius <= 1 + NEAR_ZERO) {
result.addCell(CellDescription()
Expand All @@ -85,7 +91,8 @@ DataDescription DescriptionHelper::createUnconnectedCircle(CreateUnconnectedCirc
.setStiffness(parameters._stiffness)
.setMaxConnections(parameters._maxConnections)
.setColor(parameters._color)
.setBarrier(parameters._barrier));
.setBarrier(parameters._barrier)
.setCreatureId(creatureId));
return result;
}

Expand All @@ -109,7 +116,8 @@ DataDescription DescriptionHelper::createUnconnectedCircle(CreateUnconnectedCirc
.setPos({parameters._center.x + dxMod, parameters._center.y + dy})
.setMaxConnections(parameters._maxConnections)
.setColor(parameters._color)
.setBarrier(parameters._barrier));
.setBarrier(parameters._barrier)
.setCreatureId(creatureId));

}
}
Expand Down
3 changes: 3 additions & 0 deletions source/EngineInterface/DescriptionHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DescriptionHelper
MEMBER_DECLARATION(CreateRectParameters, int, maxConnections, 6);
MEMBER_DECLARATION(CreateRectParameters, int, color, 0);
MEMBER_DECLARATION(CreateRectParameters, bool, barrier, false);
MEMBER_DECLARATION(CreateRectParameters, bool, randomCreatureId, true);
};
static DataDescription createRect(CreateRectParameters const& parameters);

Expand All @@ -32,6 +33,7 @@ class DescriptionHelper
MEMBER_DECLARATION(CreateHexParameters, int, maxConnections, 6);
MEMBER_DECLARATION(CreateHexParameters, int, color, 0);
MEMBER_DECLARATION(CreateHexParameters, bool, barrier, false);
MEMBER_DECLARATION(CreateHexParameters, bool, randomCreatureId, true);
};
static DataDescription createHex(CreateHexParameters const& parameters);

Expand All @@ -45,6 +47,7 @@ class DescriptionHelper
MEMBER_DECLARATION(CreateUnconnectedCircleParameters, int, maxConnections, 6);
MEMBER_DECLARATION(CreateUnconnectedCircleParameters, int, color, 0);
MEMBER_DECLARATION(CreateUnconnectedCircleParameters, bool, barrier, false);
MEMBER_DECLARATION(CreateUnconnectedCircleParameters, bool, randomCreatureId, true);
};
static DataDescription createUnconnectedCircle(CreateUnconnectedCircleParameters const& parameters);

Expand Down
6 changes: 6 additions & 0 deletions source/EngineInterface/Descriptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,12 @@ struct CellDescription
activationTime = value;
return *this;
}
CellDescription& setCreatureId(int value)
{
creatureId = value;
return *this;
}


bool hasGenome() const;
std::vector<uint8_t>& getGenomeRef();
Expand Down
5 changes: 4 additions & 1 deletion source/Gui/CreatorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,17 @@ void _CreatorWindow::finishDrawing()

void _CreatorWindow::createCell()
{
auto creatureId = toInt(NumberGenerator::getInstance().getRandomInt(std::numeric_limits<int>::max()));

auto cell = CellDescription()
.setPos(getRandomPos())
.setEnergy(_energy)
.setStiffness(_stiffness)
.setMaxConnections(_maxConnections)
.setExecutionOrderNumber(_lastExecutionNumber)
.setColor(_editorModel->getDefaultColorCode())
.setBarrier(_barrier);
.setBarrier(_barrier)
.setCreatureId(creatureId);
if (_ascendingExecutionNumbers) {
cell.setInputExecutionOrderNumber((_lastExecutionNumber + 5) % 6);
}
Expand Down

0 comments on commit c1b7bd1

Please sign in to comment.