71{
72 if (!hvp) {
73 throw std::invalid_argument(
74 "SketchedHessianH0::rebuild: HvP functor is null");
75 }
76
77 int rank = 0;
78 int nprocs = 1;
79#ifdef GPR_HAVE_MPI
80 int mpi_up = 0;
81 MPI_Initialized(&mpi_up);
82 if (mpi_up) {
83 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
84 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
85 }
86#endif
87
88
89
90
91
92 Eigen::MatrixXd Y_local = Eigen::MatrixXd::Zero(n_dof_, sketch_rank_);
93 for (int j = 0; j < sketch_rank_; ++j) {
94 if (j % nprocs == rank) {
95 Y_local.col(j) = hvp(Omega_.col(j));
96 }
97 }
98
99#ifdef GPR_HAVE_MPI
100 if (mpi_up && nprocs > 1) {
101
102
103
104
105 Y_.resize(n_dof_, sketch_rank_);
106 MPI_Allreduce(Y_local.data(), Y_.data(),
107 static_cast<int>(Y_local.size()), MPI_DOUBLE, MPI_SUM,
108 MPI_COMM_WORLD);
109 } else
110#endif
111 {
112 Y_ = std::move(Y_local);
113 }
114
115
116
117
118
119
120 Eigen::MatrixXd mid = Omega_.transpose() * Y_;
121 mid = (0.5 * (mid + mid.transpose())).eval();
122 mid.diagonal().array() += tikhonov_eps_;
123 mid_ldlt_.compute(mid);
124 if (mid_ldlt_.info() != Eigen::Success) {
125 throw std::runtime_error(
126 "SketchedHessianH0::rebuild: LDLT of Nystrom mid-matrix failed");
127 }
128 ready_ = true;
129}