Loading...
Searching...
No Matches
gpr::linalg::KokkosFactor Class Reference

#include <gpr/linalg/KokkosBackend.h>

Public Member Functions

 KokkosFactor (const EigenMatrix &factor)
 KokkosFactor (const KokkosFactor &)=delete
KokkosFactor & operator= (const KokkosFactor &)=delete
Eigen::Index order () const noexcept
EigenMatrix forward (const EigenMatrix &rhs) const
EigenMatrix backward (const EigenMatrix &rhs) const
EigenMatrix solve (const EigenMatrix &rhs) const
Eigen::VectorXd squaredNorms (const EigenMatrix &rhs) const
EigenMatrix inverse () const
KokkosTransferStats transferStats () const
void resetTransferStats ()
double halfLogDeterminant () const
double lowerFrobeniusNorm () const
 ||L||_F over the lower triangle, reduced on the device.
double schurRelativeError () const

Friends

class KokkosMolecularPosterior

Detailed Description

Definition at line 106 of file KokkosBackend.h.

Constructor & Destructor Documentation

◆ KokkosFactor() [1/2]

gpr::linalg::KokkosFactor::KokkosFactor ( const EigenMatrix & factor)
explicit

Definition at line 653 of file KokkosBackend.cpp.

653 : order_(factor.rows())
654{
655 if (factor.rows() != factor.cols())
656 throw std::invalid_argument("Kokkos factor must be square");
657 detail::kokkosSolverExtent(static_cast<std::size_t>(order_));
658 auto& state = workspace_.ensureState();
659 std::lock_guard<std::mutex> lock(state.mutex);
660 state.rejectFinalized();
661 if (order_ == 0) return;
662 state.requireRuntime();
663 state.bindFactor(factor);
664 // An immutable owner does not need a host witness for later queries.
665 state.witness.resize(0, 0);
666}

◆ KokkosFactor() [2/2]

gpr::linalg::KokkosFactor::KokkosFactor ( const KokkosFactor & )
delete

Member Function Documentation

◆ backward()

EigenMatrix gpr::linalg::KokkosFactor::backward ( const EigenMatrix & rhs) const

Definition at line 828 of file KokkosBackend.cpp.

829{
830 auto& state = workspace_.ensureState();
831 std::lock_guard<std::mutex> lock(state.mutex);
832 if (!state.checkBoundSolve(rhs)) return EigenMatrix(order_, rhs.cols());
833 KernelWall wall(KernelWall::Kind::Solve);
834 state.uploadRhs(rhs);
835 detail::solve_lower(detail::exec_space{}, state.factor, state.work, true);
836 return state.download(state.work);
837}
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20

◆ forward()

EigenMatrix gpr::linalg::KokkosFactor::forward ( const EigenMatrix & rhs) const

Definition at line 817 of file KokkosBackend.cpp.

818{
819 auto& state = workspace_.ensureState();
820 std::lock_guard<std::mutex> lock(state.mutex);
821 if (!state.checkBoundSolve(rhs)) return EigenMatrix(order_, rhs.cols());
822 KernelWall wall(KernelWall::Kind::Solve);
823 state.uploadRhs(rhs);
824 detail::solve_lower(detail::exec_space{}, state.factor, state.work, false);
825 return state.download(state.work);
826}

◆ halfLogDeterminant()

double gpr::linalg::KokkosFactor::halfLogDeterminant ( ) const

Definition at line 682 of file KokkosBackend.cpp.

683{
684 auto& state = workspace_.ensureState();
685 std::lock_guard<std::mutex> lock(state.mutex);
686 state.rejectFinalized();
687 if (order_ <= 0) return 0.0;
689 // Same reduction as log_det_cholesky. The tiled device sum is a
690 // different association and does not have the Eigen factor's bits.
691 const auto factor = state.factor;
692 const Eigen::Index n = order_;
693 const Eigen::Index leading =
694 static_cast<Eigen::Index>(factor.stride(1));
695 Eigen::Map<const EigenMatrix, Eigen::Unaligned,
696 Eigen::OuterStride<Eigen::Dynamic>>
697 host(factor.data(), n, n,
698 Eigen::OuterStride<Eigen::Dynamic>(leading));
699 return host.diagonal().array().log().sum();
700 }
701 const auto factor = state.factor;
702 const int n = order_;
703 constexpr int kTile = 256;
704 const int n_tiles = (n + kTile - 1) / kTile;
705 detail::device_vector_t tiles(
706 Kokkos::view_alloc(Kokkos::WithoutInitializing,
707 "gpr resident logdet tiles"),
708 static_cast<std::size_t>(n_tiles));
709 detail::device_vector_t total_view(
710 Kokkos::view_alloc(Kokkos::WithoutInitializing,
711 "gpr resident logdet scalar"),
712 1);
713 Kokkos::parallel_for(
714 "gpr resident log determinant tiles",
715 Kokkos::RangePolicy<detail::exec_space>(0, n_tiles),
716 KOKKOS_LAMBDA(const int t) {
717 const int begin = t * kTile;
718 const int end = (begin + kTile < n) ? (begin + kTile) : n;
719 double sum = 0.0, corr = 0.0;
720 for (int row = begin; row < end; ++row) {
721 const double term = Kokkos::log(factor(row, row));
722 const double next = sum + term;
723 corr += Kokkos::abs(sum) >= Kokkos::abs(term)
724 ? (sum - next) + term
725 : (term - next) + sum;
726 sum = next;
727 }
728 tiles(t) = sum + corr;
729 });
730 Kokkos::parallel_for(
731 "gpr resident log determinant fold",
732 Kokkos::RangePolicy<detail::exec_space>(0, 1),
733 KOKKOS_LAMBDA(const int) {
734 double sum = 0.0, corr = 0.0;
735 for (int t = 0; t < n_tiles; ++t) {
736 const double term = tiles(t);
737 const double next = sum + term;
738 corr += Kokkos::abs(sum) >= Kokkos::abs(term)
739 ? (sum - next) + term
740 : (term - next) + sum;
741 sum = next;
742 }
743 total_view(0) = sum + corr;
744 });
745 double result = 0.0;
746 Kokkos::View<double, Kokkos::HostSpace,
747 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
748 host(&result);
749 detail::audited_deep_copy(host, Kokkos::subview(total_view, 0),
750 sizeof(double), detail::CopyRole::ToHost);
751 state.stats.result_download_bytes += sizeof(double);
752 return result;
753}
bool kokkosHostExecutionMemory() noexcept

◆ inverse()

EigenMatrix gpr::linalg::KokkosFactor::inverse ( ) const

Definition at line 868 of file KokkosBackend.cpp.

869{
870 auto& state = workspace_.ensureState();
871 std::lock_guard<std::mutex> lock(state.mutex);
872 state.rejectFinalized();
873 if (order_ == 0) return EigenMatrix(0, 0);
874 KernelWall wall(KernelWall::Kind::Solve);
875 const auto n = static_cast<std::size_t>(order_);
876 detail::reserve_view(state.work, n, n, "gpr factor inverse root");
877 detail::reserve_view(state.product, n, n, "gpr factor inverse");
878 detail::cholesky_inverse_on_device(state.factor, state.work, state.product);
879 return state.download(state.product);
880}

◆ lowerFrobeniusNorm()

double gpr::linalg::KokkosFactor::lowerFrobeniusNorm ( ) const

||L||_F over the lower triangle, reduced on the device.

Definition at line 755 of file KokkosBackend.cpp.

756{
757 auto& state = *workspace_.impl_;
758 std::lock_guard<std::mutex> lock(state.mutex);
759 state.rejectFinalized();
760 if (order_ <= 0) return 0.0;
761 const auto factor = state.factor;
762 const int n = order_;
763 double squares = 0.0;
764 Kokkos::parallel_reduce(
765 "gpr resident factor frobenius norm",
766 Kokkos::RangePolicy<detail::exec_space>(0, n),
767 KOKKOS_LAMBDA(const int row, double& sum) {
768 double partial = 0.0;
769 for (int column = 0; column <= row; ++column)
770 partial += factor(row, column) * factor(row, column);
771 sum += partial;
772 },
773 squares);
774 state.stats.result_download_bytes += sizeof(double);
775 return std::sqrt(squares);
776}

◆ operator=()

KokkosFactor & gpr::linalg::KokkosFactor::operator= ( const KokkosFactor & )
delete

◆ order()

Eigen::Index gpr::linalg::KokkosFactor::order ( ) const
inlinenodiscardnoexcept

Definition at line 112 of file KokkosBackend.h.

113 {
114 return order_;
115 }

◆ resetTransferStats()

void gpr::linalg::KokkosFactor::resetTransferStats ( )

Definition at line 887 of file KokkosBackend.cpp.

888{
889 workspace_.resetTransferStats();
890}

◆ schurRelativeError()

double gpr::linalg::KokkosFactor::schurRelativeError ( ) const

choleskySchurRelativeError of this factor. ||L||_F is reduced on the device. ||L^-1||_1 is upperInverseNorm1FromResidual around the computed inverse, the same residual bound as the dense path. A nonpositive order returns machine epsilon, matching choleskySchurRelativeError.

Definition at line 778 of file KokkosBackend.cpp.

779{
780 if (order_ <= 0) return std::numeric_limits<double>::epsilon();
781 const double frobenius = lowerFrobeniusNorm();
782 // Same residual bound as upperInverseNorm1. The column-sum lift alone
783 // leaves out the triangular-solve error.
784 const EigenMatrix identity = EigenMatrix::Identity(order_, order_);
785 const EigenMatrix inverse_columns = forward(identity);
786 const double infinity = std::numeric_limits<double>::infinity();
787 double inverse_norm = infinity;
788 if (inverse_columns.rows() == order_ && inverse_columns.cols() == order_ &&
789 inverse_columns.allFinite()) {
790 EigenMatrix factor(order_, order_);
791 {
792 auto& state = *workspace_.impl_;
793 std::lock_guard<std::mutex> lock(state.mutex);
794 state.rejectFinalized();
795 detail::to_eigen(state.factor, factor);
796 state.stats.result_download_bytes +=
797 detail::byte_count(factor.size());
798 }
799 factor.triangularView<Eigen::StrictlyUpper>().setZero();
800 if (factor.allFinite()) {
801 const double computed =
802 inverse_columns.cwiseAbs().colwise().sum().maxCoeff();
803 const EigenMatrix residual = identity - factor * inverse_columns;
804 const double residual_norm =
805 residual.allFinite()
806 ? residual.cwiseAbs().colwise().sum().maxCoeff()
807 : infinity;
808 const double factor_norm =
809 factor.cwiseAbs().colwise().sum().maxCoeff();
810 inverse_norm = upperInverseNorm1FromResidual(
811 computed, residual_norm, factor_norm, order_);
812 }
813 }
814 return choleskySchurRelativeError(order_, frobenius, inverse_norm);
815}
EigenMatrix forward(const EigenMatrix &rhs) const
double lowerFrobeniusNorm() const
||L||_F over the lower triangle, reduced on the device.
double choleskySchurRelativeError(Eigen::Index order, double factor_frobenius_norm, double inverse_norm1) noexcept
Relative forward error of a Schur complement computed from a rounded Cholesky factor and triangular s...
Definition ExactSchur.h:259
double upperInverseNorm1FromResidual(double computed_norm, double residual_norm, double factor_abs_norm, Eigen::Index order) noexcept
Upper bound on ||L^-1||_1 from a computed triangular solve.
Definition ExactSchur.h:310

◆ solve()

EigenMatrix gpr::linalg::KokkosFactor::solve ( const EigenMatrix & rhs) const

Definition at line 839 of file KokkosBackend.cpp.

840{
841 auto& state = workspace_.ensureState();
842 std::lock_guard<std::mutex> lock(state.mutex);
843 if (!state.checkBoundSolve(rhs)) return EigenMatrix(order_, rhs.cols());
844 KernelWall wall(KernelWall::Kind::Solve);
845 state.uploadRhs(rhs);
846 detail::solve_lower(detail::exec_space{}, state.factor, state.work, false);
847 detail::solve_lower(detail::exec_space{}, state.factor, state.work, true);
848 return state.download(state.work);
849}

◆ squaredNorms()

Eigen::VectorXd gpr::linalg::KokkosFactor::squaredNorms ( const EigenMatrix & rhs) const

Definition at line 851 of file KokkosBackend.cpp.

852{
853 auto& state = workspace_.ensureState();
854 std::lock_guard<std::mutex> lock(state.mutex);
855 if (!state.checkBoundSolve(rhs)) return Eigen::VectorXd::Zero(rhs.cols());
856 KernelWall wall(KernelWall::Kind::Solve);
857 state.uploadRhs(rhs);
858 detail::solve_lower(detail::exec_space{}, state.factor, state.work, false);
859 detail::reserve_view(state.norms, static_cast<std::size_t>(rhs.cols()),
860 "gpr factor norms");
861 detail::column_squared_norms(state.work, state.norms);
862 Eigen::VectorXd result;
863 detail::to_eigen(state.norms, result);
864 state.stats.result_download_bytes += detail::byte_count(result.size());
865 return result;
866}

◆ transferStats()

KokkosTransferStats gpr::linalg::KokkosFactor::transferStats ( ) const
nodiscard

Definition at line 882 of file KokkosBackend.cpp.

883{
884 return workspace_.transferStats();
885}

◆ KokkosMolecularPosterior

friend class KokkosMolecularPosterior
friend

Definition at line 134 of file KokkosBackend.h.


The documentation for this class was generated from the following files: