KaMPIng 0.2.1
(Near) zero-overhead MPI wrapper for C++
Loading...
Searching...
No Matches
scoped_datatype.hpp
Go to the documentation of this file.
1// This file is part of KaMPIng.
2//
3// Copyright 2021-2026 The KaMPIng Authors
4//
5// KaMPIng is free software : you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
7// version. KaMPIng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9// for more details.
10//
11// You should have received a copy of the GNU Lesser General Public License along with KaMPIng. If not, see
12// <https://www.gnu.org/licenses/>.
13
14/// @file
15/// @brief RAII wrapper that commits an \c MPI_Datatype on construction and frees it on destruction.
16
17#pragma once
18#include <utility>
19
20#include <mpi.h>
21
22#include "kamping/kassert/kassert.hpp"
23
24namespace kamping::types {
25
26/// @addtogroup kamping_types
27/// @{
28
29/// @brief RAII wrapper that commits an \c MPI_Datatype on construction and frees it on destruction.
30///
31/// Calls \c MPI_Type_commit / \c MPI_Type_free directly and does not depend on the KaMPIng
32/// environment. Useful for managing custom derived datatypes with automatic lifetime.
34 MPI_Datatype _type; ///< The MPI_Datatype.
35public:
36 /// @brief Construct a new scoped MPI_Datatype and commit it.
37 /// If no type is provided, defaults to `MPI_DATATYPE_NULL` and does not commit or free anything.
39 // FIXME: ensure that we don't commit/free named types
40 if (type != MPI_DATATYPE_NULL) {
41 int const err = MPI_Type_commit(&_type);
42 KAMPING_ASSERT(err == MPI_SUCCESS, "MPI_Type_commit failed");
43 }
44 }
45 /// @brief Deleted copy constructor.
47 /// @brief Deleted copy assignment.
49
50 /// @brief Move constructor.
54 /// @brief Move assignment.
56 std::swap(_type, other._type);
57 return *this;
58 }
59 /// @brief Get the MPI_Datatype.
60 MPI_Datatype const& data_type() const {
61 return _type;
62 }
63 /// @brief Free the MPI_Datatype.
65 if (_type != MPI_DATATYPE_NULL) {
66 int const err = MPI_Type_free(&_type);
67 KAMPING_ASSERT(err == MPI_SUCCESS, "MPI_Type_free failed");
68 }
69 }
70};
71
72/// @}
73
74} // namespace kamping::types
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
RAII wrapper that commits an MPI_Datatype on construction and frees it on destruction.
Definition scoped_datatype.hpp:33
ScopedDatatype(ScopedDatatype const &)=delete
Deleted copy constructor.
ScopedDatatype(ScopedDatatype &&other) noexcept
Move constructor.
Definition scoped_datatype.hpp:51
ScopedDatatype & operator=(ScopedDatatype &&other) noexcept
Move assignment.
Definition scoped_datatype.hpp:55
~ScopedDatatype()
Free the MPI_Datatype.
Definition scoped_datatype.hpp:64
MPI_Datatype const & data_type() const
Get the MPI_Datatype.
Definition scoped_datatype.hpp:60
ScopedDatatype & operator=(ScopedDatatype const &)=delete
Deleted copy assignment.
ScopedDatatype(MPI_Datatype type=MPI_DATATYPE_NULL)
Construct a new scoped MPI_Datatype and commit it. If no type is provided, defaults to MPI_DATATYPE_N...
Definition scoped_datatype.hpp:38