Add frozen surface and scalar Schur preconditioner experiment

This commit is contained in:
2026-09-19 13:43:21 -04:00
parent 53faac1cd2
commit 2398118b34
4 changed files with 869 additions and 2 deletions

View File

@@ -14,6 +14,7 @@
#include <memory>
#include "plateau_coupled_coarse.hpp"
#include "plateau_surface_border_schur.hpp"
#include <stdexcept>
#include <string>
#include <type_traits>
@@ -31,7 +32,9 @@ enum class PreconditionerBenchmarkVariant {
gravity_schur,
stellar_ldu_gravity_schur,
coupled_shape_coarse,
gravity_schur_coarse
gravity_schur_coarse,
surface_border_schur,
gravity_schur_surface_border_schur
};
inline PreconditionerBenchmarkVariant ParsePreconditionerBenchmarkVariant(const std::string &name) {
@@ -44,6 +47,9 @@ inline PreconditionerBenchmarkVariant ParsePreconditionerBenchmarkVariant(const
if (name == "stellar-ldu-gravity-schur") return PreconditionerBenchmarkVariant::stellar_ldu_gravity_schur;
if (name == "coupled-shape-coarse") return PreconditionerBenchmarkVariant::coupled_shape_coarse;
if (name == "gravity-schur-coarse") return PreconditionerBenchmarkVariant::gravity_schur_coarse;
if (name == "surface-border-schur") return PreconditionerBenchmarkVariant::surface_border_schur;
if (name == "gravity-schur-surface-border-schur")
return PreconditionerBenchmarkVariant::gravity_schur_surface_border_schur;
throw std::invalid_argument("Unknown preconditioner benchmark variant: " + name);
}
@@ -271,6 +277,12 @@ void BenchmarkPreconditioners(State &state, const std::filesystem::path &output,
"true_check_jacobian_applications,krylov_other_seconds\n";
auto coarseReport = detail::Open(output / "preconditioner_coarse.csv");
coarseReport << "role,variant,stage,metric,value\n";
auto schurReport = detail::Open(output / "preconditioner_surface_border_schur.csv");
schurReport << "role,variant,stage,metric,value\n";
auto schurCoordinates = detail::Open(output / "preconditioner_surface_border_coordinates.csv");
schurCoordinates << "role,variant,coarse_index,residual_index,correction_index\n";
auto schurMatrices = detail::Open(output / "preconditioner_surface_border_matrices.csv");
schurMatrices << "role,variant,row,column,uneliminated,schur\n";
auto work = detail::Open(output / "preconditioner_work.csv");
work << "role,variant,stage,metric,count\n";
auto actions = detail::Open(output / "preconditioner_actions.csv");
@@ -285,7 +297,7 @@ void BenchmarkPreconditioners(State &state, const std::filesystem::path &output,
history << "role,variant,iteration,reported_norm,final\n";
const auto run = [&]<pc::StellarStructureFactorizationPolicy Policy, bool MaterialLDU = false,
bool RefinedGravity = false, bool UseCoarse = false>(
bool RefinedGravity = false, bool UseCoarse = false, bool UseSurfaceBorderSchur = false>(
const std::string &role, const std::string &variant) {
const auto candidateStart = detail::Clock::now();
auto physical = detail::Prepare<Policy, MaterialLDU, RefinedGravity>(state.Problem(), options);
@@ -319,8 +331,38 @@ void BenchmarkPreconditioners(State &state, const std::filesystem::path &output,
const double bindingSeconds = detail::Seconds(sectionStart);
using NormalizedOperation = std::remove_reference_t<decltype(*state.normalizedOperator)>;
using Coarse = PreparedCoupledShapeCoarse<NormalizedOperation, decltype(normalized)>;
using UnborderedStructure = NormalizedUnborderedStructureInverse<NormalizedOperation, decltype(physical)>;
using SurfaceBorderSchur = PreparedSurfaceBorderSchur<NormalizedOperation, UnborderedStructure>;
std::unique_ptr<Coarse> coarse;
std::unique_ptr<UnborderedStructure> unborderedStructure;
std::unique_ptr<SurfaceBorderSchur> surfaceBorderSchur;
static_assert(!(UseCoarse && UseSurfaceBorderSchur));
mfem::Solver *applicationInverse = &normalized;
const auto recordSurfaceBorderSchur = [&](const char *stage) {
if (!surfaceBorderSchur) return;
const auto &stats = surfaceBorderSchur->GetStatistics();
const auto metric = [&](const char *name, auto value) {
schurReport << role << ',' << variant << ',' << stage << ',' << name << ',' << value << '\n';
};
metric("status", static_cast<int>(stats.status));
metric("coarse_size", stats.coarseSize);
metric("reciprocal_condition", stats.reciprocalCondition);
metric("minimum_scaled_pivot", stats.minimumScaledPivot);
metric("setup_jacobian_applications", stats.setupOperatorApplications);
metric("setup_inverse_applications", stats.setupInverseApplications);
metric("setup_seconds", stats.lastSetupSeconds);
metric("setup_jacobian_seconds", stats.setupOperatorSeconds);
metric("setup_inverse_seconds", stats.setupInverseSeconds);
metric("persistent_vector_bytes", stats.persistentVectorBytes);
metric("applications", stats.applications);
metric("internal_jacobian_applications", stats.operatorApplications);
metric("internal_inverse_applications", stats.inverseApplications);
metric("internal_jacobian_seconds", stats.operatorSeconds);
metric("internal_inverse_seconds", stats.inverseSeconds);
metric("application_seconds", stats.applicationSeconds);
metric("failed_applications", stats.failedApplications);
schurReport.flush();
};
const auto recordCoarse = [&](const char *stage) {
if (!coarse) return;
const auto &stats = coarse->GetStatistics();
@@ -398,6 +440,34 @@ void BenchmarkPreconditioners(State &state, const std::filesystem::path &output,
throw std::runtime_error("The actual FEM coarse inverse failed its exact-subspace check.");
}
if constexpr (UseSurfaceBorderSchur) {
sectionStart = detail::Clock::now();
unborderedStructure = std::make_unique<UnborderedStructure>(*state.normalizedOperator, physical);
surfaceBorderSchur = std::make_unique<SurfaceBorderSchur>(
*state.normalizedOperator, *unborderedStructure, MakeSurfaceBorderSchurCoordinates(physical),
state.Problem().GetCommunicator());
const double extraConstructionSeconds = detail::Seconds(sectionStart);
constructionSeconds += extraConstructionSeconds;
const auto &coordinates = surfaceBorderSchur->GetCoordinates();
const auto &schur = surfaceBorderSchur->GetSchurMatrix();
const auto &uneliminated = surfaceBorderSchur->GetUneliminatedCoarseMatrix();
for (int row = 0; row < schur.Height(); ++row) {
schurCoordinates << role << ',' << variant << ',' << row << ','
<< coordinates.residualIndices[row] << ','
<< coordinates.correctionIndices[row] << '\n';
for (int column = 0; column < schur.Width(); ++column)
schurMatrices << role << ',' << variant << ',' << row << ',' << column << ','
<< uneliminated(row, column) << ',' << schur(row, column) << '\n';
}
schurCoordinates.flush();
schurMatrices.flush();
recordSurfaceBorderSchur("constructed");
recordWork("after_surface_border_setup");
if (!surfaceBorderSchur->IsCurrent())
throw std::runtime_error("The measured surface/scalar Schur matrix is singular or ill-conditioned; no fallback was applied.");
applicationInverse = surfaceBorderSchur.get();
}
mfem::Vector inverseAction(rhs.Size()), inverseJacobianAction(rhs.Size());
sectionStart = detail::Clock::now();
applicationInverse->Mult(rhs, inverseAction);
@@ -412,11 +482,23 @@ void BenchmarkPreconditioners(State &state, const std::filesystem::path &output,
if (!detail::Finite(inverseJacobianAction) || !std::isfinite(actionNorm))
throw std::runtime_error("The diagnostic inverse Jacobian image or its norm is nonfinite.");
inverseJacobianAction -= rhs;
if (surfaceBorderSchur) {
double squared = 0.0;
for (int row : surfaceBorderSchur->GetCoordinates().residualIndices)
squared += inverseJacobianAction(row) * inverseJacobianAction(row);
const double coarseRelative = std::sqrt(squared) / rhsNorm;
schurReport << role << ',' << variant
<< ",validation,coarse_equation_defect_relative_to_full_rhs," << coarseRelative << '\n';
schurReport.flush();
if (!std::isfinite(coarseRelative) || coarseRelative > 1.e-8)
throw std::runtime_error("The Schur inverse failed the authoritative-J coarse-equation check.");
}
mfem::Vector referenceDifference(inverseAction);
referenceDifference -= referenceCorrection;
const double referenceRelative = detail::Relative(referenceDifference.Norml2(),
std::max(inverseAction.Norml2(), referenceCorrection.Norml2()));
const bool requireEquivalent = std::same_as<Policy, pc::IndependentStellarSubsystems> && !MaterialLDU && !UseCoarse &&
!UseSurfaceBorderSchur &&
(!RefinedGravity || options.schurCorrectionSweeps == 0);
actions << role << ',' << variant << ',' << inverseAction.Norml2() << ',' << actionNorm << ','
<< inverseJacobianAction.Norml2() << ',' << inverseJacobianAction.Norml2() / rhsNorm << ','
@@ -447,6 +529,7 @@ void BenchmarkPreconditioners(State &state, const std::filesystem::path &output,
const double krylovSetupSeconds = detail::Seconds(sectionStart);
recordWork("before_krylov");
recordCoarse("before_krylov");
recordSurfaceBorderSchur("before_krylov");
countedJacobian.ResetStatistics();
countedInverse.ResetStatistics();
sectionStart = detail::Clock::now();
@@ -454,6 +537,7 @@ void BenchmarkPreconditioners(State &state, const std::filesystem::path &output,
const double krylovSeconds = detail::Seconds(sectionStart);
recordWork("after_krylov");
recordCoarse("after_krylov");
recordSurfaceBorderSchur("after_krylov");
const auto jacobianStats = countedJacobian.GetStatistics();
const auto inverseStats = countedInverse.GetStatistics();
const auto lifecycle = countedInverse.GetLifecycleStatistics();
@@ -537,6 +621,10 @@ void BenchmarkPreconditioners(State &state, const std::filesystem::path &output,
run.template operator()<pc::IndependentStellarSubsystems, false, false, true>("candidate", options.variant); break;
case PreconditionerBenchmarkVariant::gravity_schur_coarse:
run.template operator()<pc::IndependentStellarSubsystems, false, true, true>("candidate", options.variant); break;
case PreconditionerBenchmarkVariant::surface_border_schur:
run.template operator()<pc::IndependentStellarSubsystems, false, false, false, true>("candidate", options.variant); break;
case PreconditionerBenchmarkVariant::gravity_schur_surface_border_schur:
run.template operator()<pc::IndependentStellarSubsystems, false, true, false, true>("candidate", options.variant); break;
}
auto scope = detail::Open(output / "preconditioner_scope.txt");
scope << "Both solves use fresh physical inverses, separately completed scalar borders, the same physical J,\n"
@@ -549,6 +637,12 @@ void BenchmarkPreconditioners(State &state, const std::filesystem::path &output,
<< "unchanged_state_refresh_seconds measures an immediate Refresh; it is not a changed-state refresh benchmark.\n"
<< "Each candidate receives one diagnostic inverse/Jv application before the timed cold Krylov solve.\n"
<< "Application timers/counts in the summary include only Krylov work; the independent true check is separate.\n"
<< "Surface/scalar Schur setup is included in construction; its internal J applications are separately counted.\n"
<< "For that inverse, outer Krylov J counts exclude internal Schur J work; inverse seconds include it.\n"
<< "Schur status codes are 0=unprepared, 1=ready, 2=singular, 3=ill-conditioned; rejected setup has no fallback.\n"
<< "The Schur partition uses every native surface parameter and generated scalar block, including a central border if present.\n"
<< "Its field inverse restricts the unbordered structure inverse, not an exact field elimination.\n"
<< "Existing scalar-border construction is retained in setup cost but its inverse is not applied by the new wrapper.\n"
<< "setup_plus_solve_seconds includes construction, normalization binding, Krylov setup, and Krylov solve.\n"
<< "setup_solve_and_true_check_seconds adds the separately measured original-J true-residual gate.\n"
<< "measured_candidate_seconds also includes diagnostic refresh, coarse exactness checks, and inverse/Jv comparison.\n"