Skip to content

Commit

Permalink
only adopt non-changing parameters from user input in a running simul…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
chrxh committed Nov 5, 2024
1 parent bdb94db commit 3f5e983
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 18 deletions.
2 changes: 2 additions & 0 deletions source/EngineGpuKernels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ add_library(EngineGpuKernels
SimulationKernels.cuh
SimulationKernelsLauncher.cu
SimulationKernelsLauncher.cuh
SimulationParameterService.cu
SimulationParameterService.cuh
SimulationStatistics.cuh
SpotCalculator.cuh
StatisticsService.cu
Expand Down
2 changes: 1 addition & 1 deletion source/EngineGpuKernels/EditKernelsLauncher.cu
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void _EditKernelsLauncher::shallowUpdateSelectedObjects(
setValueToDevice(_cudaCenter, float2{0, 0});
setValueToDevice(_cudaNumEntities, 0);

setValueToDevice(_cudaMinCellPosYAndIndex, 0xffffffff00000000);
setValueToDevice(_cudaMinCellPosYAndIndex, 0xffffffff00000000ull);
KERNEL_CALL(cudaCalcCellWithMinimalPosY, data, _cudaMinCellPosYAndIndex);
cudaDeviceSynchronize();
auto refCellIndex = static_cast<int>(copyToHost(_cudaMinCellPosYAndIndex) & 0xffffffff);
Expand Down
17 changes: 11 additions & 6 deletions source/EngineGpuKernels/MutationProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ __inline__ __device__ void MutationProcessor::applyRandomMutationsForCell(Simula
{
auto& constructor = cell->cellFunctionData.constructor;
auto numNodes = toFloat(GenomeDecoder::getNumNodesRecursively(constructor.genome, constructor.genomeSize, false, true));
auto numNonSeparatedNodes = toFloat(GenomeDecoder::getNumNodesRecursively(constructor.genome, constructor.genomeSize, false, false));
auto cellCopyMutationNeuronData = SpotCalculator::calcParameter(
&SimulationParametersSpotValues::cellCopyMutationNeuronData,
&SimulationParametersSpotActivatedValues::cellCopyMutationNeuronData,
Expand Down Expand Up @@ -132,22 +133,26 @@ __inline__ __device__ void MutationProcessor::applyRandomMutationsForCell(Simula

executeMultipleEvents(data, cellCopyMutationCellProperties, [&]() { propertiesMutation(data, cell); });
executeMultipleEvents(data, cellCopyMutationNeuronData, [&]() { neuronDataMutation(data, cell); });
executeEvent(data, cellCopyMutationGeometry, [&]() { geometryMutation(data, cell); });
executeEvent(data, cellCopyMutationGeometry, [&]() {
if (numNodes < 2 * numNonSeparatedNodes) {
geometryMutation(data, cell);
}
});
executeEvent(data, cellCopyMutationCustomGeometry, [&]() { customGeometryMutation(data, cell); });
executeMultipleEvents(data, cellCopyMutationCellFunction, [&]() { cellFunctionMutation(data, cell); });
executeEvent(data, cellCopyMutationInsertion, [&]() {
auto numNonSeparatedNodes = toFloat(GenomeDecoder::getNumNodesRecursively(constructor.genome, constructor.genomeSize, false, false));
if (numNodes < 2 * numNonSeparatedNodes) {
insertMutation(data, cell);
}
});
executeEvent(data, cellCopyMutationDeletion, [&]() { deleteMutation(data, cell); });
executeEvent(data, cellCopyMutationCellColor, [&]() { cellColorMutation(data, cell); });
executeEvent(data, cellCopyMutationTranslation, [&]() { translateMutation(data, cell); });
executeEvent(data, cellCopyMutationTranslation, [&]() {
if (numNodes < 2 * numNonSeparatedNodes) {
translateMutation(data, cell);
}
});
executeEvent(data, cellCopyMutationDuplication, [&]() {
auto& constructor = cell->cellFunctionData.constructor;
auto numNodes = toFloat(GenomeDecoder::getNumNodesRecursively(constructor.genome, constructor.genomeSize, false, true));
auto numNonSeparatedNodes = toFloat(GenomeDecoder::getNumNodesRecursively(constructor.genome, constructor.genomeSize, false, false));
if (numNodes < 2 * numNonSeparatedNodes) {
duplicateMutation(data, cell);
}
Expand Down
6 changes: 4 additions & 2 deletions source/EngineGpuKernels/SimulationCudaFacade.cu
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "StatisticsKernelsLauncher.cuh"
#include "SelectionResult.cuh"
#include "RenderingData.cuh"
#include "SimulationParameterService.cuh"
#include "TestKernelsLauncher.cuh"
#include "StatisticsService.cuh"

Expand Down Expand Up @@ -639,8 +640,9 @@ void _SimulationCudaFacade::checkAndProcessSimulationParameterChanges()
{
std::lock_guard lock(_mutexForSimulationParameters);
if (_newSimulationParameters) {
_settings.simulationParameters = *_newSimulationParameters;
CHECK_FOR_CUDA_ERROR(cudaMemcpyToSymbol(cudaSimulationParameters, &*_newSimulationParameters, sizeof(SimulationParameters), 0, cudaMemcpyHostToDevice));
_settings.simulationParameters = SimulationParameterService::get().integrateChanges(_settings.simulationParameters, *_newSimulationParameters);
CHECK_FOR_CUDA_ERROR(
cudaMemcpyToSymbol(cudaSimulationParameters, &_settings.simulationParameters, sizeof(SimulationParameters), 0, cudaMemcpyHostToDevice));
_newSimulationParameters.reset();

if (_cudaSimulationData) {
Expand Down
31 changes: 31 additions & 0 deletions source/EngineGpuKernels/SimulationParameterService.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "SimulationParameterService.cuh"

#include <vector>

#include "EngineInterface/SimulationParameters.h"

SimulationParameters SimulationParameterService::integrateChanges(SimulationParameters const& currentParameters, SimulationParameters const& changedParameters) const
{
auto result = changedParameters;

auto numSpots = std::min(currentParameters.numSpots, changedParameters.numSpots);
for (int i = 0; i < numSpots; ++i) {
if (currentParameters.spots[i].velX != 0) {
result.spots[i].posX = currentParameters.spots[i].posX;
}
if (currentParameters.spots[i].velY != 0) {
result.spots[i].posY = currentParameters.spots[i].posY;
}
}

auto numRadiationSources = std::min(currentParameters.numRadiationSources, changedParameters.numRadiationSources);
for (int i = 0; i < numRadiationSources; ++i) {
if (currentParameters.radiationSources[i].velX != 0) {
result.radiationSources[i].posX = currentParameters.radiationSources[i].posX;
}
if (currentParameters.spots[i].velY != 0) {
result.radiationSources[i].posY = currentParameters.radiationSources[i].posY;
}
}
return result;
}
13 changes: 13 additions & 0 deletions source/EngineGpuKernels/SimulationParameterService.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <optional>

#include "Base/Singleton.h"

#include "Definitions.cuh"

class SimulationParameterService
{
MAKE_SINGLETON(SimulationParameterService);

public:
SimulationParameters integrateChanges(SimulationParameters const& currentParameters, SimulationParameters const& changedParameters) const;
};
9 changes: 4 additions & 5 deletions source/Gui/AutosaveWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void AutosaveWindow::processTable()
}
if (entry->state == SavepointState_Persisted) {
auto triggerLoadSavepoint = AlienImGui::ActionButton(AlienImGui::ActionButtonParameters().buttonText(ICON_FA_DOWNLOAD));
AlienImGui::Tooltip("Load savepoint", false);
AlienImGui::Tooltip("Load save point", false);
if (triggerLoadSavepoint) {
onLoadSavepoint(entry);
}
Expand Down Expand Up @@ -269,7 +269,7 @@ void AutosaveWindow::processSettings()
AlienImGui::Switcher(
AlienImGui::SwitcherParameters()
.name("Mode")
.values({"Circular save files", "Unlimited save files"})
.values({"Limited save files", "Unlimited save files"})
.textWidth(RightColumnWidth)
.defaultValue(_origSaveMode),
_saveMode);
Expand Down Expand Up @@ -309,7 +309,7 @@ void AutosaveWindow::onCreateSavepoint(bool usePeakSimulation)
if (usePeakSimulation && !_peakDeserializedSimulation->isEmpty()) {
auto senderInfo = SenderInfo{.senderId = SenderId{AutosaveSenderId}, .wishResultData = true, .wishErrorInfo = true};
auto saveData = SaveDeserializedSimulationRequestData{
.filename = _directory, .sharedDeserializedSimulation = _peakDeserializedSimulation, .generateNameFromTimestep = true};
.filename = _directory, .sharedDeserializedSimulation = _peakDeserializedSimulation, .generateNameFromTimestep = true, .resetDeserializedSimulation = true};
requestId = _persisterFacade->scheduleSaveDeserializedSimulation(senderInfo, saveData);
} else {
auto senderInfo = SenderInfo{.senderId = SenderId{AutosaveSenderId}, .wishResultData = true, .wishErrorInfo = true};
Expand Down Expand Up @@ -365,7 +365,7 @@ void AutosaveWindow::processAutomaticSavepoints()

if (_catchPeaks != CatchPeaks_None) {
auto minSinceLastCatchPeak = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - _lastPeakTimepoint).count();
if (minSinceLastCatchPeak >= 30) {
if (minSinceLastCatchPeak >= 10) {
_peakProcessor->executeTask(
[&](auto const& senderId) {
return _persisterFacade->scheduleGetPeakSimulation(
Expand Down Expand Up @@ -444,7 +444,6 @@ void AutosaveWindow::updateSavepoint(int row)
newEntry->filename = data.filename;
newEntry->peak = StringHelper::format(toFloat(sumColorVector(data.rawStatisticsData.timeline.timestep.genomeComplexityVariance)), 2);
newEntry->peakType = "genome complexity variance";
_peakDeserializedSimulation->reset();
}
}
if (requestState.value() == PersisterRequestState::Error) {
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/OverlayController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ void OverlayController::processProgressAnimation()
ImColor::HSV(0.66f, 1.0f, 0.1f, 0.0f));
auto N = 8 + toInt(powf(sinf(duration / 500.0f - Const::Pi / 4) + 1, 3.0f) * 5);
for (int i = 0; i < N; ++i) {
auto amplitude1 = sinf(toFloat(i) * 10.0f / toFloat(N) - duration / 700.0f * (1.0f + 0.3f * sinf(duration / 1000)));
auto amplitude2 = sinf(toFloat(i) * 14.0f / toFloat(N) - duration / 100.0f * (1.0f + 0.3f * cosf(duration / 1000)));
auto amplitude1 = sinf(toFloat(i) * 10.0f / toFloat(N) - duration / 700.0f * (1.0f + 0.3f * sinf(std::min(5000.0f, duration) / 1000)));
auto amplitude2 = sinf(toFloat(i) * 14.0f / toFloat(N) - duration / 100.0f * (1.0f + 0.3f * cosf(std::min(5000.0f, duration) / 1000)));
//auto hue = toFloat((i * 1000 / N + toInt(duration)) % 3000) / 4500.0f;
//hue = hue < 0.33f ? 0.66f + hue : 0.66f + 0.66f - hue;
auto y1 = center.y + height / 2 - amplitude1 * height;
Expand Down
12 changes: 10 additions & 2 deletions source/PersisterImpl/PersisterWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ _PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest
DeserializedSimulation deserializedSimulation;
deserializedSimulation.statistics = _simulationFacade->getStatisticsHistory().getCopiedData();
auto currentRawStatistics = _simulationFacade->getRawStatistics();
printf(
"current: %f, peak: %f\n",
sumColorVector(currentRawStatistics.timeline.timestep.genomeComplexityVariance),
sumColorVector(peakStatistics.timeline.timestep.genomeComplexityVariance));
if (sumColorVector(currentRawStatistics.timeline.timestep.genomeComplexityVariance)
>= sumColorVector(peakStatistics.timeline.timestep.genomeComplexityVariance)) {

Expand Down Expand Up @@ -668,15 +672,19 @@ _PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest
if (!SerializerService::get().serializeSimulationToFiles(filename, deserializedData)) {
throw std::runtime_error("Error");
}

return std::make_shared<_SaveDeserializedSimulationRequestResult>(
auto result = std::make_shared<_SaveDeserializedSimulationRequestResult>(
request->getRequestId(),
SaveDeserializedSimulationResultData{
.filename = filename,
.projectName = deserializedData.auxiliaryData.simulationParameters.projectName,
.timestep = deserializedData.auxiliaryData.timestep,
.timestamp = requestData.sharedDeserializedSimulation->getTimestamp(),
.rawStatisticsData = requestData.sharedDeserializedSimulation->getRawStatisticsData()});

if (requestData.resetDeserializedSimulation) {
requestData.sharedDeserializedSimulation->reset();
}
return result;
} catch (...) {
return std::make_shared<_PersisterRequestError>(
request->getRequestId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ struct SaveDeserializedSimulationRequestData
std::filesystem::path filename;
SharedDeserializedSimulation sharedDeserializedSimulation;
bool generateNameFromTimestep = false;
bool resetDeserializedSimulation = false;
};
2 changes: 2 additions & 0 deletions source/PersisterInterface/SavepointTableService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ void SavepointTableService::updateFile(SavepointTable& table) const
boost::property_tree::ptree tree;
encodeDecode(tree, table, ParserTask::Encode);
boost::property_tree::json_parser::write_json(stream, tree);
} catch (std::exception const& e) {
throw std::runtime_error(std::string("The following error occurred: ") + e.what());
} catch (...) {
throw std::runtime_error("Unknown error.");
}
Expand Down

0 comments on commit 3f5e983

Please sign in to comment.