dune-common 2.9.0
Loading...
Searching...
No Matches
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 (C) 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#include <vector>
10#include <string>
11
12#if HAVE_MPI
13
17
37namespace Dune{
38
39 template<class, class = void>
40 struct MPIData;
41
42 template<class T>
43 auto getMPIData(T& t){
44 return MPIData<T>(t);
45 }
46
47 // Default implementation for static datatypes
48 template<class T, class Enable>
49 struct MPIData
50 {
51 friend auto getMPIData<T>(T&);
52 protected:
54
55 MPIData(T& t)
56 : data_(t)
57 {}
58
59 public:
60 void* ptr() const {
61 return (void*)&data_;
62 }
63
64 // indicates whether the datatype can be resized
65 static constexpr bool static_size = true;
66
67 int size() const{
68 return 1;
69 }
70
71 MPI_Datatype type() const {
72 return MPITraits<std::decay_t<T>>::getType();
73 }
74 };
75
76 // dummy implementation for void
77 template<>
78 struct MPIData<void>{
79 protected:
81
82 public:
83 void* ptr(){
84 return nullptr;
85 }
86 int size(){
87 return 0;
88 }
89 void get(){}
90 MPI_Datatype type() const{
91 return MPI_INT;
92 }
93 };
94
95 // specializations:
96 // std::vector of static sized elements or std::string
97 template<class T>
98 struct MPIData<T, std::void_t<std::tuple<decltype(std::declval<T>().data()),
99 decltype(std::declval<T>().size()),
100 typename std::decay_t<T>::value_type>>>{
101 private:
102 template<class U>
103 using hasResizeOp = decltype(std::declval<U>().resize(0));
104
105 protected:
106 friend auto getMPIData<T>(T&);
108 : data_(t)
109 {}
110 public:
111 static constexpr bool static_size = std::is_const<T>::value || !Std::is_detected_v<hasResizeOp, T>;
112 void* ptr() {
113 return (void*) data_.data();
114 }
115 int size() {
116 return data_.size();
117 }
118 MPI_Datatype type() const{
120 }
121
122 template<class S = T>
123 auto /*void*/ resize(int size)
124 -> std::enable_if_t<!std::is_const<S>::value || !Std::is_detected_v<hasResizeOp, S>>
125 {
126 data_.resize(size);
127 }
128
129 protected:
131 };
132
133}
134
139#endif
140#endif
Traits for type conversions and type information.
Traits classes for mapping types onto MPI_Datatype.
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
STL namespace.
Dune namespace.
Definition alignedallocator.hh:13
auto getMPIData(T &t)
Definition mpidata.hh:43
A traits class describing the mapping of types onto MPI_Datatypes.
Definition mpitraits.hh:41
Definition mpidata.hh:50
T & data_
Definition mpidata.hh:53
MPI_Datatype type() const
Definition mpidata.hh:71
void * ptr() const
Definition mpidata.hh:60
int size() const
Definition mpidata.hh:67
static constexpr bool static_size
Definition mpidata.hh:65
MPIData(T &t)
Definition mpidata.hh:55
void get()
Definition mpidata.hh:89
MPIData()
Definition mpidata.hh:80
int size()
Definition mpidata.hh:86
void * ptr()
Definition mpidata.hh:83
MPI_Datatype type() const
Definition mpidata.hh:90
auto resize(int size) -> std::enable_if_t<!std::is_const< S >::value||!Std::is_detected_v< hasResizeOp, S > >
Definition mpidata.hh:123