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
petsc_vector_base.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) 2004 - 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
14
15#ifdef DEAL_II_WITH_PETSC
16
18
22
23# include <boost/container/small_vector.hpp>
24
25# include <cmath>
26
27#endif // DEAL_II_WITH_PETSC
28
30
31#ifdef DEAL_II_WITH_PETSC
32
33namespace PETScWrappers
34{
35 namespace internal
36 {
37# ifndef DOXYGEN
38 VectorReference::
39 operator PetscScalar() const
40 {
41 AssertIndexRange(index, vector.size());
42
43 // The vector may have ghost entries. In that case, we first need to
44 // figure out which elements we own locally, then get a pointer to the
45 // elements that are stored here (both the ones we own as well as the
46 // ghost elements). In this array, the locally owned elements come first
47 // followed by the ghost elements whose position we can get from an
48 // index set.
49 if (vector.ghosted)
50 {
51 PetscInt begin, end;
52 PetscErrorCode ierr =
53 VecGetOwnershipRange(vector.vector, &begin, &end);
54 AssertThrow(ierr == 0, ExcPETScError(ierr));
55
56 Vec locally_stored_elements = nullptr;
57 ierr = VecGhostGetLocalForm(vector.vector, &locally_stored_elements);
58 AssertThrow(ierr == 0, ExcPETScError(ierr));
59
60 PetscInt lsize;
61 ierr = VecGetSize(locally_stored_elements, &lsize);
62 AssertThrow(ierr == 0, ExcPETScError(ierr));
63
64 const PetscScalar *ptr;
65 ierr = VecGetArrayRead(locally_stored_elements, &ptr);
66 AssertThrow(ierr == 0, ExcPETScError(ierr));
67
68 PetscScalar value;
69
70 if (index >= static_cast<size_type>(begin) &&
71 index < static_cast<size_type>(end))
72 {
73 // local entry
74 value = *(ptr + index - begin);
75 }
76 else
77 {
78 // ghost entry
79 Assert(vector.ghost_indices.is_element(index),
81 "You are trying to access an element of a vector "
82 "that is neither a locally owned element nor a "
83 "ghost element of the vector."));
84 const size_type ghostidx =
85 vector.ghost_indices.index_within_set(index);
86
87 AssertIndexRange(ghostidx + end - begin, lsize);
88 value = *(ptr + ghostidx + end - begin);
89 }
90
91 ierr = VecRestoreArrayRead(locally_stored_elements, &ptr);
92 AssertThrow(ierr == 0, ExcPETScError(ierr));
93
94 ierr =
95 VecGhostRestoreLocalForm(vector.vector, &locally_stored_elements);
96 AssertThrow(ierr == 0, ExcPETScError(ierr));
97
98 return value;
99 }
100
101
102 // first verify that the requested
103 // element is actually locally
104 // available
105 PetscInt begin, end;
106
107 PetscErrorCode ierr = VecGetOwnershipRange(vector.vector, &begin, &end);
108 AssertThrow(ierr == 0, ExcPETScError(ierr));
109
110 AssertThrow((index >= static_cast<size_type>(begin)) &&
111 (index < static_cast<size_type>(end)),
112 ExcAccessToNonlocalElement(index, begin, end - 1));
113
114 const PetscScalar *ptr;
115 PetscScalar value;
116 ierr = VecGetArrayRead(vector.vector, &ptr);
117 AssertThrow(ierr == 0, ExcPETScError(ierr));
118 value = *(ptr + index - begin);
119 ierr = VecRestoreArrayRead(vector.vector, &ptr);
120 AssertThrow(ierr == 0, ExcPETScError(ierr));
121
122 return value;
123 }
124# endif
125 } // namespace internal
126
128 : vector(nullptr)
129 , ghosted(false)
130 , last_action(VectorOperation::unknown)
131 , ghost_vector(nullptr)
132 , ghost_vector_array(nullptr)
133 {}
134
135
136
138 : ghosted(v.ghosted)
139 , ghost_indices(v.ghost_indices)
140 , last_action(VectorOperation::unknown)
141 , ghost_vector(nullptr)
142 , ghost_vector_array(nullptr)
143 {
144 PetscErrorCode ierr = VecDuplicate(v.vector, &vector);
145 AssertThrow(ierr == 0, ExcPETScError(ierr));
146
147 ierr = VecCopy(v.vector, vector);
148 AssertThrow(ierr == 0, ExcPETScError(ierr));
149 if (ghosted)
151 }
152
153
154
156 : vector(v)
157 , ghosted(false)
158 , last_action(VectorOperation::unknown)
159 , ghost_vector(nullptr)
160 , ghost_vector_array(nullptr)
161 {
162 const PetscErrorCode ierr =
163 PetscObjectReference(reinterpret_cast<PetscObject>(vector));
164 AssertNothrow(ierr == 0, ExcPETScError(ierr));
166 if (ghosted)
168 }
169
170
171
173 {
174 if (ghosted)
176
177 const PetscErrorCode ierr = VecDestroy(&vector);
178 AssertNothrow(ierr == 0, ExcPETScError(ierr));
179 }
180
181
182
183 void
185 {
187 ExcMessage("Cannot assign a new Vec"));
188
189 if (ghosted)
191
192 PetscErrorCode ierr =
193 PetscObjectReference(reinterpret_cast<PetscObject>(v));
194 AssertThrow(ierr == 0, ExcPETScError(ierr));
195 ierr = VecDestroy(&vector);
196 AssertThrow(ierr == 0, ExcPETScError(ierr));
197 vector = v;
199 if (ghosted)
201 }
202
203
204
205 namespace
206 {
207 template <typename Iterator, typename OutType>
208 class ConvertingIterator
209 {
210 Iterator m_iterator;
211
212 public:
213 using difference_type =
214 typename std::iterator_traits<Iterator>::difference_type;
215 using value_type = OutType;
216 using pointer = OutType *;
217 using reference = OutType &;
218 using iterator_category = std::forward_iterator_tag;
219
220 ConvertingIterator(const Iterator &iterator)
222 {}
223
224 OutType
225 operator*() const
226 {
227 return static_cast<OutType>(std::real(*m_iterator));
228 }
229
230 ConvertingIterator &
231 operator++()
232 {
233 ++m_iterator;
234 return *this;
235 }
236
237 ConvertingIterator
238 operator++(int)
239 {
240 ConvertingIterator old = *this;
241 ++m_iterator;
242 return old;
243 }
244
245 bool
246 operator==(const ConvertingIterator &other) const
247 {
248 return this->m_iterator == other.m_iterator;
249 }
250
251 bool
252 operator!=(const ConvertingIterator &other) const
253 {
254 return this->m_iterator != other.m_iterator;
255 }
256 };
257 } // namespace
258
259
260
261 void
263 {
264 // Reset ghost data
265 ghosted = false;
267
268 // There's no API to infer ghost indices from a PETSc Vec which
269 // unfortunately doesn't allow integer entries. We use the
270 // "ConvertingIterator" class above to do an implicit conversion when
271 // sorting and adding ghost indices below.
272 PetscErrorCode ierr;
273 Vec ghosted_vec;
274 ierr = VecGhostGetLocalForm(vector, &ghosted_vec);
275 AssertThrow(ierr == 0, ExcPETScError(ierr));
276 if (ghosted_vec && ghosted_vec != vector)
277 {
278 Vec tvector;
279 PetscScalar *array;
280 PetscInt ghost_start_index, end_index, n_elements_stored_locally;
281
282 ierr = VecGhostRestoreLocalForm(vector, &ghosted_vec);
283 AssertThrow(ierr == 0, ExcPETScError(ierr));
284
285 ierr = VecGetOwnershipRange(vector, &ghost_start_index, &end_index);
286 AssertThrow(ierr == 0, ExcPETScError(ierr));
287 ierr = VecDuplicate(vector, &tvector);
288 AssertThrow(ierr == 0, ExcPETScError(ierr));
289 ierr = VecGetArray(tvector, &array);
290 AssertThrow(ierr == 0, ExcPETScError(ierr));
291
292 // Store the indices we care about in the vector, so that we can then
293 // exchange this information between processes. It is unfortunate that
294 // we have to store integers in floating point numbers. Let's at least
295 // make sure we do that in a way that ensures that when we get these
296 // numbers back as integers later on, we get the same thing.
297 for (PetscInt i = 0; i < end_index - ghost_start_index; i++)
298 {
299 Assert(static_cast<PetscInt>(std::real(static_cast<PetscScalar>(
300 ghost_start_index + i))) == (ghost_start_index + i),
302 array[i] = ghost_start_index + i;
303 }
304
305 ierr = VecRestoreArray(tvector, &array);
306 AssertThrow(ierr == 0, ExcPETScError(ierr));
307 ierr = VecGhostUpdateBegin(tvector, INSERT_VALUES, SCATTER_FORWARD);
308 AssertThrow(ierr == 0, ExcPETScError(ierr));
309 ierr = VecGhostUpdateEnd(tvector, INSERT_VALUES, SCATTER_FORWARD);
310 AssertThrow(ierr == 0, ExcPETScError(ierr));
311 ierr = VecGhostGetLocalForm(tvector, &ghosted_vec);
312 AssertThrow(ierr == 0, ExcPETScError(ierr));
313 ierr = VecGetLocalSize(ghosted_vec, &n_elements_stored_locally);
314 AssertThrow(ierr == 0, ExcPETScError(ierr));
315 ierr = VecGetArrayRead(ghosted_vec, (const PetscScalar **)&array);
316 AssertThrow(ierr == 0, ExcPETScError(ierr));
317
318 // Populate the 'ghosted' flag and the ghost_indices variable. The
319 // latter is an index set that is most efficiently filled by
320 // sorting the indices to add. At the same time, we don't want to
321 // sort the indices stored in a PETSc-owned array; so if the array
322 // is already sorted, pass that to the IndexSet variable, and if
323 // not then copy the indices, sort them, and then add those.
324 ghosted = true;
325 ghost_indices.set_size(this->size());
326
327 ConvertingIterator<PetscScalar *, types::global_dof_index> begin_ghosts(
328 &array[end_index - ghost_start_index]);
329 ConvertingIterator<PetscScalar *, types::global_dof_index> end_ghosts(
330 &array[n_elements_stored_locally]);
331 if (std::is_sorted(&array[end_index - ghost_start_index],
332 &array[n_elements_stored_locally],
333 [](PetscScalar left, PetscScalar right) {
334 return static_cast<PetscInt>(std::real(left)) <
335 static_cast<PetscInt>(std::real(right));
336 }))
337 {
338 ghost_indices.add_indices(begin_ghosts, end_ghosts);
339 }
340 else
341 {
342 std::vector<PetscInt> sorted_indices(begin_ghosts, end_ghosts);
343 std::sort(sorted_indices.begin(), sorted_indices.end());
344 ghost_indices.add_indices(sorted_indices.begin(),
345 sorted_indices.end());
346 }
348
349 ierr = VecRestoreArrayRead(ghosted_vec, (const PetscScalar **)&array);
350 AssertThrow(ierr == 0, ExcPETScError(ierr));
351 ierr = VecGhostRestoreLocalForm(tvector, &ghosted_vec);
352 AssertThrow(ierr == 0, ExcPETScError(ierr));
353 ierr = VecDestroy(&tvector);
354 AssertThrow(ierr == 0, ExcPETScError(ierr));
355 }
356 else
357 {
358 ierr = VecGhostRestoreLocalForm(vector, &ghosted_vec);
359 AssertThrow(ierr == 0, ExcPETScError(ierr));
360 }
361 }
362
363
364
365 void
367 {
368 AssertThrow(ghosted, ExcMessage("Vector is not ghosted"));
369
370 AssertThrow(ghost_vector == nullptr && ghost_vector_array == nullptr,
372 "Ghost vector is already acquired for the vector."));
373
374 PetscErrorCode ierr = VecGhostGetLocalForm(vector, &ghost_vector);
375 AssertThrow(ierr == 0, ExcPETScError(ierr));
376 ierr = VecGetArrayRead(ghost_vector, &ghost_vector_array);
377 AssertThrow(ierr == 0, ExcPETScError(ierr));
378 }
379
380
381
382 void
384 {
385 AssertThrow(ghost_vector != nullptr,
386 ExcInternalError("Ghost vector is not acquired"));
387
388 PetscErrorCode ierr =
389 VecRestoreArrayRead(ghost_vector, &ghost_vector_array);
390 AssertThrow(ierr == 0, ExcPETScError(ierr));
391 ierr = VecGhostRestoreLocalForm(vector, &ghost_vector);
392 AssertThrow(ierr == 0, ExcPETScError(ierr));
393
394 ghost_vector = nullptr;
395 ghost_vector_array = nullptr;
396 }
397
398
399
400 void
402 {
403 if (ghosted)
405
406 const PetscErrorCode ierr = VecDestroy(&vector);
407 AssertThrow(ierr == 0, ExcPETScError(ierr));
408
409 ghosted = false;
412 }
413
414
415
416 VectorBase &
418 {
419 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
420
421 PetscErrorCode ierr = VecCopy(v, vector);
422 AssertThrow(ierr == 0, ExcPETScError(ierr));
423
424 return *this;
425 }
426
427
428
429 VectorBase &
430 VectorBase::operator=(const PetscScalar s)
431 {
432 if (s != PetscScalar(0))
435
436 // TODO[TH]: assert(is_compressed())
437
438 // First set the elements of the locally owned part of
439 // the vector to 's':
440 PetscErrorCode ierr = VecSet(vector, s);
441 AssertThrow(ierr == 0, ExcPETScError(ierr));
442
443 // If the vector has ghost elements, then the assertion
444 // above checks that s==0. In that case, we're simply
445 // zeroing out the entire vector and need to also do
446 // that for the ghost entries of the vector.
447 if (has_ghost_elements())
448 {
449 Vec ghost = nullptr;
450 ierr = VecGhostGetLocalForm(vector, &ghost);
451 AssertThrow(ierr == 0, ExcPETScError(ierr));
452
453 ierr = VecSet(ghost, s);
454 AssertThrow(ierr == 0, ExcPETScError(ierr));
455
456 ierr = VecGhostRestoreLocalForm(vector, &ghost);
457 AssertThrow(ierr == 0, ExcPETScError(ierr));
458 }
459
460 return *this;
461 }
462
463
464
465 bool
467 {
468 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
469
470 PetscBool flag;
471 const PetscErrorCode ierr = VecEqual(vector, v.vector, &flag);
472 AssertThrow(ierr == 0, ExcPETScError(ierr));
473
474 return (flag == PETSC_TRUE);
475 }
476
477
478
479 bool
481 {
482 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
483
484 PetscBool flag;
485 const PetscErrorCode ierr = VecEqual(vector, v.vector, &flag);
486 AssertThrow(ierr == 0, ExcPETScError(ierr));
487
488 return (flag == PETSC_FALSE);
489 }
490
491
492
495 {
496 PetscInt sz;
497 const PetscErrorCode ierr = VecGetSize(vector, &sz);
498 AssertThrow(ierr == 0, ExcPETScError(ierr));
499
500 return sz;
501 }
502
503
504
507 {
508 PetscInt sz;
509 const PetscErrorCode ierr = VecGetLocalSize(vector, &sz);
510 AssertThrow(ierr == 0, ExcPETScError(ierr));
511
512 return sz;
513 }
514
515
516
517 std::pair<VectorBase::size_type, VectorBase::size_type>
519 {
520 PetscInt begin, end;
521 const PetscErrorCode ierr =
522 VecGetOwnershipRange(static_cast<const Vec &>(vector), &begin, &end);
523 AssertThrow(ierr == 0, ExcPETScError(ierr));
524
525 return std::make_pair(begin, end);
526 }
527
528
529
530 void
531 VectorBase::set(const std::vector<size_type> &indices,
532 const std::vector<PetscScalar> &values)
533 {
534 Assert(indices.size() == values.size(),
535 ExcMessage("Function called with arguments of different sizes"));
536 do_set_add_operation(indices.size(), indices.data(), values.data(), false);
537 }
538
539
540
541 void
542 VectorBase::add(const std::vector<size_type> &indices,
543 const std::vector<PetscScalar> &values)
544 {
545 Assert(indices.size() == values.size(),
546 ExcMessage("Function called with arguments of different sizes"));
547 do_set_add_operation(indices.size(), indices.data(), values.data(), true);
548 }
549
550
551
552 void
553 VectorBase::add(const std::vector<size_type> &indices,
554 const ::Vector<PetscScalar> &values)
555 {
556 Assert(indices.size() == values.size(),
557 ExcMessage("Function called with arguments of different sizes"));
558 do_set_add_operation(indices.size(), indices.data(), values.begin(), true);
559 }
560
561
562
563 void
564 VectorBase::add(const size_type n_elements,
565 const size_type *indices,
566 const PetscScalar *values)
567 {
568 do_set_add_operation(n_elements, indices, values, true);
569 }
570
571
572
573 PetscScalar
575 {
576 Assert(size() == vec.size(), ExcDimensionMismatch(size(), vec.size()));
577
578 PetscScalar result;
579
580 // For complex vectors, VecDot() computes
581 // val = (x,y) = y^H x,
582 // where y^H denotes the conjugate transpose of y.
583 // Note that this corresponds to the usual "mathematicians'"
584 // complex inner product where the SECOND argument gets the
585 // complex conjugate, which is also how we document this
586 // operation.
587 const PetscErrorCode ierr = VecDot(vec.vector, vector, &result);
588 AssertThrow(ierr == 0, ExcPETScError(ierr));
589
590 return result;
591 }
592
593
594
595 PetscScalar
596 VectorBase::add_and_dot(const PetscScalar a,
597 const VectorBase &V,
598 const VectorBase &W)
599 {
600 this->add(a, V);
601 return *this * W;
602 }
603
604
605
606 void
608 {
609 Assert(has_ghost_elements() == false,
610 ExcMessage("Calling compress() is only useful if a vector "
611 "has been written into, but this is a vector with ghost "
612 "elements and consequently is read-only. It does "
613 "not make sense to call compress() for such "
614 "vectors."));
615
616 {
617 if constexpr (running_in_debug_mode())
618 {
619 // Check that all processors agree that last_action is the same (or
620 // none!)
621
622 int my_int_last_action = last_action;
623 int all_int_last_action;
624
625 const int ierr = MPI_Allreduce(&my_int_last_action,
626 &all_int_last_action,
627 1,
628 MPI_INT,
629 MPI_BOR,
631 AssertThrowMPI(ierr);
632
633 AssertThrow(all_int_last_action !=
636 "Error: not all processors agree on the last "
637 "VectorOperation before this compress() call."));
638 }
639 }
640
644 "Missing compress() or calling with wrong VectorOperation argument."));
645
646 // note that one may think that
647 // we only need to do something
648 // if in fact the state is
649 // anything but
650 // last_action::unknown. but
651 // that's not true: one
652 // frequently gets into
653 // situations where only one
654 // processor (or a subset of
655 // processors) actually writes
656 // something into a vector, but
657 // we still need to call
658 // VecAssemblyBegin/End on all
659 // processors.
660 PetscErrorCode ierr = VecAssemblyBegin(vector);
661 AssertThrow(ierr == 0, ExcPETScError(ierr));
662 ierr = VecAssemblyEnd(vector);
663 AssertThrow(ierr == 0, ExcPETScError(ierr));
664
665 // reset the last action field to
666 // indicate that we're back to a
667 // pristine state
669 }
670
671
672
675 {
676 const real_type d = l2_norm();
677 return d * d;
678 }
679
680
681
682 PetscScalar
684 {
685 // We can only use our more efficient
686 // routine in the serial case.
687 if (dynamic_cast<const PETScWrappers::MPI::Vector *>(this) != nullptr)
688 {
689 PetscScalar sum;
690 const PetscErrorCode ierr = VecSum(vector, &sum);
691 AssertThrow(ierr == 0, ExcPETScError(ierr));
692 return sum / static_cast<PetscReal>(size());
693 }
694
695 // get a representation of the vector and
696 // loop over all the elements
697 const PetscScalar *start_ptr;
698 PetscErrorCode ierr = VecGetArrayRead(vector, &start_ptr);
699 AssertThrow(ierr == 0, ExcPETScError(ierr));
700
701 PetscScalar mean = 0;
702 {
703 PetscScalar sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
704
705 // use modern processors better by
706 // allowing pipelined commands to be
707 // executed in parallel
708 const PetscScalar *ptr = start_ptr;
709 const PetscScalar *eptr = ptr + (locally_owned_size() / 4) * 4;
710 while (ptr != eptr)
711 {
712 sum0 += *ptr++;
713 sum1 += *ptr++;
714 sum2 += *ptr++;
715 sum3 += *ptr++;
716 }
717 // add up remaining elements
718 while (ptr != start_ptr + locally_owned_size())
719 sum0 += *ptr++;
720
721 mean =
722 Utilities::MPI::sum(sum0 + sum1 + sum2 + sum3, get_mpi_communicator()) /
723 static_cast<PetscReal>(size());
724 }
725
726 // restore the representation of the
727 // vector
728 ierr = VecRestoreArrayRead(vector, &start_ptr);
729 AssertThrow(ierr == 0, ExcPETScError(ierr));
730
731 return mean;
732 }
733
734
737 {
738 real_type d;
739
740 const PetscErrorCode ierr = VecNorm(vector, NORM_1, &d);
741 AssertThrow(ierr == 0, ExcPETScError(ierr));
742
743 return d;
744 }
745
746
747
750 {
751 real_type d;
752
753 const PetscErrorCode ierr = VecNorm(vector, NORM_2, &d);
754 AssertThrow(ierr == 0, ExcPETScError(ierr));
755
756 return d;
757 }
758
759
760
763 {
764 // get a representation of the vector and
765 // loop over all the elements
766 const PetscScalar *start_ptr;
767 PetscErrorCode ierr = VecGetArrayRead(vector, &start_ptr);
768 AssertThrow(ierr == 0, ExcPETScError(ierr));
769
770 real_type norm = 0;
771 {
772 real_type sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
773
774 // use modern processors better by
775 // allowing pipelined commands to be
776 // executed in parallel
777 const PetscScalar *ptr = start_ptr;
778 const PetscScalar *eptr = ptr + (locally_owned_size() / 4) * 4;
779 while (ptr != eptr)
780 {
785 }
786 // add up remaining elements
787 while (ptr != start_ptr + locally_owned_size())
789
790 norm = std::pow(Utilities::MPI::sum(sum0 + sum1 + sum2 + sum3,
792 1. / p);
793 }
794
795 // restore the representation of the
796 // vector
797 ierr = VecRestoreArrayRead(vector, &start_ptr);
798 AssertThrow(ierr == 0, ExcPETScError(ierr));
799
800 return norm;
801 }
802
803
804
807 {
808 real_type d;
809
810 const PetscErrorCode ierr = VecNorm(vector, NORM_INFINITY, &d);
811 AssertThrow(ierr == 0, ExcPETScError(ierr));
812
813 return d;
814 }
815
816
817
818 bool
820 {
821 const PetscScalar *start_ptr;
822 PetscErrorCode ierr = VecGetArrayRead(vector, &start_ptr);
823 AssertThrow(ierr == 0, ExcPETScError(ierr));
824
825 const bool local_all_zero =
826 std::all_of(start_ptr,
827 start_ptr + locally_owned_size(),
828 numbers::value_is_zero<PetscScalar>);
829
830 ierr = VecRestoreArrayRead(vector, &start_ptr);
831 AssertThrow(ierr == 0, ExcPETScError(ierr));
832
833 return Utilities::MPI::logical_and(local_all_zero, get_mpi_communicator());
834 }
835
836
837
838 VectorBase &
839 VectorBase::operator*=(const PetscScalar a)
840 {
843
844 const PetscErrorCode ierr = VecScale(vector, a);
845 AssertThrow(ierr == 0, ExcPETScError(ierr));
846
847 return *this;
848 }
849
850
851
852 VectorBase &
853 VectorBase::operator/=(const PetscScalar a)
854 {
857
858 const PetscScalar factor = 1. / a;
859 AssertIsFinite(factor);
860
861 const PetscErrorCode ierr = VecScale(vector, factor);
862 AssertThrow(ierr == 0, ExcPETScError(ierr));
863
864 return *this;
865 }
866
867
868
869 VectorBase &
871 {
873 const PetscErrorCode ierr = VecAXPY(vector, 1, v);
874 AssertThrow(ierr == 0, ExcPETScError(ierr));
875
876 return *this;
877 }
878
879
880
881 VectorBase &
883 {
885 const PetscErrorCode ierr = VecAXPY(vector, -1, v);
886 AssertThrow(ierr == 0, ExcPETScError(ierr));
887
888 return *this;
889 }
890
891
892
893 void
894 VectorBase::add(const PetscScalar s)
895 {
898
899 const PetscErrorCode ierr = VecShift(vector, s);
900 AssertThrow(ierr == 0, ExcPETScError(ierr));
901 }
902
903
904
905 void
906 VectorBase::add(const PetscScalar a, const VectorBase &v)
907 {
910
911 const PetscErrorCode ierr = VecAXPY(vector, a, v);
912 AssertThrow(ierr == 0, ExcPETScError(ierr));
913 }
914
915
916
917 void
918 VectorBase::add(const PetscScalar a,
919 const VectorBase &v,
920 const PetscScalar b,
921 const VectorBase &w)
922 {
926
927 const PetscScalar weights[2] = {a, b};
928 Vec addends[2] = {v.vector, w.vector};
929
930 const PetscErrorCode ierr = VecMAXPY(vector, 2, weights, addends);
931 AssertThrow(ierr == 0, ExcPETScError(ierr));
932 }
933
934
935
936 void
937 VectorBase::sadd(const PetscScalar s, const VectorBase &v)
938 {
941
942 const PetscErrorCode ierr = VecAYPX(vector, s, v);
943 AssertThrow(ierr == 0, ExcPETScError(ierr));
944 }
945
946
947
948 void
949 VectorBase::sadd(const PetscScalar s,
950 const PetscScalar a,
951 const VectorBase &v)
952 {
956
957 // there is nothing like a AXPAY
958 // operation in PETSc, so do it in two
959 // steps
960 *this *= s;
961 add(a, v);
962 }
963
964
965
966 void
968 {
970 const PetscErrorCode ierr = VecPointwiseMult(vector, factors, vector);
971 AssertThrow(ierr == 0, ExcPETScError(ierr));
972 }
973
974
975
976 void
977 VectorBase::equ(const PetscScalar a, const VectorBase &v)
978 {
981
982 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
983
984 const PetscErrorCode ierr = VecAXPBY(vector, a, 0.0, v.vector);
985 AssertThrow(ierr == 0, ExcPETScError(ierr));
986 }
987
988
989
990 void
991 VectorBase::write_ascii(const PetscViewerFormat format)
992 {
993 // TODO[TH]:assert(is_compressed())
994 MPI_Comm comm = PetscObjectComm((PetscObject)vector);
995
996 // Set options
997 PetscErrorCode ierr =
998 PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(comm), format);
999 AssertThrow(ierr == 0, ExcPETScError(ierr));
1000
1001 // Write to screen
1002 ierr = VecView(vector, PETSC_VIEWER_STDOUT_(comm));
1003 AssertThrow(ierr == 0, ExcPETScError(ierr));
1004 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(comm));
1005 AssertThrow(ierr == 0, ExcPETScError(ierr));
1006 }
1007
1008
1009
1010 void
1011 VectorBase::print(std::ostream &out,
1012 const unsigned int precision,
1013 const bool scientific,
1014 const bool across) const
1015 {
1016 AssertThrow(out.fail() == false, ExcIO());
1017
1018 // get a representation of the vector and
1019 // loop over all the elements
1020 const PetscScalar *val;
1021 PetscErrorCode ierr = VecGetArrayRead(vector, &val);
1022
1023 AssertThrow(ierr == 0, ExcPETScError(ierr));
1024
1025 // save the state of out stream
1026 const std::ios::fmtflags old_flags = out.flags();
1027 const unsigned int old_precision = out.precision(precision);
1028
1029 out.precision(precision);
1030 if (scientific)
1031 out.setf(std::ios::scientific, std::ios::floatfield);
1032 else
1033 out.setf(std::ios::fixed, std::ios::floatfield);
1034
1035 if (across)
1036 for (size_type i = 0; i < locally_owned_size(); ++i)
1037 out << val[i] << ' ';
1038 else
1039 for (size_type i = 0; i < locally_owned_size(); ++i)
1040 out << val[i] << std::endl;
1041 out << std::endl;
1042
1043 // reset output format
1044 out.flags(old_flags);
1045 out.precision(old_precision);
1046
1047 // restore the representation of the
1048 // vector
1049 ierr = VecRestoreArrayRead(vector, &val);
1050 AssertThrow(ierr == 0, ExcPETScError(ierr));
1051
1052 AssertThrow(out.fail() == false, ExcIO());
1053 }
1054
1055
1056
1057 void
1059 {
1060 std::swap(this->vector, v.vector);
1061 std::swap(this->ghosted, v.ghosted);
1062 std::swap(this->last_action, v.last_action);
1063 // missing swap for IndexSet
1064 IndexSet t(this->ghost_indices);
1065 this->ghost_indices = v.ghost_indices;
1066 v.ghost_indices = std::move(t);
1067 std::swap(this->ghost_vector, v.ghost_vector);
1068 std::swap(this->ghost_vector_array, v.ghost_vector_array);
1069 }
1070
1071
1072 VectorBase::
1073 operator const Vec &() const
1074 {
1075 return vector;
1076 }
1077
1078
1079 Vec &
1081 {
1082 return vector;
1083 }
1084
1085
1086 std::size_t
1088 {
1089 std::size_t mem = sizeof(Vec) + sizeof(last_action) +
1092
1093 // TH: I am relatively sure that PETSc is
1094 // storing the local data in a contiguous
1095 // block without indices:
1096 mem += locally_owned_size() * sizeof(PetscScalar);
1097 // assume that PETSc is storing one index
1098 // and one double per ghost element
1099 if (ghosted)
1100 mem += ghost_indices.n_elements() * (sizeof(PetscScalar) + sizeof(int));
1101
1102 // TODO[TH]: size of constant memory for PETSc?
1103 return mem;
1104 }
1105
1106
1107
1108 void
1110 const size_type *indices,
1111 const PetscScalar *values,
1112 const bool add_values)
1113 {
1114 const VectorOperation::values action =
1117 internal::VectorReference::ExcWrongMode(action, last_action));
1119
1120 boost::container::small_vector<PetscInt, 200> petsc_indices(n_elements);
1121 for (size_type i = 0; i < n_elements; ++i)
1122 {
1123 const auto petsc_index = static_cast<PetscInt>(indices[i]);
1124 AssertIntegerConversion(petsc_index, indices[i]);
1125 petsc_indices[i] = petsc_index;
1126 }
1127
1128 const InsertMode mode = (add_values ? ADD_VALUES : INSERT_VALUES);
1129 const PetscErrorCode ierr = VecSetValues(
1130 vector, petsc_indices.size(), petsc_indices.data(), values, mode);
1131 AssertThrow(ierr == 0, ExcPETScError(ierr));
1132
1133 last_action = action;
1134 }
1135
1136} // namespace PETScWrappers
1137
1138
1139#endif // DEAL_II_WITH_PETSC
*  iterator end()
*  *  iterator begin()
*  *  reference operator*() const
std::ptrdiff_t difference_type
*  *  iterator & operator++()
*  *  iterator()=default
bool operator!=(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
bool operator==(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
size_type n_elements() const
Definition index_set.h:1917
void set_size(const size_type size)
Definition index_set.h:1747
void clear()
Definition index_set.h:1735
void compress() const
Definition index_set.h:1767
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
Definition index_set.h:1814
real_type lp_norm(const real_type p) const
VectorBase & operator+=(const VectorBase &V)
PetscScalar mean_value() const
VectorOperation::values last_action
PetscScalar operator*(const VectorBase &vec) const
bool operator==(const VectorBase &v) const
VectorBase & operator*=(const PetscScalar factor)
VectorBase & operator-=(const VectorBase &V)
bool operator!=(const VectorBase &v) const
std::size_t memory_consumption() const
VectorBase & operator/=(const PetscScalar factor)
MPI_Comm get_mpi_communicator() const
void scale(const VectorBase &scaling_factors)
std::pair< size_type, size_type > local_range() const
void sadd(const PetscScalar s, const VectorBase &V)
void compress(const VectorOperation::values operation)
void set(const std::vector< size_type > &indices, const std::vector< PetscScalar > &values)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
PetscScalar add_and_dot(const PetscScalar a, const VectorBase &V, const VectorBase &W)
const PetscScalar * ghost_vector_array
void swap(VectorBase &v) noexcept
void add(const std::vector< size_type > &indices, const std::vector< PetscScalar > &values)
void write_ascii(const PetscViewerFormat format=PETSC_VIEWER_DEFAULT)
bool has_ghost_elements() const
size_type locally_owned_size() const
void equ(const PetscScalar a, const VectorBase &V)
void do_set_add_operation(const size_type n_elements, const size_type *indices, const PetscScalar *values, const bool add_values)
VectorBase & operator=(const VectorBase &)
size_type size() const override
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
constexpr bool running_in_debug_mode()
Definition config.h:76
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define AssertIntegerConversion(index1, index2)
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcGhostsPresent()
#define Assert(cond, exc)
#define AssertIsFinite(number)
#define AssertThrowMPI(error_code)
#define AssertNothrow(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
const MPI_Comm comm
Definition mpi.cc:912
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
T sum(const T &t, const MPI_Comm mpi_communicator)
bool logical_and(const bool t, const MPI_Comm mpi_communicator)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
Iterator m_iterator