liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
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
//
//  Copyright (c) 2018-2019, Cem Bassoy, cem.bassoy@gmail.com
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
//  The authors gratefully acknowledge the support of
//  Fraunhofer IOSB, Ettlingen, Germany
//
 
#ifndef _BOOST_UBLAS_TENSOR_EXPRESSIONS_EVALUATION_HPP_
#define _BOOST_UBLAS_TENSOR_EXPRESSIONS_EVALUATION_HPP_
 
#include <type_traits>
#include <stdexcept>
 
 
namespace boost::numeric::ublas {
 
template<class element_type, class storage_format, class storage_type>
class tensor;
 
template<class size_type>
class basic_extents;
 
}
 
namespace boost::numeric::ublas::detail {
 
template<class T, class D>
struct tensor_expression;
 
template<class T, class EL, class ER, class OP>
struct binary_tensor_expression;
 
template<class T, class E, class OP>
struct unary_tensor_expression;
 
}
 
namespace boost::numeric::ublas::detail {
 
template<class T, class E>
struct has_tensor_types
{ static constexpr bool value = false; };
 
template<class T>
struct has_tensor_types<T,T>
{ static constexpr bool value = true; };
 
template<class T, class D>
struct has_tensor_types<T, tensor_expression<T,D>>
{ static constexpr bool value = std::is_same<T,D>::value || has_tensor_types<T,D>::value; };
 
 
template<class T, class EL, class ER, class OP>
struct has_tensor_types<T, binary_tensor_expression<T,EL,ER,OP>>
{ static constexpr bool value = std::is_same<T,EL>::value || std::is_same<T,ER>::value || has_tensor_types<T,EL>::value || has_tensor_types<T,ER>::value;  };
 
template<class T, class E, class OP>
struct has_tensor_types<T, unary_tensor_expression<T,E,OP>>
{ static constexpr bool value = std::is_same<T,E>::value || has_tensor_types<T,E>::value; };
 
} // namespace boost::numeric::ublas::detail
 
 
namespace boost::numeric::ublas::detail {
 
 
 
 
 
/** @brief Retrieves extents of the tensor
 *
*/
template<class T, class F, class A>
auto retrieve_extents(tensor<T,F,A> const& t)
{
    return t.extents();
}
 
/** @brief Retrieves extents of the tensor expression
 *
 * @note tensor expression must be a binary tree with at least one tensor type
 *
 * @returns extents of the child expression if it is a tensor or extents of one child of its child.
*/
template<class T, class D>
auto retrieve_extents(tensor_expression<T,D> const& expr)
{
    static_assert(detail::has_tensor_types<T,tensor_expression<T,D>>::value,
                  "Error in boost::numeric::ublas::detail::retrieve_extents: Expression to evaluate should contain tensors.");
 
    auto const& cast_expr = static_cast<D const&>(expr);
 
    if constexpr ( std::is_same<T,D>::value )
        return cast_expr.extents();
    else
    return retrieve_extents(cast_expr);
}
 
/** @brief Retrieves extents of the binary tensor expression
 *
 * @note tensor expression must be a binary tree with at least one tensor type
 *
 * @returns extents of the (left and if necessary then right) child expression if it is a tensor or extents of a child of its (left and if necessary then right) child.
*/
template<class T, class EL, class ER, class OP>
auto retrieve_extents(binary_tensor_expression<T,EL,ER,OP> const& expr)
{
    static_assert(detail::has_tensor_types<T,binary_tensor_expression<T,EL,ER,OP>>::value,
                  "Error in boost::numeric::ublas::detail::retrieve_extents: Expression to evaluate should contain tensors.");
 
    if constexpr ( std::is_same<T,EL>::value )
        return expr.el.extents();
 
    if constexpr ( std::is_same<T,ER>::value )
        return expr.er.extents();
 
    else if constexpr ( detail::has_tensor_types<T,EL>::value )
        return retrieve_extents(expr.el);
 
    else if constexpr ( detail::has_tensor_types<T,ER>::value  )
        return retrieve_extents(expr.er);
}
 
/** @brief Retrieves extents of the binary tensor expression
 *
 * @note tensor expression must be a binary tree with at least one tensor type
 *
 * @returns extents of the child expression if it is a tensor or extents of a child of its child.
*/
template<class T, class E, class OP>
auto retrieve_extents(unary_tensor_expression<T,E,OP> const& expr)
{
 
    static_assert(detail::has_tensor_types<T,unary_tensor_expression<T,E,OP>>::value,
                  "Error in boost::numeric::ublas::detail::retrieve_extents: Expression to evaluate should contain tensors.");
 
    if constexpr ( std::is_same<T,E>::value )
        return expr.e.extents();
 
    else if constexpr ( detail::has_tensor_types<T,E>::value  )
        return retrieve_extents(expr.e);
}
 
} // namespace boost::numeric::ublas::detail
 
 
///////////////
 
namespace boost::numeric::ublas::detail {
 
template<class T, class F, class A, class S>
auto all_extents_equal(tensor<T,F,A> const& t, basic_extents<S> const& extents)
{
    return extents == t.extents();
}
 
template<class T, class D, class S>
auto all_extents_equal(tensor_expression<T,D> const& expr, basic_extents<S> const& extents)
{
    static_assert(detail::has_tensor_types<T,tensor_expression<T,D>>::value,
                  "Error in boost::numeric::ublas::detail::all_extents_equal: Expression to evaluate should contain tensors.");
    auto const& cast_expr = static_cast<D const&>(expr);
 
 
    if constexpr ( std::is_same<T,D>::value )
        if( extents != cast_expr.extents() )
        return false;
 
    if constexpr ( detail::has_tensor_types<T,D>::value )
        if ( !all_extents_equal(cast_expr, extents))
        return false;
 
    return true;
 
}
 
template<class T, class EL, class ER, class OP, class S>
auto all_extents_equal(binary_tensor_expression<T,EL,ER,OP> const& expr, basic_extents<S> const& extents)
{
    static_assert(detail::has_tensor_types<T,binary_tensor_expression<T,EL,ER,OP>>::value,
                  "Error in boost::numeric::ublas::detail::all_extents_equal: Expression to evaluate should contain tensors.");
 
    if constexpr ( std::is_same<T,EL>::value )
        if(extents !=  expr.el.extents())
        return false;
 
    if constexpr ( std::is_same<T,ER>::value )
        if(extents != expr.er.extents())
        return false;
 
    if constexpr ( detail::has_tensor_types<T,EL>::value )
        if(!all_extents_equal(expr.el, extents))
        return false;
 
    if constexpr ( detail::has_tensor_types<T,ER>::value )
        if(!all_extents_equal(expr.er, extents))
        return false;
 
    return true;
}
 
 
template<class T, class E, class OP, class S>
auto all_extents_equal(unary_tensor_expression<T,E,OP> const& expr, basic_extents<S> const& extents)
{
 
    static_assert(detail::has_tensor_types<T,unary_tensor_expression<T,E,OP>>::value,
                  "Error in boost::numeric::ublas::detail::all_extents_equal: Expression to evaluate should contain tensors.");
 
    if constexpr ( std::is_same<T,E>::value )
        if(extents != expr.e.extents())
        return false;
 
    if constexpr ( detail::has_tensor_types<T,E>::value )
        if(!all_extents_equal(expr.e, extents))
        return false;
 
    return true;
}
 
} // namespace boost::numeric::ublas::detail
 
 
namespace boost::numeric::ublas::detail {
 
 
/** @brief Evaluates expression for a tensor
 *
 * Assigns the results of the expression to the tensor.
 *
 * \note Checks if shape of the tensor matches those of all tensors within the expression.
*/
template<class tensor_type, class derived_type>
void eval(tensor_type& lhs, tensor_expression<tensor_type, derived_type> const& expr)
{
    if constexpr (detail::has_tensor_types<tensor_type, tensor_expression<tensor_type,derived_type> >::value )
        if(!detail::all_extents_equal(expr, lhs.extents() ))
        throw std::runtime_error("Error in boost::numeric::ublas::tensor: expression contains tensors with different shapes.");
 
#pragma omp parallel for
    for(auto i = 0u; i < lhs.size(); ++i)
        lhs(i) = expr()(i);
}
 
/** @brief Evaluates expression for a tensor
 *
 * Applies a unary function to the results of the expressions before the assignment.
 * Usually applied needed for unary operators such as A += C;
 *
 * \note Checks if shape of the tensor matches those of all tensors within the expression.
*/
template<class tensor_type, class derived_type, class unary_fn>
void eval(tensor_type& lhs, tensor_expression<tensor_type, derived_type> const& expr, unary_fn const fn)
{
 
    if constexpr (detail::has_tensor_types< tensor_type, tensor_expression<tensor_type,derived_type> >::value )
        if(!detail::all_extents_equal( expr, lhs.extents() ))
        throw std::runtime_error("Error in boost::numeric::ublas::tensor: expression contains tensors with different shapes.");
 
#pragma omp parallel for
    for(auto i = 0u; i < lhs.size(); ++i)
        fn(lhs(i), expr()(i));
}
 
 
 
/** @brief Evaluates expression for a tensor
 *
 * Applies a unary function to the results of the expressions before the assignment.
 * Usually applied needed for unary operators such as A += C;
 *
 * \note Checks if shape of the tensor matches those of all tensors within the expression.
*/
template<class tensor_type, class unary_fn>
void eval(tensor_type& lhs, unary_fn const fn)
{
#pragma omp parallel for
    for(auto i = 0u; i < lhs.size(); ++i)
        fn(lhs(i));
}
 
 
}
#endif