forked from idaholab/moose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialAccess.h
181 lines (153 loc) · 5.83 KB
/
SerialAccess.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
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
#pragma once
// MOOSE includes
#include "Moose.h"
#include "MooseTypes.h"
#include "RankTwoTensorForward.h"
#include "RankFourTensorForward.h"
#include <tuple>
namespace Moose
{
/**
* Serial access requires object data to be stored contiguously. Specialize this template
* to support more types.
*/
template <typename T>
struct SerialAccess
{
static_assert(always_false<T>, "Specialize SerialAccess for this type.");
};
// Specializations for scalar types
#define SERIAL_ACCESS_SCALAR(type) \
template <> \
struct SerialAccess<type> \
{ \
static type * data(type & obj) { return &obj; } \
static constexpr std::size_t size(type &) { return 1u; } \
static constexpr std::size_t size() { return 1u; } \
}
SERIAL_ACCESS_SCALAR(Real);
SERIAL_ACCESS_SCALAR(const Real);
SERIAL_ACCESS_SCALAR(ADReal);
SERIAL_ACCESS_SCALAR(const ADReal);
// constant size containers
#define SERIAL_ACCESS_CONST_SIZE(type, dataptr, sizeval) \
template <typename T> \
struct SerialAccess<type<T>> \
{ \
static auto * data(type<T> & obj) { return dataptr; } \
static constexpr std::size_t size(type<T> &) { return sizeval; } \
static constexpr std::size_t size() { return sizeval; } \
}
SERIAL_ACCESS_CONST_SIZE(VectorValue, &obj(0u), Moose::dim);
SERIAL_ACCESS_CONST_SIZE(const VectorValue, &obj(0u), Moose::dim);
SERIAL_ACCESS_CONST_SIZE(RankTwoTensorTempl, &obj(0u, 0u), RankTwoTensorTempl<T>::N2);
SERIAL_ACCESS_CONST_SIZE(const RankTwoTensorTempl, &obj(0u, 0u), RankTwoTensorTempl<T>::N2);
SERIAL_ACCESS_CONST_SIZE(RankFourTensorTempl, &obj(0u, 0u, 0u, 0u), RankFourTensorTempl<T>::N4);
SERIAL_ACCESS_CONST_SIZE(const RankFourTensorTempl,
&obj(0u, 0u, 0u, 0u),
RankFourTensorTempl<T>::N4);
// dynamic size containers (determining size requires an object instance)
#define SERIAL_ACCESS_DYNAMIC_SIZE(type, dataptr, sizeval) \
template <typename T> \
struct SerialAccess<type<T>> \
{ \
static auto * data(type<T> & obj) { return dataptr; } \
static constexpr std::size_t size(type<T> & obj) { return sizeval; } \
}
SERIAL_ACCESS_DYNAMIC_SIZE(DenseVector, &obj(0u), obj.size());
/**
* Value type helper (necessary for any type that does not have a value_type
* member or where value_type doesn't have a suitable meaning (ADReal)).
*/
template <typename T>
struct SerialAccessValueTypeHelper
{
typedef typename T::value_type value_type;
};
template <>
struct SerialAccessValueTypeHelper<ADReal>
{
typedef ADReal value_type;
};
template <>
struct SerialAccessValueTypeHelper<Real>
{
typedef Real value_type;
};
template <typename T>
class SerialAccessRange
{
typedef typename SerialAccessValueTypeHelper<typename std::remove_const<T>::type>::value_type R;
typedef typename std::conditional<std::is_const_v<T>, const R, R>::type V;
public:
class iterator
{
public:
iterator(V * i) : _i(i) {}
V & operator*() const { return *_i; }
const iterator & operator++()
{
++_i;
return *this;
}
iterator operator++(int)
{
iterator returnval(*this);
++_i;
return returnval;
}
bool operator==(const iterator & j) const { return (_i == j._i); }
bool operator!=(const iterator & j) const { return !(*this == j); }
private:
V * _i;
};
SerialAccessRange(T & obj)
: _begin(SerialAccess<T>::data(obj)),
_end(SerialAccess<T>::data(obj) + SerialAccess<T>::size(obj))
{
}
iterator begin() const { return _begin; }
iterator end() const { return _end; }
V & operator[](int i) { return *(&*_begin + i); }
private:
iterator _begin, _end;
};
template <typename T>
SerialAccessRange<T>
serialAccess(T & obj)
{
return SerialAccessRange<T>(obj);
}
/// Helper structure to hold a list of types
template <typename... Ts>
struct TypeList
{
typedef std::tuple<Ts...> Tuple;
typedef std::tuple<Ts *...> PointerTuple;
static constexpr std::size_t size = sizeof...(Ts);
};
/// Type loop
template <template <typename, int> class L, int I, typename T, typename... Ts, typename... As>
void
typeLoopInternal(TypeList<T, Ts...>, As... args)
{
L<T, I>::apply(args...);
if constexpr (sizeof...(Ts) > 0)
typeLoopInternal<L, I + 1>(TypeList<Ts...>{}, args...);
}
/// Type loop
template <template <typename, int> class L, typename... Ts, typename... As>
void
typeLoop(TypeList<Ts...>, As... args)
{
typeLoopInternal<L, 0>(TypeList<Ts...>{}, args...);
}
} // namespace Moose;