perf(GridFire)

More preformance improvmnets

1. Switch to mimalloc which gave a roughly 10% improvment accross the
board
2. Use much faster compososition caching
3. Reusing work vector
This commit is contained in:
2025-12-07 12:34:12 -05:00
parent e48b62f231
commit 8cfa067ad0
23 changed files with 306 additions and 97 deletions

View File

@@ -144,6 +144,7 @@ namespace gridfire::engine {
* @param comp Composition object containing current abundances.
* @param T9 Temperature in units of 10^9 K.
* @param rho Density in g/cm^3.
* @param trust If true, indicates that the engine should trust the passed composition has already been collected.
* @return expected<StepDerivatives<double>> containing either dY/dt and energy generation rate or a stale engine
* error indicating that the engine must be updated
*
@@ -154,7 +155,8 @@ namespace gridfire::engine {
[[nodiscard]] virtual std::expected<StepDerivatives<double>, EngineStatus> calculateRHSAndEnergy(
const fourdst::composition::CompositionAbstract &comp,
double T9,
double rho
double rho,
bool trust
) const = 0;
};

View File

@@ -143,6 +143,7 @@ namespace gridfire::engine {
* @param comp Composition object containing current abundances.
* @param T9 Temperature in units of 10^9 K.
* @param rho Density in g/cm^3.
* @param trust
* @return StepDerivatives<double> containing dY/dt and energy generation rate.
*
* This method calculates the time derivatives of all species and the
@@ -153,7 +154,8 @@ namespace gridfire::engine {
[[nodiscard]] std::expected<StepDerivatives<double>, engine::EngineStatus> calculateRHSAndEnergy(
const fourdst::composition::CompositionAbstract &comp,
double T9,
double rho
double rho,
bool trust
) const override;
/**
@@ -883,6 +885,8 @@ namespace gridfire::engine {
mutable CppAD::sparse_jac_work m_jac_work; ///< Work object for sparse Jacobian calculations.
mutable std::vector<double> m_local_abundance_cache;
mutable std::unordered_map<size_t, StepDerivatives<double>> m_stepDerivativesCache;
mutable std::unordered_map<size_t, CppAD::sparse_rcv<std::vector<size_t>, std::vector<double>>> m_jacobianSubsetCache;
mutable std::unordered_map<size_t, CppAD::sparse_jac_work> m_jacWorkCache;
bool m_has_been_primed = false; ///< Flag indicating if the engine has been primed.
@@ -895,7 +899,7 @@ namespace gridfire::engine {
std::unique_ptr<screening::ScreeningModel> m_screeningModel = screening::selectScreeningModel(m_screeningType);
bool m_usePrecomputation = true; ///< Flag to enable or disable using precomputed reactions for efficiency. Mathematically, this should not change the results. Generally end users should not need to change this.
bool m_useReverseReactions = true; ///< Flag to enable or disable reverse reactions. If false, only forward reactions are considered.
bool m_useReverseReactions = false; ///< Flag to enable or disable reverse reactions. If false, only forward reactions are considered.
bool m_store_intermediate_reaction_contributions = false; ///< Flag to enable or disable storing intermediate reaction contributions for debugging.
BuildDepthType m_depth;

View File

@@ -92,6 +92,7 @@ namespace gridfire::engine {
* @param comp The current composition of the system.
* @param T9 The temperature in units of 10^9 K.
* @param rho The density in g/cm^3.
* @param trust
* @return A StepDerivatives struct containing the derivatives of the active species and the
* nuclear energy generation rate.
*
@@ -105,7 +106,8 @@ namespace gridfire::engine {
[[nodiscard]] std::expected<StepDerivatives<double>, engine::EngineStatus> calculateRHSAndEnergy(
const fourdst::composition::CompositionAbstract &comp,
double T9,
double rho
double rho,
bool trust
) const override;
@@ -406,6 +408,8 @@ namespace gridfire::engine {
/** @brief A flag indicating whether the view is stale and needs to be updated. */
bool m_isStale = true;
mutable std::unordered_map<size_t, fourdst::composition::Composition> m_collected_composition_cache;
private:
/**
* @brief A struct to hold a reaction and its flow rate.

View File

@@ -39,6 +39,7 @@ namespace gridfire::engine {
* @param comp A Composition object containing the current composition of the system
* @param T9 The temperature in units of 10^9 K.
* @param rho The density in g/cm^3.
* @param trust
* @return A StepDerivatives struct containing the derivatives of the active species and the
* nuclear energy generation rate.
*
@@ -47,7 +48,8 @@ namespace gridfire::engine {
[[nodiscard]] std::expected<StepDerivatives<double>, engine::EngineStatus> calculateRHSAndEnergy(
const fourdst::composition::CompositionAbstract &comp,
double T9,
double rho
double rho,
bool trust
) const override;
[[nodiscard]] EnergyDerivatives calculateEpsDerivatives(

View File

@@ -97,6 +97,7 @@ namespace gridfire::engine {
* @param comp The current composition.
* @param T9 Temperature in units of 10^9 K.
* @param rho Density in g/cm^3.
* @param trust
* @return A `std::expected` containing `StepDerivatives<double>` on success, or a
* `StaleEngineError` if the engine's QSE cache does not contain a solution
* for the given state.
@@ -121,7 +122,8 @@ namespace gridfire::engine {
[[nodiscard]] std::expected<StepDerivatives<double>, engine::EngineStatus> calculateRHSAndEnergy(
const fourdst::composition::CompositionAbstract &comp,
double T9,
double rho
double rho,
bool trust
) const override;
/**
@@ -586,6 +588,7 @@ namespace gridfire::engine {
* @param comp The input composition.
* @param T9 Temperature in units of 10^9 K.
* @param rho Density in g/cm^3.
* @param trust
* @return A new `Composition` object with algebraic species set to their equilibrium values.
*
* @par Purpose
@@ -598,7 +601,7 @@ namespace gridfire::engine {
* @pre The engine must have a valid QSE partition for the given state.
* @throws StaleEngineError If the QSE cache misses.
*/
fourdst::composition::Composition getNormalizedEquilibratedComposition(const fourdst::composition::CompositionAbstract& comp, double T9, double rho) const;
fourdst::composition::Composition getNormalizedEquilibratedComposition(const fourdst::composition::CompositionAbstract& comp, double T9, double rho, bool trust) const;
/**
* @brief Collect the composition from this and sub engines.

View File

@@ -637,6 +637,7 @@ namespace gridfire::reaction {
mutable std::optional<std::vector<fourdst::atomic::Species>> m_reactantsVec;
mutable std::optional<std::vector<fourdst::atomic::Species>> m_productsVec;
mutable std::optional<std::size_t> m_hashCache = std::nullopt; ///< Cached hash value for the reaction.
std::string m_sourceLabel; ///< Source label for the rate data (e.g., "wc12w", "st08").
RateCoefficientSet m_rateCoefficients; ///< The seven rate coefficients.
@@ -1006,6 +1007,7 @@ namespace gridfire::reaction {
std::string m_id;
std::unordered_map<std::string, size_t> m_reactionNameMap; ///< Maps reaction IDs to Reaction objects for quick lookup.
std::unordered_set<size_t> m_reactionHashes;
mutable std::optional<uint64_t> m_hashCache = std::nullopt;
};

View File

@@ -126,6 +126,7 @@ namespace gridfire::solver {
* @brief Call to evaluate which will let the user control if the trigger reasoning is displayed
* @param netIn Inputs: temperature [K], density [g cm^-3], tMax [s], composition.
* @param displayTrigger Boolean flag to control if trigger reasoning is displayed
* @param forceReinitialize Boolean flag to force reinitialization of CVODE resources at the start
* @return NetOut containing final Composition, accumulated energy [erg/g], step count,
* and dEps/dT, dEps/dRho.
* @throws std::runtime_error If any CVODE or SUNDIALS call fails (negative return codes),
@@ -133,7 +134,7 @@ namespace gridfire::solver {
* @throws exceptions::StaleEngineTrigger Propagated if the engine signals a stale state
* during RHS evaluation (captured in the wrapper then rethrown here).
*/
NetOut evaluate(const NetIn& netIn, bool displayTrigger);
NetOut evaluate(const NetIn& netIn, bool displayTrigger, bool forceReinitialize = false);
/**
* @brief Install a timestep callback.
@@ -324,5 +325,9 @@ namespace gridfire::solver {
std::optional<double> m_relTol; ///< User-specified relative tolerance.
bool m_detailed_step_logging = false; ///< If true, log detailed step diagnostics (error ratios, Jacobian, species balance).
mutable size_t m_last_size = 0;
mutable size_t m_last_composition_hash = 0ULL;
mutable sunrealtype m_last_good_time_step = 0ULL;
};
}

View File

@@ -71,20 +71,32 @@ namespace gridfire::utils {
return seed;
}
inline std::size_t fast_mix(std::size_t h) noexcept {
h ^= h >> 33;
h *= 0xff51afd7ed558ccdULL;
h ^= h >> 33;
h *= 0xc4ceb9fe1a85ec53ULL;
h ^= h >> 33;
return h;
}
inline std::size_t hash_state(
const fourdst::composition::CompositionAbstract& comp,
const double T9,
const double rho,
const reaction::ReactionSet& reactions
) noexcept {
constexpr std::size_t seed = 0;
std::size_t comp_hash = fourdst::composition::utils::CompositionHash::hash_exact(comp);
for (const auto& reaction : reactions) {
comp_hash = hash_combine(comp_hash, hash_reaction(*reaction));
}
std::size_t hash = hash_combine(seed, comp_hash);
hash = hash_combine(hash, std::bit_cast<std::size_t>(T9));
hash = hash_combine(hash, std::bit_cast<std::size_t>(rho));
std::size_t hash = comp.hash();
const std::size_t topology_hash = reactions.hash(0);
hash ^= topology_hash + 0x517cc1b727220a95 + (hash << 6) + (hash >> 2);
const std::uint64_t t9_bits = std::bit_cast<std::uint64_t>(T9);
const std::uint64_t rho_bits = std::bit_cast<std::uint64_t>(rho);
hash ^= fast_mix(t9_bits) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= fast_mix(rho_bits) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash;
}
}