MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_LocalQR.hpp
Go to the documentation of this file.
1#ifndef MUELU_LOCALQR_HPP
2#define MUELU_LOCALQR_HPP
3
4#include <Kokkos_Core.hpp>
5#include <KokkosKernels_ArithTraits.hpp>
6#include "Xpetra_ConfigDefs.hpp"
7
8#include "KokkosBlas1_set.hpp"
9#include "KokkosBatched_ApplyQ_Decl.hpp"
10#include "KokkosBatched_SetIdentity_Decl.hpp"
11#include "KokkosBatched_SetIdentity_Impl.hpp"
12#include "Kokkos_DualView.hpp"
13#include "Kokkos_Pair.hpp"
14#include "KokkosBatched_QR_Decl.hpp"
15
16namespace MueLu::LocalQR {
17
18template <class LocalOrdinal, class View>
20 public:
22 : view_(view) {}
23
24 KOKKOS_INLINE_FUNCTION
25 void operator()(const LocalOrdinal& i, LocalOrdinal& vmax) const {
26 if (vmax < view_(i))
27 vmax = view_(i);
28 }
29
30 KOKKOS_INLINE_FUNCTION
31 void join(LocalOrdinal& dst, const LocalOrdinal& src) const {
32 if (dst < src) {
33 dst = src;
34 }
35 }
36
37 KOKKOS_INLINE_FUNCTION
38 void init(LocalOrdinal& dst) const {
39 dst = 0;
40 }
41
42 private:
43 View view_;
44};
45
46#if KOKKOSKERNELS_VERSION >= 50200
47// local QR decomposition
48template <class LOType, class GOType, class SCType, class DeviceType, class NspType, class aggRowsType, class maxAggDofSizeType, class agg2RowMapLOType, class statusType, class rowsType, class rowsAuxType, class colsAuxType, class valsAuxType>
50 private:
51 typedef LOType LO;
52 typedef GOType GO;
53 typedef SCType SC;
54
55 typedef typename DeviceType::execution_space execution_space;
56 typedef typename KokkosKernels::ArithTraits<SC>::val_type impl_SC;
57 typedef KokkosKernels::ArithTraits<impl_SC> impl_ATS;
58 typedef typename impl_ATS::magnitudeType Magnitude;
59
60 public:
61 typedef Kokkos::View<impl_SC**, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged> shared_matrix;
62 typedef Kokkos::View<impl_SC*, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged> shared_vector;
63
64 private:
65 NspType fineNS;
66 NspType coarseNS;
67 aggRowsType aggRows;
68 maxAggDofSizeType maxAggDofSize; //< maximum number of dofs in aggregate (max size of aggregate * numDofsPerNode)
69 agg2RowMapLOType agg2RowMapLO;
70 statusType statusAtomic;
71 rowsType rows;
72 rowsAuxType rowsAux;
73 colsAuxType colsAux;
74 valsAuxType valsAux;
75 bool doQRStep;
76 int scratchLevel;
77
78 public:
79 LocalQRDecompFunctor(NspType fineNS_, NspType coarseNS_, aggRowsType aggRows_, maxAggDofSizeType maxAggDofSize_, agg2RowMapLOType agg2RowMapLO_, statusType statusAtomic_, rowsType rows_, rowsAuxType rowsAux_, colsAuxType colsAux_, valsAuxType valsAux_, bool doQRStep_, int scratchLevel_)
80 : fineNS(fineNS_)
81 , coarseNS(coarseNS_)
82 , aggRows(aggRows_)
83 , maxAggDofSize(maxAggDofSize_)
84 , agg2RowMapLO(agg2RowMapLO_)
85 , statusAtomic(statusAtomic_)
86 , rows(rows_)
87 , rowsAux(rowsAux_)
88 , colsAux(colsAux_)
89 , valsAux(valsAux_)
90 , doQRStep(doQRStep_)
91 , scratchLevel(scratchLevel_) {}
92
93 KOKKOS_INLINE_FUNCTION
94 void operator()(const typename Kokkos::TeamPolicy<execution_space>::member_type& thread, size_t& nnz) const {
95 auto agg = thread.league_rank();
96
97 const auto aggOffset = aggRows(agg);
98 // size of aggregate: number of DOFs in aggregate
99 const auto aggSize = aggRows(agg + 1) - aggOffset;
100
101 const impl_SC one = impl_ATS::one();
102 const impl_SC zero = impl_ATS::zero();
103
104 const int m = aggSize;
105 const int n = fineNS.extent(1);
106
107 // calculate row offset for coarse nullspace
108 Xpetra::global_size_t offset = agg * n;
109
110 if (doQRStep) {
111 // A is m x n
112 // Q is m x m
113 // R is m x n
114
115 // A (initially) gets overwritten with R in QR
116 shared_matrix r(thread.team_scratch(scratchLevel), m, n);
117 // Q
118 shared_matrix q(thread.team_scratch(scratchLevel), m, m);
119
120 // Extract the piece of the nullspace corresponding to the aggregate
121 for (int j = 0; j < n; j++)
122 for (int k = 0; k < m; k++)
123 r(k, j) = fineNS(agg2RowMapLO(aggOffset + k), j);
124
125 if (m >= n) {
126 // tau has size n
127 shared_vector tau(thread.team_scratch(scratchLevel), n);
128
129 // work has size m
130 shared_vector work(thread.team_scratch(scratchLevel), m);
131
132 // Calculate QR. After this call R will be in the upper triangular part of r.
133 // Q is in encoded in the strictly lower triangular part of r and tau. We will
134 // need to explicitly form Q as a matrix below.
135 KokkosBatched::SerialQR<KokkosBlas::Algo::QR::Unblocked>::invoke(r, tau, work);
136
137 // Initialize Q to an identity matrix
138 KokkosBatched::SerialSetIdentity::invoke(q);
139
140 // Form Q as a matrix but multiplying against the identity
141 KokkosBatched::SerialApplyQ<KokkosBatched::Side::Left, KokkosBlas::Trans::NoTranspose, KokkosBlas::Algo::ApplyQ::Unblocked>::invoke(r, tau, q, work);
142
143 // Build coarse nullspace using the upper triangular part of R
144 for (int j = 0; j < n; j++) {
145 for (int k = 0; k < n; k++)
146 coarseNS(offset + k, j) = (k <= j) ? r(k, j) : zero;
147 }
148
149 // Process each row in the local Q factor and fill helper arrays to assemble P
150 for (int j = 0; j < m; j++) {
151 LO localRow = agg2RowMapLO(aggRows(agg) + j);
152 size_t rowStart = rowsAux(localRow);
153 size_t lnnz = 0;
154 for (int k = 0; k < n; k++) {
155 // skip zeros
156 if (q(j, k) != zero) {
157 colsAux(rowStart + lnnz) = offset + k;
158 valsAux(rowStart + lnnz) = q(j, k);
159 lnnz++;
160 }
161 }
162 rows(localRow + 1) = lnnz;
163 nnz += lnnz;
164 }
165
166 } else {
167 // Special handling for m < n (i.e. single node aggregates in structural mechanics)
168
169 // The local QR decomposition is not possible in the "overconstrained"
170 // case (i.e. number of columns in qr > number of rowsAux), which
171 // corresponds to #DOFs in Aggregate < n. For usual problems this
172 // is only possible for single node aggregates in structural mechanics.
173 // (Similar problems may arise in discontinuous Galerkin problems...)
174 // We bypass the QR decomposition and use an identity block in the
175 // tentative prolongator for the single node aggregate and transfer the
176 // corresponding fine level null space information 1-to-1 to the coarse
177 // level null space part.
178
179 // NOTE: The resulting tentative prolongation operator has
180 // (m*DofsPerNode-n) zero columns leading to a singular
181 // coarse level operator A. To deal with that one has the following
182 // options:
183 // - Use the "RepairMainDiagonal" flag in the RAPFactory (default:
184 // false) to add some identity block to the diagonal of the zero rowsAux
185 // in the coarse level operator A, such that standard level smoothers
186 // can be used again.
187 // - Use special (projection-based) level smoothers, which can deal
188 // with singular matrices (very application specific)
189 // - Adapt the code below to avoid zero columns. However, we do not
190 // support a variable number of DOFs per node in MueLu/Xpetra which
191 // makes the implementation really hard.
192 //
193 // FIXME: do we need to check for singularity here somehow? Zero
194 // columns would be easy but linear dependency would require proper QR.
195
196 // R = extended (by adding identity rowsAux) qr
197 for (int j = 0; j < n; j++)
198 for (int k = 0; k < n; k++)
199 if (k < m)
200 coarseNS(offset + k, j) = r(k, j);
201 else
202 coarseNS(offset + k, j) = (k == j ? one : zero);
203
204 // Q = I (rectangular)
205 // no need to actually fill q
206
207 // Process each row in the local Q factor and fill helper arrays to assemble P
208 for (int j = 0; j < m; j++) {
209 LO localRow = agg2RowMapLO(aggRows(agg) + j);
210 size_t rowStart = rowsAux(localRow);
211 colsAux(rowStart) = offset + j;
212 valsAux(rowStart) = one;
213 rows(localRow + 1) = 1;
214 nnz += 1;
215 }
216 }
217 } else {
219 // "no-QR" option //
221 // Local Q factor is just the fine nullspace support over the current aggregate.
222 // Local R factor is the identity.
223 // TODO I have not implemented any special handling for aggregates that are too
224 // TODO small to locally support the nullspace, as is done in the standard QR
225 // TODO case above.
226
227 for (int j = 0; j < m; j++) {
228 LO localRow = agg2RowMapLO(aggRows(agg) + j);
229 size_t rowStart = rowsAux(localRow);
230 size_t lnnz = 0;
231 for (int k = 0; k < n; k++) {
232 const impl_SC qr_jk = fineNS(localRow, k);
233 // skip zeros
234 if (qr_jk != zero) {
235 colsAux(rowStart + lnnz) = offset + k;
236 valsAux(rowStart + lnnz) = qr_jk;
237 lnnz++;
238 }
239 }
240 rows(localRow + 1) = lnnz;
241 nnz += lnnz;
242 }
243
244 for (int j = 0; j < n; j++)
245 coarseNS(offset + j, j) = one;
246 }
247 }
248};
249
250#else
251
252template <class LOType, class GOType, class SCType, class DeviceType, class NspType, class aggRowsType, class maxAggDofSizeType, class agg2RowMapLOType, class statusType, class rowsType, class rowsAuxType, class colsAuxType, class valsAuxType>
254 private:
255 typedef LOType LO;
256 typedef GOType GO;
257 typedef SCType SC;
258
259 typedef typename DeviceType::execution_space execution_space;
260 typedef typename KokkosKernels::ArithTraits<SC>::val_type impl_SC;
261 typedef KokkosKernels::ArithTraits<impl_SC> impl_ATS;
262 typedef typename impl_ATS::magnitudeType Magnitude;
263
264 public:
265 typedef Kokkos::View<impl_SC**, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged> shared_matrix;
266 typedef Kokkos::View<impl_SC*, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged> shared_vector;
267
268 private:
269 NspType fineNS;
270 NspType coarseNS;
271 aggRowsType aggRows;
272 maxAggDofSizeType maxAggDofSize; //< maximum number of dofs in aggregate (max size of aggregate * numDofsPerNode)
273 agg2RowMapLOType agg2RowMapLO;
274 statusType statusAtomic;
275 rowsType rows;
276 rowsAuxType rowsAux;
277 colsAuxType colsAux;
278 valsAuxType valsAux;
281
282 public:
283 LocalQRDecompFunctor(NspType fineNS_, NspType coarseNS_, aggRowsType aggRows_, maxAggDofSizeType maxAggDofSize_, agg2RowMapLOType agg2RowMapLO_, statusType statusAtomic_, rowsType rows_, rowsAuxType rowsAux_, colsAuxType colsAux_, valsAuxType valsAux_, bool doQRStep_, int scratchLevel_)
284 : fineNS(fineNS_)
285 , coarseNS(coarseNS_)
286 , aggRows(aggRows_)
287 , maxAggDofSize(maxAggDofSize_)
288 , agg2RowMapLO(agg2RowMapLO_)
289 , statusAtomic(statusAtomic_)
290 , rows(rows_)
291 , rowsAux(rowsAux_)
292 , colsAux(colsAux_)
293 , valsAux(valsAux_)
294 , doQRStep(doQRStep_)
295 , scratchLevel(scratchLevel_) {}
296
297 KOKKOS_INLINE_FUNCTION
298 void operator()(const typename Kokkos::TeamPolicy<execution_space>::member_type& thread, size_t& nnz) const {
299 auto agg = thread.league_rank();
300
301 // size of aggregate: number of DOFs in aggregate
302 auto aggSize = aggRows(agg + 1) - aggRows(agg);
303
304 const impl_SC one = impl_ATS::one();
305 const impl_SC two = one + one;
306 const impl_SC zero = impl_ATS::zero();
307 const auto zeroM = impl_ATS::magnitude(zero);
308
309 int m = aggSize;
310 int n = fineNS.extent(1);
311
312 // calculate row offset for coarse nullspace
313 Xpetra::global_size_t offset = agg * n;
314
315 if (doQRStep) {
316 // Extract the piece of the nullspace corresponding to the aggregate
317 shared_matrix r(thread.team_scratch(scratchLevel), m, n); // A (initially), R (at the end)
318 for (int j = 0; j < n; j++)
319 for (int k = 0; k < m; k++)
320 r(k, j) = fineNS(agg2RowMapLO(aggRows(agg) + k), j);
321#if 0
322 printf("A\n");
323 for (int i = 0; i < m; i++) {
324 for (int j = 0; j < n; j++)
325 printf(" %5.3lf ", r(i,j));
326 printf("\n");
327 }
328#endif
329
330 // Calculate QR decomposition (standard)
331 shared_matrix q(thread.team_scratch(scratchLevel), m, m); // Q
332 if (m >= n) {
333 bool isSingular = false;
334
335 // Initialize Q^T
336 auto qt = q;
337 for (int i = 0; i < m; i++) {
338 for (int j = 0; j < m; j++)
339 qt(i, j) = zero;
340 qt(i, i) = one;
341 }
342
343 for (int k = 0; k < n; k++) { // we ignore "n" instead of "n-1" to normalize
344 // FIXME_KOKKOS: use team
345 Magnitude s = zeroM, norm, norm_x;
346 for (int i = k + 1; i < m; i++)
347 s += pow(impl_ATS::magnitude(r(i, k)), 2);
348 norm = sqrt(pow(impl_ATS::magnitude(r(k, k)), 2) + s);
349
350 if (norm == zero) {
351 isSingular = true;
352 break;
353 }
354
355 r(k, k) -= norm * one;
356
357 norm_x = sqrt(pow(impl_ATS::magnitude(r(k, k)), 2) + s);
358 if (norm_x == zeroM) {
359 // We have a single diagonal element in the column.
360 // No reflections required. Just need to restor r(k,k).
361 r(k, k) = norm * one;
362 continue;
363 }
364
365 // FIXME_KOKKOS: use team
366 for (int i = k; i < m; i++)
367 r(i, k) /= norm_x;
368
369 // Update R(k:m,k+1:n)
370 for (int j = k + 1; j < n; j++) {
371 // FIXME_KOKKOS: use team in the loops
372 impl_SC si = zero;
373 for (int i = k; i < m; i++)
374 si += r(i, k) * r(i, j);
375 for (int i = k; i < m; i++)
376 r(i, j) -= two * si * r(i, k);
377 }
378
379 // Update Q^T (k:m,k:m)
380 for (int j = k; j < m; j++) {
381 // FIXME_KOKKOS: use team in the loops
382 impl_SC si = zero;
383 for (int i = k; i < m; i++)
384 si += r(i, k) * qt(i, j);
385 for (int i = k; i < m; i++)
386 qt(i, j) -= two * si * r(i, k);
387 }
388
389 // Fix R(k:m,k)
390 r(k, k) = norm * one;
391 for (int i = k + 1; i < m; i++)
392 r(i, k) = zero;
393 }
394
395#if 0
396 // Q = (Q^T)^T
397 for (int i = 0; i < m; i++)
398 for (int j = 0; j < i; j++) {
399 impl_SC tmp = qt(i,j);
400 qt(i,j) = qt(j,i);
401 qt(j,i) = tmp;
402 }
403#endif
404
405 // Build coarse nullspace using the upper triangular part of R
406 for (int j = 0; j < n; j++)
407 for (int k = 0; k <= j; k++)
408 coarseNS(offset + k, j) = r(k, j);
409
410 if (isSingular) {
411 statusAtomic(1) = true;
412 return;
413 }
414
415 } else {
416 // Special handling for m < n (i.e. single node aggregates in structural mechanics)
417
418 // The local QR decomposition is not possible in the "overconstrained"
419 // case (i.e. number of columns in qr > number of rowsAux), which
420 // corresponds to #DOFs in Aggregate < n. For usual problems this
421 // is only possible for single node aggregates in structural mechanics.
422 // (Similar problems may arise in discontinuous Galerkin problems...)
423 // We bypass the QR decomposition and use an identity block in the
424 // tentative prolongator for the single node aggregate and transfer the
425 // corresponding fine level null space information 1-to-1 to the coarse
426 // level null space part.
427
428 // NOTE: The resulting tentative prolongation operator has
429 // (m*DofsPerNode-n) zero columns leading to a singular
430 // coarse level operator A. To deal with that one has the following
431 // options:
432 // - Use the "RepairMainDiagonal" flag in the RAPFactory (default:
433 // false) to add some identity block to the diagonal of the zero rowsAux
434 // in the coarse level operator A, such that standard level smoothers
435 // can be used again.
436 // - Use special (projection-based) level smoothers, which can deal
437 // with singular matrices (very application specific)
438 // - Adapt the code below to avoid zero columns. However, we do not
439 // support a variable number of DOFs per node in MueLu/Xpetra which
440 // makes the implementation really hard.
441 //
442 // FIXME: do we need to check for singularity here somehow? Zero
443 // columns would be easy but linear dependency would require proper QR.
444
445 // R = extended (by adding identity rowsAux) qr
446 for (int j = 0; j < n; j++)
447 for (int k = 0; k < n; k++)
448 if (k < m)
449 coarseNS(offset + k, j) = r(k, j);
450 else
451 coarseNS(offset + k, j) = (k == j ? one : zero);
452
453 // Q = I (rectangular)
454 for (int i = 0; i < m; i++)
455 for (int j = 0; j < n; j++)
456 q(i, j) = (j == i ? one : zero);
457 }
458
459 // Process each row in the local Q factor and fill helper arrays to assemble P
460 for (int j = 0; j < m; j++) {
461 LO localRow = agg2RowMapLO(aggRows(agg) + j);
462 size_t rowStart = rowsAux(localRow);
463 size_t lnnz = 0;
464 for (int k = 0; k < n; k++) {
465 // skip zeros
466 if (q(j, k) != zero) {
467 colsAux(rowStart + lnnz) = offset + k;
468 valsAux(rowStart + lnnz) = q(j, k);
469 lnnz++;
470 }
471 }
472 rows(localRow + 1) = lnnz;
473 nnz += lnnz;
474 }
475
476#if 0
477 printf("R\n");
478 for (int i = 0; i < m; i++) {
479 for (int j = 0; j < n; j++)
480 printf(" %5.3lf ", coarseNS(i,j));
481 printf("\n");
482 }
483
484 printf("Q\n");
485 for (int i = 0; i < aggSize; i++) {
486 for (int j = 0; j < aggSize; j++)
487 printf(" %5.3lf ", q(i,j));
488 printf("\n");
489 }
490#endif
491 } else {
493 // "no-QR" option //
495 // Local Q factor is just the fine nullspace support over the current aggregate.
496 // Local R factor is the identity.
497 // TODO I have not implemented any special handling for aggregates that are too
498 // TODO small to locally support the nullspace, as is done in the standard QR
499 // TODO case above.
500
501 for (int j = 0; j < m; j++) {
502 LO localRow = agg2RowMapLO(aggRows(agg) + j);
503 size_t rowStart = rowsAux(localRow);
504 size_t lnnz = 0;
505 for (int k = 0; k < n; k++) {
506 const impl_SC qr_jk = fineNS(localRow, k);
507 // skip zeros
508 if (qr_jk != zero) {
509 colsAux(rowStart + lnnz) = offset + k;
510 valsAux(rowStart + lnnz) = qr_jk;
511 lnnz++;
512 }
513 }
514 rows(localRow + 1) = lnnz;
515 nnz += lnnz;
516 }
517
518 for (int j = 0; j < n; j++)
519 coarseNS(offset + j, j) = one;
520 }
521 }
522};
523#endif
524
525} // namespace MueLu::LocalQR
526
527#endif
MueLu::DefaultLocalOrdinal LocalOrdinal
Kokkos::View< impl_SC **, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged > shared_matrix
LocalQRDecompFunctor(NspType fineNS_, NspType coarseNS_, aggRowsType aggRows_, maxAggDofSizeType maxAggDofSize_, agg2RowMapLOType agg2RowMapLO_, statusType statusAtomic_, rowsType rows_, rowsAuxType rowsAux_, colsAuxType colsAux_, valsAuxType valsAux_, bool doQRStep_, int scratchLevel_)
KokkosKernels::ArithTraits< impl_SC > impl_ATS
DeviceType::execution_space execution_space
Kokkos::View< impl_SC *, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged > shared_vector
KokkosKernels::ArithTraits< SC >::val_type impl_SC
KOKKOS_INLINE_FUNCTION void operator()(const typename Kokkos::TeamPolicy< execution_space >::member_type &thread, size_t &nnz) const
KOKKOS_INLINE_FUNCTION void join(LocalOrdinal &dst, const LocalOrdinal &src) const
KOKKOS_INLINE_FUNCTION void init(LocalOrdinal &dst) const
KOKKOS_INLINE_FUNCTION void operator()(const LocalOrdinal &i, LocalOrdinal &vmax) const