1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_PARALLEL_MPIFUTURE_HH
#define DUNE_COMMON_PARALLEL_MPIFUTURE_HH
#if HAVE_MPI
#include <functional>
#include <memory>
#include <optional>
#include <type_traits>
#include <utility>
#include <mpi.h>
#include <dune/common/exceptions.hh>
#include <dune/common/parallel/communication.hh>
#include <dune/common/parallel/future.hh>
#include <dune/common/parallel/mpidata.hh>
namespace Dune{
namespace Impl{
template<class T>
struct Buffer{
Buffer(bool valid){
if(valid)
value = std::make_unique<T>();
}
template<class V>
Buffer(V&& t)
: value(std::make_unique<T>(std::forward<V>(t)))
{}
std::unique_ptr<T> value;
T get(){
T tmp = std::move(*value);
value.reset();
return tmp;
}
operator bool () const {
return (bool)value;
}
T& operator *() const{
return *value;
}
};
template<class T>
struct Buffer<T&>{
Buffer(bool valid = false)
{
if(valid)
value = T();
}
template<class V>
Buffer(V&& t)
: value(std::forward<V>(t))
{}
std::optional<std::reference_wrapper<T>> value;
T& get(){
T& tmp = *value;
value.reset();
return tmp;
}
operator bool () const{
return (bool)value;
}
T& operator *() const{
return *value;
}
};
template<>
struct Buffer<void>{
bool valid_;
Buffer(bool valid = false)
: valid_(valid)
{}
operator bool () const{
return valid_;
}
void get(){}
};
}
/*! \brief Provides a future-like object for MPI communication. It contains
the object that will be received and might contain also a sending object,
which must be hold (keep alive) until the communication has been completed.
*/
template<class R, class S = void>
class MPIFuture{
mutable MPI_Request req_;
mutable MPI_Status status_;
Impl::Buffer<R> data_;
Impl::Buffer<S> send_data_;
friend class Communication<MPI_Comm>;
public:
MPIFuture(bool valid = false)
: req_(MPI_REQUEST_NULL)
, data_(valid)
{}
// Hide this constructor if R or S is void
template<class V = R, class U = S>
MPIFuture(V&& recv_data, U&& send_data, typename std::enable_if_t<!std::is_void<V>::value && !std::is_void<U>::value>* = 0) :
req_(MPI_REQUEST_NULL)
, data_(std::forward<R>(recv_data))
, send_data_(std::forward<S>(send_data))
{}
// hide this constructor if R is void
template<class V = R>
MPIFuture(V&& recv_data, typename std::enable_if_t<!std::is_void<V>::value>* = 0)
: req_(MPI_REQUEST_NULL)
, data_(std::forward<V>(recv_data))
{}
~MPIFuture() {
if(req_ != MPI_REQUEST_NULL){
try{ // might fail when it is a collective communication
MPI_Cancel(&req_);
MPI_Request_free(&req_);
}catch(...){
}
}
}
MPIFuture(MPIFuture&& f)
: req_(MPI_REQUEST_NULL)
, data_(std::move(f.data_))
, send_data_(std::move(f.send_data_))
{
std::swap(req_, f.req_);
std::swap(status_, f.status_);
}
MPIFuture& operator=(MPIFuture&& f){
std::swap(req_, f.req_);
std::swap(status_, f.status_);
std::swap(data_, f.data_);
std::swap(send_data_, f.send_data_);
return *this;
}
bool valid() const{
return (bool)data_;
}
void wait(){
if(!valid())
DUNE_THROW(InvalidFutureException, "The MPIFuture is not valid!");
MPI_Wait(&req_, &status_);
}
bool ready() const{
int flag = -1;
MPI_Test(&req_, &flag, &status_);
return flag;
}
R get() {
wait();
return data_.get();
}
S get_send_data(){
wait();
return send_data_.get();
}
auto get_mpidata(){
return getMPIData(*data_);
}
auto get_send_mpidata(){
return getMPIData(*send_data_);
}
};
}
#endif // HAVE_MPI
#endif // DUNE_COMMON_PARALLEL_MPIFUTURE_HH
|