221 for (
int i = 0; i < n; ++i)
222 for (
int j = 0; j < n; ++j)
223 v[i][j] = (i == j) ? 1.0 : 0.0;
224 for (
int sweep = 0; sweep < 50; ++sweep) {
225 double off = 0.0, all = 0.0;
226 for (
int i = 0; i < n; ++i)
227 for (
int j = 0; j < n; ++j) {
228 all += a[i][j] * a[i][j];
230 off += a[i][j] * a[i][j];
232 if (std::sqrt(off) <= 1e-15 * std::sqrt(all))
234 for (
int p = 0; p < n; ++p)
235 for (
int q = p + 1; q < n; ++q) {
238 const double theta = (a[q][q] - a[p][p]) / (2.0 * a[p][q]);
240 (theta >= 0.0 ? 1.0 : -1.0) / (std::abs(theta) + std::sqrt(theta * theta + 1.0));
241 const double c = 1.0 / std::sqrt(t * t + 1.0), s = t * c;
242 for (
int k = 0; k < n; ++k) {
243 const double akp = a[k][p], akq = a[k][q];
244 a[k][p] = c * akp - s * akq;
245 a[k][q] = s * akp + c * akq;
247 for (
int k = 0; k < n; ++k) {
248 const double apk = a[p][k], aqk = a[q][k];
249 a[p][k] = c * apk - s * aqk;
250 a[q][k] = s * apk + c * aqk;
252 for (
int k = 0; k < n; ++k) {
253 const double vkp = v[k][p], vkq = v[k][q];
254 v[k][p] = c * vkp - s * vkq;
255 v[k][q] = s * vkp + c * vkq;
259 for (
int i = 0; i < n; ++i)
351 double failed[2] = {0.0, 0.0};
355 }
catch (
const std::invalid_argument& e) {
359 const std::size_t ns = st_.fields.size();
360 const Index nPad = invalid.empty() ? numPadded() : 0;
361 if (invalid.empty()) {
363 auto alloc = [&](std::vector<View<double>>& set, const char* label) {
365 for (std::size_t f = 0; f < ns; ++f)
366 set[f] = View<double>(
367 Kokkos::view_alloc(std::string(
"anderson::") + label, Kokkos::WithoutInitializing),
371 alloc(rPrev_,
"Rprev");
372 alloc(gPrev_,
"Gprev");
373 for (int s = 0; s < m_; ++s) {
377 allocReductionScratch();
378 } catch (
const std::exception&) {
384 throw std::invalid_argument(invalid.empty()
385 ? std::string(
"AndersonCore: the state descriptor is "
386 "inconsistent on another rank")
388 if (failed[1] > 0.0) {
390 std::snprintf(msg,
sizeof msg,
391 "AndersonCore: window %d needs %zu bytes ((2m+3)*n_s*8*n_pad, n_s = %zu, "
392 "n_pad = %lld per rank); window 2 needs %zu; run on more ranks or without "
394 m_,
memoryBytesFor(m_, ns, nPad), ns,
static_cast<long long>(nPad),
396 throw std::runtime_error(msg);
407 return static_cast<std::size_t
>(2 *
window + 3) * numFields *
sizeof(
double) *
408 static_cast<std::size_t
>(nPad);
419 throw std::logic_error(
"AndersonCore::prepare: called twice without complete()");
425 const std::size_t ns = st_.fields.size();
426 for (std::size_t f = 0; f < ns; ++f)
428 Kokkos::subview(countDev_, f));
429 Kokkos::deep_copy(
ExecSpace(), countHost_, countDev_);
431 double changed = 0.0;
432 for (std::size_t f = 0; f < ns; ++f)
433 changed +=
static_cast<double>(countHost_(f));
440 for (std::size_t f = 0; f < st_.fields.size(); ++f) {
443 for (
int k = 0; k < mk_; ++k) {
444 cols.
dG[k] = dG_[order_[k]][f];
445 cols.
dR[k] = dR_[order_[k]][f];
446 cols.
gamma[k] = gamma_[k];
451 for (std::size_t f = 0; f < st_.fields.size(); ++f)
452 Kokkos::deep_copy(
ExecSpace(), x_[f], st_.fields[f]);
467 reasonCode_ = Reason::StepFailed;
468 reason_ =
"step failed at an accelerated iterate: " + what;
470 if (st_.comm.rank == 0 && !noticed_) {
471 std::fprintf(stderr,
"AndersonCore: %s; acceleration disabled\n", reason_.c_str());
482 throw std::logic_error(
"AndersonCore::complete: called without prepare()");
491 provisionalColumn(s, cols, nCols);
494 const std::vector<double> packet = reduceSums(s, cols, nCols);
497 const double xx = packet[0], uu = packet[1];
498 const double rho = std::sqrt(xx);
499 const double residualBefore = residual_, rhoMinBefore = rhoMin_;
503 residual_ = std::numeric_limits<double>::infinity();
505 residual_ = std::sqrt(xx / uu);
506 if (!std::isfinite(rho) || (mixed_ && pressureSolveFailed)) {
511 acceptColumn(packet, s, cols, nCols);
512 restartIfGrown(rho, residualBefore);
513 decCount_ = (rho < rhoPrev_) ? decCount_ + 1 : 0;
517 rhoMin_ = restarted_ ? rhoMinBefore : std::min(rhoMin_, rho);
521 broadcastDecisions();
533 pending_ = engaged_ =
false;
535 rhoMin_ = rhoPrev_ = std::numeric_limits<double>::infinity();
550 reasonCode_ = Reason::DisabledByCaller;
551 reason_ = reasonText(reasonCode_);
561 const std::string&
reason()
const {
return reason_; }
575 std::vector<double>
gamma()
const {
return std::vector<double>(gamma_, gamma_ + mk_); }
579 auto add = [&b](
const std::vector<View<double>>& set) {
580 for (
const auto& v : set)
581 b += v.span() *
sizeof(double);
586 for (
int s = 0; s < m_; ++s) {
595 enum class Reason :
int {
606 struct DecisionPacket {
613 int order[kMaxWindow];
614 double gamma[kMaxWindow];
618 static std::string reasonText(Reason r) {
622 case Reason::StepFailed:
623 return "step failed at an accelerated iterate";
624 case Reason::NonFiniteMixed:
625 return "non-finite residual / failed pressure solve at an accelerated iterate";
626 case Reason::NonFinitePlain:
627 return "non-finite residual on a plain step";
628 case Reason::TooManyRestarts:
629 return "too many restarts";
630 case Reason::DisabledByCaller:
631 return "disabled by the caller";
637 const std::size_t ns = st_.fields.size();
639 throw std::invalid_argument(
"AndersonCore: the state has no fields");
640 if (st_.roles.size() != ns)
641 throw std::invalid_argument(
"AndersonCore: one role per state field is required");
642 if (m_ < 1 || m_ > kMaxWindow)
643 throw std::invalid_argument(
"AndersonCore: window must be in [1, 8]");
644 if (!(beta_ > 0.0 && beta_ <= 1.0))
645 throw std::invalid_argument(
"AndersonCore: mixing must be in (0, 1]");
646 if (st_.ghost < 0 || st_.extent[0] <= 2 * st_.ghost || st_.extent[1] <= 2 * st_.ghost ||
647 st_.extent[2] <= 2 * st_.ghost)
648 throw std::invalid_argument(
"AndersonCore: the padded extent has no inner entries");
649 const Index nPad = numPadded();
650 for (std::size_t f = 0; f < ns; ++f) {
651 if (
static_cast<Index>(st_.fields[f].extent(0)) != nPad)
652 throw std::invalid_argument(
"AndersonCore: a state field is not the full padded box");
654 throw std::invalid_argument(
"AndersonCore: a role must be Velocity or Carried");
658 Index numPadded()
const {
return st_.extent[0] * st_.extent[1] * st_.extent[2]; }
660 void sumAll(
double* data,
int count)
const {
662 st_.comm.sumAll(data, count);
665 void restoreLastOutput() {
666 for (std::size_t f = 0; f < st_.fields.size(); ++f)
667 Kokkos::deep_copy(
ExecSpace(), st_.fields[f], gPrev_[f]);
672 void allocReductionScratch() {
677 countDev_ = Kokkos::View<Index*, MemSpace>(
"anderson::count", st_.fields.size());
678 selfDev_ = Kokkos::View<da::SumN<2>*,
MemSpace>(
"anderson::self_sums", nv);
679 colDev_ = Kokkos::View<da::SumN<2>*,
MemSpace>(
"anderson::column_sums", nv *
kMaxWindow);
680 countHost_ = Kokkos::create_mirror_view(countDev_);
681 selfHost_ = Kokkos::create_mirror_view(selfDev_);
682 colHost_ = Kokkos::create_mirror_view(colDev_);
685 void dropColumn(
int k) {
686 for (
int q = k; q + 1 < mk_; ++q)
687 order_[q] = order_[q + 1];
692 for (
int a = 0; a < mk_; ++a)
693 for (
int c = 0; c < mk_; ++c)
694 out[a][c] = full[order_[a]][order_[c]];
698 double condScaled()
const {
702 return se.lamMin / se.lamMax;
706 void solveTruncated() {
710 for (
int k = 0; k < mk_; ++k)
711 b[k] = b_[order_[k]];
720 void provisionalColumn(
int& s,
int (&cols)[kMaxWindow],
int& nCols) {
722 const std::size_t ns = st_.fields.size();
725 for (
int c = 0; c < m_ && s < 0; ++c)
726 if (std::find(order_, order_ + mk_, c) == order_ + mk_)
728 for (
int k = 0; k < mk_; ++k)
729 cols[nCols++] = order_[k];
732 for (
int k = 1; k < mk_; ++k)
733 cols[nCols++] = order_[k];
736 for (std::size_t f = 0; f < ns; ++f)
737 da::diff(st_.fields[f], x_[f], rPrev_[f], gPrev_[f], dR_[s][f], dG_[s][f]);
739 for (std::size_t f = 0; f < ns; ++f)
740 da::residual(st_.fields[f], x_[f]);
748 std::vector<double> reduceSums(
int s,
const int (&cols)[kMaxWindow],
int nCols) {
750 const std::size_t ns = st_.fields.size();
752 const int g = st_.ghost;
753 for (std::size_t f = 0, v = 0; f < ns; ++f) {
756 da::selfSums(x_[f], st_.fields[f], e, g, Kokkos::subview(selfDev_, v));
757 for (
int k = 0; k < nCols; ++k) {
758 const int j = cols[k];
759 da::columnSums(dR_[s][f], dR_[j][f], x_[f], e, g,
760 Kokkos::subview(colDev_, v * kMaxWindow + k));
764 Kokkos::deep_copy(
ExecSpace(), selfHost_, selfDev_);
766 Kokkos::deep_copy(
ExecSpace(), colHost_, colDev_);
768 std::vector<double> packet(
static_cast<std::size_t
>(2 * nCols + 2), 0.0);
769 for (std::size_t v = 0; v < selfHost_.extent(0); ++v) {
770 const da::SumN<2>& ss = selfHost_(v);
771 packet[0] += ss.v[0];
772 packet[1] += ss.v[1];
773 for (
int k = 0; k < nCols; ++k) {
774 const da::SumN<2>& cs = colHost_(v * kMaxWindow + k);
775 for (
int q = 0; q < 2; ++q)
776 packet[
static_cast<std::size_t
>(2 + 2 * k + q)] += cs.v[q];
779 sumAll(packet.data(),
static_cast<int>(packet.size()));
785 void disableNonFinite() {
788 reasonCode_ = Reason::NonFiniteMixed;
790 reasonCode_ = Reason::NonFinitePlain;
792 reason_ = reasonText(reasonCode_);
793 status_ = Status::Disabled;
799 void acceptColumn(
const std::vector<double>& packet,
int s,
const int (&cols)[kMaxWindow],
801 for (
int k = 0; k < nCols; ++k) {
802 const int j = cols[k];
803 const double* v = &packet[
static_cast<std::size_t
>(2 + 2 * k)];
804 rr_[s][j] = rr_[j][s] = v[0];
807 for (
int k = 0; k < nCols; ++k)
816 void restartIfGrown(
double rho,
double residualBefore) {
818 if (mixed_ && residual_ >= kNoiseFloor && rho > kRestartGrowth * rhoMin_) {
822 residual_ = residualBefore;
823 if (numRestarts_ >= kMaxRestarts) {
824 status_ = Status::Disabled;
825 reasonCode_ = Reason::TooManyRestarts;
826 reason_ = reasonText(reasonCode_);
832 void nextCoefficients() {
833 if (status_ == Status::Active && engaged_ && mk_ >= 1) {
836 for (
int k = 0; k < mk_;) {
837 if (rr_[order_[k]][order_[k]] > 0.0) {
843 while (mk_ > 1 && condScaled() < kCondMin)
853 void commitOutput() {
854 const std::size_t ns = st_.fields.size();
858 std::swap(rPrev_, x_);
859 for (std::size_t f = 0; f < ns; ++f)
860 Kokkos::deep_copy(
ExecSpace(), gPrev_[f], st_.fields[f]);
865 void broadcastDecisions() {
866 if (!st_.comm.broadcast)
869 p.status =
static_cast<int>(status_);
870 p.reason =
static_cast<int>(reasonCode_);
872 p.pending = pending_ ? 1 : 0;
873 p.restarted = restarted_ ? 1 : 0;
874 p.numRestarts = numRestarts_;
876 p.order[k] = k < mk_ ? order_[k] : -1;
877 p.gamma[k] = k < mk_ ? gamma_[k] : 0.0;
879 p.residual = residual_;
880 st_.comm.broadcast(&p,
sizeof p);
881 if (st_.comm.rank == 0)
883 const Reason oldReason = reasonCode_;
884 status_ =
static_cast<Status
>(p.status);
885 reasonCode_ =
static_cast<Reason
>(p.reason);
887 pending_ = p.pending != 0;
888 restarted_ = p.restarted != 0;
889 numRestarts_ = p.numRestarts;
891 order_[k] = p.order[k] >= 0 ? p.order[k] : 0;
892 gamma_[k] = p.gamma[k];
894 residual_ = p.residual;
895 if (reasonCode_ != oldReason)
896 reason_ = reasonText(reasonCode_);
904 std::vector<View<double>> x_, rPrev_, gPrev_;
907 Kokkos::View<Index*, MemSpace> countDev_;
908 Kokkos::View<detail::anderson::SumN<2>*,
MemSpace> selfDev_;
909 Kokkos::View<detail::anderson::SumN<2>*,
MemSpace> colDev_;
910 typename Kokkos::View<Index*, MemSpace>::host_mirror_type countHost_;
911 typename Kokkos::View<detail::anderson::SumN<2>*,
MemSpace>::host_mirror_type selfHost_;
912 typename Kokkos::View<detail::anderson::SumN<2>*,
MemSpace>::host_mirror_type colHost_;
920 bool havePrev_ =
false, pending_ =
false, engaged_ =
false;
921 bool prepared_ =
false, mixed_ =
false, noticed_ =
false;
922 bool restarted_ =
false;
923 int decCount_ = 0, numRestarts_ = 0, numResets_ = 0;
924 double rhoPrev_ = std::numeric_limits<double>::infinity();
925 double rhoMin_ = std::numeric_limits<double>::infinity();
926 Status status_ = Status::Active;
927 Reason reasonCode_ = Reason::None;
929 double residual_ = std::numeric_limits<double>::infinity();