My Project
Loading...
Searching...
No Matches
mpibuffer.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
27#ifndef EWOMS_MPI_BUFFER_HH
28#define EWOMS_MPI_BUFFER_HH
29
30#if HAVE_MPI
31#include <mpi.h>
32#include <type_traits>
33#endif
34
35#include <cassert>
36#include <cstddef>
37#include <vector>
38
39namespace Opm {
40
44template <class DataType>
46{
47public:
48 MpiBuffer()
49 {
50 setMpiDataType_();
51 updateMpiDataSize_();
52 }
53
54 explicit MpiBuffer(std::size_t size)
55 {
56 data_.resize(size);
57
58 setMpiDataType_();
59 updateMpiDataSize_();
60 }
61
65 void resize(std::size_t newSize)
66 {
67 data_.resize(newSize);
68 updateMpiDataSize_();
69 }
70
74 void send([[maybe_unused]] unsigned peerRank)
75 {
76#if HAVE_MPI
77 MPI_Isend(data_.data(),
78 static_cast<int>(mpiDataSize_),
80 static_cast<int>(peerRank),
81 0, // tag
84#endif
85 }
86
90 void wait()
91 {
92#if HAVE_MPI
94#endif // HAVE_MPI
95 }
96
100 void receive([[maybe_unused]] unsigned peerRank)
101 {
102#if HAVE_MPI
103 MPI_Recv(data_.data(),
104 static_cast<int>(mpiDataSize_),
106 static_cast<int>(peerRank),
107 0, // tag
110#endif // HAVE_MPI
111 }
112
113#if HAVE_MPI
120 { return mpiRequest_; }
126 const MPI_Request& request() const
127 { return mpiRequest_; }
128
134 MPI_Status& status()
135 { return mpiStatus_; }
141 const MPI_Status& status() const
142 { return mpiStatus_; }
143#endif // HAVE_MPI
144
148 std::size_t size() const
149 { return data_.size(); }
150
154 DataType& operator[](std::size_t i)
155 {
156 assert(i < data_.size());
157 return data_[i];
158 }
159
163 const DataType& operator[](std::size_t i) const
164 {
165 assert(i < data_.size());
166 return data_[i];
167 }
168
169private:
170 void setMpiDataType_()
171 {
172#if HAVE_MPI
173 // set the MPI data type
174 if (std::is_same_v<DataType, char>)
176 else if (std::is_same_v<DataType, unsigned char>)
178 else if (std::is_same_v<DataType, short>)
180 else if (std::is_same_v<DataType, unsigned short>)
182 else if (std::is_same_v<DataType, int>)
184 else if (std::is_same_v<DataType, unsigned>)
186 else if (std::is_same_v<DataType, long>)
188 else if (std::is_same_v<DataType, unsigned long>)
190 else if (std::is_same_v<DataType, long long>)
192 else if (std::is_same_v<DataType, unsigned long long>)
194 else if (std::is_same_v<DataType, float>)
196 else if (std::is_same_v<DataType, double>)
198 else if (std::is_same_v<DataType, long double>)
200 else {
202 }
203#endif // HAVE_MPI
204 }
205
206 void updateMpiDataSize_()
207 {
208#if HAVE_MPI
209 mpiDataSize_ = data_.size();
210 if (mpiDataType_ == MPI_BYTE)
211 mpiDataSize_ *= sizeof(DataType);
212#endif // HAVE_MPI
213 }
214
215 std::vector<DataType> data_;
216#if HAVE_MPI
217 std::size_t mpiDataSize_;
221#endif // HAVE_MPI
222};
223
224} // namespace Opm
225
226#endif
Simplifies handling of buffers to be used in conjunction with MPI.
Definition mpibuffer.hh:46
DataType & operator[](std::size_t i)
Provide access to the buffer data.
Definition mpibuffer.hh:154
const DataType & operator[](std::size_t i) const
Provide access to the buffer data.
Definition mpibuffer.hh:163
void wait()
Wait until the buffer was send to the peer completely.
Definition mpibuffer.hh:90
void resize(std::size_t newSize)
Set the size of the buffer.
Definition mpibuffer.hh:65
void send(unsigned peerRank)
Send the buffer asyncronously to a peer process.
Definition mpibuffer.hh:74
std::size_t size() const
Returns the number of data objects in the buffer.
Definition mpibuffer.hh:148
void receive(unsigned peerRank)
Receive the buffer syncronously from a peer rank.
Definition mpibuffer.hh:100
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilboundaryratevector.hh:37
constexpr auto getPropValue()
get the value data member of a property
Definition propertysystem.hh:242