dune-common  2.11
mpidata.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 
6 #ifndef DUNE_COMMON_PARALLEL_MPIDATA_HH
7 #define DUNE_COMMON_PARALLEL_MPIDATA_HH
8 
9 #if HAVE_MPI
10 
11 #include <vector>
12 #include <string>
13 #include <type_traits>
14 
18 
38 namespace Dune{
39 
40  template<class, class = void>
41  struct MPIData;
42 
43  template<class T>
44  auto getMPIData(T& t){
45  return MPIData<T>(t);
46  }
47 
48  // Default implementation for static datatypes
49  template<class T, class Enable>
50  struct MPIData
51  {
52  friend auto getMPIData<T>(T&);
53  protected:
54  T& data_;
55 
56  MPIData(T& t)
57  : data_(t)
58  {}
59 
60  public:
61  void* ptr() const {
62  return (void*)&data_;
63  }
64 
65  // indicates whether the datatype can be resized
66  static constexpr bool static_size = true;
67 
68  int size() const{
69  return 1;
70  }
71 
72  MPI_Datatype type() const {
73  return MPITraits<std::decay_t<T>>::getType();
74  }
75  };
76 
77  // dummy implementation for void
78  template<>
79  struct MPIData<void>{
80  protected:
81  MPIData() {}
82 
83  public:
84  void* ptr(){
85  return nullptr;
86  }
87  int size(){
88  return 0;
89  }
90  void get(){}
91  MPI_Datatype type() const{
92  return MPI_INT;
93  }
94  };
95 
96  // specializations:
97  // std::vector of static sized elements or std::string
98  template<class T>
99  struct MPIData<T, std::void_t<std::tuple<decltype(std::declval<T>().data()),
100  decltype(std::declval<T>().size()),
101  typename std::decay_t<T>::value_type>>>{
102  private:
103  template<class U>
104  using hasResizeOp = decltype(std::declval<U>().resize(0));
105 
106  protected:
107  friend auto getMPIData<T>(T&);
108  MPIData(T& t)
109  : data_(t)
110  {}
111  public:
112  static constexpr bool static_size = std::is_const<T>::value || !Std::is_detected_v<hasResizeOp, T>;
113  void* ptr() {
114  return (void*) data_.data();
115  }
116  int size() {
117  return data_.size();
118  }
119  MPI_Datatype type() const{
121  }
122 
123  template<class S = T>
124  auto /*void*/ resize(int size)
125  -> std::enable_if_t<!std::is_const<S>::value || !Std::is_detected_v<hasResizeOp, S>>
126  {
127  data_.resize(size);
128  }
129 
130  protected:
131  T& data_;
132  };
133 
134 }
135 
140 #endif // HAVE_MPI
141 #endif // DUNE_COMMON_PARALLEL_MPIDATA_HH
void * ptr() const
Definition: mpidata.hh:61
auto getMPIData(T &t)
Definition: mpidata.hh:44
int size()
Definition: mpidata.hh:87
MPIData(T &t)
Definition: mpidata.hh:56
T & data_
Definition: mpidata.hh:54
MPIData()
Definition: mpidata.hh:81
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:40
Definition: mpidata.hh:41
static constexpr bool static_size
Definition: mpidata.hh:66
Dune namespace
Definition: alignedallocator.hh:12
int size() const
Definition: mpidata.hh:68
MPI_Datatype type() const
Definition: mpidata.hh:91
void * ptr()
Definition: mpidata.hh:84
Traits classes for mapping types onto MPI_Datatype.
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: bigunsignedint.hh:29
MPI_Datatype type() const
Definition: mpidata.hh:72
STL namespace.
Traits for type conversions and type information.
auto resize(int size) -> std::enable_if_t<!std::is_const< S >::value||!Std::is_detected_v< hasResizeOp, S >>
Definition: mpidata.hh:124