| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2015 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_parallel_vector_templates_h |
| 16 |
#define dealii_parallel_vector_templates_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/partitioner.h> |
| 22 |
|
| 23 |
#include <deal.II/lac/exceptions.h> |
| 24 |
#include <deal.II/lac/la_parallel_vector.h> |
| 25 |
#include <deal.II/lac/read_write_vector.h> |
| 26 |
#include <deal.II/lac/vector.h> |
| 27 |
#include <deal.II/lac/vector_operations_internal.h> |
| 28 |
|
| 29 |
#ifdef DEAL_II_WITH_PETSC |
| 30 |
# include <deal.II/lac/petsc_vector.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef DEAL_II_WITH_TRILINOS |
| 34 |
# include <deal.II/lac/trilinos_epetra_communication_pattern.h> |
| 35 |
# include <deal.II/lac/trilinos_vector.h> |
| 36 |
|
| 37 |
DEAL_II_DISABLE_EXTRA_DIAGNOSTICS |
| 38 |
# include <Epetra_Import.h> |
| 39 |
DEAL_II_ENABLE_EXTRA_DIAGNOSTICS |
| 40 |
#endif |
| 41 |
|
| 42 |
#include <boost/io/ios_state.hpp> |
| 43 |
|
| 44 |
DEAL_II_NAMESPACE_OPEN |
| 45 |
|
| 46 |
|
| 47 |
namespace LinearAlgebra |
| 48 |
{ |
| 49 |
namespace internal |
| 50 |
{ |
| 51 |
// In the import_from_ghosted_array_finish we need to calculate the |
| 52 |
// maximal and minimal value for the given number type, which is not |
| 53 |
// straight forward for complex numbers. Therefore, comparison of complex |
| 54 |
// numbers is prohibited and throws an assert. |
| 55 |
template <typename Number> |
| 56 |
Number |
| 57 |
get_min(const Number a, const Number b) |
| 58 |
{ |
| 59 |
return std::min(a, b); |
| 60 |
} |
| 61 |
|
| 62 |
template <typename Number> |
| 63 |
std::complex<Number> |
| 64 |
get_min(const std::complex<Number> a, const std::complex<Number>) |
| 65 |
{ |
| 66 |
AssertThrow(false, |
| 67 |
ExcMessage("VectorOperation::min not " |
| 68 |
"implemented for complex numbers")); |
| 69 |
return a; |
| 70 |
} |
| 71 |
|
| 72 |
template <typename Number> |
| 73 |
Number |
| 74 |
get_max(const Number a, const Number b) |
| 75 |
{ |
| 76 |
return std::max(a, b); |
| 77 |
} |
| 78 |
|
| 79 |
template <typename Number> |
| 80 |
std::complex<Number> |
| 81 |
get_max(const std::complex<Number> a, const std::complex<Number>) |
| 82 |
{ |
| 83 |
AssertThrow(false, |
| 84 |
ExcMessage("VectorOperation::max not " |
| 85 |
"implemented for complex numbers")); |
| 86 |
return a; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
template <typename Number, typename MemorySpace> |
| 92 |
struct read_write_vector_functions |
| 93 |
{ |
| 94 |
static void |
| 95 |
import_elements( |
| 96 |
const std::shared_ptr<const ::dealii::Utilities::MPI::Partitioner> |
| 97 |
& /*communication_pattern*/, |
| 98 |
const Number * /*values*/, |
| 99 |
const VectorOperation::values /*operation*/, |
| 100 |
::dealii::LinearAlgebra::ReadWriteVector<Number> & /*rw_vector*/) |
| 101 |
{ |
| 102 |
static_assert( |
| 103 |
std::is_same_v<MemorySpace, ::dealii::MemorySpace::Host> || |
| 104 |
std::is_same_v<MemorySpace, ::dealii::MemorySpace::Default>, |
| 105 |
"MemorySpace should be Host or Default"); |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
template <typename Number> |
| 112 |
struct read_write_vector_functions<Number, ::dealii::MemorySpace::Host> |
| 113 |
{ |
| 114 |
using size_type = types::global_dof_index; |
| 115 |
|
| 116 |
|
| 117 |
static void |
| 118 |
import_elements( |
| 119 |
const std::shared_ptr<const ::dealii::Utilities::MPI::Partitioner> |
| 120 |
&communication_pattern, |
| 121 |
const Number *values, |
| 122 |
const VectorOperation::values operation, |
| 123 |
::dealii::LinearAlgebra::ReadWriteVector<Number> &rw_vector) |
| 124 |
{ |
| 125 |
distributed::Vector<Number, ::dealii::MemorySpace::Host> tmp_vector( |
| 126 |
communication_pattern); |
| 127 |
|
| 128 |
const unsigned int n_elements = |
| 129 |
communication_pattern->locally_owned_size(); |
| 130 |
std::copy(values, values + n_elements, tmp_vector.begin()); |
| 131 |
tmp_vector.update_ghost_values(); |
| 132 |
|
| 133 |
const IndexSet &stored = rw_vector.get_stored_elements(); |
| 134 |
if (operation == VectorOperation::add) |
| 135 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 136 |
rw_vector.local_element(i) += |
| 137 |
tmp_vector(stored.nth_index_in_set(i)); |
| 138 |
else if (operation == VectorOperation::min) |
| 139 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 140 |
rw_vector.local_element(i) = |
| 141 |
get_min(tmp_vector(stored.nth_index_in_set(i)), |
| 142 |
rw_vector.local_element(i)); |
| 143 |
else if (operation == VectorOperation::max) |
| 144 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 145 |
rw_vector.local_element(i) = |
| 146 |
get_max(tmp_vector(stored.nth_index_in_set(i)), |
| 147 |
rw_vector.local_element(i)); |
| 148 |
else |
| 149 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 150 |
rw_vector.local_element(i) = tmp_vector(stored.nth_index_in_set(i)); |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
template <typename Number> |
| 157 |
struct read_write_vector_functions<Number, ::dealii::MemorySpace::Default> |
| 158 |
{ |
| 159 |
using size_type = types::global_dof_index; |
| 160 |
|
| 161 |
static void |
| 162 |
import_elements( |
| 163 |
const std::shared_ptr<const ::dealii::Utilities::MPI::Partitioner> |
| 164 |
&communication_pattern, |
| 165 |
const Number *values, |
| 166 |
const VectorOperation::values operation, |
| 167 |
::dealii::LinearAlgebra::ReadWriteVector<Number> &rw_vector) |
| 168 |
{ |
| 169 |
distributed::Vector<Number, ::dealii::MemorySpace::Host> tmp_vector( |
| 170 |
communication_pattern); |
| 171 |
|
| 172 |
const unsigned int n_elements = |
| 173 |
communication_pattern->locally_owned_size(); |
| 174 |
Kokkos::deep_copy( |
| 175 |
Kokkos::View<Number *, Kokkos::HostSpace>(tmp_vector.begin(), |
| 176 |
n_elements), |
| 177 |
Kokkos::View<const Number *, |
| 178 |
::dealii::MemorySpace::Default::kokkos_space>( |
| 179 |
values, n_elements)); |
| 180 |
tmp_vector.update_ghost_values(); |
| 181 |
|
| 182 |
const IndexSet &stored = rw_vector.get_stored_elements(); |
| 183 |
if (operation == VectorOperation::add) |
| 184 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 185 |
rw_vector.local_element(i) += |
| 186 |
tmp_vector(stored.nth_index_in_set(i)); |
| 187 |
else if (operation == VectorOperation::min) |
| 188 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 189 |
rw_vector.local_element(i) = |
| 190 |
get_min(tmp_vector(stored.nth_index_in_set(i)), |
| 191 |
rw_vector.local_element(i)); |
| 192 |
else if (operation == VectorOperation::max) |
| 193 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 194 |
rw_vector.local_element(i) = |
| 195 |
get_max(tmp_vector(stored.nth_index_in_set(i)), |
| 196 |
rw_vector.local_element(i)); |
| 197 |
else |
| 198 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 199 |
rw_vector.local_element(i) = tmp_vector(stored.nth_index_in_set(i)); |
| 200 |
} |
| 201 |
}; |
| 202 |
} // namespace internal |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
template <typename Number> |
| 207 |
void |
| 208 |
ReadWriteVector<Number>::reinit(const size_type size, |
| 209 |
const bool omit_zeroing_entries) |
| 210 |
{ |
| 211 |
reinit(complete_index_set(size), omit_zeroing_entries); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
template <typename Number> |
| 217 |
template <typename Number2> |
| 218 |
void |
| 219 |
ReadWriteVector<Number>::reinit(const ReadWriteVector<Number2> &v, |
| 220 |
const bool omit_zeroing_entries) |
| 221 |
{ |
| 222 |
thread_loop_partitioner = v.thread_loop_partitioner; |
| 223 |
values.resize(v.locally_owned_size()); |
| 224 |
|
| 225 |
stored_elements = v.get_stored_elements(); |
| 226 |
|
| 227 |
if (omit_zeroing_entries == false) |
| 228 |
this->operator=(Number()); |
| 229 |
|
| 230 |
// reset the communication pattern |
| 231 |
source_stored_elements.clear(); |
| 232 |
comm_pattern.reset(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
template <typename Number> |
| 238 |
void |
| 239 |
ReadWriteVector<Number>::reinit(const IndexSet &locally_stored_indices, |
| 240 |
const bool omit_zeroing_entries) |
| 241 |
{ |
| 242 |
thread_loop_partitioner = |
| 243 |
std::make_shared<parallel::internal::TBBPartitioner>(); |
| 244 |
stored_elements = locally_stored_indices; |
| 245 |
values.resize(stored_elements.n_elements()); |
| 246 |
|
| 247 |
// initialize to zero |
| 248 |
if (omit_zeroing_entries == false) |
| 249 |
this->operator=(Number()); |
| 250 |
|
| 251 |
// reset the communication pattern |
| 252 |
source_stored_elements.clear(); |
| 253 |
comm_pattern.reset(); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
#ifdef DEAL_II_WITH_TRILINOS |
| 259 |
template <typename Number> |
| 260 |
void |
| 261 |
ReadWriteVector<Number>::reinit( |
| 262 |
const TrilinosWrappers::MPI::Vector &trilinos_vec) |
| 263 |
{ |
| 264 |
// TODO: We could avoid copying the data by just using a view into the |
| 265 |
// trilinos data but only if Number=double. Also update documentation that |
| 266 |
// the argument's lifetime needs to be longer then. If we do this, we need |
| 267 |
// to think about whether the view should be read/write. |
| 268 |
reinit(IndexSet(trilinos_vec.trilinos_partitioner()), true); |
| 269 |
|
| 270 |
TrilinosScalar *start_ptr; |
| 271 |
int leading_dimension; |
| 272 |
int ierr = trilinos_vec.trilinos_vector().ExtractView(&start_ptr, |
| 273 |
&leading_dimension); |
| 274 |
AssertThrow(ierr == 0, ExcTrilinosError(ierr)); |
| 275 |
|
| 276 |
std::copy(start_ptr, start_ptr + leading_dimension, values.data()); |
| 277 |
} |
| 278 |
#endif |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
template <typename Number> |
| 283 |
template <typename Functor> |
| 284 |
void |
| 285 |
ReadWriteVector<Number>::apply(const Functor &func) |
| 286 |
{ |
| 287 |
FunctorTemplate<Functor> functor(*this, func); |
| 288 |
dealii::internal::VectorOperations::parallel_for(functor, |
| 289 |
0, |
| 290 |
locally_owned_size(), |
| 291 |
thread_loop_partitioner); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
template <typename Number> |
| 297 |
ReadWriteVector<Number> & |
| 298 |
ReadWriteVector<Number>::operator=(const ReadWriteVector<Number> &in_vector) |
| 299 |
{ |
| 300 |
if (PointerComparison::equal(this, &in_vector)) |
| 301 |
return *this; |
| 302 |
|
| 303 |
reinit(in_vector, true); |
| 304 |
if (locally_owned_size() > 0) |
| 305 |
{ |
| 306 |
dealii::internal::VectorOperations::Vector_copy<Number, Number> copier( |
| 307 |
in_vector.values.data(), values.data()); |
| 308 |
dealii::internal::VectorOperations::parallel_for( |
| 309 |
copier, 0, locally_owned_size(), thread_loop_partitioner); |
| 310 |
} |
| 311 |
|
| 312 |
return *this; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
template <typename Number> |
| 318 |
template <typename Number2> |
| 319 |
ReadWriteVector<Number> & |
| 320 |
ReadWriteVector<Number>::operator=(const ReadWriteVector<Number2> &in_vector) |
| 321 |
{ |
| 322 |
reinit(in_vector, true); |
| 323 |
if (locally_owned_size() > 0) |
| 324 |
{ |
| 325 |
dealii::internal::VectorOperations::Vector_copy<Number, Number2> copier( |
| 326 |
in_vector.values.data(), values.data()); |
| 327 |
dealii::internal::VectorOperations::parallel_for( |
| 328 |
copier, 0, locally_owned_size(), thread_loop_partitioner); |
| 329 |
} |
| 330 |
|
| 331 |
return *this; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
template <typename Number> |
| 337 |
ReadWriteVector<Number> & |
| 338 |
ReadWriteVector<Number>::operator=(const Number s) |
| 339 |
{ |
| 340 |
Assert(s == static_cast<Number>(0), |
| 341 |
ExcMessage("Only 0 can be assigned to a vector.")); |
| 342 |
|
| 343 |
const size_type this_size = locally_owned_size(); |
| 344 |
if (this_size > 0) |
| 345 |
{ |
| 346 |
dealii::internal::VectorOperations::Vector_set<Number> setter( |
| 347 |
Number(), values.data()); |
| 348 |
dealii::internal::VectorOperations::parallel_for( |
| 349 |
setter, 0, this_size, thread_loop_partitioner); |
| 350 |
} |
| 351 |
|
| 352 |
return *this; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
namespace internal |
| 358 |
{ |
| 359 |
template <typename VectorType, typename Number> |
| 360 |
void |
| 361 |
import_serial_vector(const VectorType &values, |
| 362 |
VectorOperation::values operation, |
| 363 |
ReadWriteVector<Number> &rw_vector) |
| 364 |
{ |
| 365 |
using size_type = types::global_dof_index; |
| 366 |
|
| 367 |
const IndexSet &stored = rw_vector.get_stored_elements(); |
| 368 |
if (operation == VectorOperation::add) |
| 369 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 370 |
rw_vector.local_element(i) += values(stored.nth_index_in_set(i)); |
| 371 |
else if (operation == VectorOperation::min) |
| 372 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 373 |
rw_vector.local_element(i) = |
| 374 |
get_min(values(stored.nth_index_in_set(i)), |
| 375 |
rw_vector.local_element(i)); |
| 376 |
else if (operation == VectorOperation::max) |
| 377 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 378 |
rw_vector.local_element(i) = |
| 379 |
get_max(values(stored.nth_index_in_set(i)), |
| 380 |
rw_vector.local_element(i)); |
| 381 |
else |
| 382 |
for (size_type i = 0; i < stored.n_elements(); ++i) |
| 383 |
rw_vector.local_element(i) = values(stored.nth_index_in_set(i)); |
| 384 |
} |
| 385 |
} // namespace internal |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
template <typename Number> |
| 390 |
void |
| 391 |
ReadWriteVector<Number>::import_elements( |
| 392 |
const dealii::Vector<Number> &vec, |
| 393 |
VectorOperation::values operation, |
| 394 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 395 |
& /*communication_pattern*/) |
| 396 |
{ |
| 397 |
internal::import_serial_vector(vec, operation, *this); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
template <typename Number> |
| 403 |
template <typename MemorySpace> |
| 404 |
void |
| 405 |
ReadWriteVector<Number>::import_elements( |
| 406 |
const distributed::Vector<Number, MemorySpace> &vec, |
| 407 |
VectorOperation::values operation, |
| 408 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 409 |
&communication_pattern) |
| 410 |
{ |
| 411 |
// If no communication pattern is given, create one. Otherwise, use the |
| 412 |
// given one. |
| 413 |
std::shared_ptr<const Utilities::MPI::Partitioner> comm_pattern; |
| 414 |
if (communication_pattern.get() == nullptr) |
| 415 |
{ |
| 416 |
comm_pattern = std::make_shared<Utilities::MPI::Partitioner>( |
| 417 |
vec.locally_owned_elements(), |
| 418 |
get_stored_elements(), |
| 419 |
vec.get_mpi_communicator()); |
| 420 |
} |
| 421 |
else |
| 422 |
{ |
| 423 |
comm_pattern = |
| 424 |
std::dynamic_pointer_cast<const Utilities::MPI::Partitioner>( |
| 425 |
communication_pattern); |
| 426 |
AssertThrow(comm_pattern != nullptr, |
| 427 |
ExcMessage("The communication pattern is not of type " |
| 428 |
"Utilities::MPI::Partitioner.")); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
internal::read_write_vector_functions<Number, MemorySpace>::import_elements( |
| 433 |
comm_pattern, vec.begin(), operation, *this); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
#ifdef DEAL_II_WITH_PETSC |
| 439 |
namespace internal |
| 440 |
{ |
| 441 |
template <typename PETSC_Number, typename Number> |
| 442 |
void |
| 443 |
copy_petsc_vector(const PETSC_Number *petsc_start_ptr, |
| 444 |
const PETSC_Number *petsc_end_ptr, |
| 445 |
Number *ptr) |
| 446 |
{ |
| 447 |
std::copy(petsc_start_ptr, petsc_end_ptr, ptr); |
| 448 |
} |
| 449 |
|
| 450 |
template <typename PETSC_Number, typename Number> |
| 451 |
void |
| 452 |
copy_petsc_vector(const std::complex<PETSC_Number> *petsc_start_ptr, |
| 453 |
const std::complex<PETSC_Number> *petsc_end_ptr, |
| 454 |
std::complex<Number> *ptr) |
| 455 |
{ |
| 456 |
std::copy(petsc_start_ptr, petsc_end_ptr, ptr); |
| 457 |
} |
| 458 |
|
| 459 |
template <typename PETSC_Number, typename Number> |
| 460 |
void |
| 461 |
copy_petsc_vector(const std::complex<PETSC_Number> * /*petsc_start_ptr*/, |
| 462 |
const std::complex<PETSC_Number> * /*petsc_end_ptr*/, |
| 463 |
Number * /*ptr*/) |
| 464 |
{ |
| 465 |
AssertThrow(false, ExcMessage("Tried to copy complex -> real")); |
| 466 |
} |
| 467 |
} // namespace internal |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
template <typename Number> |
| 472 |
void |
| 473 |
ReadWriteVector<Number>::import_elements( |
| 474 |
const PETScWrappers::MPI::Vector &petsc_vec, |
| 475 |
VectorOperation::values /*operation*/, |
| 476 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 477 |
& /*communication_pattern*/) |
| 478 |
{ |
| 479 |
// TODO: this works only if no communication is needed. |
| 480 |
Assert(petsc_vec.locally_owned_elements() == stored_elements, |
| 481 |
StandardExceptions::ExcInvalidState()); |
| 482 |
|
| 483 |
// get a representation of the vector and copy it |
| 484 |
const PetscScalar *start_ptr; |
| 485 |
PetscErrorCode ierr = |
| 486 |
VecGetArrayRead(static_cast<const Vec &>(petsc_vec), &start_ptr); |
| 487 |
AssertThrow(ierr == 0, ExcPETScError(ierr)); |
| 488 |
|
| 489 |
const size_type vec_size = petsc_vec.locally_owned_size(); |
| 490 |
internal::copy_petsc_vector(start_ptr, start_ptr + vec_size, begin()); |
| 491 |
|
| 492 |
// restore the representation of the vector |
| 493 |
ierr = VecRestoreArrayRead(static_cast<const Vec &>(petsc_vec), &start_ptr); |
| 494 |
AssertThrow(ierr == 0, ExcPETScError(ierr)); |
| 495 |
} |
| 496 |
#endif |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
#ifdef DEAL_II_TRILINOS_WITH_TPETRA |
| 501 |
template <typename Number> |
| 502 |
template <typename NodeType, typename Dummy> |
| 503 |
std::enable_if_t<std::is_same_v<Dummy, Number> && |
| 504 |
dealii::is_tpetra_type<Number>::value> |
| 505 |
ReadWriteVector<Number>::import_elements( |
| 506 |
const Tpetra::Vector<Number, int, types::signed_global_dof_index, NodeType> |
| 507 |
&vector, |
| 508 |
const IndexSet &source_elements, |
| 509 |
VectorOperation::values operation, |
| 510 |
const MPI_Comm mpi_comm, |
| 511 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 512 |
&communication_pattern) |
| 513 |
{ |
| 514 |
using MemorySpace = std::conditional_t< |
| 515 |
std::is_same_v<typename NodeType::memory_space, Kokkos::HostSpace>, |
| 516 |
dealii::MemorySpace::Host, |
| 517 |
dealii::MemorySpace::Default>; |
| 518 |
|
| 519 |
std::shared_ptr<const TpetraWrappers::CommunicationPattern<MemorySpace>> |
| 520 |
tpetra_comm_pattern; |
| 521 |
|
| 522 |
// If no communication pattern is given, create one. Otherwise, use the one |
| 523 |
// given. |
| 524 |
if (communication_pattern == nullptr) |
| 525 |
{ |
| 526 |
// The first time import is called, we create a communication pattern. |
| 527 |
// Check if the communication pattern already exists and if it can be |
| 528 |
// reused. |
| 529 |
if ((source_elements.size() == source_stored_elements.size()) && |
| 530 |
(source_elements == source_stored_elements)) |
| 531 |
{ |
| 532 |
tpetra_comm_pattern = std::dynamic_pointer_cast< |
| 533 |
const TpetraWrappers::CommunicationPattern<MemorySpace>>( |
| 534 |
comm_pattern); |
| 535 |
if (tpetra_comm_pattern == nullptr) |
| 536 |
tpetra_comm_pattern = std::make_shared< |
| 537 |
const TpetraWrappers::CommunicationPattern<MemorySpace>>( |
| 538 |
create_tpetra_comm_pattern<MemorySpace>(source_elements, |
| 539 |
mpi_comm)); |
| 540 |
} |
| 541 |
else |
| 542 |
tpetra_comm_pattern = std::make_shared< |
| 543 |
const TpetraWrappers::CommunicationPattern<MemorySpace>>( |
| 544 |
create_tpetra_comm_pattern<MemorySpace>(source_elements, mpi_comm)); |
| 545 |
} |
| 546 |
else |
| 547 |
{ |
| 548 |
tpetra_comm_pattern = std::dynamic_pointer_cast< |
| 549 |
const TpetraWrappers::CommunicationPattern<MemorySpace>>( |
| 550 |
communication_pattern); |
| 551 |
AssertThrow(tpetra_comm_pattern != nullptr, |
| 552 |
ExcMessage( |
| 553 |
"The communication pattern is not of type " |
| 554 |
"LinearAlgebra::TpetraWrappers::CommunicationPattern.")); |
| 555 |
} |
| 556 |
|
| 557 |
Tpetra::Export<int, types::signed_global_dof_index, NodeType> tpetra_export( |
| 558 |
tpetra_comm_pattern->get_tpetra_export()); |
| 559 |
|
| 560 |
Tpetra::Vector<Number, int, types::signed_global_dof_index, NodeType> |
| 561 |
target_vector(tpetra_export.getSourceMap()); |
| 562 |
|
| 563 |
// Communicate the vector to the correct map. |
| 564 |
// Remark: We use here doImport on an Export object since we have to use |
| 565 |
// the communication plan stored in the tpetra_comm_pattern |
| 566 |
// backward. |
| 567 |
target_vector.doImport(vector, tpetra_export, Tpetra::INSERT); |
| 568 |
|
| 569 |
# if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) |
| 570 |
auto vector_2d = target_vector.template getLocalView<Kokkos::HostSpace>( |
| 571 |
Tpetra::Access::ReadOnly); |
| 572 |
# else |
| 573 |
target_vector.template sync<Kokkos::HostSpace>(); |
| 574 |
auto vector_2d = target_vector.template getLocalView<Kokkos::HostSpace>(); |
| 575 |
# endif |
| 576 |
auto new_values = Kokkos::subview(vector_2d, Kokkos::ALL(), 0); |
| 577 |
auto size = target_vector.getLocalLength(); |
| 578 |
|
| 579 |
using size_type = std::decay_t<decltype(size)>; |
| 580 |
|
| 581 |
Assert(size == 0 || !values.empty(), ExcInternalError("Import failed.")); |
| 582 |
AssertDimension(size, stored_elements.n_elements()); |
| 583 |
|
| 584 |
switch (operation) |
| 585 |
{ |
| 586 |
case VectorOperation::insert: |
| 587 |
for (size_type i = 0; i < size; ++i) |
| 588 |
values[i] = Number(new_values(i)); |
| 589 |
break; |
| 590 |
|
| 591 |
case VectorOperation::add: |
| 592 |
for (size_type i = 0; i < size; ++i) |
| 593 |
values[i] += Number(new_values(i)); |
| 594 |
break; |
| 595 |
|
| 596 |
case VectorOperation::min: |
| 597 |
// To ensure that this code also compiles with complex |
| 598 |
// numbers, we only compare the real part of the |
| 599 |
// variable. Note that min/max do not make sense with complex |
| 600 |
// numbers. |
| 601 |
for (size_type i = 0; i < size; ++i) |
| 602 |
{ |
| 603 |
Assert( |
| 604 |
std::imag(Number(new_values(i))) == 0., |
| 605 |
ExcMessage( |
| 606 |
"VectorOperation::min is not defined if there is an imaginary part!)")); |
| 607 |
Assert( |
| 608 |
std::imag(values[i]) == 0., |
| 609 |
ExcMessage( |
| 610 |
"VectorOperation::min is not defined if there is an imaginary part!)")); |
| 611 |
if (std::real(Number(new_values(i))) - std::real(values[i]) < 0.0) |
| 612 |
values[i] = new_values(i); |
| 613 |
} |
| 614 |
break; |
| 615 |
|
| 616 |
case VectorOperation::max: |
| 617 |
for (size_type i = 0; i < size; ++i) |
| 618 |
{ |
| 619 |
Assert( |
| 620 |
std::imag(Number(new_values(i))) == 0., |
| 621 |
ExcMessage( |
| 622 |
"VectorOperation::max is not defined if there is an imaginary part!)")); |
| 623 |
Assert( |
| 624 |
std::imag(values[i]) == 0., |
| 625 |
ExcMessage( |
| 626 |
"VectorOperation::max is not defined if there is an imaginary part!)")); |
| 627 |
if (std::real(Number(new_values(i))) - std::real(values[i]) > 0.0) |
| 628 |
values[i] = Number(new_values(i)); |
| 629 |
} |
| 630 |
break; |
| 631 |
|
| 632 |
default: |
| 633 |
AssertThrow(false, ExcNotImplemented()); |
| 634 |
} |
| 635 |
} |
| 636 |
#endif |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
#ifdef DEAL_II_WITH_TRILINOS |
| 641 |
template <typename Number> |
| 642 |
void |
| 643 |
ReadWriteVector<Number>::import_elements( |
| 644 |
const Epetra_MultiVector &multivector, |
| 645 |
const IndexSet &source_elements, |
| 646 |
VectorOperation::values operation, |
| 647 |
const MPI_Comm mpi_comm, |
| 648 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 649 |
&communication_pattern) |
| 650 |
{ |
| 651 |
std::shared_ptr<const EpetraWrappers::CommunicationPattern> |
| 652 |
epetra_comm_pattern; |
| 653 |
|
| 654 |
// If no communication pattern is given, create one. Otherwise, use the one |
| 655 |
// given. |
| 656 |
if (communication_pattern == nullptr) |
| 657 |
{ |
| 658 |
// The first time import is called, we create a communication pattern. |
| 659 |
// Check if the communication pattern already exists and if it can be |
| 660 |
// reused. |
| 661 |
if ((source_elements.size() == source_stored_elements.size()) && |
| 662 |
(source_elements == source_stored_elements)) |
| 663 |
{ |
| 664 |
epetra_comm_pattern = std::dynamic_pointer_cast< |
| 665 |
const EpetraWrappers::CommunicationPattern>(comm_pattern); |
| 666 |
if (epetra_comm_pattern == nullptr) |
| 667 |
epetra_comm_pattern = |
| 668 |
std::make_shared<const EpetraWrappers::CommunicationPattern>( |
| 669 |
create_epetra_comm_pattern(source_elements, mpi_comm)); |
| 670 |
} |
| 671 |
else |
| 672 |
epetra_comm_pattern = |
| 673 |
std::make_shared<const EpetraWrappers::CommunicationPattern>( |
| 674 |
create_epetra_comm_pattern(source_elements, mpi_comm)); |
| 675 |
} |
| 676 |
else |
| 677 |
{ |
| 678 |
epetra_comm_pattern = |
| 679 |
std::dynamic_pointer_cast<const EpetraWrappers::CommunicationPattern>( |
| 680 |
communication_pattern); |
| 681 |
AssertThrow(epetra_comm_pattern != nullptr, |
| 682 |
ExcMessage( |
| 683 |
"The communication pattern is not of type " |
| 684 |
"LinearAlgebra::EpetraWrappers::CommunicationPattern.")); |
| 685 |
} |
| 686 |
|
| 687 |
Epetra_Import import_map(epetra_comm_pattern->get_epetra_import()); |
| 688 |
|
| 689 |
Epetra_FEVector target_vector(import_map.TargetMap()); |
| 690 |
|
| 691 |
if (operation == VectorOperation::insert) |
| 692 |
{ |
| 693 |
const int err = target_vector.Import(multivector, import_map, Insert); |
| 694 |
AssertThrow(err == 0, |
| 695 |
ExcMessage("Epetra Import() failed with error code: " + |
| 696 |
std::to_string(err))); |
| 697 |
|
| 698 |
const double *new_values = target_vector.Values(); |
| 699 |
const int size = target_vector.MyLength(); |
| 700 |
Assert(size == 0 || values.size() > 0, |
| 701 |
ExcInternalError("Import failed.")); |
| 702 |
|
| 703 |
for (int i = 0; i < size; ++i) |
| 704 |
values[i] = new_values[i]; |
| 705 |
} |
| 706 |
else if (operation == VectorOperation::add) |
| 707 |
{ |
| 708 |
const int err = target_vector.Import(multivector, import_map, Add); |
| 709 |
AssertThrow(err == 0, |
| 710 |
ExcMessage("Epetra Import() failed with error code: " + |
| 711 |
std::to_string(err))); |
| 712 |
|
| 713 |
const double *new_values = target_vector.Values(); |
| 714 |
const int size = target_vector.MyLength(); |
| 715 |
Assert(size == 0 || values.size() > 0, |
| 716 |
ExcInternalError("Import failed.")); |
| 717 |
|
| 718 |
for (int i = 0; i < size; ++i) |
| 719 |
values[i] += new_values[i]; |
| 720 |
} |
| 721 |
else if (operation == VectorOperation::min) |
| 722 |
{ |
| 723 |
const int err = target_vector.Import(multivector, import_map, Add); |
| 724 |
AssertThrow(err == 0, |
| 725 |
ExcMessage("Epetra Import() failed with error code: " + |
| 726 |
std::to_string(err))); |
| 727 |
|
| 728 |
const double *new_values = target_vector.Values(); |
| 729 |
const int size = target_vector.MyLength(); |
| 730 |
Assert(size == 0 || values.size() > 0, |
| 731 |
ExcInternalError("Import failed.")); |
| 732 |
|
| 733 |
// To ensure that this code also compiles with complex |
| 734 |
// numbers, we only compare the real part of the |
| 735 |
// variable. Note that min/max do not make sense with complex |
| 736 |
// numbers. |
| 737 |
for (int i = 0; i < size; ++i) |
| 738 |
{ |
| 739 |
Assert( |
| 740 |
std::imag(new_values[i]) == 0., |
| 741 |
ExcMessage( |
| 742 |
"VectorOperation::min is not defined if there is an imaginary part!)")); |
| 743 |
Assert( |
| 744 |
std::imag(values[i]) == 0., |
| 745 |
ExcMessage( |
| 746 |
"VectorOperation::min is not defined if there is an imaginary part!)")); |
| 747 |
if (std::real(new_values[i]) - std::real(values[i]) < 0.0) |
| 748 |
values[i] = new_values[i]; |
| 749 |
} |
| 750 |
} |
| 751 |
else if (operation == VectorOperation::max) |
| 752 |
{ |
| 753 |
const int err = target_vector.Import(multivector, import_map, Add); |
| 754 |
AssertThrow(err == 0, |
| 755 |
ExcMessage("Epetra Import() failed with error code: " + |
| 756 |
std::to_string(err))); |
| 757 |
|
| 758 |
const double *new_values = target_vector.Values(); |
| 759 |
const int size = target_vector.MyLength(); |
| 760 |
Assert(size == 0 || values.size() > 0, |
| 761 |
ExcInternalError("Import failed.")); |
| 762 |
|
| 763 |
for (int i = 0; i < size; ++i) |
| 764 |
{ |
| 765 |
Assert( |
| 766 |
std::imag(new_values[i]) == 0., |
| 767 |
ExcMessage( |
| 768 |
"VectorOperation::max is not defined if there is an imaginary part!)")); |
| 769 |
Assert( |
| 770 |
std::imag(values[i]) == 0., |
| 771 |
ExcMessage( |
| 772 |
"VectorOperation::max is not defined if there is an imaginary part!)")); |
| 773 |
if (std::real(new_values[i]) - std::real(values[i]) > 0.0) |
| 774 |
values[i] = new_values[i]; |
| 775 |
} |
| 776 |
} |
| 777 |
else |
| 778 |
AssertThrow(false, ExcNotImplemented()); |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
template <typename Number> |
| 783 |
void |
| 784 |
ReadWriteVector<Number>::import_elements( |
| 785 |
const TrilinosWrappers::MPI::Vector &trilinos_vec, |
| 786 |
VectorOperation::values operation, |
| 787 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 788 |
&communication_pattern) |
| 789 |
{ |
| 790 |
// While the import does work with Trilinos 12.8.x, it fails with 12.4.x. To |
| 791 |
// be safe, we disable it here. Note that it would be a useful case, as |
| 792 |
// ReadWriteVector is supposed to replace ghosted vectors anyways. |
| 793 |
AssertThrow( |
| 794 |
!trilinos_vec.has_ghost_elements(), |
| 795 |
ExcMessage( |
| 796 |
"Import() from TrilinosWrappers::MPI::Vector with ghost entries is not supported!")); |
| 797 |
import_elements(trilinos_vec.trilinos_vector(), |
| 798 |
trilinos_vec.locally_owned_elements(), |
| 799 |
operation, |
| 800 |
trilinos_vec.get_mpi_communicator(), |
| 801 |
communication_pattern); |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
# ifdef DEAL_II_TRILINOS_WITH_TPETRA |
| 807 |
template <typename Number> |
| 808 |
template <typename MemorySpace, typename Dummy> |
| 809 |
std::enable_if_t<std::is_same_v<Dummy, Number> && |
| 810 |
dealii::is_tpetra_type<Number>::value> |
| 811 |
ReadWriteVector<Number>::import_elements( |
| 812 |
const LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace> |
| 813 |
&trilinos_vec, |
| 814 |
VectorOperation::values operation, |
| 815 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 816 |
&communication_pattern) |
| 817 |
{ |
| 818 |
import_elements(trilinos_vec.trilinos_vector(), |
| 819 |
trilinos_vec.locally_owned_elements(), |
| 820 |
operation, |
| 821 |
trilinos_vec.get_mpi_communicator(), |
| 822 |
communication_pattern); |
| 823 |
} |
| 824 |
# endif |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
template <typename Number> |
| 829 |
void |
| 830 |
ReadWriteVector<Number>::import_elements( |
| 831 |
const LinearAlgebra::EpetraWrappers::Vector &trilinos_vec, |
| 832 |
VectorOperation::values operation, |
| 833 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 834 |
&communication_pattern) |
| 835 |
{ |
| 836 |
import_elements(trilinos_vec.trilinos_vector(), |
| 837 |
trilinos_vec.locally_owned_elements(), |
| 838 |
operation, |
| 839 |
trilinos_vec.get_mpi_communicator(), |
| 840 |
communication_pattern); |
| 841 |
} |
| 842 |
#endif |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
template <typename Number> |
| 847 |
void |
| 848 |
ReadWriteVector<Number>::swap(ReadWriteVector<Number> &v) noexcept |
| 849 |
{ |
| 850 |
std::swap(stored_elements, v.stored_elements); |
| 851 |
std::swap(values, v.values); |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
template <typename Number> |
| 857 |
std::size_t |
| 858 |
ReadWriteVector<Number>::memory_consumption() const |
| 859 |
{ |
| 860 |
std::size_t memory = sizeof(*this); |
| 861 |
memory += |
| 862 |
sizeof(Number) * static_cast<std::size_t>(this->locally_owned_size()); |
| 863 |
|
| 864 |
memory += stored_elements.memory_consumption(); |
| 865 |
|
| 866 |
return memory; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
template <typename Number> |
| 872 |
void |
| 873 |
ReadWriteVector<Number>::print(std::ostream &out, |
| 874 |
const unsigned int precision, |
| 875 |
const bool scientific) const |
| 876 |
{ |
| 877 |
AssertThrow(out.fail() == false, ExcIO()); |
| 878 |
boost::io::ios_flags_saver restore_flags(out); |
| 879 |
|
| 880 |
out.precision(precision); |
| 881 |
if (scientific) |
| 882 |
out.setf(std::ios::scientific, std::ios::floatfield); |
| 883 |
else |
| 884 |
out.setf(std::ios::fixed, std::ios::floatfield); |
| 885 |
|
| 886 |
out << "IndexSet: "; |
| 887 |
stored_elements.print(out); |
| 888 |
out << std::endl; |
| 889 |
unsigned int i = 0; |
| 890 |
for (const auto idx : this->stored_elements) |
| 891 |
out << '[' << idx << "]: " << values[i++] << '\n'; |
| 892 |
out << std::flush; |
| 893 |
|
| 894 |
AssertThrow(out.fail() == false, ExcIO()); |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
#ifdef DEAL_II_WITH_TRILINOS |
| 900 |
# ifdef DEAL_II_TRILINOS_WITH_TPETRA |
| 901 |
template <typename Number> |
| 902 |
template <typename MemorySpace> |
| 903 |
TpetraWrappers::CommunicationPattern<MemorySpace> |
| 904 |
ReadWriteVector<Number>::create_tpetra_comm_pattern( |
| 905 |
const IndexSet &source_index_set, |
| 906 |
const MPI_Comm mpi_comm) |
| 907 |
{ |
| 908 |
source_stored_elements = source_index_set; |
| 909 |
TpetraWrappers::CommunicationPattern<MemorySpace> tpetra_comm_pattern( |
| 910 |
source_stored_elements, stored_elements, mpi_comm); |
| 911 |
comm_pattern = |
| 912 |
std::make_shared<TpetraWrappers::CommunicationPattern<MemorySpace>>( |
| 913 |
source_stored_elements, stored_elements, mpi_comm); |
| 914 |
|
| 915 |
return tpetra_comm_pattern; |
| 916 |
} |
| 917 |
# endif |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
template <typename Number> |
| 922 |
EpetraWrappers::CommunicationPattern |
| 923 |
ReadWriteVector<Number>::create_epetra_comm_pattern( |
| 924 |
const IndexSet &source_index_set, |
| 925 |
const MPI_Comm mpi_comm) |
| 926 |
{ |
| 927 |
source_stored_elements = source_index_set; |
| 928 |
EpetraWrappers::CommunicationPattern epetra_comm_pattern( |
| 929 |
source_stored_elements, stored_elements, mpi_comm); |
| 930 |
comm_pattern = std::make_shared<EpetraWrappers::CommunicationPattern>( |
| 931 |
source_stored_elements, stored_elements, mpi_comm); |
| 932 |
|
| 933 |
return epetra_comm_pattern; |
| 934 |
} |
| 935 |
#endif |
| 936 |
} // end of namespace LinearAlgebra |
| 937 |
|
| 938 |
|
| 939 |
DEAL_II_NAMESPACE_CLOSE |
| 940 |
|
| 941 |
#endif |