| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2020 - 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_mpi_noncontiguous_partitioner_templates_h |
| 16 |
#define dealii_mpi_noncontiguous_partitioner_templates_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/mpi.h> |
| 21 |
#include <deal.II/base/mpi.templates.h> |
| 22 |
#include <deal.II/base/mpi_noncontiguous_partitioner.h> |
| 23 |
#include <deal.II/base/mpi_tags.h> |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
DEAL_II_NAMESPACE_OPEN |
| 28 |
|
| 29 |
namespace Utilities |
| 30 |
{ |
| 31 |
namespace MPI |
| 32 |
{ |
| 33 |
template <typename Number, unsigned int n_components_templated> |
| 34 |
void |
| 35 |
NoncontiguousPartitioner::export_to_ghosted_array( |
| 36 |
const ArrayView<const Number> &src, |
| 37 |
const ArrayView<Number> &dst, |
| 38 |
const unsigned int n_components) const |
| 39 |
{ |
| 40 |
Assert((n_components_templated != 0) != (n_components != 0), |
| 41 |
ExcNotImplemented()); |
| 42 |
|
| 43 |
const unsigned int n_components_to_be_used = |
| 44 |
(n_components_templated != 0) ? n_components_templated : n_components; |
| 45 |
|
| 46 |
// allocate internal memory since needed |
| 47 |
if (requests.size() != send_ranks.size() + recv_ranks.size()) |
| 48 |
requests.resize(send_ranks.size() + recv_ranks.size()); |
| 49 |
|
| 50 |
if (this->buffers.size() != |
| 51 |
send_ptr.back() * sizeof(Number) * n_components_to_be_used) |
| 52 |
this->buffers.resize(this->temporary_storage_size() * sizeof(Number) * |
| 53 |
n_components_to_be_used); |
| 54 |
|
| 55 |
// perform actual exchange |
| 56 |
this->template export_to_ghosted_array<Number, n_components_templated>( |
| 57 |
0, |
| 58 |
src, |
| 59 |
ArrayView<Number>(reinterpret_cast<Number *>(this->buffers.data()), |
| 60 |
send_ptr.back() * n_components_to_be_used), |
| 61 |
dst, |
| 62 |
this->requests, |
| 63 |
n_components); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
template <typename Number, unsigned int n_components_templated> |
| 68 |
void |
| 69 |
NoncontiguousPartitioner::export_to_ghosted_array( |
| 70 |
const unsigned int communication_channel, |
| 71 |
const ArrayView<const Number> &locally_owned_array, |
| 72 |
const ArrayView<Number> &temporary_storage, |
| 73 |
const ArrayView<Number> &ghost_array, |
| 74 |
std::vector<MPI_Request> &requests, |
| 75 |
const unsigned int n_components) const |
| 76 |
{ |
| 77 |
this->template export_to_ghosted_array_start<Number, |
| 78 |
n_components_templated>( |
| 79 |
communication_channel, |
| 80 |
locally_owned_array, |
| 81 |
temporary_storage, |
| 82 |
requests, |
| 83 |
n_components); |
| 84 |
this->template export_to_ghosted_array_finish<Number, |
| 85 |
n_components_templated>( |
| 86 |
temporary_storage, ghost_array, requests, n_components); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
template <typename Number, unsigned int n_components_templated> |
| 92 |
void |
| 93 |
NoncontiguousPartitioner::export_to_ghosted_array_start( |
| 94 |
const unsigned int communication_channel, |
| 95 |
const ArrayView<const Number> &src, |
| 96 |
const ArrayView<Number> &buffers, |
| 97 |
std::vector<MPI_Request> &requests, |
| 98 |
const unsigned int n_components) const |
| 99 |
{ |
| 100 |
#ifndef DEAL_II_WITH_MPI |
| 101 |
(void)communication_channel; |
| 102 |
(void)src; |
| 103 |
(void)buffers; |
| 104 |
(void)requests; |
| 105 |
(void)n_components; |
| 106 |
Assert(false, ExcNeedsMPI()); |
| 107 |
#else |
| 108 |
AssertDimension(requests.size(), recv_ranks.size() + send_ranks.size()); |
| 109 |
|
| 110 |
Assert((n_components_templated != 0) != (n_components != 0), |
| 111 |
ExcNotImplemented()); |
| 112 |
|
| 113 |
const unsigned int n_components_to_be_used = |
| 114 |
(n_components_templated != 0) ? n_components_templated : n_components; |
| 115 |
|
| 116 |
const auto tag = |
| 117 |
communication_channel + |
| 118 |
internal::Tags::noncontiguous_partitioner_update_ghost_values_start; |
| 119 |
|
| 120 |
AssertIndexRange( |
| 121 |
tag, |
| 122 |
internal::Tags::noncontiguous_partitioner_update_ghost_values_end + 1); |
| 123 |
|
| 124 |
// post recv |
| 125 |
AssertIndexRange(recv_ranks.size(), recv_ptr.size()); |
| 126 |
for (types::global_dof_index i = 0; i < recv_ranks.size(); ++i) |
| 127 |
{ |
| 128 |
const int ierr = |
| 129 |
MPI_Irecv(buffers.data() + recv_ptr[i] * n_components_to_be_used, |
| 130 |
(recv_ptr[i + 1] - recv_ptr[i]) * n_components_to_be_used, |
| 131 |
Utilities::MPI::mpi_type_id_for_type<Number>, |
| 132 |
recv_ranks[i], |
| 133 |
tag, |
| 134 |
communicator, |
| 135 |
&requests[i + send_ranks.size()]); |
| 136 |
AssertThrowMPI(ierr); |
| 137 |
} |
| 138 |
|
| 139 |
// post send |
| 140 |
AssertIndexRange(send_ranks.size(), send_ptr.size()); |
| 141 |
for (types::global_dof_index i = 0, k = 0; i < send_ranks.size(); ++i) |
| 142 |
{ |
| 143 |
// collect data to be send |
| 144 |
for (types::global_dof_index j = send_ptr[i]; j < send_ptr[i + 1]; |
| 145 |
++j, ++k) |
| 146 |
{ |
| 147 |
AssertIndexRange(k, send_indices.size()); |
| 148 |
for (unsigned int comp = 0; comp < n_components_to_be_used; |
| 149 |
++comp) |
| 150 |
buffers[j * n_components_to_be_used + comp] = |
| 151 |
src[send_indices[k] * n_components_to_be_used + comp]; |
| 152 |
} |
| 153 |
|
| 154 |
// send data |
| 155 |
Assert((send_ptr[i] * n_components_to_be_used < buffers.size()) || |
| 156 |
(send_ptr[i] * n_components_to_be_used == buffers.size() && |
| 157 |
send_ptr[i + 1] * n_components_to_be_used == |
| 158 |
send_ptr[i] * n_components_to_be_used), |
| 159 |
ExcMessage("The input buffer doesn't contain enough entries")); |
| 160 |
const int ierr = |
| 161 |
MPI_Isend(buffers.data() + send_ptr[i] * n_components_to_be_used, |
| 162 |
(send_ptr[i + 1] - send_ptr[i]) * n_components_to_be_used, |
| 163 |
Utilities::MPI::mpi_type_id_for_type<Number>, |
| 164 |
send_ranks[i], |
| 165 |
tag, |
| 166 |
communicator, |
| 167 |
&requests[i]); |
| 168 |
AssertThrowMPI(ierr); |
| 169 |
} |
| 170 |
#endif |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
template <typename Number, unsigned int n_components_templated> |
| 176 |
void |
| 177 |
NoncontiguousPartitioner::export_to_ghosted_array_finish( |
| 178 |
const ArrayView<const Number> &buffers, |
| 179 |
const ArrayView<Number> &dst, |
| 180 |
std::vector<MPI_Request> &requests, |
| 181 |
const unsigned int n_components) const |
| 182 |
{ |
| 183 |
#ifndef DEAL_II_WITH_MPI |
| 184 |
(void)buffers; |
| 185 |
(void)dst; |
| 186 |
(void)requests; |
| 187 |
(void)n_components; |
| 188 |
Assert(false, ExcNeedsMPI()); |
| 189 |
#else |
| 190 |
|
| 191 |
Assert((n_components_templated != 0) != (n_components != 0), |
| 192 |
ExcNotImplemented()); |
| 193 |
|
| 194 |
const unsigned int n_components_to_be_used = |
| 195 |
(n_components_templated != 0) ? n_components_templated : n_components; |
| 196 |
|
| 197 |
// receive all data packages and copy data from buffers |
| 198 |
for (types::global_dof_index proc = 0; proc < recv_ranks.size(); ++proc) |
| 199 |
{ |
| 200 |
int i; |
| 201 |
MPI_Status status; |
| 202 |
const auto ierr = MPI_Waitany(recv_ranks.size(), |
| 203 |
requests.data() + send_ranks.size(), |
| 204 |
&i, |
| 205 |
&status); |
| 206 |
AssertThrowMPI(ierr); |
| 207 |
|
| 208 |
AssertIndexRange(i + 1, recv_ptr.size()); |
| 209 |
for (types::global_dof_index j = recv_ptr[i]; j < recv_ptr[i + 1]; |
| 210 |
++j) |
| 211 |
for (unsigned int comp = 0; comp < n_components_to_be_used; ++comp) |
| 212 |
{ |
| 213 |
const auto &value = buffers[j * n_components_to_be_used + comp]; |
| 214 |
|
| 215 |
if (recv_indices_duplicates_ptr.empty()) |
| 216 |
dst[recv_indices[j] * n_components_to_be_used + comp] = value; |
| 217 |
else |
| 218 |
for (auto k = recv_indices_duplicates_ptr[recv_indices[j]]; |
| 219 |
k < recv_indices_duplicates_ptr[recv_indices[j] + 1]; |
| 220 |
++k) |
| 221 |
dst[recv_indices_duplicates[k] * n_components_to_be_used + |
| 222 |
comp] = value; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// wait that all data packages have been sent |
| 227 |
const int ierr = |
| 228 |
MPI_Waitall(send_ranks.size(), requests.data(), MPI_STATUSES_IGNORE); |
| 229 |
AssertThrowMPI(ierr); |
| 230 |
#endif |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
template <typename Number> |
| 236 |
void |
| 237 |
NoncontiguousPartitioner::import_from_ghosted_array( |
| 238 |
const VectorOperation::values vector_operation, |
| 239 |
const ArrayView<Number> &src, |
| 240 |
const ArrayView<Number> &dst) const |
| 241 |
{ |
| 242 |
// allocate internal memory since needed |
| 243 |
if (requests.size() != send_ranks.size() + recv_ranks.size()) |
| 244 |
requests.resize(send_ranks.size() + recv_ranks.size()); |
| 245 |
|
| 246 |
if (this->buffers.size() != send_ptr.back() * sizeof(Number)) |
| 247 |
this->buffers.resize(this->temporary_storage_size() * sizeof(Number)); |
| 248 |
|
| 249 |
// perform actual exchange |
| 250 |
this->template import_from_ghosted_array<Number>( |
| 251 |
vector_operation, |
| 252 |
0, |
| 253 |
src, |
| 254 |
ArrayView<Number>(reinterpret_cast<Number *>(this->buffers.data()), |
| 255 |
send_ptr.back()), |
| 256 |
dst, |
| 257 |
this->requests); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
template <typename Number> |
| 263 |
void |
| 264 |
NoncontiguousPartitioner::import_from_ghosted_array( |
| 265 |
const VectorOperation::values vector_operation, |
| 266 |
const unsigned int communication_channel, |
| 267 |
const ArrayView<Number> &ghost_array, |
| 268 |
const ArrayView<Number> &temporary_storage, |
| 269 |
const ArrayView<Number> &locally_owned_array, |
| 270 |
std::vector<MPI_Request> &requests) const |
| 271 |
{ |
| 272 |
this->template import_from_ghosted_array_start<Number>( |
| 273 |
vector_operation, |
| 274 |
communication_channel, |
| 275 |
ghost_array, |
| 276 |
temporary_storage, |
| 277 |
requests); |
| 278 |
this->template import_from_ghosted_array_finish<Number>( |
| 279 |
vector_operation, temporary_storage, locally_owned_array, requests); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
template <typename Number> |
| 285 |
void |
| 286 |
NoncontiguousPartitioner::import_from_ghosted_array_start( |
| 287 |
const VectorOperation::values vector_operation, |
| 288 |
const unsigned int communication_channel, |
| 289 |
const ArrayView<Number> &src, |
| 290 |
const ArrayView<Number> &buffers, |
| 291 |
std::vector<MPI_Request> &requests) const |
| 292 |
{ |
| 293 |
#ifndef DEAL_II_WITH_MPI |
| 294 |
(void)vector_operation; |
| 295 |
(void)communication_channel; |
| 296 |
(void)src; |
| 297 |
(void)buffers; |
| 298 |
(void)requests; |
| 299 |
Assert(false, ExcNeedsMPI()); |
| 300 |
#else |
| 301 |
(void)vector_operation; // nothing to do here |
| 302 |
|
| 303 |
AssertDimension(requests.size(), recv_ranks.size() + send_ranks.size()); |
| 304 |
|
| 305 |
const auto tag = |
| 306 |
communication_channel + |
| 307 |
internal::Tags::noncontiguous_partitioner_update_ghost_values_start; |
| 308 |
|
| 309 |
AssertIndexRange( |
| 310 |
tag, |
| 311 |
internal::Tags::noncontiguous_partitioner_update_ghost_values_end + 1); |
| 312 |
|
| 313 |
// post recv |
| 314 |
AssertIndexRange(send_ranks.size(), send_ptr.size()); |
| 315 |
for (types::global_dof_index i = 0; i < send_ranks.size(); ++i) |
| 316 |
{ |
| 317 |
const int ierr = |
| 318 |
MPI_Irecv(buffers.data() + send_ptr[i], |
| 319 |
send_ptr[i + 1] - send_ptr[i], |
| 320 |
Utilities::MPI::mpi_type_id_for_type<Number>, |
| 321 |
send_ranks[i], |
| 322 |
tag, |
| 323 |
communicator, |
| 324 |
&requests[i]); |
| 325 |
AssertThrowMPI(ierr); |
| 326 |
} |
| 327 |
|
| 328 |
// pack data and send away |
| 329 |
for (types::global_dof_index i = 0; i < recv_ranks.size(); ++i) |
| 330 |
{ |
| 331 |
AssertIndexRange(i + 1, recv_ptr.size()); |
| 332 |
for (types::global_dof_index j = recv_ptr[i], c = 0; |
| 333 |
j < recv_ptr[i + 1]; |
| 334 |
j++) |
| 335 |
buffers[recv_ptr[i] + c++] = src[recv_indices[j]]; |
| 336 |
|
| 337 |
// send data |
| 338 |
Assert((recv_ptr[i] < buffers.size()) || |
| 339 |
(recv_ptr[i] == buffers.size() && |
| 340 |
recv_ptr[i + 1] == recv_ptr[i]), |
| 341 |
ExcMessage("The input buffer doesn't contain enough entries")); |
| 342 |
const int ierr = |
| 343 |
MPI_Isend(buffers.data() + recv_ptr[i], |
| 344 |
recv_ptr[i + 1] - recv_ptr[i], |
| 345 |
Utilities::MPI::mpi_type_id_for_type<Number>, |
| 346 |
recv_ranks[i], |
| 347 |
tag, |
| 348 |
communicator, |
| 349 |
&requests[i + send_ranks.size()]); |
| 350 |
AssertThrowMPI(ierr); |
| 351 |
} |
| 352 |
#endif |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
template <typename Number> |
| 358 |
void |
| 359 |
NoncontiguousPartitioner::import_from_ghosted_array_finish( |
| 360 |
const VectorOperation::values vector_operation, |
| 361 |
const ArrayView<const Number> &buffers, |
| 362 |
const ArrayView<Number> &dst, |
| 363 |
std::vector<MPI_Request> &requests) const |
| 364 |
{ |
| 365 |
#ifndef DEAL_II_WITH_MPI |
| 366 |
(void)vector_operation; |
| 367 |
(void)buffers; |
| 368 |
(void)dst; |
| 369 |
(void)requests; |
| 370 |
Assert(false, ExcNeedsMPI()); |
| 371 |
#else |
| 372 |
(void)vector_operation; |
| 373 |
Assert(vector_operation == VectorOperation::add, ExcNotImplemented()); |
| 374 |
|
| 375 |
AssertDimension(requests.size(), recv_ranks.size() + send_ranks.size()); |
| 376 |
|
| 377 |
Assert(std::accumulate(dst.begin(), dst.end(), 0) == 0, |
| 378 |
ExcMessage("The destination vector has to be empty.")); |
| 379 |
|
| 380 |
// wait that all data packages have been received |
| 381 |
// note: this for-loop cold be merged with the next for-loop, |
| 382 |
// however, for this send_indices would be needed to stored |
| 383 |
// rank by rank |
| 384 |
for (types::global_dof_index proc = 0; proc < send_ranks.size(); ++proc) |
| 385 |
{ |
| 386 |
int i; |
| 387 |
MPI_Status status; |
| 388 |
const auto ierr = |
| 389 |
MPI_Waitany(send_ranks.size(), requests.data(), &i, &status); |
| 390 |
AssertThrowMPI(ierr); |
| 391 |
} |
| 392 |
|
| 393 |
// write data into destination vector |
| 394 |
for (types::global_dof_index i = 0, k = 0; i < send_ranks.size(); ++i) |
| 395 |
{ |
| 396 |
// collect data to be send |
| 397 |
for (types::global_dof_index j = send_ptr[i]; j < send_ptr[i + 1]; |
| 398 |
j++) |
| 399 |
{ |
| 400 |
AssertIndexRange(k, send_indices.size()); |
| 401 |
dst[send_indices[k]] += buffers[j]; |
| 402 |
++k; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
// wait that all data packages have been sent |
| 407 |
const int ierr = MPI_Waitall(recv_ranks.size(), |
| 408 |
requests.data() + send_ranks.size(), |
| 409 |
MPI_STATUSES_IGNORE); |
| 410 |
AssertThrowMPI(ierr); |
| 411 |
#endif |
| 412 |
} |
| 413 |
|
| 414 |
} // namespace MPI |
| 415 |
} // namespace Utilities |
| 416 |
|
| 417 |
DEAL_II_NAMESPACE_CLOSE |
| 418 |
|
| 419 |
#endif |