| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2018 - 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_trilinos_tpetra_vector_h |
| 16 |
#define dealii_trilinos_tpetra_vector_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/types.h> |
| 22 |
|
| 23 |
#include <deal.II/lac/trilinos_tpetra_types.h> |
| 24 |
|
| 25 |
#ifdef DEAL_II_TRILINOS_WITH_TPETRA |
| 26 |
|
| 27 |
# include <deal.II/base/index_set.h> |
| 28 |
# include <deal.II/base/mpi_stub.h> |
| 29 |
|
| 30 |
# include <deal.II/lac/read_vector.h> |
| 31 |
# include <deal.II/lac/trilinos_tpetra_communication_pattern.h> |
| 32 |
# include <deal.II/lac/vector.h> |
| 33 |
# include <deal.II/lac/vector_operation.h> |
| 34 |
# include <deal.II/lac/vector_type_traits.h> |
| 35 |
|
| 36 |
# include <Teuchos_Comm.hpp> |
| 37 |
# include <Teuchos_OrdinalTraits.hpp> |
| 38 |
# include <Tpetra_Core.hpp> |
| 39 |
# include <Tpetra_Vector.hpp> |
| 40 |
# include <Tpetra_Version.hpp> |
| 41 |
|
| 42 |
# include <memory> |
| 43 |
# include <optional> |
| 44 |
|
| 45 |
DEAL_II_NAMESPACE_OPEN |
| 46 |
|
| 47 |
/** |
| 48 |
* Type trait indicating if a certain number type has been explicitly |
| 49 |
* instantiated in Tpetra. deal.II only supports those number types in Tpetra |
| 50 |
* wrapper classes. |
| 51 |
*/ |
| 52 |
template <typename Number> |
| 53 |
struct is_tpetra_type : std::false_type |
| 54 |
{}; |
| 55 |
|
| 56 |
# ifdef HAVE_TPETRA_INST_FLOAT |
| 57 |
template <> |
| 58 |
struct is_tpetra_type<float> : std::true_type |
| 59 |
{}; |
| 60 |
# endif |
| 61 |
|
| 62 |
# ifdef HAVE_TPETRA_INST_DOUBLE |
| 63 |
template <> |
| 64 |
struct is_tpetra_type<double> : std::true_type |
| 65 |
{}; |
| 66 |
# endif |
| 67 |
|
| 68 |
# ifdef DEAL_II_WITH_COMPLEX_VALUES |
| 69 |
# ifdef HAVE_TPETRA_INST_COMPLEX_FLOAT |
| 70 |
template <> |
| 71 |
struct is_tpetra_type<std::complex<float>> : std::true_type |
| 72 |
{}; |
| 73 |
# endif |
| 74 |
|
| 75 |
# ifdef HAVE_TPETRA_INST_COMPLEX_DOUBLE |
| 76 |
template <> |
| 77 |
struct is_tpetra_type<std::complex<double>> : std::true_type |
| 78 |
{}; |
| 79 |
# endif |
| 80 |
# endif |
| 81 |
|
| 82 |
namespace LinearAlgebra |
| 83 |
{ |
| 84 |
// Forward declaration |
| 85 |
# ifndef DOXYGEN |
| 86 |
template <typename Number> |
| 87 |
class ReadWriteVector; |
| 88 |
# endif |
| 89 |
|
| 90 |
/** |
| 91 |
* @addtogroup TpetraWrappers |
| 92 |
* @{ |
| 93 |
*/ |
| 94 |
|
| 95 |
/** |
| 96 |
* A namespace for classes that provide wrappers for Trilinos' Tpetra vectors. |
| 97 |
* |
| 98 |
* This namespace provides wrappers for the Tpetra::Vector class from the |
| 99 |
* Tpetra package (https://trilinos.github.io/tpetra.html) that is part of |
| 100 |
* Trilinos. |
| 101 |
* |
| 102 |
* @ingroup TpetraWrappers |
| 103 |
*/ |
| 104 |
namespace TpetraWrappers |
| 105 |
{ |
| 106 |
|
| 107 |
/** |
| 108 |
* This class defines type aliases that are used in vector classes |
| 109 |
* within the TpetraWrappers namespace. |
| 110 |
*/ |
| 111 |
class VectorTraits |
| 112 |
{ |
| 113 |
public: |
| 114 |
using size_type = types::global_dof_index; |
| 115 |
}; |
| 116 |
|
| 117 |
/** |
| 118 |
* @cond internal |
| 119 |
*/ |
| 120 |
|
| 121 |
/** |
| 122 |
* A namespace for internal implementation details of the TrilinosWrapper |
| 123 |
* members. |
| 124 |
* |
| 125 |
* @ingroup TpetraWrappers |
| 126 |
*/ |
| 127 |
namespace internal |
| 128 |
{ |
| 129 |
/** |
| 130 |
* Declare type for container size. |
| 131 |
*/ |
| 132 |
using size_type = types::global_dof_index; |
| 133 |
|
| 134 |
/** |
| 135 |
* This class implements a wrapper for accessing the Trilinos Tpetra |
| 136 |
* vector in the same way as we access deal.II objects: it is initialized |
| 137 |
* with a vector and an element within it, and has a conversion operator |
| 138 |
* to extract the scalar value of this element. It also has a variety of |
| 139 |
* assignment operator for writing to this one element. |
| 140 |
* |
| 141 |
* @ingroup TpetraWrappers |
| 142 |
*/ |
| 143 |
template <typename Number, |
| 144 |
typename MemorySpace = dealii::MemorySpace::Host> |
| 145 |
class VectorReference |
| 146 |
{ |
| 147 |
private: |
| 148 |
/** |
| 149 |
* Constructor. It is made private so as to only allow the actual vector |
| 150 |
* class to create it. |
| 151 |
*/ |
| 152 |
VectorReference(Vector<Number, MemorySpace> &vector, |
| 153 |
const size_type index); |
| 154 |
|
| 155 |
public: |
| 156 |
/** |
| 157 |
* Copy constructor. |
| 158 |
*/ |
| 159 |
VectorReference(const VectorReference &) = default; |
| 160 |
|
| 161 |
/** |
| 162 |
* This looks like a copy operator, but does something different than |
| 163 |
* usual. In particular, it does not copy the member variables of this |
| 164 |
* reference. Rather, it handles the situation where we have two vectors |
| 165 |
* @p v and @p w, and assign elements like in <tt>v(i)=w(i)</tt>. Here, |
| 166 |
* both left and right hand side of the assignment have data type |
| 167 |
* VectorReference, but what we really mean is to assign the vector |
| 168 |
* elements represented by the two references. This operator implements |
| 169 |
* this operation. Note also that this allows us to make the assignment |
| 170 |
* operator const. |
| 171 |
*/ |
| 172 |
const VectorReference & |
| 173 |
operator=(const VectorReference &r) const; |
| 174 |
|
| 175 |
/** |
| 176 |
* Same as above but for non-const reference objects. |
| 177 |
*/ |
| 178 |
VectorReference & |
| 179 |
operator=(const VectorReference &r); |
| 180 |
|
| 181 |
/** |
| 182 |
* Set the referenced element of the vector to <tt>s</tt>. |
| 183 |
*/ |
| 184 |
const VectorReference & |
| 185 |
operator=(const Number &s) const; |
| 186 |
|
| 187 |
/** |
| 188 |
* Add <tt>s</tt> to the referenced element of the vector-> |
| 189 |
*/ |
| 190 |
const VectorReference & |
| 191 |
operator+=(const Number &s) const; |
| 192 |
|
| 193 |
/** |
| 194 |
* Subtract <tt>s</tt> from the referenced element of the vector-> |
| 195 |
*/ |
| 196 |
const VectorReference & |
| 197 |
operator-=(const Number &s) const; |
| 198 |
|
| 199 |
/** |
| 200 |
* Multiply the referenced element of the vector by <tt>s</tt>. |
| 201 |
*/ |
| 202 |
const VectorReference & |
| 203 |
operator*=(const Number &s) const; |
| 204 |
|
| 205 |
/** |
| 206 |
* Divide the referenced element of the vector by <tt>s</tt>. |
| 207 |
*/ |
| 208 |
const VectorReference & |
| 209 |
operator/=(const Number &s) const; |
| 210 |
|
| 211 |
/** |
| 212 |
* Convert the reference to an actual value, i.e. return the value of |
| 213 |
* the referenced element of the vector. |
| 214 |
*/ |
| 215 |
operator Number() const; |
| 216 |
|
| 217 |
/** |
| 218 |
* Exception |
| 219 |
*/ |
| 220 |
DeclException1(ExcTrilinosError, |
| 221 |
int, |
| 222 |
<< "An error with error number " << arg1 |
| 223 |
<< " occurred while calling a Trilinos function"); |
| 224 |
|
| 225 |
/* |
| 226 |
* Access to a an element that is not (locally-)owned. |
| 227 |
* |
| 228 |
* @ingroup Exceptions |
| 229 |
*/ |
| 230 |
DeclException4( |
| 231 |
ExcAccessToNonLocalElement, |
| 232 |
size_type, |
| 233 |
size_type, |
| 234 |
size_type, |
| 235 |
size_type, |
| 236 |
<< "You are trying to access element " << arg1 |
| 237 |
<< " of a distributed vector, but this element is not stored " |
| 238 |
<< "on the current processor. Note: There are " << arg2 |
| 239 |
<< " elements stored " |
| 240 |
<< "on the current processor from within the range [" << arg3 << ',' |
| 241 |
<< arg4 << "] but Trilinos vectors need not store contiguous " |
| 242 |
<< "ranges on each processor, and not every element in " |
| 243 |
<< "this range may in fact be stored locally." |
| 244 |
<< "\n\n" |
| 245 |
<< "A common source for this kind of problem is that you " |
| 246 |
<< "are passing a 'fully distributed' vector into a function " |
| 247 |
<< "that needs read access to vector elements that correspond " |
| 248 |
<< "to degrees of freedom on ghost cells (or at least to " |
| 249 |
<< "'locally active' degrees of freedom that are not also " |
| 250 |
<< "'locally owned'). You need to pass a vector that has these " |
| 251 |
<< "elements as ghost entries."); |
| 252 |
|
| 253 |
private: |
| 254 |
/** |
| 255 |
* Point to the vector we are referencing. |
| 256 |
*/ |
| 257 |
Vector<Number, MemorySpace> &vector; |
| 258 |
|
| 259 |
/** |
| 260 |
* Index of the referenced element of the vector. |
| 261 |
*/ |
| 262 |
const size_type index; |
| 263 |
|
| 264 |
// Make the vector class a friend, so that it can create objects of the |
| 265 |
// present type. |
| 266 |
friend class Vector<Number, MemorySpace>; |
| 267 |
}; // class VectorReference |
| 268 |
|
| 269 |
} // namespace internal |
| 270 |
/** |
| 271 |
* @endcond |
| 272 |
*/ |
| 273 |
|
| 274 |
|
| 275 |
/** |
| 276 |
* This class implements a wrapper to the Trilinos distributed vector |
| 277 |
* class Tpetra::Vector. This class requires Trilinos to be |
| 278 |
* compiled with MPI support. |
| 279 |
* |
| 280 |
* Moreover, this class takes an optional template argument for |
| 281 |
* the memory space used. By default, all memory is allocated on the CPU. |
| 282 |
* |
| 283 |
* @ingroup TpetraWrappers |
| 284 |
* @ingroup Vectors |
| 285 |
*/ |
| 286 |
template <typename Number, typename MemorySpace = dealii::MemorySpace::Host> |
| 287 |
class Vector : public ReadVector<Number> |
| 288 |
{ |
| 289 |
public: |
| 290 |
/** |
| 291 |
* Declare some of the standard types used in all containers. |
| 292 |
*/ |
| 293 |
using value_type = Number; |
| 294 |
using real_type = typename numbers::NumberTraits<Number>::real_type; |
| 295 |
using size_type = types::global_dof_index; |
| 296 |
using reference = internal::VectorReference<Number, MemorySpace>; |
| 297 |
using const_reference = |
| 298 |
const internal::VectorReference<Number, MemorySpace>; |
| 299 |
|
| 300 |
/** |
| 301 |
* @name 1: Basic Object-handling |
| 302 |
*/ |
| 303 |
/** @{ */ |
| 304 |
/** |
| 305 |
* Default constructor that generates an empty (zero size) vector. The |
| 306 |
* function <tt>reinit()</tt> will have to give the vector the correct |
| 307 |
* size and distribution among processes in case of an MPI run. |
| 308 |
*/ |
| 309 |
Vector(); |
| 310 |
|
| 311 |
/** |
| 312 |
* Copy constructor. Sets the dimension and the partitioning to that of |
| 313 |
* the given vector and copies all elements. |
| 314 |
*/ |
| 315 |
Vector(const Vector &V); |
| 316 |
|
| 317 |
/** |
| 318 |
* Copy constructor from Teuchos::RCP<Tpetra::Vector>. |
| 319 |
*/ |
| 320 |
Vector( |
| 321 |
const Teuchos::RCP<TpetraTypes::VectorType<Number, MemorySpace>> V); |
| 322 |
|
| 323 |
/** |
| 324 |
* TODO: This is not used |
| 325 |
* This constructor takes an IndexSet that defines how to distribute the |
| 326 |
* individual components among the MPI processors. Since it also |
| 327 |
* includes information about the size of the vector, this is all we |
| 328 |
* need to generate a %parallel vector. |
| 329 |
*/ |
| 330 |
explicit Vector(const IndexSet ¶llel_partitioner, |
| 331 |
const MPI_Comm communicator); |
| 332 |
|
| 333 |
/** |
| 334 |
* In addition to just specifying one index set as in all the other |
| 335 |
* methods above, this method allows to supply an additional set of |
| 336 |
* ghost entries. |
| 337 |
* |
| 338 |
* Depending on whether the @p locally_relevant_or_ghost_entries argument uniquely |
| 339 |
* subdivides elements among processors or not, the resulting vector may |
| 340 |
* or may not have ghost elements. See the general documentation of this |
| 341 |
* class for more information. |
| 342 |
* |
| 343 |
* @see |
| 344 |
* @ref GlossGhostedVector "vectors with ghost elements" |
| 345 |
|
| 346 |
*/ |
| 347 |
explicit Vector(const IndexSet &locally_owned_entries, |
| 348 |
const IndexSet &ghost_entries, |
| 349 |
const MPI_Comm communicator, |
| 350 |
const bool vector_writable = false); |
| 351 |
|
| 352 |
/** |
| 353 |
* Release all memory and return to a state just like after having called |
| 354 |
* the default constructor. |
| 355 |
*/ |
| 356 |
void |
| 357 |
clear(); |
| 358 |
|
| 359 |
/** |
| 360 |
* Reinit functionality. This function destroys the old vector content |
| 361 |
* and generates a new one based on the input partitioning. The flag |
| 362 |
* <tt>omit_zeroing_entries</tt> determines whether the vector should be |
| 363 |
* filled with zeros (`false`) or left in an undetermined state (`true`). |
| 364 |
*/ |
| 365 |
void |
| 366 |
reinit(const IndexSet ¶llel_partitioner, |
| 367 |
const MPI_Comm communicator = MPI_COMM_WORLD, |
| 368 |
const bool omit_zeroing_entries = false); |
| 369 |
|
| 370 |
/** |
| 371 |
* Reinit functionality. This function destroys the old vector content |
| 372 |
* and generates a new one based on the input partitioning. In addition |
| 373 |
* to just specifying one index set as in all the other methods above, |
| 374 |
* this method allows to supply an additional set of ghost entries. |
| 375 |
* |
| 376 |
* Depending on whether the @p locally_relevant_or_ghost_entries argument uniquely |
| 377 |
* subdivides elements among processors or not, the resulting vector may |
| 378 |
* or may not have ghost elements. See the general documentation of this |
| 379 |
* class for more information. |
| 380 |
* |
| 381 |
* @see |
| 382 |
* @ref GlossGhostedVector "vectors with ghost elements" |
| 383 |
*/ |
| 384 |
void |
| 385 |
reinit(const IndexSet &locally_owned_entries, |
| 386 |
const IndexSet &locally_relevant_or_ghost_entries, |
| 387 |
const MPI_Comm communicator = MPI_COMM_WORLD, |
| 388 |
const bool vector_writable = false); |
| 389 |
|
| 390 |
/** |
| 391 |
* Change the dimension to that of the vector @p V. The elements of @p V are not |
| 392 |
* copied. |
| 393 |
*/ |
| 394 |
void |
| 395 |
reinit(const Vector<Number, MemorySpace> &V, |
| 396 |
const bool omit_zeroing_entries = false); |
| 397 |
|
| 398 |
/** |
| 399 |
* Swap the contents of this vector and the other vector @p v. One could do |
| 400 |
* this operation with a temporary variable and copying over the data |
| 401 |
* elements, but this function is significantly more efficient since it |
| 402 |
* only swaps the pointers to the data of the two vectors and therefore |
| 403 |
* does not need to allocate temporary storage and move data around. |
| 404 |
* |
| 405 |
* This function is analogous to the @p swap function of all C++ |
| 406 |
* standard containers. Also, there is a global function |
| 407 |
* <tt>swap(u,v)</tt> that simply calls <tt>u.swap(v)</tt>, again in |
| 408 |
* analogy to standard functions. |
| 409 |
* |
| 410 |
* This function is virtual in order to allow for derived classes to |
| 411 |
* handle memory separately. |
| 412 |
*/ |
| 413 |
virtual void |
| 414 |
swap(Vector &v) noexcept; |
| 415 |
|
| 416 |
/** |
| 417 |
* Extract a range of elements all at once. |
| 418 |
*/ |
| 419 |
virtual void |
| 420 |
extract_subvector_to( |
| 421 |
const ArrayView<const types::global_dof_index> &indices, |
| 422 |
const ArrayView<Number> &elements) const override; |
| 423 |
|
| 424 |
/** |
| 425 |
* Copy function. This function takes a Vector and copies all the |
| 426 |
* elements. The Vector will have the same parallel distribution as @p |
| 427 |
* V. |
| 428 |
* |
| 429 |
* The semantics of this operator are complex. If the two vectors have |
| 430 |
* the same size, and |
| 431 |
* if either the left or right hand side vector of the assignment (i.e., |
| 432 |
* either the input vector on the right hand side, or the calling vector |
| 433 |
* to the left of the assignment operator) currently has ghost elements, |
| 434 |
* then the left hand side vector will also have ghost values and will |
| 435 |
* consequently be a read-only vector (see also the |
| 436 |
* @ref GlossGhostedVector "glossary entry" on the issue). Otherwise, the |
| 437 |
* left hand vector will be a writable vector after this operation. |
| 438 |
* These semantics facilitate having a vector with ghost elements on the |
| 439 |
* left hand side of the assignment, and a vector without ghost elements |
| 440 |
* on the right hand side, with the resulting left hand side vector |
| 441 |
* having the correct values in both its locally owned and its ghost |
| 442 |
* elements. |
| 443 |
* |
| 444 |
* On the other hand, if the left hand side vector does not have the |
| 445 |
* correct size yet, or is perhaps an entirely uninitialized vector, |
| 446 |
* then the assignment is simply a copy operation in the usual sense: |
| 447 |
* In that case, if the right hand side has no ghost elements (i.e., |
| 448 |
* is a completely distributed vector), then the left hand side will |
| 449 |
* have no ghost elements either. And if the right hand side has |
| 450 |
* ghost elements (and is consequently read-only), then the left |
| 451 |
* hand side will have these same properties after the operation. |
| 452 |
*/ |
| 453 |
Vector & |
| 454 |
operator=(const Vector &V); |
| 455 |
|
| 456 |
/** |
| 457 |
* Copy function. This function takes a Vector and copies all the |
| 458 |
* elements. The Vector will have the same parallel distribution as @p |
| 459 |
* V. |
| 460 |
*/ |
| 461 |
template <typename OtherNumber> |
| 462 |
Vector & |
| 463 |
operator=(const dealii::Vector<OtherNumber> &V); |
| 464 |
|
| 465 |
/** |
| 466 |
* Sets all elements of the vector to the scalar @p s. This operation is |
| 467 |
* only allowed if @p s is equal to zero. |
| 468 |
*/ |
| 469 |
Vector & |
| 470 |
operator=(const Number s); |
| 471 |
|
| 472 |
/** |
| 473 |
* Imports all the elements present in the vector's IndexSet from the |
| 474 |
* input vector @p V. VectorOperation::values @p operation is used to decide if |
| 475 |
* the elements in @p V should be added to the current vector or replace the |
| 476 |
* current elements. The last parameter can be used if the same |
| 477 |
* communication pattern is used multiple times. This can be used to |
| 478 |
* improve performance. |
| 479 |
*/ |
| 480 |
void |
| 481 |
import_elements( |
| 482 |
const ReadWriteVector<Number> &V, |
| 483 |
VectorOperation::values operation, |
| 484 |
const Teuchos::RCP<const Utilities::MPI::CommunicationPatternBase> |
| 485 |
&communication_pattern); |
| 486 |
|
| 487 |
/** |
| 488 |
* @deprecated Use Teuchos::RCP<> instead of std::shared_ptr<>. |
| 489 |
*/ |
| 490 |
DEAL_II_DEPRECATED |
| 491 |
void |
| 492 |
import_elements( |
| 493 |
const ReadWriteVector<Number> &V, |
| 494 |
VectorOperation::values operation, |
| 495 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 496 |
&communication_pattern); |
| 497 |
|
| 498 |
/* |
| 499 |
* Imports all the elements present in the vector's IndexSet from the |
| 500 |
* input vector @p V. VectorOperation::values @p operation is used to decide if |
| 501 |
* the elements in @p V should be added to the current vector or replace the |
| 502 |
* current elements. |
| 503 |
*/ |
| 504 |
void |
| 505 |
import_elements(const ReadWriteVector<Number> &V, |
| 506 |
VectorOperation::values operation); |
| 507 |
|
| 508 |
/** |
| 509 |
* @deprecated Use import_elements() instead. |
| 510 |
*/ |
| 511 |
DEAL_II_DEPRECATED |
| 512 |
void |
| 513 |
import(const ReadWriteVector<Number> &V, |
| 514 |
VectorOperation::values operation, |
| 515 |
std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 516 |
communication_pattern = {}) |
| 517 |
{ |
| 518 |
import_elements(V, operation, communication_pattern); |
| 519 |
} |
| 520 |
|
| 521 |
/** @} */ |
| 522 |
|
| 523 |
|
| 524 |
/** |
| 525 |
* @name 2: Data-Access |
| 526 |
*/ |
| 527 |
/** @{ */ |
| 528 |
|
| 529 |
/** |
| 530 |
* Provide access to a given element, both read and write. |
| 531 |
* |
| 532 |
* When using a vector distributed with MPI, this operation only makes |
| 533 |
* sense for elements that are actually present on the calling processor. |
| 534 |
* Otherwise, an exception is thrown. |
| 535 |
*/ |
| 536 |
reference |
| 537 |
operator()(const size_type index); |
| 538 |
|
| 539 |
/** |
| 540 |
* Provide read-only access to an element. |
| 541 |
* |
| 542 |
* When using a vector distributed with MPI, this operation only makes |
| 543 |
* sense for elements that are actually present on the calling processor. |
| 544 |
* Otherwise, an exception is thrown. |
| 545 |
*/ |
| 546 |
Number |
| 547 |
operator()(const size_type index) const; |
| 548 |
|
| 549 |
/** |
| 550 |
* Provide access to a given element, both read and write. |
| 551 |
* |
| 552 |
* Exactly the same as operator(). |
| 553 |
*/ |
| 554 |
reference |
| 555 |
operator[](const size_type index); |
| 556 |
|
| 557 |
/** |
| 558 |
* Provide read-only access to an element. |
| 559 |
* |
| 560 |
* Exactly the same as operator(). |
| 561 |
*/ |
| 562 |
Number |
| 563 |
operator[](const size_type index) const; |
| 564 |
|
| 565 |
/** @} */ |
| 566 |
|
| 567 |
|
| 568 |
/** |
| 569 |
* @name 3: Modification of vectors |
| 570 |
*/ |
| 571 |
/** @{ */ |
| 572 |
|
| 573 |
/** |
| 574 |
* Multiply the entire vector by a fixed factor. |
| 575 |
*/ |
| 576 |
Vector & |
| 577 |
operator*=(const Number factor); |
| 578 |
|
| 579 |
/** |
| 580 |
* Divide the entire vector by a fixed factor. |
| 581 |
*/ |
| 582 |
Vector & |
| 583 |
operator/=(const Number factor); |
| 584 |
|
| 585 |
/** |
| 586 |
* Add the vector @p V to the present one. |
| 587 |
*/ |
| 588 |
Vector & |
| 589 |
operator+=(const Vector<Number, MemorySpace> &V); |
| 590 |
|
| 591 |
/** |
| 592 |
* Subtract the vector @p V from the present one. |
| 593 |
*/ |
| 594 |
Vector & |
| 595 |
operator-=(const Vector<Number, MemorySpace> &V); |
| 596 |
|
| 597 |
/** |
| 598 |
* Return the scalar product of two vectors. The vectors need to have the |
| 599 |
* same layout. |
| 600 |
*/ |
| 601 |
Number |
| 602 |
operator*(const Vector<Number, MemorySpace> &V) const; |
| 603 |
|
| 604 |
/** |
| 605 |
* Add @p a to all components. Note that @p is a scalar not a vector. |
| 606 |
*/ |
| 607 |
void |
| 608 |
add(const Number a); |
| 609 |
|
| 610 |
/** |
| 611 |
* Simple addition of a multiple of a vector, i.e. <tt>*this += |
| 612 |
* a*V</tt>. The vectors need to have the same layout. |
| 613 |
*/ |
| 614 |
void |
| 615 |
add(const Number a, const Vector<Number, MemorySpace> &V); |
| 616 |
|
| 617 |
/** |
| 618 |
* Multiple addition of multiple of a vector, i.e. <tt>*this> += |
| 619 |
* a*V+b*W</tt>. The vectors need to have the same layout. |
| 620 |
*/ |
| 621 |
void |
| 622 |
add(const Number a, |
| 623 |
const Vector<Number, MemorySpace> &V, |
| 624 |
const Number b, |
| 625 |
const Vector<Number, MemorySpace> &W); |
| 626 |
|
| 627 |
/** |
| 628 |
* A collective add operation: This function adds a whole set of values |
| 629 |
* stored in @p values to the vector components specified by @p indices. |
| 630 |
*/ |
| 631 |
void |
| 632 |
add(const std::vector<size_type> &indices, |
| 633 |
const std::vector<Number> &values); |
| 634 |
|
| 635 |
|
| 636 |
/** |
| 637 |
* Take an address where <tt>n_elements</tt> are stored contiguously and |
| 638 |
* add them into the vector. Handles all cases which are not covered by |
| 639 |
* the other two <tt>add()</tt> functions above. |
| 640 |
*/ |
| 641 |
void |
| 642 |
add(const size_type n_elements, |
| 643 |
const size_type *indices, |
| 644 |
const Number *values); |
| 645 |
|
| 646 |
/** |
| 647 |
* Scaling and simple addition of a multiple of a vector, i.e. <tt>*this |
| 648 |
* = s*(*this)+a*V</tt>. |
| 649 |
*/ |
| 650 |
void |
| 651 |
sadd(const Number s, |
| 652 |
const Number a, |
| 653 |
const Vector<Number, MemorySpace> &V); |
| 654 |
|
| 655 |
/** |
| 656 |
* A collective set operation: instead of setting individual elements of a |
| 657 |
* vector, this function allows to set a whole set of elements at once. |
| 658 |
* It is assumed that the elements to be set are located in contiguous |
| 659 |
* memory. |
| 660 |
*/ |
| 661 |
void |
| 662 |
set(const size_type n_elements, |
| 663 |
const size_type *indices, |
| 664 |
const Number *values); |
| 665 |
|
| 666 |
/** |
| 667 |
* Scale each element of this vector by the corresponding element in the |
| 668 |
* argument. This function is mostly meant to simulate multiplication |
| 669 |
* (and immediate re-assignment) by a diagonal scaling matrix. The |
| 670 |
* vectors need to have the same layout. |
| 671 |
*/ |
| 672 |
void |
| 673 |
scale(const Vector<Number, MemorySpace> &scaling_factors); |
| 674 |
|
| 675 |
/** |
| 676 |
* Assignment <tt>*this = a*V</tt>. |
| 677 |
*/ |
| 678 |
void |
| 679 |
equ(const Number a, const Vector<Number, MemorySpace> &V); |
| 680 |
|
| 681 |
/** |
| 682 |
* Return whether the vector contains only elements with value zero. |
| 683 |
*/ |
| 684 |
bool |
| 685 |
all_zero() const; |
| 686 |
|
| 687 |
/** |
| 688 |
* Return @p true if the vector has no negative entries, i.e. all entries |
| 689 |
* are zero or positive. This function is used, for example, to check |
| 690 |
* whether refinement indicators are really all positive (or zero). |
| 691 |
*/ |
| 692 |
bool |
| 693 |
is_non_negative() const; |
| 694 |
|
| 695 |
/** @} */ |
| 696 |
|
| 697 |
|
| 698 |
/** |
| 699 |
* @name 4: Scalar products, norms and related operations |
| 700 |
*/ |
| 701 |
/** @{ */ |
| 702 |
|
| 703 |
/** |
| 704 |
* Return the mean value of the element of this vector. |
| 705 |
*/ |
| 706 |
Number |
| 707 |
mean_value() const; |
| 708 |
|
| 709 |
/** |
| 710 |
* Return the l<sub>1</sub> norm of the vector (i.e., the sum of the |
| 711 |
* absolute values of all entries among all processors). |
| 712 |
*/ |
| 713 |
real_type |
| 714 |
l1_norm() const; |
| 715 |
|
| 716 |
/** |
| 717 |
* Return the l<sub>2</sub> norm of the vector (i.e., the square root of |
| 718 |
* the sum of the square of all entries among all processors). |
| 719 |
*/ |
| 720 |
real_type |
| 721 |
l2_norm() const; |
| 722 |
|
| 723 |
/** |
| 724 |
* Return the maximum norm of the vector (i.e., the maximum absolute value |
| 725 |
* among all entries and among all processors). |
| 726 |
*/ |
| 727 |
real_type |
| 728 |
linfty_norm() const; |
| 729 |
|
| 730 |
/** |
| 731 |
* Return the square of the l<sub>2</sub>-norm. |
| 732 |
*/ |
| 733 |
real_type |
| 734 |
norm_sqr() const; |
| 735 |
|
| 736 |
/** |
| 737 |
* |
| 738 |
* |
| 739 |
*/ |
| 740 |
/** |
| 741 |
* Performs a combined operation of a vector addition and a subsequent |
| 742 |
* inner product, returning the value of the inner product. In other |
| 743 |
* words, the result of this function is the same as if the user called |
| 744 |
* @code |
| 745 |
* this->add(a, V); |
| 746 |
* return_value = *this * W; |
| 747 |
* @endcode |
| 748 |
* |
| 749 |
* The reason this function exists is that this operation involves less |
| 750 |
* memory transfer than calling the two functions separately. This |
| 751 |
* method only needs to load three vectors, @p this, @p V, @p W, whereas |
| 752 |
* calling separate methods means to load the calling vector @p this |
| 753 |
* twice. Since most vector operations are memory transfer limited, this |
| 754 |
* reduces the time by 25\% (or 50\% if @p W equals @p this). |
| 755 |
* |
| 756 |
* The vectors need to have the same layout. |
| 757 |
* |
| 758 |
* For complex-valued vectors, the scalar product in the second step is |
| 759 |
* implemented as |
| 760 |
* $\left<v,w\right>=\sum_i v_i \bar{w_i}$. |
| 761 |
*/ |
| 762 |
Number |
| 763 |
add_and_dot(const Number a, |
| 764 |
const Vector<Number, MemorySpace> &V, |
| 765 |
const Vector<Number, MemorySpace> &W); |
| 766 |
|
| 767 |
/** @} */ |
| 768 |
|
| 769 |
|
| 770 |
/** |
| 771 |
* @name 5: Scalar products, norms and related operations |
| 772 |
*/ |
| 773 |
/** @{ */ |
| 774 |
|
| 775 |
/** |
| 776 |
* Return whether the vector has ghost elements or not. |
| 777 |
*/ |
| 778 |
bool |
| 779 |
has_ghost_elements() const; |
| 780 |
|
| 781 |
/** |
| 782 |
* Test for equality. This function assumes that the present vector and |
| 783 |
* the one to compare with have the same size already, since comparing |
| 784 |
* vectors of different sizes makes not much sense anyway. |
| 785 |
*/ |
| 786 |
bool |
| 787 |
operator==(const Vector<Number, MemorySpace> &v) const; |
| 788 |
|
| 789 |
/** |
| 790 |
* Test for inequality. This function assumes that the present vector and |
| 791 |
* the one to compare with have the same size already, since comparing |
| 792 |
* vectors of different sizes makes not much sense anyway. |
| 793 |
*/ |
| 794 |
bool |
| 795 |
operator!=(const Vector<Number, MemorySpace> &v) const; |
| 796 |
|
| 797 |
/** |
| 798 |
* Return the global size of the vector, equal to the sum of the number of |
| 799 |
* locally owned indices among all processors. |
| 800 |
*/ |
| 801 |
virtual size_type |
| 802 |
size() const override; |
| 803 |
|
| 804 |
/** |
| 805 |
* Return the local size of the vector, i.e., the number of indices |
| 806 |
* owned locally. |
| 807 |
*/ |
| 808 |
size_type |
| 809 |
locally_owned_size() const; |
| 810 |
|
| 811 |
/** |
| 812 |
* Return a pair of indices indicating which elements of this vector are |
| 813 |
* stored locally. The first number is the index of the first element |
| 814 |
* stored, the second the index of the one past the last one that is |
| 815 |
* stored locally. If this is a sequential vector, then the result will be |
| 816 |
* the pair <code>(0,N)</code>, otherwise it will be a pair |
| 817 |
* <code>(i,i+n)</code>, where <code>n</code> is the number of elements |
| 818 |
* stored on this processor and and <code>i</code> is the first element of |
| 819 |
* the vector stored on this processor, corresponding to the half open |
| 820 |
* interval $[i,i+n)$ |
| 821 |
* |
| 822 |
* @note The description above is true most of the time, but not always. |
| 823 |
* In particular, Trilinos vectors need not store contiguous ranges of |
| 824 |
* elements such as $[i,i+n)$. Rather, it can store vectors where the |
| 825 |
* elements are distributed in an arbitrary way across all processors and |
| 826 |
* each processor simply stores a particular subset, not necessarily |
| 827 |
* contiguous. In this case, this function clearly makes no sense since it |
| 828 |
* could, at best, return a range that includes all elements that are |
| 829 |
* stored locally. Thus, the function only succeeds if the locally stored |
| 830 |
* range is indeed contiguous. It will trigger an assertion if the local |
| 831 |
* portion of the vector is not contiguous. |
| 832 |
*/ |
| 833 |
std::pair<size_type, size_type> |
| 834 |
local_range() const; |
| 835 |
|
| 836 |
/** |
| 837 |
* Return whether @p index is in the local range or not, see also |
| 838 |
* local_range(). |
| 839 |
* |
| 840 |
* @note The same limitation for the applicability of this function |
| 841 |
* applies as listed in the documentation of local_range(). |
| 842 |
*/ |
| 843 |
bool |
| 844 |
in_local_range(const size_type index) const; |
| 845 |
|
| 846 |
/** |
| 847 |
* Return the state of the vector, i.e., whether compress() needs to be |
| 848 |
* called after an operation requiring data exchange. A call to compress() |
| 849 |
* is also needed when the method set() or add() has been called. |
| 850 |
*/ |
| 851 |
bool |
| 852 |
is_compressed() const; |
| 853 |
|
| 854 |
/** |
| 855 |
* Return the underlying MPI communicator. |
| 856 |
*/ |
| 857 |
MPI_Comm |
| 858 |
get_mpi_communicator() const; |
| 859 |
|
| 860 |
/** |
| 861 |
* Return an index set that describes which elements of this vector are |
| 862 |
* owned by the current processor. As a consequence, the index sets |
| 863 |
* returned on different processors if this is a distributed vector will |
| 864 |
* form disjoint sets that add up to the complete index set. Obviously, if |
| 865 |
* a vector is created on only one processor, then the result would |
| 866 |
* satisfy |
| 867 |
* @code |
| 868 |
* vec.locally_owned_elements() == complete_index_set(vec.size()) |
| 869 |
* @endcode |
| 870 |
*/ |
| 871 |
::dealii::IndexSet |
| 872 |
locally_owned_elements() const; |
| 873 |
|
| 874 |
/** @} */ |
| 875 |
|
| 876 |
|
| 877 |
/** |
| 878 |
* @name 6: Mixed stuff |
| 879 |
*/ |
| 880 |
/** @{ */ |
| 881 |
/** |
| 882 |
* Compress the underlying representation of the Trilinos object, i.e. |
| 883 |
* flush the buffers of the vector object if it has any. This function is |
| 884 |
* necessary after writing into a vector element-by-element and before |
| 885 |
* anything else can be done on it. |
| 886 |
* |
| 887 |
* @param operation The compress mode (<code>Add</code> or <code>Insert</code>) |
| 888 |
* in case the vector has not been written to since the last time this |
| 889 |
* function was called. The argument is ignored if the vector has been |
| 890 |
* added or written to since the last time compress() was called. |
| 891 |
* |
| 892 |
* See |
| 893 |
* @ref GlossCompress "Compressing distributed objects" |
| 894 |
* for more information. |
| 895 |
*/ |
| 896 |
void |
| 897 |
compress(const VectorOperation::values operation); |
| 898 |
|
| 899 |
/** |
| 900 |
* Return a const reference to the underlying Trilinos |
| 901 |
* Tpetra::Vector class. |
| 902 |
*/ |
| 903 |
const TpetraTypes::VectorType<Number, MemorySpace> & |
| 904 |
trilinos_vector() const; |
| 905 |
|
| 906 |
/** |
| 907 |
* Return a (modifiable) reference to the underlying Trilinos |
| 908 |
* Tpetra::Vector class. |
| 909 |
*/ |
| 910 |
TpetraTypes::VectorType<Number, MemorySpace> & |
| 911 |
trilinos_vector(); |
| 912 |
|
| 913 |
/** |
| 914 |
* Return a const Teuchos::RCP to the underlying Trilinos |
| 915 |
* Tpetra::Vector class. |
| 916 |
*/ |
| 917 |
Teuchos::RCP<const TpetraTypes::VectorType<Number, MemorySpace>> |
| 918 |
trilinos_rcp() const; |
| 919 |
|
| 920 |
/** |
| 921 |
* Return a (modifiable) Teuchos::RCP to the underlying Trilinos |
| 922 |
* Tpetra::Vector class. |
| 923 |
*/ |
| 924 |
Teuchos::RCP<TpetraTypes::VectorType<Number, MemorySpace>> |
| 925 |
trilinos_rcp(); |
| 926 |
|
| 927 |
/** |
| 928 |
* Prints the vector to the output stream @p out. |
| 929 |
*/ |
| 930 |
void |
| 931 |
print(std::ostream &out, |
| 932 |
const unsigned int precision = 3, |
| 933 |
const bool scientific = true, |
| 934 |
const bool across = true) const; |
| 935 |
|
| 936 |
/** |
| 937 |
* Return the memory consumption of this class in bytes. |
| 938 |
*/ |
| 939 |
std::size_t |
| 940 |
memory_consumption() const; |
| 941 |
|
| 942 |
/** |
| 943 |
* Return the mpi communicator |
| 944 |
*/ |
| 945 |
MPI_Comm |
| 946 |
mpi_comm() const; |
| 947 |
|
| 948 |
/** @} */ |
| 949 |
|
| 950 |
|
| 951 |
/** |
| 952 |
* The vectors have different partitioning, i.e. their IndexSet objects |
| 953 |
* don't represent the same indices. |
| 954 |
* |
| 955 |
* @ingroup Exceptions |
| 956 |
*/ |
| 957 |
DeclException0(ExcDifferentParallelPartitioning); |
| 958 |
|
| 959 |
/** |
| 960 |
* Attempt to perform an operation between two incompatible vector types. |
| 961 |
* |
| 962 |
* @ingroup Exceptions |
| 963 |
*/ |
| 964 |
DeclException0(ExcVectorTypeNotCompatible); |
| 965 |
|
| 966 |
/* |
| 967 |
* Access to a an element that is not (locally-)owned. |
| 968 |
* |
| 969 |
* @ingroup Exceptions |
| 970 |
*/ |
| 971 |
DeclException4( |
| 972 |
ExcAccessToNonLocalElement, |
| 973 |
size_type, |
| 974 |
size_type, |
| 975 |
size_type, |
| 976 |
size_type, |
| 977 |
<< "You are trying to access element " << arg1 |
| 978 |
<< " of a distributed vector, but this element is not stored " |
| 979 |
<< "on the current processor. Note: There are " << arg2 |
| 980 |
<< " elements stored " |
| 981 |
<< "on the current processor from within the range [" << arg3 << ',' |
| 982 |
<< arg4 << "] but Trilinos vectors need not store contiguous " |
| 983 |
<< "ranges on each processor, and not every element in " |
| 984 |
<< "this range may in fact be stored locally." |
| 985 |
<< "\n\n" |
| 986 |
<< "A common source for this kind of problem is that you " |
| 987 |
<< "are passing a 'fully distributed' vector into a function " |
| 988 |
<< "that needs read access to vector elements that correspond " |
| 989 |
<< "to degrees of freedom on ghost cells (or at least to " |
| 990 |
<< "'locally active' degrees of freedom that are not also " |
| 991 |
<< "'locally owned'). You need to pass a vector that has these " |
| 992 |
<< "elements as ghost entries."); |
| 993 |
|
| 994 |
/** |
| 995 |
* Missing index set. |
| 996 |
* |
| 997 |
* @ingroup Exceptions |
| 998 |
*/ |
| 999 |
DeclExceptionMsg(ExcMissingIndexSet, |
| 1000 |
"To compress a vector, a locally_relevant_dofs " |
| 1001 |
"index set, and a locally_owned_dofs index set " |
| 1002 |
"must be provided. These index sets must be " |
| 1003 |
"provided either when the vector is initialized " |
| 1004 |
"or when compress is called. See the documentation " |
| 1005 |
"of compress() for more information."); |
| 1006 |
|
| 1007 |
/** |
| 1008 |
* Exception thrown by an error in Trilinos. |
| 1009 |
* |
| 1010 |
* @ingroup Exceptions |
| 1011 |
*/ |
| 1012 |
DeclException1(ExcTrilinosError, |
| 1013 |
int, |
| 1014 |
<< "An error with error number " << arg1 |
| 1015 |
<< " occurred while calling a Trilinos function"); |
| 1016 |
|
| 1017 |
private: |
| 1018 |
/** |
| 1019 |
* Create the CommunicationPattern for the communication between the |
| 1020 |
* IndexSet @p source_index_set and the current vector based |
| 1021 |
* on the communicator @p mpi_comm. |
| 1022 |
*/ |
| 1023 |
void |
| 1024 |
create_tpetra_comm_pattern(const IndexSet &source_index_set, |
| 1025 |
const MPI_Comm mpi_comm); |
| 1026 |
|
| 1027 |
/** |
| 1028 |
* A boolean variable to hold information on whether the vector is |
| 1029 |
* compressed or not. |
| 1030 |
*/ |
| 1031 |
bool compressed; |
| 1032 |
|
| 1033 |
/** |
| 1034 |
* Store whether the vector has ghost elements or not. |
| 1035 |
* |
| 1036 |
* If the vector has no ghost elements, it can only access and modify |
| 1037 |
* entries included in the locally owned index set. |
| 1038 |
* And if the vector has ghost elements it can access and modify |
| 1039 |
* entries included in the locally relevant index set. |
| 1040 |
*/ |
| 1041 |
bool has_ghost; |
| 1042 |
|
| 1043 |
/** |
| 1044 |
* Teuchos::RCP to the actual Tpetra vector object. |
| 1045 |
*/ |
| 1046 |
Teuchos::RCP<TpetraTypes::VectorType<Number, MemorySpace>> vector; |
| 1047 |
|
| 1048 |
/** |
| 1049 |
* A vector object in Trilinos to be used for collecting the non-local |
| 1050 |
* elements if the vector was constructed with an additional IndexSet |
| 1051 |
* describing ghost elements. |
| 1052 |
*/ |
| 1053 |
Teuchos::RCP<TpetraTypes::VectorType<Number, MemorySpace>> |
| 1054 |
nonlocal_vector; |
| 1055 |
|
| 1056 |
/** |
| 1057 |
* IndexSet of the elements of the last imported vector. |
| 1058 |
*/ |
| 1059 |
dealii::IndexSet source_stored_elements; |
| 1060 |
|
| 1061 |
/** |
| 1062 |
* CommunicationPattern for the communication between the |
| 1063 |
* source_stored_elements IndexSet and the current vector. |
| 1064 |
*/ |
| 1065 |
Teuchos::RCP<const TpetraWrappers::CommunicationPattern<MemorySpace>> |
| 1066 |
tpetra_comm_pattern; |
| 1067 |
|
| 1068 |
// Make the reference class a friend. |
| 1069 |
friend class internal::VectorReference<Number, MemorySpace>; |
| 1070 |
}; |
| 1071 |
|
| 1072 |
|
| 1073 |
/* ------------------------- Inline functions ---------------------- */ |
| 1074 |
|
| 1075 |
template <typename Number, typename MemorySpace> |
| 1076 |
inline void |
| 1077 |
swap(Vector<Number, MemorySpace> &u, |
| 1078 |
Vector<Number, MemorySpace> &v) noexcept |
| 1079 |
{ |
| 1080 |
u.swap(v); |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
template <typename Number, typename MemorySpace> |
| 1085 |
inline bool |
| 1086 |
Vector<Number, MemorySpace>::has_ghost_elements() const |
| 1087 |
{ |
| 1088 |
return has_ghost; |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
template <typename Number, typename MemorySpace> |
| 1094 |
inline bool |
| 1095 |
Vector<Number, MemorySpace>::is_compressed() const |
| 1096 |
{ |
| 1097 |
return compressed; |
| 1098 |
} |
| 1099 |
|
| 1100 |
template <typename Number, typename MemorySpace> |
| 1101 |
inline void |
| 1102 |
Vector<Number, MemorySpace>::swap(Vector<Number, MemorySpace> &v) noexcept |
| 1103 |
{ |
| 1104 |
vector.swap(v.vector); |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
template <typename Number, typename MemorySpace> |
| 1109 |
inline void |
| 1110 |
Vector<Number, MemorySpace>::add(const std::vector<size_type> &indices, |
| 1111 |
const std::vector<Number> &values) |
| 1112 |
{ |
| 1113 |
// if we have ghost values, do not allow |
| 1114 |
// writing to this vector at all. |
| 1115 |
AssertDimension(indices.size(), values.size()); |
| 1116 |
|
| 1117 |
add(indices.size(), indices.data(), values.data()); |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
template <typename Number, typename MemorySpace> |
| 1123 |
inline void |
| 1124 |
Vector<Number, MemorySpace>::add(const size_type n_elements, |
| 1125 |
const size_type *indices, |
| 1126 |
const Number *values) |
| 1127 |
{ |
| 1128 |
// if we have ghost values, do not allow |
| 1129 |
// writing to this vector at all. |
| 1130 |
Assert(!has_ghost_elements(), ExcGhostsPresent()); |
| 1131 |
|
| 1132 |
auto vector_2d_local = vector->template getLocalView<Kokkos::HostSpace>( |
| 1133 |
Tpetra::Access::ReadWrite); |
| 1134 |
|
| 1135 |
// Having extracted a view into the multivectors above, now also |
| 1136 |
// extract a view into the one vector we actually store. We can |
| 1137 |
// do this right away for the locally owned part. We defer creating |
| 1138 |
// the view into the nonlocal part to when we know that we actually |
| 1139 |
// need it; this also makes sure that we correctly deal with the |
| 1140 |
// case where we do not actually store a nonlocal part. |
| 1141 |
auto vector_1d_local = Kokkos::subview(vector_2d_local, Kokkos::ALL(), 0); |
| 1142 |
using ViewType1d = decltype(vector_1d_local); |
| 1143 |
std::optional<ViewType1d> vector_1d_nonlocal; |
| 1144 |
|
| 1145 |
# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 1146 |
// Mark vector as to-be-modified. We may do the same with |
| 1147 |
// the nonlocal part too if we end up writing into it. |
| 1148 |
vector->template modify<Kokkos::HostSpace>(); |
| 1149 |
# endif |
| 1150 |
|
| 1151 |
for (size_type i = 0; i < n_elements; ++i) |
| 1152 |
{ |
| 1153 |
const size_type row = indices[i]; |
| 1154 |
|
| 1155 |
// Check if the index is in the locally owned index set. |
| 1156 |
// If so, we can write right into the locally owned |
| 1157 |
// part of the vector. |
| 1158 |
if (TrilinosWrappers::types::int_type local_row = |
| 1159 |
vector->getMap()->getLocalElement(row); |
| 1160 |
local_row != Teuchos::OrdinalTraits<int>::invalid()) |
| 1161 |
{ |
| 1162 |
vector_1d_local(local_row) += values[i]; |
| 1163 |
|
| 1164 |
// Set the compressed state to false only if there is nonlocal |
| 1165 |
// part in this distributed vector, otherwise it's always |
| 1166 |
// compressed. |
| 1167 |
if (nonlocal_vector.get() != nullptr) |
| 1168 |
compressed = false; |
| 1169 |
} |
| 1170 |
else |
| 1171 |
{ |
| 1172 |
// If the element was not in the locally owned part, |
| 1173 |
// we need to figure out whether it is in the nonlocal |
| 1174 |
// part. It better be: |
| 1175 |
Assert(nonlocal_vector.get() != nullptr, ExcInternalError()); |
| 1176 |
TrilinosWrappers::types::int_type nonlocal_row = |
| 1177 |
nonlocal_vector->getMap()->getLocalElement(row); |
| 1178 |
|
| 1179 |
# if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0) |
| 1180 |
Assert(nonlocal_row != Teuchos::OrdinalTraits<int>::invalid(), |
| 1181 |
ExcAccessToNonLocalElement( |
| 1182 |
row, |
| 1183 |
vector->getMap()->getLocalNumElements(), |
| 1184 |
vector->getMap()->getMinLocalIndex(), |
| 1185 |
vector->getMap()->getMaxLocalIndex())); |
| 1186 |
# else |
| 1187 |
Assert(nonlocal_row != Teuchos::OrdinalTraits<int>::invalid(), |
| 1188 |
ExcAccessToNonLocalElement( |
| 1189 |
row, |
| 1190 |
vector->getMap()->getNodeNumElements(), |
| 1191 |
vector->getMap()->getMinLocalIndex(), |
| 1192 |
vector->getMap()->getMaxLocalIndex())); |
| 1193 |
|
| 1194 |
# endif |
| 1195 |
|
| 1196 |
// Having asserted that it is, write into the nonlocal part. |
| 1197 |
// To do so, we first need to make sure that we have a view |
| 1198 |
// of the nonlocal part of the vectors, since we have |
| 1199 |
// deferred creating this view previously: |
| 1200 |
if (!vector_1d_nonlocal) |
| 1201 |
{ |
| 1202 |
# if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 1203 |
auto vector_2d_nonlocal = |
| 1204 |
nonlocal_vector->template getLocalView<Kokkos::HostSpace>( |
| 1205 |
Tpetra::Access::ReadWrite); |
| 1206 |
# else |
| 1207 |
auto vector_2d_nonlocal = |
| 1208 |
nonlocal_vector->template getLocalView<Kokkos::HostSpace>(); |
| 1209 |
# endif |
| 1210 |
|
| 1211 |
vector_1d_nonlocal = |
| 1212 |
Kokkos::subview(vector_2d_nonlocal, Kokkos::ALL(), 0); |
| 1213 |
|
| 1214 |
# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 1215 |
// Mark the nonlocal vector as to-be-modified as well. |
| 1216 |
nonlocal_vector->template modify<Kokkos::HostSpace>(); |
| 1217 |
# endif |
| 1218 |
} |
| 1219 |
(*vector_1d_nonlocal)(nonlocal_row) += values[i]; |
| 1220 |
compressed = false; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 1225 |
vector->template sync< |
| 1226 |
typename Tpetra::Vector<Number, int, types::signed_global_dof_index>:: |
| 1227 |
device_type::memory_space>(); |
| 1228 |
|
| 1229 |
// If we have created a view to the nonlocal part, then we have also |
| 1230 |
// written into it. Flush these modifications. |
| 1231 |
if (vector_1d_nonlocal) |
| 1232 |
nonlocal_vector->template sync< |
| 1233 |
typename Tpetra::Vector<Number, int, types::signed_global_dof_index>:: |
| 1234 |
device_type::memory_space>(); |
| 1235 |
# endif |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
template <typename Number, typename MemorySpace> |
| 1241 |
inline void |
| 1242 |
Vector<Number, MemorySpace>::set(const size_type n_elements, |
| 1243 |
const size_type *indices, |
| 1244 |
const Number *values) |
| 1245 |
{ |
| 1246 |
// if we have ghost values, do not allow |
| 1247 |
// writing to this vector at all. |
| 1248 |
Assert(!has_ghost_elements(), ExcGhostsPresent()); |
| 1249 |
|
| 1250 |
# if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 1251 |
auto vector_2d_local = vector->template getLocalView<Kokkos::HostSpace>( |
| 1252 |
Tpetra::Access::ReadWrite); |
| 1253 |
# else |
| 1254 |
vector->template sync<Kokkos::HostSpace>(); |
| 1255 |
|
| 1256 |
auto vector_2d_local = vector->template getLocalView<Kokkos::HostSpace>(); |
| 1257 |
# endif |
| 1258 |
|
| 1259 |
// Having extracted a view into the multivectors above, now also |
| 1260 |
// extract a view into the one vector we actually store. We can |
| 1261 |
// do this right away for the locally owned part. We defer creating |
| 1262 |
// the view into the nonlocal part to when we know that we actually |
| 1263 |
// need it; this also makes sure that we correctly deal with the |
| 1264 |
// case where we do not actually store a nonlocal part. |
| 1265 |
auto vector_1d_local = Kokkos::subview(vector_2d_local, Kokkos::ALL(), 0); |
| 1266 |
using ViewType1d = decltype(vector_1d_local); |
| 1267 |
std::optional<ViewType1d> vector_1d_nonlocal; |
| 1268 |
|
| 1269 |
# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 1270 |
// Mark vector as to-be-modified. We may do the same with |
| 1271 |
// the nonlocal part too if we end up writing into it. |
| 1272 |
vector->template modify<Kokkos::HostSpace>(); |
| 1273 |
# endif |
| 1274 |
|
| 1275 |
for (size_type i = 0; i < n_elements; ++i) |
| 1276 |
{ |
| 1277 |
const size_type row = indices[i]; |
| 1278 |
|
| 1279 |
// Check if the index is in the locally owned index set. |
| 1280 |
// If so, we can write right into the locally owned |
| 1281 |
// part of the vector. |
| 1282 |
if (TrilinosWrappers::types::int_type local_row = |
| 1283 |
vector->getMap()->getLocalElement(row); |
| 1284 |
local_row != Teuchos::OrdinalTraits<int>::invalid()) |
| 1285 |
{ |
| 1286 |
vector_1d_local(local_row) = values[i]; |
| 1287 |
|
| 1288 |
// Set the compressed state to false only if there is nonlocal |
| 1289 |
// part in this distributed vector, otherwise it's always |
| 1290 |
// compressed. |
| 1291 |
if (nonlocal_vector.get() != nullptr) |
| 1292 |
compressed = false; |
| 1293 |
} |
| 1294 |
else |
| 1295 |
{ |
| 1296 |
// If the element was not in the locally owned part, |
| 1297 |
// we need to figure out whether it is in the nonlocal |
| 1298 |
// part. It better be: |
| 1299 |
Assert(nonlocal_vector.get() != nullptr, ExcInternalError()); |
| 1300 |
TrilinosWrappers::types::int_type nonlocal_row = |
| 1301 |
nonlocal_vector->getMap()->getLocalElement(row); |
| 1302 |
|
| 1303 |
# if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0) |
| 1304 |
Assert(nonlocal_row != Teuchos::OrdinalTraits<int>::invalid(), |
| 1305 |
ExcAccessToNonLocalElement( |
| 1306 |
row, |
| 1307 |
vector->getMap()->getLocalNumElements(), |
| 1308 |
vector->getMap()->getMinLocalIndex(), |
| 1309 |
vector->getMap()->getMaxLocalIndex())); |
| 1310 |
# else |
| 1311 |
Assert(nonlocal_row != Teuchos::OrdinalTraits<int>::invalid(), |
| 1312 |
ExcAccessToNonLocalElement( |
| 1313 |
row, |
| 1314 |
vector->getMap()->getNodeNumElements(), |
| 1315 |
vector->getMap()->getMinLocalIndex(), |
| 1316 |
vector->getMap()->getMaxLocalIndex())); |
| 1317 |
|
| 1318 |
# endif |
| 1319 |
|
| 1320 |
// Having asserted that it is, write into the nonlocal part. |
| 1321 |
// To do so, we first need to make sure that we have a view |
| 1322 |
// of the nonlocal part of the vectors, since we have |
| 1323 |
// deferred creating this view previously: |
| 1324 |
if (!vector_1d_nonlocal) |
| 1325 |
{ |
| 1326 |
# if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 1327 |
auto vector_2d_nonlocal = |
| 1328 |
nonlocal_vector->template getLocalView<Kokkos::HostSpace>( |
| 1329 |
Tpetra::Access::ReadWrite); |
| 1330 |
# else |
| 1331 |
auto vector_2d_nonlocal = |
| 1332 |
nonlocal_vector->template getLocalView<Kokkos::HostSpace>(); |
| 1333 |
# endif |
| 1334 |
|
| 1335 |
vector_1d_nonlocal = |
| 1336 |
Kokkos::subview(vector_2d_nonlocal, Kokkos::ALL(), 0); |
| 1337 |
|
| 1338 |
# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 1339 |
// Mark the nonlocal vector as to-be-modified as well. |
| 1340 |
nonlocal_vector->template modify<Kokkos::HostSpace>(); |
| 1341 |
# endif |
| 1342 |
} |
| 1343 |
(*vector_1d_nonlocal)(nonlocal_row) = values[i]; |
| 1344 |
compressed = false; |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 1349 |
vector->template sync< |
| 1350 |
typename Tpetra::Vector<Number, int, types::signed_global_dof_index>:: |
| 1351 |
device_type::memory_space>(); |
| 1352 |
|
| 1353 |
// If we have created a view to the nonlocal part, then we have also |
| 1354 |
// written into it. Flush these modifications. |
| 1355 |
if (vector_1d_nonlocal) |
| 1356 |
nonlocal_vector->template sync< |
| 1357 |
typename Tpetra::Vector<Number, int, types::signed_global_dof_index>:: |
| 1358 |
device_type::memory_space>(); |
| 1359 |
# endif |
| 1360 |
} |
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
template <typename Number, typename MemorySpace> |
| 1365 |
inline internal::VectorReference<Number, MemorySpace> |
| 1366 |
Vector<Number, MemorySpace>::operator()(const size_type index) |
| 1367 |
{ |
| 1368 |
return internal::VectorReference(*this, index); |
| 1369 |
} |
| 1370 |
|
| 1371 |
template <typename Number, typename MemorySpace> |
| 1372 |
inline internal::VectorReference<Number, MemorySpace> |
| 1373 |
Vector<Number, MemorySpace>::operator[](const size_type index) |
| 1374 |
{ |
| 1375 |
return operator()(index); |
| 1376 |
} |
| 1377 |
|
| 1378 |
template <typename Number, typename MemorySpace> |
| 1379 |
inline Number |
| 1380 |
Vector<Number, MemorySpace>::operator[](const size_type index) const |
| 1381 |
{ |
| 1382 |
return operator()(index); |
| 1383 |
} |
| 1384 |
|
| 1385 |
# ifndef DOXYGEN |
| 1386 |
|
| 1387 |
// VectorReference |
| 1388 |
namespace internal |
| 1389 |
{ |
| 1390 |
template <typename Number, typename MemorySpace> |
| 1391 |
inline VectorReference<Number, MemorySpace>::VectorReference( |
| 1392 |
Vector<Number, MemorySpace> &vector, |
| 1393 |
const size_type index) |
| 1394 |
: vector(vector) |
| 1395 |
, index(index) |
| 1396 |
{} |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
template <typename Number, typename MemorySpace> |
| 1401 |
inline const VectorReference<Number, MemorySpace> & |
| 1402 |
VectorReference<Number, MemorySpace>::operator=( |
| 1403 |
const VectorReference<Number, MemorySpace> &r) const |
| 1404 |
{ |
| 1405 |
// as explained in the class |
| 1406 |
// documentation, this is not the copy |
| 1407 |
// operator. so simply pass on to the |
| 1408 |
// "correct" assignment operator |
| 1409 |
*this = static_cast<Number>(r); |
| 1410 |
|
| 1411 |
return *this; |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
template <typename Number, typename MemorySpace> |
| 1417 |
inline VectorReference<Number, MemorySpace> & |
| 1418 |
VectorReference<Number, MemorySpace>::operator=( |
| 1419 |
const VectorReference<Number, MemorySpace> &r) |
| 1420 |
{ |
| 1421 |
// as above |
| 1422 |
*this = static_cast<Number>(r); |
| 1423 |
|
| 1424 |
return *this; |
| 1425 |
} |
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
template <typename Number, typename MemorySpace> |
| 1430 |
inline const VectorReference<Number, MemorySpace> & |
| 1431 |
VectorReference<Number, MemorySpace>::operator=(const Number &value) const |
| 1432 |
{ |
| 1433 |
vector.set(1, &index, &value); |
| 1434 |
return *this; |
| 1435 |
} |
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
template <typename Number, typename MemorySpace> |
| 1440 |
inline const VectorReference<Number, MemorySpace> & |
| 1441 |
VectorReference<Number, MemorySpace>::operator+=( |
| 1442 |
const Number &value) const |
| 1443 |
{ |
| 1444 |
vector.add(1, &index, &value); |
| 1445 |
return *this; |
| 1446 |
} |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
template <typename Number, typename MemorySpace> |
| 1451 |
inline const VectorReference<Number, MemorySpace> & |
| 1452 |
VectorReference<Number, MemorySpace>::operator-=( |
| 1453 |
const Number &value) const |
| 1454 |
{ |
| 1455 |
Number new_value = -value; |
| 1456 |
vector.add(1, &index, &new_value); |
| 1457 |
return *this; |
| 1458 |
} |
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
template <typename Number, typename MemorySpace> |
| 1463 |
inline const VectorReference<Number, MemorySpace> & |
| 1464 |
VectorReference<Number, MemorySpace>::operator*=( |
| 1465 |
const Number &value) const |
| 1466 |
{ |
| 1467 |
Number new_value = static_cast<Number>(*this) * value; |
| 1468 |
vector.set(1, &index, &new_value); |
| 1469 |
return *this; |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
template <typename Number, typename MemorySpace> |
| 1475 |
inline const VectorReference<Number, MemorySpace> & |
| 1476 |
VectorReference<Number, MemorySpace>::operator/=( |
| 1477 |
const Number &value) const |
| 1478 |
{ |
| 1479 |
Number new_value = static_cast<Number>(*this) / value; |
| 1480 |
vector.set(1, &index, &new_value); |
| 1481 |
return *this; |
| 1482 |
} |
| 1483 |
} // namespace internal |
| 1484 |
|
| 1485 |
# endif /* DOXYGEN */ |
| 1486 |
|
| 1487 |
} // namespace TpetraWrappers |
| 1488 |
|
| 1489 |
/** @} */ |
| 1490 |
|
| 1491 |
} // namespace LinearAlgebra |
| 1492 |
|
| 1493 |
|
| 1494 |
namespace internal |
| 1495 |
{ |
| 1496 |
namespace LinearOperatorImplementation |
| 1497 |
{ |
| 1498 |
template <typename> |
| 1499 |
class ReinitHelper; |
| 1500 |
|
| 1501 |
/** |
| 1502 |
* A helper class used internally in linear_operator.h. Specialization for |
| 1503 |
* LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace>. |
| 1504 |
*/ |
| 1505 |
template <typename Number, typename MemorySpace> |
| 1506 |
class ReinitHelper< |
| 1507 |
LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace>> |
| 1508 |
{ |
| 1509 |
public: |
| 1510 |
template <typename Matrix> |
| 1511 |
static void |
| 1512 |
reinit_range_vector( |
| 1513 |
const Matrix &matrix, |
| 1514 |
LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace> &v, |
| 1515 |
bool omit_zeroing_entries) |
| 1516 |
{ |
| 1517 |
v.reinit(matrix.locally_owned_range_indices(), |
| 1518 |
matrix.get_mpi_communicator(), |
| 1519 |
omit_zeroing_entries); |
| 1520 |
} |
| 1521 |
|
| 1522 |
template <typename Matrix> |
| 1523 |
static void |
| 1524 |
reinit_domain_vector( |
| 1525 |
const Matrix &matrix, |
| 1526 |
LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace> &v, |
| 1527 |
bool omit_zeroing_entries) |
| 1528 |
{ |
| 1529 |
v.reinit(matrix.locally_owned_domain_indices(), |
| 1530 |
matrix.get_mpi_communicator(), |
| 1531 |
omit_zeroing_entries); |
| 1532 |
} |
| 1533 |
}; |
| 1534 |
|
| 1535 |
} // namespace LinearOperatorImplementation |
| 1536 |
} /* namespace internal */ |
| 1537 |
|
| 1538 |
/** |
| 1539 |
* Declare dealii::LinearAlgebra::TpetraWrappers::Vector as distributed vector. |
| 1540 |
*/ |
| 1541 |
template <typename Number, typename MemorySpace> |
| 1542 |
struct is_serial_vector< |
| 1543 |
LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace>> : std::false_type |
| 1544 |
{}; |
| 1545 |
|
| 1546 |
DEAL_II_NAMESPACE_CLOSE |
| 1547 |
|
| 1548 |
#else |
| 1549 |
|
| 1550 |
// Make sure the scripts that create the C++20 module input files have |
| 1551 |
// something to latch on if the preprocessor #ifdef above would |
| 1552 |
// otherwise lead to an empty content of the file. |
| 1553 |
DEAL_II_NAMESPACE_OPEN |
| 1554 |
DEAL_II_NAMESPACE_CLOSE |
| 1555 |
|
| 1556 |
#endif |
| 1557 |
|
| 1558 |
#endif |