Dune Core Modules (2.8.0)

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
4#ifndef DUNE_COMMON_PARALLEL_MPIDATA_HH
5#define DUNE_COMMON_PARALLEL_MPIDATA_HH
6
7#include <vector>
8#include <string>
9
10#if HAVE_MPI
11
13#include <dune/common/std/type_traits.hh>
15
35namespace Dune{
36
37 template<class, class = void>
38 struct MPIData;
39
40 template<class T>
41 auto getMPIData(T& t){
42 return MPIData<T>(t);
43 }
44
45 // Default implementation for static datatypes
46 template<class T, class Enable>
47 struct MPIData
48 {
49 friend auto getMPIData<T>(T&);
50 protected:
51 T& data_;
52
53 MPIData(T& t)
54 : data_(t)
55 {}
56
57 public:
58 void* ptr() const {
59 return (void*)&data_;
60 }
61
62 // indicates whether the datatype can be resized
63 static constexpr bool static_size = true;
64
65 int size() const{
66 return 1;
67 }
68
69 MPI_Datatype type() const {
70 return MPITraits<std::decay_t<T>>::getType();
71 }
72 };
73
74 // dummy implementation for void
75 template<>
76 struct MPIData<void>{
77 protected:
78 MPIData() {}
79
80 public:
81 void* ptr(){
82 return nullptr;
83 }
84 int size(){
85 return 0;
86 }
87 void get(){}
88 MPI_Datatype type() const{
89 return MPI_INT;
90 }
91 };
92
93 // specializations:
94 // std::vector of static sized elements or std::string
95 template<class T>
96 struct MPIData<T, std::void_t<std::tuple<decltype(std::declval<T>().data()),
97 decltype(std::declval<T>().size()),
98 typename std::decay_t<T>::value_type>>>{
99 private:
100 template<class U>
101 using hasResizeOp = decltype(std::declval<U>().resize(0));
102
103 protected:
104 friend auto getMPIData<T>(T&);
105 MPIData(T& t)
106 : data_(t)
107 {}
108 public:
109 static constexpr bool static_size = std::is_const<T>::value || !Std::is_detected_v<hasResizeOp, T>;
110 void* ptr() {
111 return (void*) data_.data();
112 }
113 int size() {
114 return data_.size();
115 }
116 MPI_Datatype type() const{
117 return MPITraits<typename std::decay_t<T>::value_type>::getType();
118 }
119
120 template<class S = T>
121 auto /*void*/ resize(int size)
122 -> std::enable_if_t<!std::is_const<S>::value || !Std::is_detected_v<hasResizeOp, S>>
123 {
124 data_.resize(size);
125 }
126
127 protected:
128 T& data_;
129 };
130
131}
132
137#endif
138#endif
Traits for type conversions and type information.
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:38
Traits classes for mapping types onto MPI_Datatype.
Dune namespace.
Definition: alignedallocator.hh:11
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)