Dune Core Modules (2.9.1)

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
15#include <dune/common/std/type_traits.hh>
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:
53 T& data_;
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:
80 MPIData() {}
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&);
107 MPIData(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{
119 return MPITraits<typename std::decay_t<T>::value_type>::getType();
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:
130 T& data_;
131 };
132
133}
134
139#endif
140#endif
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
STL namespace.
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)