Skip to content

Commit

Permalink
removed benchmark mode and added file i/o toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
xLPMG committed Jan 10, 2024
1 parent fe0f105 commit 6f6882f
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 141 deletions.
12 changes: 8 additions & 4 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ vars.AddVariables(
'enables the GUI',
'yes',
allowed_values=('yes', 'no')
),
EnumVariable( 'use_filesystem',
'enables or disabled the filesystem usage',
'yes',
allowed_values=('yes', 'no')
)
)

Expand Down Expand Up @@ -206,11 +211,10 @@ if 'san' in env['mode']:
'-fsanitize=undefined' ] )

#####################
# BENCHMARKING MODE #
# NO FILESYSTEM #
#####################
if 'benchmark' in env['mode']:
env.Append( CXXFLAGS = [ '-DBENCHMARK' ] )

if 'no' in env['use_filesystem']:
env.Append( CXXFLAGS = [ '-DNOFILESYSTEM' ] )

env.Append( CXXFLAGS = [ '-isystem', 'include' ] )
#####################
Expand Down
227 changes: 120 additions & 107 deletions src/Launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <limits>
#include <chrono>

#ifndef BENCHMARK
#ifndef NOFILESYSTEM
#include <filesystem>
#endif

Expand Down Expand Up @@ -124,6 +124,11 @@ void tsunami_lab::Launcher::loadConfiguration()
else if (l_boundaryStringB == "wall" || l_boundaryStringB == "WALL")
m_boundaryB = Boundary::WALL;

m_useFileIO = m_configData.value("useFileIO", true);
#ifdef NOFILESYSTEM
m_useFileIO = false;
#endif

m_bathymetryFilePath = m_configData.value("bathymetry", "");
m_displacementFilePath = m_configData.value("displacement", "");

Expand All @@ -132,17 +137,18 @@ void tsunami_lab::Launcher::loadConfiguration()

// read station data
m_stationFrequency = m_configData.value("stationFrequency", 1);
#ifndef BENCHMARK
std::string l_outputMethod = m_configData.value("outputMethod", "netcdf");
if (l_outputMethod == "netcdf" || l_outputMethod == "NETCDF")
if (m_useFileIO)
{
m_dataWriter = NETCDF;
}
else if (l_outputMethod == "csv" || l_outputMethod == "CSV")
{
m_dataWriter = CSV;
std::string l_outputMethod = m_configData.value("outputMethod", "netcdf");
if (l_outputMethod == "netcdf" || l_outputMethod == "NETCDF")
{
m_dataWriter = NETCDF;
}
else if (l_outputMethod == "csv" || l_outputMethod == "CSV")
{
m_dataWriter = CSV;
}
}
#endif
}

void tsunami_lab::Launcher::constructSetup()
Expand Down Expand Up @@ -321,9 +327,8 @@ void tsunami_lab::Launcher::constructSolver()
m_dy = m_simulationSizeY / m_ny;
createWaveProp();
std::cout << "Setting up solver..." << std::endl;
// set up solver
#ifndef BENCHMARK
if (m_setupChoice == "CHECKPOINT")
// set up solver
if (m_setupChoice == "CHECKPOINT" && m_useFileIO)
{
tsunami_lab::t_real *l_hCheck = new tsunami_lab::t_real[m_nx * m_ny];
tsunami_lab::t_real *l_huCheck = new tsunami_lab::t_real[m_nx * m_ny];
Expand Down Expand Up @@ -367,9 +372,7 @@ void tsunami_lab::Launcher::constructSolver()
delete[] l_hvCheck;
delete[] l_bCheck;
}
#endif

if (m_setupChoice != "CHECKPOINT")
else
{
#ifdef USEOMP
#pragma omp parallel for
Expand Down Expand Up @@ -519,22 +522,23 @@ void tsunami_lab::Launcher::deriveTimeStep()
m_scalingY = m_dt / m_dy;

// options for checkpointing
#ifndef BENCHMARK
std::cout << "Writing every " << m_writingFrequency << " time steps" << std::endl;
if (m_checkpointFrequency > 0)
if (m_useFileIO)
{
std::cout << "Saving checkpoint every " << m_checkpointFrequency << " seconds" << std::endl;
}
else
{
std::cout << "Warning: Checkpoints have been disabled for this run. " << std::endl;
}
// set count in case we load from a checkpoint file
if (m_simTime > 0)
{
m_captureCount = std::floor(m_simTime / m_stationFrequency);
std::cout << "Writing every " << m_writingFrequency << " time steps" << std::endl;
if (m_checkpointFrequency > 0)
{
std::cout << "Saving checkpoint every " << m_checkpointFrequency << " seconds" << std::endl;
}
else
{
std::cout << "Warning: Checkpoints have been disabled for this run. " << std::endl;
}
// set count in case we load from a checkpoint file
if (m_simTime > 0)
{
m_captureCount = std::floor(m_simTime / m_stationFrequency);
}
}
#endif
}

void tsunami_lab::Launcher::writeCheckpoint()
Expand All @@ -557,65 +561,66 @@ void tsunami_lab::Launcher::runCalculation()
//------------------------------------------//
//---------------Write output---------------//
//------------------------------------------//
#ifndef BENCHMARK
if (m_timeStep % m_writingFrequency == 0)
if (m_useFileIO)
{
std::cout << " simulation time / #time steps: "
<< m_simTime << " / " << m_timeStep << std::endl;

switch (m_dataWriter)
if (m_timeStep % m_writingFrequency == 0)
{
case NETCDF:
{
std::cout << " writing to netcdf " << std::endl;
m_netCdf->write(m_waveProp->getStride(),
m_waveProp->getHeight(),
m_waveProp->getMomentumX(),
m_waveProp->getMomentumY(),
m_waveProp->getBathymetry(),
m_simTime);
break;
std::cout << " simulation time / #time steps: "
<< m_simTime << " / " << m_timeStep << std::endl;

switch (m_dataWriter)
{
case NETCDF:
{
std::cout << " writing to netcdf " << std::endl;
m_netCdf->write(m_waveProp->getStride(),
m_waveProp->getHeight(),
m_waveProp->getMomentumX(),
m_waveProp->getMomentumY(),
m_waveProp->getBathymetry(),
m_simTime);
break;
}
case CSV:
{
std::string l_csvOutputPath = "solutions/" + m_outputFileName + "_" + std::to_string(m_nOut) + ".csv";
std::cout << " writing wave field to " << l_csvOutputPath << std::endl;
std::ofstream l_file;
l_file.open(l_csvOutputPath);
tsunami_lab::io::Csv::write(m_dx,
m_dy,
m_nx,
m_ny,
m_waveProp->getStride(),
m_waveProp->getHeight(),
m_waveProp->getMomentumX(),
m_waveProp->getMomentumY(),
m_waveProp->getBathymetry(),
l_file);
l_file.close();
m_nOut++;
break;
}
}
}
case CSV:
// write stations
if (m_simTime >= m_stationFrequency * m_captureCount)
{
std::string l_csvOutputPath = "solutions/" + m_outputFileName + "_" + std::to_string(m_nOut) + ".csv";
std::cout << " writing wave field to " << l_csvOutputPath << std::endl;
std::ofstream l_file;
l_file.open(l_csvOutputPath);
tsunami_lab::io::Csv::write(m_dx,
m_dy,
m_nx,
m_ny,
m_waveProp->getStride(),
m_waveProp->getHeight(),
m_waveProp->getMomentumX(),
m_waveProp->getMomentumY(),
m_waveProp->getBathymetry(),
l_file);
l_file.close();
m_nOut++;
break;
}
for (tsunami_lab::io::Station *l_s : m_stations)
{
l_s->capture(m_simTime);
}
++m_captureCount;
}
}
// write stations
if (m_simTime >= m_stationFrequency * m_captureCount)
{
for (tsunami_lab::io::Station *l_s : m_stations)
// write checkpoint
if (m_checkpointFrequency > 0 &&
std::chrono::system_clock::now() - l_lastWrite >= std::chrono::duration<float>(m_checkpointFrequency))
{
l_s->capture(m_simTime);
std::cout << "saving checkpoint to " << m_checkPointFilePathString << std::endl;
writeCheckpoint();
l_lastWrite = std::chrono::system_clock::now();
}
++m_captureCount;
}
// write checkpoint
if (m_checkpointFrequency > 0 &&
std::chrono::system_clock::now() - l_lastWrite >= std::chrono::duration<float>(m_checkpointFrequency))
{
std::cout << "saving checkpoint to " << m_checkPointFilePathString << std::endl;
writeCheckpoint();
l_lastWrite = std::chrono::system_clock::now();
}
#endif
//------------------------------------------//
//------------Update loop params------------//
//------------------------------------------//
Expand All @@ -630,14 +635,15 @@ void tsunami_lab::Launcher::freeMemory()
{
delete m_setup;
delete m_waveProp;
#ifndef BENCHMARK
std::filesystem::remove(m_checkPointFilePathString);
delete m_netCdf;
for (tsunami_lab::io::Station *l_s : m_stations)
if (m_useFileIO)
{
delete l_s;
std::filesystem::remove(m_checkPointFilePathString);
delete m_netCdf;
for (tsunami_lab::io::Station *l_s : m_stations)
{
delete l_s;
}
}
#endif
}

//-------------------------------------------//
Expand Down Expand Up @@ -669,23 +675,27 @@ int tsunami_lab::Launcher::start(std::string i_config)
if (SHOULD_EXIT)
return 0;

#ifndef BENCHMARK
setupFolders();
#endif
if (m_useFileIO)
{
setupFolders();
}

if (SHOULD_EXIT)
return 0;
loadConfigDataFromFile(m_configFilePath);

if (SHOULD_EXIT)
return 0;
#ifndef BENCHMARK
configureFiles();
#else
l_setupChoice = l_configData.value("setup", "CIRCULARDAMBREAK2D");
if (l_setupChoice == "CHECKPOINT")
std::cerr << "Error: Cannot use checkpoints in benchmarking mode" << std::endl;
#endif
if (m_useFileIO)
{
configureFiles();
}
else
{
m_setupChoice = m_configData.value("setup", "CIRCULARDAMBREAK2D");
if (m_setupChoice == "CHECKPOINT")
std::cerr << "Error: Cannot use checkpoints in benchmarking mode" << std::endl;
}

if (SHOULD_EXIT)
return 0;
Expand All @@ -697,9 +707,10 @@ int tsunami_lab::Launcher::start(std::string i_config)

if (SHOULD_EXIT)
return 0;
#ifndef BENCHMARK
setUpNetCdf();
#endif
if (m_useFileIO)
{
setUpNetCdf();
}

if (SHOULD_EXIT)
return 0;
Expand All @@ -711,9 +722,10 @@ int tsunami_lab::Launcher::start(std::string i_config)

if (SHOULD_EXIT)
return 0;
#ifndef BENCHMARK
loadStations();
#endif
if (m_useFileIO)
{
loadStations();
}

if (SHOULD_EXIT)
return 0;
Expand All @@ -728,10 +740,11 @@ int tsunami_lab::Launcher::start(std::string i_config)
runCalculation();
std::cout << "finished time loop" << std::endl;

// write station data to files
#ifndef BENCHMARK
writeStations();
#endif
// write station data to files
if (m_useFileIO)
{
writeStations();
}

// free memory
freeMemory();
Expand Down
11 changes: 11 additions & 0 deletions src/Launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class tsunami_lab::Launcher
json m_configData;

bool SHOULD_EXIT = false;
bool m_useFileIO = true;

// input parameters
std::string m_bathymetryFilePath = "";
Expand Down Expand Up @@ -308,6 +309,16 @@ class tsunami_lab::Launcher
*/
void runCalculation();

/**
* Enables or disables file I/O.
*
* @param i_useFileIO true if file I/O should be enabled.
* @return void
*/
void toggleFileIO(bool i_useFileIO){
m_useFileIO = i_useFileIO;
}

int start(std::string i_config);

void startLauncher();
Expand Down
Loading

0 comments on commit 6f6882f

Please sign in to comment.