KaMPIng 0.1.2
Flexible and (near) zero-overhead C++ bindings for MPI
Loading...
Searching...
No Matches
send.hpp
1// This file is part of KaMPIng.
2//
3// Copyright 2022-2024 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 <kassert/kassert.hpp>
21#include <mpi.h>
22
23#include "kamping/communicator.hpp"
24#include "kamping/implementation_helpers.hpp"
29#include "kamping/p2p/helpers.hpp"
31
32///// @addtogroup kamping_p2p
33/// @{
34
35// @brief Wrapper for \c MPI_Send.
36///
37/// This wraps \c MPI_Send. This operation sends the elements in the input buffer provided via \c
38/// kamping::send_buf() to the specified receiver rank using standard send mode.
39/// The following parameters are required:
40/// - \ref kamping::send_buf() containing the data that is sent.
41///
42/// - \ref kamping::send_count() specifying how many elements of the buffer are sent. If omitted, the size of the send
43/// buffer is used as a default. This parameter is mandatory if \ref kamping::send_type() is given.
44///
45// - \ref kamping::send_type() specifying the \c MPI datatype to use as send type. If omitted, the \c MPI datatype is
46/// derived automatically based on send_buf's underlying \c value_type.
47///
48/// - \ref kamping::destination() the receiving rank.
49///
50/// The following parameters are optional:
51/// - \ref kamping::tag() the tag added to the message. Defaults to the communicator's default tag (\ref
52/// Communicator::default_tag()) if not present.
53///
54/// @tparam Args Automatically deduced template parameters.
55/// @param args All required and any number of the optional parameters described above.
56///
57/// @see \ref docs/parameter_handling.md for general information about parameter handling in KaMPIng.
58/// <hr>
59/// \include{doc} docs/resize_policy.dox
60template <
61 template <typename...>
62 typename DefaultContainerType,
63 template <typename, template <typename...> typename>
64 typename... Plugins>
65template <typename... Args>
67 using namespace kamping::internal;
69 Args,
72 );
73
74 auto send_buf = internal::select_parameter_type<internal::ParameterType::send_buf>(args...)
77 if constexpr (is_serialization_used) {
80 send_buf.underlying().serialize();
81 }
82 using send_value_type = typename std::remove_reference_t<decltype(send_buf)>::value_type;
83
84 auto send_type = internal::determine_mpi_send_datatype<send_value_type>(args...);
85
87 auto send_count =
88 internal::select_parameter_type_or_default<internal::ParameterType::send_count, default_send_count_type>(
89 {},
90 args...
91 )
92 .construct_buffer_or_rebind();
93 if constexpr (has_to_be_computed<decltype(send_count)>) {
94 send_count.underlying() = asserting_cast<int>(send_buf.size());
95 }
96
97 auto const& destination = internal::select_parameter_type<internal::ParameterType::destination>(args...);
98 constexpr auto rank_type = std::remove_reference_t<decltype(destination)>::rank_type;
99 static_assert(
100 rank_type == RankType::value || rank_type == RankType::null,
101 "Please provide an explicit destination or destination(ranks::null)."
102 );
103
104 using default_tag_buf_type = decltype(kamping::tag(this->default_tag()));
105
106 auto&& tag_param = internal::select_parameter_type_or_default<internal::ParameterType::tag, default_tag_buf_type>(
107 std::tuple(this->default_tag()),
108 args...
109 );
110
111 // this ensures that the user does not try to pass MPI_ANY_TAG, which is not allowed for sends
112 static_assert(
113 std::remove_reference_t<decltype(tag_param)>::tag_type == TagType::value,
114 "Please provide a tag for the message."
115 );
116 int tag = tag_param.tag();
117 KASSERT(
119 "invalid tag " << tag << ", must be in range [0, " << Environment<>::tag_upper_bound() << "]"
120 );
121
122 using send_mode_obj_type =
126 using send_mode = typename std::remove_reference_t<send_mode_obj_type>::send_mode;
127
128 // RankType::null is valid, RankType::any is not.
129 KASSERT(is_valid_rank_in_comm(destination, *this, true, false), "Invalid destination rank.");
130
131 if constexpr (std::is_same_v<send_mode, internal::standard_mode_t>) {
132 [[maybe_unused]] int err = MPI_Send(
133 send_buf.data(), // send_buf
134 send_count.get_single_element(), // send_count
135 send_type.get_single_element(), // send_type
136 destination.rank_signed(), // destination
137 tag, // tag
138 this->mpi_communicator()
139 );
140 this->mpi_error_hook(err, "MPI_Send");
141 } else if constexpr (std::is_same_v<send_mode, internal::buffered_mode_t>) {
142 [[maybe_unused]] int err = MPI_Bsend(
143 send_buf.data(), // send_buf
144 send_count.get_single_element(), // send_count
145 send_type.get_single_element(), // send_type
146 destination.rank_signed(), // destination
147 tag, // tag
148 this->mpi_communicator()
149 );
150 this->mpi_error_hook(err, "MPI_Bsend");
151 } else if constexpr (std::is_same_v<send_mode, internal::synchronous_mode_t>) {
152 [[maybe_unused]] int err = MPI_Ssend(
153 send_buf.data(), // send_buf
154 send_count.get_single_element(), // send_count
155 send_type.get_single_element(), // send_type
156 destination.rank_signed(), // destination
157 tag, // tag
158 this->mpi_communicator()
159 );
160 this->mpi_error_hook(err, "MPI_Ssend");
161 } else if constexpr (std::is_same_v<send_mode, internal::ready_mode_t>) {
162 [[maybe_unused]] int err = MPI_Rsend(
163 send_buf.data(), // send_buf
164 send_count.get_single_element(), // send_count
165 send_type.get_single_element(), // send_type
166 destination.rank_signed(), // destination
167 tag, // tag
168 this->mpi_communicator()
169 );
170 this->mpi_error_hook(err, "MPI_Rsend");
171 }
172}
173
174/// @brief Convenience wrapper for MPI_Bsend. Calls \ref kamping::Communicator::send() with the appropriate send mode
175/// set.
176template <
177 template <typename...>
178 typename DefaultContainerType,
179 template <typename, template <typename...> typename>
180 typename... Plugins>
181template <typename... Args>
183 this->send(std::forward<Args>(args)..., send_mode(send_modes::buffered));
184}
185
186/// @brief Convenience wrapper for MPI_Ssend. Calls \ref kamping::Communicator::send() with the appropriate send mode
187/// set.
188template <
189 template <typename...>
190 typename DefaultContainerType,
191 template <typename, template <typename...> typename>
192 typename... Plugins>
193template <typename... Args>
195 this->send(std::forward<Args>(args)..., send_mode(send_modes::synchronous));
196}
197
198/// @brief Convenience wrapper for MPI_Rsend. Calls \ref kamping::Communicator::send() with the appropriate send mode
199/// set.
200template <
201 template <typename...>
202 typename DefaultContainerType,
203 template <typename, template <typename...> typename>
204 typename... Plugins>
205template <typename... Args>
207 this->send(std::forward<Args>(args)..., send_mode(send_modes::ready));
208}
209/// @}
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
T value_type
The value type.
Definition allocator.hpp:53
auto send_mode(SendModeTag)
Passes the send mode parameter for point to point communication to the underlying call....
Definition named_parameters.hpp:1201
auto tag(internal::any_tag_t)
Indicates to use MPI_ANY_TAG as tag in the underlying call.
Definition named_parameters.hpp:1066
auto send_count(int count)
Passes count as send count to the underlying call.
Definition named_parameters.hpp:323
auto destination(int rank)
Passes rank as destination rank to the underlying call. This parameter is needed in point-to-point ex...
Definition named_parameters.hpp:1001
auto send_type(MPI_Datatype send_type)
Passes send_type as send type to the underlying call.
Definition named_parameters.hpp:1259
auto send_count_out()
Indicates to deduce the send count and return it to the caller as part of the underlying call's resul...
Definition named_parameters.hpp:349
auto send_buf(internal::ignore_t< Data > ignore)
Generates a dummy send buf that wraps a nullptr.
Definition named_parameters.hpp:53
void bsend(Args... args) const
Convenience wrapper for MPI_Bsend. Calls kamping::Communicator::send() with the appropriate send mode...
Definition send.hpp:182
void send(Args... args) const
Definition send.hpp:66
void rsend(Args... args) const
Convenience wrapper for MPI_Rsend. Calls kamping::Communicator::send() with the appropriate send mode...
Definition send.hpp:206
void ssend(Args... args) const
Convenience wrapper for MPI_Ssend. Calls kamping::Communicator::send() with the appropriate send mode...
Definition send.hpp:194
decltype(auto) select_parameter_type_or_default(std::tuple< DefaultArguments... > default_arguments, Args &... args)
Checks if parameter with requested parameter type exists, if not constructs a default value.
Definition named_parameter_selection.hpp:239
@ send_mode
Tag used to represent the send mode used by a send operation.
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.
Internal namespace marking the code that is not user-facing.
Definition collectives_helpers.hpp:20
static constexpr bool buffer_uses_serialization
Checks if DataBufferType is a serialization buffer.
Definition named_parameter_check.hpp:415
Parameter objects return by named parameter factory functions.
Parameter object for send_mode encapsulating the send mode compile-time tag.
Definition parameter_objects.hpp:478