Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_CompositeSystemUtil.H
Go to the documentation of this file.
1#ifndef AMREX_COMPOSITE_SYSTEM_UTIL_H_
2#define AMREX_COMPOSITE_SYSTEM_UTIL_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_AlgMG.H>
10#include <AMReX_ParmParse.H>
11#include <AMReX_Reduce.H>
12#include <AMReX_Scan.H>
13#include <AMReX_SpMatrix.H>
14#include <AMReX_Vector.H>
15#include <HYPRE_IJ_mv.h>
16
17#include <algorithm>
18#include <array>
19#include <cmath>
20#include <fstream>
21#include <functional>
22#include <iomanip>
23#include <limits>
24#include <map>
25#include <sstream>
26#include <string>
27#include <utility>
28
38
39namespace amrex::composite {
40
42struct Triplet { Long row; Long col; Real val; };
43
51{
54 [[nodiscard]] bool empty () const { return offset.empty(); }
55 [[nodiscard]] Long numTriplets () const { return Long(trip.size()); }
56 void clear () { offset.clear(); trip.clear(); }
57};
58
61inline void define_gather (EntryGather& g, Vector<Long> const& entry, Long nentries)
62{
63 const auto ntrip = Long(entry.size());
64 Vector<Long> offset(nentries+1, 0);
65 for (Long n = 0; n < ntrip; ++n) { ++offset[entry[n]+1]; }
66 for (Long e = 0; e < nentries; ++e) { offset[e+1] += offset[e]; }
67 Vector<Long> fill(offset.begin(), offset.end()-1);
68 Vector<Long> trip(ntrip);
69 for (Long n = 0; n < ntrip; ++n) { trip[fill[entry[n]]++] = n; }
70 g.offset.resize(nentries+1);
71 g.trip.resize(ntrip);
72 Gpu::copyAsync(Gpu::hostToDevice, offset.begin(), offset.end(), g.offset.begin());
73 Gpu::copyAsync(Gpu::hostToDevice, trip.begin(), trip.end(), g.trip.begin());
75}
76
79inline void gather_sum (EntryGather const& g, Real const* vals, Real* out, Long nentries)
80{
81 AMREX_ALWAYS_ASSERT(Long(g.offset.size()) == nentries+1);
82 auto const* go = g.offset.data();
83 auto const* gt = g.trip.data();
84 amrex::ParallelFor(nentries, [=] AMREX_GPU_DEVICE (Long e) noexcept
85 {
86 Real sum = 0.0;
87 for (Long k = go[e]; k < go[e+1]; ++k) { sum += vals[gt[k]]; }
88 out[e] = sum;
89 });
91}
92
100bool ij_entry_slots (HYPRE_IJMatrix A, Gpu::DeviceVector<HYPRE_BigInt> const& rows,
102 EntryGather& g);
103
106void ij_update_values (HYPRE_IJMatrix A, EntryGather const& g, Gpu::DeviceVector<Real> const& vals);
107
123{
125 Real scale = 1.0;
129 bool have_vals = false;
132 bool capture = false;
136 bool current = false;
137
140 {
141 max_change = f;
142 if (f > Real(0.0)) {
144 } else {
145 setup_vals.clear();
146 have_vals = false;
147 capture = false;
148 }
149 }
150
156 void setupDone ()
157 {
158 if (!current) {
159 capture = (max_change > Real(0.0));
160 current = true;
161 }
162 }
163
167 void setSetupMatrix (Real norm2)
168 {
169 setup_norm2 = norm2;
170 scale = Real(1.0);
171 current = true;
172 setup_vals.clear();
173 have_vals = false;
174 }
175
186 template <class P>
187 bool rescale (Gpu::DeviceVector<Real>& vals, P const& scaled, bool new_setup,
188 MPI_Comm comm, int verbose, std::string const& prefix)
189 {
190 BL_PROFILE("composite::SetupReuse::rescale");
191 const auto n = Long(vals.size());
192 auto* v = vals.data();
193 Real norm2 = (n > 0) ? Reduce::Sum<Real>(n, [=] AMREX_GPU_DEVICE (Long i) noexcept
194 { return scaled(i) ? v[i]*v[i] : Real(0.0); })
195 : Real(0.0);
196 ParallelAllReduce::Sum(norm2, comm);
197 // No setup matrix to match: this is it
198 new_setup = new_setup || !(setup_norm2 > Real(0.0)) || !(norm2 > Real(0.0));
199 bool changed = false;
200 // have_vals and capture are the same on all ranks, so that the
201 // reduction below is collective
202 if (!new_setup && max_change > Real(0.0) && have_vals && !capture) {
203 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(std::cmp_equal(setup_vals.size(), n),
204 "composite::SetupReuse: the matrix pattern changed since the kept setup");
205 // The change of the scaled matrix since the setup, relative to
206 // the setup matrix
207 const Real s = std::sqrt(setup_norm2 / norm2);
208 auto const* v0 = setup_vals.data();
209 Real d2 = Reduce::Sum<Real>(n, [=] AMREX_GPU_DEVICE (Long i) noexcept
210 { Real d = scaled(i) ? s*v[i] - v0[i] : Real(0.0); return d*d; });
211 ParallelAllReduce::Sum(d2, comm);
212 const Real change = std::sqrt(d2 / setup_norm2);
213 if (change > max_change) {
214 if (verbose) {
215 amrex::Print() << prefix << ": the matrix changed by " << change
216 << " since the kept setup; redoing the setup\n";
217 }
218 changed = true;
219 }
220 }
221 if (new_setup || changed) {
222 setup_norm2 = norm2;
223 scale = Real(1.0);
224 } else {
225 scale = std::sqrt(setup_norm2 / norm2);
226 const Real s = scale;
227 amrex::ParallelFor(n, [=] AMREX_GPU_DEVICE (Long i) noexcept
228 {
229 if (scaled(i)) { v[i] *= s; }
230 });
232 }
233 current = (new_setup || changed);
234 if (max_change > Real(0.0) && (new_setup || changed || capture)) {
235 // The reference: the (scaled) values the setup is, or was, done for
236 setup_vals.resize(n);
237 Gpu::copyAsync(Gpu::deviceToDevice, vals.begin(), vals.end(), setup_vals.begin());
239 have_vals = true;
240 }
241 capture = false;
242 return changed;
243 }
244};
245
249{
250 int num_iters = 0;
252 bool redone = false;
255 std::string error;
256};
257
267ReusedSolve ij_solve_reused (HypreIJIface& ij, bool reused, bool throw_exception,
268 Real reltol, Real abstol, int maxiter, MPI_Comm comm,
269 int verbose, std::string const& prefix,
270 std::function<bool(Real)> const& converged,
271 std::function<void()> const& restore_x);
272
283 bool throw_exception, AlgVector<Real>& x,
284 AlgVector<Real> const& b, Real bnorm, MPI_Comm comm,
285 int verbose, std::string const& prefix,
286 std::function<bool(Real)> const& converged,
287 std::function<void()> const& restore_x);
288
290inline std::string to_sci (double x)
291{
292 std::ostringstream os;
293 os << std::scientific << std::setprecision(6) << x;
294 return os.str();
295}
296
302{
308
309 [[nodiscard]] Long nnz () const { return values.size(); }
310
313 void define (Long a_row_begin, Long a_nrows, Vector<Triplet> trips)
314 {
315 row_begin = a_row_begin;
316 nrows = a_nrows;
317 std::sort(trips.begin(), trips.end(), [] (Triplet const& a, Triplet const& b) {
318 return (a.row < b.row) || (a.row == b.row && a.col < b.col);
319 });
320 row_offset.assign(nrows+1, 0);
321 col_index.clear();
322 values.clear();
323 col_index.reserve(trips.size());
324 values.reserve(trips.size());
325 Long prev_row = -1, prev_col = -1;
326 for (auto const& t : trips) {
327 AMREX_ALWAYS_ASSERT(t.row >= row_begin && t.row < row_begin+nrows);
328 if (t.row == prev_row && t.col == prev_col) {
329 values.back() += t.val;
330 } else {
331 col_index.push_back(t.col);
332 values.push_back(t.val);
333 ++row_offset[t.row-row_begin+1];
334 prev_row = t.row;
335 prev_col = t.col;
336 }
337 }
338 for (Long r = 0; r < nrows; ++r) { row_offset[r+1] += row_offset[r]; }
339 }
340
342 void toSpMatrix (SpMatrix<Real>& mat, Vector<Long> const& rank_offset) const
343 {
345 csr.mat.resize(values.size());
346 csr.col_index.resize(col_index.size());
347 csr.row_offset.resize(row_offset.size());
348 csr.nnz = nnz();
349 Gpu::copyAsync(Gpu::hostToDevice, values.begin(), values.end(), csr.mat.begin());
350 Gpu::copyAsync(Gpu::hostToDevice, col_index.begin(), col_index.end(), csr.col_index.begin());
353 mat.define(AlgPartition(rank_offset), std::move(csr), CsrSorted{true});
354 }
355};
356
363 MPI_Comm comm)
364{
365 const int nprocs = ParallelDescriptor::NProcs(comm);
366 if (nprocs == 1) { return std::move(trips); }
367#ifdef AMREX_USE_MPI
368 const int myproc = ParallelDescriptor::MyProc(comm);
369 Vector<Vector<Triplet>> sendbuf(nprocs);
370 Vector<Triplet> local;
371 for (auto const& t : trips) {
372 auto it = std::upper_bound(rank_offset.begin(), rank_offset.end(), t.row);
373 const int p = static_cast<int>(it - rank_offset.begin()) - 1;
374 AMREX_ALWAYS_ASSERT(p >= 0 && p < nprocs); // row in [0, total)
375 if (p == myproc) { local.push_back(t); } else { sendbuf[p].push_back(t); }
376 }
377 trips.clear();
378 // Counts in triplets (one MPI element per triplet), checked against int
379 constexpr Long int_max = std::numeric_limits<int>::max();
380 Vector<int> sendcnt(nprocs), recvcnt(nprocs), sdispl(nprocs), rdispl(nprocs);
381 for (int p = 0; p < nprocs; ++p) {
382 AMREX_ALWAYS_ASSERT(sendbuf[p].size() <= int_max);
383 sendcnt[p] = static_cast<int>(sendbuf[p].size());
384 }
385 MPI_Alltoall(sendcnt.data(), 1, MPI_INT, recvcnt.data(), 1, MPI_INT, comm);
386 Long stot = 0, rtot = 0;
387 for (int p = 0; p < nprocs; ++p) {
388 sdispl[p] = static_cast<int>(stot); stot += sendcnt[p];
389 rdispl[p] = static_cast<int>(rtot); rtot += recvcnt[p];
390 }
391 AMREX_ALWAYS_ASSERT(stot <= int_max && rtot <= int_max);
392 Vector<Triplet> sbuf;
393 sbuf.reserve(stot);
394 for (auto const& b : sendbuf) { sbuf.insert(sbuf.end(), b.begin(), b.end()); }
395 sendbuf.clear();
396 const Long nlocal = local.size();
397 local.resize(nlocal + rtot);
398 MPI_Datatype ttype;
399 MPI_Type_contiguous(int(sizeof(Triplet)), MPI_BYTE, &ttype);
400 MPI_Type_commit(&ttype);
401 MPI_Alltoallv(sbuf.data(), sendcnt.data(), sdispl.data(), ttype,
402 local.data()+nlocal, recvcnt.data(), rdispl.data(), ttype, comm);
403 MPI_Type_free(&ttype);
404 return local;
405#else
406 amrex::ignore_unused(rank_offset);
407 return std::move(trips);
408#endif
409}
410
418template <typename I>
420 Gpu::DeviceVector<Real>& vals, Long row_begin, Long nrows,
421 Vector<Long> const& rank_offset, MPI_Comm comm)
422{
423 if (ParallelDescriptor::NProcs(comm) == 1) { return; }
424 BL_PROFILE("composite::localize_triplets");
425 const Long row_end = row_begin + nrows;
426 const auto n = Long(rows.size());
427 auto* pr = rows.data();
428 auto* pc = cols.data();
429 auto* pv = vals.data();
431 auto* ppos = pos.data();
432 const Long nsend = Scan::PrefixSum<Long>(n,
433 [=] AMREX_GPU_DEVICE (Long i) -> Long { return (pr[i] < row_begin || pr[i] >= row_end) ? 1 : 0; },
434 [=] AMREX_GPU_DEVICE (Long i, Long const& x) { ppos[i] = x; },
436 Gpu::DeviceVector<Triplet> dsend(nsend);
437 Gpu::DeviceVector<I> lrows(n-nsend), lcols(n-nsend);
438 Gpu::DeviceVector<Real> lvals(n-nsend);
439 auto* ps = dsend.data();
440 auto* lr = lrows.data();
441 auto* lc = lcols.data();
442 auto* lv = lvals.data();
443 amrex::ParallelFor(n, [=] AMREX_GPU_DEVICE (Long i) noexcept
444 {
445 if (pr[i] < row_begin || pr[i] >= row_end) {
446 ps[ppos[i]] = Triplet{.row = Long(pr[i]), .col = Long(pc[i]), .val = pv[i]};
447 } else {
448 const Long il = i - ppos[i];
449 lr[il] = pr[i];
450 lc[il] = pc[i];
451 lv[il] = pv[i];
452 }
453 });
454 Vector<Triplet> hsend(nsend);
455 Gpu::copyAsync(Gpu::deviceToHost, dsend.begin(), dsend.end(), hsend.begin());
457 Vector<Triplet> const hrecv = exchange_triplets(std::move(hsend), rank_offset, comm);
458 const auto nrecv = Long(hrecv.size());
459 const Long nloc = n - nsend;
460 rows = std::move(lrows);
461 cols = std::move(lcols);
462 vals = std::move(lvals);
463 if (nrecv > 0) {
464 Gpu::DeviceVector<Triplet> drecv(nrecv);
465 Gpu::copyAsync(Gpu::hostToDevice, hrecv.begin(), hrecv.end(), drecv.begin());
466 rows.resize(nloc+nrecv);
467 cols.resize(nloc+nrecv);
468 vals.resize(nloc+nrecv);
469 auto const* pt = drecv.data();
470 auto* r = rows.data();
471 auto* c = cols.data();
472 auto* v = vals.data();
473 amrex::ParallelFor(nrecv, [=] AMREX_GPU_DEVICE (Long i) noexcept
474 {
475 r[nloc+i] = I(pt[i].row);
476 c[nloc+i] = I(pt[i].col);
477 v[nloc+i] = pt[i].val;
478 });
479 Gpu::streamSynchronize(); // before drecv is freed
480 }
481}
482
496 Gpu::DeviceVector<Real>& vals, Long row_begin, Long nrows,
497 Vector<Long> const& rank_offset, MPI_Comm comm,
498 SpMatrix<Real>& mat, bool localized = false)
499{
500 BL_PROFILE("composite::triplets_to_spmatrix");
501 AMREX_ALWAYS_ASSERT(rows.size() == cols.size() && rows.size() == vals.size());
502 if (!localized) {
503 localize_triplets(rows, cols, vals, row_begin, nrows, rank_offset, comm);
504 }
505
506 // Bucket the triplet indices by row
507 const auto ntrip = Long(rows.size());
508 auto const* pr = rows.data();
509 auto const* pc = cols.data();
510 auto const* pv = vals.data();
511 Gpu::DeviceVector<int> cnt(nrows, 0);
512 Gpu::DeviceVector<Long> start(nrows+1);
513 auto* pcnt = cnt.data();
514 auto* pstart = start.data();
515 amrex::ParallelFor(ntrip, [=] AMREX_GPU_DEVICE (Long i) noexcept
516 {
517 Gpu::Atomic::AddNoRet(pcnt + (pr[i]-row_begin), 1);
518 });
519 const Long n_owned = Scan::PrefixSum<Long>(nrows,
520 [=] AMREX_GPU_DEVICE (Long r) -> Long { return pcnt[r]; },
521 [=] AMREX_GPU_DEVICE (Long r, Long const& x) { pstart[r] = x; },
523 amrex::ParallelFor(nrows+1, [=] AMREX_GPU_DEVICE (Long r) noexcept
524 {
525 if (r < nrows) { pcnt[r] = 0; } else { pstart[r] = n_owned; }
526 });
527 Gpu::DeviceVector<Long> perm(n_owned);
528 auto* pp = perm.data();
529 amrex::ParallelFor(ntrip, [=] AMREX_GPU_DEVICE (Long i) noexcept
530 {
531 const Long r = pr[i]-row_begin;
532 pp[pstart[r] + Gpu::Atomic::Add(pcnt+r, 1)] = i;
533 });
534
535 // Sort each row by (column, triplet index) and count its distinct columns
536 Gpu::DeviceVector<Long> nuniq(nrows);
537 auto* pnu = nuniq.data();
538 amrex::ParallelFor(nrows, [=] AMREX_GPU_DEVICE (Long r) noexcept
539 {
540 const Long b = pstart[r], e = pstart[r+1];
541 for (Long k = b+1; k < e; ++k) {
542 const Long t = pp[k];
543 const Long ct = pc[t];
544 Long m = k;
545 while (m > b && (pc[pp[m-1]] > ct || (pc[pp[m-1]] == ct && pp[m-1] > t))) {
546 pp[m] = pp[m-1];
547 --m;
548 }
549 pp[m] = t;
550 }
551 Long u = 0;
552 for (Long k = b; k < e; ++k) {
553 if (k == b || pc[pp[k]] != pc[pp[k-1]]) { ++u; }
554 }
555 pnu[r] = u;
556 });
557
558 // Sum the duplicates
560 csr.row_offset.resize(nrows+1);
561 auto* pro = csr.row_offset.data();
562 const Long nnz = Scan::PrefixSum<Long>(nrows,
563 [=] AMREX_GPU_DEVICE (Long r) -> Long { return pnu[r]; },
564 [=] AMREX_GPU_DEVICE (Long r, Long const& x) { pro[r] = x; },
566 csr.nnz = nnz;
567 csr.col_index.resize(nnz);
568 csr.mat.resize(nnz);
569 auto* pci = csr.col_index.data();
570 auto* pm = csr.mat.data();
571 amrex::ParallelFor(nrows+1, [=] AMREX_GPU_DEVICE (Long r) noexcept
572 {
573 if (r == nrows) { pro[r] = nnz; return; }
574 Long u = pro[r]-1;
575 for (Long k = pstart[r]; k < pstart[r+1]; ++k) {
576 const Long t = pp[k];
577 if (k == pstart[r] || pc[t] != pc[pp[k-1]]) {
578 ++u;
579 pci[u] = pc[t];
580 pm[u] = pv[t];
581 } else {
582 pm[u] += pv[t];
583 }
584 }
585 });
587 rows.clear();
588 cols.clear();
589 vals.clear();
590 mat.define(AlgPartition(rank_offset), std::move(csr), CsrSorted{true});
591}
592
594inline std::map<std::string,int>& dump_counters ()
595{
596 static std::map<std::string,int> counter;
597 return counter;
598}
599
601inline bool dump_pending (std::string const& prefix, int max)
602{
603 return max > 0 && dump_counters()[prefix] < max;
604}
605
607inline int next_dump_index (std::string const& prefix, int max)
608{
609 if (!dump_pending(prefix, max)) { return -1; }
610 return dump_counters()[prefix]++;
611}
612
617template <typename T>
619{
620 const int nprocs = ParallelDescriptor::NProcs(comm);
621 if (nprocs == 1) { return v; }
622#ifdef AMREX_USE_MPI
623 // MPI counts are int (callers check the total with dump_single_file)
624 AMREX_ALWAYS_ASSERT(Long(v.size()) <= Long(std::numeric_limits<int>::max()));
625 int nloc = static_cast<int>(v.size());
626 Vector<int> counts(nprocs), displs(nprocs, 0);
627 MPI_Gather(&nloc, 1, MPI_INT, counts.data(), 1, MPI_INT, 0, comm);
628 Vector<T> g;
629 if (ParallelDescriptor::MyProc(comm) == 0) {
630 Long ntot = 0;
631 for (int p = 0; p < nprocs; ++p) { displs[p] = int(ntot); ntot += counts[p]; }
632 AMREX_ALWAYS_ASSERT(ntot <= Long(std::numeric_limits<int>::max()));
633 g.resize(ntot);
634 }
636 MPI_Gatherv(v.data(), nloc, mt, g.data(), counts.data(), displs.data(), mt, 0, comm);
637 return g;
638#else
639 return v;
640#endif
641}
642
648inline bool dump_single_file (Long total, Long gather_max, MPI_Comm comm)
649{
650 return (ParallelDescriptor::NProcs(comm) == 1) ||
651 (total <= std::min(gather_max, Long(std::numeric_limits<int>::max())));
652}
653
655inline std::ofstream open_dump (std::string const& name)
656{
657 std::ofstream ofs(name);
658 if (!ofs) { amrex::Abort("Cannot open dump file " + name); }
659 ofs << std::setprecision(std::numeric_limits<Real>::max_digits10);
660 return ofs;
661}
662
664inline void check_dump (std::ofstream const& ofs, std::string const& name)
665{
666 if (!ofs) { amrex::Abort("Failed to write dump file " + name); }
667}
668
676inline void write_row_map (std::string const& file, std::string const& title,
677 Vector<std::array<Long,4>> const& info, Vector<Real> const& scaling,
678 Long row_begin, MPI_Comm comm, Long gather_max)
679{
680 const int nprocs = ParallelDescriptor::NProcs(comm);
681 const int myproc = ParallelDescriptor::MyProc(comm);
682 Long nglobal = info.size();
683 ParallelAllReduce::Sum(nglobal, comm);
684
685 Vector<Long> flat(info.size()*4);
686 for (Long r = 0; r < info.size(); ++r) {
687 for (int c = 0; c < 4; ++c) { flat[r*4+c] = info[r][c]; }
688 }
689 auto write = [&] (std::string const& name, std::string const& note, Long first_row,
690 Vector<Long> const& fl, Vector<Real> const& sc)
691 {
692 auto ofs = open_dump(name);
693 ofs << "# " << title << "\n" << note << "# row level i j k scaling\n";
694 for (Long r = 0; r < sc.size(); ++r) {
695 ofs << first_row+r+1 << ' ' << fl[r*4] << ' ' << fl[r*4+1] << ' '
696 << fl[r*4+2] << ' ' << fl[r*4+3] << ' ' << sc[r] << '\n';
697 }
698 check_dump(ofs, name);
699 };
700 if (dump_single_file(4*nglobal, gather_max, comm)) {
701 auto gflat = gather_on_root(flat, comm);
702 auto gscl = gather_on_root(scaling, comm);
703 if (myproc == 0) { write(file, "", 0, gflat, gscl); }
704 } else {
705 write(file + "." + std::to_string(myproc),
706 "# part " + std::to_string(myproc) + " of " + std::to_string(nprocs) +
707 ": rows owned by this rank\n", row_begin, flat, scaling);
708 }
709}
710
719inline void algmg_parse_options (AlgMG<Real>& mg, std::string const& prefix,
720 AlgMGKrylovSolver krylov)
721{
722 ParmParse pp(prefix + ".algmg");
723 int iv = 0;
724 Real rv = 0;
725 Long lv = 0;
726 if (pp.query("verbose", iv)) { mg.setVerbose(iv); }
727 pp.query_enum_case_insensitive("krylov", krylov);
728 mg.setKrylovSolver(krylov);
729 AlgMGSmoother sm{};
730 if (pp.query_enum_case_insensitive("smoother", sm)) { mg.setSmoother(sm); }
731 AlgMGInterpType it{};
732 if (pp.query_enum_case_insensitive("interp_type", it)) { mg.setInterpType(it); }
734 if (pp.query_enum_case_insensitive("bottom_solver", bs)) { mg.setBottomSolver(bs); }
735 if (pp.query("strong_threshold", rv)) { mg.setStrongThreshold(rv); }
736 if (pp.query("max_levels", iv)) { mg.setMaxLevels(iv); }
737 if (pp.query("max_coarse_size", lv)) { mg.setMaxCoarseSize(lv); }
738 if (pp.query("agg_num_levels", iv)) { mg.setAggressiveNumLevels(iv); }
739 if (pp.query("pmax_elmts", iv)) { mg.setPMaxElmts(iv); }
740 if (pp.query("trunc_factor", rv)) { mg.setTruncFactor(rv); }
741 if (pp.query("pre_smooth", iv)) { mg.setPreSmooth(iv); }
742 if (pp.query("post_smooth", iv)) { mg.setPostSmooth(iv); }
743 if (pp.query("cheby_degree", iv)) { mg.setChebyshevDegree(iv); }
744}
745
752inline void write_matrix_market (std::string const& file, OwnedCSR const& csr,
753 Long nglobal, MPI_Comm comm, Long gather_max)
754{
755 const int nprocs = ParallelDescriptor::NProcs(comm);
756 const int myproc = ParallelDescriptor::MyProc(comm);
757 Long nnz_global = csr.nnz();
758 ParallelAllReduce::Sum(nnz_global, comm);
759
760 Vector<Long> rows(csr.nnz());
761 for (Long r = 0; r < csr.nrows; ++r) {
762 for (Long n = csr.row_offset[r]; n < csr.row_offset[r+1]; ++n) { rows[n] = csr.row_begin + r; }
763 }
764 auto write = [&] (std::string const& name, std::string const& note, Vector<Long> const& rr,
765 Vector<Long> const& cc, Vector<Real> const& vv)
766 {
767 auto ofs = open_dump(name);
768 ofs << "%%MatrixMarket matrix coordinate real general\n" << note;
769 ofs << nglobal << ' ' << nglobal << ' ' << vv.size() << '\n';
770 for (Long n = 0; n < vv.size(); ++n) {
771 ofs << rr[n]+1 << ' ' << cc[n]+1 << ' ' << vv[n] << '\n';
772 }
773 check_dump(ofs, name);
774 };
775 if (dump_single_file(nnz_global, gather_max, comm)) {
776 auto grows = gather_on_root(rows, comm);
777 auto gcols = gather_on_root(csr.col_index, comm);
778 auto gvals = gather_on_root(csr.values, comm);
779 if (myproc == 0) { write(file, "", grows, gcols, gvals); }
780 } else {
781 write(file + "." + std::to_string(myproc),
782 "% part " + std::to_string(myproc) + " of " + std::to_string(nprocs) +
783 ": rows owned by this rank\n", rows, csr.col_index, csr.values);
784 }
785}
786
793inline void write_vector_market (std::string const& file, Vector<Real> const& v,
794 Long nglobal, Long row_begin, MPI_Comm comm, Long gather_max)
795{
796 const int nprocs = ParallelDescriptor::NProcs(comm);
797 const int myproc = ParallelDescriptor::MyProc(comm);
798 auto write = [&] (std::string const& name, std::string const& note, Vector<Real> const& x)
799 {
800 auto ofs = open_dump(name);
801 ofs << "%%MatrixMarket matrix array real general\n" << note;
802 ofs << x.size() << " 1\n";
803 for (auto const& xi : x) { ofs << xi << '\n'; }
804 check_dump(ofs, name);
805 };
806 if (dump_single_file(nglobal, gather_max, comm)) {
807 auto g = gather_on_root(v, comm);
808 if (myproc == 0) { write(file, "", g); }
809 } else {
810 write(file + "." + std::to_string(myproc),
811 "% part " + std::to_string(myproc) + " of " + std::to_string(nprocs) +
812 ": rows " + std::to_string(row_begin+1) + " to " +
813 std::to_string(row_begin+v.size()) + " of " + std::to_string(nglobal) + "\n", v);
814 }
815}
816
817}
818
819#endif
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:593
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:49
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
while parser_exe_t p
Definition AMReX_Parser_Exe_Body.H:33
Algebraic multigrid solver for SpMatrix / AlgVector systems.
Definition AMReX_AlgMG.H:254
void setAggressiveNumLevels(int n)
Number of levels with aggressive coarsening, starting at the finest. Next setup.
Definition AMReX_AlgMG.H:328
void setChebyshevDegree(int d)
Chebyshev polynomial degree. Next solve.
Definition AMReX_AlgMG.H:304
void setMaxCoarseSize(Long n)
Stop coarsening when a level has at most this many rows. Next setup.
Definition AMReX_AlgMG.H:326
void setPreSmooth(int nu)
Number of pre-smoothing sweeps; negative restores the smoother's default.
Definition AMReX_AlgMG.H:311
void setBottomSolver(BottomSolver bs)
Definition AMReX_AlgMG.H:366
void setPMaxElmts(int n)
Definition AMReX_AlgMG.H:343
void setPostSmooth(int nu)
Number of post-smoothing sweeps; negative restores the smoother's default.
Definition AMReX_AlgMG.H:313
void setInterpType(InterpType it)
Takes effect at the next setup.
Definition AMReX_AlgMG.H:340
void setTruncFactor(T f)
Drop interpolation weights below this fraction of the row maximum. Next setup.
Definition AMReX_AlgMG.H:351
void setMaxLevels(int n)
Definition AMReX_AlgMG.H:324
void setStrongThreshold(T theta)
Definition AMReX_AlgMG.H:321
void setSmoother(Smoother sm)
Takes effect at the next solve; the hierarchy is kept.
Definition AMReX_AlgMG.H:300
void setVerbose(int v)
Definition AMReX_AlgMG.H:290
void setKrylovSolver(KrylovSolver a)
Krylov solver with the V-cycle as preconditioner (see KrylovSolver). Default None.
Definition AMReX_AlgMG.H:353
Definition AMReX_AlgPartition.H:26
Distributed dense vector that mirrors the layout of an AlgPartition.
Definition AMReX_AlgVector.H:29
Convenience interface for configuring and invoking HYPRE IJ solvers.
Definition AMReX_HypreIJIface.H:25
size_type size() const noexcept
Definition AMReX_PODVector.H:654
iterator begin() noexcept
Definition AMReX_PODVector.H:680
void resize(size_type a_new_size, GrowthStrategy strategy=GrowthStrategy::Poisson)
Definition AMReX_PODVector.H:734
iterator end() noexcept
Definition AMReX_PODVector.H:684
void clear() noexcept
Definition AMReX_PODVector.H:652
T * data() noexcept
Definition AMReX_PODVector.H:672
Parse Parameters From Command Line and Input Files.
Definition AMReX_ParmParse.H:355
int query_enum_case_insensitive(std::string_view name, T &ref, int ival=FIRST) const
. Query enum value using given name.
Definition AMReX_ParmParse.H:1465
int query(std::string_view name, bool &ref, int ival=FIRST) const
Same as querykth() but searches for the last occurrence of name.
Definition AMReX_ParmParse.cpp:2032
This class provides the user with a few print options.
Definition AMReX_Print.H:35
Distributed CSR matrix that manages storage and GPU-friendly partitions.
Definition AMReX_SpMatrix.H:65
CSR< T, container_type > csr_type
Definition AMReX_SpMatrix.H:70
void define(AlgPartition partition, int nnz_per_row)
Allocate storage for a default-constructed matrix with a fixed number of nonzeros per row.
Definition AMReX_SpMatrix.H:917
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
Long size() const noexcept
Definition AMReX_Vector.H:54
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
amrex_long Long
Definition AMReX_INT.H:30
T Sum(N n, T const *v, T init_val=0)
Compute the sum of an array of values.
Definition AMReX_Reduce.H:1082
PODVector< T, ArenaAllocator< T > > DeviceVector
A PODVector that uses the standard memory Arena. Note that the memory might or might not be managed d...
Definition AMReX_GpuContainers.H:24
int MyProc() noexcept
Definition AMReX_ParallelDescriptor.H:128
void Sum(Gpu::DeviceVector< T > &v, MPI_Comm comm)
Definition AMReX_GpuParallelReduce.H:37
int NProcs() noexcept
Definition AMReX_ParallelDescriptor.H:255
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:54
__host__ __device__ AMREX_FORCE_INLINE void AddNoRet(T *sum, T value) noexcept
Definition AMReX_GpuAtomic.H:322
__host__ __device__ AMREX_FORCE_INLINE T Add(T *sum, T value) noexcept
Definition AMReX_GpuAtomic.H:239
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition AMReX_GpuContainers.H:228
static constexpr DeviceToDevice deviceToDevice
Definition AMReX_GpuContainers.H:107
static constexpr DeviceToHost deviceToHost
Definition AMReX_GpuContainers.H:106
static constexpr HostToDevice hostToDevice
Definition AMReX_GpuContainers.H:105
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:329
static constexpr struct amrex::Scan::Type::Exclusive exclusive
static constexpr RetSum retSum
Definition AMReX_Scan.H:34
T PrefixSum(N n, FIN const &fin, FOUT const &fout, TYPE, RetSum a_ret_sum=retSum)
Definition AMReX_Scan.H:815
Definition AMReX_CompositeSystemUtil.cpp:12
void write_row_map(std::string const &file, std::string const &title, Vector< std::array< Long, 4 > > const &info, Vector< Real > const &scaling, Long row_begin, MPI_Comm comm, Long gather_max)
Write the row map of a composite system: one line per row with the 1-based row, AMR level,...
Definition AMReX_CompositeSystemUtil.H:676
std::string to_sci(double x)
Scientific notation for messages (std::to_string prints 0.000000 for 1e-7).
Definition AMReX_CompositeSystemUtil.H:290
void define_gather(EntryGather &g, Vector< Long > const &entry, Long nentries)
Definition AMReX_CompositeSystemUtil.H:61
void triplets_to_spmatrix(Gpu::DeviceVector< Long > &rows, Gpu::DeviceVector< Long > &cols, Gpu::DeviceVector< Real > &vals, Long row_begin, Long nrows, Vector< Long > const &rank_offset, MPI_Comm comm, SpMatrix< Real > &mat, bool localized=false)
Build an SpMatrix of this rank's rows [row_begin, row_begin+nrows) from (row, column,...
Definition AMReX_CompositeSystemUtil.H:495
void write_vector_market(std::string const &file, Vector< Real > const &v, Long nglobal, Long row_begin, MPI_Comm comm, Long gather_max)
Write a distributed vector (this rank's rows start at the global row row_begin) as a Matrix Market de...
Definition AMReX_CompositeSystemUtil.H:793
void localize_triplets(Gpu::DeviceVector< I > &rows, Gpu::DeviceVector< I > &cols, Gpu::DeviceVector< Real > &vals, Long row_begin, Long nrows, Vector< Long > const &rank_offset, MPI_Comm comm)
Send the (row, column, value) triplets in device memory whose rows are owned by other ranks to their ...
Definition AMReX_CompositeSystemUtil.H:419
bool dump_single_file(Long total, Long gather_max, MPI_Comm comm)
Whether a dump of total values goes to one file written by rank 0 (always on one rank)....
Definition AMReX_CompositeSystemUtil.H:648
void check_dump(std::ofstream const &ofs, std::string const &name)
Abort if writing a dump file failed.
Definition AMReX_CompositeSystemUtil.H:664
bool ij_entry_slots(HYPRE_IJMatrix A, Gpu::DeviceVector< HYPRE_BigInt > const &rows, Gpu::DeviceVector< HYPRE_BigInt > const &cols, MPI_Comm comm, EntryGather &g)
The entries of the assembled hypre IJ matrix A (its diag values first, then its offd values) that the...
Definition AMReX_CompositeSystemUtil.cpp:14
void algmg_parse_options(AlgMG< Real > &mg, std::string const &prefix, AlgMGKrylovSolver krylov)
Configure an AlgMG solver from <prefix>.algmg.*: verbose, krylov (none|bicgstab|gmres|pcg; default kr...
Definition AMReX_CompositeSystemUtil.H:719
bool dump_pending(std::string const &prefix, int max)
Whether a dump with this prefix is still pending (fewer than max written).
Definition AMReX_CompositeSystemUtil.H:601
void write_matrix_market(std::string const &file, OwnedCSR const &csr, Long nglobal, MPI_Comm comm, Long gather_max)
Write a distributed matrix in Matrix Market coordinate format (1-based indices, "general"),...
Definition AMReX_CompositeSystemUtil.H:752
void ij_update_values(HYPRE_IJMatrix A, EntryGather const &g, Gpu::DeviceVector< Real > const &vals)
Definition AMReX_CompositeSystemUtil.cpp:85
ReusedSolve algmg_solve_kept(AlgMG< Real > &mg, SpMatrix< Real > &mat, bool kept, bool throw_exception, AlgVector< Real > &x, AlgVector< Real > const &b, Real bnorm, MPI_Comm comm, int verbose, std::string const &prefix, std::function< bool(Real)> const &converged, std::function< void()> const &restore_x)
Solve with mg, whose tolerances and iteration limit are set. With a kept hierarchy (kept: from an ear...
Definition AMReX_CompositeSystemUtil.cpp:165
std::ofstream open_dump(std::string const &name)
Open a dump file for writing, aborting on failure.
Definition AMReX_CompositeSystemUtil.H:655
Vector< T > gather_on_root(Vector< T > const &v, MPI_Comm comm)
Gather the chunks of a distributed array on rank 0 (ranks in order); other ranks return an empty vect...
Definition AMReX_CompositeSystemUtil.H:618
int next_dump_index(std::string const &prefix, int max)
Index of the next dump for this prefix, or -1 once max have been written.
Definition AMReX_CompositeSystemUtil.H:607
ReusedSolve ij_solve_reused(HypreIJIface &ij, bool reused, bool throw_exception, Real reltol, Real abstol, int maxiter, MPI_Comm comm, int verbose, std::string const &prefix, std::function< bool(Real)> const &converged, std::function< void()> const &restore_x)
Solve with ij. With a reused setup (reused: kept from an earlier matrix), a solve that blows up (a hy...
Definition AMReX_CompositeSystemUtil.cpp:112
Vector< Triplet > exchange_triplets(Vector< Triplet > &&trips, Vector< Long > const &rank_offset, MPI_Comm comm)
Send each triplet to the rank owning its row (rank p owns rows [rank_offset[p], rank_offset[p+1])) an...
Definition AMReX_CompositeSystemUtil.H:362
std::map< std::string, int > & dump_counters()
Dumps written so far per dump prefix, over all solver objects of the run.
Definition AMReX_CompositeSystemUtil.H:594
void gather_sum(EntryGather const &g, Real const *vals, Real *out, Long nentries)
Definition AMReX_CompositeSystemUtil.H:79
int MPI_Comm
Definition AMReX_ccse-mpi.H:51
int MPI_Datatype
Definition AMReX_ccse-mpi.H:53
__host__ __device__ void ignore_unused(const Ts &...)
No-op helper that marks variables as intentionally unused.
Definition AMReX.H:273
AlgMGInterpType
Definition AMReX_AlgMG.H:210
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
AlgMGBottomSolver
Definition AMReX_AlgMG.H:200
AlgMGKrylovSolver
Definition AMReX_AlgMG.H:206
@ x
Definition AMReX_Orientation.H:14
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:239
const int[]
Definition AMReX_BLProfiler.cpp:1665
@ bnorm
Definition AMReX_MLMG.H:23
void fill(BaseFab< STRUCT > &aos_fab, F const &f)
Fill an array-of-structs BaseFab by invoking a functor per cell.
Definition AMReX_BaseFabUtility.H:58
AlgMGSmoother
Definition AMReX_AlgMG.H:219
V< Index > col_index
Definition AMReX_CSR.H:62
V< T > mat
Definition AMReX_CSR.H:61
V< Index > row_offset
Definition AMReX_CSR.H:63
Long nnz
Definition AMReX_CSR.H:64
Sorted CSR means for each row the column indices are sorted.
Definition AMReX_SpMatrix.H:49
With setup reuse, the triplets of later assemblies are summed directly into a matrix's value array,...
Definition AMReX_CompositeSystemUtil.H:51
Long numTriplets() const
Definition AMReX_CompositeSystemUtil.H:55
Gpu::DeviceVector< Long > trip
Definition AMReX_CompositeSystemUtil.H:53
Gpu::DeviceVector< Long > offset
Definition AMReX_CompositeSystemUtil.H:52
void clear()
Definition AMReX_CompositeSystemUtil.H:56
bool empty() const
Definition AMReX_CompositeSystemUtil.H:54
Rows owned by this rank in CSR form, columns sorted within each row and duplicates summed.
Definition AMReX_CompositeSystemUtil.H:302
Vector< Long > col_index
global column indices
Definition AMReX_CompositeSystemUtil.H:306
Long nnz() const
Definition AMReX_CompositeSystemUtil.H:309
void define(Long a_row_begin, Long a_nrows, Vector< Triplet > trips)
Definition AMReX_CompositeSystemUtil.H:313
Long nrows
number of rows owned by this rank
Definition AMReX_CompositeSystemUtil.H:304
Vector< Real > values
Definition AMReX_CompositeSystemUtil.H:307
Long row_begin
first global row of this rank
Definition AMReX_CompositeSystemUtil.H:303
Vector< Long > row_offset
size nrows+1
Definition AMReX_CompositeSystemUtil.H:305
void toSpMatrix(SpMatrix< Real > &mat, Vector< Long > const &rank_offset) const
Define an SpMatrix (device memory in GPU builds) with this CSR.
Definition AMReX_CompositeSystemUtil.H:342
Definition AMReX_CompositeSystemUtil.H:249
bool redone
Definition AMReX_CompositeSystemUtil.H:252
std::string error
Definition AMReX_CompositeSystemUtil.H:255
int num_iters
Definition AMReX_CompositeSystemUtil.H:250
Real final_res
Definition AMReX_CompositeSystemUtil.H:251
The state of setup reuse (HypreMLABecLap::setReuseSolverSetup, HypreMLNodeLap::setReuseSolverSetup).
Definition AMReX_CompositeSystemUtil.H:123
void setMaxChange(Real f)
The change threshold (see rescale).
Definition AMReX_CompositeSystemUtil.H:139
void setSetupMatrix(Real norm2)
Definition AMReX_CompositeSystemUtil.H:167
bool capture
Definition AMReX_CompositeSystemUtil.H:132
bool have_vals
Definition AMReX_CompositeSystemUtil.H:129
Gpu::DeviceVector< Real > setup_vals
The setup matrix's values (max_change > 0); valid on all ranks or none.
Definition AMReX_CompositeSystemUtil.H:128
Real scale
the current matrix (and rhs) is scaled by this
Definition AMReX_CompositeSystemUtil.H:125
bool rescale(Gpu::DeviceVector< Real > &vals, P const &scaled, bool new_setup, MPI_Comm comm, int verbose, std::string const &prefix)
Scale the values of vals (on the device) for which scaled(i) is true to the setup matrix (the others,...
Definition AMReX_CompositeSystemUtil.H:187
void setupDone()
Definition AMReX_CompositeSystemUtil.H:156
Real setup_norm2
squared 2-norm of the setup matrix's values
Definition AMReX_CompositeSystemUtil.H:124
Real max_change
Definition AMReX_CompositeSystemUtil.H:126
bool current
Definition AMReX_CompositeSystemUtil.H:136
(row, column, value) of a matrix entry with global indices
Definition AMReX_CompositeSystemUtil.H:42
Real val
Definition AMReX_CompositeSystemUtil.H:42
Long row
Definition AMReX_CompositeSystemUtil.H:42
Long col
Definition AMReX_CompositeSystemUtil.H:42