| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2012 - 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_la_parallel_vector_templates_h |
| 16 |
#define dealii_la_parallel_vector_templates_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/mpi.h> |
| 22 |
|
| 23 |
#include <deal.II/lac/exceptions.h> |
| 24 |
#include <deal.II/lac/la_parallel_vector.h> |
| 25 |
#include <deal.II/lac/petsc_vector.h> |
| 26 |
#include <deal.II/lac/read_write_vector.h> |
| 27 |
#include <deal.II/lac/trilinos_vector.h> |
| 28 |
#include <deal.II/lac/vector_operations_internal.h> |
| 29 |
|
| 30 |
#include <Kokkos_Core.hpp> |
| 31 |
|
| 32 |
#include <memory> |
| 33 |
|
| 34 |
|
| 35 |
DEAL_II_NAMESPACE_OPEN |
| 36 |
|
| 37 |
|
| 38 |
namespace LinearAlgebra |
| 39 |
{ |
| 40 |
namespace distributed |
| 41 |
{ |
| 42 |
namespace internal |
| 43 |
{ |
| 44 |
// In the import_from_ghosted_array_finish we might need to calculate the |
| 45 |
// maximal and minimal value for the given number type, which is not |
| 46 |
// straightforward for complex numbers. Therefore, comparison of complex |
| 47 |
// numbers is prohibited and throws an exception. |
| 48 |
template <typename Number> |
| 49 |
Number |
| 50 |
get_min(const Number a, const Number b) |
| 51 |
{ |
| 52 |
return std::min(a, b); |
| 53 |
} |
| 54 |
|
| 55 |
template <typename Number> |
| 56 |
std::complex<Number> |
| 57 |
get_min(const std::complex<Number> a, const std::complex<Number>) |
| 58 |
{ |
| 59 |
AssertThrow(false, |
| 60 |
ExcMessage("VectorOperation::min not " |
| 61 |
"implemented for complex numbers")); |
| 62 |
return a; |
| 63 |
} |
| 64 |
|
| 65 |
template <typename Number> |
| 66 |
Number |
| 67 |
get_max(const Number a, const Number b) |
| 68 |
{ |
| 69 |
return std::max(a, b); |
| 70 |
} |
| 71 |
|
| 72 |
template <typename Number> |
| 73 |
std::complex<Number> |
| 74 |
get_max(const std::complex<Number> a, const std::complex<Number>) |
| 75 |
{ |
| 76 |
AssertThrow(false, |
| 77 |
ExcMessage("VectorOperation::max not " |
| 78 |
"implemented for complex numbers")); |
| 79 |
return a; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
// Resize the underlying array on the host or on the device |
| 85 |
template <typename Number, typename MemorySpaceType> |
| 86 |
struct la_parallel_vector_templates_functions |
| 87 |
{ |
| 88 |
static_assert(std::is_same_v<MemorySpaceType, MemorySpace::Host> || |
| 89 |
std::is_same_v<MemorySpaceType, MemorySpace::Default>, |
| 90 |
"MemorySpace should be Host or Default"); |
| 91 |
|
| 92 |
static void |
| 93 |
resize_val( |
| 94 |
const types::global_dof_index /*new_alloc_size*/, |
| 95 |
types::global_dof_index & /*allocated_size*/, |
| 96 |
::dealii::MemorySpace::MemorySpaceData<Number, MemorySpaceType> |
| 97 |
& /*data*/, |
| 98 |
const MPI_Comm /*comm_sm*/) |
| 99 |
{} |
| 100 |
|
| 101 |
static void |
| 102 |
import_elements( |
| 103 |
const ::dealii::LinearAlgebra::ReadWriteVector<Number> & /*V*/, |
| 104 |
VectorOperation::values /*operation*/, |
| 105 |
const std::shared_ptr<const ::dealii::Utilities::MPI::Partitioner> & |
| 106 |
/*communication_pattern*/, |
| 107 |
const IndexSet & /*locally_owned_elem*/, |
| 108 |
::dealii::MemorySpace::MemorySpaceData<Number, MemorySpaceType> |
| 109 |
& /*data*/) |
| 110 |
{} |
| 111 |
|
| 112 |
template <typename RealType> |
| 113 |
static void |
| 114 |
linfty_norm_local( |
| 115 |
const ::dealii::MemorySpace::MemorySpaceData<Number, MemorySpaceType> |
| 116 |
& /*data*/, |
| 117 |
const unsigned int /*size*/, |
| 118 |
RealType & /*max*/) |
| 119 |
{} |
| 120 |
}; |
| 121 |
|
| 122 |
template <typename Number> |
| 123 |
struct la_parallel_vector_templates_functions<Number, |
| 124 |
::dealii::MemorySpace::Host> |
| 125 |
{ |
| 126 |
using size_type = types::global_dof_index; |
| 127 |
|
| 128 |
static void |
| 129 |
resize_val(const types::global_dof_index new_alloc_size, |
| 130 |
types::global_dof_index &allocated_size, |
| 131 |
::dealii::MemorySpace:: |
| 132 |
MemorySpaceData<Number, ::dealii::MemorySpace::Host> &data, |
| 133 |
const MPI_Comm comm_shared) |
| 134 |
{ |
| 135 |
if (comm_shared == MPI_COMM_SELF) |
| 136 |
{ |
| 137 |
#if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0) |
| 138 |
Kokkos::resize(Kokkos::WithoutInitializing, |
| 139 |
data.values, |
| 140 |
new_alloc_size); |
| 141 |
#else |
| 142 |
Kokkos::resize(data.values, new_alloc_size); |
| 143 |
#endif |
| 144 |
|
| 145 |
allocated_size = new_alloc_size; |
| 146 |
|
| 147 |
data.values_sm = { |
| 148 |
ArrayView<const Number>(data.values.data(), new_alloc_size)}; |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
#ifdef DEAL_II_WITH_MPI |
| 153 |
allocated_size = new_alloc_size; |
| 154 |
|
| 155 |
const unsigned int size_sm = |
| 156 |
Utilities::MPI::n_mpi_processes(comm_shared); |
| 157 |
const unsigned int rank_sm = |
| 158 |
Utilities::MPI::this_mpi_process(comm_shared); |
| 159 |
|
| 160 |
MPI_Win mpi_window; |
| 161 |
Number *data_this; |
| 162 |
|
| 163 |
|
| 164 |
std::vector<Number *> others(size_sm); |
| 165 |
|
| 166 |
MPI_Info info; |
| 167 |
int ierr = MPI_Info_create(&info); |
| 168 |
AssertThrowMPI(ierr); |
| 169 |
|
| 170 |
ierr = MPI_Info_set(info, "alloc_shared_noncontig", "true"); |
| 171 |
AssertThrowMPI(ierr); |
| 172 |
|
| 173 |
const std::size_t align_by = 64; |
| 174 |
|
| 175 |
std::size_t s = ((new_alloc_size * sizeof(Number) + align_by) / |
| 176 |
sizeof(Number)) * |
| 177 |
sizeof(Number); |
| 178 |
|
| 179 |
ierr = MPI_Win_allocate_shared( |
| 180 |
s, sizeof(Number), info, comm_shared, &data_this, &mpi_window); |
| 181 |
AssertThrowMPI(ierr); |
| 182 |
|
| 183 |
for (unsigned int i = 0; i < size_sm; ++i) |
| 184 |
{ |
| 185 |
int disp_unit; |
| 186 |
MPI_Aint ssize; |
| 187 |
const auto ierr = MPI_Win_shared_query( |
| 188 |
mpi_window, i, &ssize, &disp_unit, &others[i]); |
| 189 |
AssertThrowMPI(ierr); |
| 190 |
} |
| 191 |
|
| 192 |
Number *ptr_unaligned = others[rank_sm]; |
| 193 |
Number *ptr_aligned = ptr_unaligned; |
| 194 |
|
| 195 |
AssertThrow(std::align(align_by, |
| 196 |
new_alloc_size * sizeof(Number), |
| 197 |
reinterpret_cast<void *&>(ptr_aligned), |
| 198 |
s) != nullptr, |
| 199 |
ExcNotImplemented()); |
| 200 |
|
| 201 |
unsigned int n_align_local = ptr_aligned - ptr_unaligned; |
| 202 |
std::vector<unsigned int> n_align_sm(size_sm); |
| 203 |
|
| 204 |
ierr = MPI_Allgather(&n_align_local, |
| 205 |
1, |
| 206 |
MPI_UNSIGNED, |
| 207 |
n_align_sm.data(), |
| 208 |
1, |
| 209 |
MPI_UNSIGNED, |
| 210 |
comm_shared); |
| 211 |
AssertThrowMPI(ierr); |
| 212 |
|
| 213 |
for (unsigned int i = 0; i < size_sm; ++i) |
| 214 |
others[i] += n_align_sm[i]; |
| 215 |
|
| 216 |
std::vector<unsigned int> new_alloc_sizes(size_sm); |
| 217 |
|
| 218 |
ierr = MPI_Allgather(&new_alloc_size, |
| 219 |
1, |
| 220 |
MPI_UNSIGNED, |
| 221 |
new_alloc_sizes.data(), |
| 222 |
1, |
| 223 |
MPI_UNSIGNED, |
| 224 |
comm_shared); |
| 225 |
AssertThrowMPI(ierr); |
| 226 |
|
| 227 |
data.values_sm.resize(size_sm); |
| 228 |
for (unsigned int i = 0; i < size_sm; ++i) |
| 229 |
data.values_sm[i] = |
| 230 |
ArrayView<const Number>(others[i], new_alloc_sizes[i]); |
| 231 |
|
| 232 |
data.values = |
| 233 |
Kokkos::View<Number *, |
| 234 |
Kokkos::HostSpace, |
| 235 |
Kokkos::MemoryTraits<Kokkos::Unmanaged>>( |
| 236 |
ptr_aligned, new_alloc_size); |
| 237 |
|
| 238 |
// Kokkos will not free the memory because the memory is |
| 239 |
// unmanaged. Instead we use a shared pointer to take care of |
| 240 |
// that. |
| 241 |
data.values_sm_ptr = {ptr_aligned, |
| 242 |
[mpi_window](Number *) mutable { |
| 243 |
// note: we are creating here a copy of |
| 244 |
// the window other approaches led to |
| 245 |
// segmentation faults |
| 246 |
const auto ierr = |
| 247 |
MPI_Win_free(&mpi_window); |
| 248 |
AssertThrowMPI(ierr); |
| 249 |
}}; |
| 250 |
|
| 251 |
#else |
| 252 |
DEAL_II_ASSERT_UNREACHABLE(); |
| 253 |
#endif |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
static void |
| 258 |
import_elements( |
| 259 |
const ::dealii::LinearAlgebra::ReadWriteVector<Number> &V, |
| 260 |
VectorOperation::values operation, |
| 261 |
const std::shared_ptr<const ::dealii::Utilities::MPI::Partitioner> |
| 262 |
&communication_pattern, |
| 263 |
const IndexSet &locally_owned_elem, |
| 264 |
::dealii::MemorySpace::MemorySpaceData<Number, |
| 265 |
::dealii::MemorySpace::Host> |
| 266 |
&data) |
| 267 |
{ |
| 268 |
Assert( |
| 269 |
(operation == VectorOperation::add) || |
| 270 |
(operation == VectorOperation::insert), |
| 271 |
ExcMessage( |
| 272 |
"Only VectorOperation::add and VectorOperation::insert are allowed")); |
| 273 |
|
| 274 |
::dealii::LinearAlgebra::distributed:: |
| 275 |
Vector<Number, ::dealii::MemorySpace::Host> |
| 276 |
tmp_vector(communication_pattern); |
| 277 |
|
| 278 |
// fill entries from ReadWriteVector into the distributed vector, |
| 279 |
// including ghost entries. this is not really efficient right now |
| 280 |
// because indices are translated twice, once by nth_index_in_set(i) |
| 281 |
// and once for operator() of tmp_vector |
| 282 |
const IndexSet &v_stored = V.get_stored_elements(); |
| 283 |
for (size_type i = 0; i < v_stored.n_elements(); ++i) |
| 284 |
tmp_vector(v_stored.nth_index_in_set(i)) = V.local_element(i); |
| 285 |
|
| 286 |
tmp_vector.compress(operation); |
| 287 |
|
| 288 |
// Copy the local elements of tmp_vector to the right place in val |
| 289 |
IndexSet tmp_index_set = tmp_vector.locally_owned_elements(); |
| 290 |
if (operation == VectorOperation::add) |
| 291 |
{ |
| 292 |
for (size_type i = 0; i < tmp_index_set.n_elements(); ++i) |
| 293 |
{ |
| 294 |
data.values[locally_owned_elem.index_within_set( |
| 295 |
tmp_index_set.nth_index_in_set(i))] += |
| 296 |
tmp_vector.local_element(i); |
| 297 |
} |
| 298 |
} |
| 299 |
else |
| 300 |
{ |
| 301 |
for (size_type i = 0; i < tmp_index_set.n_elements(); ++i) |
| 302 |
{ |
| 303 |
data.values[locally_owned_elem.index_within_set( |
| 304 |
tmp_index_set.nth_index_in_set(i))] = |
| 305 |
tmp_vector.local_element(i); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
template <typename RealType> |
| 311 |
static void |
| 312 |
linfty_norm_local(const ::dealii::MemorySpace::MemorySpaceData< |
| 313 |
Number, |
| 314 |
::dealii::MemorySpace::Host> &data, |
| 315 |
const unsigned int size, |
| 316 |
RealType &max) |
| 317 |
{ |
| 318 |
for (size_type i = 0; i < size; ++i) |
| 319 |
max = |
| 320 |
std::max(numbers::NumberTraits<Number>::abs(data.values[i]), max); |
| 321 |
} |
| 322 |
}; |
| 323 |
|
| 324 |
template <typename Number> |
| 325 |
struct la_parallel_vector_templates_functions< |
| 326 |
Number, |
| 327 |
::dealii::MemorySpace::Default> |
| 328 |
{ |
| 329 |
using size_type = types::global_dof_index; |
| 330 |
|
| 331 |
static void |
| 332 |
resize_val( |
| 333 |
const types::global_dof_index new_alloc_size, |
| 334 |
types::global_dof_index &allocated_size, |
| 335 |
::dealii::MemorySpace::MemorySpaceData<Number, |
| 336 |
::dealii::MemorySpace::Default> |
| 337 |
&data, |
| 338 |
const MPI_Comm /*comm_sm*/) |
| 339 |
{ |
| 340 |
static_assert( |
| 341 |
std::is_same_v<Number, float> || std::is_same_v<Number, double>, |
| 342 |
"Number should be float or double for Default memory space"); |
| 343 |
|
| 344 |
if (new_alloc_size > allocated_size) |
| 345 |
{ |
| 346 |
Assert(((allocated_size > 0 && data.values.size() != 0) || |
| 347 |
data.values.size() == 0), |
| 348 |
ExcInternalError()); |
| 349 |
|
| 350 |
#if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0) |
| 351 |
Kokkos::resize(Kokkos::WithoutInitializing, |
| 352 |
data.values, |
| 353 |
new_alloc_size); |
| 354 |
#else |
| 355 |
Kokkos::resize(data.values, new_alloc_size); |
| 356 |
#endif |
| 357 |
|
| 358 |
allocated_size = new_alloc_size; |
| 359 |
} |
| 360 |
else if (new_alloc_size == 0) |
| 361 |
{ |
| 362 |
Kokkos::resize(data.values, 0); |
| 363 |
allocated_size = 0; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
static void |
| 368 |
import_elements( |
| 369 |
const ReadWriteVector<Number> &V, |
| 370 |
VectorOperation::values operation, |
| 371 |
std::shared_ptr<const Utilities::MPI::Partitioner> |
| 372 |
&communication_pattern, |
| 373 |
const IndexSet &locally_owned_elem, |
| 374 |
::dealii::MemorySpace::MemorySpaceData<Number, |
| 375 |
::dealii::MemorySpace::Default> |
| 376 |
&data) |
| 377 |
{ |
| 378 |
Assert( |
| 379 |
(operation == VectorOperation::add) || |
| 380 |
(operation == VectorOperation::insert), |
| 381 |
ExcMessage( |
| 382 |
"Only VectorOperation::add and VectorOperation::insert are allowed")); |
| 383 |
|
| 384 |
::dealii::LinearAlgebra::distributed:: |
| 385 |
Vector<Number, ::dealii::MemorySpace::Default> |
| 386 |
tmp_vector(communication_pattern); |
| 387 |
|
| 388 |
// fill entries from ReadWriteVector into the distributed vector, |
| 389 |
// including ghost entries. this is not really efficient right now |
| 390 |
// because indices are translated twice, once by nth_index_in_set(i) |
| 391 |
// and once for operator() of tmp_vector |
| 392 |
const IndexSet &v_stored = V.get_stored_elements(); |
| 393 |
const size_type n_elements = v_stored.n_elements(); |
| 394 |
Kokkos::DefaultHostExecutionSpace host_exec; |
| 395 |
Kokkos::View<size_type *, Kokkos::HostSpace> indices( |
| 396 |
Kokkos::view_alloc(Kokkos::WithoutInitializing, "indices"), |
| 397 |
n_elements); |
| 398 |
Kokkos::parallel_for( |
| 399 |
"dealii::import_elements: fill indices", |
| 400 |
Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(host_exec, |
| 401 |
0, |
| 402 |
n_elements), |
| 403 |
KOKKOS_LAMBDA(size_type i) { |
| 404 |
indices[i] = communication_pattern->global_to_local( |
| 405 |
v_stored.nth_index_in_set(i)); |
| 406 |
}); |
| 407 |
host_exec.fence(); |
| 408 |
|
| 409 |
// Move the indices to the device |
| 410 |
::dealii::MemorySpace::Default::kokkos_space::execution_space exec; |
| 411 |
auto indices_dev = Kokkos::create_mirror_view_and_copy( |
| 412 |
::dealii::MemorySpace::Default::kokkos_space{}, indices); |
| 413 |
|
| 414 |
// Move the data to the device |
| 415 |
Kokkos::View<Number *, Kokkos::HostSpace> V_view(V.begin(), |
| 416 |
n_elements); |
| 417 |
auto V_dev = Kokkos::create_mirror_view_and_copy( |
| 418 |
::dealii::MemorySpace::Default::kokkos_space{}, V_view); |
| 419 |
|
| 420 |
// Set the values in tmp_vector |
| 421 |
Kokkos::parallel_for( |
| 422 |
"dealii::import_elements: set values tmp_vector", |
| 423 |
Kokkos::RangePolicy< |
| 424 |
::dealii::MemorySpace::Default::kokkos_space::execution_space>( |
| 425 |
exec, 0, n_elements), |
| 426 |
KOKKOS_LAMBDA(size_type i) { |
| 427 |
tmp_vector(indices_dev(i)) = V_dev(i); |
| 428 |
}); |
| 429 |
exec.fence(); |
| 430 |
|
| 431 |
tmp_vector.compress(operation); |
| 432 |
|
| 433 |
// Copy the local elements of tmp_vector to the right place in val |
| 434 |
IndexSet tmp_index_set = tmp_vector.locally_owned_elements(); |
| 435 |
const size_type tmp_n_elements = tmp_index_set.n_elements(); |
| 436 |
Kokkos::realloc(indices, tmp_n_elements); |
| 437 |
Kokkos::parallel_for( |
| 438 |
"dealii::import_elements: copy local elements to val", |
| 439 |
Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(host_exec, |
| 440 |
0, |
| 441 |
n_elements), |
| 442 |
KOKKOS_LAMBDA(size_type i) { |
| 443 |
indices[i] = locally_owned_elem.index_within_set( |
| 444 |
tmp_index_set.nth_index_in_set(i)); |
| 445 |
}); |
| 446 |
host_exec.fence(); |
| 447 |
Kokkos::realloc(indices_dev, tmp_n_elements); |
| 448 |
Kokkos::deep_copy(indices_dev, |
| 449 |
Kokkos::subview(indices, |
| 450 |
Kokkos::make_pair(size_type(0), |
| 451 |
tmp_n_elements))); |
| 452 |
|
| 453 |
if (operation == VectorOperation::add) |
| 454 |
Kokkos::parallel_for( |
| 455 |
"dealii::import_elements: add values", |
| 456 |
Kokkos::RangePolicy< |
| 457 |
::dealii::MemorySpace::Default::kokkos_space::execution_space>( |
| 458 |
exec, 0, n_elements), |
| 459 |
KOKKOS_LAMBDA(size_type i) { |
| 460 |
data.values(indices_dev(i)) += tmp_vector(i); |
| 461 |
}); |
| 462 |
else |
| 463 |
Kokkos::parallel_for( |
| 464 |
"dealii::import_elements: set values", |
| 465 |
Kokkos::RangePolicy< |
| 466 |
::dealii::MemorySpace::Default::kokkos_space::execution_space>( |
| 467 |
exec, 0, n_elements), |
| 468 |
KOKKOS_LAMBDA(size_type i) { |
| 469 |
data.values(indices_dev(i)) = tmp_vector(i); |
| 470 |
}); |
| 471 |
exec.fence(); |
| 472 |
} |
| 473 |
|
| 474 |
template <typename RealType> |
| 475 |
static void |
| 476 |
linfty_norm_local(const ::dealii::MemorySpace::MemorySpaceData< |
| 477 |
Number, |
| 478 |
::dealii::MemorySpace::Default> &data, |
| 479 |
const unsigned int size, |
| 480 |
RealType &result) |
| 481 |
{ |
| 482 |
static_assert(std::is_same_v<Number, RealType>, |
| 483 |
"RealType should be the same type as Number"); |
| 484 |
|
| 485 |
typename ::dealii::MemorySpace::Default::kokkos_space::execution_space |
| 486 |
exec; |
| 487 |
Kokkos::parallel_reduce( |
| 488 |
"dealii::linfty_norm_local", |
| 489 |
Kokkos::RangePolicy< |
| 490 |
::dealii::MemorySpace::Default::kokkos_space::execution_space>( |
| 491 |
exec, 0, size), |
| 492 |
KOKKOS_LAMBDA(size_type i, RealType & update) { |
| 493 |
#if DEAL_II_KOKKOS_VERSION_GTE(3, 7, 0) |
| 494 |
update = Kokkos::fmax(update, Kokkos::abs(data.values(i))); |
| 495 |
#else |
| 496 |
update = Kokkos::Experimental::fmax( |
| 497 |
update, Kokkos::Experimental::fabs(data.values(i))); |
| 498 |
#endif |
| 499 |
}, |
| 500 |
Kokkos::Max<RealType, Kokkos::HostSpace>(result)); |
| 501 |
} |
| 502 |
}; |
| 503 |
} // namespace internal |
| 504 |
|
| 505 |
|
| 506 |
template <typename Number, typename MemorySpaceType> |
| 507 |
void |
| 508 |
Vector<Number, MemorySpaceType>::clear_mpi_requests() |
| 509 |
{ |
| 510 |
#ifdef DEAL_II_WITH_MPI |
| 511 |
for (auto &compress_request : compress_requests) |
| 512 |
{ |
| 513 |
const int ierr = MPI_Request_free(&compress_request); |
| 514 |
AssertThrowMPI(ierr); |
| 515 |
} |
| 516 |
compress_requests.clear(); |
| 517 |
for (auto &update_ghost_values_request : update_ghost_values_requests) |
| 518 |
{ |
| 519 |
const int ierr = MPI_Request_free(&update_ghost_values_request); |
| 520 |
AssertThrowMPI(ierr); |
| 521 |
} |
| 522 |
update_ghost_values_requests.clear(); |
| 523 |
#endif |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
template <typename Number, typename MemorySpaceType> |
| 529 |
void |
| 530 |
Vector<Number, MemorySpaceType>::resize_val(const size_type new_alloc_size, |
| 531 |
const MPI_Comm comm_sm) |
| 532 |
{ |
| 533 |
internal::la_parallel_vector_templates_functions< |
| 534 |
Number, |
| 535 |
MemorySpaceType>::resize_val(new_alloc_size, |
| 536 |
allocated_size, |
| 537 |
data, |
| 538 |
comm_sm); |
| 539 |
|
| 540 |
thread_loop_partitioner = |
| 541 |
std::make_shared<::dealii::parallel::internal::TBBPartitioner>(); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
template <typename Number, typename MemorySpaceType> |
| 547 |
void |
| 548 |
Vector<Number, MemorySpaceType>::reinit(const size_type size, |
| 549 |
const bool omit_zeroing_entries) |
| 550 |
{ |
| 551 |
clear_mpi_requests(); |
| 552 |
|
| 553 |
// check whether we need to reallocate |
| 554 |
resize_val(size, comm_sm); |
| 555 |
|
| 556 |
// delete previous content in import data |
| 557 |
Kokkos::resize(import_data.values_host_buffer, 0); |
| 558 |
Kokkos::resize(import_data.values, 0); |
| 559 |
|
| 560 |
// set partitioner to serial version |
| 561 |
partitioner = std::make_shared<Utilities::MPI::Partitioner>(size); |
| 562 |
|
| 563 |
// set entries to zero if so requested |
| 564 |
if (omit_zeroing_entries == false) |
| 565 |
this->operator=(Number()); |
| 566 |
else |
| 567 |
zero_out_ghost_values(); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
template <typename Number, typename MemorySpaceType> |
| 573 |
void |
| 574 |
Vector<Number, MemorySpaceType>::reinit( |
| 575 |
const types::global_dof_index local_size, |
| 576 |
const types::global_dof_index ghost_size, |
| 577 |
const MPI_Comm comm, |
| 578 |
const MPI_Comm comm_sm) |
| 579 |
{ |
| 580 |
clear_mpi_requests(); |
| 581 |
|
| 582 |
this->comm_sm = comm_sm; |
| 583 |
|
| 584 |
// check whether we need to reallocate |
| 585 |
resize_val(local_size + ghost_size, comm_sm); |
| 586 |
|
| 587 |
// delete previous content in import data |
| 588 |
Kokkos::resize(import_data.values_host_buffer, 0); |
| 589 |
Kokkos::resize(import_data.values, 0); |
| 590 |
|
| 591 |
// create partitioner |
| 592 |
partitioner = std::make_shared<Utilities::MPI::Partitioner>(local_size, |
| 593 |
ghost_size, |
| 594 |
comm); |
| 595 |
|
| 596 |
this->operator=(Number()); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
template <typename Number, typename MemorySpaceType> |
| 602 |
template <typename Number2> |
| 603 |
void |
| 604 |
Vector<Number, MemorySpaceType>::reinit( |
| 605 |
const Vector<Number2, MemorySpaceType> &v, |
| 606 |
const bool omit_zeroing_entries) |
| 607 |
{ |
| 608 |
clear_mpi_requests(); |
| 609 |
Assert(v.partitioner.get() != nullptr, ExcNotInitialized()); |
| 610 |
|
| 611 |
this->comm_sm = v.comm_sm; |
| 612 |
|
| 613 |
// check whether the partitioners are |
| 614 |
// different (check only if the are allocated |
| 615 |
// differently, not if the actual data is |
| 616 |
// different) |
| 617 |
if (partitioner.get() != v.partitioner.get()) |
| 618 |
{ |
| 619 |
partitioner = v.partitioner; |
| 620 |
const size_type new_allocated_size = |
| 621 |
partitioner->locally_owned_size() + partitioner->n_ghost_indices(); |
| 622 |
resize_val(new_allocated_size, this->comm_sm); |
| 623 |
} |
| 624 |
|
| 625 |
if (omit_zeroing_entries == false) |
| 626 |
this->operator=(Number()); |
| 627 |
else |
| 628 |
zero_out_ghost_values(); |
| 629 |
|
| 630 |
// do not reallocate import_data directly, but only upon request. It |
| 631 |
// is only used as temporary storage for compress() and |
| 632 |
// update_ghost_values, and we might have vectors where we never |
| 633 |
// call these methods and hence do not need to have the storage. |
| 634 |
Kokkos::resize(import_data.values_host_buffer, 0); |
| 635 |
Kokkos::resize(import_data.values, 0); |
| 636 |
|
| 637 |
thread_loop_partitioner = v.thread_loop_partitioner; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
template <typename Number, typename MemorySpaceType> |
| 643 |
void |
| 644 |
Vector<Number, MemorySpaceType>::reinit( |
| 645 |
const IndexSet &locally_owned_indices, |
| 646 |
const IndexSet &ghost_indices, |
| 647 |
const MPI_Comm communicator) |
| 648 |
{ |
| 649 |
// set up parallel partitioner with index sets and communicator |
| 650 |
reinit(std::make_shared<Utilities::MPI::Partitioner>( |
| 651 |
locally_owned_indices, ghost_indices, communicator)); |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
template <typename Number, typename MemorySpaceType> |
| 657 |
void |
| 658 |
Vector<Number, MemorySpaceType>::reinit( |
| 659 |
const IndexSet &locally_owned_indices, |
| 660 |
const MPI_Comm communicator) |
| 661 |
{ |
| 662 |
// set up parallel partitioner with index sets and communicator |
| 663 |
reinit( |
| 664 |
std::make_shared<Utilities::MPI::Partitioner>(locally_owned_indices, |
| 665 |
communicator)); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
template <typename Number, typename MemorySpaceType> |
| 671 |
void |
| 672 |
Vector<Number, MemorySpaceType>::reinit( |
| 673 |
const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner_in, |
| 674 |
const MPI_Comm comm_sm) |
| 675 |
{ |
| 676 |
clear_mpi_requests(); |
| 677 |
|
| 678 |
this->comm_sm = comm_sm; |
| 679 |
|
| 680 |
// set vector size and allocate memory |
| 681 |
if (partitioner.get() != partitioner_in.get()) |
| 682 |
{ |
| 683 |
partitioner = partitioner_in; |
| 684 |
const size_type new_allocated_size = |
| 685 |
partitioner->locally_owned_size() + partitioner->n_ghost_indices(); |
| 686 |
resize_val(new_allocated_size, comm_sm); |
| 687 |
} |
| 688 |
|
| 689 |
// initialize to zero |
| 690 |
*this = Number(); |
| 691 |
|
| 692 |
|
| 693 |
// do not reallocate import_data directly, but only upon request. It |
| 694 |
// is only used as temporary storage for compress() and |
| 695 |
// update_ghost_values, and we might have vectors where we never |
| 696 |
// call these methods and hence do not need to have the storage. |
| 697 |
Kokkos::resize(import_data.values_host_buffer, 0); |
| 698 |
Kokkos::resize(import_data.values, 0); |
| 699 |
|
| 700 |
vector_is_ghosted = false; |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
template <typename Number, typename MemorySpaceType> |
| 706 |
void |
| 707 |
Vector<Number, MemorySpaceType>::reinit( |
| 708 |
const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner_in, |
| 709 |
const bool /*make_ghosted*/, |
| 710 |
const MPI_Comm &comm_sm) |
| 711 |
{ |
| 712 |
this->reinit(partitioner_in, comm_sm); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
template <typename Number, typename MemorySpaceType> |
| 718 |
Vector<Number, MemorySpaceType>::Vector() |
| 719 |
: partitioner(std::make_shared<Utilities::MPI::Partitioner>()) |
| 720 |
, allocated_size(0) |
| 721 |
, comm_sm(MPI_COMM_SELF) |
| 722 |
{ |
| 723 |
reinit(0); |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
template <typename Number, typename MemorySpaceType> |
| 729 |
Vector<Number, MemorySpaceType>::Vector( |
| 730 |
const Vector<Number, MemorySpaceType> &v) |
| 731 |
: allocated_size(0) |
| 732 |
, vector_is_ghosted(false) |
| 733 |
, comm_sm(MPI_COMM_SELF) |
| 734 |
{ |
| 735 |
reinit(v, true); |
| 736 |
|
| 737 |
thread_loop_partitioner = v.thread_loop_partitioner; |
| 738 |
|
| 739 |
copy_locally_owned_data_from(v); |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
template <typename Number, typename MemorySpaceType> |
| 745 |
Vector<Number, MemorySpaceType>::Vector( // NOLINT |
| 746 |
Vector<Number, MemorySpaceType> &&v) |
| 747 |
: Vector() |
| 748 |
{ |
| 749 |
static_cast<EnableObserverPointer &>(*this) = |
| 750 |
static_cast<EnableObserverPointer &&>(v); |
| 751 |
this->swap(v); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
template <typename Number, typename MemorySpaceType> |
| 757 |
Vector<Number, MemorySpaceType>::Vector(const IndexSet &local_range, |
| 758 |
const IndexSet &ghost_indices, |
| 759 |
const MPI_Comm communicator) |
| 760 |
: allocated_size(0) |
| 761 |
, vector_is_ghosted(false) |
| 762 |
, comm_sm(MPI_COMM_SELF) |
| 763 |
{ |
| 764 |
reinit(local_range, ghost_indices, communicator); |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
template <typename Number, typename MemorySpaceType> |
| 770 |
Vector<Number, MemorySpaceType>::Vector(const IndexSet &local_range, |
| 771 |
const MPI_Comm communicator) |
| 772 |
: allocated_size(0) |
| 773 |
, vector_is_ghosted(false) |
| 774 |
, comm_sm(MPI_COMM_SELF) |
| 775 |
{ |
| 776 |
reinit(local_range, communicator); |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
template <typename Number, typename MemorySpaceType> |
| 782 |
Vector<Number, MemorySpaceType>::Vector(const size_type size) |
| 783 |
: allocated_size(0) |
| 784 |
, vector_is_ghosted(false) |
| 785 |
, comm_sm(MPI_COMM_SELF) |
| 786 |
{ |
| 787 |
reinit(size, false); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
template <typename Number, typename MemorySpaceType> |
| 793 |
Vector<Number, MemorySpaceType>::Vector( |
| 794 |
const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner) |
| 795 |
: allocated_size(0) |
| 796 |
, vector_is_ghosted(false) |
| 797 |
, comm_sm(MPI_COMM_SELF) |
| 798 |
{ |
| 799 |
reinit(partitioner); |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
template <typename Number, typename MemorySpaceType> |
| 805 |
inline Vector<Number, MemorySpaceType>::~Vector() |
| 806 |
{ |
| 807 |
try |
| 808 |
{ |
| 809 |
clear_mpi_requests(); |
| 810 |
} |
| 811 |
catch (...) |
| 812 |
{} |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
template <typename Number, typename MemorySpaceType> |
| 818 |
inline Vector<Number, MemorySpaceType> & |
| 819 |
Vector<Number, MemorySpaceType>::operator=( |
| 820 |
const Vector<Number, MemorySpaceType> &c) |
| 821 |
{ |
| 822 |
#ifdef _MSC_VER |
| 823 |
return this->operator=<Number>(c); |
| 824 |
#else |
| 825 |
return this->template operator=<Number>(c); |
| 826 |
#endif |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
template <typename Number, typename MemorySpaceType> |
| 832 |
template <typename Number2> |
| 833 |
inline Vector<Number, MemorySpaceType> & |
| 834 |
Vector<Number, MemorySpaceType>::operator=( |
| 835 |
const Vector<Number2, MemorySpaceType> &c) |
| 836 |
{ |
| 837 |
Assert(c.partitioner.get() != nullptr, ExcNotInitialized()); |
| 838 |
|
| 839 |
// we update ghost values whenever one of the input or output vector |
| 840 |
// already held ghost values or when we import data from a vector with |
| 841 |
// the same local range but different ghost layout |
| 842 |
bool must_update_ghost_values = c.vector_is_ghosted; |
| 843 |
|
| 844 |
this->comm_sm = c.comm_sm; |
| 845 |
|
| 846 |
// check whether the two vectors use the same parallel partitioner. if |
| 847 |
// not, check if all local ranges are the same (that way, we can |
| 848 |
// exchange data between different parallel layouts). One variant which |
| 849 |
// is included here and necessary for compatibility with the other |
| 850 |
// distributed vector classes (Trilinos, PETSc) is the case when vector |
| 851 |
// c does not have any ghosts (constructed without ghost elements given) |
| 852 |
// but the current vector does: In that case, we need to exchange data |
| 853 |
// also when none of the two vector had updated its ghost values before. |
| 854 |
if (partitioner.get() == nullptr) |
| 855 |
reinit(c, true); |
| 856 |
else if (partitioner.get() != c.partitioner.get()) |
| 857 |
{ |
| 858 |
// local ranges are also the same if both partitioners are empty |
| 859 |
// (even if they happen to define the empty range as [0,0) or [c,c) |
| 860 |
// for some c!=0 in a different way). |
| 861 |
int local_ranges_are_identical = |
| 862 |
(partitioner->local_range() == c.partitioner->local_range() || |
| 863 |
(partitioner->local_range().second == |
| 864 |
partitioner->local_range().first && |
| 865 |
c.partitioner->local_range().second == |
| 866 |
c.partitioner->local_range().first)); |
| 867 |
if ((c.partitioner->n_mpi_processes() > 1 && |
| 868 |
Utilities::MPI::min(local_ranges_are_identical, |
| 869 |
c.partitioner->get_mpi_communicator()) == |
| 870 |
0) || |
| 871 |
!local_ranges_are_identical) |
| 872 |
reinit(c, true); |
| 873 |
else |
| 874 |
must_update_ghost_values |= vector_is_ghosted; |
| 875 |
|
| 876 |
must_update_ghost_values |= |
| 877 |
(c.partitioner->ghost_indices_initialized() == false && |
| 878 |
partitioner->ghost_indices_initialized() == true); |
| 879 |
} |
| 880 |
else |
| 881 |
must_update_ghost_values |= vector_is_ghosted; |
| 882 |
|
| 883 |
thread_loop_partitioner = c.thread_loop_partitioner; |
| 884 |
|
| 885 |
copy_locally_owned_data_from(c); |
| 886 |
|
| 887 |
if (must_update_ghost_values) |
| 888 |
update_ghost_values(); |
| 889 |
else |
| 890 |
zero_out_ghost_values(); |
| 891 |
return *this; |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
template <typename Number, typename MemorySpaceType> |
| 897 |
template <typename Number2> |
| 898 |
void |
| 899 |
Vector<Number, MemorySpaceType>::copy_locally_owned_data_from( |
| 900 |
const Vector<Number2, MemorySpaceType> &src) |
| 901 |
{ |
| 902 |
AssertDimension(partitioner->locally_owned_size(), |
| 903 |
src.partitioner->locally_owned_size()); |
| 904 |
if (partitioner->locally_owned_size() > 0) |
| 905 |
{ |
| 906 |
dealii::internal::VectorOperations:: |
| 907 |
functions<Number, Number2, MemorySpaceType>::copy( |
| 908 |
thread_loop_partitioner, |
| 909 |
partitioner->locally_owned_size(), |
| 910 |
src.data, |
| 911 |
data); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
template <typename Number, typename MemorySpaceType> |
| 918 |
template <typename MemorySpaceType2> |
| 919 |
void |
| 920 |
Vector<Number, MemorySpaceType>::import_elements( |
| 921 |
const Vector<Number, MemorySpaceType2> &src, |
| 922 |
const VectorOperation::values operation) |
| 923 |
{ |
| 924 |
Assert(src.partitioner.get() != nullptr, ExcNotInitialized()); |
| 925 |
Assert(partitioner->locally_owned_range() == |
| 926 |
src.partitioner->locally_owned_range(), |
| 927 |
ExcMessage("Locally owned indices should be identical.")); |
| 928 |
Assert(partitioner->ghost_indices() == src.partitioner->ghost_indices(), |
| 929 |
ExcMessage("Ghost indices should be identical.")); |
| 930 |
::dealii::internal::VectorOperations:: |
| 931 |
functions<Number, Number, MemorySpaceType>::import_elements( |
| 932 |
thread_loop_partitioner, allocated_size, operation, src.data, data); |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
template <typename Number, typename MemorySpaceType> |
| 938 |
void |
| 939 |
Vector<Number, MemorySpaceType>::compress(VectorOperation::values operation) |
| 940 |
{ |
| 941 |
compress_start(0, operation); |
| 942 |
compress_finish(operation); |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
template <typename Number, typename MemorySpaceType> |
| 948 |
void |
| 949 |
Vector<Number, MemorySpaceType>::update_ghost_values() const |
| 950 |
{ |
| 951 |
update_ghost_values_start(); |
| 952 |
update_ghost_values_finish(); |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
template <typename Number, typename MemorySpaceType> |
| 958 |
void |
| 959 |
Vector<Number, MemorySpaceType>::zero_out_ghost_values() const |
| 960 |
{ |
| 961 |
Kokkos::pair<size_type, size_type> range( |
| 962 |
partitioner->locally_owned_size(), |
| 963 |
partitioner->locally_owned_size() + partitioner->n_ghost_indices()); |
| 964 |
if (data.values.size() > 0) |
| 965 |
Kokkos::deep_copy(Kokkos::subview(data.values, range), 0); |
| 966 |
|
| 967 |
vector_is_ghosted = false; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
template <typename Number, typename MemorySpaceType> |
| 973 |
void |
| 974 |
Vector<Number, MemorySpaceType>::compress_start( |
| 975 |
const unsigned int communication_channel, |
| 976 |
VectorOperation::values operation) |
| 977 |
{ |
| 978 |
AssertIndexRange(communication_channel, 200); |
| 979 |
Assert(vector_is_ghosted == false, |
| 980 |
ExcMessage("Cannot call compress() on a ghosted vector")); |
| 981 |
|
| 982 |
#ifdef DEAL_II_WITH_MPI |
| 983 |
// make this function thread safe |
| 984 |
std::lock_guard<std::mutex> lock(mutex); |
| 985 |
|
| 986 |
// allocate import_data in case it is not set up yet |
| 987 |
if (partitioner->n_import_indices() > 0) |
| 988 |
{ |
| 989 |
# if !defined(DEAL_II_MPI_WITH_DEVICE_SUPPORT) |
| 990 |
if (std::is_same_v<MemorySpaceType, dealii::MemorySpace::Default>) |
| 991 |
{ |
| 992 |
if (import_data.values_host_buffer.size() == 0) |
| 993 |
# if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0) |
| 994 |
Kokkos::resize(Kokkos::WithoutInitializing, |
| 995 |
import_data.values_host_buffer, |
| 996 |
partitioner->n_import_indices()); |
| 997 |
# else |
| 998 |
Kokkos::resize(import_data.values_host_buffer, |
| 999 |
partitioner->n_import_indices()); |
| 1000 |
# endif |
| 1001 |
} |
| 1002 |
else |
| 1003 |
# endif |
| 1004 |
{ |
| 1005 |
if (import_data.values.size() == 0) |
| 1006 |
# if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0) |
| 1007 |
Kokkos::resize(Kokkos::WithoutInitializing, |
| 1008 |
import_data.values, |
| 1009 |
partitioner->n_import_indices()); |
| 1010 |
# else |
| 1011 |
Kokkos::resize(import_data.values, |
| 1012 |
partitioner->n_import_indices()); |
| 1013 |
# endif |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
# if !defined(DEAL_II_MPI_WITH_DEVICE_SUPPORT) |
| 1018 |
if (std::is_same_v<MemorySpaceType, dealii::MemorySpace::Default>) |
| 1019 |
{ |
| 1020 |
// Move the data to the host and then move it back to the |
| 1021 |
// device. We use values to store the elements because the function |
| 1022 |
// uses a view of the array and thus we need the data on the host to |
| 1023 |
// outlive the scope of the function. |
| 1024 |
data.values_host_buffer = |
| 1025 |
# if DEAL_II_KOKKOS_VERSION_GTE(4, 0, 0) |
| 1026 |
Kokkos::create_mirror_view_and_copy(Kokkos::SharedHostPinnedSpace{}, |
| 1027 |
data.values); |
| 1028 |
# else |
| 1029 |
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, |
| 1030 |
data.values); |
| 1031 |
# endif |
| 1032 |
partitioner->import_from_ghosted_array_start( |
| 1033 |
operation, |
| 1034 |
communication_channel, |
| 1035 |
ArrayView<Number, MemorySpace::Host>( |
| 1036 |
data.values_host_buffer.data() + |
| 1037 |
partitioner->locally_owned_size(), |
| 1038 |
partitioner->n_ghost_indices()), |
| 1039 |
ArrayView<Number, MemorySpace::Host>( |
| 1040 |
import_data.values_host_buffer.data(), |
| 1041 |
partitioner->n_import_indices()), |
| 1042 |
compress_requests); |
| 1043 |
} |
| 1044 |
else |
| 1045 |
# endif |
| 1046 |
{ |
| 1047 |
partitioner->import_from_ghosted_array_start( |
| 1048 |
operation, |
| 1049 |
communication_channel, |
| 1050 |
ArrayView<Number, MemorySpaceType>( |
| 1051 |
data.values.data() + partitioner->locally_owned_size(), |
| 1052 |
partitioner->n_ghost_indices()), |
| 1053 |
ArrayView<Number, MemorySpaceType>(import_data.values.data(), |
| 1054 |
partitioner->n_import_indices()), |
| 1055 |
compress_requests); |
| 1056 |
} |
| 1057 |
#else |
| 1058 |
(void)communication_channel; |
| 1059 |
(void)operation; |
| 1060 |
#endif |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
template <typename Number, typename MemorySpaceType> |
| 1066 |
void |
| 1067 |
Vector<Number, MemorySpaceType>::compress_finish( |
| 1068 |
VectorOperation::values operation) |
| 1069 |
{ |
| 1070 |
#ifdef DEAL_II_WITH_MPI |
| 1071 |
vector_is_ghosted = false; |
| 1072 |
|
| 1073 |
// in order to zero ghost part of the vector, we need to call |
| 1074 |
// import_from_ghosted_array_finish() regardless of |
| 1075 |
// compress_requests.empty() |
| 1076 |
|
| 1077 |
// make this function thread safe |
| 1078 |
std::lock_guard<std::mutex> lock(mutex); |
| 1079 |
# if !defined(DEAL_II_MPI_WITH_DEVICE_SUPPORT) |
| 1080 |
if (std::is_same_v<MemorySpaceType, MemorySpace::Default>) |
| 1081 |
{ |
| 1082 |
Assert(partitioner->n_import_indices() == 0 || |
| 1083 |
import_data.values_host_buffer.size() != 0, |
| 1084 |
ExcNotInitialized()); |
| 1085 |
partitioner |
| 1086 |
->import_from_ghosted_array_finish<Number, MemorySpace::Host>( |
| 1087 |
operation, |
| 1088 |
ArrayView<const Number, MemorySpace::Host>( |
| 1089 |
import_data.values_host_buffer.data(), |
| 1090 |
partitioner->n_import_indices()), |
| 1091 |
ArrayView<Number, MemorySpace::Host>( |
| 1092 |
data.values_host_buffer.data(), |
| 1093 |
partitioner->locally_owned_size()), |
| 1094 |
ArrayView<Number, MemorySpace::Host>( |
| 1095 |
data.values_host_buffer.data() + |
| 1096 |
partitioner->locally_owned_size(), |
| 1097 |
partitioner->n_ghost_indices()), |
| 1098 |
compress_requests); |
| 1099 |
|
| 1100 |
// The communication is done on the host, so we need to |
| 1101 |
// move the data back to the device. |
| 1102 |
Kokkos::deep_copy(data.values, data.values_host_buffer); |
| 1103 |
|
| 1104 |
Kokkos::resize(data.values_host_buffer, 0); |
| 1105 |
} |
| 1106 |
else |
| 1107 |
# endif |
| 1108 |
{ |
| 1109 |
Assert(partitioner->n_import_indices() == 0 || |
| 1110 |
import_data.values.size() != 0, |
| 1111 |
ExcNotInitialized()); |
| 1112 |
partitioner |
| 1113 |
->import_from_ghosted_array_finish<Number, MemorySpaceType>( |
| 1114 |
operation, |
| 1115 |
ArrayView<const Number, MemorySpaceType>( |
| 1116 |
import_data.values.data(), partitioner->n_import_indices()), |
| 1117 |
ArrayView<Number, MemorySpaceType>( |
| 1118 |
data.values.data(), partitioner->locally_owned_size()), |
| 1119 |
ArrayView<Number, MemorySpaceType>( |
| 1120 |
data.values.data() + partitioner->locally_owned_size(), |
| 1121 |
partitioner->n_ghost_indices()), |
| 1122 |
compress_requests); |
| 1123 |
} |
| 1124 |
#else |
| 1125 |
(void)operation; |
| 1126 |
#endif |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
template <typename Number, typename MemorySpaceType> |
| 1132 |
void |
| 1133 |
Vector<Number, MemorySpaceType>::update_ghost_values_start( |
| 1134 |
const unsigned int communication_channel) const |
| 1135 |
{ |
| 1136 |
AssertIndexRange(communication_channel, 200); |
| 1137 |
#ifdef DEAL_II_WITH_MPI |
| 1138 |
// nothing to do when we neither have import nor ghost indices. |
| 1139 |
if (partitioner->n_ghost_indices() == 0 && |
| 1140 |
partitioner->n_import_indices() == 0) |
| 1141 |
return; |
| 1142 |
|
| 1143 |
// make this function thread safe |
| 1144 |
std::lock_guard<std::mutex> lock(mutex); |
| 1145 |
|
| 1146 |
// allocate import_data in case it is not set up yet |
| 1147 |
if (partitioner->n_import_indices() > 0) |
| 1148 |
{ |
| 1149 |
# if !defined(DEAL_II_MPI_WITH_DEVICE_SUPPORT) |
| 1150 |
if (std::is_same_v<MemorySpaceType, MemorySpace::Default>) |
| 1151 |
{ |
| 1152 |
if (import_data.values_host_buffer.size() == 0) |
| 1153 |
# if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0) |
| 1154 |
Kokkos::resize(Kokkos::WithoutInitializing, |
| 1155 |
import_data.values_host_buffer, |
| 1156 |
partitioner->n_import_indices()); |
| 1157 |
# else |
| 1158 |
Kokkos::resize(import_data.values_host_buffer, |
| 1159 |
partitioner->n_import_indices()); |
| 1160 |
# endif |
| 1161 |
} |
| 1162 |
else |
| 1163 |
# endif |
| 1164 |
{ |
| 1165 |
if (import_data.values.size() == 0) |
| 1166 |
# if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0) |
| 1167 |
Kokkos::resize(Kokkos::WithoutInitializing, |
| 1168 |
import_data.values, |
| 1169 |
partitioner->n_import_indices()); |
| 1170 |
# else |
| 1171 |
Kokkos::resize(import_data.values, |
| 1172 |
partitioner->n_import_indices()); |
| 1173 |
# endif |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
# if !defined(DEAL_II_MPI_WITH_DEVICE_SUPPORT) |
| 1178 |
if (std::is_same_v<MemorySpaceType, MemorySpace::Default>) |
| 1179 |
{ |
| 1180 |
// Move the data to the host and then move it back to the |
| 1181 |
// device. We use values to store the elements because the function |
| 1182 |
// uses a view of the array and thus we need the data on the host to |
| 1183 |
// outlive the scope of the function. |
| 1184 |
data.values_host_buffer = |
| 1185 |
# if DEAL_II_KOKKOS_VERSION_GTE(4, 0, 0) |
| 1186 |
Kokkos::create_mirror_view_and_copy(Kokkos::SharedHostPinnedSpace{}, |
| 1187 |
data.values); |
| 1188 |
# else |
| 1189 |
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, |
| 1190 |
data.values); |
| 1191 |
# endif |
| 1192 |
|
| 1193 |
partitioner->export_to_ghosted_array_start<Number, MemorySpace::Host>( |
| 1194 |
communication_channel, |
| 1195 |
ArrayView<const Number, MemorySpace::Host>( |
| 1196 |
data.values_host_buffer.data(), |
| 1197 |
partitioner->locally_owned_size()), |
| 1198 |
ArrayView<Number, MemorySpace::Host>( |
| 1199 |
import_data.values_host_buffer.data(), |
| 1200 |
partitioner->n_import_indices()), |
| 1201 |
ArrayView<Number, MemorySpace::Host>( |
| 1202 |
data.values_host_buffer.data() + |
| 1203 |
partitioner->locally_owned_size(), |
| 1204 |
partitioner->n_ghost_indices()), |
| 1205 |
update_ghost_values_requests); |
| 1206 |
} |
| 1207 |
else |
| 1208 |
# endif |
| 1209 |
{ |
| 1210 |
partitioner->export_to_ghosted_array_start<Number, MemorySpaceType>( |
| 1211 |
communication_channel, |
| 1212 |
ArrayView<const Number, MemorySpaceType>( |
| 1213 |
data.values.data(), partitioner->locally_owned_size()), |
| 1214 |
ArrayView<Number, MemorySpaceType>(import_data.values.data(), |
| 1215 |
partitioner->n_import_indices()), |
| 1216 |
ArrayView<Number, MemorySpaceType>( |
| 1217 |
data.values.data() + partitioner->locally_owned_size(), |
| 1218 |
partitioner->n_ghost_indices()), |
| 1219 |
update_ghost_values_requests); |
| 1220 |
} |
| 1221 |
|
| 1222 |
#else |
| 1223 |
(void)communication_channel; |
| 1224 |
#endif |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
template <typename Number, typename MemorySpaceType> |
| 1230 |
void |
| 1231 |
Vector<Number, MemorySpaceType>::update_ghost_values_finish() const |
| 1232 |
{ |
| 1233 |
#ifdef DEAL_II_WITH_MPI |
| 1234 |
// wait for both sends and receives to complete, even though only |
| 1235 |
// receives are really necessary. this gives (much) better performance |
| 1236 |
AssertDimension(partitioner->ghost_targets().size() + |
| 1237 |
partitioner->import_targets().size(), |
| 1238 |
update_ghost_values_requests.size()); |
| 1239 |
if (update_ghost_values_requests.size() > 0) |
| 1240 |
{ |
| 1241 |
// make this function thread safe |
| 1242 |
std::lock_guard<std::mutex> lock(mutex); |
| 1243 |
|
| 1244 |
# if !defined(DEAL_II_MPI_WITH_DEVICE_SUPPORT) |
| 1245 |
if (std::is_same_v<MemorySpaceType, MemorySpace::Default>) |
| 1246 |
{ |
| 1247 |
partitioner->export_to_ghosted_array_finish( |
| 1248 |
ArrayView<Number, MemorySpace::Host>( |
| 1249 |
data.values_host_buffer.data() + |
| 1250 |
partitioner->locally_owned_size(), |
| 1251 |
partitioner->n_ghost_indices()), |
| 1252 |
update_ghost_values_requests); |
| 1253 |
|
| 1254 |
// The communication is done on the host, so we need to |
| 1255 |
// move the data back to the device. |
| 1256 |
auto range = Kokkos::make_pair(partitioner->locally_owned_size(), |
| 1257 |
partitioner->locally_owned_size() + |
| 1258 |
partitioner->n_ghost_indices()); |
| 1259 |
Kokkos::deep_copy(Kokkos::subview(data.values, range), |
| 1260 |
Kokkos::subview(data.values_host_buffer, |
| 1261 |
range)); |
| 1262 |
|
| 1263 |
Kokkos::resize(data.values_host_buffer, 0); |
| 1264 |
} |
| 1265 |
else |
| 1266 |
# endif |
| 1267 |
{ |
| 1268 |
partitioner->export_to_ghosted_array_finish( |
| 1269 |
ArrayView<Number, MemorySpaceType>( |
| 1270 |
data.values.data() + partitioner->locally_owned_size(), |
| 1271 |
partitioner->n_ghost_indices()), |
| 1272 |
update_ghost_values_requests); |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
#endif |
| 1277 |
vector_is_ghosted = true; |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
template <typename Number, typename MemorySpaceType> |
| 1283 |
void |
| 1284 |
Vector<Number, MemorySpaceType>::import_elements( |
| 1285 |
const ReadWriteVector<Number> &V, |
| 1286 |
const VectorOperation::values operation, |
| 1287 |
const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase> |
| 1288 |
&communication_pattern) |
| 1289 |
{ |
| 1290 |
// If no communication pattern is given, create one. Otherwise, use the |
| 1291 |
// given one. |
| 1292 |
std::shared_ptr<const Utilities::MPI::Partitioner> comm_pattern; |
| 1293 |
if (communication_pattern.get() == nullptr) |
| 1294 |
{ |
| 1295 |
// Split the IndexSet of V in locally owned elements and ghost indices |
| 1296 |
// then create the communication pattern |
| 1297 |
IndexSet locally_owned_elem = locally_owned_elements(); |
| 1298 |
IndexSet ghost_indices = V.get_stored_elements(); |
| 1299 |
ghost_indices.subtract_set(locally_owned_elem); |
| 1300 |
comm_pattern = std::make_shared<Utilities::MPI::Partitioner>( |
| 1301 |
locally_owned_elem, ghost_indices, get_mpi_communicator()); |
| 1302 |
} |
| 1303 |
else |
| 1304 |
{ |
| 1305 |
comm_pattern = |
| 1306 |
std::dynamic_pointer_cast<const Utilities::MPI::Partitioner>( |
| 1307 |
communication_pattern); |
| 1308 |
AssertThrow(comm_pattern != nullptr, |
| 1309 |
ExcMessage("The communication pattern is not of type " |
| 1310 |
"Utilities::MPI::Partitioner.")); |
| 1311 |
} |
| 1312 |
Vector<Number, ::dealii::MemorySpace::Host> tmp_vector(comm_pattern); |
| 1313 |
|
| 1314 |
data.copy_to(tmp_vector.begin(), locally_owned_size()); |
| 1315 |
|
| 1316 |
// fill entries from ReadWriteVector into the distributed vector, |
| 1317 |
// including ghost entries. this is not really efficient right now |
| 1318 |
// because indices are translated twice, once by nth_index_in_set(i) and |
| 1319 |
// once for operator() of tmp_vector |
| 1320 |
const IndexSet &v_stored = V.get_stored_elements(); |
| 1321 |
const size_type v_n_elements = v_stored.n_elements(); |
| 1322 |
switch (operation) |
| 1323 |
{ |
| 1324 |
case VectorOperation::insert: |
| 1325 |
{ |
| 1326 |
for (size_type i = 0; i < v_n_elements; ++i) |
| 1327 |
tmp_vector(v_stored.nth_index_in_set(i)) = V.local_element(i); |
| 1328 |
|
| 1329 |
break; |
| 1330 |
} |
| 1331 |
case VectorOperation::add: |
| 1332 |
{ |
| 1333 |
for (size_type i = 0; i < v_n_elements; ++i) |
| 1334 |
tmp_vector(v_stored.nth_index_in_set(i)) += V.local_element(i); |
| 1335 |
|
| 1336 |
break; |
| 1337 |
} |
| 1338 |
case VectorOperation::min: |
| 1339 |
{ |
| 1340 |
for (size_type i = 0; i < v_n_elements; ++i) |
| 1341 |
tmp_vector(v_stored.nth_index_in_set(i)) = |
| 1342 |
internal::get_min(tmp_vector(v_stored.nth_index_in_set(i)), |
| 1343 |
V.local_element(i)); |
| 1344 |
|
| 1345 |
break; |
| 1346 |
} |
| 1347 |
case VectorOperation::max: |
| 1348 |
{ |
| 1349 |
for (size_type i = 0; i < v_n_elements; ++i) |
| 1350 |
tmp_vector(v_stored.nth_index_in_set(i)) = |
| 1351 |
internal::get_max(tmp_vector(v_stored.nth_index_in_set(i)), |
| 1352 |
V.local_element(i)); |
| 1353 |
|
| 1354 |
break; |
| 1355 |
} |
| 1356 |
default: |
| 1357 |
{ |
| 1358 |
Assert(false, ExcMessage("This operation is not supported.")); |
| 1359 |
} |
| 1360 |
} |
| 1361 |
tmp_vector.compress(operation); |
| 1362 |
|
| 1363 |
data.copy_from(tmp_vector.begin(), locally_owned_size()); |
| 1364 |
} |
| 1365 |
|
| 1366 |
template <typename Number, typename MemorySpaceType> |
| 1367 |
void |
| 1368 |
Vector<Number, MemorySpaceType>::swap( |
| 1369 |
Vector<Number, MemorySpaceType> &v) noexcept |
| 1370 |
{ |
| 1371 |
#ifdef DEAL_II_WITH_MPI |
| 1372 |
|
| 1373 |
if constexpr (running_in_debug_mode()) |
| 1374 |
{ |
| 1375 |
Assert(Utilities::MPI::job_supports_mpi() || |
| 1376 |
(update_ghost_values_requests.empty() && |
| 1377 |
compress_requests.empty()), |
| 1378 |
ExcInternalError()); |
| 1379 |
|
| 1380 |
// make sure that there are not outstanding requests from updating |
| 1381 |
// ghost values or compress |
| 1382 |
if (update_ghost_values_requests.size() > 0) |
| 1383 |
{ |
| 1384 |
int flag = 1; |
| 1385 |
const int ierr = MPI_Testall(update_ghost_values_requests.size(), |
| 1386 |
update_ghost_values_requests.data(), |
| 1387 |
&flag, |
| 1388 |
MPI_STATUSES_IGNORE); |
| 1389 |
AssertThrowMPI(ierr); |
| 1390 |
Assert(flag == 1, |
| 1391 |
ExcMessage( |
| 1392 |
"MPI found unfinished update_ghost_values() requests " |
| 1393 |
"when calling swap, which is not allowed.")); |
| 1394 |
} |
| 1395 |
if (compress_requests.size() > 0) |
| 1396 |
{ |
| 1397 |
int flag = 1; |
| 1398 |
const int ierr = MPI_Testall(compress_requests.size(), |
| 1399 |
compress_requests.data(), |
| 1400 |
&flag, |
| 1401 |
MPI_STATUSES_IGNORE); |
| 1402 |
AssertThrowMPI(ierr); |
| 1403 |
Assert(flag == 1, |
| 1404 |
ExcMessage("MPI found unfinished compress() requests " |
| 1405 |
"when calling swap, which is not allowed.")); |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
std::swap(compress_requests, v.compress_requests); |
| 1410 |
std::swap(update_ghost_values_requests, v.update_ghost_values_requests); |
| 1411 |
std::swap(comm_sm, v.comm_sm); |
| 1412 |
#endif |
| 1413 |
|
| 1414 |
std::swap(partitioner, v.partitioner); |
| 1415 |
std::swap(thread_loop_partitioner, v.thread_loop_partitioner); |
| 1416 |
std::swap(allocated_size, v.allocated_size); |
| 1417 |
std::swap(data, v.data); |
| 1418 |
std::swap(import_data, v.import_data); |
| 1419 |
std::swap(vector_is_ghosted, v.vector_is_ghosted); |
| 1420 |
} |
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
template <typename Number, typename MemorySpaceType> |
| 1425 |
Vector<Number, MemorySpaceType> & |
| 1426 |
Vector<Number, MemorySpaceType>::operator=( // NOLINT |
| 1427 |
Vector<Number, MemorySpaceType> &&v) |
| 1428 |
{ |
| 1429 |
static_cast<EnableObserverPointer &>(*this) = |
| 1430 |
static_cast<EnableObserverPointer &&>(v); |
| 1431 |
this->swap(v); |
| 1432 |
return *this; |
| 1433 |
} |
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
template <typename Number, typename MemorySpaceType> |
| 1438 |
Vector<Number, MemorySpaceType> & |
| 1439 |
Vector<Number, MemorySpaceType>::operator=(const Number s) |
| 1440 |
{ |
| 1441 |
const size_type this_size = locally_owned_size(); |
| 1442 |
if (this_size > 0) |
| 1443 |
{ |
| 1444 |
dealii::internal::VectorOperations:: |
| 1445 |
functions<Number, Number, MemorySpaceType>::set( |
| 1446 |
thread_loop_partitioner, this_size, s, data); |
| 1447 |
} |
| 1448 |
|
| 1449 |
// if we call Vector::operator=0, we want to zero out all the entries |
| 1450 |
// plus ghosts. |
| 1451 |
if (s == Number()) |
| 1452 |
zero_out_ghost_values(); |
| 1453 |
|
| 1454 |
return *this; |
| 1455 |
} |
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
template <typename Number, typename MemorySpaceType> |
| 1460 |
Vector<Number, MemorySpaceType> & |
| 1461 |
Vector<Number, MemorySpaceType>::operator+=( |
| 1462 |
const Vector<Number, MemorySpaceType> &v) |
| 1463 |
{ |
| 1464 |
AssertDimension(locally_owned_size(), v.locally_owned_size()); |
| 1465 |
|
| 1466 |
dealii::internal::VectorOperations:: |
| 1467 |
functions<Number, Number, MemorySpaceType>::add_vector( |
| 1468 |
thread_loop_partitioner, |
| 1469 |
partitioner->locally_owned_size(), |
| 1470 |
v.data, |
| 1471 |
data); |
| 1472 |
|
| 1473 |
if (vector_is_ghosted) |
| 1474 |
update_ghost_values(); |
| 1475 |
else |
| 1476 |
assert_no_residual_content_in_ghost_region(); |
| 1477 |
|
| 1478 |
return *this; |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
template <typename Number, typename MemorySpaceType> |
| 1484 |
Vector<Number, MemorySpaceType> & |
| 1485 |
Vector<Number, MemorySpaceType>::operator-=( |
| 1486 |
const Vector<Number, MemorySpaceType> &v) |
| 1487 |
{ |
| 1488 |
AssertDimension(locally_owned_size(), v.locally_owned_size()); |
| 1489 |
|
| 1490 |
dealii::internal::VectorOperations:: |
| 1491 |
functions<Number, Number, MemorySpaceType>::subtract_vector( |
| 1492 |
thread_loop_partitioner, |
| 1493 |
partitioner->locally_owned_size(), |
| 1494 |
v.data, |
| 1495 |
data); |
| 1496 |
|
| 1497 |
if (vector_is_ghosted) |
| 1498 |
update_ghost_values(); |
| 1499 |
else |
| 1500 |
assert_no_residual_content_in_ghost_region(); |
| 1501 |
|
| 1502 |
return *this; |
| 1503 |
} |
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
template <typename Number, typename MemorySpaceType> |
| 1508 |
void |
| 1509 |
Vector<Number, MemorySpaceType>::add(const Number a) |
| 1510 |
{ |
| 1511 |
AssertIsFinite(a); |
| 1512 |
|
| 1513 |
dealii::internal::VectorOperations:: |
| 1514 |
functions<Number, Number, MemorySpaceType>::add_factor( |
| 1515 |
thread_loop_partitioner, partitioner->locally_owned_size(), a, data); |
| 1516 |
|
| 1517 |
if (vector_is_ghosted) |
| 1518 |
update_ghost_values(); |
| 1519 |
else |
| 1520 |
assert_no_residual_content_in_ghost_region(); |
| 1521 |
} |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
template <typename Number, typename MemorySpaceType> |
| 1526 |
void |
| 1527 |
Vector<Number, MemorySpaceType>::add_local( |
| 1528 |
const Number a, |
| 1529 |
const Vector<Number, MemorySpaceType> &v) |
| 1530 |
{ |
| 1531 |
AssertIsFinite(a); |
| 1532 |
AssertDimension(locally_owned_size(), v.locally_owned_size()); |
| 1533 |
|
| 1534 |
// nothing to do if a is zero |
| 1535 |
if (a == Number(0.)) |
| 1536 |
return; |
| 1537 |
|
| 1538 |
dealii::internal::VectorOperations:: |
| 1539 |
functions<Number, Number, MemorySpaceType>::add_av( |
| 1540 |
thread_loop_partitioner, |
| 1541 |
partitioner->locally_owned_size(), |
| 1542 |
a, |
| 1543 |
v.data, |
| 1544 |
data); |
| 1545 |
} |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
template <typename Number, typename MemorySpaceType> |
| 1550 |
void |
| 1551 |
Vector<Number, MemorySpaceType>::add( |
| 1552 |
const Number a, |
| 1553 |
const Vector<Number, MemorySpaceType> &vv) |
| 1554 |
{ |
| 1555 |
add_local(a, vv); |
| 1556 |
|
| 1557 |
if (vector_is_ghosted) |
| 1558 |
update_ghost_values(); |
| 1559 |
else |
| 1560 |
assert_no_residual_content_in_ghost_region(); |
| 1561 |
} |
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
template <typename Number, typename MemorySpaceType> |
| 1566 |
void |
| 1567 |
Vector<Number, MemorySpaceType>::add( |
| 1568 |
const Number a, |
| 1569 |
const Vector<Number, MemorySpaceType> &v, |
| 1570 |
const Number b, |
| 1571 |
const Vector<Number, MemorySpaceType> &w) |
| 1572 |
{ |
| 1573 |
AssertIsFinite(a); |
| 1574 |
AssertIsFinite(b); |
| 1575 |
|
| 1576 |
AssertDimension(locally_owned_size(), v.locally_owned_size()); |
| 1577 |
AssertDimension(locally_owned_size(), w.locally_owned_size()); |
| 1578 |
|
| 1579 |
dealii::internal::VectorOperations:: |
| 1580 |
functions<Number, Number, MemorySpaceType>::add_avpbw( |
| 1581 |
thread_loop_partitioner, |
| 1582 |
partitioner->locally_owned_size(), |
| 1583 |
a, |
| 1584 |
b, |
| 1585 |
v.data, |
| 1586 |
w.data, |
| 1587 |
data); |
| 1588 |
|
| 1589 |
if (vector_is_ghosted) |
| 1590 |
update_ghost_values(); |
| 1591 |
else |
| 1592 |
assert_no_residual_content_in_ghost_region(); |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
template <typename Number, typename MemorySpaceType> |
| 1598 |
void |
| 1599 |
Vector<Number, MemorySpaceType>::add(const std::vector<size_type> &indices, |
| 1600 |
const std::vector<Number> &values) |
| 1601 |
{ |
| 1602 |
for (std::size_t i = 0; i < indices.size(); ++i) |
| 1603 |
{ |
| 1604 |
this->operator()(indices[i]) += values[i]; |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
template <typename Number, typename MemorySpaceType> |
| 1611 |
void |
| 1612 |
Vector<Number, MemorySpaceType>::sadd( |
| 1613 |
const Number x, |
| 1614 |
const Vector<Number, MemorySpaceType> &v) |
| 1615 |
{ |
| 1616 |
AssertIsFinite(x); |
| 1617 |
AssertDimension(locally_owned_size(), v.locally_owned_size()); |
| 1618 |
|
| 1619 |
dealii::internal::VectorOperations:: |
| 1620 |
functions<Number, Number, MemorySpaceType>::sadd_xv( |
| 1621 |
thread_loop_partitioner, |
| 1622 |
partitioner->locally_owned_size(), |
| 1623 |
x, |
| 1624 |
v.data, |
| 1625 |
data); |
| 1626 |
|
| 1627 |
if (vector_is_ghosted) |
| 1628 |
update_ghost_values(); |
| 1629 |
else |
| 1630 |
assert_no_residual_content_in_ghost_region(); |
| 1631 |
} |
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
template <typename Number, typename MemorySpaceType> |
| 1636 |
void |
| 1637 |
Vector<Number, MemorySpaceType>::sadd_local( |
| 1638 |
const Number x, |
| 1639 |
const Number a, |
| 1640 |
const Vector<Number, MemorySpaceType> &v) |
| 1641 |
{ |
| 1642 |
AssertIsFinite(x); |
| 1643 |
AssertIsFinite(a); |
| 1644 |
AssertDimension(locally_owned_size(), v.locally_owned_size()); |
| 1645 |
|
| 1646 |
dealii::internal::VectorOperations:: |
| 1647 |
functions<Number, Number, MemorySpaceType>::sadd_xav( |
| 1648 |
thread_loop_partitioner, |
| 1649 |
partitioner->locally_owned_size(), |
| 1650 |
x, |
| 1651 |
a, |
| 1652 |
v.data, |
| 1653 |
data); |
| 1654 |
} |
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
template <typename Number, typename MemorySpaceType> |
| 1659 |
void |
| 1660 |
Vector<Number, MemorySpaceType>::sadd( |
| 1661 |
const Number x, |
| 1662 |
const Number a, |
| 1663 |
const Vector<Number, MemorySpaceType> &v) |
| 1664 |
{ |
| 1665 |
sadd_local(x, a, v); |
| 1666 |
|
| 1667 |
if (vector_is_ghosted) |
| 1668 |
update_ghost_values(); |
| 1669 |
else |
| 1670 |
assert_no_residual_content_in_ghost_region(); |
| 1671 |
} |
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
template <typename Number, typename MemorySpaceType> |
| 1676 |
Vector<Number, MemorySpaceType> & |
| 1677 |
Vector<Number, MemorySpaceType>::operator*=(const Number factor) |
| 1678 |
{ |
| 1679 |
AssertIsFinite(factor); |
| 1680 |
|
| 1681 |
dealii::internal::VectorOperations:: |
| 1682 |
functions<Number, Number, MemorySpaceType>::multiply_factor( |
| 1683 |
thread_loop_partitioner, |
| 1684 |
partitioner->locally_owned_size(), |
| 1685 |
factor, |
| 1686 |
data); |
| 1687 |
|
| 1688 |
if (vector_is_ghosted) |
| 1689 |
update_ghost_values(); |
| 1690 |
else |
| 1691 |
assert_no_residual_content_in_ghost_region(); |
| 1692 |
|
| 1693 |
return *this; |
| 1694 |
} |
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
template <typename Number, typename MemorySpaceType> |
| 1699 |
Vector<Number, MemorySpaceType> & |
| 1700 |
Vector<Number, MemorySpaceType>::operator/=(const Number factor) |
| 1701 |
{ |
| 1702 |
operator*=(static_cast<Number>(1.) / factor); |
| 1703 |
return *this; |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
template <typename Number, typename MemorySpaceType> |
| 1709 |
void |
| 1710 |
Vector<Number, MemorySpaceType>::scale( |
| 1711 |
const Vector<Number, MemorySpaceType> &v) |
| 1712 |
{ |
| 1713 |
AssertDimension(locally_owned_size(), v.locally_owned_size()); |
| 1714 |
|
| 1715 |
dealii::internal::VectorOperations:: |
| 1716 |
functions<Number, Number, MemorySpaceType>::scale( |
| 1717 |
thread_loop_partitioner, locally_owned_size(), v.data, data); |
| 1718 |
|
| 1719 |
if (vector_is_ghosted) |
| 1720 |
update_ghost_values(); |
| 1721 |
else |
| 1722 |
assert_no_residual_content_in_ghost_region(); |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
template <typename Number, typename MemorySpaceType> |
| 1728 |
void |
| 1729 |
Vector<Number, MemorySpaceType>::equ( |
| 1730 |
const Number a, |
| 1731 |
const Vector<Number, MemorySpaceType> &v) |
| 1732 |
{ |
| 1733 |
AssertIsFinite(a); |
| 1734 |
AssertDimension(locally_owned_size(), v.locally_owned_size()); |
| 1735 |
|
| 1736 |
dealii::internal::VectorOperations:: |
| 1737 |
functions<Number, Number, MemorySpaceType>::equ_au( |
| 1738 |
thread_loop_partitioner, |
| 1739 |
partitioner->locally_owned_size(), |
| 1740 |
a, |
| 1741 |
v.data, |
| 1742 |
data); |
| 1743 |
|
| 1744 |
|
| 1745 |
if (vector_is_ghosted) |
| 1746 |
update_ghost_values(); |
| 1747 |
else |
| 1748 |
assert_no_residual_content_in_ghost_region(); |
| 1749 |
} |
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
template <typename Number, typename MemorySpaceType> |
| 1754 |
void |
| 1755 |
Vector<Number, |
| 1756 |
MemorySpaceType>::assert_no_residual_content_in_ghost_region() const |
| 1757 |
{ |
| 1758 |
if constexpr (running_in_debug_mode()) |
| 1759 |
{ |
| 1760 |
// This should only be called for non-ghosted vectors |
| 1761 |
Assert(!vector_is_ghosted, ExcInternalError()); |
| 1762 |
|
| 1763 |
// Run a reduction over the ghost range only to find out whether some |
| 1764 |
// entries are non-zero |
| 1765 |
real_type sum = real_type(); |
| 1766 |
dealii::internal::VectorOperations:: |
| 1767 |
functions<Number, Number, MemorySpaceType>::norm_1( |
| 1768 |
thread_loop_partitioner, |
| 1769 |
partitioner->n_ghost_indices(), |
| 1770 |
sum, |
| 1771 |
data, |
| 1772 |
partitioner->locally_owned_size()); |
| 1773 |
|
| 1774 |
Assert(sum == real_type(), |
| 1775 |
ExcMessage( |
| 1776 |
"You called a vector space operation like add(), " |
| 1777 |
"scale(), operator* for a non-ghosted vector, which " |
| 1778 |
"will not update the content in the memory locations " |
| 1779 |
"reserved for ghost values. However, a non-zero " |
| 1780 |
"content was detected for some of those entries, which " |
| 1781 |
"can lead to an invalid state of the vector. Please " |
| 1782 |
"call Vector::compress(VectorOperation::add) or " |
| 1783 |
"Vector::zero_out_ghost_values() before calling a " |
| 1784 |
"vector space operation to avoid this problem.")); |
| 1785 |
} |
| 1786 |
} |
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
template <typename Number, typename MemorySpaceType> |
| 1791 |
void |
| 1792 |
Vector<Number, MemorySpaceType>::extract_subvector_to( |
| 1793 |
const ArrayView<const types::global_dof_index> &indices, |
| 1794 |
const ArrayView<Number> &elements) const |
| 1795 |
{ |
| 1796 |
AssertDimension(indices.size(), elements.size()); |
| 1797 |
for (unsigned int i = 0; i < indices.size(); ++i) |
| 1798 |
{ |
| 1799 |
AssertIndexRange(indices[i], size()); |
| 1800 |
elements[i] = (*this)[indices[i]]; |
| 1801 |
} |
| 1802 |
} |
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
template <typename Number, typename MemorySpaceType> |
| 1807 |
bool |
| 1808 |
Vector<Number, MemorySpaceType>::all_zero() const |
| 1809 |
{ |
| 1810 |
return (linfty_norm() == 0) ? true : false; |
| 1811 |
} |
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
template <typename Number, typename MemorySpaceType> |
| 1816 |
template <typename Number2> |
| 1817 |
Number |
| 1818 |
Vector<Number, MemorySpaceType>::inner_product_local( |
| 1819 |
const Vector<Number2, MemorySpaceType> &v) const |
| 1820 |
{ |
| 1821 |
if (PointerComparison::equal(this, &v)) |
| 1822 |
return norm_sqr_local(); |
| 1823 |
|
| 1824 |
AssertDimension(partitioner->locally_owned_size(), |
| 1825 |
v.partitioner->locally_owned_size()); |
| 1826 |
|
| 1827 |
return dealii::internal::VectorOperations:: |
| 1828 |
functions<Number, Number2, MemorySpaceType>::dot( |
| 1829 |
thread_loop_partitioner, |
| 1830 |
partitioner->locally_owned_size(), |
| 1831 |
v.data, |
| 1832 |
data); |
| 1833 |
} |
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
template <typename Number, typename MemorySpaceType> |
| 1838 |
Number |
| 1839 |
Vector<Number, MemorySpaceType>::operator*( |
| 1840 |
const Vector<Number, MemorySpaceType> &v) const |
| 1841 |
{ |
| 1842 |
Number local_result = inner_product_local(v); |
| 1843 |
if (partitioner->n_mpi_processes() > 1) |
| 1844 |
return Utilities::MPI::sum(local_result, |
| 1845 |
partitioner->get_mpi_communicator()); |
| 1846 |
else |
| 1847 |
return local_result; |
| 1848 |
} |
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
template <typename Number, typename MemorySpaceType> |
| 1853 |
typename Vector<Number, MemorySpaceType>::real_type |
| 1854 |
Vector<Number, MemorySpaceType>::norm_sqr_local() const |
| 1855 |
{ |
| 1856 |
real_type sum; |
| 1857 |
|
| 1858 |
dealii::internal::VectorOperations:: |
| 1859 |
functions<Number, Number, MemorySpaceType>::norm_2( |
| 1860 |
thread_loop_partitioner, |
| 1861 |
partitioner->locally_owned_size(), |
| 1862 |
sum, |
| 1863 |
data); |
| 1864 |
|
| 1865 |
AssertIsFinite(sum); |
| 1866 |
|
| 1867 |
return sum; |
| 1868 |
} |
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
template <typename Number, typename MemorySpaceType> |
| 1873 |
Number |
| 1874 |
Vector<Number, MemorySpaceType>::mean_value_local() const |
| 1875 |
{ |
| 1876 |
Assert(size() != 0, ExcEmptyObject()); |
| 1877 |
|
| 1878 |
if (partitioner->locally_owned_size() == 0) |
| 1879 |
return Number(); |
| 1880 |
|
| 1881 |
Number sum = ::dealii::internal::VectorOperations:: |
| 1882 |
functions<Number, Number, MemorySpaceType>::mean_value( |
| 1883 |
thread_loop_partitioner, partitioner->locally_owned_size(), data); |
| 1884 |
|
| 1885 |
return sum / real_type(partitioner->locally_owned_size()); |
| 1886 |
} |
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
template <typename Number, typename MemorySpaceType> |
| 1891 |
Number |
| 1892 |
Vector<Number, MemorySpaceType>::mean_value() const |
| 1893 |
{ |
| 1894 |
Number local_result = mean_value_local(); |
| 1895 |
if (partitioner->n_mpi_processes() > 1) |
| 1896 |
return Utilities::MPI::sum(local_result * |
| 1897 |
static_cast<real_type>( |
| 1898 |
partitioner->locally_owned_size()), |
| 1899 |
partitioner->get_mpi_communicator()) / |
| 1900 |
static_cast<real_type>(partitioner->size()); |
| 1901 |
else |
| 1902 |
return local_result; |
| 1903 |
} |
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
template <typename Number, typename MemorySpaceType> |
| 1908 |
typename Vector<Number, MemorySpaceType>::real_type |
| 1909 |
Vector<Number, MemorySpaceType>::l1_norm_local() const |
| 1910 |
{ |
| 1911 |
real_type sum; |
| 1912 |
|
| 1913 |
dealii::internal::VectorOperations:: |
| 1914 |
functions<Number, Number, MemorySpaceType>::norm_1( |
| 1915 |
thread_loop_partitioner, |
| 1916 |
partitioner->locally_owned_size(), |
| 1917 |
sum, |
| 1918 |
data); |
| 1919 |
|
| 1920 |
return sum; |
| 1921 |
} |
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
template <typename Number, typename MemorySpaceType> |
| 1926 |
typename Vector<Number, MemorySpaceType>::real_type |
| 1927 |
Vector<Number, MemorySpaceType>::l1_norm() const |
| 1928 |
{ |
| 1929 |
real_type local_result = l1_norm_local(); |
| 1930 |
if (partitioner->n_mpi_processes() > 1) |
| 1931 |
return Utilities::MPI::sum(local_result, |
| 1932 |
partitioner->get_mpi_communicator()); |
| 1933 |
else |
| 1934 |
return local_result; |
| 1935 |
} |
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
template <typename Number, typename MemorySpaceType> |
| 1940 |
typename Vector<Number, MemorySpaceType>::real_type |
| 1941 |
Vector<Number, MemorySpaceType>::norm_sqr() const |
| 1942 |
{ |
| 1943 |
real_type local_result = norm_sqr_local(); |
| 1944 |
if (partitioner->n_mpi_processes() > 1) |
| 1945 |
return Utilities::MPI::sum(local_result, |
| 1946 |
partitioner->get_mpi_communicator()); |
| 1947 |
else |
| 1948 |
return local_result; |
| 1949 |
} |
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
template <typename Number, typename MemorySpaceType> |
| 1954 |
typename Vector<Number, MemorySpaceType>::real_type |
| 1955 |
Vector<Number, MemorySpaceType>::l2_norm() const |
| 1956 |
{ |
| 1957 |
return std::sqrt(norm_sqr()); |
| 1958 |
} |
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
template <typename Number, typename MemorySpaceType> |
| 1963 |
typename Vector<Number, MemorySpaceType>::real_type |
| 1964 |
Vector<Number, MemorySpaceType>::lp_norm_local(const real_type p) const |
| 1965 |
{ |
| 1966 |
real_type sum = 0.; |
| 1967 |
|
| 1968 |
dealii::internal::VectorOperations:: |
| 1969 |
functions<Number, Number, MemorySpaceType>::norm_p( |
| 1970 |
thread_loop_partitioner, |
| 1971 |
partitioner->locally_owned_size(), |
| 1972 |
sum, |
| 1973 |
p, |
| 1974 |
data); |
| 1975 |
|
| 1976 |
return std::pow(sum, 1. / p); |
| 1977 |
} |
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
template <typename Number, typename MemorySpaceType> |
| 1982 |
typename Vector<Number, MemorySpaceType>::real_type |
| 1983 |
Vector<Number, MemorySpaceType>::lp_norm(const real_type p) const |
| 1984 |
{ |
| 1985 |
const real_type local_result = lp_norm_local(p); |
| 1986 |
if (partitioner->n_mpi_processes() > 1) |
| 1987 |
return std::pow( |
| 1988 |
Utilities::MPI::sum(std::pow(local_result, p), |
| 1989 |
partitioner->get_mpi_communicator()), |
| 1990 |
static_cast<real_type>(1.0 / p)); |
| 1991 |
else |
| 1992 |
return local_result; |
| 1993 |
} |
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
template <typename Number, typename MemorySpaceType> |
| 1998 |
typename Vector<Number, MemorySpaceType>::real_type |
| 1999 |
Vector<Number, MemorySpaceType>::linfty_norm_local() const |
| 2000 |
{ |
| 2001 |
real_type max = 0.; |
| 2002 |
|
| 2003 |
const size_type locally_owned_size = partitioner->locally_owned_size(); |
| 2004 |
internal::la_parallel_vector_templates_functions< |
| 2005 |
Number, |
| 2006 |
MemorySpaceType>::linfty_norm_local(data, locally_owned_size, max); |
| 2007 |
|
| 2008 |
return max; |
| 2009 |
} |
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
template <typename Number, typename MemorySpaceType> |
| 2014 |
inline typename Vector<Number, MemorySpaceType>::real_type |
| 2015 |
Vector<Number, MemorySpaceType>::linfty_norm() const |
| 2016 |
{ |
| 2017 |
const real_type local_result = linfty_norm_local(); |
| 2018 |
if (partitioner->n_mpi_processes() > 1) |
| 2019 |
return Utilities::MPI::max(local_result, |
| 2020 |
partitioner->get_mpi_communicator()); |
| 2021 |
else |
| 2022 |
return local_result; |
| 2023 |
} |
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
template <typename Number, typename MemorySpaceType> |
| 2028 |
Number |
| 2029 |
Vector<Number, MemorySpaceType>::add_and_dot_local( |
| 2030 |
const Number a, |
| 2031 |
const Vector<Number, MemorySpaceType> &v, |
| 2032 |
const Vector<Number, MemorySpaceType> &w) |
| 2033 |
{ |
| 2034 |
const size_type vec_size = partitioner->locally_owned_size(); |
| 2035 |
AssertDimension(vec_size, v.locally_owned_size()); |
| 2036 |
AssertDimension(vec_size, w.locally_owned_size()); |
| 2037 |
|
| 2038 |
Number sum = dealii::internal::VectorOperations:: |
| 2039 |
functions<Number, Number, MemorySpaceType>::add_and_dot( |
| 2040 |
thread_loop_partitioner, vec_size, a, v.data, w.data, data); |
| 2041 |
|
| 2042 |
AssertIsFinite(sum); |
| 2043 |
|
| 2044 |
return sum; |
| 2045 |
} |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
template <typename Number, typename MemorySpaceType> |
| 2050 |
Number |
| 2051 |
Vector<Number, MemorySpaceType>::add_and_dot( |
| 2052 |
const Number a, |
| 2053 |
const Vector<Number, MemorySpaceType> &v, |
| 2054 |
const Vector<Number, MemorySpaceType> &w) |
| 2055 |
{ |
| 2056 |
Number local_result = add_and_dot_local(a, v, w); |
| 2057 |
if (partitioner->n_mpi_processes() > 1) |
| 2058 |
return Utilities::MPI::sum(local_result, |
| 2059 |
partitioner->get_mpi_communicator()); |
| 2060 |
else |
| 2061 |
return local_result; |
| 2062 |
} |
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
template <typename Number, typename MemorySpaceType> |
| 2067 |
inline bool |
| 2068 |
Vector<Number, MemorySpaceType>::partitioners_are_compatible( |
| 2069 |
const Utilities::MPI::Partitioner &part) const |
| 2070 |
{ |
| 2071 |
return partitioner->is_compatible(part); |
| 2072 |
} |
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
template <typename Number, typename MemorySpaceType> |
| 2077 |
inline bool |
| 2078 |
Vector<Number, MemorySpaceType>::partitioners_are_globally_compatible( |
| 2079 |
const Utilities::MPI::Partitioner &part) const |
| 2080 |
{ |
| 2081 |
return partitioner->is_globally_compatible(part); |
| 2082 |
} |
| 2083 |
|
| 2084 |
|
| 2085 |
|
| 2086 |
template <typename Number, typename MemorySpaceType> |
| 2087 |
std::size_t |
| 2088 |
Vector<Number, MemorySpaceType>::memory_consumption() const |
| 2089 |
{ |
| 2090 |
std::size_t memory = sizeof(*this); |
| 2091 |
memory += sizeof(Number) * static_cast<std::size_t>(allocated_size); |
| 2092 |
|
| 2093 |
// if the partitioner is shared between more processors, just count a |
| 2094 |
// fraction of that memory, since we're not actually using more memory |
| 2095 |
// for it. |
| 2096 |
if (partitioner.use_count() > 0) |
| 2097 |
memory += |
| 2098 |
partitioner->memory_consumption() / partitioner.use_count() + 1; |
| 2099 |
if (import_data.values_host_buffer.size() != 0 || |
| 2100 |
import_data.values.size() != 0) |
| 2101 |
memory += (static_cast<std::size_t>(partitioner->n_import_indices()) * |
| 2102 |
sizeof(Number)); |
| 2103 |
return memory; |
| 2104 |
} |
| 2105 |
|
| 2106 |
|
| 2107 |
|
| 2108 |
template <typename Number, typename MemorySpaceType> |
| 2109 |
void |
| 2110 |
Vector<Number, MemorySpaceType>::print(std::ostream &out, |
| 2111 |
const unsigned int precision, |
| 2112 |
const bool scientific, |
| 2113 |
const bool across) const |
| 2114 |
{ |
| 2115 |
Assert(partitioner.get() != nullptr, ExcInternalError()); |
| 2116 |
AssertThrow(out.fail() == false, ExcIO()); |
| 2117 |
std::ios::fmtflags old_flags = out.flags(); |
| 2118 |
unsigned int old_precision = out.precision(precision); |
| 2119 |
|
| 2120 |
out.precision(precision); |
| 2121 |
if (scientific) |
| 2122 |
out.setf(std::ios::scientific, std::ios::floatfield); |
| 2123 |
else |
| 2124 |
out.setf(std::ios::fixed, std::ios::floatfield); |
| 2125 |
|
| 2126 |
// to make the vector write out all the information in order, use as |
| 2127 |
// many barriers as there are processors and start writing when it's our |
| 2128 |
// turn |
| 2129 |
#ifdef DEAL_II_WITH_MPI |
| 2130 |
if (partitioner->n_mpi_processes() > 1) |
| 2131 |
for (unsigned int i = 0; i < partitioner->this_mpi_process(); ++i) |
| 2132 |
{ |
| 2133 |
const int ierr = MPI_Barrier(partitioner->get_mpi_communicator()); |
| 2134 |
AssertThrowMPI(ierr); |
| 2135 |
} |
| 2136 |
#endif |
| 2137 |
|
| 2138 |
std::vector<Number> stored_elements(allocated_size); |
| 2139 |
data.copy_to(stored_elements.data(), allocated_size); |
| 2140 |
|
| 2141 |
out << "Process #" << partitioner->this_mpi_process() << std::endl |
| 2142 |
<< "Local range: [" << partitioner->local_range().first << ", " |
| 2143 |
<< partitioner->local_range().second |
| 2144 |
<< "), global size: " << partitioner->size() << std::endl |
| 2145 |
<< "Vector data:" << std::endl; |
| 2146 |
if (across) |
| 2147 |
for (size_type i = 0; i < partitioner->locally_owned_size(); ++i) |
| 2148 |
out << stored_elements[i] << ' '; |
| 2149 |
else |
| 2150 |
for (size_type i = 0; i < partitioner->locally_owned_size(); ++i) |
| 2151 |
out << stored_elements[i] << std::endl; |
| 2152 |
out << std::endl; |
| 2153 |
|
| 2154 |
if (vector_is_ghosted) |
| 2155 |
{ |
| 2156 |
out << "Ghost entries (global index / value):" << std::endl; |
| 2157 |
if (across) |
| 2158 |
for (size_type i = 0; i < partitioner->n_ghost_indices(); ++i) |
| 2159 |
out << '(' << partitioner->ghost_indices().nth_index_in_set(i) |
| 2160 |
<< '/' |
| 2161 |
<< stored_elements[partitioner->locally_owned_size() + i] |
| 2162 |
<< ") "; |
| 2163 |
else |
| 2164 |
for (size_type i = 0; i < partitioner->n_ghost_indices(); ++i) |
| 2165 |
out << '(' << partitioner->ghost_indices().nth_index_in_set(i) |
| 2166 |
<< '/' |
| 2167 |
<< stored_elements[partitioner->locally_owned_size() + i] |
| 2168 |
<< ')' << std::endl; |
| 2169 |
out << std::endl; |
| 2170 |
} |
| 2171 |
out << std::flush; |
| 2172 |
|
| 2173 |
#ifdef DEAL_II_WITH_MPI |
| 2174 |
if (partitioner->n_mpi_processes() > 1) |
| 2175 |
{ |
| 2176 |
int ierr = MPI_Barrier(partitioner->get_mpi_communicator()); |
| 2177 |
AssertThrowMPI(ierr); |
| 2178 |
|
| 2179 |
for (unsigned int i = partitioner->this_mpi_process() + 1; |
| 2180 |
i < partitioner->n_mpi_processes(); |
| 2181 |
i++) |
| 2182 |
{ |
| 2183 |
ierr = MPI_Barrier(partitioner->get_mpi_communicator()); |
| 2184 |
AssertThrowMPI(ierr); |
| 2185 |
} |
| 2186 |
} |
| 2187 |
#endif |
| 2188 |
|
| 2189 |
AssertThrow(out.fail() == false, ExcIO()); |
| 2190 |
// reset output format |
| 2191 |
out.flags(old_flags); |
| 2192 |
out.precision(old_precision); |
| 2193 |
} |
| 2194 |
|
| 2195 |
} // end of namespace distributed |
| 2196 |
} // end of namespace LinearAlgebra |
| 2197 |
|
| 2198 |
|
| 2199 |
DEAL_II_NAMESPACE_CLOSE |
| 2200 |
|
| 2201 |
#endif |