Line data Source code
1 : // The libMesh Finite Element Library.
2 : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 :
4 : // This library is free software; you can redistribute it and/or
5 : // modify it under the terms of the GNU Lesser General Public
6 : // License as published by the Free Software Foundation; either
7 : // version 2.1 of the License, or (at your option) any later version.
8 :
9 : // This library is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : // Lesser General Public License for more details.
13 :
14 : // You should have received a copy of the GNU Lesser General Public
15 : // License along with this library; if not, write to the Free Software
16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 :
18 :
19 :
20 :
21 : #ifndef LIBMESH_PETSC_VECTOR_H
22 : #define LIBMESH_PETSC_VECTOR_H
23 :
24 :
25 : #include "libmesh/libmesh_config.h"
26 :
27 : #ifdef LIBMESH_HAVE_PETSC
28 :
29 : // Local includes
30 : #include "libmesh/numeric_vector.h"
31 : #include "libmesh/petsc_macro.h"
32 : #include "libmesh/int_range.h"
33 : #include "libmesh/libmesh_common.h"
34 : #include "libmesh/petsc_solver_exception.h"
35 : #include "libmesh/parallel_only.h"
36 : #include "libmesh/enum_to_string.h"
37 :
38 : // PETSc include files.
39 : #ifdef I
40 : # define LIBMESH_SAW_I
41 : #endif
42 : #include <petscvec.h>
43 : #ifndef LIBMESH_SAW_I
44 : # undef I // Avoid complex.h contamination
45 : #endif
46 :
47 : // C++ includes
48 : #include <cstddef>
49 : #include <cstring>
50 : #include <vector>
51 : #include <unordered_map>
52 : #include <limits>
53 :
54 : #include <atomic>
55 : #include <mutex>
56 : #include <condition_variable>
57 :
58 : namespace libMesh
59 : {
60 :
61 : // forward declarations
62 : template <typename T> class SparseMatrix;
63 :
64 : /**
65 : * This class provides a nice interface to PETSc's Vec object. All
66 : * overridden virtual functions are documented in numeric_vector.h.
67 : *
68 : * \author Benjamin S. Kirk
69 : * \date 2002
70 : * \brief NumericVector interface to PETSc Vec.
71 : */
72 : template <typename T>
73 : class PetscVector final : public NumericVector<T>
74 : {
75 : public:
76 :
77 : /**
78 : * Dummy-Constructor. Dimension=0
79 : */
80 : explicit
81 : PetscVector (const Parallel::Communicator & comm_in,
82 : const ParallelType type = AUTOMATIC);
83 :
84 : /**
85 : * Constructor. Set dimension to \p n and initialize all elements with zero.
86 : */
87 : explicit
88 : PetscVector (const Parallel::Communicator & comm_in,
89 : const numeric_index_type n,
90 : const ParallelType type = AUTOMATIC);
91 :
92 : /**
93 : * Constructor. Set local dimension to \p n_local, the global dimension
94 : * to \p n, and initialize all elements with zero.
95 : */
96 : PetscVector (const Parallel::Communicator & comm_in,
97 : const numeric_index_type n,
98 : const numeric_index_type n_local,
99 : const ParallelType type = AUTOMATIC);
100 :
101 : /**
102 : * Constructor. Set local dimension to \p n_local, the global
103 : * dimension to \p n, but additionally reserve memory for the
104 : * indices specified by the \p ghost argument.
105 : */
106 : PetscVector (const Parallel::Communicator & comm_in,
107 : const numeric_index_type N,
108 : const numeric_index_type n_local,
109 : const std::vector<numeric_index_type> & ghost,
110 : const ParallelType type = AUTOMATIC);
111 :
112 : /**
113 : * Constructor. Creates a PetscVector assuming you already have a
114 : * valid PETSc Vec object. In this case, \p v is NOT destroyed by the
115 : * PetscVector constructor when this object goes out of scope.
116 : * This allows ownership of \p v to remain with the original creator,
117 : * and to simply provide additional functionality with the PetscVector.
118 : */
119 : PetscVector(Vec v,
120 : const Parallel::Communicator & comm_in);
121 :
122 : /**
123 : * Copy assignment operator.
124 : * Supported assignments based on ParallelType combination (note that we lump ghosted into
125 : * parallel for this method documentation):
126 : * - Assign from parallel to parallel
127 : * - Assign from serial to serial
128 : * - Assign from parallel to serial
129 : * \returns A reference to *this as the derived type.
130 : */
131 : PetscVector<T> & operator= (const PetscVector<T> & v);
132 :
133 : /**
134 : * This class manages a C-style struct (Vec) manually, so we
135 : * don't want to allow any automatic copy/move functions to be
136 : * generated, and we can't default the destructor.
137 : */
138 : PetscVector (PetscVector &&) = delete;
139 : PetscVector (const PetscVector &) = delete;
140 : PetscVector & operator= (PetscVector &&) = delete;
141 : virtual ~PetscVector ();
142 :
143 : virtual void close () override;
144 :
145 : /**
146 : * clear() is called from the destructor, so it should not throw.
147 : */
148 : virtual void clear () noexcept override;
149 :
150 : virtual void zero () override;
151 :
152 : virtual std::unique_ptr<NumericVector<T>> zero_clone () const override;
153 :
154 : virtual std::unique_ptr<NumericVector<T>> clone () const override;
155 :
156 : virtual void init (const numeric_index_type N,
157 : const numeric_index_type n_local,
158 : const bool fast=false,
159 : const ParallelType type=AUTOMATIC) override;
160 :
161 : virtual void init (const numeric_index_type N,
162 : const bool fast=false,
163 : const ParallelType type=AUTOMATIC) override;
164 :
165 : virtual void init (const numeric_index_type N,
166 : const numeric_index_type n_local,
167 : const std::vector<numeric_index_type> & ghost,
168 : const bool fast = false,
169 : const ParallelType = AUTOMATIC) override;
170 :
171 : virtual void init (const NumericVector<T> & other,
172 : const bool fast = false) override;
173 :
174 : virtual NumericVector<T> & operator= (const T s) override;
175 :
176 : virtual NumericVector<T> & operator= (const NumericVector<T> & v) override;
177 :
178 : virtual NumericVector<T> & operator= (const std::vector<T> & v) override;
179 :
180 : virtual Real min () const override;
181 :
182 : virtual Real max () const override;
183 :
184 : virtual T sum () const override;
185 :
186 : virtual Real l1_norm () const override;
187 :
188 : virtual Real l2_norm () const override;
189 :
190 : virtual Real linfty_norm () const override;
191 :
192 : virtual numeric_index_type size () const override;
193 :
194 : virtual numeric_index_type local_size() const override;
195 :
196 : virtual numeric_index_type first_local_index() const override;
197 :
198 : virtual numeric_index_type last_local_index() const override;
199 :
200 : /**
201 : * \returns The local index corresponding to global index \p i.
202 : *
203 : * If the index is not a ghost cell, this is done by subtraction the
204 : * number of the first local index. If it is a ghost cell, it has
205 : * to be looked up in the map.
206 : */
207 : numeric_index_type map_global_to_local_index(const numeric_index_type i) const;
208 :
209 : virtual T operator() (const numeric_index_type i) const override;
210 :
211 : virtual void get(const std::vector<numeric_index_type> & index,
212 : T * values) const override;
213 :
214 : /**
215 : * Get read/write access to the raw PETSc Vector data array.
216 : *
217 : * \note This is an advanced interface. In general you should avoid
218 : * using it unless you know what you are doing!
219 : */
220 : PetscScalar * get_array();
221 :
222 : /**
223 : * Get read only access to the raw PETSc Vector data array.
224 : *
225 : * \note This is an advanced interface. In general you should avoid
226 : * using it unless you know what you are doing!
227 : */
228 : const PetscScalar * get_array_read() const;
229 :
230 : /**
231 : * Restore the data array.
232 : *
233 : * \note This MUST be called after get_array() or get_array_read()
234 : * and before using any other interface functions on PetscVector.
235 : */
236 : void restore_array();
237 :
238 : virtual NumericVector<T> & operator += (const NumericVector<T> & v) override;
239 :
240 : virtual NumericVector<T> & operator -= (const NumericVector<T> & v) override;
241 :
242 : virtual void reciprocal() override;
243 :
244 : virtual void conjugate() override;
245 :
246 : virtual void set (const numeric_index_type i,
247 : const T value) override;
248 :
249 : virtual void add (const numeric_index_type i,
250 : const T value) override;
251 :
252 : virtual void add (const T s) override;
253 :
254 : virtual void add (const NumericVector<T> & v) override;
255 :
256 : virtual void add (const T a, const NumericVector<T> & v) override;
257 :
258 : /**
259 : * We override two NumericVector<T>::add_vector() methods but don't
260 : * want to hide the other defaults.
261 : */
262 : using NumericVector<T>::add_vector;
263 :
264 : virtual void add_vector (const T * v,
265 : const std::vector<numeric_index_type> & dof_indices) override;
266 :
267 : virtual void add_vector (const NumericVector<T> & v,
268 : const SparseMatrix<T> & A) override;
269 :
270 : virtual void add_vector_transpose (const NumericVector<T> & v,
271 : const SparseMatrix<T> & A) override;
272 :
273 : /**
274 : * \f$ U \leftarrow U + A^H v \f$.
275 : *
276 : * Adds the product of the conjugate-transpose of \p SparseMatrix \p
277 : * A and a \p NumericVector \p v to \p this.
278 : */
279 : void add_vector_conjugate_transpose (const NumericVector<T> & v,
280 : const SparseMatrix<T> & A);
281 :
282 : /**
283 : * We override one NumericVector<T>::insert() method but don't want
284 : * to hide the other defaults
285 : */
286 : using NumericVector<T>::insert;
287 :
288 : virtual void insert (const T * v,
289 : const std::vector<numeric_index_type> & dof_indices) override;
290 :
291 : virtual void scale (const T factor) override;
292 :
293 : virtual NumericVector<T> & operator *= (const NumericVector<T> & v) override;
294 :
295 : virtual NumericVector<T> & operator /= (const NumericVector<T> & v) override;
296 :
297 : virtual void abs() override;
298 :
299 : virtual T dot(const NumericVector<T> & v) const override;
300 :
301 : /**
302 : * \returns The dot product of (*this) with the vector \p v.
303 : *
304 : * \note Does *not* use the complex-conjugate of v in the complex-valued case.
305 : */
306 : T indefinite_dot(const NumericVector<T> & v) const;
307 :
308 : virtual void localize (std::vector<T> & v_local) const override;
309 :
310 : virtual void localize (NumericVector<T> & v_local) const override;
311 :
312 : virtual void localize (NumericVector<T> & v_local,
313 : const std::vector<numeric_index_type> & send_list) const override;
314 :
315 : virtual void localize (std::vector<T> & v_local,
316 : const std::vector<numeric_index_type> & indices) const override;
317 :
318 : virtual void localize (const numeric_index_type first_local_idx,
319 : const numeric_index_type last_local_idx,
320 : const std::vector<numeric_index_type> & send_list) override;
321 :
322 : virtual void localize_to_one (std::vector<T> & v_local,
323 : const processor_id_type proc_id=0) const override;
324 :
325 : virtual void pointwise_mult (const NumericVector<T> & vec1,
326 : const NumericVector<T> & vec2) override;
327 :
328 : virtual void pointwise_divide (const NumericVector<T> & vec1,
329 : const NumericVector<T> & vec2) override;
330 :
331 : virtual void print_matlab(const std::string & name = "") const override;
332 :
333 : virtual void create_subvector(NumericVector<T> & subvector,
334 : const std::vector<numeric_index_type> & rows,
335 : bool supplying_global_rows = true) const override;
336 :
337 : virtual void swap (NumericVector<T> & v) override;
338 :
339 : virtual std::size_t max_allowed_id() const override;
340 :
341 : /**
342 : * \returns The raw PETSc Vec pointer.
343 : *
344 : * \note This is generally not required in user-level code.
345 : *
346 : * \note Don't do anything crazy like calling VecDestroy() on
347 : * it, or very bad things will likely happen!
348 : */
349 572389 : Vec vec () { libmesh_assert (_vec); return _vec; }
350 :
351 4369965 : Vec vec () const { libmesh_assert (_vec); return _vec; }
352 :
353 : virtual std::unique_ptr<NumericVector<T>>
354 : get_subvector(const std::vector<numeric_index_type> & rows) override;
355 :
356 : virtual void restore_subvector(std::unique_ptr<NumericVector<T>> subvector,
357 : const std::vector<numeric_index_type> & rows) override;
358 :
359 : private:
360 :
361 : /**
362 : * Actual PETSc vector datatype to hold vector entries.
363 : */
364 : Vec _vec;
365 :
366 : /**
367 : * If \p true, the actual PETSc array of the values of the vector is
368 : * currently accessible. That means that the members \p _local_form
369 : * and \p _values are valid.
370 : */
371 : #ifdef LIBMESH_HAVE_CXX11_THREAD
372 : // We can't use std::atomic_flag here because we need load and store operations.
373 : mutable std::atomic<bool> _array_is_present;
374 : #else
375 : mutable bool _array_is_present;
376 : #endif
377 :
378 : /**
379 : * First local index.
380 : *
381 : * Only valid when _array_is_present
382 : */
383 : mutable numeric_index_type _first;
384 :
385 : /**
386 : * Last local index.
387 : *
388 : * Only valid when _array_is_present
389 : */
390 : mutable numeric_index_type _last;
391 :
392 : /**
393 : * Size of the local values from _get_array()
394 : */
395 : mutable numeric_index_type _local_size;
396 :
397 : /**
398 : * PETSc vector datatype to hold the local form of a ghosted vector.
399 : * The contents of this field are only valid if the vector is
400 : * ghosted and \p _array_is_present is \p true.
401 : */
402 : mutable Vec _local_form;
403 :
404 : /**
405 : * Pointer to the actual PETSc array of the values of the vector.
406 : * This pointer is only valid if \p _array_is_present is \p true.
407 : * We're using PETSc's VecGetArrayRead() function, which requires a
408 : * constant PetscScalar *, but _get_array and _restore_array are
409 : * const member functions, so _values also needs to be mutable
410 : * (otherwise it is a "const PetscScalar * const" in that context).
411 : */
412 : mutable const PetscScalar * _read_only_values;
413 :
414 : /**
415 : * Pointer to the actual PETSc array of the values of the vector.
416 : * This pointer is only valid if \p _array_is_present is \p true.
417 : * We're using PETSc's VecGetArrayRead() function, which requires a
418 : * constant PetscScalar *, but _get_array and _restore_array are
419 : * const member functions, so _values also needs to be mutable
420 : * (otherwise it is a "const PetscScalar * const" in that context).
421 : */
422 : mutable PetscScalar * _values;
423 :
424 : /**
425 : * Mutex for _get_array and _restore_array. This is part of the
426 : * object to keep down thread contention when reading from multiple
427 : * PetscVectors simultaneously
428 : */
429 : mutable std::mutex _petsc_get_restore_array_mutex;
430 :
431 : /**
432 : * Queries the array (and the local form if the vector is ghosted)
433 : * from PETSc.
434 : *
435 : * \param read_only Whether or not a read only copy of the raw data
436 : */
437 : void _get_array(bool read_only) const;
438 :
439 : /**
440 : * Restores the array (and the local form if the vector is ghosted)
441 : * to PETSc.
442 : */
443 : void _restore_array() const;
444 :
445 : /**
446 : * Type for map that maps global to local ghost cells.
447 : */
448 : typedef std::unordered_map<numeric_index_type,numeric_index_type> GlobalToLocalMap;
449 :
450 : /**
451 : * Map that maps global to local ghost cells (will be empty if not
452 : * in ghost cell mode).
453 : */
454 : GlobalToLocalMap _global_to_local_map;
455 :
456 : /**
457 : * This boolean value should only be set to false
458 : * for the constructor which takes a PETSc Vec object.
459 : */
460 : bool _destroy_vec_on_exit;
461 :
462 : /**
463 : * Whether or not the data array has been manually retrieved using
464 : * get_array() or get_array_read()
465 : */
466 : mutable bool _values_manually_retrieved;
467 :
468 : /**
469 : * Whether or not the data array is for read only access
470 : */
471 : mutable bool _values_read_only;
472 :
473 : /**
474 : * \returns A norm of the vector, where the type of norm to compute is
475 : * determined by the template parameter N of the PETSc-defined type NormType.
476 : * The valid template arguments are NORM_1, NORM_2 and NORM_INFINITY, as used
477 : * to define l1_norm(), l2_norm() and linfty_norm().
478 : */
479 : template <NormType N> Real norm () const;
480 : };
481 :
482 :
483 : /*----------------------- Inline functions ----------------------------------*/
484 :
485 :
486 :
487 : template <typename T>
488 : inline
489 2554413 : PetscVector<T>::PetscVector (const Parallel::Communicator & comm_in, const ParallelType ptype) :
490 : NumericVector<T>(comm_in, ptype),
491 2424023 : _vec(nullptr),
492 : _array_is_present(false),
493 2424023 : _first(0),
494 2424023 : _last(0),
495 2424023 : _local_size(0),
496 2424023 : _local_form(nullptr),
497 2424023 : _read_only_values(nullptr),
498 2424023 : _values(nullptr),
499 2424023 : _global_to_local_map(),
500 2424023 : _destroy_vec_on_exit(true),
501 2424023 : _values_manually_retrieved(false),
502 2554413 : _values_read_only(false)
503 : {
504 2478679 : this->_type = ptype;
505 2554413 : }
506 :
507 :
508 :
509 : template <typename T>
510 : inline
511 0 : PetscVector<T>::PetscVector (const Parallel::Communicator & comm_in,
512 : const numeric_index_type n,
513 : const ParallelType ptype) :
514 : NumericVector<T>(comm_in, ptype),
515 0 : _vec(nullptr),
516 : _array_is_present(false),
517 0 : _first(0),
518 0 : _last(0),
519 0 : _local_size(0),
520 0 : _local_form(nullptr),
521 0 : _read_only_values(nullptr),
522 0 : _values(nullptr),
523 0 : _global_to_local_map(),
524 0 : _destroy_vec_on_exit(true),
525 0 : _values_manually_retrieved(false),
526 0 : _values_read_only(false)
527 : {
528 0 : this->init(n, n, false, ptype);
529 0 : }
530 :
531 :
532 :
533 : template <typename T>
534 : inline
535 0 : PetscVector<T>::PetscVector (const Parallel::Communicator & comm_in,
536 : const numeric_index_type n,
537 : const numeric_index_type n_local,
538 : const ParallelType ptype) :
539 : NumericVector<T>(comm_in, ptype),
540 0 : _vec(nullptr),
541 : _array_is_present(false),
542 0 : _first(0),
543 0 : _last(0),
544 0 : _local_size(0),
545 0 : _local_form(nullptr),
546 0 : _read_only_values(nullptr),
547 0 : _values(nullptr),
548 0 : _global_to_local_map(),
549 0 : _destroy_vec_on_exit(true),
550 0 : _values_manually_retrieved(false),
551 0 : _values_read_only(false)
552 : {
553 0 : this->init(n, n_local, false, ptype);
554 0 : }
555 :
556 :
557 :
558 : template <typename T>
559 : inline
560 0 : PetscVector<T>::PetscVector (const Parallel::Communicator & comm_in,
561 : const numeric_index_type n,
562 : const numeric_index_type n_local,
563 : const std::vector<numeric_index_type> & ghost,
564 : const ParallelType ptype) :
565 : NumericVector<T>(comm_in, ptype),
566 0 : _vec(nullptr),
567 : _array_is_present(false),
568 0 : _first(0),
569 0 : _last(0),
570 0 : _local_size(0),
571 0 : _local_form(nullptr),
572 0 : _read_only_values(nullptr),
573 0 : _values(nullptr),
574 0 : _global_to_local_map(),
575 0 : _destroy_vec_on_exit(true),
576 0 : _values_manually_retrieved(false),
577 0 : _values_read_only(false)
578 : {
579 0 : this->init(n, n_local, ghost, false, ptype);
580 0 : }
581 :
582 :
583 :
584 :
585 :
586 : template <typename T>
587 : inline
588 1010859 : PetscVector<T>::PetscVector (Vec v,
589 : const Parallel::Communicator & comm_in) :
590 : NumericVector<T>(comm_in, AUTOMATIC),
591 968675 : _vec(v),
592 : _array_is_present(false),
593 968675 : _first(0),
594 968675 : _last(0),
595 968675 : _local_size(0),
596 968675 : _local_form(nullptr),
597 968675 : _read_only_values(nullptr),
598 968675 : _values(nullptr),
599 968675 : _global_to_local_map(),
600 968675 : _destroy_vec_on_exit(false),
601 968675 : _values_manually_retrieved(false),
602 1010859 : _values_read_only(false)
603 : {
604 1010859 : this->_is_closed = true;
605 1010859 : this->_is_initialized = true;
606 :
607 : /* We need to ask PETSc about the (local to global) ghost value
608 : mapping and create the inverse mapping out of it. */
609 1010859 : PetscInt petsc_local_size=0;
610 1010859 : LibmeshPetscCall(VecGetLocalSize(_vec, &petsc_local_size));
611 :
612 : // Get the vector type from PETSc.
613 : VecType ptype;
614 1010859 : LibmeshPetscCall(VecGetType(_vec, &ptype));
615 :
616 : // PETSc supports some exotic types nowadays. Just use Vec sizes to
617 : // determine whether we're SERIAL vs PARALLEL-or-GHOSTED.
618 1010859 : PetscInt petsc_global_size = 0;
619 1010859 : LibmeshPetscCall(VecGetSize(_vec, &petsc_global_size));
620 :
621 : // If we have a parallel Vec with all its DoFs on one processor, we
622 : // might be unable to tell on that processor that it's not a serial
623 : // vector unless we communicate.
624 1010859 : bool is_serial = (petsc_local_size == petsc_global_size);
625 1010859 : comm_in.min(is_serial);
626 :
627 : #ifdef DEBUG
628 21704 : dof_id_type sum_of_local_sizes = petsc_local_size;
629 21704 : comm_in.sum(sum_of_local_sizes);
630 21704 : libmesh_assert_equal_to(sum_of_local_sizes,
631 : cast_int<dof_id_type>(petsc_global_size));
632 : #endif
633 :
634 : #if PETSC_RELEASE_GREATER_EQUALS(3, 21, 0)
635 : // Fande only implemented VecGhostGetGhostIS for VECMPI
636 : if (std::strcmp(ptype, VECMPI) == 0)
637 : {
638 : IS ghostis;
639 : LibmeshPetscCall(VecGhostGetGhostIS(_vec, &ghostis));
640 :
641 : Vec localrep;
642 : LibmeshPetscCall(VecGhostGetLocalForm(_vec, &localrep));
643 :
644 : // If is a sparsely stored vector, set up our new mapping
645 : // Only checking mapping is not enough to determine if a vec is ghosted
646 : // We need to check if vec has a local representation
647 : if (ghostis && localrep)
648 : {
649 : PetscInt ghost_size;
650 : LibmeshPetscCall(ISGetSize(ghostis, &ghost_size));
651 :
652 : const PetscInt * indices;
653 : LibmeshPetscCall(ISGetIndices(ghostis, &indices));
654 :
655 : for (const auto i : make_range(ghost_size))
656 : _global_to_local_map[indices[i]] = i;
657 : this->_type = GHOSTED;
658 : LibmeshPetscCall(ISRestoreIndices(ghostis, &indices));
659 : }
660 : else
661 : this->_type = PARALLEL;
662 : }
663 : else if (!is_serial)
664 : #else // PETSc < 3.21.0
665 1010859 : if (!is_serial)
666 : #endif
667 : {
668 996009 : ISLocalToGlobalMapping mapping = nullptr;
669 996009 : Vec localrep = nullptr;
670 :
671 : // Non-VECMPI types won't even let us call VecGetLocalToGlobalMapping;
672 : // we'll just assume for now that no such type is ghosted
673 996009 : if (std::strcmp(ptype, VECMPI) == 0)
674 : {
675 996009 : LibmeshPetscCall(VecGetLocalToGlobalMapping(_vec, &mapping));
676 996009 : LibmeshPetscCall(VecGhostGetLocalForm(_vec,&localrep));
677 : }
678 :
679 : // If is a sparsely stored vector, set up our new mapping
680 : // Only checking mapping is not enough to determine if a vec is ghosted
681 : // We need to check if vec has a local representation
682 996009 : if (mapping && localrep)
683 : {
684 0 : const numeric_index_type my_local_size = static_cast<numeric_index_type>(petsc_local_size);
685 0 : const numeric_index_type ghost_begin = static_cast<numeric_index_type>(petsc_local_size);
686 : PetscInt n;
687 0 : LibmeshPetscCall(ISLocalToGlobalMappingGetSize(mapping, &n));
688 :
689 0 : const numeric_index_type ghost_end = static_cast<numeric_index_type>(n);
690 : const PetscInt * indices;
691 0 : LibmeshPetscCall(ISLocalToGlobalMappingGetIndices(mapping,&indices));
692 :
693 0 : for (numeric_index_type i=ghost_begin; i<ghost_end; i++)
694 0 : _global_to_local_map[indices[i]] = i-my_local_size;
695 0 : this->_type = GHOSTED;
696 0 : LibmeshPetscCall(ISLocalToGlobalMappingRestoreIndices(mapping, &indices));
697 0 : }
698 : else
699 996009 : this->_type = PARALLEL;
700 :
701 996009 : LibmeshPetscCall(VecGhostRestoreLocalForm(_vec,&localrep));
702 : }
703 : else
704 14850 : this->_type = SERIAL;
705 1010859 : }
706 :
707 :
708 :
709 :
710 : template <typename T>
711 : inline
712 4658021 : PetscVector<T>::~PetscVector ()
713 : {
714 2837423 : this->clear ();
715 7419102 : }
716 :
717 :
718 :
719 : template <typename T>
720 : inline
721 946643 : void PetscVector<T>::init (const numeric_index_type n,
722 : const numeric_index_type n_local,
723 : const bool fast,
724 : const ParallelType ptype)
725 : {
726 27882 : parallel_object_only();
727 :
728 946643 : PetscInt petsc_n=static_cast<PetscInt>(n);
729 :
730 : // Clear initialized vectors
731 946643 : if (this->initialized())
732 31058 : this->clear();
733 :
734 946643 : if (ptype == AUTOMATIC)
735 : {
736 4830 : if (n == n_local)
737 621 : this->_type = SERIAL;
738 : else
739 4209 : this->_type = PARALLEL;
740 : }
741 : else
742 941813 : this->_type = ptype;
743 :
744 : // We should have been given consistent settings
745 27882 : libmesh_assert ((this->_type==SERIAL && n==n_local) ||
746 : (this->n_processors()==1 && n==n_local) ||
747 : this->_type==PARALLEL);
748 :
749 : // create a sequential vector if on only 1 processor, or if asked
750 946643 : if (this->_type == SERIAL || (this->n_processors() == 1))
751 : {
752 23206 : LibmeshPetscCallA(PETSC_COMM_SELF, VecCreate(PETSC_COMM_SELF, &_vec));
753 23206 : LibmeshPetscCallA(PETSC_COMM_SELF, VecSetSizes(_vec, petsc_n, petsc_n));
754 23206 : LibmeshPetscCallA(PETSC_COMM_SELF, VecSetOptionsPrefix(_vec, this->options_prefix().c_str()));
755 23206 : LibmeshPetscCallA(PETSC_COMM_SELF, VecSetFromOptions (_vec));
756 : }
757 : // or create an MPI-enabled PARALLEL vector w/o ghosting if asked
758 923437 : else if (this->_type == PARALLEL)
759 : {
760 : #ifdef LIBMESH_HAVE_MPI
761 27834 : PetscInt petsc_n_local=cast_int<PetscInt>(n_local);
762 27834 : libmesh_assert_less_equal (n_local, n);
763 : // Use more generic function instead of VecCreateSeq/MPI
764 923437 : LibmeshPetscCall(VecCreate(this->comm().get(), &_vec));
765 923437 : LibmeshPetscCall(VecSetSizes(_vec, petsc_n_local, petsc_n));
766 : #else
767 : libmesh_assert_equal_to (n_local, n);
768 : LibmeshPetscCallA(PETSC_COMM_SELF, VecCreate(PETSC_COMM_SELF, &_vec));
769 : LibmeshPetscCallA(PETSC_COMM_SELF, VecSetSizes(_vec, petsc_n, petsc_n));
770 : #endif
771 923437 : LibmeshPetscCall(VecSetOptionsPrefix(_vec, this->options_prefix().c_str()));
772 923437 : LibmeshPetscCall(VecSetFromOptions(_vec));
773 : }
774 : // or yell because we don't know what to do
775 : else
776 0 : libmesh_error_msg("Unsupported type " <<
777 : Utility::enum_to_string(this->_type) <<
778 : " for parallel init with no ghost indices supplied");
779 :
780 946643 : this->_is_initialized = true;
781 946643 : this->_is_closed = true;
782 :
783 :
784 946643 : if (fast == false)
785 925720 : this->zero ();
786 946643 : }
787 :
788 :
789 :
790 : template <typename T>
791 : inline
792 1523 : void PetscVector<T>::init (const numeric_index_type n,
793 : const bool fast,
794 : const ParallelType ptype)
795 : {
796 2067 : this->init(n,n,fast,ptype);
797 2051 : }
798 :
799 :
800 :
801 : template <typename T>
802 : inline
803 556436 : void PetscVector<T>::init (const numeric_index_type n,
804 : const numeric_index_type n_local,
805 : const std::vector<numeric_index_type> & ghost,
806 : const bool fast,
807 : const ParallelType ptype)
808 : {
809 17580 : parallel_object_only();
810 :
811 556436 : if (this->comm().size() == 1)
812 : {
813 0 : libmesh_assert(ghost.empty());
814 7925 : this->init(n, n_local, fast, ptype);
815 7925 : return;
816 : }
817 :
818 548511 : PetscInt petsc_n=static_cast<PetscInt>(n);
819 548511 : PetscInt petsc_n_local=static_cast<PetscInt>(n_local);
820 34972 : PetscInt petsc_n_ghost=static_cast<PetscInt>(ghost.size());
821 :
822 : // If the mesh is not disjoint, every processor will either have
823 : // all the dofs, none of the dofs, or some non-zero dofs at the
824 : // boundary between processors.
825 : //
826 : // However we can't assert this, because someone might want to
827 : // construct a GHOSTED vector which doesn't include neighbor element
828 : // dofs. Boyce tried to do so in user code, and we're going to want
829 : // to do so in System::project_vector().
830 : //
831 : // libmesh_assert(n_local == 0 || n_local == n || !ghost.empty());
832 :
833 : libmesh_assert(sizeof(PetscInt) == sizeof(numeric_index_type));
834 :
835 548511 : PetscInt * petsc_ghost = ghost.empty() ? LIBMESH_PETSC_NULLPTR :
836 16864 : const_cast<PetscInt *>(reinterpret_cast<const PetscInt *>(ghost.data()));
837 :
838 : // Clear initialized vectors
839 548511 : if (this->initialized())
840 27690 : this->clear();
841 :
842 17580 : libmesh_assert(ptype == AUTOMATIC || ptype == GHOSTED);
843 548511 : this->_type = GHOSTED;
844 :
845 : /* Make the global-to-local ghost cell map. */
846 42167719 : for (auto i : index_range(ghost))
847 : {
848 41619208 : _global_to_local_map[ghost[i]] = i;
849 : }
850 :
851 : /* Create vector. */
852 548511 : LibmeshPetscCall(VecCreateGhost (this->comm().get(), petsc_n_local, petsc_n,
853 : petsc_n_ghost, petsc_ghost,
854 : &_vec));
855 :
856 : // Add a prefix so that we can at least distinguish a ghosted vector from a
857 : // nonghosted vector when using a petsc option.
858 : // PETSc does not fully support VecGhost on GPU yet. This change allows us to
859 : // trigger a nonghosted vector to use GPU without bothering the ghosted vectors.
860 548511 : LibmeshPetscCall(VecSetOptionsPrefix(_vec, this->options_prefix().c_str()));
861 548511 : LibmeshPetscCall(PetscObjectAppendOptionsPrefix((PetscObject)_vec,"ghost_"));
862 :
863 548511 : LibmeshPetscCall(VecSetFromOptions (_vec));
864 :
865 548511 : this->_is_initialized = true;
866 548511 : this->_is_closed = true;
867 :
868 548511 : if (fast == false)
869 191385 : this->zero ();
870 : }
871 :
872 :
873 :
874 : template <typename T>
875 : inline
876 442644 : void PetscVector<T>::init (const NumericVector<T> & other,
877 : const bool fast)
878 : {
879 13036 : parallel_object_only();
880 :
881 : // Clear initialized vectors
882 442644 : if (this->initialized())
883 32690 : this->clear();
884 :
885 13036 : const PetscVector<T> & v = cast_ref<const PetscVector<T> &>(other);
886 :
887 : // Other vector should restore array.
888 442644 : if (v.initialized())
889 : {
890 442644 : v._restore_array();
891 : }
892 :
893 13036 : this->_global_to_local_map = v._global_to_local_map;
894 :
895 : // Even if we're initializing sizes based on an uninitialized or
896 : // unclosed vector, *this* vector is being initialized now and is
897 : // initially closed.
898 442644 : this->_is_closed = true; // v._is_closed;
899 442644 : this->_is_initialized = true; // v._is_initialized;
900 :
901 442644 : this->_type = v._type;
902 :
903 : // We want to have a valid Vec, even if it's initially of size zero
904 442644 : LibmeshPetscCall(VecDuplicate (v._vec, &this->_vec));
905 : // VecDuplicate() keeps the type but not the options prefix
906 : const char * prefix;
907 442644 : LibmeshPetscCall(PetscObjectGetOptionsPrefix((PetscObject)v._vec, &prefix));
908 442644 : LibmeshPetscCall(PetscObjectSetOptionsPrefix((PetscObject)this->_vec, prefix));
909 442644 : this->_options_prefix = v._options_prefix;
910 :
911 442644 : if (fast == false)
912 53906 : this->zero ();
913 442644 : }
914 :
915 :
916 :
917 : template <typename T>
918 : inline
919 3554070 : void PetscVector<T>::close ()
920 : {
921 96608 : parallel_object_only();
922 :
923 3554070 : this->_restore_array();
924 :
925 3554070 : VecAssemblyBeginEnd(this->comm(), _vec);
926 :
927 96608 : if (this->is_effectively_ghosted())
928 802654 : VecGhostUpdateBeginEnd(this->comm(), _vec, INSERT_VALUES, SCATTER_FORWARD);
929 :
930 3554070 : this->_is_closed = true;
931 3554070 : }
932 :
933 :
934 :
935 : template <typename T>
936 : inline
937 3109001 : void PetscVector<T>::clear () noexcept
938 : {
939 84768 : exceptionless_parallel_object_only();
940 :
941 3109001 : if (this->initialized())
942 2948099 : this->_restore_array();
943 :
944 3109001 : if ((this->initialized()) && (this->_destroy_vec_on_exit))
945 : {
946 : // If we encounter an error here, print a warning but otherwise
947 : // keep going since we may be recovering from an exception.
948 1937866 : PetscErrorCode ierr = VecDestroy(&_vec);
949 : if (ierr)
950 : libmesh_warning("Warning: VecDestroy returned a non-zero error code which we ignored.");
951 : }
952 :
953 3109001 : this->_is_closed = this->_is_initialized = false;
954 :
955 84768 : _global_to_local_map.clear();
956 3109001 : }
957 :
958 :
959 :
960 : template <typename T>
961 : inline
962 6057391 : void PetscVector<T>::zero ()
963 : {
964 172924 : parallel_object_only();
965 :
966 172924 : libmesh_assert(this->closed());
967 :
968 6057391 : this->_restore_array();
969 :
970 172924 : PetscScalar z=0.;
971 :
972 172924 : if (!this->is_effectively_ghosted())
973 5812600 : LibmeshPetscCall(VecSet (_vec, z));
974 : else
975 : {
976 : /* Vectors that include ghost values require a special
977 : handling. */
978 : Vec loc_vec;
979 244791 : LibmeshPetscCall(VecGhostGetLocalForm (_vec,&loc_vec));
980 244791 : LibmeshPetscCall(VecSet (loc_vec, z));
981 244791 : LibmeshPetscCall(VecGhostRestoreLocalForm (_vec, &loc_vec));
982 : }
983 6057391 : }
984 :
985 :
986 :
987 : template <typename T>
988 : inline
989 21216 : std::unique_ptr<NumericVector<T>> PetscVector<T>::zero_clone () const
990 : {
991 21216 : std::unique_ptr<NumericVector<T>> cloned_vector =
992 20590 : std::make_unique<PetscVector<T>>(this->comm(), this->type());
993 21216 : cloned_vector->init(*this);
994 21216 : return cloned_vector;
995 0 : }
996 :
997 :
998 :
999 : template <typename T>
1000 : inline
1001 355628 : std::unique_ptr<NumericVector<T>> PetscVector<T>::clone () const
1002 : {
1003 355628 : std::unique_ptr<NumericVector<T>> cloned_vector =
1004 345098 : std::make_unique<PetscVector<T>>(this->comm(), this->type());
1005 355628 : cloned_vector->init(*this, true);
1006 355628 : *cloned_vector = *this;
1007 355628 : return cloned_vector;
1008 0 : }
1009 :
1010 :
1011 :
1012 : template <typename T>
1013 : inline
1014 190016329 : numeric_index_type PetscVector<T>::size () const
1015 : {
1016 189406905 : libmesh_assert (this->initialized());
1017 :
1018 190016329 : PetscInt petsc_size=0;
1019 :
1020 190016329 : if (!this->initialized())
1021 0 : return 0;
1022 :
1023 190016329 : LibmeshPetscCall(VecGetSize(_vec, &petsc_size));
1024 :
1025 190016329 : return static_cast<numeric_index_type>(petsc_size);
1026 : }
1027 :
1028 :
1029 :
1030 : template <typename T>
1031 : inline
1032 8622270 : numeric_index_type PetscVector<T>::local_size () const
1033 : {
1034 213444 : libmesh_assert (this->initialized());
1035 :
1036 8622270 : PetscInt petsc_size=0;
1037 :
1038 8622270 : LibmeshPetscCall(VecGetLocalSize(_vec, &petsc_size));
1039 :
1040 8622270 : return static_cast<numeric_index_type>(petsc_size);
1041 : }
1042 :
1043 :
1044 :
1045 : template <typename T>
1046 : inline
1047 7074011 : numeric_index_type PetscVector<T>::first_local_index () const
1048 : {
1049 3321258 : libmesh_assert (this->initialized());
1050 :
1051 3321258 : numeric_index_type first = 0;
1052 :
1053 7074011 : if (_array_is_present) // Can we use cached values?
1054 1572489 : first = _first;
1055 : else
1056 : {
1057 5501522 : PetscInt petsc_first=0, petsc_last=0;
1058 5501522 : LibmeshPetscCall(VecGetOwnershipRange (_vec, &petsc_first, &petsc_last));
1059 5501522 : first = static_cast<numeric_index_type>(petsc_first);
1060 : }
1061 :
1062 7074011 : return first;
1063 : }
1064 :
1065 :
1066 :
1067 : template <typename T>
1068 : inline
1069 6933677 : numeric_index_type PetscVector<T>::last_local_index () const
1070 : {
1071 3310606 : libmesh_assert (this->initialized());
1072 :
1073 3310606 : numeric_index_type last = 0;
1074 :
1075 6933677 : if (_array_is_present) // Can we use cached values?
1076 1454202 : last = _last;
1077 : else
1078 : {
1079 5479475 : PetscInt petsc_first=0, petsc_last=0;
1080 5479475 : LibmeshPetscCall(VecGetOwnershipRange (_vec, &petsc_first, &petsc_last));
1081 5479475 : last = static_cast<numeric_index_type>(petsc_last);
1082 : }
1083 :
1084 6933677 : return last;
1085 : }
1086 :
1087 :
1088 :
1089 : template <typename T>
1090 : inline
1091 7225699229 : numeric_index_type PetscVector<T>::map_global_to_local_index (const numeric_index_type i) const
1092 : {
1093 571273862 : libmesh_assert (this->initialized());
1094 :
1095 571273862 : numeric_index_type first=0;
1096 571273862 : numeric_index_type last=0;
1097 :
1098 7225699229 : if (_array_is_present) // Can we use cached values?
1099 : {
1100 7225699229 : first = _first;
1101 7225699229 : last = _last;
1102 : }
1103 : else
1104 : {
1105 0 : PetscInt petsc_first=0, petsc_last=0;
1106 0 : LibmeshPetscCall(VecGetOwnershipRange (_vec, &petsc_first, &petsc_last));
1107 0 : first = static_cast<numeric_index_type>(petsc_first);
1108 0 : last = static_cast<numeric_index_type>(petsc_last);
1109 : }
1110 :
1111 :
1112 7225699229 : if ((i>=first) && (i<last))
1113 : {
1114 7036883015 : return i-first;
1115 : }
1116 :
1117 5838326 : GlobalToLocalMap::const_iterator it = _global_to_local_map.find(i);
1118 : #ifndef NDEBUG
1119 5838326 : const GlobalToLocalMap::const_iterator end = _global_to_local_map.end();
1120 5838326 : if (it == end)
1121 : {
1122 0 : std::ostringstream error_message;
1123 0 : error_message << "No index " << i << " in ghosted vector.\n"
1124 0 : << "Vector contains [" << first << ',' << last << ")\n";
1125 0 : GlobalToLocalMap::const_iterator b = _global_to_local_map.begin();
1126 0 : if (b == end)
1127 : {
1128 0 : error_message << "And empty ghost array.\n";
1129 : }
1130 : else
1131 : {
1132 0 : error_message << "And ghost array {" << b->first;
1133 0 : for (++b; b != end; ++b)
1134 0 : error_message << ',' << b->first;
1135 0 : error_message << "}\n";
1136 : }
1137 :
1138 0 : libmesh_error_msg(error_message.str());
1139 : }
1140 5838326 : libmesh_assert (it != _global_to_local_map.end());
1141 : #endif
1142 188816214 : return it->second+last-first;
1143 : }
1144 :
1145 :
1146 :
1147 : template <typename T>
1148 : inline
1149 6746267800 : T PetscVector<T>::operator() (const numeric_index_type i) const
1150 : {
1151 6746267800 : this->_get_array(true);
1152 :
1153 6746267800 : const numeric_index_type local_index = this->map_global_to_local_index(i);
1154 :
1155 : #ifndef NDEBUG
1156 527303446 : if (this->is_effectively_ghosted())
1157 523571630 : libmesh_assert_less (local_index, _local_size);
1158 : #endif
1159 :
1160 6746267800 : return static_cast<T>(_read_only_values[local_index]);
1161 : }
1162 :
1163 :
1164 :
1165 : template <typename T>
1166 : inline
1167 92096945 : void PetscVector<T>::get(const std::vector<numeric_index_type> & index,
1168 : T * values) const
1169 : {
1170 92096945 : this->_get_array(true);
1171 :
1172 17098631 : const std::size_t num = index.size();
1173 :
1174 571528374 : for (std::size_t i=0; i<num; i++)
1175 : {
1176 523402660 : const numeric_index_type local_index = this->map_global_to_local_index(index[i]);
1177 : #ifndef NDEBUG
1178 43970416 : if (this->is_effectively_ghosted())
1179 43800740 : libmesh_assert_less (local_index, _local_size);
1180 : #endif
1181 479431429 : values[i] = static_cast<T>(_read_only_values[local_index]);
1182 : }
1183 92096945 : }
1184 :
1185 :
1186 : template <typename T>
1187 : inline
1188 0 : PetscScalar * PetscVector<T>::get_array()
1189 : {
1190 0 : _get_array(false);
1191 0 : _values_manually_retrieved = true;
1192 :
1193 0 : return _values;
1194 : }
1195 :
1196 :
1197 : template <typename T>
1198 : inline
1199 0 : const PetscScalar * PetscVector<T>::get_array_read() const
1200 : {
1201 0 : _get_array(true);
1202 0 : _values_manually_retrieved = true;
1203 :
1204 0 : return _read_only_values;
1205 : }
1206 :
1207 : template <typename T>
1208 : inline
1209 0 : void PetscVector<T>::restore_array()
1210 : {
1211 : // \note \p _values_manually_retrieved needs to be set to \p false
1212 : // \e before calling \p _restore_array()!
1213 0 : _values_manually_retrieved = false;
1214 0 : _restore_array();
1215 0 : }
1216 :
1217 : template <typename T>
1218 : inline
1219 0 : Real PetscVector<T>::min () const
1220 : {
1221 0 : parallel_object_only();
1222 :
1223 0 : this->_restore_array();
1224 :
1225 0 : PetscInt index=0;
1226 0 : PetscReal returnval=0.;
1227 :
1228 0 : LibmeshPetscCall(VecMin (_vec, &index, &returnval));
1229 :
1230 : // this return value is correct: VecMin returns a PetscReal
1231 0 : return static_cast<Real>(returnval);
1232 : }
1233 :
1234 :
1235 :
1236 : template <typename T>
1237 : inline
1238 0 : Real PetscVector<T>::max() const
1239 : {
1240 0 : parallel_object_only();
1241 :
1242 0 : this->_restore_array();
1243 :
1244 0 : PetscInt index=0;
1245 0 : PetscReal returnval=0.;
1246 :
1247 0 : LibmeshPetscCall(VecMax (_vec, &index, &returnval));
1248 :
1249 : // this return value is correct: VecMax returns a PetscReal
1250 0 : return static_cast<Real>(returnval);
1251 : }
1252 :
1253 :
1254 :
1255 : template <typename T>
1256 : inline
1257 1327868 : void PetscVector<T>::swap (NumericVector<T> & other)
1258 : {
1259 28712 : parallel_object_only();
1260 :
1261 1300776 : NumericVector<T>::swap(other);
1262 :
1263 28712 : PetscVector<T> & v = cast_ref<PetscVector<T> &>(other);
1264 :
1265 28712 : std::swap(_vec, v._vec);
1266 28712 : std::swap(_destroy_vec_on_exit, v._destroy_vec_on_exit);
1267 28712 : std::swap(_global_to_local_map, v._global_to_local_map);
1268 :
1269 : #ifdef LIBMESH_HAVE_CXX11_THREAD
1270 : // Only truly atomic for v... but swap() doesn't really need to be thread safe!
1271 1354960 : _array_is_present = v._array_is_present.exchange(_array_is_present);
1272 : #else
1273 : std::swap(_array_is_present, v._array_is_present);
1274 : #endif
1275 :
1276 28712 : std::swap(_local_form, v._local_form);
1277 28712 : std::swap(_values, v._values);
1278 1327868 : }
1279 :
1280 :
1281 :
1282 : template <typename T>
1283 : inline
1284 28915 : std::size_t PetscVector<T>::max_allowed_id () const
1285 : {
1286 : // The PetscInt type is used for indexing, it may be either a signed
1287 : // 4-byte or 8-byte integer depending on how PETSc is configured.
1288 28915 : return std::numeric_limits<PetscInt>::max();
1289 : }
1290 :
1291 :
1292 :
1293 : #ifdef LIBMESH_HAVE_CXX11
1294 : static_assert(sizeof(PetscInt) == sizeof(numeric_index_type),
1295 : "PETSc and libMesh integer sizes must match!");
1296 : #endif
1297 :
1298 :
1299 : inline
1300 7054345 : PetscInt * numeric_petsc_cast(const numeric_index_type * p)
1301 : {
1302 7054345 : return reinterpret_cast<PetscInt *>(const_cast<numeric_index_type *>(p));
1303 : }
1304 :
1305 : } // namespace libMesh
1306 :
1307 :
1308 : #endif // #ifdef LIBMESH_HAVE_PETSC
1309 : #endif // LIBMESH_PETSC_VECTOR_H
|