Skip to content

Wait_any: Gracefully handle no active requests #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions include/boost/mpi/nonblocking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ namespace boost { namespace mpi {
*
* @returns A pair containing the status object that corresponds to
* the completed operation and the iterator referencing the completed
* request.
* request. If there are no active handles (distance(first, last) == 0,
* all requests already done or null requests), returns an empty
* status and iterator "last".
*/
template<typename ForwardIterator>
std::pair<status, ForwardIterator>
Expand All @@ -56,13 +58,16 @@ wait_any(ForwardIterator first, ForwardIterator last)

bool all_trivial_requests = true;
difference_type n = 0;
bool has_outstanding_request = false;
ForwardIterator current = first;
while (true) {
// Check if we have found a completed request. If so, return it.
if (current->active()) {
optional<status> result = current->test();
if (bool(result)) {
return std::make_pair(*result, current);
} else {
has_outstanding_request = true;
}
}

Expand Down Expand Up @@ -93,23 +98,26 @@ wait_any(ForwardIterator first, ForwardIterator last)
BOOST_MPI_CHECK_RESULT(MPI_Waitany,
(n, detail::c_data(requests), &index, &stat.m_status));

// We don't have a notion of empty requests or status objects,
// so this is an error.
// No active handles
if (index == MPI_UNDEFINED)
boost::throw_exception(exception("MPI_Waitany", MPI_ERR_REQUEST));
return std::make_pair(stat, last);

// Find the iterator corresponding to the completed request.
current = first;
advance(current, index);
*current->trivial() = requests[index];
return std::make_pair(stat, current);
} else if (!has_outstanding_request) {
// No active handles
return std::make_pair(status{}, last);
}

// There are some nontrivial requests, so we must continue our
// busy waiting loop.
n = 0;
current = first;
all_trivial_requests = true;
has_outstanding_request = false;
}
}

Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ test-suite mpi
[ mpi-test ring_test : : : 2 3 4 7 8 13 17 ]
[ mpi-test sendrecv_test : : : 1 4 7 48 ]
[ mpi-test wait_any_test : : : 1 4 7 20 ]
[ mpi-test wait_any_on_null : : : 1 2 ]
[ mpi-test wait_all_vector_test : : : 2 ]
[ mpi-test wait_all_on_null : : : 1 2 ]
[ mpi-test scan_test ]
Expand Down
64 changes: 64 additions & 0 deletions test/wait_any_on_null.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (C) 2021 Steffen Hirschmann

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/mpi.hpp>
#include <boost/mpi/nonblocking.hpp>
#include <vector>

#define BOOST_TEST_NO_MAIN
#define BOOST_TEST_ALTERNATIVE_INIT_API
#define BOOST_TEST_MODULE mpi_wait_on_null
#include <boost/test/included/unit_test.hpp>

/** Check default constructed requests.
* Must not deadlock.
*/
BOOST_AUTO_TEST_CASE(wait_any_default_constructed_request)
{
std::vector<boost::mpi::request> req(1);
boost::mpi::status s;
decltype(req)::iterator it;
std::tie(s, it) = boost::mpi::wait_any(req.begin(), req.end());
BOOST_CHECK(it == req.end());
}

/** Check a trivial request twice.
*/
BOOST_AUTO_TEST_CASE(wait_any_all_trivial_and_done)
{
boost::mpi::communicator comm;
std::vector<boost::mpi::request> req;
int dummy1 = 1, dummy2 = 0;

req.push_back(comm.irecv(comm.rank(), 0, dummy2));
comm.isend(comm.rank(), 0, dummy1);

boost::mpi::status s;
decltype(req)::iterator it;
// All trivial requests
std::tie(s, it) = boost::mpi::wait_any(req.begin(), req.end());
BOOST_CHECK(it != req.end());
BOOST_CHECK(it == req.begin());
BOOST_CHECK(s.count<int>());
BOOST_CHECK(*s.count<int>() == 1);

// Call a second time
std::tie(s, it) = boost::mpi::wait_any(req.begin(), req.end());
BOOST_CHECK(it == req.end());
BOOST_CHECK(s.count<int>());
// empty status according to MPI 3.1 §3.7.3, l.39 (p. 52)
BOOST_CHECK(*s.count<int>() == 0);
BOOST_CHECK(!s.cancelled());
BOOST_CHECK(s.source() == MPI_ANY_SOURCE);
BOOST_CHECK(s.tag() == MPI_ANY_TAG);
BOOST_CHECK(s.error() == MPI_SUCCESS);
}

int main(int argc, char **argv)
{
boost::mpi::environment env(argc, argv);
return boost::unit_test::unit_test_main(init_unit_test, argc, argv);
}