75 n_ =
static_cast<Index
>(A.invVol.size());
77 S_ = core::solver::HostCsrOp{};
81 S_.coef.assign(A.coef.size(), 0.0);
82 S_.diag.assign(
static_cast<std::size_t
>(n_), 0.0);
83 identity_.assign(
static_cast<std::size_t
>(n_), 0);
85 for (Index i = 0; i < n_; ++i) {
87 for (Index k = A.start[
static_cast<std::size_t
>(i)];
88 k < A.start[
static_cast<std::size_t
>(i) + 1]; ++k) {
89 S_.coef[
static_cast<std::size_t
>(k)] = -A.coef[
static_cast<std::size_t
>(k)];
90 s += A.coef[
static_cast<std::size_t
>(k)];
92 S_.diag[
static_cast<std::size_t
>(i)] = s + A.bcDiag[
static_cast<std::size_t
>(i)];
93 if (S_.diag[
static_cast<std::size_t
>(i)] <= 0.0) {
97 identity_[
static_cast<std::size_t
>(i)] = 1;
98 S_.diag[
static_cast<std::size_t
>(i)] = 1.0;
103 const core::solver::AmgParams prm;
105 dIn_ = View<double>(
"amgb_in",
static_cast<std::size_t
>(n_));
106 dOut_ = View<double>(
"amgb_out",
static_cast<std::size_t
>(n_));
108 amgD_.build(S_, prm);
113 r_.assign(
static_cast<std::size_t
>(n_), 0.0);
114 z_.assign(
static_cast<std::size_t
>(n_), 0.0);
115 p_.assign(
static_cast<std::size_t
>(n_), 0.0);
116 q_.assign(
static_cast<std::size_t
>(n_), 0.0);
117 rhs_.assign(
static_cast<std::size_t
>(n_), 0.0);
118 bHost_.assign(
static_cast<std::size_t
>(n_), 0.0);
119 xHost_.assign(
static_cast<std::size_t
>(n_), 0.0);
120 bMirror_ = Kokkos::View<double*, Kokkos::HostSpace>(
"amgb_b",
static_cast<std::size_t
>(n_));
121 xMirror_ = Kokkos::View<double*, Kokkos::HostSpace>(
"amgb_x",
static_cast<std::size_t
>(n_));
126 bool ready()
const {
return n_ > 0; }
130 Index
size()
const {
return n_; }
146 void solve(View<const double> b, View<double> x)
const {
148 Kokkos::parallel_for(
"amr::amgb_in", n_, KOKKOS_LAMBDA(
const Index i) { in(i) = b(i); });
154 Kokkos::parallel_for(
"amr::amgb_out", n_, KOKKOS_LAMBDA(
const Index i) { x(i) = out(i); });
159 void solveHost()
const {
160 Kokkos::deep_copy(bMirror_, dIn_);
161 for (Index i = 0; i < n_; ++i)
162 bHost_[
static_cast<std::size_t
>(i)] = bMirror_(i);
163 std::fill(xHost_.begin(), xHost_.end(), 0.0);
164 for (Index i = 0; i < n_; ++i)
165 rhs_[
static_cast<std::size_t
>(i)] =
166 identity_[
static_cast<std::size_t
>(i)]
168 : -bHost_[static_cast<std::size_t>(i)] / invVol_[static_cast<std::size_t>(i)];
172 for (Index i = 0; i < n_; ++i)
173 bnorm = std::max(bnorm, std::fabs(rhs_[
static_cast<std::size_t
>(i)]));
177 const double rnorm0 = norm2(r_);
178 double rz = 0.0, rzOld = 0.0;
179 for (
int it = 0; it < kMaxIters; ++it) {
186 const double beta = rz / rzOld;
187 for (Index i = 0; i < n_; ++i)
188 p_[
static_cast<std::size_t
>(i)] =
189 z_[
static_cast<std::size_t
>(i)] + beta * p_[
static_cast<std::size_t
>(i)];
192 const double pq = dot(p_, q_);
193 if (!(std::fabs(pq) > 0.0))
195 const double alpha = rz / pq;
196 for (Index i = 0; i < n_; ++i) {
197 xHost_[
static_cast<std::size_t
>(i)] += alpha * p_[
static_cast<std::size_t
>(i)];
198 r_[
static_cast<std::size_t
>(i)] -= alpha * q_[
static_cast<std::size_t
>(i)];
202 if (norm2(r_) <= kTol * rnorm0)
206 for (Index i = 0; i < n_; ++i)
207 if (identity_[
static_cast<std::size_t
>(i)])
208 xHost_[
static_cast<std::size_t
>(i)] = 0.0;
209 S_.apply(xHost_, q_);
211 for (Index i = 0; i < n_; ++i)
213 e, std::fabs(rhs_[
static_cast<std::size_t
>(i)] - q_[
static_cast<std::size_t
>(i)]));
214 lastRel_ = e / bnorm;
216 for (Index i = 0; i < n_; ++i)
217 xMirror_(i) = xHost_[
static_cast<std::size_t
>(i)];
218 Kokkos::deep_copy(dOut_, xMirror_);
221 static double dot(
const std::vector<double>& a,
const std::vector<double>& b) {
223 for (std::size_t i = 0; i < a.size(); ++i)
227 static double norm2(
const std::vector<double>& a) {
return std::sqrt(dot(a, a)); }
231 void project(std::vector<double>& v)
const {
232 if (!singular_ || nComp_ == 0)
234 std::vector<double> sum(
static_cast<std::size_t
>(nComp_), 0.0);
235 for (Index i = 0; i < n_; ++i) {
236 const Index c = comp_[
static_cast<std::size_t
>(i)];
238 sum[
static_cast<std::size_t
>(c)] += v[
static_cast<std::size_t
>(i)];
240 for (
int c = 0; c < nComp_; ++c)
241 sum[
static_cast<std::size_t
>(c)] /=
242 static_cast<double>(compSize_[
static_cast<std::size_t
>(c)]);
243 for (Index i = 0; i < n_; ++i) {
244 const Index c = comp_[
static_cast<std::size_t
>(i)];
246 v[
static_cast<std::size_t
>(i)] -= sum[
static_cast<std::size_t
>(c)];
251 void uploadDevice(
const typename Poisson::FvAssembled& A) {
252 dDiag_ = toDevice(S_.diag,
"amgb_diag");
253 dStart_ = toDevice(S_.start,
"amgb_start");
254 dNbr_ = toDevice(S_.nbr,
"amgb_nbr");
255 dCoef_ = toDevice(S_.coef,
"amgb_coef");
256 dInvVol_ = toDevice(A.invVol,
"amgb_invvol");
257 dComp_ = toDevice(comp_,
"amgb_comp");
258 std::vector<double> ident(
static_cast<std::size_t
>(n_), 0.0);
259 for (Index i = 0; i < n_; ++i)
260 ident[
static_cast<std::size_t
>(i)] = identity_[
static_cast<std::size_t
>(i)] ? 1.0 : 0.0;
261 dIdent_ = toDevice(ident,
"amgb_ident");
262 const std::size_t nn =
static_cast<std::size_t
>(n_);
263 dR_ = View<double>(
"amgb_r", nn);
264 dZ_ = View<double>(
"amgb_z", nn);
265 dP_ = View<double>(
"amgb_p", nn);
266 dQ_ = View<double>(
"amgb_q", nn);
267 dRhs_ = View<double>(
"amgb_rhs", nn);
276 if (!singular_ || nComp_ == 0)
279 for (
int c = 0; c < nComp_; ++c) {
281 Kokkos::parallel_reduce(
282 "amr::amgb_proj_sum", n_,
283 KOKKOS_LAMBDA(
const Index i,
double& acc) {
288 const double m = s /
static_cast<double>(compSize_[
static_cast<std::size_t
>(c)]);
289 Kokkos::parallel_for(
290 "amr::amgb_proj_sub", n_, KOKKOS_LAMBDA(
const Index i) {
297 void applyS(View<const double> in, View<double> out)
const {
299 auto start = dStart_;
302 Kokkos::parallel_for(
303 "amr::amgb_spmv", n_, KOKKOS_LAMBDA(
const Index i) {
304 double s = diag(i) * in(i);
305 for (Index k = start(i); k < start(i + 1); ++k)
306 s += coef(k) * in(nbr(k));
311 static double dotDevice(Index n, View<const double> a, View<const double> b) {
313 Kokkos::parallel_reduce(
314 "amr::amgb_dot", n, KOKKOS_LAMBDA(
const Index i,
double& acc) { acc += a(i) * b(i); }, s);
317 static double maxAbs(Index n, View<const double> a) {
319 Kokkos::parallel_reduce(
320 "amr::amgb_maxabs", n,
321 KOKKOS_LAMBDA(
const Index i,
double& acc) {
322 const double v = a(i) < 0.0 ? -a(i) : a(i);
326 Kokkos::Max<double>(m));
332 auto invVol = dInvVol_;
333 auto ident = dIdent_;
335 View<double> x = dOut_;
336 Kokkos::parallel_for(
338 KOKKOS_LAMBDA(
const Index i) { rhs(i) = (ident(i) != 0.0) ? 0.0 : -b(i) / invVol(i); });
340 Kokkos::deep_copy(x, 0.0);
341 Kokkos::deep_copy(dR_, dRhs_);
342 const double bnorm =
maxAbs(n_, View<const double>(dRhs_));
346 const double rnorm0 =
347 std::sqrt(
dotDevice(n_, View<const double>(dR_), View<const double>(dR_)));
348 double rz = 0.0, rzOld = 0.0;
349 for (
int it = 0; it < kMaxIters; ++it) {
350 amgD_.apply(dR_, dZ_);
352 rz =
dotDevice(n_, View<const double>(dR_), View<const double>(dZ_));
354 Kokkos::deep_copy(dP_, dZ_);
356 const double beta = rz / rzOld;
359 Kokkos::parallel_for(
360 "amr::amgb_pupd", n_, KOKKOS_LAMBDA(
const Index i) { p(i) = z(i) + beta * p(i); });
362 applyS(View<const double>(dP_), dQ_);
363 const double pq =
dotDevice(n_, View<const double>(dP_), View<const double>(dQ_));
364 if (!(std::fabs(pq) > 0.0))
366 const double alpha = rz / pq;
370 Kokkos::parallel_for(
371 "amr::amgb_xupd", n_, KOKKOS_LAMBDA(
const Index i) {
372 x(i) += alpha * p(i);
373 r(i) -= alpha * q(i);
377 if (std::sqrt(
dotDevice(n_, View<const double>(dR_), View<const double>(dR_))) <=
382 Kokkos::parallel_for(
383 "amr::amgb_ident0", n_, KOKKOS_LAMBDA(
const Index i) {
387 applyS(View<const double>(x), dQ_);
391 Kokkos::parallel_reduce(
392 "amr::amgb_resid", n_,
393 KOKKOS_LAMBDA(
const Index i,
double& acc) {
394 const double v = rr(i) - q(i);
395 const double av = v < 0.0 ? -v : v;
399 Kokkos::Max<double>(e));
400 lastRel_ = e / bnorm;
410 void buildComponents(
const typename Poisson::FvAssembled& A) {
411 parent_.resize(
static_cast<std::size_t
>(n_));
412 for (Index i = 0; i < n_; ++i)
413 parent_[
static_cast<std::size_t
>(i)] = i;
414 for (Index i = 0; i < n_; ++i) {
415 if (identity_[
static_cast<std::size_t
>(i)])
417 for (Index k = A.start[
static_cast<std::size_t
>(i)];
418 k < A.start[
static_cast<std::size_t
>(i) + 1]; ++k) {
419 if (!(A.coef[
static_cast<std::size_t
>(k)] > 0.0))
421 const Index j = A.nbr[
static_cast<std::size_t
>(k)];
422 if (j < 0 || j >= n_ || identity_[
static_cast<std::size_t
>(j)])
427 comp_.assign(
static_cast<std::size_t
>(n_), -1);
429 std::vector<Index> label(
static_cast<std::size_t
>(n_), -1);
430 for (Index i = 0; i < n_; ++i) {
431 if (identity_[
static_cast<std::size_t
>(i)])
433 const Index r = find(i);
434 if (label[
static_cast<std::size_t
>(r)] < 0)
435 label[
static_cast<std::size_t
>(r)] = nComp_++;
436 comp_[
static_cast<std::size_t
>(i)] = label[
static_cast<std::size_t
>(r)];
438 compSize_.assign(
static_cast<std::size_t
>(nComp_), 0);
439 for (Index i = 0; i < n_; ++i)
440 if (comp_[
static_cast<std::size_t
>(i)] >= 0)
441 ++compSize_[
static_cast<std::size_t
>(comp_[
static_cast<std::size_t
>(i)])];
444 Index find(Index i)
const {
445 while (parent_[
static_cast<std::size_t
>(i)] != i) {
446 parent_[
static_cast<std::size_t
>(i)] =
447 parent_[
static_cast<std::size_t
>(parent_[
static_cast<std::size_t
>(i)])];
448 i = parent_[
static_cast<std::size_t
>(i)];
452 void unite(Index a, Index b) {
453 const Index ra = find(a), rb = find(b);
455 parent_[
static_cast<std::size_t
>(ra)] = rb;
466 static constexpr double kTol = 1e-10;
467 static constexpr int kMaxIters = 100;
469 Index n_ = 0, nIdentity_ = 0;
470 bool singular_ =
true, onDevice_ =
false;
472 core::solver::HostCsrOp S_;
473 core::solver::GraphAMG amg_;
474 core::solver::GraphAMGDevice amgD_;
475 std::vector<double> invVol_;
476 std::vector<char> identity_;
477 mutable std::vector<Index> parent_;
478 std::vector<Index> comp_, compSize_;
479 mutable std::vector<double> r_, z_, p_, q_, rhs_, bHost_, xHost_;
480 mutable Kokkos::View<double*, Kokkos::HostSpace> bMirror_, xMirror_;
481 View<double> dDiag_, dCoef_, dInvVol_, dIdent_;
482 View<Index> dStart_, dNbr_, dComp_;
483 mutable View<double> dR_, dZ_, dP_, dQ_, dRhs_, dIn_, dOut_;
484 mutable int lastIters_ = 0;
485 mutable double lastRel_ = 0.0;