Reference documentation for deal.II version GIT relicensing-249-g48dc7357c7 2024-03-29 12:30:02+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: LGPL-2.1-or-later
4// Copyright (C) 2004 - 2024 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_petsc_vector_h
16#define dealii_petsc_vector_h
17
18
19#include <deal.II/base/config.h>
20
21#ifdef DEAL_II_WITH_PETSC
22
26
29# include <deal.II/lac/vector.h>
32
34
35
40namespace PETScWrappers
41{
48 namespace MPI
49 {
157 class Vector : public VectorBase
158 {
159 public:
164
168 Vector();
169
174
191 explicit Vector(const MPI_Comm communicator,
192 const size_type n,
194
205 template <typename Number>
206 explicit Vector(const MPI_Comm communicator,
207 const ::Vector<Number> &v,
209
233 Vector(const IndexSet &local,
234 const IndexSet &ghost,
235 const MPI_Comm communicator);
236
248 explicit Vector(const IndexSet &local, const MPI_Comm communicator);
249
253 Vector(const Vector &v);
254
259 virtual void
260 clear() override;
261
266 Vector &
267 operator=(const Vector &v);
268
275 Vector &
276 operator=(const PetscScalar s);
277
287 template <typename number>
288 Vector &
289 operator=(const ::Vector<number> &v);
290
291 using VectorBase::reinit;
292
309 void
310 reinit(const MPI_Comm communicator,
311 const size_type N,
313 const bool omit_zeroing_entries = false);
314
324 void
325 reinit(const Vector &v, const bool omit_zeroing_entries = false);
326
334 void
335 reinit(const IndexSet &local,
336 const IndexSet &ghost,
337 const MPI_Comm communicator);
338
346 void
347 reinit(const IndexSet &local, const MPI_Comm communicator);
348
356 void
357 reinit(
358 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner,
359 const bool make_ghosted = true);
360
372 void
373 print(std::ostream &out,
374 const unsigned int precision = 3,
375 const bool scientific = true,
376 const bool across = true) const;
377
384 bool
385 all_zero() const;
386
387 protected:
394 virtual void
396 const size_type n,
398
399
400
406 virtual void
408 const size_type n,
410 const IndexSet &ghostnodes);
411 };
412
413
414 // ------------------ template and inline functions -------------
415
416
424 inline void
426 {
427 u.swap(v);
428 }
429
430
431# ifndef DOXYGEN
432
433 template <typename number>
434 Vector::Vector(const MPI_Comm communicator,
435 const ::Vector<number> &v,
436 const size_type locally_owned_size)
437 {
438 Vector::create_vector(communicator, v.size(), locally_owned_size);
439
440 *this = v;
441 }
442
443
444
445 inline Vector &
446 Vector::operator=(const PetscScalar s)
447 {
449
450 return *this;
451 }
452
453
454
455 template <typename number>
456 inline Vector &
457 Vector::operator=(const ::Vector<number> &v)
458 {
459 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
460
461 // FIXME: the following isn't necessarily fast, but this is due to
462 // the fact that PETSc doesn't offer an inlined access operator.
463 //
464 // if someone wants to contribute some code: to make this code
465 // faster, one could either first convert all values to PetscScalar,
466 // and then set them all at once using VecSetValues. This has the
467 // drawback that it could take quite some memory, if the vector is
468 // large, and it would in addition allocate memory on the heap, which
469 // is expensive. an alternative would be to split the vector into
470 // chunks of, say, 128 elements, convert a chunk at a time and set it
471 // in the output vector using VecSetValues. since 128 elements is
472 // small enough, this could easily be allocated on the stack (as a
473 // local variable) which would make the whole thing much more
474 // efficient.
475 //
476 // a second way to make things faster is for the special case that
477 // number==PetscScalar. we could then declare a specialization of
478 // this template, and omit the conversion. the problem with this is
479 // that the best we can do is to use VecSetValues, but this isn't
480 // very efficient either: it wants to see an array of indices, which
481 // in this case a) again takes up a whole lot of memory on the heap,
482 // and b) is totally dumb since its content would simply be the
483 // sequence 0,1,2,3,...,n. the best of all worlds would probably be a
484 // function in PETSc that would take a pointer to an array of
485 // PetscScalar values and simply copy n elements verbatim into the
486 // vector...
487 for (size_type i = 0; i < v.size(); ++i)
488 (*this)(i) = v(i);
489
491
492 return *this;
493 }
494
495
496
497# endif // DOXYGEN
498 } // namespace MPI
499} // namespace PETScWrappers
500
501namespace internal
502{
503 namespace LinearOperatorImplementation
504 {
505 template <typename>
506 class ReinitHelper;
507
512 template <>
513 class ReinitHelper<PETScWrappers::MPI::Vector>
514 {
515 public:
516 template <typename Matrix>
517 static void
518 reinit_range_vector(const Matrix &matrix,
520 bool /*omit_zeroing_entries*/)
521 {
522 v.reinit(matrix.locally_owned_range_indices(),
523 matrix.get_mpi_communicator());
524 }
525
526 template <typename Matrix>
527 static void
528 reinit_domain_vector(const Matrix &matrix,
530 bool /*omit_zeroing_entries*/)
531 {
532 v.reinit(matrix.locally_owned_domain_indices(),
533 matrix.get_mpi_communicator());
534 }
535 };
536
537 } // namespace LinearOperatorImplementation
538} /* namespace internal */
539
546template <>
547struct is_serial_vector<PETScWrappers::MPI::Vector> : std::false_type
548{};
549
550
552
553#endif // DEAL_II_WITH_PETSC
554
555#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
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 swap(Vector &u, Vector &v)
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:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
unsigned int global_dof_index
Definition types.h:81
*braid_SplitCommworld & comm