-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathzarray_impl.hpp
More file actions
315 lines (249 loc) · 8.73 KB
/
zarray_impl.hpp
File metadata and controls
315 lines (249 loc) · 8.73 KB
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
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTENSOR_ZARRAY_IMPL_HPP
#define XTENSOR_ZARRAY_IMPL_HPP
#include <nlohmann/json.hpp>
#include <xtl/xplatform.hpp>
#include <xtl/xhalf_float.hpp>
#include <xtl/xmeta_utils.hpp>
#include <xtensor/xarray.hpp>
#include <xtensor/xchunked_array.hpp>
#include <xtensor/xchunked_view.hpp>
#include <xtensor/xio.hpp>
#include <xtensor/xnoalias.hpp>
#include <xtensor/xscalar.hpp>
#include <xtensor/xshape.hpp>
#include <xtensor/xshape.hpp>
#include <xtensor/xstrided_view.hpp>
namespace xt
{
template <class CTE>
class zarray_wrapper;
template <class CTE>
class zchunked_wrapper;
template <class CTE>
class zscalar_wrapper;
template <class CTE>
class zexpression_wrapper;
/******************
* zarray builder *
******************/
namespace detail
{
template <class E>
struct is_xarray : std::false_type
{
};
template <class T, layout_type L, class A, class SA>
struct is_xarray<xarray<T, L, A, SA>> : std::true_type
{
};
template <class E>
struct is_chunked_array : std::false_type
{
};
template <class CS>
struct is_chunked_array<xchunked_array<CS>> : std::true_type
{
};
template <class E>
struct is_xscalar : std::false_type
{
};
template <class T>
struct is_xscalar<xscalar<T>> : std::true_type
{
};
template <class E>
struct is_xstrided_view : std::false_type
{
};
template <class CT, class S, layout_type L, class FST>
struct is_xstrided_view<xstrided_view<CT, S, L, FST>> : std::true_type
{
};
template <class E>
struct zwrapper_builder
{
using closure_type = xtl::closure_type_t<E>;
using wrapper_type = xtl::mpl::switch_t<
is_xarray<std::decay_t<E>>, zarray_wrapper<closure_type>,
is_chunked_array<std::decay_t<E>>, zchunked_wrapper<closure_type>,
is_xscalar<std::decay_t<E>>, zscalar_wrapper<closure_type>,
std::true_type, zexpression_wrapper<closure_type>
>;
template <class OE>
static wrapper_type* run(OE&& e)
{
return new wrapper_type(std::forward<OE>(e));
}
};
template <class E>
inline auto build_zarray(E&& e)
{
return zwrapper_builder<E>::run(std::forward<E>(e));
}
template <class CT>
using is_const = std::is_const<std::remove_reference_t<CT>>;
template <class CT, class R = void>
using disable_xstrided_view_t = std::enable_if_t<!is_xstrided_view<CT>::value || is_const<CT>::value, R>;
template <class CT, class R = void>
using enable_const_t = std::enable_if_t<is_const<CT>::value, R>;
template <class CT, class R = void>
using disable_const_t = std::enable_if_t<!is_const<CT>::value, R>;
}
/*************************
* zarray_expression_tag *
*************************/
struct zarray_expression_tag {};
namespace extension
{
template <>
struct expression_tag_and<xtensor_expression_tag, zarray_expression_tag>
{
using type = zarray_expression_tag;
};
template <>
struct expression_tag_and<zarray_expression_tag, xtensor_expression_tag>
: expression_tag_and<xtensor_expression_tag, zarray_expression_tag>
{
};
template <>
struct expression_tag_and<zarray_expression_tag, zarray_expression_tag>
{
using type = zarray_expression_tag;
};
}
/***************
* zarray_impl *
***************/
class zarray_impl
{
public:
using self_type = zarray_impl;
using shape_type = dynamic_shape<std::size_t>;
virtual ~zarray_impl() = default;
zarray_impl(zarray_impl&&) = delete;
zarray_impl& operator=(const zarray_impl&) = delete;
zarray_impl& operator=(zarray_impl&&) = delete;
virtual self_type* clone() const = 0;
virtual std::ostream& print(std::ostream& out) const = 0;
virtual bool is_array() const = 0;
virtual bool is_chunked() const = 0;
virtual self_type* strided_view(xstrided_slice_vector& slices) = 0;
virtual const nlohmann::json& get_metadata() const = 0;
virtual void set_metadata(const nlohmann::json& metadata) = 0;
virtual std::size_t dimension() const = 0;
virtual const shape_type& shape() const = 0;
virtual void reshape(const shape_type& shape) = 0;
virtual void reshape(shape_type&& shape) = 0;
virtual void resize(const shape_type& shape) = 0;
virtual void resize(shape_type&& shape) = 0;
virtual bool broadcast_shape(shape_type& shape, bool reuse_cache = 0) const = 0;
XTL_IMPLEMENT_INDEXABLE_CLASS()
protected:
zarray_impl() = default;
zarray_impl(const zarray_impl&) = default;
};
/****************
* ztyped_array *
****************/
template <class T>
class ztyped_array : public zarray_impl
{
public:
using base_type = zarray_impl;
using shape_type = base_type::shape_type;
using slice_vector = xstrided_slice_vector;
virtual ~ztyped_array() = default;
virtual xarray<T>& get_array() = 0;
virtual const xarray<T>& get_array() const = 0;
virtual xarray<T> get_chunk(const slice_vector& slices) const = 0;
XTL_IMPLEMENT_INDEXABLE_CLASS()
protected:
ztyped_array() = default;
ztyped_array(const ztyped_array&) = default;
};
/*****************
* set_data_type *
*****************/
namespace detail
{
inline const std::string endianness_string()
{
static std::string endianness = (xtl::endianness() == xtl::endian::little_endian) ? "<" : ">";
return endianness;
}
template <class T>
inline void set_data_type(nlohmann::json& metadata)
{
}
template <>
inline void set_data_type<bool>(nlohmann::json& metadata)
{
metadata["data_type"] = "bool";
}
template <>
inline void set_data_type<uint8_t>(nlohmann::json& metadata)
{
metadata["data_type"] = "u1";
}
template <>
inline void set_data_type<int8_t>(nlohmann::json& metadata)
{
metadata["data_type"] = "i1";
}
template <>
inline void set_data_type<int16_t>(nlohmann::json& metadata)
{
metadata["data_type"] = endianness_string() + "i2";
}
template <>
inline void set_data_type<uint16_t>(nlohmann::json& metadata)
{
metadata["data_type"] = endianness_string() + "u2";
}
template <>
inline void set_data_type<int32_t>(nlohmann::json& metadata)
{
metadata["data_type"] = endianness_string() + "i4";
}
template <>
inline void set_data_type<uint32_t>(nlohmann::json& metadata)
{
metadata["data_type"] = endianness_string() + "u4";
}
template <>
inline void set_data_type<int64_t>(nlohmann::json& metadata)
{
metadata["data_type"] = endianness_string() + "i8";
}
template <>
inline void set_data_type<uint64_t>(nlohmann::json& metadata)
{
metadata["data_type"] = endianness_string() + "u8";
}
template <>
inline void set_data_type<xtl::half_float>(nlohmann::json& metadata)
{
metadata["data_type"] = endianness_string() + "f2";
}
template <>
inline void set_data_type<float>(nlohmann::json& metadata)
{
metadata["data_type"] = endianness_string() + "f4";
}
template <>
inline void set_data_type<double>(nlohmann::json& metadata)
{
metadata["data_type"] = endianness_string() + "f8";
}
}
}
#endif