KaMPIng 0.1.1
Flexible and (near) zero-overhead C++ bindings for MPI
Loading...
Searching...
No Matches
iprobe.hpp
1// This file is part of KaMPIng.
2//
3// Copyright 2023 The KaMPIng Authors
4//
5// KaMPIng is free software : you can redistribute it and/or modify it under the
6// terms of the GNU Lesser General Public License as published by the Free
7// Software Foundation, either version 3 of the License, or (at your option) any
8// later version. KaMPIng is distributed in the hope that it will be useful, but
9// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11// for more details.
12//
13// You should have received a copy of the GNU Lesser General Public License
14// along with KaMPIng. If not, see <https://www.gnu.org/licenses/>.
15
16#pragma once
17
18#include <type_traits>
19
20#include <mpi.h>
21
22#include "kamping/communicator.hpp"
23#include "kamping/implementation_helpers.hpp"
28#include "kamping/result.hpp"
29
30//// @addtogroup kamping_p2p
31/// @{
32
33// @brief Wrapper for \c MPI_Iprobe.
34///
35/// This wraps \c MPI_Iprobe. This operation checks if there is a message matching the (optionally) specified source
36/// and tag that can be received, and returns a \c bool indicating whether a message matched by default.
37/// If the user passes \ref kamping::status_out(), returns a \c std::optional containing an \ref kamping::MPIResult,
38/// which encapsulates a status object. If the probe does not match any message, returns \c std::nullopt.
39///
40/// The following parameters are optional:
41/// - \ref kamping::tag() probe for messages with this tag. Defaults to probing for an arbitrary tag, i.e. \c
42/// tag(tags::any).
43/// - \ref kamping::source() probe for messages sent from this source rank.
44/// Defaults to probing for an arbitrary source, i.e. \c source(rank::any).
45/// - \ref kamping::status() or \ref kamping::status_out(). Returns info about
46/// the probed message by setting the appropriate fields in the status object
47/// passed by the user. If \ref kamping::status_out() is passed, constructs a
48/// status object which may be retrieved by the user.
49/// The status can be ignored by passing \c kamping::status(kamping::ignore<>). This is the default.
50///
51/// @tparam Args Automatically deduced template parameters.
52/// @param args All required and any number of the optional buffers described
53/// above.
54template <
55 template <typename...>
56 typename DefaultContainerType,
57 template <typename, template <typename...> typename>
58 typename... Plugins>
59template <typename... Args>
62
63 using default_source_buf_type = decltype(kamping::source(rank::any));
64
65 auto&& source =
66 internal::select_parameter_type_or_default<internal::ParameterType::source, default_source_buf_type>(
67 {},
68 args...
69 );
70
71 using default_tag_buf_type = decltype(kamping::tag(tags::any));
72
73 auto&& tag_param =
74 internal::select_parameter_type_or_default<internal::ParameterType::tag, default_tag_buf_type>({}, args...);
75 int tag = tag_param.tag();
76
77 constexpr auto tag_type = std::remove_reference_t<decltype(tag_param)>::tag_type;
78 if constexpr (tag_type == internal::TagType::value) {
79 KASSERT(
81 "invalid tag " << tag << ", must be in range [0, " << Environment<>::tag_upper_bound() << "]"
82 );
83 }
84
86
87 auto status =
88 internal::select_parameter_type_or_default<internal::ParameterType::status, default_status_param_type>(
89 {},
90 args...
91 )
92 .construct_buffer_or_rebind();
93
94 KASSERT(internal::is_valid_rank_in_comm(source, *this, true, true), "Invalid source rank.");
95
96 int flag;
97 [[maybe_unused]] int err = MPI_Iprobe(
98 source.rank_signed(), // source
99 tag, // tag
100 this->mpi_communicator(), // comm
101 &flag, // flag
103 );
104 this->mpi_error_hook(err, "MPI_Iprobe");
105
106 // if KaMPIng owns the status (i.e. when the user passed status_out()) we
107 // return an optional, containing the status, otherwise just a bool
108 // indicating probe success.
109 if constexpr (internal::is_extractable<std::remove_reference_t<decltype(status)>>) {
110 if (flag) {
111 return std::optional{internal::make_mpi_result<std::tuple<Args...>>(std::move(status))};
112 } else {
113 return std::optional<decltype(internal::make_mpi_result<std::tuple<Args...>>(std::move(status)))>{};
114 }
115 } else {
116 return static_cast<bool>(flag);
117 }
118}
119/// @}
Wrapper for MPI functions that don't require a communicator. If the template parameter init_finalize_...
Definition environment.hpp:52
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
auto status(internal::ignore_t< void >)
pass MPI_STATUS_IGNORE to the underlying MPI call.
Definition status_parameters.hpp:54
auto tag(internal::any_tag_t)
Indicates to use MPI_ANY_TAG as tag in the underlying call.
Definition named_parameters.hpp:1066
auto source(int rank)
Passes rank as source rank to the underlying call. This parameter is needed in point-to-point exchang...
Definition named_parameters.hpp:1030
auto iprobe(Args... args) const
Definition iprobe.hpp:60
Template magic to check named parameters passed to wrappers at compile time.
#define KAMPING_REQUIRED_PARAMETERS(...)
Wrapper to pass (possibly empty) list of parameter type names as required parameters to KAMPING_CHECK...
Definition named_parameter_check.hpp:52
#define KAMPING_OPTIONAL_PARAMETERS(...)
Wrapper to pass (possibly empty) list of parameter type names as optional parameters to KAMPING_CHECK...
Definition named_parameter_check.hpp:58
#define KAMPING_CHECK_PARAMETERS(args, required, optional)
Assertion macro that checks if passed parameters are correct, i.e., all parameter types are unique,...
Definition named_parameter_check.hpp:80
Template magic to implement named parameters in cpp.
File containing the parameter types used by the KaMPIng library.
Factory methods for buffer wrappers.
constexpr bool is_extractable
Helper for implementing the extract_* functions in MPIResult. Is true if the passed buffer type owns ...
Definition result.hpp:48
@ value
holds an actual value
constexpr bool is_valid_rank_in_comm(RankDataBufferClass const &rank_data_buffer, Comm const &comm, bool const allow_null=false, bool const allow_any=false)
Checks whether a RankDataBuffer contains a valid rank in the given communicator.
Definition implementation_helpers.hpp:30
auto make_mpi_result(Buffers &&... buffers)
Construct result object for a wrapped MPI call. Four different cases are handled: a) The recv_buffer ...
Definition result.hpp:1017
static MPI_Status * status_param_to_native_ptr(StatusParam &param)
returns a pointer to the MPI_Status encapsulated by the provided status parameter object.
Definition parameter_objects.hpp:489
Some functions and types simplifying/enabling the development of wrapped MPI calls in KaMPIng.