Debian Codegraph @

include/deal.II/lac/read_write_vector.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2015 - 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_read_write_vector_h
16 #define dealii_read_write_vector_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/aligned_vector.h>
21 #include <deal.II/base/communication_pattern_base.h>
22 #include <deal.II/base/index_set.h>
23 #include <deal.II/base/memory_consumption.h>
24 #include <deal.II/base/mpi_stub.h>
25 #include <deal.II/base/parallel.h>
26 #include <deal.II/base/template_constraints.h>
27 #include <deal.II/base/types.h>
28
29 #include <deal.II/lac/read_vector.h>
30 #include <deal.II/lac/vector_operation.h>
31
32 #include <cstdlib>
33 #include <cstring>
34 #include <iomanip>
35
36 #ifdef DEAL_II_WITH_TRILINOS
37 # include <deal.II/lac/trilinos_epetra_communication_pattern.h>
38 # include <deal.II/lac/trilinos_epetra_vector.h>
39 # include <deal.II/lac/trilinos_tpetra_block_vector.h>
40 # include <deal.II/lac/trilinos_tpetra_vector.h>
41
42 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
43 # include <Epetra_MultiVector.h>
44 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
45
46 #endif
47
48 DEAL_II_NAMESPACE_OPEN
49
50 // Forward declarations
51 #ifndef DOXYGEN
52 template <typename>
53 class Vector;
54
55 namespace LinearAlgebra
56 {
57 template <typename>
58 class Vector;
59 namespace distributed
60 {
61 template <typename, typename>
62 class Vector;
63 } // namespace distributed
64 } // namespace LinearAlgebra
65
66 # ifdef DEAL_II_WITH_PETSC
67 namespace PETScWrappers
68 {
69 namespace MPI
70 {
71 class Vector;
72 }
73 } // namespace PETScWrappers
74 # endif
75
76 # ifdef DEAL_II_WITH_TRILINOS
77 namespace TrilinosWrappers
78 {
79 namespace MPI
80 {
81 class Vector;
82 }
83 } // namespace TrilinosWrappers
84 # endif
85
86 #endif
87
88 namespace LinearAlgebra
89 {
90 /**
91 * @addtogroup Vectors
92 * @{
93 */
94
95 /**
96 * ReadWriteVector is intended to represent vectors in ${\mathbb R}^N$ for
97 * which it stores all or a subset of elements. The latter case in important
98 * in parallel computations, where $N$ may be so large that no processor can
99 * actually store all elements of a solution vector, but where this is also
100 * not necessary: one typically only has to store the values of degrees of
101 * freedom that live on cells that are locally owned plus potentially those
102 * degrees of freedom that live on ghost cells.
103 *
104 * This class provides access to individual elements to be read or written.
105 * However, it does not allow global operations such as taking the norm
106 * or dot products between vectors.
107 *
108 * <h3>Storing elements</h3>
109 * Most of the time, one will simply read from or
110 * write into a vector of the current class using the global numbers of
111 * these degrees of freedom. This is done using operator()() or operator[]()
112 * which call global_to_local() to transform the <i>global</i> index into a
113 * <i>local</i> one. In such cases, it is clear that one can only access
114 * elements of the vector that the current object indeed stores.
115 *
116 * However, it is also possible to access elements in the order in which
117 * they are stored by the current object. In other words, one is not
118 * interested in accessing elements with their <i>global</i> indices, but
119 * instead using an enumeration that only takes into account the elements
120 * that are actually stored. This is facilitated by the local_element()
121 * function. To this end, it is necessary to know <i>in which order</i> the
122 * current class stores its element. The elements of all the consecutive
123 * ranges are stored in ascending order of the first index of each range.
124 * The function IndexSet::largest_range_starting_index() can be used to
125 * get the first index of the largest range.
126 */
127 template <typename Number>
128 class ReadWriteVector : public ReadVector<Number>
129 {
130 public:
131 /**
132 * Declare standard types used in all containers. These types parallel
133 * those in the <tt>C++</tt> standard libraries <tt>vector<...></tt>
134 * class.
135 */
136 using value_type = Number;
137 using pointer = value_type *;
138 using const_pointer = const value_type *;
139 using iterator = value_type *;
140 using const_iterator = const value_type *;
141 using reference = value_type &;
142 using const_reference = const value_type &;
143 using size_type = types::global_dof_index;
144 using real_type = typename numbers::NumberTraits<Number>::real_type;
145
146 /**
147 * @name 1: Basic Object-handling
148 */
149 /** @{ */
150 /**
151 * Empty constructor.
152 */
153 ReadWriteVector();
154
155 /**
156 * Copy constructor.
157 */
158 ReadWriteVector(const ReadWriteVector<Number> &in_vector);
159
160 /**
161 * Construct a vector given the size, the stored elements have their
162 * index in [0,size).
163 */
164 explicit ReadWriteVector(const size_type size);
165
166 /**
167 * Construct a vector whose stored elements indices are given by the
168 * IndexSet @p locally_stored_indices.
169 */
170 explicit ReadWriteVector(const IndexSet &locally_stored_indices);
171
172 /**
173 * Destructor.
174 */
175 ~ReadWriteVector() override = default;
176
177 /**
178 * Set the global size of the vector to @p size. The stored elements have
179 * their index in [0,size).
180 *
181 * If the flag @p omit_zeroing_entries is set to false, the memory will be
182 * initialized with zero, otherwise the memory will be untouched (and the
183 * user must make sure to fill it with reasonable data before using it).
184 */
185 virtual void
186 reinit(const size_type size, const bool omit_zeroing_entries = false);
187
188 /**
189 * Uses the same IndexSet as the one of the input vector @p in_vector and
190 * allocates memory for this vector.
191 *
192 * If the flag @p omit_zeroing_entries is set to false, the memory will be
193 * initialized with zero, otherwise the memory will be untouched (and the
194 * user must make sure to fill it with reasonable data before using it).
195 */
196 template <typename Number2>
197 void
198 reinit(const ReadWriteVector<Number2> &in_vector,
199 const bool omit_zeroing_entries = false);
200
201 /**
202 * Initializes the vector. The indices are specified by @p
203 * locally_stored_indices.
204 *
205 * If the flag @p omit_zeroing_entries is set to false, the memory will be
206 * initialized with zero, otherwise the memory will be untouched (and the
207 * user must make sure to fill it with reasonable data before using it).
208 * locally_stored_indices.
209 */
210 virtual void
211 reinit(const IndexSet &locally_stored_indices,
212 const bool omit_zeroing_entries = false);
213
214
215 #ifdef DEAL_II_WITH_TRILINOS
216 /**
217 * Initialize this ReadWriteVector by supplying access to all locally
218 * available entries in the given ghosted or non-ghosted vector.
219 *
220 * @note This function currently copies the values from the argument into
221 * the ReadWriteVector, so modifications here will not modify @p trilinos_vec.
222 *
223 * This function is mainly written for backwards-compatibility to get
224 * element access to a ghosted TrilinosWrappers::MPI::Vector inside the
225 * library.
226 */
227 void
228 reinit(const TrilinosWrappers::MPI::Vector &trilinos_vec);
229 #endif
230
231 /**
232 * Apply the functor @p func to each element of the vector. The functor
233 * should look like
234 * @code
235 * struct Functor
236 * {
237 * void operator() (Number &value);
238 * };
239 * @endcode
240 *
241 * @note This function requires that the header read_write_vector.templates.h
242 * be included.
243 */
244 template <typename Functor>
245 void
246 apply(const Functor &func);
247
248 /**
249 * Swap the contents of this vector and the other vector @p v. One could
250 * do this operation with a temporary variable and copying over the data
251 * elements, but this function is significantly more efficient since it
252 * only swaps the pointers to the data of the two vectors and therefore
253 * does not need to allocate temporary storage and move data around.
254 *
255 * This function is analogous to the @p swap function of all C++
256 * standard containers. Also, there is a global function
257 * <tt>swap(u,v)</tt> that simply calls <tt>u.swap(v)</tt>, again in
258 * analogy to standard functions.
259 */
260 void
261 swap(ReadWriteVector<Number> &v) noexcept;
262
263 /**
264 * Copies the data and the IndexSet of the input vector @p in_vector.
265 */
266 ReadWriteVector<Number> &
267 operator=(const ReadWriteVector<Number> &in_vector);
268
269 /**
270 * Copies the data and the IndexSet of the input vector @p in_vector.
271 */
272 template <typename Number2>
273 ReadWriteVector<Number> &
274 operator=(const ReadWriteVector<Number2> &in_vector);
275
276 /**
277 * Sets all elements of the vector to the scalar @p s. This operation is
278 * only allowed if @p s is equal to zero.
279 */
280 ReadWriteVector<Number> &
281 operator=(const Number s);
282
283 /**
284 * Imports all the elements present in the vector's IndexSet from the
285 * input vector @p vec. VectorOperation::values @p operation
286 * is used to decide if the elements in @p V should be added to the
287 * current vector or replace the current elements.
288 *
289 * @note The parameter @p communication_pattern is ignored since we are
290 * dealing with a serial vector here.
291 */
292 void
293 import_elements(
294 const dealii::Vector<Number> &vec,
295 VectorOperation::values operation,
296 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
297 &communication_pattern = {});
298
299 /**
300 * @deprecated Use import_elements() instead.
301 */
302 DEAL_II_DEPRECATED
303 void
304 import(const dealii::Vector<Number> &V,
305 VectorOperation::values operation,
306 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
307 &communication_pattern = {})
308 {
309 import_elements(V, operation, communication_pattern);
310 }
311
312 /**
313 * Imports all the elements present in the vector's IndexSet from the
314 * input vector @p vec. VectorOperation::values @p operation
315 * is used to decide if the elements in @p V should be added to the
316 * current vector or replace the current elements. The last parameter can
317 * be used if the same communication pattern is used multiple times. This
318 * can be used to improve performance.
319 */
320 template <typename MemorySpace>
321 void
322 import_elements(
323 const distributed::Vector<Number, MemorySpace> &vec,
324 VectorOperation::values operation,
325 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
326 &communication_pattern = {});
327
328 /**
329 * @deprecated Use import_elements() instead.
330 */
331 template <typename MemorySpace>
332 DEAL_II_DEPRECATED void
333 import(const distributed::Vector<Number, MemorySpace> &V,
334 VectorOperation::values operation,
335 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
336 &communication_pattern = {})
337 {
338 import_elements(V, operation, communication_pattern);
339 }
340
341
342 #ifdef DEAL_II_WITH_PETSC
343 /**
344 * Imports all the elements present in the vector's IndexSet from the input
345 * vector @p petsc_vec. VectorOperation::values @p operation is used to decide
346 * if the elements in @p V should be added to the current vector or replace
347 * the current elements. The last parameter can be used if the same
348 * communication pattern is used multiple times. This can be used to improve
349 * performance.
350 */
351 void
352 import_elements(
353 const PETScWrappers::MPI::Vector &petsc_vec,
354 VectorOperation::values operation,
355 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
356 &communication_pattern = {});
357
358 /**
359 * @deprecated Use import_elements() instead.
360 */
361 DEAL_II_DEPRECATED
362 void
363 import(const PETScWrappers::MPI::Vector &V,
364 VectorOperation::values operation,
365 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
366 &communication_pattern = {})
367 {
368 import_elements(V, operation, communication_pattern);
369 }
370 #endif
371
372 #ifdef DEAL_II_WITH_TRILINOS
373 /**
374 * Imports all the elements present in the vector's IndexSet from the input
375 * vector @p trilinos_vec. VectorOperation::values @p operation is used to
376 * decide if the elements in @p V should be added to the current vector or
377 * replace the current elements. The last parameter can be used if the same
378 * communication pattern is used multiple times. This can be used to improve
379 * performance.
380 *
381 * @note The @p trilinos_vec is not allowed to have ghost entries.
382 */
383 void
384 import_elements(
385 const TrilinosWrappers::MPI::Vector &trilinos_vec,
386 VectorOperation::values operation,
387 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
388 &communication_pattern = {});
389
390 /**
391 * @deprecated Use import_elements() instead.
392 */
393 DEAL_II_DEPRECATED
394 void
395 import(const TrilinosWrappers::MPI::Vector &V,
396 VectorOperation::values operation,
397 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
398 &communication_pattern = {})
399 {
400 import_elements(V, operation, communication_pattern);
401 }
402
403 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
404 /**
405 * Imports all the elements present in the vector's IndexSet from the input
406 * vector @p tpetra_vec. VectorOperation::values @p operation is used to
407 * decide if the elements in @p V should be added to the current vector or
408 * replace the current elements. The last parameter can be used if the same
409 * communication pattern is used multiple times. This can be used to improve
410 * performance.
411 */
412 template <typename MemorySpace, typename Dummy = Number>
413 std::enable_if_t<std::is_same_v<Dummy, Number> &&
414 dealii::is_tpetra_type<Number>::value>
415 import_elements(
416 const TpetraWrappers::Vector<Number, MemorySpace> &tpetra_vec,
417 VectorOperation::values operation,
418 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
419 &communication_pattern = {});
420
421 /**
422 * @deprecated Use import_elements() instead.
423 */
424 template <typename MemorySpace, typename Dummy = Number>
425 DEAL_II_DEPRECATED std::enable_if_t<std::is_same_v<Dummy, Number> &&
426 dealii::is_tpetra_type<Number>::value>
427 import(const TpetraWrappers::Vector<Number, MemorySpace> &V,
428 VectorOperation::values operation,
429 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
430 &communication_pattern = {})
431 {
432 import_elements(V, operation, communication_pattern);
433 }
434 # endif
435
436 /**
437 * Imports all the elements present in the vector's IndexSet from the input
438 * vector @p epetra_vec. VectorOperation::values @p operation is used to
439 * decide if the elements in @p V should be added to the current vector or
440 * replace the current elements. The last parameter can be used if the same
441 * communication pattern is used multiple times. This can be used to improve
442 * performance.
443 */
444 void
445 import_elements(
446 const EpetraWrappers::Vector &epetra_vec,
447 VectorOperation::values operation,
448 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
449 &communication_pattern = {});
450
451 /**
452 * @deprecated Use import_elements() instead.
453 */
454 DEAL_II_DEPRECATED
455 void
456 import(const EpetraWrappers::Vector &V,
457 VectorOperation::values operation,
458 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
459 &communication_pattern = {})
460 {
461 import_elements(V, operation, communication_pattern);
462 }
463 #endif
464
465 /**
466 * The value returned by this function denotes the dimension of the vector
467 * spaces that are modeled by objects of this kind. However, objects of
468 * the current class do not actually stores all elements of vectors of
469 * this space but may, in fact store only a subset. The number of elements
470 * stored is returned by n_elements() and is smaller or equal to the
471 * number returned by the current function.
472 */
473 size_type
474 size() const override;
475
476 /**
477 * Return the local size of the vector, i.e., the number of indices
478 * owned locally.
479 */
480 size_type
481 locally_owned_size() const;
482
483 /**
484 * Return the IndexSet that represents the indices of the elements stored.
485 */
486 const IndexSet &
487 get_stored_elements() const;
488
489 /**
490 * Make the @p ReadWriteVector class a bit like the <tt>vector<></tt>
491 * class of the C++ standard library by returning iterators to the start
492 * and end of the <i>locally stored</i> elements of this vector.
493 */
494 iterator
495 begin();
496
497 /**
498 * Return constant iterator to the start of the locally stored elements
499 * of the vector.
500 */
501 const_iterator
502 begin() const;
503
504 /**
505 * Return an iterator pointing to the element past the end of the array
506 * of locally stored entries.
507 */
508 iterator
509 end();
510
511 /**
512 * Return a constant iterator pointing to the element past the end of the
513 * array of the locally stored entries.
514 */
515 const_iterator
516 end() const;
517 /** @} */
518
519
520 /**
521 * @name 2: Data-Access
522 */
523 /** @{ */
524
525 /**
526 * Read access to the data in the position corresponding to @p
527 * global_index. An exception is thrown if @p global_index is not stored
528 * by the current object.
529 */
530 Number
531 operator()(const size_type global_index) const;
532
533 /**
534 * Read and write access to the data in the position corresponding to @p
535 * global_index. An exception is thrown if @p global_index is not stored
536 * by the current object.
537 */
538 Number &
539 operator()(const size_type global_index);
540
541 /**
542 * Read access to the data in the position corresponding to @p
543 * global_index. An exception is thrown if @p global_index is not stored
544 * by the current object.
545 *
546 * This function does the same thing as operator().
547 */
548 Number
549 operator[](const size_type global_index) const;
550
551 /**
552 * Read and write access to the data in the position corresponding to @p
553 * global_index. An exception is thrown if @p global_index is not stored
554 * by the current object.
555 *
556 * This function does the same thing as operator().
557 */
558 Number &
559 operator[](const size_type global_index);
560
561 /**
562 * Instead of getting individual elements of a vector via operator(),
563 * this function allows getting a whole set of elements at once. The
564 * indices of the elements to be read are stated in the first argument, the
565 * corresponding values are returned in the second.
566 *
567 * If the current vector is called @p v, then this function is the equivalent
568 * to the code
569 * @code
570 * for (unsigned int i=0; i<indices.size(); ++i)
571 * values[i] = v[indices[i]];
572 * @endcode
573 *
574 * @pre The sizes of the @p indices and @p values arrays must be identical.
575 */
576 template <typename Number2>
577 void
578 extract_subvector_to(const std::vector<size_type> &indices,
579 std::vector<Number2> &values) const;
580
581 /**
582 * Extract a range of elements all at once.
583 */
584 virtual void
585 extract_subvector_to(
586 const ArrayView<const types::global_dof_index> &indices,
587 const ArrayView<Number> &entries) const override;
588
589 /**
590 * Instead of getting individual elements of a vector via operator(),
591 * this function allows getting a whole set of elements at once. In
592 * contrast to the previous function, this function obtains the
593 * indices of the elements by dereferencing all elements of the iterator
594 * range provided by the first two arguments, and puts the vector
595 * values into memory locations obtained by dereferencing a range
596 * of iterators starting at the location pointed to by the third
597 * argument.
598 *
599 * If the current vector is called @p v, then this function is the equivalent
600 * to the code
601 * @code
602 * ForwardIterator indices_p = indices_begin;
603 * OutputIterator values_p = values_begin;
604 * while (indices_p != indices_end)
605 * {
606 * *values_p = v[*indices_p];
607 * ++indices_p;
608 * ++values_p;
609 * }
610 * @endcode
611 *
612 * @pre It must be possible to write into as many memory locations
613 * starting at @p values_begin as there are iterators between
614 * @p indices_begin and @p indices_end.
615 */
616 template <typename ForwardIterator, typename OutputIterator>
617 void
618 extract_subvector_to(ForwardIterator indices_begin,
619 const ForwardIterator indices_end,
620 OutputIterator values_begin) const;
621
622 /**
623 * Read access to the data field specified by @p local_index. When you
624 * access elements in the order in which they are stored, it is necessary
625 * that you know in which they are stored. In other words, you need to
626 * know the map between the global indices of the elements this class
627 * stores, and the local indices into the contiguous array of these global
628 * elements. For this, see the general documentation of this class.
629 *
630 * Performance: Direct array access (fast).
631 */
632 Number
633 local_element(const size_type local_index) const;
634
635 /**
636 * Read and write access to the data field specified by @p local_index.
637 * When you access elements in the order in which they are stored, it is
638 * necessary that you know in which they are stored. In other words, you
639 * need to know the map between the global indices of the elements this
640 * class stores, and the local indices into the contiguous array of these
641 * global elements. For this, see the general documentation of this class.
642 *
643 * Performance: Direct array access (fast).
644 */
645 Number &
646 local_element(const size_type local_index);
647 /** @} */
648
649
650 /**
651 * @name 3: Modification of vectors
652 */
653 /** @{ */
654
655 /**
656 * This function adds a whole set of values stored in @p values to the
657 * vector components specified by @p indices.
658 */
659 template <typename Number2>
660 void
661 add(const std::vector<size_type> &indices,
662 const std::vector<Number2> &values);
663
664 /**
665 * This function is similar to the previous one but takes a
666 * ReadWriteVector of values.
667 */
668 template <typename Number2>
669 void
670 add(const std::vector<size_type> &indices,
671 const ReadWriteVector<Number2> &values);
672
673 /**
674 * Take an address where <tt>n_elements</tt> are stored contiguously and
675 * add them into the vector. Handles all cases which are not covered by
676 * the other two <tt>add()</tt> functions above.
677 */
678 template <typename Number2>
679 void
680 add(const size_type n_elements,
681 const size_type *indices,
682 const Number2 *values);
683
684 /**
685 * Prints the vector to the output stream @p out.
686 */
687 void
688 print(std::ostream &out,
689 const unsigned int precision = 3,
690 const bool scientific = true) const;
691
692 /**
693 * Return the memory consumption of this class in bytes.
694 */
695 std::size_t
696 memory_consumption() const;
697 /** @} */
698
699 protected:
700 #ifdef DEAL_II_WITH_TRILINOS
701 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
702 /**
703 * Import all the elements present in the vector's IndexSet from the input
704 * vector @p tpetra_vector. This is an helper function and it should not be
705 * used directly.
706 */
707 template <typename MemorySpace, typename Dummy = Number>
708 std::enable_if_t<std::is_same_v<Dummy, Number> &&
709 dealii::is_tpetra_type<Number>::value>
710 import_elements(
711 const Tpetra::
712 Vector<Number, int, types::signed_global_dof_index, MemorySpace>
713 &tpetra_vector,
714 const IndexSet &locally_owned_elements,
715 VectorOperation::values operation,
716 const MPI_Comm mpi_comm,
717 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
718 &communication_pattern);
719 # endif
720
721 /**
722 * Import all the elements present in the vector's IndexSet from the input
723 * vector @p multivector. This is an helper function and it should not be
724 * used directly.
725 */
726 void
727 import_elements(
728 const Epetra_MultiVector &multivector,
729 const IndexSet &locally_owned_elements,
730 VectorOperation::values operation,
731 const MPI_Comm mpi_comm,
732 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
733 &communication_pattern);
734 #endif
735
736 /**
737 * Return the local position of @p global_index.
738 */
739 unsigned int
740 global_to_local(const types::global_dof_index global_index) const;
741
742 #ifdef DEAL_II_WITH_TRILINOS
743 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
744 /**
745 * Return a TpetraWrappers::CommunicationPattern and store it for future
746 * use.
747 */
748 template <typename MemorySpace = dealii::MemorySpace::Host>
749 TpetraWrappers::CommunicationPattern<MemorySpace>
750 create_tpetra_comm_pattern(const IndexSet &source_index_set,
751 const MPI_Comm mpi_comm);
752 # endif
753
754 /**
755 * Return a EpetraWrappers::CommunicationPattern and store it for future
756 * use.
757 */
758 EpetraWrappers::CommunicationPattern
759 create_epetra_comm_pattern(const IndexSet &source_index_set,
760 const MPI_Comm mpi_comm);
761 #endif
762
763 /**
764 * Indices of the elements stored.
765 */
766 IndexSet stored_elements;
767
768 /**
769 * IndexSet of the elements of the last imported vector;
770 */
771 IndexSet source_stored_elements;
772
773 /**
774 * CommunicationPattern for the communication between the
775 * source_stored_elements IndexSet and the current vector.
776 */
777 std::shared_ptr<Utilities::MPI::CommunicationPatternBase> comm_pattern;
778
779 /**
780 * Locally stored elements.
781 */
782 AlignedVector<Number> values;
783
784 /**
785 * For parallel loops with TBB, this member variable stores the affinity
786 * information of loops.
787 */
788 mutable std::shared_ptr<::dealii::parallel::internal::TBBPartitioner>
789 thread_loop_partitioner;
790
791 // Make all other ReadWriteVector types friends.
792 template <typename Number2>
793 friend class ReadWriteVector;
794
795 private:
796 /**
797 * This class provides a wrapper around a Functor which acts on
798 * single elements of the vector. This is necessary to use
799 * tbb::parallel_for which requires a TBBForFunctor.
800 */
801 template <typename Functor>
802 class FunctorTemplate
803 {
804 public:
805 /**
806 * Constructor. Take a functor and store a copy of it.
807 */
808 FunctorTemplate(ReadWriteVector<Number> &parent, const Functor &functor);
809
810 /**
811 * Evaluate the element with the stored copy of the functor.
812 */
813 virtual void
814 operator()(const size_type begin, const size_type end);
815
816 private:
817 /**
818 * Alias to the ReadWriteVector object that owns the FunctorTemplate.
819 */
820 ReadWriteVector &parent;
821
822 /**
823 * Copy of the functor.
824 */
825 const Functor &functor;
826 };
827 };
828
829 /** @} */
830
831
832 /*---------------------------- Inline functions ---------------------------*/
833
834 #ifndef DOXYGEN
835
836
837 template <typename Number>
838 inline ReadWriteVector<Number>::ReadWriteVector()
839 {
840 // virtual functions called in constructors and destructors never use the
841 // override in a derived class
842 // for clarity be explicit on which function is called
843 ReadWriteVector<Number>::reinit(0, true);
844 }
845
846
847
848 template <typename Number>
849 inline ReadWriteVector<Number>::ReadWriteVector(
850 const ReadWriteVector<Number> &v)
851 {
852 this->operator=(v);
853 }
854
855
856
857 template <typename Number>
858 inline ReadWriteVector<Number>::ReadWriteVector(const size_type size)
859 {
860 // virtual functions called in constructors and destructors never use the
861 // override in a derived class
862 // for clarity be explicit on which function is called
863 ReadWriteVector<Number>::reinit(size, false);
864 }
865
866
867
868 template <typename Number>
869 inline ReadWriteVector<Number>::ReadWriteVector(
870 const IndexSet &locally_stored_indices)
871 {
872 // virtual functions called in constructors and destructors never use the
873 // override in a derived class
874 // for clarity be explicit on which function is called
875 ReadWriteVector<Number>::reinit(locally_stored_indices);
876 }
877
878
879
880 template <typename Number>
881 inline typename ReadWriteVector<Number>::size_type
882 ReadWriteVector<Number>::size() const
883 {
884 return stored_elements.size();
885 }
886
887
888
889 template <typename Number>
890 inline typename ReadWriteVector<Number>::size_type
891 ReadWriteVector<Number>::locally_owned_size() const
892 {
893 return stored_elements.n_elements();
894 }
895
896
897
898 template <typename Number>
899 inline const IndexSet &
900 ReadWriteVector<Number>::get_stored_elements() const
901 {
902 return stored_elements;
903 }
904
905
906
907 template <typename Number>
908 inline typename ReadWriteVector<Number>::iterator
909 ReadWriteVector<Number>::begin()
910 {
911 return values.begin();
912 }
913
914
915
916 template <typename Number>
917 inline typename ReadWriteVector<Number>::const_iterator
918 ReadWriteVector<Number>::begin() const
919 {
920 return values.begin();
921 }
922
923
924
925 template <typename Number>
926 inline typename ReadWriteVector<Number>::iterator
927 ReadWriteVector<Number>::end()
928 {
929 return values.end();
930 }
931
932
933
934 template <typename Number>
935 inline typename ReadWriteVector<Number>::const_iterator
936 ReadWriteVector<Number>::end() const
937 {
938 return values.end();
939 }
940
941
942
943 template <typename Number>
944 inline Number
945 ReadWriteVector<Number>::operator()(const size_type global_index) const
946 {
947 return values[global_to_local(global_index)];
948 }
949
950
951
952 template <typename Number>
953 inline Number &
954 ReadWriteVector<Number>::operator()(const size_type global_index)
955 {
956 return values[global_to_local(global_index)];
957 }
958
959
960
961 template <typename Number>
962 inline Number
963 ReadWriteVector<Number>::operator[](const size_type global_index) const
964 {
965 return operator()(global_index);
966 }
967
968
969
970 template <typename Number>
971 inline Number &
972 ReadWriteVector<Number>::operator[](const size_type global_index)
973 {
974 return operator()(global_index);
975 }
976
977
978
979 template <typename Number>
980 template <typename Number2>
981 inline void
982 ReadWriteVector<Number>::extract_subvector_to(
983 const std::vector<size_type> &indices,
984 std::vector<Number2> &extracted_values) const
985 {
986 for (size_type i = 0; i < indices.size(); ++i)
987 extracted_values[i] = operator()(indices[i]);
988 }
989
990
991
992 template <typename Number>
993 void
994 ReadWriteVector<Number>::extract_subvector_to(
995 const ArrayView<const types::global_dof_index> &indices,
996 const ArrayView<Number> &entries) const
997 {
998 AssertDimension(indices.size(), entries.size());
999 for (unsigned int i = 0; i < indices.size(); ++i)
1000 entries[i] = (*this)[indices[i]];
1001 }
1002
1003
1004
1005 template <typename Number>
1006 template <typename ForwardIterator, typename OutputIterator>
1007 inline void
1008 ReadWriteVector<Number>::extract_subvector_to(
1009 ForwardIterator indices_begin,
1010 const ForwardIterator indices_end,
1011 OutputIterator values_begin) const
1012 {
1013 while (indices_begin != indices_end)
1014 {
1015 *values_begin = operator()(*indices_begin);
1016 ++indices_begin;
1017 ++values_begin;
1018 }
1019 }
1020
1021
1022
1023 template <typename Number>
1024 inline Number
1025 ReadWriteVector<Number>::local_element(const size_type local_index) const
1026 {
1027 AssertIndexRange(local_index, this->locally_owned_size());
1028
1029 return values[local_index];
1030 }
1031
1032
1033
1034 template <typename Number>
1035 inline Number &
1036 ReadWriteVector<Number>::local_element(const size_type local_index)
1037 {
1038 AssertIndexRange(local_index, this->locally_owned_size());
1039
1040 return values[local_index];
1041 }
1042
1043
1044
1045 template <typename Number>
1046 template <typename Number2>
1047 inline void
1048 ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1049 const std::vector<Number2> &values)
1050 {
1051 AssertDimension(indices.size(), values.size());
1052 add(indices.size(), indices.data(), values.data());
1053 }
1054
1055
1056
1057 template <typename Number>
1058 template <typename Number2>
1059 inline void
1060 ReadWriteVector<Number>::add(const std::vector<size_type> &indices,
1061 const ReadWriteVector<Number2> &values)
1062 {
1063 const size_type size = indices.size();
1064 for (size_type i = 0; i < size; ++i)
1065 {
1066 Assert(
1067 numbers::is_finite(values[i]),
1068 ExcMessage(
1069 "The given value is not finite but either infinite or Not A Number (NaN)"));
1070 this->operator()(indices[i]) += values[indices[i]];
1071 }
1072 }
1073
1074
1075
1076 template <typename Number>
1077 template <typename Number2>
1078 inline void
1079 ReadWriteVector<Number>::add(const size_type n_indices,
1080 const size_type *indices,
1081 const Number2 *values_to_add)
1082 {
1083 for (size_type i = 0; i < n_indices; ++i)
1084 {
1085 Assert(
1086 numbers::is_finite(values[i]),
1087 ExcMessage(
1088 "The given value is not finite but either infinite or Not A Number (NaN)"));
1089 this->operator()(indices[i]) += values_to_add[i];
1090 }
1091 }
1092
1093
1094
1095 template <typename Number>
1096 inline unsigned int
1097 ReadWriteVector<Number>::global_to_local(
1098 const types::global_dof_index global_index) const
1099 {
1100 // the following will throw an exception if the global_index is not
1101 // in the remaining_elements
1102 return static_cast<unsigned int>(
1103 stored_elements.index_within_set(global_index));
1104 }
1105
1106
1107
1108 template <typename Number>
1109 template <typename Functor>
1110 inline ReadWriteVector<Number>::FunctorTemplate<Functor>::FunctorTemplate(
1111 ReadWriteVector<Number> &parent,
1112 const Functor &functor)
1113 : parent(parent)
1114 , functor(functor)
1115 {}
1116
1117
1118
1119 template <typename Number>
1120 template <typename Functor>
1121 void
1122 ReadWriteVector<Number>::FunctorTemplate<Functor>::operator()(
1123 const size_type begin,
1124 const size_type end)
1125 {
1126 for (size_type i = begin; i < end; ++i)
1127 functor(parent.values[i]);
1128 }
1129
1130 #endif // ifndef DOXYGEN
1131
1132 } // end of namespace LinearAlgebra
1133
1134
1135
1136 /**
1137 * Global function @p swap which overloads the default implementation of the
1138 * C++ standard library which uses a temporary object. The function simply
1139 * exchanges the data of the two vectors.
1140 *
1141 * @relatesalso Vector
1142 */
1143 template <typename Number>
1144 inline void
1145 swap(LinearAlgebra::ReadWriteVector<Number> &u,
1146 LinearAlgebra::ReadWriteVector<Number> &v) noexcept
1147 {
1148 u.swap(v);
1149 }
1150
1151
1152 DEAL_II_NAMESPACE_CLOSE
1153
1154 #endif