Dune Core Modules (2.4.2)

mpitraits.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#ifndef DUNE_MPITRAITS_HH
4#define DUNE_MPITRAITS_HH
5
6#if HAVE_MPI
7#include <mpi.h>
8#endif
9
10#include <cstdint>
11#include <utility>
12
13namespace Dune
14{
25#if HAVE_MPI
35 template<typename T>
36 struct MPITraits
37 {
38 private:
39 MPITraits(){}
40 MPITraits(const MPITraits&){}
41 static MPI_Datatype datatype;
42 static MPI_Datatype vectortype;
43 public:
44 static inline MPI_Datatype getType()
45 {
46 if(datatype==MPI_DATATYPE_NULL) {
47 MPI_Type_contiguous(sizeof(T),MPI_BYTE,&datatype);
48 MPI_Type_commit(&datatype);
49 }
50 return datatype;
51 }
52
53 };
54 template<class T>
55 MPI_Datatype MPITraits<T>::datatype = MPI_DATATYPE_NULL;
56
57#ifndef DOXYGEN
58#if HAVE_MPI
59
60 // A Macro for defining traits for the primitive data types
61#define ComposeMPITraits(p,m) \
62 template<> \
63 struct MPITraits<p>{ \
64 static inline MPI_Datatype getType(){ \
65 return m; \
66 } \
67 }
68
69 ComposeMPITraits(char, MPI_CHAR);
70 ComposeMPITraits(unsigned char,MPI_UNSIGNED_CHAR);
71 ComposeMPITraits(short,MPI_SHORT);
72 ComposeMPITraits(unsigned short,MPI_UNSIGNED_SHORT);
73 ComposeMPITraits(int,MPI_INT);
74 ComposeMPITraits(unsigned int,MPI_UNSIGNED);
75 ComposeMPITraits(long,MPI_LONG);
76 ComposeMPITraits(unsigned long,MPI_UNSIGNED_LONG);
77 ComposeMPITraits(float,MPI_FLOAT);
78 ComposeMPITraits(double,MPI_DOUBLE);
79 ComposeMPITraits(long double,MPI_LONG_DOUBLE);
80
81
82#undef ComposeMPITraits
83
84 template<class K, int n> class FieldVector;
85
86 template<class K, int n>
87 struct MPITraits<FieldVector<K,n> >
88 {
89 static MPI_Datatype datatype;
90 static MPI_Datatype vectortype;
91
92 static inline MPI_Datatype getType()
93 {
94 if(datatype==MPI_DATATYPE_NULL) {
95 MPI_Type_contiguous(n, MPITraits<K>::getType(), &vectortype);
96 MPI_Type_commit(&vectortype);
97 FieldVector<K,n> fvector;
98 MPI_Aint base;
99 MPI_Aint displ;
100 MPI_Get_address(&fvector, &base);
101 MPI_Get_address(&(fvector[0]), &displ);
102 displ -= base;
103 int length[1]={1};
104
105 MPI_Type_create_struct(1, length, &displ, &vectortype, &datatype);
106 MPI_Type_commit(&datatype);
107 }
108 return datatype;
109 }
110
111 };
112
113 template<class K, int n>
114 MPI_Datatype MPITraits<FieldVector<K,n> >::datatype = MPI_DATATYPE_NULL;
115 template<class K, int n>
116 MPI_Datatype MPITraits<FieldVector<K,n> >::vectortype = {MPI_DATATYPE_NULL};
117
118
119 template<int k>
120 class bigunsignedint;
121
122 template<int k>
123 struct MPITraits<bigunsignedint<k> >
124 {
125 static MPI_Datatype datatype;
126 static MPI_Datatype vectortype;
127
128 static inline MPI_Datatype getType()
129 {
130 if(datatype==MPI_DATATYPE_NULL) {
131 MPI_Type_contiguous(bigunsignedint<k>::n, MPITraits<std::uint16_t>::getType(),
132 &vectortype);
133 //MPI_Type_commit(&vectortype);
134 bigunsignedint<k> data;
135 MPI_Aint base;
136 MPI_Aint displ;
137 MPI_Get_address(&data, &base);
138 MPI_Get_address(&(data.digit), &displ);
139 displ -= base;
140 int length[1]={1};
141 MPI_Type_create_struct(1, length, &displ, &vectortype, &datatype);
142 MPI_Type_commit(&datatype);
143 }
144 return datatype;
145 }
146 };
147}
148
149namespace Dune
150{
151 template<int k>
152 MPI_Datatype MPITraits<bigunsignedint<k> >::datatype = MPI_DATATYPE_NULL;
153 template<int k>
154 MPI_Datatype MPITraits<bigunsignedint<k> >::vectortype = MPI_DATATYPE_NULL;
155
156 template<typename T1, typename T2>
157 struct MPITraits<std::pair<T1,T2 > >
158 {
159 public:
160 inline static MPI_Datatype getType();
161 private:
162 static MPI_Datatype type;
163 };
164 template<typename T1, typename T2>
165 MPI_Datatype MPITraits<std::pair<T1,T2> >::getType()
166 {
167 if(type==MPI_DATATYPE_NULL) {
168 int length[4];
169 MPI_Aint disp[4];
170 MPI_Datatype types[4] = {MPI_LB, MPITraits<T1>::getType(),
171 MPITraits<T2>::getType(), MPI_UB};
172 std::pair<T1,T2> rep[2];
173 length[0]=length[1]=length[2]=length[3]=1;
174 MPI_Get_address(rep, disp); // lower bound of the datatype
175 MPI_Get_address(&(rep[0].first), disp+1);
176 MPI_Get_address(&(rep[0].second), disp+2);
177 MPI_Get_address(rep+1, disp+3); // upper bound of the datatype
178 for(int i=3; i >= 0; --i)
179 disp[i] -= disp[0];
180 MPI_Type_create_struct(4, length, disp, types, &type);
181 MPI_Type_commit(&type);
182 }
183 return type;
184 }
185
186 template<typename T1, typename T2>
187 MPI_Datatype MPITraits<std::pair<T1,T2> >::type=MPI_DATATYPE_NULL;
188#endif
189#endif
190#endif
192}
193
194#endif
vector space out of a tensor product of fields.
Definition: fvector.hh:94
Dune namespace.
Definition: alignment.hh:10
STL namespace.
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: mpitraits.hh:37
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)