From ec0a986bacfa96aff3ec4081328127677c318108 Mon Sep 17 00:00:00 2001 From: Timo Heister Date: Sat, 19 Oct 2019 20:59:53 +0200 Subject: [PATCH] Avoid empty waitall in consensus algorithm - check return values of MPI_ calls - avoid call to MPI_Waitall with potentially 0 requests part of #8929 --- .../base/mpi_compute_index_owner_internal.h | 35 +++++++---- source/base/mpi.cc | 60 ++++++++++++------- 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/include/deal.II/base/mpi_compute_index_owner_internal.h b/include/deal.II/base/mpi_compute_index_owner_internal.h index 00642c81b5..bf246890a3 100644 --- a/include/deal.II/base/mpi_compute_index_owner_internal.h +++ b/include/deal.II/base/mpi_compute_index_owner_internal.h @@ -192,13 +192,13 @@ namespace Utilities for (const auto &rank_pair : buffers) { request.push_back(MPI_Request()); - const auto ierr = MPI_Isend(rank_pair.second.data(), - rank_pair.second.size() * 2, - DEAL_II_DOF_INDEX_MPI_TYPE, - rank_pair.first, - tag_setup, - comm, - &request.back()); + const int ierr = MPI_Isend(rank_pair.second.data(), + rank_pair.second.size() * 2, + DEAL_II_DOF_INDEX_MPI_TYPE, + rank_pair.first, + tag_setup, + comm, + &request.back()); AssertThrowMPI(ierr); } @@ -270,9 +270,14 @@ namespace Utilities ExcInternalError()); // 5) make sure that all messages have been sent - const auto ierr = - MPI_Waitall(request.size(), request.data(), MPI_STATUSES_IGNORE); - AssertThrowMPI(ierr); + if (request.size() > 0) + { + const int ierr = MPI_Waitall(request.size(), + request.data(), + MPI_STATUSES_IGNORE); + AssertThrowMPI(ierr); + } + #else (void)owned_indices; (void)comm; @@ -743,9 +748,13 @@ namespace Utilities } if (send_requests.size() > 0) - MPI_Waitall(send_requests.size(), - send_requests.data(), - MPI_STATUSES_IGNORE); + { + const auto ierr = MPI_Waitall(send_requests.size(), + send_requests.data(), + MPI_STATUSES_IGNORE); + AssertThrowMPI(ierr); + } + # ifdef DEBUG for (const auto &it : requested_indices) diff --git a/source/base/mpi.cc b/source/base/mpi.cc index 4de6ac44ef..d132f50f9c 100644 --- a/source/base/mpi.cc +++ b/source/base/mpi.cc @@ -273,13 +273,13 @@ namespace Utilities AssertThrowMPI(ierr); } - - { - const int ierr = MPI_Waitall(destinations.size(), - send_requests.data(), - MPI_STATUSES_IGNORE); - AssertThrowMPI(ierr); - } + if (destinations.size() > 0) + { + const int ierr = MPI_Waitall(destinations.size(), + send_requests.data(), + MPI_STATUSES_IGNORE); + AssertThrowMPI(ierr); + } return origins; # else @@ -1106,17 +1106,24 @@ namespace Utilities #ifdef DEAL_II_WITH_MPI // clean up { - auto ierr = MPI_Waitall(send_requests.size(), - send_requests.data(), - MPI_STATUSES_IGNORE); - AssertThrowMPI(ierr); + if (send_requests.size() > 0) + { + const int ierr = MPI_Waitall(send_requests.size(), + send_requests.data(), + MPI_STATUSES_IGNORE); + AssertThrowMPI(ierr); + } - ierr = MPI_Waitall(recv_requests.size(), - recv_requests.data(), - MPI_STATUSES_IGNORE); - AssertThrowMPI(ierr); + if (recv_requests.size() > 0) + { + const int ierr = MPI_Waitall(recv_requests.size(), + recv_requests.data(), + MPI_STATUSES_IGNORE); + AssertThrowMPI(ierr); + } - ierr = MPI_Wait(&barrier_request, MPI_STATUS_IGNORE); + + const int ierr = MPI_Wait(&barrier_request, MPI_STATUS_IGNORE); AssertThrowMPI(ierr); for (auto &i : request_requests) @@ -1361,12 +1368,21 @@ namespace Utilities { #ifdef DEAL_II_WITH_MPI // finalize all MPI_Requests - MPI_Waitall(send_and_recv_buffers.size(), - send_and_recv_buffers.data(), - MPI_STATUSES_IGNORE); - MPI_Waitall(requests_answers.size(), - requests_answers.data(), - MPI_STATUSES_IGNORE); + if (send_and_recv_buffers.size() > 0) + { + auto ierr = MPI_Waitall(send_and_recv_buffers.size(), + send_and_recv_buffers.data(), + MPI_STATUSES_IGNORE); + AssertThrowMPI(ierr); + } + + if (requests_answers.size() > 0) + { + auto ierr = MPI_Waitall(requests_answers.size(), + requests_answers.data(), + MPI_STATUSES_IGNORE); + AssertThrowMPI(ierr); + } // unpack received data for (unsigned int i = 0; i < targets.size(); i++) -- 2.39.5