1#ifndef GPR_ML_PHASE_TIMERS_H_
2#define GPR_ML_PHASE_TIMERS_H_
27 using Clock = std::chrono::steady_clock;
28 using Now = std::function<Clock::time_point()>;
32 now_(now ?
std::move(now) :
Now{[] {
return Clock::now(); }}),
37 [[nodiscard]]
bool enabled() const noexcept
39 return seconds_ > 0.0 && std::isfinite(seconds_);
44 return std::chrono::duration<double>(now_() - start_).count();
52 [[nodiscard]]
double seconds() const noexcept
58 double seconds_ = 0.0;
60 Clock::time_point start_;
67 [[nodiscard]]
const char*
what() const noexcept
override
69 return "wall-clock limit reached";
82 const std::shared_ptr<const WallClockLimit>& limit,
85 if (!limit || !limit->enabled())
return false;
86 const bool reached = limit->reached();
87#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
89 const int local = reached ? 1 : 0;
91 if (MPI_Allreduce(&local, &global, 1, MPI_INT, MPI_MAX, context.
comm) !=
93 throw std::runtime_error(
"could not agree on wall-clock limit");
104 const std::shared_ptr<const WallClockLimit>& limit)
106 if (!limit || !limit->enabled())
return false;
107#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
108 int initialized = 0, finalized = 0;
109 MPI_Initialized(&initialized);
110 MPI_Finalized(&finalized);
111 if (initialized && !finalized)
113 linalg::MpiContext::world());
115 return limit->reached();
199 static inline std::atomic_size_t
reuses = 0;
227 "no constant kernel",
228 "non-Gaussian likelihood",
230 "stable-basis (HS-SVD) backend",
231 "residual correction",
233 "geodesic heat kernel",
234 "objective other than NLML",
235 "length scales not one per pair type",
239 static std::string
report(
const char* execution)
241 return std::string(
"Device posterior: execution ") + execution +
242 ", device fits " + std::to_string(
conditions.load()) +
244 ", appends " + std::to_string(
appends.load()) +
", reuses " +
245 std::to_string(
reuses.load()) +
"), host fallbacks " +
247 std::to_string(
gradients.load()) +
", predictions " +
248 std::to_string(
predictions.load()) +
", bytes to device " +
249 std::to_string(
input_bytes.load()) +
", bytes to host " +
250 std::to_string(
output_bytes.load()) +
", factor downloads " +
253 " bytes), coefficient downloads " +
265 if (count == 0)
continue;
266 text += text.empty() ?
"; fallback reasons: " :
", ";
267 text += std::string(
reason_names[r]) +
" " + std::to_string(count);
369 static void recordFit(
long evaluations,
bool unmoved)
noexcept
373 if (!unmoved)
return;
389 std::ifstream statm(
"/proc/self/statm");
390 long pages = 0, resident = 0;
391 if (!(statm >> pages >> resident))
return 0.0;
392 return static_cast<double>(resident) *
393 static_cast<double>(sysconf(_SC_PAGESIZE)) / (1024.0 * 1024.0);
554 static inline Totals run_{};
555 static Totals add(
const Totals& a,
const Totals& b)
noexcept
558 t.objective_seconds = a.objective_seconds + b.objective_seconds;
559 t.objective_count = a.objective_count + b.objective_count;
560 t.objective_rows = a.objective_rows + b.objective_rows;
561 t.objective_channels = a.objective_channels + b.objective_channels;
562 t.assembly_seconds = a.assembly_seconds + b.assembly_seconds;
563 t.gradient_seconds = a.gradient_seconds + b.gradient_seconds;
564 t.gradient_inverse_seconds =
565 a.gradient_inverse_seconds + b.gradient_inverse_seconds;
566 t.gradient_blocks_seconds =
567 a.gradient_blocks_seconds + b.gradient_blocks_seconds;
568 t.gradient_accumulate_seconds =
569 a.gradient_accumulate_seconds + b.gradient_accumulate_seconds;
570 t.gradient_count = a.gradient_count + b.gradient_count;
571 t.geometry_builds = a.geometry_builds + b.geometry_builds;
572 t.pair_contraction_tiles =
573 a.pair_contraction_tiles + b.pair_contraction_tiles;
574 t.chart_assembly_tiles =
575 a.chart_assembly_tiles + b.chart_assembly_tiles;
576 t.factorization_seconds =
577 a.factorization_seconds + b.factorization_seconds;
578 t.factorization_count = a.factorization_count + b.factorization_count;
579 t.factorization_rows = a.factorization_rows + b.factorization_rows;
580 t.cholesky_seconds = a.cholesky_seconds + b.cholesky_seconds;
581 t.eigensolve_seconds = a.eigensolve_seconds + b.eigensolve_seconds;
582 t.setup_seconds = a.setup_seconds + b.setup_seconds;
583 t.cache_seconds = a.cache_seconds + b.cache_seconds;
584 t.descriptor_seconds = a.descriptor_seconds + b.descriptor_seconds;
585 t.posterior_seconds = a.posterior_seconds + b.posterior_seconds;
586 t.curvature_seconds = a.curvature_seconds + b.curvature_seconds;
587 t.energy_seconds = a.energy_seconds + b.energy_seconds;
588 t.schedule_seconds = a.schedule_seconds + b.schedule_seconds;
589 t.predict_seconds = a.predict_seconds + b.predict_seconds;
590 t.predict_count = a.predict_count + b.predict_count;
591 t.variance_seconds = a.variance_seconds + b.variance_seconds;
592 t.variance_count = a.variance_count + b.variance_count;
593 t.residual_seconds = a.residual_seconds + b.residual_seconds;
594 t.correction_seconds = a.correction_seconds + b.correction_seconds;
595 t.pair_objective_seconds =
596 a.pair_objective_seconds + b.pair_objective_seconds;
597 t.pair_covariance_seconds =
598 a.pair_covariance_seconds + b.pair_covariance_seconds;
599 t.pair_contraction_seconds =
600 a.pair_contraction_seconds + b.pair_contraction_seconds;
601 t.pair_objective_count =
602 a.pair_objective_count + b.pair_objective_count;
603 t.pair_max_joint_rows =
604 std::max(a.pair_max_joint_rows, b.pair_max_joint_rows);
605 t.chart_objective_seconds =
606 a.chart_objective_seconds + b.chart_objective_seconds;
607 t.chart_objective_count =
608 a.chart_objective_count + b.chart_objective_count;
609 t.chart_max_joint_rows =
610 std::max(a.chart_max_joint_rows, b.chart_max_joint_rows);
611 t.support_seconds = a.support_seconds + b.support_seconds;
612 t.support_refit_seconds =
613 a.support_refit_seconds + b.support_refit_seconds;
614 t.support_expansions = a.support_expansions + b.support_expansions;
615 t.support_refit_count = a.support_refit_count + b.support_refit_count;
616 t.projection_assembly_seconds =
617 a.projection_assembly_seconds + b.projection_assembly_seconds;
618 t.projection_factor_seconds =
619 a.projection_factor_seconds + b.projection_factor_seconds;
620 t.projection_cross_seconds =
621 a.projection_cross_seconds + b.projection_cross_seconds;
622 t.projection_solve_seconds =
623 a.projection_solve_seconds + b.projection_solve_seconds;
624 t.projection_schur_seconds =
625 a.projection_schur_seconds + b.projection_schur_seconds;
626 t.projection_count = a.projection_count + b.projection_count;
627 t.projection_max_anchor_rows = std::max(a.projection_max_anchor_rows,
628 b.projection_max_anchor_rows);
629 t.condition_seconds = a.condition_seconds + b.condition_seconds;
630 t.subset_seconds = a.subset_seconds + b.subset_seconds;
631 t.oracle_seconds = a.oracle_seconds + b.oracle_seconds;
632 t.communication_seconds =
633 a.communication_seconds + b.communication_seconds;
634 t.band_inner_seconds = a.band_inner_seconds + b.band_inner_seconds;
635 t.band_refit_seconds = a.band_refit_seconds + b.band_refit_seconds;
636 t.band_path_seconds = a.band_path_seconds + b.band_path_seconds;
637 t.band_predict_seconds =
638 a.band_predict_seconds + b.band_predict_seconds;
639 t.band_predict_count = a.band_predict_count + b.band_predict_count;
640 t.band_certificate_seconds =
641 a.band_certificate_seconds + b.band_certificate_seconds;
642 t.band_drift_seconds = a.band_drift_seconds + b.band_drift_seconds;
657 const char* label =
nullptr) noexcept
658 : accumulator_(accumulator),
659 start_(
std::chrono::steady_clock::now()),
661 partition_(isPartition(accumulator))
663 if (partition_) snapshot();
667 double wall = std::chrono::duration<double>(
668 std::chrono::steady_clock::now() - start_)
670 if (partition_) wall -= children();
671 if (wall > 0.0) accumulator_ += wall;
673 label_ !=
nullptr ? label_
686 static double*
const* partitionTimers() noexcept
688 static double*
const timers[] = {
697 static constexpr int partitionCount() noexcept
701 static bool isPartition(
const double& accumulator)
noexcept
703 for (
int i = 0; i < partitionCount(); ++i)
704 if (partitionTimers()[i] == &accumulator)
return true;
707 void snapshot() noexcept
709 for (
int i = 0; i < partitionCount(); ++i)
710 snap_[i] = *partitionTimers()[i];
712 double children() const noexcept
718 for (
int i = 0; i < partitionCount(); ++i) {
719 const double grew = *partitionTimers()[i] - snap_[i];
720 if (grew > 0.0) nested += grew;
725 double& accumulator_;
726 std::chrono::steady_clock::time_point start_;
728 bool partition_ =
false;
729 double snap_[12] = {};
738 : outermost_(depth_++ == 0), start_(
std::chrono::steady_clock::now())
747 std::chrono::duration<double>(std::chrono::steady_clock::now() -
755 static inline int depth_ = 0;
756 bool outermost_ =
false;
757 std::chrono::steady_clock::time_point start_;
766 : outermost_(depth_++ == 0), start_(
std::chrono::steady_clock::now())
775 std::chrono::duration<double>(std::chrono::steady_clock::now() -
783 static inline int depth_ = 0;
784 bool outermost_ =
false;
785 std::chrono::steady_clock::time_point start_;
Tiny value type describing the MPI dispatch context for a probe loop.
ScopedPhaseTimer(double &accumulator, const char *label=nullptr) noexcept
ScopedPhaseTimer(const ScopedPhaseTimer &)=delete
ScopedPhaseTimer & operator=(const ScopedPhaseTimer &)=delete
ScopedPredictTimer(const ScopedPredictTimer &)=delete
ScopedPredictTimer() noexcept
ScopedPredictTimer & operator=(const ScopedPredictTimer &)=delete
ScopedVarianceTimer() noexcept
ScopedVarianceTimer(const ScopedVarianceTimer &)=delete
ScopedVarianceTimer & operator=(const ScopedVarianceTimer &)=delete
double elapsedSeconds() const
double seconds() const noexcept
bool enabled() const noexcept
WallClockLimit(double seconds, Now now={})
std::function< Clock::time_point()> Now
std::chrono::steady_clock Clock
const char * what() const noexcept override
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
void requireWallClock(const std::shared_ptr< const WallClockLimit > &limit)
bool collectiveWallClockLimitReached(const std::shared_ptr< const WallClockLimit > &limit, const linalg::MpiContext &context)
static std::atomic_size_t coefficient_downloads
static std::atomic_size_t input_bytes
static std::atomic_size_t factor_downloads
static std::atomic_size_t coefficient_download_bytes
static std::atomic_size_t factor_download_bytes
static std::atomic_size_t conditions
static std::atomic_size_t appends
static std::atomic_size_t reuses
static std::atomic_size_t host_fallbacks
static std::atomic_size_t output_bytes
static std::string report(const char *execution)
One line for the drivers' closing report.
static constexpr const char * reason_names[ReasonCount]
static std::atomic_size_t gradients
static std::atomic_size_t predictions
@ LengthScalesNotPerPairType
static std::atomic_size_t fallback_reasons[ReasonCount]
static std::atomic_size_t factorizations
static std::string fallbackReasons()
The properties behind the fallbacks, or nothing when there were none.
static double band_predict_seconds
static void resetRun() noexcept
static long chart_max_joint_rows
static double gradient_inverse_seconds
static int geometry_builds
static int support_expansions
static long fit_evaluations
static std::atomic_size_t chart_assembly_tiles
static void reset() noexcept
static double cholesky_seconds
static double band_refit_seconds
static long unmoved_fit_evaluations
static const char * rss_peak_phase
static long factorization_rows
static double projection_cross_seconds
static int pair_objective_count
static long unmoved_fit_count
static double band_inner_seconds
static double cache_seconds
Inverse-distance cache built once before the hyperparameter fit.
static void recordFitRestart() noexcept
static std::atomic_size_t pair_contraction_tiles
Configuration pairs contracted by this process, excluding transposes.
static long objective_channels
static double condition_seconds
static double projection_schur_seconds
static double setup_seconds
static void notePeak(const char *phase) noexcept
static double band_certificate_seconds
static double eigensolve_seconds
static double pair_contraction_seconds
static double oracle_seconds
static long projection_max_anchor_rows
static Totals runTotals() noexcept
Everything accumulated since the last resetRun(), across reset().
static double support_seconds
static double chart_objective_seconds
static int factorization_count
static double subset_seconds
static double objective_seconds
static double factorization_seconds
static Totals current() noexcept
static double support_refit_seconds
static double pair_objective_seconds
static double band_drift_seconds
static double energy_seconds
Objective energy outside factor, solve, eigen, kernel, and curvature.
static double residentMegabytes() noexcept
static long band_predict_count
static int projection_count
static double projection_factor_seconds
static int support_refit_count
static double projection_assembly_seconds
static double assembly_seconds
static double gradient_seconds
static int chart_objective_count
static double correction_seconds
static double descriptor_seconds
static void recordFit(long evaluations, bool unmoved) noexcept
static double rss_peak_mb
static double schedule_seconds
Optimizer outside the named numerical leaves.
static long pair_max_joint_rows
static double band_path_seconds
static double curvature_seconds
static double projection_solve_seconds
static double pair_covariance_seconds
static int gradient_count
static double residual_seconds
Residual-kernel candidate fits of the active correction.
static double predict_seconds
static double posterior_seconds
static int variance_count
static long objective_rows
static double variance_seconds
static double gradient_blocks_seconds
static double communication_seconds
static double gradient_accumulate_seconds
static int objective_count
std::size_t pair_contraction_tiles
double pair_contraction_seconds
double projection_cross_seconds
double pair_covariance_seconds
double gradient_blocks_seconds
long chart_max_joint_rows
double chart_objective_seconds
double communication_seconds
double eigensolve_seconds
double band_certificate_seconds
double gradient_inverse_seconds
double gradient_accumulate_seconds
double factorization_seconds
double band_refit_seconds
double projection_factor_seconds
double band_drift_seconds
int chart_objective_count
double projection_assembly_seconds
double projection_schur_seconds
double descriptor_seconds
long projection_max_anchor_rows
double band_inner_seconds
double band_predict_seconds
double support_refit_seconds
double pair_objective_seconds
double projection_solve_seconds
double correction_seconds
std::size_t chart_assembly_tiles
bool is_distributed() const noexcept