deal.II version GIT relicensing-6809-ge913b9bb34 2026-09-25 17:20: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.h
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
13#ifndef dealii_petsc_vector_h
14#define dealii_petsc_vector_h
15
16
17#include <deal.II/base/config.h>
18
19#ifdef DEAL_II_WITH_PETSC
20
24
27# include <deal.II/lac/vector.h>
30
31#endif // DEAL_II_WITH_PETSC
32
34
35#ifdef DEAL_II_WITH_PETSC
40namespace PETScWrappers
41{
48 namespace MPI
49 {
157 class Vector : public VectorBase
158 {
159 public:
164
168 Vector();
169
174
194 explicit Vector(const MPI_Comm communicator,
195 const size_type n,
197
210 template <typename Number>
211 explicit Vector(const MPI_Comm communicator,
212 const ::Vector<Number> &v,
214
238 Vector(const IndexSet &local,
239 const IndexSet &ghost,
240 const MPI_Comm communicator);
241
253 explicit Vector(const IndexSet &local, const MPI_Comm communicator);
254
258 Vector(const Vector &v);
259
264 virtual void
265 clear() override;
266
295 Vector &
296 operator=(const Vector &v);
297
304 Vector &
305 operator=(const PetscScalar s);
306
316 template <typename number>
317 Vector &
318 operator=(const ::Vector<number> &v);
319
320 using VectorBase::reinit;
321
338 void
339 reinit(const MPI_Comm communicator,
340 const size_type N,
342 const bool omit_zeroing_entries = false);
343
353 void
354 reinit(const Vector &v, const bool omit_zeroing_entries = false);
355
363 void
364 reinit(const IndexSet &local,
365 const IndexSet &ghost,
366 const MPI_Comm communicator);
367
375 void
376 reinit(const IndexSet &local, const MPI_Comm communicator);
377
385 void
386 reinit(
387 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner,
388 const bool make_ghosted = true);
389
401 void
402 print(std::ostream &out,
403 const unsigned int precision = 3,
404 const bool scientific = true,
405 const bool across = true) const;
406
407 protected:
414 virtual void
416 const size_type n,
418
419
420
426 virtual void
428 const size_type n,
430 const IndexSet &ghostnodes);
431 };
432
433
434 // ------------------ template and inline functions -------------
435
436
444 inline void
445 swap(Vector &u, Vector &v) noexcept
446 {
447 u.swap(v);
448 }
449
450
451# ifndef DOXYGEN
452
453 template <typename number>
454 Vector::Vector(const MPI_Comm communicator,
455 const ::Vector<number> &v,
457 {
458 Vector::create_vector(communicator, v.size(), locally_owned_size);
459
460 *this = v;
461 }
462
463
464
465 inline Vector &
466 Vector::operator=(const PetscScalar s)
467 {
469
470 return *this;
471 }
472
473
474
475 template <typename number>
476 inline Vector &
477 Vector::operator=(const ::Vector<number> &v)
478 {
479 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
480
481 // FIXME: the following isn't necessarily fast, but this is due to
482 // the fact that PETSc doesn't offer an inlined access operator.
483 //
484 // if someone wants to contribute some code: to make this code
485 // faster, one could either first convert all values to PetscScalar,
486 // and then set them all at once using VecSetValues. This has the
487 // drawback that it could take quite some memory, if the vector is
488 // large, and it would in addition allocate memory on the heap, which
489 // is expensive. an alternative would be to split the vector into
490 // chunks of, say, 128 elements, convert a chunk at a time and set it
491 // in the output vector using VecSetValues. since 128 elements is
492 // small enough, this could easily be allocated on the stack (as a
493 // local variable) which would make the whole thing much more
494 // efficient.
495 //
496 // a second way to make things faster is for the special case that
497 // number==PetscScalar. we could then declare a specialization of
498 // this template, and omit the conversion. the problem with this is
499 // that the best we can do is to use VecSetValues, but this isn't
500 // very efficient either: it wants to see an array of indices, which
501 // in this case a) again takes up a whole lot of memory on the heap,
502 // and b) is totally dumb since its content would simply be the
503 // sequence 0,1,2,3,...,n. the best of all worlds would probably be a
504 // function in PETSc that would take a pointer to an array of
505 // PetscScalar values and simply copy n elements verbatim into the
506 // vector...
507 for (size_type i = 0; i < v.size(); ++i)
508 (*this)(i) = v(i);
509
511
512 return *this;
513 }
514
515
516
517# endif // DOXYGEN
518 } // namespace MPI
519} // namespace PETScWrappers
520
521namespace internal
522{
523 namespace LinearOperatorImplementation
524 {
525 template <typename>
526 class ReinitHelper;
527
532 template <>
533 class ReinitHelper<PETScWrappers::MPI::Vector>
534 {
535 public:
536 template <typename Matrix>
537 static void
538 reinit_range_vector(const Matrix &matrix,
540 bool /*omit_zeroing_entries*/)
541 {
542 v.reinit(matrix.locally_owned_range_indices(),
543 matrix.get_mpi_communicator());
544 }
545
546 template <typename Matrix>
547 static void
548 reinit_domain_vector(const Matrix &matrix,
550 bool /*omit_zeroing_entries*/)
551 {
552 v.reinit(matrix.locally_owned_domain_indices(),
553 matrix.get_mpi_communicator());
554 }
555 };
556
557 } // namespace LinearOperatorImplementation
558} /* namespace internal */
559
566template <>
567struct is_serial_vector<PETScWrappers::MPI::Vector> : std::false_type
568{};
569
570#endif // DEAL_II_WITH_PETSC
571
573
574#endif
types::global_dof_index size_type
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
void swap(Vector &u, Vector &v) noexcept
Vector & operator=(const ::Vector< number > &v)
Vector & operator=(const Vector &v)
virtual void create_vector(const MPI_Comm comm, const size_type n, const size_type locally_owned_size)
Vector & operator=(const PetscScalar s)
void reinit(const MPI_Comm communicator, const size_type N, const size_type locally_owned_size, const bool omit_zeroing_entries=false)
Vector(const MPI_Comm communicator, const ::Vector< Number > &v, const size_type locally_owned_size)
void compress(const VectorOperation::values operation)
size_type locally_owned_size() const
VectorBase & operator=(const VectorBase &)
size_type size() const override
static void reinit_domain_vector(const Matrix &matrix, PETScWrappers::MPI::Vector &v, bool)
static void reinit_range_vector(const Matrix &matrix, PETScWrappers::MPI::Vector &v, bool)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
const MPI_Comm comm
Definition mpi.cc:912
types::global_dof_index locally_owned_size
Definition mpi.cc:821
unsigned int global_dof_index
Definition types.h:92