version GIT relicensing-7278-g091132d5eb 2026-12-02 03:10:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
solver_gmres.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2024 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
16
18
19#include <Kokkos_Core.hpp>
20
21#include <algorithm>
22
24
25namespace internal
26{
27 namespace SolverGMRESImplementation
28 {
35 template <int block_size, bool delayed_reorthogonalization>
36 struct BlockSums
37 {
38 double dot[block_size];
39 double correct[delayed_reorthogonalization ? block_size : 1];
40 double norm_sqr;
41
42 KOKKOS_INLINE_FUNCTION
44 {
45 for (int b = 0; b < block_size; ++b)
46 dot[b] = 0.;
47 for (int b = 0; b < (delayed_reorthogonalization ? block_size : 1); ++b)
48 correct[b] = 0.;
49 norm_sqr = 0.;
50 }
51
52 KOKKOS_INLINE_FUNCTION BlockSums &
53 operator+=(const BlockSums &other)
54 {
55 for (int b = 0; b < block_size; ++b)
56 dot[b] += other.dot[b];
57 if constexpr (delayed_reorthogonalization)
58 for (int b = 0; b < block_size; ++b)
59 correct[b] += other.correct[b];
60 norm_sqr += other.norm_sqr;
61 return *this;
62 }
63 };
64
65
66
75 template <int block_size, bool delayed_reorthogonalization>
77 {
81 Kokkos::View<value_type,
82 Kokkos::HostSpace,
83 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
84
85 KOKKOS_INLINE_FUNCTION
87 : value(&result)
88 {}
89
90 KOKKOS_INLINE_FUNCTION void
91 join(value_type &dest, const value_type &src) const
92 {
93 dest += src;
94 }
95
96 KOKKOS_INLINE_FUNCTION void
97 init(value_type &val) const
98 {
99 val = value_type();
100 }
101
102 KOKKOS_INLINE_FUNCTION value_type &
103 reference() const
104 {
105 return *value;
106 }
107
108 KOKKOS_INLINE_FUNCTION result_view_type
109 view() const
110 {
111 return result_view_type(value);
112 }
113
114 KOKKOS_INLINE_FUNCTION bool
116 {
117 return true;
118 }
119
120 private:
122 };
123
124
125
126 template <bool delayed_reorthogonalization, typename Number>
127 void
128 do_Tvmult_add_host(const unsigned int n_vectors,
129 const std::size_t locally_owned_size,
130 const Number *current_vector,
133 {
134 unsigned int j = 0;
135 const auto &orthogonal_vectors = temp_data.vector_ptrs;
136
137 if (n_vectors <= 128)
138 {
139 // optimized path
140 static constexpr unsigned int n_lanes =
142
144 for (unsigned int i = 0; i < n_vectors; ++i)
145 hs[i] = 0.0;
147 correct[delayed_reorthogonalization ? 129 : 1];
148 if (delayed_reorthogonalization)
149 for (unsigned int i = 0; i < n_vectors + 1; ++i)
150 correct[i] = 0.0;
151
152 unsigned int c = 0;
153
154 constexpr unsigned int inner_batch_size =
155 delayed_reorthogonalization ? 6 : 12;
156
157 const unsigned int loop_length_c =
158 locally_owned_size / n_lanes / inner_batch_size;
159
160 // At this point, we would like to run a loop over the variable 'c'
161 // from 0 all the way to loop_length_c, and then run a loop over 'i'
162 // to work on all vectors we want to compute the inner product
163 // against. In other words, the loop layout would be:
164 //
165 // for (unsigned int c = 0; c < loop_length_c; ++c)
166 // {
167 // // do some work that only depends on the index c or the
168 // // derived index j = c * n_lanes * inner_batch_size
169 // ...
170 // for (unsigned int i = 0; i < n_vectors - 1; ++i)
171 // {
172 // // do the work with orthonormal_vectors[i][j] and
173 // // current_vector[j] to fill the summation variables
174 // }
175 // }
176 //
177 // However, this access pattern leads to relatively poor memory
178 // behavior with low performance because we would access only a few
179 // entries of n_vectors 'orthonormal_vectors[i][j]' for an index j
180 // at a time, before we pass on to the next vector i. More
181 // precisely, we access inner_batch_size * n_lanes many entries
182 // before moving on to the next vector. Modern CPUs derive a good
183 // deal of performance from so-called hardware prefetching, which is
184 // a mechanism that speculatively initiates loads from main memory
185 // that the hardware guesses will be accessed soon, in order to
186 // reduce the waiting time once the access actually happens. This
187 // data is put into fast cache memory in the meantime. Prefetching
188 // gets typically initiated when we access the entries of an array
189 // consecutively, or also when looping over the data elements of a
190 // few vectors at the same time. However, for more than around 10
191 // vectors looped over simultaneously, the hardware gets to see too
192 // many streams and the capacity of the loop stream detectors gets
193 // exceeded. As a result, the hardware will first initiate a load
194 // when the entry is actually requested by the respective code with
195 // that loop index. As it takes many clock cycles for data to arrive
196 // from memory (on the order of 200-500 clock cycles on 2024
197 // hardware, whereas caches can deliver data in 4-20 cycles and
198 // computations can be done in 3-6 cycles), and since even CPUs with
199 // good out-of-order execution capabilities can issue only a limited
200 // number of loads, the memory interface will become under-utilized,
201 // which is counter-intuitive for a loop we expect to be very
202 // memory-bandwidth heavy.
203 //
204 // The solution is to perform so-called loop blocking, see, e.g.,
205 // https://en.wikipedia.org/wiki/Loop_nest_optimization - we split
206 // the loop over the variable i into tiles (or blocks) of size 8 to
207 // make sure the hardware prefetchers are able to follow all 8
208 // streams, using a variable called 'i_block', and then run the loop
209 // over 'c' inside. Since we do not want to re-load the entries of
210 // 'current_vector' every time we work on blocks of size 8 for the
211 // 'i' variable, but want to make sure to obtained it from faster
212 // cache memory, we also tile the loop over 'c' into blocks. These
213 // blocks have size 64, which is big enough to get most of the
214 // effect of prefetchers (64 * inner_block_size * n_lanes is already
215 // several kB of data) but small enough for 'current_vector' to
216 // still be in fast cache memory for the next round of
217 // 'i_block'. The end result are thus three nested loops visible
218 // here, over 'c_block', 'i_block', and 'c', and an inner loop over
219 // i and the inner batch size for the actual work. Not the prettiest
220 // code, but giving adequate performance.
221 for (unsigned int c_block = 0; c_block < (loop_length_c + 63) / 64;
222 ++c_block)
223 for (unsigned int i_block = 0; i_block < (n_vectors + 7) / 8;
224 ++i_block)
225 for (c = c_block * 64, j = c * n_lanes * inner_batch_size;
226 c < std::min(loop_length_c, (c_block + 1) * 64);
227 ++c, j += n_lanes * inner_batch_size)
228 {
229 VectorizedArray<double> vvec[inner_batch_size];
230 for (unsigned int k = 0; k < inner_batch_size; ++k)
231 vvec[k].load(current_vector + j + k * n_lanes);
232 VectorizedArray<double> prev_vector[inner_batch_size];
233 if (delayed_reorthogonalization || i_block == 0)
234 for (unsigned int k = 0; k < inner_batch_size; ++k)
235 prev_vector[k].load(orthogonal_vectors[n_vectors - 1] +
236 j + k * n_lanes);
237
238 if (i_block == 0)
239 {
240 VectorizedArray<double> local_sum_0 =
241 prev_vector[0] * vvec[0];
242 VectorizedArray<double> local_sum_1 =
243 prev_vector[0] * prev_vector[0];
244 VectorizedArray<double> local_sum_2 = vvec[0] * vvec[0];
245 for (unsigned int k = 1; k < inner_batch_size; ++k)
246 {
247 local_sum_0 += prev_vector[k] * vvec[k];
248 if (delayed_reorthogonalization)
249 {
250 local_sum_1 += prev_vector[k] * prev_vector[k];
251 local_sum_2 += vvec[k] * vvec[k];
252 }
253 }
254 hs[n_vectors - 1] += local_sum_0;
255 if (delayed_reorthogonalization)
256 {
257 correct[n_vectors - 1] += local_sum_1;
258 correct[n_vectors] += local_sum_2;
259 }
260 }
261
262 for (unsigned int i = i_block * 8;
263 i < std::min(n_vectors - 1, (i_block + 1) * 8);
264 ++i)
265 {
266 // break the dependency chain into the field hs[i] for
267 // small sizes i by first accumulating 6 or 12 results
268 // into a local variable
270 temp.load(orthogonal_vectors[i] + j);
271 VectorizedArray<double> local_sum_0 = temp * vvec[0];
272 VectorizedArray<double> local_sum_1 =
273 delayed_reorthogonalization ? temp * prev_vector[0] :
274 0.;
275 for (unsigned int k = 1; k < inner_batch_size; ++k)
276 {
277 temp.load(orthogonal_vectors[i] + j + k * n_lanes);
278 local_sum_0 += temp * vvec[k];
279 if (delayed_reorthogonalization)
280 local_sum_1 += temp * prev_vector[k];
281 }
282 hs[i] += local_sum_0;
283 if (delayed_reorthogonalization)
284 correct[i] += local_sum_1;
285 }
286 }
287
288 c *= inner_batch_size;
289 for (; c < locally_owned_size / n_lanes; ++c, j += n_lanes)
290 {
291 VectorizedArray<double> vvec, prev_vector;
292 vvec.load(current_vector + j);
293 prev_vector.load(orthogonal_vectors[n_vectors - 1] + j);
294 hs[n_vectors - 1] += prev_vector * vvec;
295 if (delayed_reorthogonalization)
296 {
297 correct[n_vectors - 1] += prev_vector * prev_vector;
298 correct[n_vectors] += vvec * vvec;
299 }
300
301 for (unsigned int i = 0; i < n_vectors - 1; ++i)
302 {
304 temp.load(orthogonal_vectors[i] + j);
305 hs[i] += temp * vvec;
306 if (delayed_reorthogonalization)
307 correct[i] += temp * prev_vector;
308 }
309 }
310
311 for (unsigned int i = 0; i < n_vectors; ++i)
312 {
313 h(i) += hs[i].sum();
314 if (delayed_reorthogonalization)
315 h(i + n_vectors) += correct[i].sum();
316 }
317 if (delayed_reorthogonalization)
318 h(n_vectors + n_vectors) += correct[n_vectors].sum();
319 }
320
321 // remainder loop of optimized path or non-optimized path (if
322 // n>128)
323 for (; j < locally_owned_size; ++j)
324 {
325 const double vvec = current_vector[j];
326 const double prev_vector = orthogonal_vectors[n_vectors - 1][j];
327 h(n_vectors - 1) += prev_vector * vvec;
328 if (delayed_reorthogonalization)
329 {
330 h(n_vectors + n_vectors - 1) += prev_vector * prev_vector;
331 h(n_vectors + n_vectors) += vvec * vvec;
332 }
333 for (unsigned int i = 0; i < n_vectors - 1; ++i)
334 {
335 const double temp = orthogonal_vectors[i][j];
336 h(i) += temp * vvec;
337 if (delayed_reorthogonalization)
338 h(n_vectors + i) += temp * prev_vector;
339 }
340 }
341 }
342
343
344
345 template <bool delayed_reorthogonalization, typename Number>
346 double
347 do_subtract_and_norm_host(const unsigned int n_vectors,
348 const std::size_t locally_owned_size,
350 const Vector<double> &h,
351 Number *current_vector)
352 {
353 const auto &orthogonal_vectors = temp_data.vector_ptrs;
354 double norm_vv_temp = 0;
355
356 Number *previous_vector =
357 const_cast<Number *>(orthogonal_vectors[n_vectors - 1]);
358 const double inverse_norm_previous =
359 delayed_reorthogonalization ? 1. / h(n_vectors + n_vectors - 1) : 0.;
360 const double scaling_factor_vv =
361 delayed_reorthogonalization ?
362 (h(n_vectors + n_vectors) > 0.0 ?
363 inverse_norm_previous / h(n_vectors + n_vectors) :
364 inverse_norm_previous / h(n_vectors + n_vectors - 1)) :
365 0.;
366 const double last_factor = h(n_vectors - 1);
367
368 VectorizedArray<double> norm_vv_temp_vectorized = 0.0;
369
370 static constexpr unsigned int n_lanes = VectorizedArray<double>::size();
371 constexpr unsigned int inner_batch_size =
372 delayed_reorthogonalization ? 6 : 12;
373
374 // As for the do_Tvmult_add loop above, we perform loop blocking on both
375 // the 'i' and 'c' variable to help hardware prefetchers to perform
376 // adequately, and get three nested loops here plus the inner loops. See
377 // the extensive comments above for the full rationale.
378 unsigned int j = 0;
379 unsigned int c = 0;
380 const unsigned int loop_length_c =
381 locally_owned_size / n_lanes / inner_batch_size;
382 const unsigned int loop_length_i = (n_vectors + 7) / 8;
383 for (unsigned int c_block = 0; c_block < (loop_length_c + 63) / 64;
384 ++c_block)
385 for (unsigned int i_block = 0; i_block < (n_vectors + 7) / 8; ++i_block)
386 for (c = c_block * 64, j = c * n_lanes * inner_batch_size;
387 c < std::min(loop_length_c, (c_block + 1) * 64);
388 ++c, j += n_lanes * inner_batch_size)
389 {
390 VectorizedArray<double> temp[inner_batch_size];
391 for (unsigned int k = 0; k < inner_batch_size; ++k)
392 temp[k].load(current_vector + j + k * n_lanes);
393 VectorizedArray<double> prev_vector[inner_batch_size];
394 if (delayed_reorthogonalization)
395 for (unsigned int k = 0; k < inner_batch_size; ++k)
396 prev_vector[k].load(previous_vector + j + k * n_lanes);
397
398 for (unsigned int i = i_block * 8;
399 i < std::min(n_vectors - 1, (i_block + 1) * 8);
400 ++i)
401 {
402 const double factor = h(i);
403 const double correction_factor =
404 (delayed_reorthogonalization ? h(n_vectors + i) : 0.0);
405 for (unsigned int k = 0; k < inner_batch_size; ++k)
406 {
408 vec.load(orthogonal_vectors[i] + j + k * n_lanes);
409 temp[k] -= factor * vec;
410 if (delayed_reorthogonalization)
411 prev_vector[k] -= correction_factor * vec;
412 }
413 }
414
415 if (delayed_reorthogonalization)
416 {
417 if (i_block + 1 == loop_length_i)
418 for (unsigned int k = 0; k < inner_batch_size; ++k)
419 {
420 prev_vector[k] = prev_vector[k] * inverse_norm_previous;
421 prev_vector[k].store(previous_vector + j + k * n_lanes);
422 temp[k] -= last_factor * prev_vector[k];
423 temp[k] = temp[k] * scaling_factor_vv;
424 temp[k].store(current_vector + j + k * n_lanes);
425 }
426 else
427 for (unsigned int k = 0; k < inner_batch_size; ++k)
428 {
429 prev_vector[k].store(previous_vector + j + k * n_lanes);
430 temp[k].store(current_vector + j + k * n_lanes);
431 }
432 }
433 else
434 {
435 if (i_block + 1 == loop_length_i)
436 for (unsigned int k = 0; k < inner_batch_size; ++k)
437 {
438 prev_vector[k].load(previous_vector + j + k * n_lanes);
439 temp[k] -= last_factor * prev_vector[k];
440 temp[k].store(current_vector + j + k * n_lanes);
441 norm_vv_temp_vectorized += temp[k] * temp[k];
442 }
443 else
444 for (unsigned int k = 0; k < inner_batch_size; ++k)
445 temp[k].store(current_vector + j + k * n_lanes);
446 }
447 }
448
449 c *= inner_batch_size;
450 for (; c < locally_owned_size / n_lanes; ++c, j += n_lanes)
451 {
452 VectorizedArray<double> temp, prev_vector;
453 temp.load(current_vector + j);
454 prev_vector.load(previous_vector + j);
455 if (!delayed_reorthogonalization)
456 temp -= h(n_vectors - 1) * prev_vector;
457
458 for (unsigned int i = 0; i < n_vectors - 1; ++i)
459 {
461 vec.load(orthogonal_vectors[i] + j);
462 temp -= h(i) * vec;
463 if (delayed_reorthogonalization)
464 prev_vector -= h(n_vectors + i) * vec;
465 }
466
467 if (delayed_reorthogonalization)
468 {
469 prev_vector = prev_vector * inverse_norm_previous;
470 prev_vector.store(previous_vector + j);
471 temp -= h(n_vectors - 1) * prev_vector;
472 temp = temp * scaling_factor_vv;
473 temp.store(current_vector + j);
474 }
475 else
476 {
477 temp.store(current_vector + j);
478 norm_vv_temp_vectorized += temp * temp;
479 }
480 }
481
482 if (!delayed_reorthogonalization)
483 norm_vv_temp += norm_vv_temp_vectorized.sum();
484
485 for (; j < locally_owned_size; ++j)
486 {
487 double temp = current_vector[j];
488 double prev_vector = previous_vector[j];
489 if (delayed_reorthogonalization)
490 {
491 for (unsigned int i = 0; i < n_vectors - 1; ++i)
492 {
493 const double vec = orthogonal_vectors[i][j];
494 temp -= h(i) * vec;
495 prev_vector -= h(n_vectors + i) * vec;
496 }
497 prev_vector *= inverse_norm_previous;
498 previous_vector[j] = prev_vector;
499 temp -= h(n_vectors - 1) * prev_vector;
500 temp *= scaling_factor_vv;
501 }
502 else
503 {
504 temp -= h(n_vectors - 1) * prev_vector;
505 for (unsigned int i = 0; i < n_vectors - 1; ++i)
506 temp -= h(i) * orthogonal_vectors[i][j];
507 norm_vv_temp += temp * temp;
508 }
509 current_vector[j] = temp;
510 }
511
512 return norm_vv_temp;
513 }
514
515
516
517 template <typename Number>
518 void
519 do_add_host(const unsigned int n_vectors,
520 const std::size_t locally_owned_size,
522 const Vector<double> &h,
523 const bool zero_out,
524 Number *output)
525 {
526 const auto &orthogonal_vectors = temp_data.vector_ptrs;
527
528 static constexpr unsigned int n_lanes = VectorizedArray<double>::size();
529 constexpr unsigned int inner_batch_size = 12;
530
531 unsigned int j = 0;
532 unsigned int c = 0;
533 for (; c < locally_owned_size / n_lanes / inner_batch_size;
534 ++c, j += n_lanes * inner_batch_size)
535 {
536 VectorizedArray<double> temp[inner_batch_size];
537 if (zero_out)
538 for (VectorizedArray<double> &a : temp)
539 a = {};
540 else
541 for (unsigned int k = 0; k < inner_batch_size; ++k)
542 temp[k].load(output + j + k * n_lanes);
543
544 for (unsigned int i = 0; i < n_vectors; ++i)
545 {
546 const double h_i = h(i);
547 for (unsigned int k = 0; k < inner_batch_size; ++k)
548 {
550 v_ij.load(orthogonal_vectors[i] + j + k * n_lanes);
551 temp[k] += v_ij * h_i;
552 }
553 }
554
555 for (unsigned int k = 0; k < inner_batch_size; ++k)
556 temp[k].store(output + j + k * n_lanes);
557 }
558
559 c *= inner_batch_size;
560 for (; c < locally_owned_size / n_lanes; ++c, j += n_lanes)
561 {
562 VectorizedArray<double> temp = {};
563 if (!zero_out)
564 temp.load(output + j);
565
566 for (unsigned int i = 0; i < n_vectors; ++i)
567 {
569 v_ij.load(orthogonal_vectors[i] + j);
570 temp += v_ij * h(i);
571 }
572
573 temp.store(output + j);
574 }
575
576 for (; j < locally_owned_size; ++j)
577 {
578 double temp = zero_out ? 0.0 : output[j];
579 for (unsigned int i = 0; i < n_vectors; ++i)
580 temp += orthogonal_vectors[i][j] * h(i);
581 output[j] = temp;
582 }
583 }
584
585
586
592 template <typename Number>
594 {
595 Number *ptr;
596 };
597
598
599
607 template <typename ViewType>
608 Kokkos::View<typename ViewType::data_type,
609 typename ViewType::memory_space,
610 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
611 leading_entries(const ViewType &view, const std::size_t n)
612 {
613 AssertIndexRange(n, view.extent(0) + 1);
614 return Kokkos::View<typename ViewType::data_type,
615 typename ViewType::memory_space,
616 Kokkos::MemoryTraits<Kokkos::Unmanaged>>(view.data(),
617 n);
618 }
619
620
621
622 template <typename Number>
624 {
626 MemorySpace::Default::kokkos_space::execution_space;
628
636 static constexpr std::size_t rows_per_team = 2048;
637
641 static constexpr int block_size = 8;
642
643 void
645 const std::vector<const Number *> &vector_ptrs)
646 {
647 const std::size_t n_vectors = vector_ptrs.size();
648 if (ptrs.extent(0) < n_vectors)
649 {
650 // grow geometrically to avoid re-allocations as the basis grows
651 ptrs = Kokkos::View<DevicePointer<Number> *, KokkosSpace>(
652 Kokkos::view_alloc(Kokkos::WithoutInitializing,
653 "::gmres_vector_pointers"),
654 std::max(n_vectors, 2 * ptrs.extent(0)));
655 ptrs_host = Kokkos::create_mirror_view(ptrs);
656 }
657 // the pointers are only used for read access, except for the
658 // in-place update of the last vector in the delayed variant
659 for (std::size_t i = 0; i < n_vectors; ++i)
660 ptrs_host(i).ptr = const_cast<Number *>(vector_ptrs[i]);
661 Kokkos::deep_copy(exec,
662 leading_entries(ptrs, n_vectors),
663 leading_entries(ptrs_host, n_vectors));
664 }
665
666 void
668 const std::size_t n_coefficients,
669 const double *h)
670 {
671 if (coefficients.extent(0) < n_coefficients)
672 {
673 coefficients = Kokkos::View<double *, KokkosSpace>(
674 Kokkos::view_alloc(Kokkos::WithoutInitializing,
675 "::gmres_coefficients"),
676 std::max(n_coefficients, 2 * coefficients.extent(0)));
677 coefficients_host = Kokkos::create_mirror_view(coefficients);
678 }
679 for (std::size_t i = 0; i < n_coefficients; ++i)
680 coefficients_host(i) = h[i];
681 Kokkos::deep_copy(exec,
682 leading_entries(coefficients, n_coefficients),
683 leading_entries(coefficients_host, n_coefficients));
684 }
685
686 void
687 reserve_results(const std::size_t n_teams, const std::size_t n_results)
688 {
689 if (partial_sums.extent(0) < n_teams ||
690 partial_sums.extent(1) < n_results)
692 Kokkos::View<double **, Kokkos::LayoutLeft, KokkosSpace>(
693 Kokkos::view_alloc(Kokkos::WithoutInitializing,
694 "::gmres_partial_sums"),
695 std::max(n_teams, partial_sums.extent(0)),
696 std::max(2 * n_results, partial_sums.extent(1)));
697 if (results.extent(0) < n_results)
698 {
699 results = Kokkos::View<double *, KokkosSpace>(
700 Kokkos::view_alloc(Kokkos::WithoutInitializing,
701 "::gmres_results"),
702 2 * n_results);
703 results_host = Kokkos::create_mirror_view(results);
704 }
705 }
706
707 Kokkos::View<DevicePointer<Number> *, KokkosSpace> ptrs;
708 typename Kokkos::View<DevicePointer<Number> *,
709 KokkosSpace>::host_mirror_type ptrs_host;
710
711 Kokkos::View<double *, KokkosSpace> coefficients;
712 typename Kokkos::View<double *, KokkosSpace>::host_mirror_type
714
715 // layout (team, result) with LayoutLeft for coalesced access in the
716 // final reduction over the teams
717 Kokkos::View<double **, Kokkos::LayoutLeft, KokkosSpace> partial_sums;
718
719 Kokkos::View<double *, KokkosSpace> results;
720 typename Kokkos::View<double *, KokkosSpace>::host_mirror_type
722 };
723
724
725
726 template <typename Number>
729 {
730 if (temp_data.device_data == nullptr)
731 temp_data.device_data = std::make_shared<DeviceData<Number>>();
732 return *temp_data.device_data;
733 }
734
735
736
737 template <bool delayed_reorthogonalization, typename Number>
738 void
739 do_Tvmult_add_device(const unsigned int n_vectors,
740 const std::size_t locally_owned_size,
741 const Number *current_vector,
744 {
745 using Data = DeviceData<Number>;
746 constexpr int block_size = Data::block_size;
747 constexpr std::size_t rows_per_team = Data::rows_per_team;
748
749 Assert(n_vectors > 0, ExcInternalError());
750 AssertDimension(temp_data.vector_ptrs.size(), n_vectors);
751
752 // Compute the following quantities, with v_i the orthogonal vectors
753 // and w the current vector:
754 // h(i) += v_i * w for i < n
755 // and for the delayed variant additionally
756 // h(n + i) += v_i * v_{n-1} for i < n
757 // h(2n) += w * w
758 const unsigned int n_results =
759 delayed_reorthogonalization ? 2 * n_vectors + 1 : n_vectors;
760 AssertDimension(h.size(), n_results);
761 const std::size_t n_teams =
762 (locally_owned_size + rows_per_team - 1) / rows_per_team;
763 if (n_teams == 0)
764 return;
765
766 Data &data = get_device_data(temp_data);
767 typename Data::ExecutionSpace exec;
768 data.set_pointers(exec, temp_data.vector_ptrs);
769 data.reserve_results(n_teams, n_results);
770
771 const auto ptrs = data.ptrs;
772 const auto partial_sums = data.partial_sums;
773 const auto results = data.results;
774 const unsigned int n_blocks = (n_vectors + block_size - 1) / block_size;
775 const std::size_t size = locally_owned_size;
776
777 using TeamPolicy = Kokkos::TeamPolicy<typename Data::ExecutionSpace>;
778 using Member = typename TeamPolicy::member_type;
779
780 // Each team works on a tile of the vectors and loops over the
781 // orthogonal vectors in blocks of size block_size, which are kept in
782 // registers. The current vector is re-read from caches for each block.
783 // Partial sums of all teams are written to an intermediate array and
784 // summed in a second kernel, which makes the result deterministic in
785 // contrast to an approach with atomics.
786 Kokkos::parallel_for(
787 "::gmres_multi_dot",
788 TeamPolicy(exec, n_teams, Kokkos::AUTO),
789 KOKKOS_LAMBDA(const Member &team) {
790 const std::size_t team_id = team.league_rank();
791 const std::size_t begin = team_id * rows_per_team;
792
793 // Would like to use Kokkos::min, but we support Kokkos versions
794 // lower than 3.7, so choose low-level 'min' representation.
795 const std::size_t end =
796 (begin + rows_per_team) < size ? (begin + rows_per_team) : size;
797 [[maybe_unused]] const Number *prev = ptrs(n_vectors - 1).ptr;
798
799 for (unsigned int block = 0; block < n_blocks; ++block)
800 {
801 const unsigned int i0 = block * block_size;
802 const unsigned int nb =
803 block_size < (n_vectors - i0) ? block_size : (n_vectors - i0);
804
805 // Pad the last block with a valid pointer (data is in caches,
806 // results are discarded) to keep the inner loop unrolled.
807 const Number *v[block_size];
808 for (int b = 0; b < block_size; ++b)
809 v[b] = ptrs(i0 + (b < static_cast<int>(nb) ? b : 0)).ptr;
810
812 Kokkos::parallel_reduce(
813 Kokkos::TeamThreadRange(team, begin, end),
814 [&](const std::size_t j,
816 const double w_j = current_vector[j];
817 double prev_j = 0.;
818 if constexpr (delayed_reorthogonalization)
819 prev_j = prev[j];
820 for (int b = 0; b < block_size; ++b)
821 {
822 const double v_ij = v[b][j];
823 s.dot[b] += v_ij * w_j;
824 if constexpr (delayed_reorthogonalization)
825 s.correct[b] += v_ij * prev_j;
826 }
827 if (delayed_reorthogonalization && block == 0)
828 s.norm_sqr += w_j * w_j;
829 },
831 sums));
832
833 Kokkos::single(Kokkos::PerTeam(team), [&]() {
834 for (unsigned int b = 0; b < nb; ++b)
835 {
836 partial_sums(team_id, i0 + b) = sums.dot[b];
837 if constexpr (delayed_reorthogonalization)
838 partial_sums(team_id, n_vectors + i0 + b) =
839 sums.correct[b];
840 }
841 if (delayed_reorthogonalization && block == 0)
842 partial_sums(team_id, 2 * n_vectors) = sums.norm_sqr;
843 });
844 }
845 });
846
847 Kokkos::parallel_for(
848 "::gmres_multi_dot_finalize",
849 TeamPolicy(exec, n_results, Kokkos::AUTO),
850 KOKKOS_LAMBDA(const Member &team) {
851 const unsigned int i = team.league_rank();
852 double sum = 0.;
853 Kokkos::parallel_reduce(
854 Kokkos::TeamThreadRange(team, n_teams),
855 [&](const std::size_t t, double &s) { s += partial_sums(t, i); },
856 sum);
857 Kokkos::single(Kokkos::PerTeam(team), [&]() { results(i) = sum; });
858 });
859
860 Kokkos::deep_copy(exec,
861 leading_entries(data.results_host, n_results),
862 leading_entries(results, n_results));
863 exec.fence();
864 for (unsigned int i = 0; i < n_results; ++i)
865 h(i) += data.results_host(i);
866 }
867
868
869
870 template <bool delayed_reorthogonalization, typename Number>
871 double
872 do_subtract_and_norm_device(const unsigned int n_vectors,
873 const std::size_t locally_owned_size,
875 const Vector<double> &h,
876 Number *current_vector)
877 {
878 using Data = DeviceData<Number>;
879 using Policy = Kokkos::RangePolicy<typename Data::ExecutionSpace,
880 Kokkos::IndexType<std::size_t>>;
881
882 Assert(n_vectors > 0, ExcInternalError());
883 AssertDimension(temp_data.vector_ptrs.size(), n_vectors);
885 delayed_reorthogonalization ? 2 * n_vectors + 1 :
886 n_vectors);
887 if (locally_owned_size == 0)
888 return 0.;
889
890 Data &data = get_device_data(temp_data);
891 typename Data::ExecutionSpace exec;
892 data.set_pointers(exec, temp_data.vector_ptrs);
893 data.set_coefficients(exec, h.size(), h.data());
894
895 const auto ptrs = data.ptrs;
896 const auto coeffs = data.coefficients;
897
898 if constexpr (delayed_reorthogonalization)
899 {
900 // Same operations as in do_subtract_and_norm: subtract the
901 // projections from the current vector and the correction terms
902 // from the previous vector, which gets normalized, all in one sweep
903 const double inverse_norm_previous =
904 1. / h(n_vectors + n_vectors - 1);
905 const double scaling_factor_vv =
906 h(n_vectors + n_vectors) > 0.0 ?
907 inverse_norm_previous / h(n_vectors + n_vectors) :
908 inverse_norm_previous / h(n_vectors + n_vectors - 1);
909 const double last_factor = h(n_vectors - 1);
910
911 Kokkos::parallel_for(
912 "::gmres_subtract_delayed",
913 Policy(exec, 0, locally_owned_size),
914 KOKKOS_LAMBDA(const std::size_t j) {
915 Number *previous_vector = ptrs(n_vectors - 1).ptr;
916 double temp = current_vector[j];
917 double prev = previous_vector[j];
918 for (unsigned int i = 0; i + 1 < n_vectors; ++i)
919 {
920 const double vec = ptrs(i).ptr[j];
921 temp -= coeffs(i) * vec;
922 prev -= coeffs(n_vectors + i) * vec;
923 }
924 prev *= inverse_norm_previous;
925 previous_vector[j] = prev;
926 temp -= last_factor * prev;
927 current_vector[j] = temp * scaling_factor_vv;
928 });
929
930 // the norm is computed from other quantities in the delayed
931 // variant, and no synchronization with the device is needed here
932 return 0.;
933 }
934 else
935 {
936 double norm_sqr = 0.;
937 Kokkos::parallel_reduce(
938 "::gmres_subtract_and_norm",
939 Policy(exec, 0, locally_owned_size),
940 KOKKOS_LAMBDA(const std::size_t j, double &sum) {
941 double temp = current_vector[j];
942 for (unsigned int i = 0; i < n_vectors; ++i)
943 temp -= coeffs(i) * ptrs(i).ptr[j];
944 current_vector[j] = temp;
945 sum += temp * temp;
946 },
947 norm_sqr);
948 return norm_sqr;
949 }
950 }
951
952
953
954 template <typename Number>
955 void
956 do_add_device(const unsigned int n_vectors,
957 const std::size_t locally_owned_size,
959 const Vector<double> &h,
960 const bool zero_out,
961 Number *output)
962 {
963 using Data = DeviceData<Number>;
964
965 AssertDimension(temp_data.vector_ptrs.size(), n_vectors);
966 AssertIndexRange(n_vectors, h.size() + 1);
967 if (locally_owned_size == 0 || n_vectors == 0)
968 return;
969
970 Data &data = get_device_data(temp_data);
971 typename Data::ExecutionSpace exec;
972 data.set_pointers(exec, temp_data.vector_ptrs);
973 data.set_coefficients(exec, n_vectors, h.data());
974
975 const auto ptrs = data.ptrs;
976 const auto coeffs = data.coefficients;
977
978 Kokkos::parallel_for(
979 "::gmres_add",
980 Kokkos::RangePolicy<typename Data::ExecutionSpace,
981 Kokkos::IndexType<std::size_t>>(exec,
982 0,
984 KOKKOS_LAMBDA(const std::size_t j) {
985 double temp = zero_out ? 0. : static_cast<double>(output[j]);
986 for (unsigned int i = 0; i < n_vectors; ++i)
987 temp += coeffs(i) * ptrs(i).ptr[j];
988 output[j] = temp;
989 });
990 }
991
992
993
994 template <bool delayed_reorthogonalization,
995 typename MemorySpace,
996 typename Number>
997 void
998 do_Tvmult_add(const unsigned int n_vectors,
999 const std::size_t locally_owned_size,
1000 const Number *current_vector,
1002 Vector<double> &h)
1003 {
1004 if constexpr (std::is_same_v<MemorySpace, ::MemorySpace::Default>)
1005 do_Tvmult_add_device<delayed_reorthogonalization>(
1006 n_vectors, locally_owned_size, current_vector, temp_data, h);
1007 else
1008 do_Tvmult_add_host<delayed_reorthogonalization>(
1009 n_vectors, locally_owned_size, current_vector, temp_data, h);
1010 }
1011
1012
1013
1014 template <bool delayed_reorthogonalization,
1015 typename MemorySpace,
1016 typename Number>
1017 double
1018 do_subtract_and_norm(const unsigned int n_vectors,
1019 const std::size_t locally_owned_size,
1021 const Vector<double> &h,
1022 Number *current_vector)
1023 {
1024 if constexpr (std::is_same_v<MemorySpace, ::MemorySpace::Default>)
1025 return do_subtract_and_norm_device<delayed_reorthogonalization>(
1026 n_vectors, locally_owned_size, temp_data, h, current_vector);
1027 else
1028 return do_subtract_and_norm_host<delayed_reorthogonalization>(
1029 n_vectors, locally_owned_size, temp_data, h, current_vector);
1030 }
1031
1032
1033
1034 template <typename MemorySpace, typename Number>
1035 void
1036 do_add(const unsigned int n_vectors,
1037 const std::size_t locally_owned_size,
1039 const Vector<double> &h,
1040 const bool zero_out,
1041 Number *output)
1042 {
1043 if constexpr (std::is_same_v<MemorySpace, ::MemorySpace::Default>)
1045 n_vectors, locally_owned_size, temp_data, h, zero_out, output);
1046 else
1048 n_vectors, locally_owned_size, temp_data, h, zero_out, output);
1049 }
1050 } // namespace SolverGMRESImplementation
1051} // namespace internal
1052
1053#include "lac/solver_gmres.inst"
1054
*  iterator end()
*  *  iterator begin()
pointer data()
virtual size_type size() const override
Number sum() const
void store(OtherNumber *ptr) const
void load(const OtherNumber *ptr)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
std::vector< index_type > data
Definition mpi.cc:734
std::size_t size
Definition mpi.cc:733
types::global_dof_index locally_owned_size
Definition mpi.cc:821
void do_add(const unsigned int n_vectors, const std::size_t locally_owned_size, OrthogonalizationData< Number > &temp_data, const Vector< double > &h, const bool zero_out, Number *output)
void do_add_host(const unsigned int n_vectors, const std::size_t locally_owned_size, OrthogonalizationData< Number > &temp_data, const Vector< double > &h, const bool zero_out, Number *output)
void do_Tvmult_add_device(const unsigned int n_vectors, const std::size_t locally_owned_size, const Number *current_vector, OrthogonalizationData< Number > &temp_data, Vector< double > &h)
void do_Tvmult_add(const unsigned int n_vectors, const std::size_t locally_owned_size, const Number *current_vector, OrthogonalizationData< Number > &temp_data, Vector< double > &h)
double do_subtract_and_norm_device(const unsigned int n_vectors, const std::size_t locally_owned_size, OrthogonalizationData< Number > &temp_data, const Vector< double > &h, Number *current_vector)
double do_subtract_and_norm(const unsigned int n_vectors, const std::size_t locally_owned_size, OrthogonalizationData< Number > &temp_data, const Vector< double > &h, Number *current_vector)
Kokkos::View< typename ViewType::data_type, typename ViewType::memory_space, Kokkos::MemoryTraits< Kokkos::Unmanaged > > leading_entries(const ViewType &view, const std::size_t n)
double do_subtract_and_norm_host(const unsigned int n_vectors, const std::size_t locally_owned_size, OrthogonalizationData< Number > &temp_data, const Vector< double > &h, Number *current_vector)
void do_Tvmult_add_host(const unsigned int n_vectors, const std::size_t locally_owned_size, const Number *current_vector, OrthogonalizationData< Number > &temp_data, Vector< double > &h)
void do_add_device(const unsigned int n_vectors, const std::size_t locally_owned_size, OrthogonalizationData< Number > &temp_data, const Vector< double > &h, const bool zero_out, Number *output)
DeviceData< Number > & get_device_data(OrthogonalizationData< Number > &temp_data)
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::Kokkos::DefaultExecutionSpace::memory_space kokkos_space
KOKKOS_INLINE_FUNCTION result_view_type view() const
Kokkos::View< value_type, Kokkos::HostSpace, Kokkos::MemoryTraits< Kokkos::Unmanaged > > result_view_type
BlockSums< block_size, delayed_reorthogonalization > value_type
KOKKOS_INLINE_FUNCTION void init(value_type &val) const
KOKKOS_INLINE_FUNCTION void join(value_type &dest, const value_type &src) const
KOKKOS_INLINE_FUNCTION bool references_scalar() const
KOKKOS_INLINE_FUNCTION BlockSumsReducer(value_type &result)
KOKKOS_INLINE_FUNCTION value_type & reference() const
KOKKOS_INLINE_FUNCTION BlockSums & operator+=(const BlockSums &other)
double correct[delayed_reorthogonalization ? block_size :1]
void set_coefficients(const ExecutionSpace &exec, const std::size_t n_coefficients, const double *h)
MemorySpace::Default::kokkos_space::execution_space ExecutionSpace
Kokkos::View< double *, KokkosSpace > coefficients
MemorySpace::Default::kokkos_space KokkosSpace
void reserve_results(const std::size_t n_teams, const std::size_t n_results)
Kokkos::View< double **, Kokkos::LayoutLeft, KokkosSpace > partial_sums
Kokkos::View< DevicePointer< Number > *, KokkosSpace > ptrs
Kokkos::View< double *, KokkosSpace >::host_mirror_type coefficients_host
void set_pointers(const ExecutionSpace &exec, const std::vector< const Number * > &vector_ptrs)
Kokkos::View< double *, KokkosSpace > results
Kokkos::View< double *, KokkosSpace >::host_mirror_type results_host
Kokkos::View< DevicePointer< Number > *, KokkosSpace >::host_mirror_type ptrs_host
std::shared_ptr< DeviceData< Number > > device_data
void prev(std::tuple< I1, I2 > &t)