Dune Core Modules (unstable)

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 
16 #include <dune/common/std/type_traits.hh>
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{
120  return MPITraits<typename std::decay_t<T>::value_type>::getType();
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
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
Traits classes for mapping types onto MPI_Datatype.
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)