Debian Codegraph @

include/deal.II/lac/la_parallel_block_vector.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2016 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_parallel_block_vector_templates_h
16 #define dealii_parallel_block_vector_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/lac/la_parallel_block_vector.h>
22 #include <deal.II/lac/lapack_support.h>
23 #include <deal.II/lac/petsc_block_vector.h>
24 #include <deal.II/lac/read_write_vector.h>
25 #include <deal.II/lac/trilinos_parallel_block_vector.h>
26 #include <deal.II/lac/vector.h>
27
28
29 DEAL_II_NAMESPACE_OPEN
30
31 // Forward type declaration to have special treatment of
32 // LAPACKFullMatrix<number>
33 // in multivector_inner_product()
34 #ifndef DOXYGEN
35 template <typename Number>
36 class LAPACKFullMatrix;
37 #endif
38
39 namespace LinearAlgebra
40 {
41 namespace distributed
42 {
43 template <typename Number, typename MemorySpace>
44 BlockVector<Number, MemorySpace>::BlockVector(const size_type n_blocks,
45 const size_type block_size)
46 {
47 reinit(n_blocks, block_size);
48 }
49
50
51
52 template <typename Number, typename MemorySpace>
53 BlockVector<Number, MemorySpace>::BlockVector(
54 const std::vector<size_type> &n)
55 {
56 reinit(n, false);
57 }
58
59
60 template <typename Number, typename MemorySpace>
61 BlockVector<Number, MemorySpace>::BlockVector(
62 const std::vector<IndexSet> &local_ranges,
63 const std::vector<IndexSet> &ghost_indices,
64 const MPI_Comm communicator)
65 {
66 reinit(local_ranges, ghost_indices, communicator);
67 }
68
69
70 template <typename Number, typename MemorySpace>
71 BlockVector<Number, MemorySpace>::BlockVector(
72 const std::vector<IndexSet> &local_ranges,
73 const MPI_Comm communicator)
74 {
75 reinit(local_ranges, communicator);
76 }
77
78
79
80 template <typename Number, typename MemorySpace>
81 BlockVector<Number, MemorySpace>::BlockVector(
82 const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
83 &partitioners,
84 const MPI_Comm &comm_sm)
85 {
86 reinit(partitioners, comm_sm);
87 }
88
89
90
91 template <typename Number, typename MemorySpace>
92 BlockVector<Number, MemorySpace>::BlockVector(
93 const BlockVector<Number, MemorySpace> &v)
94 : BlockVectorBase<Vector<Number, MemorySpace>>()
95 {
96 this->block_indices = v.block_indices;
97
98 this->components.resize(this->n_blocks());
99 for (unsigned int i = 0; i < this->n_blocks(); ++i)
100 this->components[i] = v.components[i];
101
102 this->collect_sizes();
103 }
104
105
106
107 template <typename Number, typename MemorySpace>
108 template <typename OtherNumber>
109 BlockVector<Number, MemorySpace>::BlockVector(
110 const BlockVector<OtherNumber, MemorySpace> &v)
111 {
112 reinit(v, true);
113 *this = v;
114 }
115
116
117
118 template <typename Number, typename MemorySpace>
119 void
120 BlockVector<Number, MemorySpace>::reinit(const size_type n_bl,
121 const size_type bl_sz,
122 const bool omit_zeroing_entries)
123 {
124 std::vector<size_type> n(n_bl, bl_sz);
125 reinit(n, omit_zeroing_entries);
126 }
127
128
129 template <typename Number, typename MemorySpace>
130 void
131 BlockVector<Number, MemorySpace>::reinit(
132 const std::vector<size_type> &block_sizes,
133 const bool omit_zeroing_entries)
134 {
135 this->block_indices.reinit(block_sizes);
136
137 this->components.resize(this->n_blocks());
138 for (unsigned int i = 0; i < this->n_blocks(); ++i)
139 this->components[i].reinit(block_sizes[i], omit_zeroing_entries);
140
141 this->collect_sizes();
142 }
143
144
145
146 template <typename Number, typename MemorySpace>
147 template <typename Number2>
148 void
149 BlockVector<Number, MemorySpace>::reinit(
150 const BlockVector<Number2, MemorySpace> &v,
151 const bool omit_zeroing_entries)
152 {
153 if (this->n_blocks() != v.n_blocks())
154 this->block_indices = v.get_block_indices();
155
156 this->components.resize(this->n_blocks());
157 for (unsigned int i = 0; i < this->n_blocks(); ++i)
158 this->components[i].reinit(v.block(i), omit_zeroing_entries);
159
160 this->collect_sizes();
161 }
162
163
164
165 template <typename Number, typename MemorySpace>
166 void
167 BlockVector<Number, MemorySpace>::reinit(
168 const std::vector<IndexSet> &local_ranges,
169 const std::vector<IndexSet> &ghost_indices,
170 const MPI_Comm communicator)
171 {
172 AssertDimension(local_ranges.size(), ghost_indices.size());
173
174 // update the number of blocks
175 this->block_indices.reinit(local_ranges.size(), 0);
176
177 // initialize each block
178 this->components.resize(this->n_blocks());
179 for (unsigned int i = 0; i < this->n_blocks(); ++i)
180 this->components[i].reinit(local_ranges[i],
181 ghost_indices[i],
182 communicator);
183
184 // update block_indices content
185 this->collect_sizes();
186 }
187
188
189
190 template <typename Number, typename MemorySpace>
191 void
192 BlockVector<Number, MemorySpace>::reinit(
193 const std::vector<IndexSet> &local_ranges,
194 const MPI_Comm communicator)
195 {
196 // update the number of blocks
197 this->block_indices.reinit(local_ranges.size(), 0);
198
199 // initialize each block
200 this->components.resize(this->n_blocks());
201 for (unsigned int i = 0; i < this->n_blocks(); ++i)
202 this->components[i].reinit(local_ranges[i], communicator);
203
204 // update block_indices content
205 this->collect_sizes();
206 }
207
208
209
210 template <typename Number, typename MemorySpace>
211 void
212 BlockVector<Number, MemorySpace>::reinit(
213 const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
214 &partitioners,
215 const MPI_Comm &comm_sm)
216 {
217 // update the number of blocks
218 this->block_indices.reinit(partitioners.size(), 0);
219
220 // initialize each block
221 this->components.resize(this->n_blocks());
222 for (unsigned int i = 0; i < this->n_blocks(); ++i)
223 this->components[i].reinit(partitioners[i], comm_sm);
224
225 // update block_indices content
226 this->collect_sizes();
227 }
228
229
230
231 template <typename Number, typename MemorySpace>
232 void
233 BlockVector<Number, MemorySpace>::reinit(
234 const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
235 &partitioners,
236 const bool /*make_ghosted*/,
237 const MPI_Comm &comm_sm)
238 {
239 reinit(partitioners, comm_sm);
240 }
241
242
243
244 template <typename Number, typename MemorySpace>
245 BlockVector<Number, MemorySpace> &
246 BlockVector<Number, MemorySpace>::operator=(const value_type s)
247 {
248 AssertIsFinite(s);
249
250 BaseClass::operator=(s);
251 return *this;
252 }
253
254
255
256 template <typename Number, typename MemorySpace>
257 BlockVector<Number, MemorySpace> &
258 BlockVector<Number, MemorySpace>::operator=(const BlockVector &v)
259 {
260 // we only allow assignment to vectors with the same number of blocks
261 // or to an empty BlockVector
262 Assert(this->n_blocks() == 0 || this->n_blocks() == v.n_blocks(),
263 ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));
264
265 if (this->n_blocks() != v.n_blocks())
266 this->block_indices = v.block_indices;
267
268 this->components.resize(this->n_blocks());
269 for (size_type i = 0; i < this->n_blocks(); ++i)
270 this->components[i] = v.components[i];
271
272 this->collect_sizes();
273
274 return *this;
275 }
276
277
278
279 template <typename Number, typename MemorySpace>
280 BlockVector<Number, MemorySpace> &
281 BlockVector<Number, MemorySpace>::operator=(
282 const Vector<Number, MemorySpace> &v)
283 {
284 BaseClass::operator=(v);
285 return *this;
286 }
287
288
289
290 template <typename Number, typename MemorySpace>
291 template <typename Number2>
292 BlockVector<Number, MemorySpace> &
293 BlockVector<Number, MemorySpace>::operator=(
294 const BlockVector<Number2, MemorySpace> &v)
295 {
296 reinit(v, true);
297 BaseClass::operator=(v);
298 return *this;
299 }
300
301
302
303 template <typename Number, typename MemorySpace>
304 template <typename Number2>
305 void
306 BlockVector<Number, MemorySpace>::copy_locally_owned_data_from(
307 const BlockVector<Number2, MemorySpace> &v)
308 {
309 AssertDimension(this->n_blocks(), v.n_blocks());
310
311 for (unsigned int b = 0; b < this->n_blocks(); ++b)
312 this->block(b).copy_locally_owned_data_from(v.block(b));
313 }
314
315
316
317 #ifdef DEAL_II_WITH_PETSC
318
319 namespace petsc_helpers
320 {
321 template <typename PETSC_Number, typename Number>
322 void
323 copy_petsc_vector(const PETSC_Number *petsc_start_ptr,
324 const PETSC_Number *petsc_end_ptr,
325 Number *ptr)
326 {
327 std::copy(petsc_start_ptr, petsc_end_ptr, ptr);
328 }
329
330 template <typename PETSC_Number, typename Number>
331 void
332 copy_petsc_vector(const std::complex<PETSC_Number> *petsc_start_ptr,
333 const std::complex<PETSC_Number> *petsc_end_ptr,
334 std::complex<Number> *ptr)
335 {
336 std::copy(petsc_start_ptr, petsc_end_ptr, ptr);
337 }
338
339 template <typename PETSC_Number, typename Number>
340 void
341 copy_petsc_vector(const std::complex<PETSC_Number> * /*petsc_start_ptr*/,
342 const std::complex<PETSC_Number> * /*petsc_end_ptr*/,
343 Number * /*ptr*/)
344 {
345 AssertThrow(false, ExcMessage("Tried to copy complex -> real"));
346 }
347 } // namespace petsc_helpers
348
349
350
351 template <typename Number, typename MemorySpace>
352 BlockVector<Number, MemorySpace> &
353 BlockVector<Number, MemorySpace>::operator=(
354 const PETScWrappers::MPI::BlockVector &petsc_vec)
355 {
356 AssertDimension(this->n_blocks(), petsc_vec.n_blocks());
357 for (unsigned int i = 0; i < this->n_blocks(); ++i)
358 {
359 // We would like to use the same compact infrastructure as for the
360 // Trilinos vector below, but the interface through ReadWriteVector
361 // does not support overlapping (ghosted) PETSc vectors, which we need
362 // for backward compatibility.
363
364 Assert(petsc_vec.block(i).locally_owned_elements() ==
365 this->block(i).locally_owned_elements(),
366 StandardExceptions::ExcInvalidState());
367
368 // get a representation of the vector and copy it
369 const PetscScalar *start_ptr;
370 PetscErrorCode ierr =
371 VecGetArrayRead(static_cast<const Vec &>(petsc_vec.block(i)),
372 &start_ptr);
373 AssertThrow(ierr == 0, ExcPETScError(ierr));
374
375 const size_type vec_size = this->block(i).locally_owned_size();
376 petsc_helpers::copy_petsc_vector(start_ptr,
377 start_ptr + vec_size,
378 this->block(i).begin());
379
380 // restore the representation of the vector
381 ierr =
382 VecRestoreArrayRead(static_cast<const Vec &>(petsc_vec.block(i)),
383 &start_ptr);
384 AssertThrow(ierr == 0, ExcPETScError(ierr));
385
386 // spread ghost values between processes?
387 if (this->block(i).vector_is_ghosted ||
388 petsc_vec.block(i).has_ghost_elements())
389 this->block(i).update_ghost_values();
390 }
391
392 return *this;
393 }
394
395 #endif
396
397
398
399 #ifdef DEAL_II_WITH_TRILINOS
400
401 template <typename Number, typename MemorySpace>
402 BlockVector<Number, MemorySpace> &
403 BlockVector<Number, MemorySpace>::operator=(
404 const TrilinosWrappers::MPI::BlockVector &trilinos_vec)
405 {
406 AssertDimension(this->n_blocks(), trilinos_vec.n_blocks());
407 for (unsigned int i = 0; i < this->n_blocks(); ++i)
408 {
409 const auto &partitioner = this->block(i).get_partitioner();
410 IndexSet combined_set = partitioner->locally_owned_range();
411 combined_set.add_indices(partitioner->ghost_indices());
412 ReadWriteVector<Number> rw_vector(combined_set);
413 rw_vector.import_elements(trilinos_vec.block(i),
414 VectorOperation::insert);
415 this->block(i).import_elements(rw_vector, VectorOperation::insert);
416
417 if (this->block(i).has_ghost_elements() ||
418 trilinos_vec.block(i).has_ghost_elements())
419 this->block(i).update_ghost_values();
420 }
421
422 return *this;
423 }
424
425 #endif
426
427
428
429 template <typename Number, typename MemorySpace>
430 void
431 BlockVector<Number, MemorySpace>::compress(
432 VectorOperation::values operation)
433 {
434 const unsigned int n_chunks =
435 (this->n_blocks() + communication_block_size - 1) /
436 communication_block_size;
437 for (unsigned int c = 0; c < n_chunks; ++c)
438 {
439 const unsigned int start = c * communication_block_size;
440 const unsigned int end =
441 std::min(start + communication_block_size, this->n_blocks());
442
443 // start all requests for all blocks before finishing the transfers as
444 // this saves repeated synchronizations. In order to avoid conflict
445 // with possible other ongoing communication requests (from
446 // LA::distributed::Vector that supports unfinished requests), add
447 // 100 to the communication tag (the first 100 can be used by normal
448 // vectors).
449 for (unsigned int block = start; block < end; ++block)
450 this->block(block).compress_start(block - start + 100, operation);
451 for (unsigned int block = start; block < end; ++block)
452 this->block(block).compress_finish(operation);
453 }
454 }
455
456
457
458 template <typename Number, typename MemorySpace>
459 void
460 BlockVector<Number, MemorySpace>::update_ghost_values() const
461 {
462 const unsigned int n_chunks =
463 (this->n_blocks() + communication_block_size - 1) /
464 communication_block_size;
465 for (unsigned int c = 0; c < n_chunks; ++c)
466 {
467 const unsigned int start = c * communication_block_size;
468 const unsigned int end =
469 std::min(start + communication_block_size, this->n_blocks());
470
471 // In order to avoid conflict with possible other ongoing
472 // communication requests (from LA::distributed::Vector that supports
473 // unfinished requests), add 100 to the communication tag (the first
474 // 100 can be used by normal vectors)
475 for (unsigned int block = start; block < end; ++block)
476 this->block(block).update_ghost_values_start(block - start + 100);
477 for (unsigned int block = start; block < end; ++block)
478 this->block(block).update_ghost_values_finish();
479 }
480 }
481
482
483
484 template <typename Number, typename MemorySpace>
485 void
486 BlockVector<Number, MemorySpace>::zero_out_ghost_values() const
487 {
488 for (unsigned int block = 0; block < this->n_blocks(); ++block)
489 this->block(block).zero_out_ghost_values();
490 }
491
492
493
494 template <typename Number, typename MemorySpace>
495 bool
496 BlockVector<Number, MemorySpace>::has_ghost_elements() const
497 {
498 bool has_ghost_elements = false;
499 for (unsigned int block = 0; block < this->n_blocks(); ++block)
500 if (this->block(block).has_ghost_elements() == true)
501 has_ghost_elements = true;
502 return has_ghost_elements;
503 }
504
505
506
507 template <typename Number, typename MemorySpace>
508 void
509 BlockVector<Number, MemorySpace>::set_ghost_state(const bool ghosted) const
510 {
511 for (unsigned int block = 0; block < this->n_blocks(); ++block)
512 this->block(block).set_ghost_state(ghosted);
513 }
514
515
516
517 template <typename Number, typename MemorySpace>
518 BlockVector<Number, MemorySpace> &
519 BlockVector<Number, MemorySpace>::operator*=(const Number factor)
520 {
521 for (unsigned int block = 0; block < this->n_blocks(); ++block)
522 this->block(block) *= factor;
523 return *this;
524 }
525
526
527
528 template <typename Number, typename MemorySpace>
529 BlockVector<Number, MemorySpace> &
530 BlockVector<Number, MemorySpace>::operator/=(const Number factor)
531 {
532 operator*=(static_cast<Number>(1.) / factor);
533 return *this;
534 }
535
536
537
538 template <typename Number, typename MemorySpace>
539 void
540 BlockVector<Number, MemorySpace>::scale(
541 const BlockVector<Number, MemorySpace> &v)
542 {
543 AssertDimension(this->n_blocks(), v.n_blocks());
544 for (unsigned int block = 0; block < this->n_blocks(); ++block)
545 this->block(block).scale(v.block(block));
546 }
547
548
549
550 template <typename Number, typename MemorySpace>
551 void
552 BlockVector<Number, MemorySpace>::equ(
553 const Number a,
554 const BlockVector<Number, MemorySpace> &v)
555 {
556 AssertDimension(this->n_blocks(), v.n_blocks());
557 for (unsigned int block = 0; block < this->n_blocks(); ++block)
558 this->block(block).equ(a, v.block(block));
559 }
560
561
562
563 template <typename Number, typename MemorySpace>
564 BlockVector<Number, MemorySpace> &
565 BlockVector<Number, MemorySpace>::operator+=(
566 const BlockVector<Number, MemorySpace> &v)
567 {
568 AssertDimension(this->n_blocks(), v.n_blocks());
569 for (unsigned int block = 0; block < this->n_blocks(); ++block)
570 this->block(block) += v.block(block);
571
572 return *this;
573 }
574
575
576
577 template <typename Number, typename MemorySpace>
578 BlockVector<Number, MemorySpace> &
579 BlockVector<Number, MemorySpace>::operator-=(
580 const BlockVector<Number, MemorySpace> &v)
581 {
582 AssertDimension(this->n_blocks(), v.n_blocks());
583 for (unsigned int block = 0; block < this->n_blocks(); ++block)
584 this->block(block) -= v.block(block);
585
586 return *this;
587 }
588
589
590
591 template <typename Number, typename MemorySpace>
592 void
593 BlockVector<Number, MemorySpace>::add(const Number a)
594 {
595 for (unsigned int block = 0; block < this->n_blocks(); ++block)
596 this->block(block).add(a);
597 }
598
599
600
601 template <typename Number, typename MemorySpace>
602 void
603 BlockVector<Number, MemorySpace>::add(
604 const Number a,
605 const BlockVector<Number, MemorySpace> &v)
606 {
607 AssertDimension(this->n_blocks(), v.n_blocks());
608 for (unsigned int block = 0; block < this->n_blocks(); ++block)
609 this->block(block).add(a, v.block(block));
610 }
611
612
613
614 template <typename Number, typename MemorySpace>
615 void
616 BlockVector<Number, MemorySpace>::add(
617 const Number a,
618 const BlockVector<Number, MemorySpace> &v,
619 const Number b,
620 const BlockVector<Number, MemorySpace> &w)
621 {
622 AssertDimension(this->n_blocks(), v.n_blocks());
623 AssertDimension(this->n_blocks(), w.n_blocks());
624
625 for (unsigned int block = 0; block < this->n_blocks(); ++block)
626 this->block(block).add(a, v.block(block), b, w.block(block));
627 }
628
629
630
631 template <typename Number, typename MemorySpace>
632 void
633 BlockVector<Number, MemorySpace>::sadd(
634 const Number x,
635 const Number a,
636 const BlockVector<Number, MemorySpace> &v)
637 {
638 AssertDimension(this->n_blocks(), v.n_blocks());
639 for (unsigned int block = 0; block < this->n_blocks(); ++block)
640 this->block(block).sadd(x, a, v.block(block));
641 }
642
643
644
645 template <typename Number, typename MemorySpace>
646 void
647 BlockVector<Number, MemorySpace>::sadd(
648 const Number x,
649 const BlockVector<Number, MemorySpace> &v)
650 {
651 AssertDimension(this->n_blocks(), v.n_blocks());
652 for (unsigned int block = 0; block < this->n_blocks(); ++block)
653 this->block(block).sadd(x, v.block(block));
654 }
655
656
657
658 template <typename Number, typename MemorySpace>
659 template <typename OtherNumber>
660 void
661 BlockVector<Number, MemorySpace>::add(
662 const std::vector<size_type> &indices,
663 const ::dealii::Vector<OtherNumber> &values)
664 {
665 for (size_type i = 0; i < indices.size(); ++i)
666 (*this)(indices[i]) += values[i];
667 }
668
669
670
671 template <typename Number, typename MemorySpace>
672 void
673 BlockVector<Number, MemorySpace>::add(const std::vector<size_type> &indices,
674 const std::vector<Number> &values)
675 {
676 for (size_type i = 0; i < indices.size(); ++i)
677 (*this)(indices[i]) += values[i];
678 }
679
680
681
682 template <typename Number, typename MemorySpace>
683 bool
684 BlockVector<Number, MemorySpace>::all_zero() const
685 {
686 Assert(this->n_blocks() > 0, ExcEmptyObject());
687
688 // use int instead of bool
689 int local_result = -1;
690 for (unsigned int i = 0; i < this->n_blocks(); ++i)
691 local_result =
692 std::max(local_result,
693 (this->block(i).linfty_norm_local() == 0) ? -1 : 0);
694
695 if (this->block(0).partitioner->n_mpi_processes() > 1)
696 return -Utilities::MPI::max(
697 local_result, this->block(0).partitioner->get_mpi_communicator());
698 else
699 return local_result != 0;
700 }
701
702
703
704 template <typename Number, typename MemorySpace>
705 Number
706 BlockVector<Number, MemorySpace>::operator*(
707 const BlockVector<Number, MemorySpace> &v) const
708 {
709 Assert(this->n_blocks() > 0, ExcEmptyObject());
710 AssertDimension(this->n_blocks(), v.n_blocks());
711
712 Number local_result = Number();
713 for (unsigned int i = 0; i < this->n_blocks(); ++i)
714 local_result += this->block(i).inner_product_local(v.block(i));
715
716 if (this->block(0).partitioner->n_mpi_processes() > 1)
717 return Utilities::MPI::sum(
718 local_result, this->block(0).partitioner->get_mpi_communicator());
719 else
720 return local_result;
721 }
722
723
724
725 template <typename Number, typename MemorySpace>
726 inline Number
727 BlockVector<Number, MemorySpace>::mean_value() const
728 {
729 Assert(this->n_blocks() > 0, ExcEmptyObject());
730
731 Number local_result = Number();
732 for (unsigned int i = 0; i < this->n_blocks(); ++i)
733 local_result += this->block(i).mean_value_local() *
734 static_cast<real_type>(
735 this->block(i).partitioner->locally_owned_size());
736
737 if (this->block(0).partitioner->n_mpi_processes() > 1)
738 return Utilities::MPI::sum(
739 local_result,
740 this->block(0).partitioner->get_mpi_communicator()) /
741 static_cast<real_type>(this->size());
742 else
743 return local_result / static_cast<real_type>(this->size());
744 }
745
746
747
748 template <typename Number, typename MemorySpace>
749 inline typename BlockVector<Number, MemorySpace>::real_type
750 BlockVector<Number, MemorySpace>::l1_norm() const
751 {
752 Assert(this->n_blocks() > 0, ExcEmptyObject());
753
754 real_type local_result = real_type();
755 for (unsigned int i = 0; i < this->n_blocks(); ++i)
756 local_result += this->block(i).l1_norm_local();
757
758 if (this->block(0).partitioner->n_mpi_processes() > 1)
759 return Utilities::MPI::sum(
760 local_result, this->block(0).partitioner->get_mpi_communicator());
761 else
762 return local_result;
763 }
764
765
766
767 template <typename Number, typename MemorySpace>
768 inline typename BlockVector<Number, MemorySpace>::real_type
769 BlockVector<Number, MemorySpace>::norm_sqr() const
770 {
771 Assert(this->n_blocks() > 0, ExcEmptyObject());
772
773 real_type local_result = real_type();
774 for (unsigned int i = 0; i < this->n_blocks(); ++i)
775 local_result += this->block(i).norm_sqr_local();
776
777 if (this->block(0).partitioner->n_mpi_processes() > 1)
778 return Utilities::MPI::sum(
779 local_result, this->block(0).partitioner->get_mpi_communicator());
780 else
781 return local_result;
782 }
783
784
785
786 template <typename Number, typename MemorySpace>
787 inline typename BlockVector<Number, MemorySpace>::real_type
788 BlockVector<Number, MemorySpace>::l2_norm() const
789 {
790 return std::sqrt(norm_sqr());
791 }
792
793
794
795 template <typename Number, typename MemorySpace>
796 inline typename BlockVector<Number, MemorySpace>::real_type
797 BlockVector<Number, MemorySpace>::lp_norm(const real_type p) const
798 {
799 Assert(this->n_blocks() > 0, ExcEmptyObject());
800
801 real_type local_result = real_type();
802 for (unsigned int i = 0; i < this->n_blocks(); ++i)
803 local_result += std::pow(this->block(i).lp_norm_local(p), p);
804
805 if (this->block(0).partitioner->n_mpi_processes() > 1)
806 return std::pow(Utilities::MPI::sum(
807 local_result,
808 this->block(0).partitioner->get_mpi_communicator()),
809 static_cast<real_type>(1.0 / p));
810 else
811 return std::pow(local_result, static_cast<real_type>(1.0 / p));
812 }
813
814
815
816 template <typename Number, typename MemorySpace>
817 inline typename BlockVector<Number, MemorySpace>::real_type
818 BlockVector<Number, MemorySpace>::linfty_norm() const
819 {
820 Assert(this->n_blocks() > 0, ExcEmptyObject());
821
822 real_type local_result = real_type();
823 for (unsigned int i = 0; i < this->n_blocks(); ++i)
824 local_result =
825 std::max(local_result, this->block(i).linfty_norm_local());
826
827 if (this->block(0).partitioner->n_mpi_processes() > 1)
828 return Utilities::MPI::max(
829 local_result, this->block(0).partitioner->get_mpi_communicator());
830 else
831 return local_result;
832 }
833
834
835
836 template <typename Number, typename MemorySpace>
837 inline Number
838 BlockVector<Number, MemorySpace>::add_and_dot(
839 const Number a,
840 const BlockVector<Number, MemorySpace> &v,
841 const BlockVector<Number, MemorySpace> &w)
842 {
843 AssertDimension(this->n_blocks(), v.n_blocks());
844 AssertDimension(this->n_blocks(), w.n_blocks());
845
846 Number local_result = Number();
847 for (unsigned int i = 0; i < this->n_blocks(); ++i)
848 local_result +=
849 this->block(i).add_and_dot_local(a, v.block(i), w.block(i));
850
851 if (this->block(0).partitioner->n_mpi_processes() > 1)
852 return Utilities::MPI::sum(
853 local_result, this->block(0).partitioner->get_mpi_communicator());
854 else
855 return local_result;
856 }
857
858
859
860 template <typename Number, typename MemorySpace>
861 inline void
862 BlockVector<Number, MemorySpace>::swap(
863 BlockVector<Number, MemorySpace> &v) noexcept
864 {
865 Assert(this->n_blocks() == v.n_blocks(),
866 ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));
867
868 for (size_type i = 0; i < this->n_blocks(); ++i)
869 dealii::swap(this->components[i], v.components[i]);
870 dealii::swap(this->block_indices, v.block_indices);
871 }
872
873
874
875 template <typename Number, typename MemorySpace>
876 typename BlockVector<Number, MemorySpace>::size_type
877 BlockVector<Number, MemorySpace>::size() const
878 {
879 return this->block_indices.total_size();
880 }
881
882
883
884 template <typename Number, typename MemorySpace>
885 inline void
886 BlockVector<Number, MemorySpace>::import_elements(
887 const LinearAlgebra::ReadWriteVector<Number> &,
888 const VectorOperation::values,
889 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> &)
890 {
891 AssertThrow(false, ExcNotImplemented());
892 }
893
894
895
896 template <typename Number, typename MemorySpace>
897 IndexSet
898 BlockVector<Number, MemorySpace>::locally_owned_elements() const
899 {
900 IndexSet is(size());
901
902 // copy index sets from blocks into the global one, shifted by the
903 // appropriate amount for each block
904 for (unsigned int b = 0; b < this->n_blocks(); ++b)
905 {
906 IndexSet x = this->block(b).locally_owned_elements();
907 is.add_indices(x, this->block_indices.block_start(b));
908 }
909
910 is.compress();
911
912 return is;
913 }
914
915 template <typename Number, typename MemorySpace>
916 void
917 BlockVector<Number, MemorySpace>::print(std::ostream &out,
918 const unsigned int precision,
919 const bool scientific,
920 const bool across) const
921 {
922 for (unsigned int b = 0; b < this->n_blocks(); ++b)
923 this->block(b).print(out, precision, scientific, across);
924 }
925
926
927
928 template <typename Number, typename MemorySpace>
929 std::size_t
930 BlockVector<Number, MemorySpace>::memory_consumption() const
931 {
932 return (MemoryConsumption::memory_consumption(this->block_indices) +
933 MemoryConsumption::memory_consumption(this->components));
934 }
935
936
937
938 namespace internal
939 {
940 template <typename FullMatrixType>
941 inline void
942 set_symmetric(FullMatrixType &, const bool)
943 {}
944
945 template <typename NumberType>
946 inline void
947 set_symmetric(LAPACKFullMatrix<NumberType> &matrix, const bool symmetric)
948 {
949 if (symmetric)
950 matrix.set_property(LAPACKSupport::symmetric);
951 else
952 matrix.set_property(LAPACKSupport::general);
953 }
954 } // namespace internal
955
956
957
958 template <typename Number, typename MemorySpace>
959 template <typename FullMatrixType>
960 void
961 BlockVector<Number, MemorySpace>::multivector_inner_product(
962 FullMatrixType &matrix,
963 const BlockVector<Number, MemorySpace> &V,
964 const bool symmetric) const
965 {
966 const unsigned int m = this->n_blocks();
967 const unsigned int n = V.n_blocks();
968
969 // in case one vector is empty and the second one is not, the
970 // FullMatrix resized to (m,n) will have 0 both in m() and n()
971 // which is how TableBase<N,T>::reinit() works as of [email protected].
972 // Since in this case there is nothing to do anyway -- return immediately.
973 if (n == 0 || m == 0)
974 return;
975
976 Assert(matrix.m() == m, ExcDimensionMismatch(matrix.m(), m));
977 Assert(matrix.n() == n, ExcDimensionMismatch(matrix.n(), n));
978
979 // reset the matrix
980 matrix = typename FullMatrixType::value_type(0.0);
981
982 internal::set_symmetric(matrix, symmetric);
983 if (symmetric)
984 {
985 Assert(m == n, ExcDimensionMismatch(m, n));
986
987 for (unsigned int i = 0; i < m; ++i)
988 for (unsigned int j = i; j < n; ++j)
989 matrix(i, j) = this->block(i).inner_product_local(V.block(j));
990
991 for (unsigned int i = 0; i < m; ++i)
992 for (unsigned int j = i + 1; j < n; ++j)
993 matrix(j, i) = matrix(i, j);
994 }
995 else
996 {
997 for (unsigned int i = 0; i < m; ++i)
998 for (unsigned int j = 0; j < n; ++j)
999 matrix(i, j) = this->block(i).inner_product_local(V.block(j));
1000 }
1001
1002 Utilities::MPI::sum(matrix,
1003 this->block(0).get_mpi_communicator(),
1004 matrix);
1005 }
1006
1007
1008
1009 template <typename Number, typename MemorySpace>
1010 template <typename FullMatrixType>
1011 Number
1012 BlockVector<Number, MemorySpace>::multivector_inner_product_with_metric(
1013 const FullMatrixType &matrix,
1014 const BlockVector<Number, MemorySpace> &V,
1015 const bool symmetric) const
1016 {
1017 Number res = Number(0.);
1018
1019 const unsigned int m = this->n_blocks();
1020 const unsigned int n = V.n_blocks();
1021
1022 // in case one vector is empty and the second one is not, the
1023 // FullMatrix resized to (m,n) will have 0 both in m() and n()
1024 // which is how TableBase<N,T>::reinit() works.
1025 // Since in this case there is nothing to do anyway -- return immediately.
1026 if (n == 0 || m == 0)
1027 return res;
1028
1029 Assert(matrix.m() == m, ExcDimensionMismatch(matrix.m(), m));
1030 Assert(matrix.n() == n, ExcDimensionMismatch(matrix.n(), n));
1031
1032 if (symmetric)
1033 {
1034 Assert(m == n, ExcDimensionMismatch(m, n));
1035
1036 for (unsigned int i = 0; i < m; ++i)
1037 {
1038 res +=
1039 matrix(i, i) * this->block(i).inner_product_local(V.block(i));
1040 for (unsigned int j = i + 1; j < n; ++j)
1041 res += 2. * matrix(i, j) *
1042 this->block(i).inner_product_local(V.block(j));
1043 }
1044 }
1045 else
1046 {
1047 for (unsigned int i = 0; i < m; ++i)
1048 for (unsigned int j = 0; j < n; ++j)
1049 res +=
1050 matrix(i, j) * this->block(i).inner_product_local(V.block(j));
1051 }
1052
1053 return Utilities::MPI::sum(res, this->block(0).get_mpi_communicator());
1054 }
1055
1056
1057
1058 template <typename Number, typename MemorySpace>
1059 template <typename FullMatrixType>
1060 void
1061 BlockVector<Number, MemorySpace>::mmult(BlockVector<Number, MemorySpace> &V,
1062 const FullMatrixType &matrix,
1063 const Number s,
1064 const Number b) const
1065 {
1066 const unsigned int m = this->n_blocks();
1067 const unsigned int n = V.n_blocks();
1068
1069 // in case one vector is empty and the second one is not, the
1070 // FullMatrix resized to (m,n) will have 0 both in m() and n()
1071 // which is how TableBase<N,T>::reinit() works.
1072 // Since in this case there is nothing to do anyway -- return immediately.
1073 if (n == 0 || m == 0)
1074 return;
1075
1076 Assert(matrix.m() == m, ExcDimensionMismatch(matrix.m(), m));
1077 Assert(matrix.n() == n, ExcDimensionMismatch(matrix.n(), n));
1078
1079 for (unsigned int i = 0; i < n; ++i)
1080 {
1081 // below we make this work gracefully for identity-like matrices in
1082 // which case the two loops over j won't do any work as A(j,i)==0
1083 const unsigned int k = std::min(i, m - 1);
1084 V.block(i).sadd_local(s, matrix(k, i) * b, this->block(k));
1085 for (unsigned int j = 0; j < k; ++j)
1086 V.block(i).add_local(matrix(j, i) * b, this->block(j));
1087 for (unsigned int j = k + 1; j < m; ++j)
1088 V.block(i).add_local(matrix(j, i) * b, this->block(j));
1089 }
1090
1091 if (V.block(0).vector_is_ghosted)
1092 {
1093 for (unsigned int i = 0; i < n; ++i)
1094 Assert(V.block(i).vector_is_ghosted,
1095 ExcMessage(
1096 "All blocks should be either in ghosted state or not."));
1097
1098 V.update_ghost_values();
1099 }
1100 }
1101
1102
1103
1104 } // end of namespace distributed
1105
1106 } // namespace LinearAlgebra
1107
1108 DEAL_II_NAMESPACE_CLOSE
1109
1110 #endif