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#endif
33
34#include <stddef.h>
35
36#include <type_traits>
37#include <cassert>
38
39namespace Opm {
40
44template <class DataType>
46{
47public:
49 {
50 data_ = NULL;
51 dataSize_ = 0;
52
53 setMpiDataType_();
54 updateMpiDataSize_();
55 }
56
58 {
59 data_ = new DataType[size];
60 dataSize_ = size;
61
62 setMpiDataType_();
63 updateMpiDataSize_();
64 }
65
66 MpiBuffer(const MpiBuffer&) = default;
67
69 { delete[] data_; }
70
74 void resize(size_t newSize)
75 {
76 delete[] data_;
77 data_ = new DataType[newSize];
78 dataSize_ = newSize;
79 updateMpiDataSize_();
80 }
81
85 void send([[maybe_unused]] unsigned peerRank)
86 {
87#if HAVE_MPI
88 MPI_Isend(data_,
89 static_cast<int>(mpiDataSize_),
90 mpiDataType_,
91 static_cast<int>(peerRank),
92 0, // tag
93 MPI_COMM_WORLD,
94 &mpiRequest_);
95#endif
96 }
97
101 void wait()
102 {
103#if HAVE_MPI
104 MPI_Wait(&mpiRequest_, &mpiStatus_);
105#endif // HAVE_MPI
106 }
107
111 void receive([[maybe_unused]] unsigned peerRank)
112 {
113#if HAVE_MPI
114 MPI_Recv(data_,
115 static_cast<int>(mpiDataSize_),
116 mpiDataType_,
117 static_cast<int>(peerRank),
118 0, // tag
119 MPI_COMM_WORLD,
120 MPI_STATUS_IGNORE);
121#endif // HAVE_MPI
122 }
123
124#if HAVE_MPI
130 MPI_Request& request()
131 { return mpiRequest_; }
137 const MPI_Request& request() const
138 { return mpiRequest_; }
139
145 MPI_Status& status()
146 { return mpiStatus_; }
152 const MPI_Status& status() const
153 { return mpiStatus_; }
154#endif // HAVE_MPI
155
159 size_t size() const
160 { return dataSize_; }
161
165 DataType& operator[](size_t i)
166 {
167 assert(i < dataSize_);
168 return data_[i];
169 }
170
174 const DataType& operator[](size_t i) const
175 {
176 assert(i < dataSize_);
177 return data_[i];
178 }
179
180private:
181 void setMpiDataType_()
182 {
183#if HAVE_MPI
184 // set the MPI data type
185 if (std::is_same<DataType, char>::value)
186 mpiDataType_ = MPI_CHAR;
187 else if (std::is_same<DataType, unsigned char>::value)
188 mpiDataType_ = MPI_UNSIGNED_CHAR;
189 else if (std::is_same<DataType, short>::value)
190 mpiDataType_ = MPI_SHORT;
191 else if (std::is_same<DataType, unsigned short>::value)
192 mpiDataType_ = MPI_UNSIGNED_SHORT;
193 else if (std::is_same<DataType, int>::value)
194 mpiDataType_ = MPI_INT;
195 else if (std::is_same<DataType, unsigned>::value)
196 mpiDataType_ = MPI_UNSIGNED;
197 else if (std::is_same<DataType, long>::value)
198 mpiDataType_ = MPI_LONG;
199 else if (std::is_same<DataType, unsigned long>::value)
200 mpiDataType_ = MPI_UNSIGNED_LONG;
201 else if (std::is_same<DataType, long long>::value)
202 mpiDataType_ = MPI_LONG_LONG;
203 else if (std::is_same<DataType, unsigned long long>::value)
204 mpiDataType_ = MPI_UNSIGNED_LONG_LONG;
205 else if (std::is_same<DataType, float>::value)
206 mpiDataType_ = MPI_FLOAT;
207 else if (std::is_same<DataType, double>::value)
208 mpiDataType_ = MPI_DOUBLE;
209 else if (std::is_same<DataType, long double>::value)
210 mpiDataType_ = MPI_LONG_DOUBLE;
211 else {
212 mpiDataType_ = MPI_BYTE;
213 }
214#endif // HAVE_MPI
215 }
216
217 void updateMpiDataSize_()
218 {
219#if HAVE_MPI
220 mpiDataSize_ = dataSize_;
221 if (mpiDataType_ == MPI_BYTE)
222 mpiDataSize_ *= sizeof(DataType);
223#endif // HAVE_MPI
224 }
225
226 DataType *data_;
227 size_t dataSize_;
228#if HAVE_MPI
229 size_t mpiDataSize_;
230 MPI_Datatype mpiDataType_;
231 MPI_Request mpiRequest_;
232 MPI_Status mpiStatus_;
233#endif // HAVE_MPI
234};
235
236} // namespace Opm
237
238#endif
Simplifies handling of buffers to be used in conjunction with MPI.
Definition: mpibuffer.hh:46
const MPI_Request & request() const
Returns the current MPI_Request object.
Definition: mpibuffer.hh:137
size_t size() const
Returns the number of data objects in the buffer.
Definition: mpibuffer.hh:159
MpiBuffer(size_t size)
Definition: mpibuffer.hh:57
MPI_Status & status()
Returns the current MPI_Status object.
Definition: mpibuffer.hh:145
void resize(size_t newSize)
Set the size of the buffer.
Definition: mpibuffer.hh:74
const DataType & operator[](size_t i) const
Provide access to the buffer data.
Definition: mpibuffer.hh:174
MpiBuffer()
Definition: mpibuffer.hh:48
MpiBuffer(const MpiBuffer &)=default
MPI_Request & request()
Returns the current MPI_Request object.
Definition: mpibuffer.hh:130
~MpiBuffer()
Definition: mpibuffer.hh:68
const MPI_Status & status() const
Returns the current MPI_Status object.
Definition: mpibuffer.hh:152
DataType & operator[](size_t i)
Provide access to the buffer data.
Definition: mpibuffer.hh:165
void wait()
Wait until the buffer was send to the peer completely.
Definition: mpibuffer.hh:101
void send(unsigned peerRank)
Send the buffer asyncronously to a peer process.
Definition: mpibuffer.hh:85
void receive(unsigned peerRank)
Receive the buffer syncronously from a peer rank.
Definition: mpibuffer.hh:111
Definition: blackoilboundaryratevector.hh:37