Skip to content

Commit

Permalink
Changed default 2dRect benchmark to use xml file and removed performa…
Browse files Browse the repository at this point in the history
…nce test cli option (simply always log csv file at the end, no performance overhead during simulation)
  • Loading branch information
TobiasEppacher committed Jan 8, 2024
1 parent 451fc69 commit 1340473
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 145 deletions.
58 changes: 18 additions & 40 deletions benchmarks/2DParticleRect/2DParticleRect.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <array>
#include <memory>

#include "../FileLoader.h"
#include "io/input/FileInputHandler.h"
#include "io/logger/Logger.h"
#include "io/output/FileOutputHandler.h"
#include "particles/containers/ParticleContainer.h"
Expand All @@ -16,6 +18,10 @@ void execute2DRectBenchmark(int rect_width, int rect_height, double spacing, dou
Logger::logger->set_level(spdlog::level::info);
Logger::logger->info("Starting 2DRect-benchmark. Dimensions {}x{}...", rect_width, rect_height);

// Parse input file
auto [initial_particles, simulation_arguments] =
FileInputHandler::readFile(FileLoader::get_input_file_path("2DParticleRect/BenchmarkRectFrame.xml"), true);

// Settings for the Linked Cells Container simulation
std::array<double, 3> domain_size = {300, 300, 3};
std::array<LinkedCellsContainer::BoundaryCondition, 6> boundary_conditions = {
Expand All @@ -28,10 +34,6 @@ void execute2DRectBenchmark(int rect_width, int rect_height, double spacing, dou
CuboidSpawner spawner(center_offset - std::array<double, 3>{rect_width * spacing / 2, rect_height * spacing / 2, 0},
{rect_width, rect_height, 1}, spacing, 1, {0, 0, 0}, 0);

// Settings for the forces for both simulations
std::vector<std::shared_ptr<PairwiseForceSource>> forces;
forces.push_back(std::make_shared<LennardJonesForce>());

// ############################################################
// # Direct Sum Container
// ############################################################
Expand All @@ -41,7 +43,7 @@ void execute2DRectBenchmark(int rect_width, int rect_height, double spacing, dou

// Instantiation of the Direct Sum Container simulation

SimulationParams params_ds("2DParticleRect.xml", 0.01, 5, SimulationParams::DirectSumType{}, {}, {}, forces, true, true);
SimulationParams params_ds{*simulation_arguments};

params_ds.num_particles = particles_ds.size();

Expand All @@ -51,6 +53,7 @@ void execute2DRectBenchmark(int rect_width, int rect_height, double spacing, dou
params_ds.logSummary();
SimulationOverview direct_sum_data = simulation_ds.runSimulation();
direct_sum_data.logSummary();
direct_sum_data.savePerformanceDataCSV("DirectSum" + std::to_string(rect_width) + "x" + std::to_string(rect_height));

// ############################################################
// # Linked Cells Container
Expand All @@ -59,52 +62,27 @@ void execute2DRectBenchmark(int rect_width, int rect_height, double spacing, dou
std::vector<Particle> particles_lc;
spawner.spawnParticles(particles_lc);
// Instantiation of the Linked Cells Container simulation
SimulationParams params_lc{"2DParticleRect.xml",
0.01,
5,
SimulationParams::LinkedCellsType{domain_size, lc_cutoff, boundary_conditions},
{},
{},
forces,
true,
true};
SimulationParams params_lc{*simulation_arguments};
params_lc.container_type = SimulationParams::LinkedCellsType{domain_size, lc_cutoff, boundary_conditions};
params_lc.num_particles = particles_lc.size();

Simulation simulation_lc(particles_lc, params_lc);
// Simulating with Linked Cells Container
params_lc.logSummary();
SimulationOverview linked_cells_data = simulation_lc.runSimulation();
linked_cells_data.logSummary();
linked_cells_data.savePerformanceDataCSV("LinkedCells" + std::to_string(rect_width) + "x" + std::to_string(rect_height));

// ############################################################
// # Comparison Logging
// ############################################################

std::string ds_summary = "";
for (auto& summary : direct_sum_data.interceptor_summaries) {
ds_summary += summary + ";";
}

std::string lc_summary = "";
for (auto& summary : linked_cells_data.interceptor_summaries) {
lc_summary += summary + ";";
}

Logger::logger->info("Simulation of {} particles in a {}x{} grid\n", rect_width * rect_height, rect_width, rect_height);

Logger::logger->info("Direct sum container:");
Logger::logger->info(" Simulation took {:.3f}s", direct_sum_data.total_time_seconds);
Logger::logger->info(" Total iterations: {}", direct_sum_data.total_iterations);
Logger::logger->info(" Average time per iteration: {:.3f}ms\n", direct_sum_data.total_time_seconds / direct_sum_data.total_iterations);
Logger::logger->info(" Summary {}", ds_summary);

Logger::logger->info("Linked cells container:");
Logger::logger->info(" Domain size: {:.0f}x{:.0f}x{:.0f}", domain_size[0], domain_size[1], domain_size[2]);
Logger::logger->info(" Linked cells cutoff radius: {:.0f}", lc_cutoff);
Logger::logger->info(" Simulation took {:.3f}s", linked_cells_data.total_time_seconds);
Logger::logger->info(" Total iterations: {}", linked_cells_data.total_iterations);
Logger::logger->info(" Average time per iteration: {:.3f}ms\n",
linked_cells_data.total_time_seconds / linked_cells_data.total_iterations);
Logger::logger->info(" Summary {}", lc_summary);
Logger::logger->info("Comparison of Direct Sum and Linked Cells Container:");
// Ratio between Direct Sum and Linked Cells Container performance
Logger::logger->info("Runtime Direct Sum: {} s", direct_sum_data.total_time_seconds);
Logger::logger->info("Runtime Linked Cells: {} s", linked_cells_data.total_time_seconds);
Logger::logger->info("Performance ratio (DirectSum / LinkedCells): {}",
direct_sum_data.total_time_seconds / linked_cells_data.total_time_seconds);
}

/*
Expand Down
25 changes: 25 additions & 0 deletions benchmarks/2DParticleRect/BenchmarkRectFrame.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<!-- just a sanity check: body collision rewritten to xml -->
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../simulation_schema.xsd">

<settings>
<delta_t>0.01</delta_t>
<end_time>5.0</end_time>
<third_dimension>false</third_dimension>
<particle_container>
<directsum_container />
</particle_container>
<forces>
<LennardJones />
</forces>
<interceptors>
<ParticleUpdatesPerSecond />
</interceptors>
</settings>


<particle_source>
</particle_source>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ void execute2DRectBenchmark(int rect_width, int rect_height, double spacing, dou
{},
{},
forces,
true,
true};
params_lc.num_particles = particles_lc.size();
Simulation simulation_lc(particles_lc, params_lc);
// Simulating with Linked Cells Container
params_lc.logSummary();
SimulationOverview linked_cells_data = simulation_lc.runSimulation();
linked_cells_data.logSummary();
linked_cells_data.savePerformanceDataCSV("Task4" + std::to_string(rect_width) + "x" + std::to_string(rect_height));
}

int main() {
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ foreach (_source IN ITEMS ${BENCHMARK_SRC})
add_executable(${_name} EXCLUDE_FROM_ALL ${_source})
target_link_libraries(${_name} project_lib)
add_dependencies(benchmarks ${_name})
target_compile_definitions(${_name} PRIVATE BENCHMARK_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
endforeach ()

30 changes: 30 additions & 0 deletions benchmarks/FileLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <io/logger/Logger.h>

#include <filesystem>
#include <string>

#pragma once

class FileLoader {
public:
/**
* @brief Gets the path to the data directory
*
* Uses the cmake target_compile_definitions PRIVATE BENCHMARK_DATA_DIR to define the path to the data directory.
*/
static std::string get_benchmark_data_dir() {
#ifdef BENCHMARK_DATA_DIR
return BENCHMARK_DATA_DIR;
#else
throw std::runtime_error("Error: BENCHMARK_DATA_DIR not defined");
#endif
}

/**
* @brief Gets the path to a file in the input directory
*
* @param file_name The name of the file
* @return std::filesystem::path The path to the file
*/
static std::filesystem::path get_input_file_path(const std::string& file_name) { return {get_benchmark_data_dir() + "/" + file_name}; }
};
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ void execute2DRectBenchmark() {
simple_force_sources,
pairwise_forces,
false,
false,
"./output"};
params_lc.num_particles = particles_lc.size();
Simulation simulation_lc(particles_lc, params_lc);
// Simulating with Linked Cells Container
params_lc.logSummary();
SimulationOverview linked_cells_data = simulation_lc.runSimulation();
linked_cells_data.logSummary();
linked_cells_data.savePerformanceDataCSV("Contest");
}

int main() {
Expand Down
38 changes: 0 additions & 38 deletions benchmarks/profiles/profile_task4.txt

This file was deleted.

1 change: 1 addition & 0 deletions benchmarks/simulation_schema.xsd
3 changes: 3 additions & 0 deletions src/MolSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ int main(int argc, char* argsv[]) {
// Print simulation overview
overview.logSummary();

// Save performance data to csv file
overview.savePerformanceDataCSV();

return 0;
}
5 changes: 0 additions & 5 deletions src/io/cli/CLIParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ struct CLIParams {
*/
std::filesystem::path input_file_path;

/**
* Whether to run a performance test
*/
bool performance_test;

/**
* Whether to use cached data or rerun all the (sub)simulations from scratch
*/
Expand Down
8 changes: 1 addition & 7 deletions src/io/cli/CLIParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ CLIParams parse_arguments(int argc, char* argsv[]) {
std::string log_level;
std::string log_output;

bool performance_test = false;
bool fresh = false;

// choosing 0 as one of the parameters (end_time, delta_t, fps, video_length) is equivalent to choosing the default value
Expand All @@ -22,7 +21,6 @@ CLIParams parse_arguments(int argc, char* argsv[]) {
"The path to the input file. Must be specified, otherwise the program will terminate. Can be inserted as positional argument.");
options_desc.add_options()("log_level,l", boost::program_options::value<std::string>(&log_level)->default_value("info"),
"The log level. Possible values: trace, debug, info, warning, error, critical, off");
options_desc.add_options()("performance_test,p", "Run the simulation in performance test mode");
options_desc.add_options()(
"log_output", boost::program_options::value<std::string>(&log_output)->default_value("std"),
"You can only choose between the output options std(only cl output) and file (only file output). Default: no file output");
Expand Down Expand Up @@ -64,11 +62,8 @@ CLIParams parse_arguments(int argc, char* argsv[]) {
Logger::logger->info(help_message.str());
exit(-1);
}
if (variables_map.count("performance_test")) {
performance_test = true;
}

return CLIParams{input_file_path, performance_test, fresh};
return CLIParams{input_file_path, fresh};
}

SimulationParams merge_parameters(const CLIParams& params_cli, const std::optional<SimulationParams>& file_params) {
Expand All @@ -81,7 +76,6 @@ SimulationParams merge_parameters(const CLIParams& params_cli, const std::option

// Update the parameters with the ones from the command line
params.fresh = params_cli.fresh;
params.performance_test = params_cli.performance_test;

return params;
}
6 changes: 3 additions & 3 deletions src/io/input/xml/XMLFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ std::tuple<std::vector<Particle>, SimulationParams> prepareParticles(std::filesy

auto forces = XSDToInternalTypeAdapter::convertToForces(settings.forces());

auto params = SimulationParams{curr_file_path, settings.delta_t(), settings.end_time(), container_type,
interceptors, std::get<0>(forces), std::get<1>(forces), false,
fresh, output_base_path};
auto params = SimulationParams{curr_file_path, settings.delta_t(), settings.end_time(), container_type,
interceptors, std::get<0>(forces), std::get<1>(forces), fresh,
output_base_path};

if (output_base_path.empty()) {
output_base_path = params.output_dir_path;
Expand Down
File renamed without changes.
File renamed without changes.
31 changes: 0 additions & 31 deletions src/simulation/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
#include <tuple>

#include "integration/IntegrationMethods.h"
#include "io/csv/CSVWriter.h"
#include "io/logger/Logger.h"
#include "particles/containers/directsum/DirectSumContainer.h"
#include "particles/containers/linkedcells/LinkedCellsContainer.h"
#include "simulation/SimulationParams.h"
#include "simulation/interceptors/SimulationInterceptor.h"
#include "simulation/interceptors/frame_writer/FrameWriterInterceptor.h"
#include "simulation/interceptors/particle_update_counter/ParticleUpdateCounterInterceptor.h"
#include "simulation/interceptors/progress_bar/ProgressBarInterceptor.h"
#include "simulation/interceptors/radial_distribution_function/RadialDistributionFunctionInterceptor.h"
#include "simulation/interceptors/thermostat/ThermostatInterceptor.h"
Expand Down Expand Up @@ -98,34 +96,5 @@ SimulationOverview Simulation::runSimulation() {
SimulationOverview overview{params, total_time_ms / 1000.0, iteration, interceptor_summaries,
std::vector<Particle>(particle_container->begin(), particle_container->end())};

if (params.performance_test) {
savePerformanceTest(overview, params);
}

return overview;
}

void Simulation::savePerformanceTest(const SimulationOverview& overview, const SimulationParams& params) {
// write the results to the file
std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
auto formatted_time = fmt::format("{:%d.%m.%Y-%H:%M:%S}", fmt::localtime(now));

CSVWriter csv_writer(
params.output_dir_path / ("performance_test_" + formatted_time + ".csv"),
{"num_particles", "particle_container", "delta_t", "total_time[s]", "particle_updates_per_second[1/s]", "total_iterations"});

// find ParticleUpdateCounterInterceptor
auto particle_update_counter = std::find_if(params.interceptors.begin(), params.interceptors.end(), [](auto& interceptor) {
return std::dynamic_pointer_cast<ParticleUpdateCounterInterceptor>(interceptor) != nullptr;
});

auto particle_updates_per_second =
particle_update_counter != params.interceptors.end()
? std::dynamic_pointer_cast<ParticleUpdateCounterInterceptor>(*particle_update_counter)->getParticleUpdatesPerSecond()
: -1;

std::string container_type_string = std::visit([](auto&& arg) { return std::string(arg); }, params.container_type);

csv_writer.writeRow({params.num_particles, container_type_string, params.delta_t, overview.total_time_seconds,
particle_updates_per_second, overview.total_iterations});
}
2 changes: 0 additions & 2 deletions src/simulation/Simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class Simulation {
*/
std::unique_ptr<IntegrationFunctor> integration_functor;

static void savePerformanceTest(const SimulationOverview& overview, const SimulationParams& params);

/**
* Befriend the interceptors to allow them to access the private members of this class
*/
Expand Down
Loading

0 comments on commit 1340473

Please sign in to comment.