forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_xchunked_array.cpp
169 lines (139 loc) · 4.62 KB
/
test_xchunked_array.cpp
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
/***************************************************************************
* 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. *
****************************************************************************/
#include "test_common_macros.hpp"
#include "xtensor/xbroadcast.hpp"
#include "xtensor/xchunked_array.hpp"
#include "xtensor/xcsv.hpp"
#include "xtensor/xnoalias.hpp"
namespace xt
{
using in_memory_chunked_array = xchunked_array<xarray<xarray<double>>>;
TEST(xchunked_array, indexed_access)
{
auto a = chunked_array<double>({10, 10, 10}, {2, 3, 4});
std::vector<size_t> idx = {3, 9, 8};
double val;
val = 1.;
a[idx] = val;
ASSERT_EQ(a[idx], val);
ASSERT_EQ(a(3, 9, 8), val);
val = 2.;
a(3, 9, 8) = val;
ASSERT_EQ(a(3, 9, 8), val);
ASSERT_EQ(a[idx], val);
val = 3.;
for (auto& it: a)
it = val;
for (auto it: a)
ASSERT_EQ(it, val);
}
TEST(xchunked_array, assign_expression)
{
std::vector<size_t> shape1 = {2, 2, 2};
std::vector<size_t> chunk_shape1 = {2, 3, 4};
auto a1 = chunked_array<double>(shape1, chunk_shape1);
double val;
val = 3.;
a1 = broadcast(val, a1.shape());
for (const auto& v: a1)
{
EXPECT_EQ(v, val);
}
std::vector<size_t> shape2 = {32, 10, 10};
auto a2 = chunked_array<double>(shape2, chunk_shape1);
a2 = broadcast(val, a2.shape());
for (const auto& v: a2)
{
EXPECT_EQ(v, val);
}
a2 += a2;
for (const auto& v: a2)
{
EXPECT_EQ(v, 2. * val);
}
a2 += 2.;
for (const auto& v: a2)
{
EXPECT_EQ(v, 2. * val + 2.);
}
xarray<double> a3
{{1., 2., 3.},
{4., 5., 6.},
{7., 8., 9.}};
EXPECT_EQ(is_chunked(a3), false);
std::vector<size_t> chunk_shape4 = {2, 2};
auto a4 = chunked_array(a3, chunk_shape4);
EXPECT_EQ(is_chunked(a4), true);
double i = 1.;
for (const auto& v: a4)
{
EXPECT_EQ(v, i);
i += 1.;
}
auto a5 = in_memory_chunked_array(a4);
EXPECT_EQ(is_chunked(a5), true);
for (const auto& v: a5.chunk_shape())
{
EXPECT_EQ(v, 2);
}
auto a6 = chunked_array(a3);
EXPECT_EQ(is_chunked(a6), true);
for (const auto& v: a6.chunk_shape())
{
EXPECT_EQ(v, 3);
}
std::vector<size_t> shape3 = {3, 3};
std::vector<size_t> chunk_shape3 = {1, 2};
auto a7 = chunked_array<double>(shape3, chunk_shape3);
for (auto it = a7.chunks().begin(); it != a7.chunks().end(); ++it)
{
it->resize(chunk_shape3);
}
a7 = a3;
for (auto it = a7.chunks().begin(); it != a7.chunks().end(); ++it)
{
EXPECT_EQ(it->shape(), chunk_shape3);
}
}
TEST(xchunked_array, noalias)
{
std::vector<std::size_t> shape = {10, 10, 10};
std::vector<std::size_t> chunk_shape = {2, 2, 2};
auto a = chunked_array<double>(shape, chunk_shape);
xt::xarray<double> b = arange(1000).reshape({10, 10, 10});
noalias(a) = b;
EXPECT_EQ(a, b);
}
TEST(xchunked_array, chunk_iterator)
{
std::vector<std::size_t> shape = {10, 10, 10};
std::vector<std::size_t> chunk_shape = {2, 2, 2};
auto a = chunked_array<double>(shape, chunk_shape);
xt::xarray<double> b = arange(1000).reshape({10, 10, 10});
noalias(a) = b;
auto it = a.chunk_begin();
auto cit = a.chunk_cbegin();
for (size_t i = 0; i < 5; ++i)
{
for (size_t j = 0; j < 5; ++j)
{
for (size_t k = 0; k < 5; ++k)
{
EXPECT_EQ(*((*it).begin()), a(2*i, 2*j, 2*k));
EXPECT_EQ(*((*cit).cbegin()), a(2*i, 2*j, 2*k));
++it;
++cit;
}
}
}
it = a.chunk_begin();
std::advance(it, 2);
EXPECT_EQ(*((*it).begin()), a(0, 0, 4));
}
}