perf(multi): Simple parallel multi zone solver
Added a simple parallel multi-zone solver
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
|
||||
#include <clocale>
|
||||
|
||||
#include "gridfire/reaction/reaclib.h"
|
||||
#include "gridfire/utils/gf_omp.h"
|
||||
|
||||
|
||||
static std::terminate_handler g_previousHandler = nullptr;
|
||||
@@ -31,7 +31,7 @@ gridfire::NetIn init(const double temp, const double rho, const double tMax) {
|
||||
std::setlocale(LC_ALL, "");
|
||||
g_previousHandler = std::set_terminate(quill_terminate_handler);
|
||||
quill::Logger* logger = fourdst::logging::LogManager::getInstance().getLogger("log");
|
||||
logger->set_log_level(quill::LogLevel::TraceL2);
|
||||
logger->set_log_level(quill::LogLevel::Info);
|
||||
|
||||
using namespace gridfire;
|
||||
const std::vector<double> X = {0.7081145999999999, 2.94e-5, 0.276, 0.003, 0.0011, 9.62e-3, 1.62e-3, 5.16e-4};
|
||||
@@ -143,7 +143,7 @@ void log_results(const gridfire::NetOut& netOut, const gridfire::NetIn& netIn) {
|
||||
}
|
||||
|
||||
|
||||
void record_abundance_history_callback(const gridfire::solver::CVODESolverStrategy::TimestepContext& ctx) {
|
||||
void record_abundance_history_callback(const gridfire::solver::PointSolverTimestepContext& ctx) {
|
||||
s_wrote_abundance_history = true;
|
||||
const auto& engine = ctx.engine;
|
||||
// std::unordered_map<std::string, std::pair<double, double>> abundances;
|
||||
@@ -224,11 +224,12 @@ void quill_terminate_handler()
|
||||
std::abort();
|
||||
}
|
||||
|
||||
void callback_main(const gridfire::solver::CVODESolverStrategy::TimestepContext& ctx) {
|
||||
void callback_main(const gridfire::solver::PointSolverTimestepContext& ctx) {
|
||||
record_abundance_history_callback(ctx);
|
||||
}
|
||||
|
||||
int main() {
|
||||
GF_PAR_INIT();
|
||||
using namespace gridfire;
|
||||
|
||||
constexpr size_t breaks = 1;
|
||||
@@ -239,98 +240,20 @@ int main() {
|
||||
const NetIn netIn = init(temp, rho, tMax);
|
||||
|
||||
policy::MainSequencePolicy stellarPolicy(netIn.composition);
|
||||
policy::ConstructionResults construct = stellarPolicy.construct();
|
||||
auto [engine, ctx_template] = stellarPolicy.construct();
|
||||
std::println("Sandbox Engine Stack: {}", stellarPolicy);
|
||||
std::println("Scratch Blob State: {}", *construct.scratch_blob);
|
||||
std::println("Scratch Blob State: {}", *ctx_template);
|
||||
|
||||
|
||||
constexpr size_t runs = 1000;
|
||||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// arrays to store timings
|
||||
std::array<std::chrono::duration<double>, runs> setup_times;
|
||||
std::array<std::chrono::duration<double>, runs> eval_times;
|
||||
std::array<NetOut, runs> serial_results;
|
||||
for (size_t i = 0; i < runs; ++i) {
|
||||
auto start_setup_time = std::chrono::high_resolution_clock::now();
|
||||
std::print("Run {}/{}\r", i + 1, runs);
|
||||
solver::CVODESolverStrategy solver(construct.engine, *construct.scratch_blob);
|
||||
// solver.set_callback(solver::CVODESolverStrategy::TimestepCallback(callback_main));
|
||||
solver.set_stdout_logging_enabled(false);
|
||||
auto end_setup_time = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> setup_elapsed = end_setup_time - start_setup_time;
|
||||
setup_times[i] = setup_elapsed;
|
||||
|
||||
auto start_eval_time = std::chrono::high_resolution_clock::now();
|
||||
const NetOut netOut = solver.evaluate(netIn);
|
||||
auto end_eval_time = std::chrono::high_resolution_clock::now();
|
||||
serial_results[i] = netOut;
|
||||
std::chrono::duration<double> eval_elapsed = end_eval_time - start_eval_time;
|
||||
eval_times[i] = eval_elapsed;
|
||||
|
||||
// log_results(netOut, netIn);
|
||||
}
|
||||
auto endTime = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> elapsed = endTime - startTime;
|
||||
std::println("");
|
||||
|
||||
// Summarize serial timings
|
||||
double total_setup_time = 0.0;
|
||||
double total_eval_time = 0.0;
|
||||
for (size_t i = 0; i < runs; ++i) {
|
||||
total_setup_time += setup_times[i].count();
|
||||
total_eval_time += eval_times[i].count();
|
||||
}
|
||||
std::println("Average Setup Time over {} runs: {:.6f} seconds", runs, total_setup_time / runs);
|
||||
std::println("Average Evaluation Time over {} runs: {:.6f} seconds", runs, total_eval_time / runs);
|
||||
std::println("Total Time for {} runs: {:.6f} seconds", runs, elapsed.count());
|
||||
std::println("Final H-1 Abundances Serial: {}", serial_results[0].composition.getMolarAbundance(fourdst::atomic::H_1));
|
||||
|
||||
// OPTIONAL: Prevent CppAD from returning memory to the system
|
||||
// during execution to reduce overhead (can speed up tight loops)
|
||||
CppAD::thread_alloc::hold_memory(true);
|
||||
|
||||
std::array<NetOut, runs> parallelResults;
|
||||
std::array<std::chrono::duration<double>, runs> setupTimes;
|
||||
std::array<std::chrono::duration<double>, runs> evalTimes;
|
||||
std::array<std::unique_ptr<gridfire::engine::scratch::StateBlob>, runs> workspaces;
|
||||
for (size_t i = 0; i < runs; ++i) {
|
||||
workspaces[i] = construct.scratch_blob->clone_structure();
|
||||
constexpr size_t nZones = 100;
|
||||
std::array<NetIn, nZones> netIns;
|
||||
for (size_t zone = 0; zone < nZones; ++zone) {
|
||||
netIns[zone] = netIn;
|
||||
netIns[zone].temperature = 1.0e7;
|
||||
}
|
||||
|
||||
const solver::PointSolver localSolver(engine);
|
||||
solver::GridSolverContext solverCtx(*ctx_template);
|
||||
const solver::GridSolver gridSolver(engine, localSolver);
|
||||
|
||||
// Parallel runs
|
||||
startTime = std::chrono::high_resolution_clock::now();
|
||||
for (size_t i = 0; i < runs; ++i) {
|
||||
auto start_setup_time = std::chrono::high_resolution_clock::now();
|
||||
solver::CVODESolverStrategy solver(construct.engine, *workspaces[i]);
|
||||
solver.set_stdout_logging_enabled(false);
|
||||
auto end_setup_time = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> setup_elapsed = end_setup_time - start_setup_time;
|
||||
setupTimes[i] = setup_elapsed;
|
||||
auto start_eval_time = std::chrono::high_resolution_clock::now();
|
||||
parallelResults[i] = solver.evaluate(netIn);
|
||||
auto end_eval_time = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> eval_elapsed = end_eval_time - start_eval_time;
|
||||
evalTimes[i] = eval_elapsed;
|
||||
}
|
||||
endTime = std::chrono::high_resolution_clock::now();
|
||||
elapsed = endTime - startTime;
|
||||
std::println("");
|
||||
|
||||
// Summarize parallel timings
|
||||
total_setup_time = 0.0;
|
||||
total_eval_time = 0.0;
|
||||
for (size_t i = 0; i < runs; ++i) {
|
||||
total_setup_time += setupTimes[i].count();
|
||||
total_eval_time += evalTimes[i].count();
|
||||
}
|
||||
|
||||
std::println("Average Parallel Setup Time over {} runs: {:.6f} seconds", runs, total_setup_time / runs);
|
||||
std::println("Average Parallel Evaluation Time over {} runs: {:.6f} seconds", runs, total_eval_time / runs);
|
||||
std::println("Total Parallel Time for {} runs: {:.6f} seconds", runs, elapsed.count());
|
||||
|
||||
std::println("Final H-1 Abundances Parallel: {}", utils::iterable_to_delimited_string(parallelResults, ",", [](const auto& result) {
|
||||
return result.composition.getMolarAbundance(fourdst::atomic::H_1);
|
||||
}));
|
||||
std::vector<NetOut> netOuts = gridSolver.evaluate(solverCtx, netIns | std::ranges::to<std::vector>());
|
||||
}
|
||||
@@ -4,8 +4,8 @@ executable(
|
||||
dependencies: [gridfire_dep, cli11_dep],
|
||||
)
|
||||
|
||||
executable(
|
||||
'spectral_sandbox',
|
||||
'spectral_main.cpp',
|
||||
dependencies: [gridfire_dep, cli11_dep]
|
||||
)
|
||||
#executable(
|
||||
# 'spectral_sandbox',
|
||||
# 'spectral_main.cpp',
|
||||
# dependencies: [gridfire_dep, cli11_dep]
|
||||
#)
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "gridfire/gridfire.h"
|
||||
|
||||
#include "fourdst/composition/composition.h"
|
||||
#include "fourdst/logging/logging.h"
|
||||
#include "fourdst/atomic/species.h"
|
||||
#include "fourdst/composition/utils.h"
|
||||
|
||||
#include "quill/Logger.h"
|
||||
#include "quill/Backend.h"
|
||||
#include "CLI/CLI.hpp"
|
||||
|
||||
#include <clocale>
|
||||
|
||||
|
||||
static std::terminate_handler g_previousHandler = nullptr;
|
||||
static std::vector<std::pair<double, std::unordered_map<std::string, std::pair<double, double>>>> g_callbackHistory;
|
||||
static bool s_wrote_abundance_history = false;
|
||||
void quill_terminate_handler();
|
||||
|
||||
std::vector<double> linspace(const double start, const double end, const size_t num) {
|
||||
std::vector<double> result;
|
||||
if (num == 0) return result;
|
||||
if (num == 1) {
|
||||
result.push_back(start);
|
||||
return result;
|
||||
}
|
||||
const double step = (end - start) / static_cast<double>(num - 1);
|
||||
for (size_t i = 0; i < num; ++i) {
|
||||
result.push_back(start + i * step);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<gridfire::NetIn> init(const double tMin, const double tMax, const double rhoMin, const double rhoMax, const double nShells, const double evolveTime) {
|
||||
std::setlocale(LC_ALL, "");
|
||||
g_previousHandler = std::set_terminate(quill_terminate_handler);
|
||||
quill::Logger* logger = fourdst::logging::LogManager::getInstance().getLogger("log");
|
||||
logger->set_log_level(quill::LogLevel::TraceL2);
|
||||
LOG_INFO(logger, "Initializing GridFire Spectral Solver Sandbox...");
|
||||
|
||||
using namespace gridfire;
|
||||
const std::vector<double> X = {0.7081145999999999, 2.94e-5, 0.276, 0.003, 0.0011, 9.62e-3, 1.62e-3, 5.16e-4};
|
||||
const std::vector<std::string> symbols = {"H-1", "He-3", "He-4", "C-12", "N-14", "O-16", "Ne-20", "Mg-24"};
|
||||
|
||||
|
||||
const fourdst::composition::Composition composition = fourdst::composition::buildCompositionFromMassFractions(symbols, X);
|
||||
|
||||
std::vector<NetIn> netIns;
|
||||
for (const auto& [T, ρ]: std::views::zip(linspace(tMin, tMax, nShells), linspace(rhoMax, rhoMin, nShells))) {
|
||||
NetIn netIn;
|
||||
netIn.composition = composition;
|
||||
netIn.temperature = T;
|
||||
netIn.density = ρ;
|
||||
netIn.energy = 0;
|
||||
|
||||
netIn.tMax = evolveTime;
|
||||
netIn.dt0 = 1e-12;
|
||||
netIns.push_back(netIn);
|
||||
}
|
||||
|
||||
return netIns;
|
||||
}
|
||||
void quill_terminate_handler()
|
||||
{
|
||||
quill::Backend::stop();
|
||||
if (g_previousHandler)
|
||||
g_previousHandler();
|
||||
else
|
||||
std::abort();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
using namespace gridfire;
|
||||
|
||||
CLI::App app{"GridFire Sandbox Application."};
|
||||
|
||||
double tMin = 1.5e7;
|
||||
double tMax = 1.7e7;
|
||||
double rhoMin = 1.5e2;
|
||||
double rhoMax = 1.5e2;
|
||||
double nShells = 15;
|
||||
double evolveTime = 3.1536e+16;
|
||||
|
||||
app.add_option("--tMin", tMin, "Minimum time in seconds");
|
||||
app.add_option("--tMax", tMax, "Maximum time in seconds");
|
||||
app.add_option("--rhoMin", rhoMin, "Minimum density in g/cm^3");
|
||||
app.add_option("--rhoMax", rhoMax, "Maximum density in g/cm^3");
|
||||
app.add_option("--nShells", nShells, "Number of shells");
|
||||
app.add_option("--evolveTime", evolveTime, "Maximum time in seconds");
|
||||
|
||||
CLI11_PARSE(app, argc, argv);
|
||||
|
||||
const std::vector<NetIn> netIns = init(tMin, tMax, rhoMin, rhoMax, nShells, evolveTime);
|
||||
|
||||
policy::MainSequencePolicy stellarPolicy(netIns[0].composition);
|
||||
stellarPolicy.construct();
|
||||
policy::ConstructionResults construct = stellarPolicy.construct();
|
||||
|
||||
solver::SpectralSolverStrategy solver(construct.engine);
|
||||
std::vector<double> mass_coords = linspace(1e-5, 1.0, nShells);
|
||||
|
||||
std::vector<NetOut> results = solver.evaluate(netIns, mass_coords, *construct.scratch_blob);
|
||||
}
|
||||
Reference in New Issue
Block a user