-
Notifications
You must be signed in to change notification settings - Fork 707
/
type_traits.h
354 lines (277 loc) · 9.62 KB
/
type_traits.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// ---------------------------------------------------------------------
//
// Copyright (C) 2019 - 2020 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#ifndef dealii_matrix_free_type_traits_h
#define dealii_matrix_free_type_traits_h
// various type-traits used exclusively within the matrix-free framework
#include <deal.II/base/config.h>
#include <deal.II/base/partitioner.h>
#include <deal.II/lac/vector_type_traits.h>
DEAL_II_NAMESPACE_OPEN
#ifndef DOXYGEN
namespace internal
{
//
// type traits for FEEvaluation
//
// a helper type-trait that leverage SFINAE to figure out if type T has
// ... T::local_element() const
template <typename T>
struct has_local_element
{
private:
// this will work always.
// we let it be void as we know T::local_element() (if exists) should
// certainly return something
static void
detect(...);
// this detecter will work only if we have "... T::local_element() const"
// and its return type will be the same as local_element(),
// that we expect to be T::value_type
template <typename U>
static decltype(std::declval<U const>().local_element(0))
detect(const U &);
public:
// finally here we check if our detector has non-void return type
// T::value_type. This will happen if compiler can use second detector,
// otherwise SFINAE let it work with the more general first one that is void
static const bool value =
!std::is_same<void, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_local_element<T>::value;
// a helper type-trait that leverage SFINAE to figure out if type T has
// void T::add_local_element(const uint, const typename T::value_type)
template <typename T>
struct has_add_local_element
{
private:
static int
detect(...);
template <typename U>
static decltype(
std::declval<U>().add_local_element(0, typename T::value_type()))
detect(const U &);
public:
static const bool value =
!std::is_same<int, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_add_local_element<T>::value;
// a helper type-trait that leverage SFINAE to figure out if type T has
// void T::set_local_element(const uint, const typename T::value_type)
template <typename T>
struct has_set_local_element
{
private:
static int
detect(...);
template <typename U>
static decltype(
std::declval<U>().set_local_element(0, typename T::value_type()))
detect(const U &);
public:
static const bool value =
!std::is_same<int, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_set_local_element<T>::value;
// same as above to check
// bool T::partitioners_are_compatible(const Utilities::MPI::Partitioner &)
// const
template <typename T>
struct has_partitioners_are_compatible
{
private:
static void
detect(...);
template <typename U>
static decltype(std::declval<U const>().partitioners_are_compatible(
std::declval<Utilities::MPI::Partitioner>()))
detect(const U &);
public:
static const bool value =
std::is_same<bool, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_partitioners_are_compatible<T>::value;
// same as above to check
// ... T::begin() const
template <typename T>
struct has_begin
{
private:
static void
detect(...);
template <typename U>
static decltype(std::declval<U const>().begin())
detect(const U &);
public:
static const bool value =
!std::is_same<void, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_begin<T>::value;
// same as above to check
// ... T::shared_vector_data() const
template <typename T>
struct has_shared_vector_data
{
private:
static void
detect(...);
template <typename U>
static decltype(std::declval<U const>().shared_vector_data())
detect(const U &);
public:
static const bool value =
!std::is_same<void, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_shared_vector_data<T>::value;
// type trait for vector T and Number to see if
// we can do vectorized load/save.
// for VectorReader and VectorDistributorLocalToGlobal we assume that
// if both begin() and local_element()
// exist, then begin() + offset == local_element(offset)
template <typename T, typename Number>
struct is_vectorizable
{
static const bool value =
has_begin<T>::value &&
(has_local_element<T>::value ||
is_serial_vector<typename std::remove_const<T>::type>::value) &&
std::is_same<typename T::value_type, Number>::value;
};
// We need to have a separate declaration for static const members
template <typename T, typename Number>
const bool is_vectorizable<T, Number>::value;
//
// type-traits for Matrix-Free
//
// similar to type traits in FEEvaluation, below we add type-traits
// to distinguish between vectors that provide different interface for
// operations like update_ghost_values(), compress(), etc.
// see internal::has_local_element in fe_evaluation.h that documents
// how those type traits work.
// a helper type-trait that leverage SFINAE to figure out if type T has
// void T::update_ghost_values_start(const uint) const
template <typename T>
struct has_update_ghost_values_start
{
private:
static bool
detect(...);
template <typename U>
static decltype(std::declval<U const>().update_ghost_values_start(0))
detect(const U &);
public:
static const bool value =
!std::is_same<bool, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_update_ghost_values_start<T>::value;
// a helper type-trait that leverage SFINAE to figure out if type T has
// void T:: compress_start(const uint, VectorOperation::values)
template <typename T>
struct has_compress_start
{
private:
static bool
detect(...);
template <typename U>
static decltype(std::declval<U>().compress_start(0, VectorOperation::add))
detect(const U &);
public:
static const bool value =
!std::is_same<bool, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_compress_start<T>::value;
// type trait for vector T to see if
// we do a custom data exchange route.
// We assume that if both begin() and local_element()
// exist, then begin() + offset == local_element(offset)
template <typename T>
struct has_exchange_on_subset
{
static const bool value = has_begin<T>::value &&
has_local_element<T>::value &&
has_partitioners_are_compatible<T>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_exchange_on_subset<T>::value;
// a helper type-trait that leverage SFINAE to figure out if type T has
// T::communication_block_size
template <typename T>
struct has_communication_block_size
{
private:
static void
detect(...);
template <typename U>
static decltype(U::communication_block_size)
detect(const U &);
public:
static const bool value =
!std::is_same<void, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool has_communication_block_size<T>::value;
// type trait for vector T to see if
// we need to do any data exchange for this vector type at all.
// is_serial_vector<> would have been enough, but in some circumstances
// (like calculation of diagonals for matrix-free operators)
// a dummy InVector == unsigned int is provided.
// Thus we have to treat this case as well.
template <typename T>
struct is_serial_or_dummy
{
private:
// catches all cases including unsigned int
static void
detect(...);
// catches serial vectors
template <
typename U,
typename std::enable_if<is_serial_vector<U>::value, U>::type * = nullptr>
static void
detect(const U &);
// catches parallel vectors
template <
typename U,
typename std::enable_if<!is_serial_vector<U>::value, U>::type * = nullptr>
static bool
detect(const U &);
public:
static const bool value =
std::is_same<void, decltype(detect(std::declval<T>()))>::value;
};
// We need to have a separate declaration for static const members
template <typename T>
const bool is_serial_or_dummy<T>::value;
} // namespace internal
#endif
DEAL_II_NAMESPACE_CLOSE
#endif