Skip to content

Commit

Permalink
+ move code for max age balancing to SimulationParameters
Browse files Browse the repository at this point in the history
+ update simulation parameters properly in loading flashbacks
  • Loading branch information
chrxh committed Nov 6, 2024
1 parent 92aba36 commit e64930e
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 127 deletions.
4 changes: 2 additions & 2 deletions source/EngineGpuKernels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ add_library(EngineGpuKernels
SimulationKernels.cuh
SimulationKernelsLauncher.cu
SimulationKernelsLauncher.cuh
SimulationParameterService.cu
SimulationParameterService.cuh
SimulationParametersService.cu
SimulationParametersService.cuh
SimulationStatistics.cuh
SpotCalculator.cuh
StatisticsService.cu
Expand Down
12 changes: 8 additions & 4 deletions source/EngineGpuKernels/SimulationCudaFacade.cu
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
#include "StatisticsKernelsLauncher.cuh"
#include "SelectionResult.cuh"
#include "RenderingData.cuh"
#include "SimulationParameterService.cuh"
#include "SimulationParametersService.cuh"
#include "TestKernelsLauncher.cuh"
#include "StatisticsService.cuh"
#include "MaxAgeBalancer.cuh"

namespace
{
Expand All @@ -63,6 +64,7 @@ _SimulationCudaFacade::_SimulationCudaFacade(uint64_t timestep, Settings const&
_cudaSelectionResult = std::make_shared<SelectionResult>();
_cudaAccessTO = std::make_shared<DataTO>();
_cudaSimulationStatistics = std::make_shared<SimulationStatistics>();
_maxAgeBalancer = std::make_shared<_MaxAgeBalancer>();

_cudaSimulationData->init({settings.generalSettings.worldSizeX, settings.generalSettings.worldSizeY}, timestep);
_cudaRenderingData->init();
Expand Down Expand Up @@ -141,7 +143,7 @@ void _SimulationCudaFacade::calcTimestep(uint64_t timesteps, bool forceUpdateSta
auto statistics = getRawStatistics();
{
std::lock_guard lock(_mutexForSimulationParameters);
if (_simulationKernels->updateSimulationParametersAfterTimestep(_settings, simulationData, statistics)) {
if (SimulationParametersService::get().updateSimulationParametersAfterTimestep(_settings, _maxAgeBalancer, simulationData, statistics)) {
CHECK_FOR_CUDA_ERROR(
cudaMemcpyToSymbol(cudaSimulationParameters, &_settings.simulationParameters, sizeof(SimulationParameters), 0, cudaMemcpyHostToDevice));
}
Expand Down Expand Up @@ -399,10 +401,11 @@ SimulationParameters _SimulationCudaFacade::getSimulationParameters() const
return _newSimulationParameters ? *_newSimulationParameters : _settings.simulationParameters;
}

void _SimulationCudaFacade::setSimulationParameters(SimulationParameters const& parameters)
void _SimulationCudaFacade::setSimulationParameters(SimulationParameters const& parameters, SimulationParametersUpdateConfig const& updateConfig)
{
std::lock_guard lock(_mutexForSimulationParameters);
_newSimulationParameters = parameters;
_simulationParametersUpdateConfig = updateConfig;
}

auto _SimulationCudaFacade::getArraySizes() const -> ArraySizes
Expand Down Expand Up @@ -640,7 +643,8 @@ void _SimulationCudaFacade::checkAndProcessSimulationParameterChanges()
{
std::lock_guard lock(_mutexForSimulationParameters);
if (_newSimulationParameters) {
_settings.simulationParameters = SimulationParameterService::get().integrateChanges(_settings.simulationParameters, *_newSimulationParameters);
_settings.simulationParameters =
SimulationParametersService::get().integrateChanges(_settings.simulationParameters, *_newSimulationParameters, _simulationParametersUpdateConfig);
CHECK_FOR_CUDA_ERROR(
cudaMemcpyToSymbol(cudaSimulationParameters, &_settings.simulationParameters, sizeof(SimulationParameters), 0, cudaMemcpyHostToDevice));
_newSimulationParameters.reset();
Expand Down
8 changes: 7 additions & 1 deletion source/EngineGpuKernels/SimulationCudaFacade.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "EngineInterface/ShallowUpdateSelectionData.h"
#include "EngineInterface/MutationType.h"
#include "EngineInterface/StatisticsHistory.h"
#include "EngineInterface/SimulationParametersUpdateConfig.h"

#include "Definitions.cuh"

Expand Down Expand Up @@ -70,7 +71,9 @@ public:

void setGpuConstants(GpuSettings const& cudaConstants);
SimulationParameters getSimulationParameters() const;
void setSimulationParameters(SimulationParameters const& parameters);
void setSimulationParameters(
SimulationParameters const& parameters,
SimulationParametersUpdateConfig const& updateConfig = SimulationParametersUpdateConfig::All);

ArraySizes getArraySizes() const;

Expand Down Expand Up @@ -107,6 +110,8 @@ private:

mutable std::mutex _mutexForSimulationParameters;
std::optional<SimulationParameters> _newSimulationParameters;
SimulationParametersUpdateConfig _simulationParametersUpdateConfig = SimulationParametersUpdateConfig::All;

Settings _settings;

mutable std::mutex _mutexForSimulationData;
Expand All @@ -121,6 +126,7 @@ private:
std::optional<RawStatisticsData> _statisticsData;
StatisticsHistory _statisticsHistory;
std::shared_ptr<SimulationStatistics> _cudaSimulationStatistics;
MaxAgeBalancer _maxAgeBalancer;

SimulationKernelsLauncher _simulationKernels;
DataAccessKernelsLauncher _dataAccessKernels;
Expand Down
62 changes: 0 additions & 62 deletions source/EngineGpuKernels/SimulationKernelsLauncher.cu
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
#include "FlowFieldKernels.cuh"
#include "GarbageCollectorKernelsLauncher.cuh"
#include "DebugKernels.cuh"
#include "MaxAgeBalancer.cuh"
#include "SimulationStatistics.cuh"

_SimulationKernelsLauncher::_SimulationKernelsLauncher()
{
_garbageCollector = std::make_shared<_GarbageCollectorKernelsLauncher>();
_maxAgeBalancer = std::make_shared<_MaxAgeBalancer>();
}

namespace
Expand Down Expand Up @@ -92,66 +90,6 @@ void _SimulationKernelsLauncher::calcTimestep(Settings const& settings, Simulati
_garbageCollector->cleanupAfterTimestep(settings.gpuSettings, data);
}

bool _SimulationKernelsLauncher::updateSimulationParametersAfterTimestep(
Settings& settings,
SimulationData const& simulationData,
RawStatisticsData const& statistics)
{
auto result = false;

auto const& worldSizeX = settings.generalSettings.worldSizeX;
auto const& worldSizeY = settings.generalSettings.worldSizeY;
SpaceCalculator space({worldSizeX, worldSizeY});
for (int i = 0; i < settings.simulationParameters.numRadiationSources; ++i) {
auto& source = settings.simulationParameters.radiationSources[i];
if (source.velX != 0) {
source.posX += source.velX * settings.simulationParameters.timestepSize;
result = true;
}
if (source.velY != 0) {
source.posY += source.velY * settings.simulationParameters.timestepSize;
result = true;
}
auto correctedPosition = space.getCorrectedPosition({source.posX, source.posY});
source.posX = correctedPosition.x;
source.posY = correctedPosition.y;
}
for (int i = 0; i < settings.simulationParameters.numSpots; ++i) {
auto& spot = settings.simulationParameters.spots[i];
if (spot.velX != 0) {
spot.posX += spot.velX * settings.simulationParameters.timestepSize;
result = true;
}
if (spot.velY != 0) {
spot.posY += spot.velY * settings.simulationParameters.timestepSize;
result = true;
}
auto correctedPosition = space.getCorrectedPosition({spot.posX, spot.posY});
spot.posX = correctedPosition.x;
spot.posY = correctedPosition.y;
}

auto externalEnergyPresent = settings.simulationParameters.externalEnergy > 0;
for (int i = 0; i < MAX_COLORS; ++i) {
externalEnergyPresent |= settings.simulationParameters.externalEnergyBackflowFactor[i] > 0;
}
externalEnergyPresent &= settings.simulationParameters.features.externalEnergyControl;
if (externalEnergyPresent) {
double temp;
CHECK_FOR_CUDA_ERROR(cudaMemcpy(
&temp,
simulationData.externalEnergy,
sizeof(double),
cudaMemcpyDeviceToHost));
settings.simulationParameters.externalEnergy = toFloat(temp);
result = true;
}

result |= _maxAgeBalancer->balance(settings.simulationParameters, statistics, simulationData.timestep);

return result;
}

void _SimulationKernelsLauncher::prepareForSimulationParametersChanges(Settings const& settings, SimulationData const& data)
{
auto const gpuSettings = settings.gpuSettings;
Expand Down
6 changes: 0 additions & 6 deletions source/EngineGpuKernels/SimulationKernelsLauncher.cuh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "EngineInterface/Settings.h"
#include "EngineInterface/RawStatisticsData.h"

#include "Definitions.cuh"
#include "Macros.cuh"
Expand All @@ -12,16 +11,11 @@ public:
_SimulationKernelsLauncher();

void calcTimestep(Settings const& settings, SimulationData const& simulationData, SimulationStatistics const& statistics);
bool updateSimulationParametersAfterTimestep(
Settings& settings,
SimulationData const& simulationData,
RawStatisticsData const& statistics); //returns true if parameters have been changed
void prepareForSimulationParametersChanges(Settings const& settings, SimulationData const& simulationData);

private:
bool isRigidityUpdateEnabled(Settings const& settings) const;

GarbageCollectorKernelsLauncher _garbageCollector;
MaxAgeBalancer _maxAgeBalancer;
};

31 changes: 0 additions & 31 deletions source/EngineGpuKernels/SimulationParameterService.cu

This file was deleted.

13 changes: 0 additions & 13 deletions source/EngineGpuKernels/SimulationParameterService.cuh

This file was deleted.

98 changes: 98 additions & 0 deletions source/EngineGpuKernels/SimulationParametersService.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "SimulationParametersService.cuh"

#include <vector>

#include "EngineInterface/SimulationParameters.h"
#include "EngineInterface/SpaceCalculator.h"

#include "Base.cuh"
#include "SimulationData.cuh"
#include "MaxAgeBalancer.cuh"

SimulationParameters SimulationParametersService::integrateChanges(
SimulationParameters const& currentParameters,
SimulationParameters const& changedParameters,
SimulationParametersUpdateConfig const& updateConfig) const
{
auto result = changedParameters;

if (updateConfig == SimulationParametersUpdateConfig::AllExceptChangingPositions) {
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;
}

bool SimulationParametersService::updateSimulationParametersAfterTimestep(
Settings& settings,
MaxAgeBalancer const& maxAgeBalancer,
SimulationData const& simulationData,
RawStatisticsData const& statistics)
{
auto result = false;

auto const& worldSizeX = settings.generalSettings.worldSizeX;
auto const& worldSizeY = settings.generalSettings.worldSizeY;
SpaceCalculator space({worldSizeX, worldSizeY});
for (int i = 0; i < settings.simulationParameters.numRadiationSources; ++i) {
auto& source = settings.simulationParameters.radiationSources[i];
if (abs(source.velX) > NEAR_ZERO) {
source.posX += source.velX * settings.simulationParameters.timestepSize;
result = true;
}
if (abs(source.velY) > NEAR_ZERO) {
source.posY += source.velY * settings.simulationParameters.timestepSize;
result = true;
}
auto correctedPosition = space.getCorrectedPosition({source.posX, source.posY});
source.posX = correctedPosition.x;
source.posY = correctedPosition.y;
}
for (int i = 0; i < settings.simulationParameters.numSpots; ++i) {
auto& spot = settings.simulationParameters.spots[i];
if (abs(spot.velX) > NEAR_ZERO) {
spot.posX += spot.velX * settings.simulationParameters.timestepSize;
result = true;
}
if (abs(spot.velY) > NEAR_ZERO) {
spot.posY += spot.velY * settings.simulationParameters.timestepSize;
result = true;
}
auto correctedPosition = space.getCorrectedPosition({spot.posX, spot.posY});
spot.posX = correctedPosition.x;
spot.posY = correctedPosition.y;
}

auto externalEnergyPresent = settings.simulationParameters.externalEnergy > 0;
for (int i = 0; i < MAX_COLORS; ++i) {
externalEnergyPresent |= settings.simulationParameters.externalEnergyBackflowFactor[i] > 0;
}
externalEnergyPresent &= settings.simulationParameters.features.externalEnergyControl;
if (externalEnergyPresent) {
double temp;
CHECK_FOR_CUDA_ERROR(cudaMemcpy(&temp, simulationData.externalEnergy, sizeof(double), cudaMemcpyDeviceToHost));
settings.simulationParameters.externalEnergy = toFloat(temp);
result = true;
}

result |= maxAgeBalancer->balance(settings.simulationParameters, statistics, simulationData.timestep);

return result;
}
26 changes: 26 additions & 0 deletions source/EngineGpuKernels/SimulationParametersService.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <optional>

#include "Base/Singleton.h"

#include "EngineInterface/RawStatisticsData.h"
#include "EngineInterface/Settings.h"
#include "EngineInterface/SimulationParametersUpdateConfig.h"

#include "Definitions.cuh"

class SimulationParametersService
{
MAKE_SINGLETON(SimulationParametersService);

public:
SimulationParameters integrateChanges(
SimulationParameters const& currentParameters,
SimulationParameters const& changedParameters,
SimulationParametersUpdateConfig const& updateConfig) const;

bool updateSimulationParametersAfterTimestep(
Settings& settings,
MaxAgeBalancer const& maxAgeBalancer,
SimulationData const& simulationData,
RawStatisticsData const& statistics); //returns true if parameters have been changed
};
4 changes: 2 additions & 2 deletions source/EngineImpl/EngineWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ SimulationParameters EngineWorker::getSimulationParameters() const
return _simulationCudaFacade->getSimulationParameters();
}

void EngineWorker::setSimulationParameters(SimulationParameters const& parameters)
void EngineWorker::setSimulationParameters(SimulationParameters const& parameters, SimulationParametersUpdateConfig const& updateConfig)
{
_simulationCudaFacade->setSimulationParameters(parameters);
_simulationCudaFacade->setSimulationParameters(parameters, updateConfig);
}

void EngineWorker::setGpuSettings_async(GpuSettings const& gpuSettings)
Expand Down
Loading

0 comments on commit e64930e

Please sign in to comment.