| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2020 - 2023 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 |
|
| 16 |
#ifndef dealii_matrix_free_vector_data_exchange_h |
| 17 |
#define dealii_matrix_free_vector_data_exchange_h |
| 18 |
|
| 19 |
|
| 20 |
#include <deal.II/base/config.h> |
| 21 |
|
| 22 |
#include <deal.II/base/array_view.h> |
| 23 |
#include <deal.II/base/mpi_stub.h> |
| 24 |
#include <deal.II/base/partitioner.h> |
| 25 |
|
| 26 |
#include <deal.II/lac/vector_operation.h> |
| 27 |
|
| 28 |
#include <memory> |
| 29 |
|
| 30 |
DEAL_II_NAMESPACE_OPEN |
| 31 |
|
| 32 |
namespace internal |
| 33 |
{ |
| 34 |
namespace MatrixFreeFunctions |
| 35 |
{ |
| 36 |
/** |
| 37 |
* Namespace containing classes for inter-process data exchange (i.e., |
| 38 |
* for update_ghost_values and compress) in MatrixFree. |
| 39 |
*/ |
| 40 |
namespace VectorDataExchange |
| 41 |
{ |
| 42 |
/** |
| 43 |
* Interface needed by MatrixFree. |
| 44 |
*/ |
| 45 |
class Base |
| 46 |
{ |
| 47 |
public: |
| 48 |
virtual ~Base() = default; |
| 49 |
|
| 50 |
virtual unsigned int |
| 51 |
locally_owned_size() const = 0; |
| 52 |
|
| 53 |
virtual unsigned int |
| 54 |
n_ghost_indices() const = 0; |
| 55 |
|
| 56 |
virtual unsigned int |
| 57 |
n_import_indices() const = 0; |
| 58 |
|
| 59 |
virtual unsigned int |
| 60 |
n_import_sm_procs() const = 0; |
| 61 |
|
| 62 |
virtual types::global_dof_index |
| 63 |
size() const = 0; |
| 64 |
|
| 65 |
virtual void |
| 66 |
export_to_ghosted_array_start( |
| 67 |
const unsigned int communication_channel, |
| 68 |
const ArrayView<const double> &locally_owned_array, |
| 69 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 70 |
const ArrayView<double> &ghost_array, |
| 71 |
const ArrayView<double> &temporary_storage, |
| 72 |
std::vector<MPI_Request> &requests) const = 0; |
| 73 |
|
| 74 |
virtual void |
| 75 |
export_to_ghosted_array_finish( |
| 76 |
const ArrayView<const double> &locally_owned_array, |
| 77 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 78 |
const ArrayView<double> &ghost_array, |
| 79 |
std::vector<MPI_Request> &requests) const = 0; |
| 80 |
|
| 81 |
virtual void |
| 82 |
import_from_ghosted_array_start( |
| 83 |
const VectorOperation::values vector_operation, |
| 84 |
const unsigned int communication_channel, |
| 85 |
const ArrayView<const double> &locally_owned_array, |
| 86 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 87 |
const ArrayView<double> &ghost_array, |
| 88 |
const ArrayView<double> &temporary_storage, |
| 89 |
std::vector<MPI_Request> &requests) const = 0; |
| 90 |
|
| 91 |
virtual void |
| 92 |
import_from_ghosted_array_finish( |
| 93 |
const VectorOperation::values vector_operation, |
| 94 |
const ArrayView<double> &locally_owned_storage, |
| 95 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 96 |
const ArrayView<double> &ghost_array, |
| 97 |
const ArrayView<const double> &temporary_storage, |
| 98 |
std::vector<MPI_Request> &requests) const = 0; |
| 99 |
|
| 100 |
virtual void |
| 101 |
reset_ghost_values(const ArrayView<double> &ghost_array) const = 0; |
| 102 |
|
| 103 |
virtual void |
| 104 |
export_to_ghosted_array_start( |
| 105 |
const unsigned int communication_channel, |
| 106 |
const ArrayView<const float> &locally_owned_array, |
| 107 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 108 |
const ArrayView<float> &ghost_array, |
| 109 |
const ArrayView<float> &temporary_storage, |
| 110 |
std::vector<MPI_Request> &requests) const = 0; |
| 111 |
|
| 112 |
virtual void |
| 113 |
export_to_ghosted_array_finish( |
| 114 |
const ArrayView<const float> &locally_owned_array, |
| 115 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 116 |
const ArrayView<float> &ghost_array, |
| 117 |
std::vector<MPI_Request> &requests) const = 0; |
| 118 |
|
| 119 |
virtual void |
| 120 |
import_from_ghosted_array_start( |
| 121 |
const VectorOperation::values vector_operation, |
| 122 |
const unsigned int communication_channel, |
| 123 |
const ArrayView<const float> &locally_owned_array, |
| 124 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 125 |
const ArrayView<float> &ghost_array, |
| 126 |
const ArrayView<float> &temporary_storage, |
| 127 |
std::vector<MPI_Request> &requests) const = 0; |
| 128 |
|
| 129 |
virtual void |
| 130 |
import_from_ghosted_array_finish( |
| 131 |
const VectorOperation::values vector_operation, |
| 132 |
const ArrayView<float> &locally_owned_storage, |
| 133 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 134 |
const ArrayView<float> &ghost_array, |
| 135 |
const ArrayView<const float> &temporary_storage, |
| 136 |
std::vector<MPI_Request> &requests) const = 0; |
| 137 |
|
| 138 |
virtual void |
| 139 |
reset_ghost_values(const ArrayView<float> &ghost_array) const = 0; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Class that simply delegates the task to a Utilities::MPI::Partitioner. |
| 145 |
*/ |
| 146 |
class PartitionerWrapper : public Base |
| 147 |
{ |
| 148 |
public: |
| 149 |
PartitionerWrapper( |
| 150 |
const std::shared_ptr<const Utilities::MPI::Partitioner> |
| 151 |
&partitioner); |
| 152 |
|
| 153 |
virtual ~PartitionerWrapper() = default; |
| 154 |
|
| 155 |
unsigned int |
| 156 |
locally_owned_size() const override; |
| 157 |
|
| 158 |
unsigned int |
| 159 |
n_ghost_indices() const override; |
| 160 |
|
| 161 |
unsigned int |
| 162 |
n_import_indices() const override; |
| 163 |
|
| 164 |
unsigned int |
| 165 |
n_import_sm_procs() const override; |
| 166 |
|
| 167 |
types::global_dof_index |
| 168 |
size() const override; |
| 169 |
|
| 170 |
void |
| 171 |
export_to_ghosted_array_start( |
| 172 |
const unsigned int communication_channel, |
| 173 |
const ArrayView<const double> &locally_owned_array, |
| 174 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 175 |
const ArrayView<double> &ghost_array, |
| 176 |
const ArrayView<double> &temporary_storage, |
| 177 |
std::vector<MPI_Request> &requests) const override; |
| 178 |
|
| 179 |
void |
| 180 |
export_to_ghosted_array_finish( |
| 181 |
const ArrayView<const double> &locally_owned_array, |
| 182 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 183 |
const ArrayView<double> &ghost_array, |
| 184 |
std::vector<MPI_Request> &requests) const override; |
| 185 |
|
| 186 |
void |
| 187 |
import_from_ghosted_array_start( |
| 188 |
const VectorOperation::values vector_operation, |
| 189 |
const unsigned int communication_channel, |
| 190 |
const ArrayView<const double> &locally_owned_array, |
| 191 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 192 |
const ArrayView<double> &ghost_array, |
| 193 |
const ArrayView<double> &temporary_storage, |
| 194 |
std::vector<MPI_Request> &requests) const override; |
| 195 |
|
| 196 |
void |
| 197 |
import_from_ghosted_array_finish( |
| 198 |
const VectorOperation::values vector_operation, |
| 199 |
const ArrayView<double> &locally_owned_storage, |
| 200 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 201 |
const ArrayView<double> &ghost_array, |
| 202 |
const ArrayView<const double> &temporary_storage, |
| 203 |
std::vector<MPI_Request> &requests) const override; |
| 204 |
|
| 205 |
void |
| 206 |
reset_ghost_values(const ArrayView<double> &ghost_array) const override; |
| 207 |
|
| 208 |
void |
| 209 |
export_to_ghosted_array_start( |
| 210 |
const unsigned int communication_channel, |
| 211 |
const ArrayView<const float> &locally_owned_array, |
| 212 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 213 |
const ArrayView<float> &ghost_array, |
| 214 |
const ArrayView<float> &temporary_storage, |
| 215 |
std::vector<MPI_Request> &requests) const override; |
| 216 |
|
| 217 |
void |
| 218 |
export_to_ghosted_array_finish( |
| 219 |
const ArrayView<const float> &locally_owned_array, |
| 220 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 221 |
const ArrayView<float> &ghost_array, |
| 222 |
std::vector<MPI_Request> &requests) const override; |
| 223 |
|
| 224 |
void |
| 225 |
import_from_ghosted_array_start( |
| 226 |
const VectorOperation::values vector_operation, |
| 227 |
const unsigned int communication_channel, |
| 228 |
const ArrayView<const float> &locally_owned_array, |
| 229 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 230 |
const ArrayView<float> &ghost_array, |
| 231 |
const ArrayView<float> &temporary_storage, |
| 232 |
std::vector<MPI_Request> &requests) const override; |
| 233 |
|
| 234 |
void |
| 235 |
import_from_ghosted_array_finish( |
| 236 |
const VectorOperation::values vector_operation, |
| 237 |
const ArrayView<float> &locally_owned_storage, |
| 238 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 239 |
const ArrayView<float> &ghost_array, |
| 240 |
const ArrayView<const float> &temporary_storage, |
| 241 |
std::vector<MPI_Request> &requests) const override; |
| 242 |
|
| 243 |
void |
| 244 |
reset_ghost_values(const ArrayView<float> &ghost_array) const override; |
| 245 |
|
| 246 |
private: |
| 247 |
template <typename Number> |
| 248 |
void |
| 249 |
reset_ghost_values_impl(const ArrayView<Number> &ghost_array) const; |
| 250 |
|
| 251 |
const std::shared_ptr<const Utilities::MPI::Partitioner> partitioner; |
| 252 |
}; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
/** |
| 257 |
* Similar to the above but using the internal data structures in the |
| 258 |
* partitioner in order to identify indices of degrees of freedom that are |
| 259 |
* in the same shared memory region. |
| 260 |
*/ |
| 261 |
class Full : public Base |
| 262 |
{ |
| 263 |
public: |
| 264 |
Full( |
| 265 |
const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner, |
| 266 |
const MPI_Comm communicator_sm); |
| 267 |
|
| 268 |
unsigned int |
| 269 |
locally_owned_size() const override; |
| 270 |
|
| 271 |
unsigned int |
| 272 |
n_ghost_indices() const override; |
| 273 |
|
| 274 |
unsigned int |
| 275 |
n_import_indices() const override; |
| 276 |
|
| 277 |
virtual unsigned int |
| 278 |
n_import_sm_procs() const override; |
| 279 |
|
| 280 |
virtual types::global_dof_index |
| 281 |
size() const override; |
| 282 |
|
| 283 |
MPI_Comm |
| 284 |
get_sm_mpi_communicator() const; |
| 285 |
|
| 286 |
void |
| 287 |
export_to_ghosted_array_start( |
| 288 |
const unsigned int communication_channel, |
| 289 |
const ArrayView<const double> &locally_owned_array, |
| 290 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 291 |
const ArrayView<double> &ghost_array, |
| 292 |
const ArrayView<double> &temporary_storage, |
| 293 |
std::vector<MPI_Request> &requests) const override; |
| 294 |
|
| 295 |
void |
| 296 |
export_to_ghosted_array_finish( |
| 297 |
const ArrayView<const double> &locally_owned_array, |
| 298 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 299 |
const ArrayView<double> &ghost_array, |
| 300 |
std::vector<MPI_Request> &requests) const override; |
| 301 |
|
| 302 |
void |
| 303 |
import_from_ghosted_array_start( |
| 304 |
const VectorOperation::values vector_operation, |
| 305 |
const unsigned int communication_channel, |
| 306 |
const ArrayView<const double> &locally_owned_array, |
| 307 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 308 |
const ArrayView<double> &ghost_array, |
| 309 |
const ArrayView<double> &temporary_storage, |
| 310 |
std::vector<MPI_Request> &requests) const override; |
| 311 |
|
| 312 |
void |
| 313 |
import_from_ghosted_array_finish( |
| 314 |
const VectorOperation::values vector_operation, |
| 315 |
const ArrayView<double> &locally_owned_storage, |
| 316 |
const std::vector<ArrayView<const double>> &shared_arrays, |
| 317 |
const ArrayView<double> &ghost_array, |
| 318 |
const ArrayView<const double> &temporary_storage, |
| 319 |
std::vector<MPI_Request> &requests) const override; |
| 320 |
|
| 321 |
void |
| 322 |
reset_ghost_values(const ArrayView<double> &ghost_array) const override; |
| 323 |
|
| 324 |
void |
| 325 |
export_to_ghosted_array_start( |
| 326 |
const unsigned int communication_channel, |
| 327 |
const ArrayView<const float> &locally_owned_array, |
| 328 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 329 |
const ArrayView<float> &ghost_array, |
| 330 |
const ArrayView<float> &temporary_storage, |
| 331 |
std::vector<MPI_Request> &requests) const override; |
| 332 |
|
| 333 |
void |
| 334 |
export_to_ghosted_array_finish( |
| 335 |
const ArrayView<const float> &locally_owned_array, |
| 336 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 337 |
const ArrayView<float> &ghost_array, |
| 338 |
std::vector<MPI_Request> &requests) const override; |
| 339 |
|
| 340 |
void |
| 341 |
import_from_ghosted_array_start( |
| 342 |
const VectorOperation::values vector_operation, |
| 343 |
const unsigned int communication_channel, |
| 344 |
const ArrayView<const float> &locally_owned_array, |
| 345 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 346 |
const ArrayView<float> &ghost_array, |
| 347 |
const ArrayView<float> &temporary_storage, |
| 348 |
std::vector<MPI_Request> &requests) const override; |
| 349 |
|
| 350 |
void |
| 351 |
import_from_ghosted_array_finish( |
| 352 |
const VectorOperation::values vector_operation, |
| 353 |
const ArrayView<float> &locally_owned_storage, |
| 354 |
const std::vector<ArrayView<const float>> &shared_arrays, |
| 355 |
const ArrayView<float> &ghost_array, |
| 356 |
const ArrayView<const float> &temporary_storage, |
| 357 |
std::vector<MPI_Request> &requests) const override; |
| 358 |
|
| 359 |
void |
| 360 |
reset_ghost_values(const ArrayView<float> &ghost_array) const override; |
| 361 |
|
| 362 |
private: |
| 363 |
template <typename Number> |
| 364 |
void |
| 365 |
export_to_ghosted_array_start_impl( |
| 366 |
const unsigned int communication_channel, |
| 367 |
const ArrayView<const Number> &locally_owned_array, |
| 368 |
const std::vector<ArrayView<const Number>> &shared_arrays, |
| 369 |
const ArrayView<Number> &ghost_array, |
| 370 |
const ArrayView<Number> &temporary_storage, |
| 371 |
std::vector<MPI_Request> &requests) const; |
| 372 |
|
| 373 |
template <typename Number> |
| 374 |
void |
| 375 |
export_to_ghosted_array_finish_impl( |
| 376 |
const ArrayView<const Number> &locally_owned_array, |
| 377 |
const std::vector<ArrayView<const Number>> &shared_arrays, |
| 378 |
const ArrayView<Number> &ghost_array, |
| 379 |
std::vector<MPI_Request> &requests) const; |
| 380 |
|
| 381 |
template <typename Number> |
| 382 |
void |
| 383 |
import_from_ghosted_array_start_impl( |
| 384 |
const VectorOperation::values vector_operation, |
| 385 |
const unsigned int communication_channel, |
| 386 |
const ArrayView<const Number> &locally_owned_array, |
| 387 |
const std::vector<ArrayView<const Number>> &shared_arrays, |
| 388 |
const ArrayView<Number> &ghost_array, |
| 389 |
const ArrayView<Number> &temporary_storage, |
| 390 |
std::vector<MPI_Request> &requests) const; |
| 391 |
|
| 392 |
template <typename Number> |
| 393 |
void |
| 394 |
import_from_ghosted_array_finish_impl( |
| 395 |
const VectorOperation::values vector_operation, |
| 396 |
const ArrayView<Number> &locally_owned_storage, |
| 397 |
const std::vector<ArrayView<const Number>> &shared_arrays, |
| 398 |
const ArrayView<Number> &ghost_array, |
| 399 |
const ArrayView<const Number> &temporary_storage, |
| 400 |
std::vector<MPI_Request> &requests) const; |
| 401 |
|
| 402 |
template <typename Number> |
| 403 |
void |
| 404 |
reset_ghost_values_impl(const ArrayView<Number> &ghost_array) const; |
| 405 |
|
| 406 |
private: |
| 407 |
/** |
| 408 |
* Global communicator. |
| 409 |
*/ |
| 410 |
const MPI_Comm comm; |
| 411 |
|
| 412 |
/** |
| 413 |
* Shared-memory sub-communicator. |
| 414 |
*/ |
| 415 |
const MPI_Comm comm_sm; |
| 416 |
|
| 417 |
/** |
| 418 |
* Number of locally-owned vector entries. |
| 419 |
*/ |
| 420 |
const unsigned int n_local_elements; |
| 421 |
|
| 422 |
/** |
| 423 |
* Number of ghost vector entries. |
| 424 |
*/ |
| 425 |
const unsigned int n_ghost_elements; |
| 426 |
|
| 427 |
/** |
| 428 |
* Number of global vector entries. |
| 429 |
*/ |
| 430 |
const types::global_dof_index n_global_elements; |
| 431 |
|
| 432 |
/** |
| 433 |
* A variable caching the number of ghost indices in a larger set of |
| 434 |
* indices by rank. |
| 435 |
*/ |
| 436 |
std::vector<unsigned int> n_ghost_indices_in_larger_set_by_remote_rank; |
| 437 |
|
| 438 |
/** |
| 439 |
* The set of indices that appear for an IndexSet that is a subset of a |
| 440 |
* larger set for each rank in a compressed manner. |
| 441 |
*/ |
| 442 |
std::pair<std::vector<unsigned int>, |
| 443 |
std::vector<std::pair<unsigned int, unsigned int>>> |
| 444 |
ghost_indices_subset_data; |
| 445 |
|
| 446 |
/** |
| 447 |
* An array that contains information which processors my ghost indices |
| 448 |
* belong to, at which offset and how many those indices are |
| 449 |
*/ |
| 450 |
std::vector<std::array<unsigned int, 3>> ghost_targets_data; |
| 451 |
|
| 452 |
/** |
| 453 |
* The set of processors and length of data field which send us their |
| 454 |
* ghost data. |
| 455 |
* |
| 456 |
* @note Structured as ghost_targets_data. |
| 457 |
*/ |
| 458 |
std::vector<std::array<unsigned int, 3>> import_targets_data; |
| 459 |
|
| 460 |
/** |
| 461 |
* An array that caches the number of chunks in the import indices per |
| 462 |
* MPI rank. The length is import_indices_data.size()+1. |
| 463 |
* |
| 464 |
* The set of (local) indices that we are importing during compress() |
| 465 |
* from remote processes, i.e., others' ghosts that belong to the local |
| 466 |
* range. |
| 467 |
*/ |
| 468 |
std::pair<std::vector<unsigned int>, |
| 469 |
std::vector<std::pair<unsigned int, unsigned int>>> |
| 470 |
import_indices_data; |
| 471 |
|
| 472 |
/** |
| 473 |
* Shared-memory ranks from which data is copied from during |
| 474 |
* export_to_ghosted_array_finish(). |
| 475 |
*/ |
| 476 |
std::vector<unsigned int> sm_ghost_ranks; |
| 477 |
|
| 478 |
/** |
| 479 |
* Indices from where to copy data from during |
| 480 |
* export_to_ghosted_array_finish(). |
| 481 |
*/ |
| 482 |
std::pair<std::vector<unsigned int>, |
| 483 |
std::vector<std::pair<unsigned int, unsigned int>>> |
| 484 |
sm_export_data; |
| 485 |
|
| 486 |
/** |
| 487 |
* Indices where to copy data to during |
| 488 |
* export_to_ghosted_array_finish(). |
| 489 |
*/ |
| 490 |
std::pair<std::vector<unsigned int>, |
| 491 |
std::vector<std::pair<unsigned int, unsigned int>>> |
| 492 |
sm_export_data_this; |
| 493 |
|
| 494 |
/** |
| 495 |
* Shared-memory ranks from where to copy data from during |
| 496 |
* import_from_ghosted_array_finish(). |
| 497 |
*/ |
| 498 |
std::vector<unsigned int> sm_import_ranks; |
| 499 |
|
| 500 |
/** |
| 501 |
* Indices from where to copy data from during |
| 502 |
* import_from_ghosted_array_finish(). |
| 503 |
*/ |
| 504 |
std::pair<std::vector<unsigned int>, |
| 505 |
std::vector<std::pair<unsigned int, unsigned int>>> |
| 506 |
sm_import_data; |
| 507 |
|
| 508 |
/** |
| 509 |
* Indices where to copy data to during |
| 510 |
* import_from_ghosted_array_finish(). |
| 511 |
*/ |
| 512 |
std::pair<std::vector<unsigned int>, |
| 513 |
std::vector<std::pair<unsigned int, unsigned int>>> |
| 514 |
sm_import_data_this; |
| 515 |
}; |
| 516 |
|
| 517 |
} // namespace VectorDataExchange |
| 518 |
} // end of namespace MatrixFreeFunctions |
| 519 |
} // end of namespace internal |
| 520 |
|
| 521 |
DEAL_II_NAMESPACE_CLOSE |
| 522 |
|
| 523 |
#endif |