-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathjoin.cpp
More file actions
333 lines (278 loc) · 11 KB
/
join.cpp
File metadata and controls
333 lines (278 loc) · 11 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/defines.h>
#include <af/dim4.hpp>
#include <af/index.h>
#include <af/traits.hpp>
#include <array>
#include <complex>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using af::array;
using af::cdouble;
using af::cfloat;
using af::dim4;
using af::dtype_traits;
using af::join;
using af::randu;
using af::seq;
using af::sum;
using std::endl;
using std::string;
using std::vector;
template<typename T>
class Join : public ::testing::Test {
public:
virtual void SetUp() {
subMat0.push_back(af_make_seq(0, 4, 1));
subMat0.push_back(af_make_seq(2, 6, 1));
subMat0.push_back(af_make_seq(0, 2, 1));
}
vector<af_seq> subMat0;
};
// create a list of types to be tested
typedef ::testing::Types<float, double, cfloat, cdouble, int, unsigned int,
intl, uintl, char, signed char, unsigned char, short,
ushort, half_float::half>
TestTypes;
// register the type list
TYPED_TEST_SUITE(Join, TestTypes);
template<typename T>
void joinTest(string pTestFile, const unsigned dim, const unsigned in0,
const unsigned in1, const unsigned resultIdx,
bool isSubRef = false, const vector<af_seq>* seqv = NULL) {
SUPPORTED_TYPE_CHECK(T);
vector<dim4> numDims;
vector<vector<T>> in;
vector<vector<T>> tests;
readTests<T, T, int>(pTestFile, numDims, in, tests);
dim4 i0dims = numDims[in0];
dim4 i1dims = numDims[in1];
af_array in0Array = 0;
af_array in1Array = 0;
af_array outArray = 0;
af_array tempArray = 0;
if (isSubRef) {
ASSERT_SUCCESS(af_create_array(&tempArray, &(in[in0].front()),
i0dims.ndims(), i0dims.get(),
(af_dtype)dtype_traits<T>::af_type));
ASSERT_SUCCESS(
af_index(&in0Array, tempArray, seqv->size(), &seqv->front()));
} else {
ASSERT_SUCCESS(af_create_array(&in0Array, &(in[in0].front()),
i0dims.ndims(), i0dims.get(),
(af_dtype)dtype_traits<T>::af_type));
}
if (isSubRef) {
ASSERT_SUCCESS(af_create_array(&tempArray, &(in[in1].front()),
i1dims.ndims(), i1dims.get(),
(af_dtype)dtype_traits<T>::af_type));
ASSERT_SUCCESS(
af_index(&in1Array, tempArray, seqv->size(), &seqv->front()));
} else {
ASSERT_SUCCESS(af_create_array(&in1Array, &(in[in1].front()),
i1dims.ndims(), i1dims.get(),
(af_dtype)dtype_traits<T>::af_type));
}
ASSERT_SUCCESS(af_join(&outArray, dim, in0Array, in1Array));
dim4 goldDims = i0dims;
goldDims[dim] = i0dims[dim] + i1dims[dim];
ASSERT_VEC_ARRAY_EQ(tests[resultIdx], goldDims, outArray);
if (in0Array != 0) af_release_array(in0Array);
if (in1Array != 0) af_release_array(in1Array);
if (outArray != 0) af_release_array(outArray);
if (tempArray != 0) af_release_array(tempArray);
}
#define JOIN_INIT(desc, file, dim, in0, in1, resultIdx) \
TYPED_TEST(Join, desc) { \
joinTest<TypeParam>(string(TEST_DIR "/join/" #file ".test"), dim, in0, \
in1, resultIdx); \
}
JOIN_INIT(JoinBig0, join_big, 0, 0, 1, 0);
JOIN_INIT(JoinBig1, join_big, 1, 0, 2, 1);
JOIN_INIT(JoinBig2, join_big, 2, 0, 3, 2);
JOIN_INIT(JoinSmall0, join_small, 0, 0, 1, 0);
JOIN_INIT(JoinSmall1, join_small, 1, 0, 2, 1);
JOIN_INIT(JoinSmall2, join_small, 2, 0, 3, 2);
TEST(Join, JoinLargeDim) {
using af::constant;
using af::deviceGC;
using af::span;
// const int nx = 32;
const int nx = 1;
const int ny = 4 * 1024 * 1024;
const int nw = 4 * 1024 * 1024;
deviceGC();
{
array in = randu(nx, ny, u8);
array joined = join(0, in, in);
dim4 in_dims = in.dims();
dim4 joined_dims = joined.dims();
ASSERT_EQ(2 * in_dims[0], joined_dims[0]);
ASSERT_EQ(0.f, sum<float>((joined(0, span) - joined(1, span)).as(f32)));
array in2 = constant(1, (dim_t)nx, (dim_t)ny, (dim_t)2, (dim_t)nw, u8);
joined = join(3, in, in);
in_dims = in.dims();
joined_dims = joined.dims();
ASSERT_EQ(2 * in_dims[3], joined_dims[3]);
}
}
///////////////////////////////// CPP ////////////////////////////////////
//
TEST(Join, CPP) {
const unsigned resultIdx = 2;
const unsigned dim = 2;
vector<dim4> numDims;
vector<vector<float>> in;
vector<vector<float>> tests;
readTests<float, float, int>(string(TEST_DIR "/join/join_big.test"),
numDims, in, tests);
dim4 i0dims = numDims[0];
dim4 i1dims = numDims[3];
array input0(i0dims, &(in[0].front()));
array input1(i1dims, &(in[3].front()));
array output = join(dim, input0, input1);
dim4 goldDims = i0dims;
goldDims[dim] = i0dims[dim] + i1dims[dim];
ASSERT_VEC_ARRAY_EQ(tests[resultIdx], goldDims, output);
}
TEST(JoinMany0, CPP) {
array a0 = randu(10, 5);
array a1 = randu(20, 5);
array a2 = randu(5, 5);
array output = join(0, a0, a1, a2);
array gold = join(0, a0, join(0, a1, a2));
ASSERT_EQ(sum<float>(output - gold), 0);
}
TEST(JoinMany1, CPP) {
array a0 = randu(20, 200);
array a1 = randu(20, 400);
array a2 = randu(20, 10);
array a3 = randu(20, 100);
int dim = 1;
array output = join(dim, a0, a1, a2, a3);
array gold = join(dim, a0, join(dim, a1, join(dim, a2, a3)));
ASSERT_EQ(sum<float>(output - gold), 0);
}
TEST(Join, DifferentSizes) {
array a = seq(10);
array b = seq(11);
array c = seq(12);
array d = join(0, a, b, c);
vector<float> ha(10);
vector<float> hb(11);
vector<float> hc(12);
for (size_t i = 0; i < ha.size(); i++) { ha[i] = i; }
for (size_t i = 0; i < hb.size(); i++) { hb[i] = i; }
for (size_t i = 0; i < hc.size(); i++) { hc[i] = i; }
vector<float> hgold(10 + 11 + 12);
vector<float>::iterator it = copy(ha.begin(), ha.end(), hgold.begin());
it = copy(hb.begin(), hb.end(), it);
it = copy(hc.begin(), hc.end(), it);
ASSERT_VEC_ARRAY_EQ(hgold, dim4(10 + 11 + 12), d);
}
TEST(Join, SameSize) {
array a = seq(10);
array b = seq(10);
array c = seq(10);
array d = join(0, a, b, c);
vector<float> ha(10);
vector<float> hb(10);
vector<float> hc(10);
for (size_t i = 0; i < ha.size(); i++) { ha[i] = i; }
for (size_t i = 0; i < hb.size(); i++) { hb[i] = i; }
for (size_t i = 0; i < hc.size(); i++) { hc[i] = i; }
vector<float> hgold(10 + 10 + 10);
vector<float>::iterator it = copy(ha.begin(), ha.end(), hgold.begin());
it = copy(hb.begin(), hb.end(), it);
it = copy(hc.begin(), hc.end(), it);
ASSERT_VEC_ARRAY_EQ(hgold, dim4(10 + 10 + 10), d);
}
TEST(Join, ManyEmpty) {
array gold = af::constant(0, 15, 5);
array a = af::randn(5, 5);
array e;
array c = af::randn(10, 5);
array ee = af::join(0, e, e);
ASSERT_EQ(ee.elements(), 0);
array eee = af::join(0, e, e, e);
ASSERT_EQ(eee.elements(), 0);
array eeac = af::join(0, e, e, a, c);
array eace = af::join(0, e, a, c, e);
array acee = af::join(0, a, c, e, e);
gold(af::seq(0, 4), af::span) = a;
gold(af::seq(5, 14), af::span) = c;
ASSERT_ARRAYS_EQ(gold, eeac);
ASSERT_ARRAYS_EQ(gold, eace);
ASSERT_ARRAYS_EQ(gold, acee);
}
TEST(Join, respect_parameters_order_ISSUE3511) {
const float column_host1[] = {1., 2., 3.};
const float column_host2[] = {4., 5., 6.};
const af::array buf1(3, 1, column_host1);
const af::array buf2(3, 1, column_host2);
// We need to avoid that JIT arrays are evaluated during whatever call,
// so we will have to work with copies for single use
const af::array jit1{buf1 + 1.0};
const af::array jit2{buf2 + 2.0};
const std::array<af::array, 8> cases{jit1, -jit1, jit1 + 1.0, jit2,
-jit2, jit1 + jit2, buf1, buf2};
const std::array<const char*, 8> cases_name{"JIT1", "-JIT1", "JIT1+1.0",
"JIT2", "-JIT2", "JIT1+JIT2",
"BUF1", "BUF2"};
assert(cases.size() == cases_name.size());
for (size_t cl0{0}; cl0 < cases.size(); ++cl0) {
for (size_t cl1{0}; cl1 < cases.size(); ++cl1) {
printf("Testing: af::join(1,%s,%s)\n", cases_name[cl0],
cases_name[cl1]);
const array col0{cases[cl0]};
const array col1{cases[cl1]};
const array result{af::join(1, col0, col1)};
ASSERT_ARRAYS_EQ(result(af::span, 0), col0);
ASSERT_ARRAYS_EQ(result(af::span, 1), col1);
}
}
// Join of 3 arrays
for (size_t cl0{0}; cl0 < cases.size(); ++cl0) {
for (size_t cl1{0}; cl1 < cases.size(); ++cl1) {
for (size_t cl2{0}; cl2 < cases.size(); ++cl2) {
printf("Testing: af::join(1,%s,%s,%s)\n", cases_name[cl0],
cases_name[cl1], cases_name[cl2]);
const array col0{cases[cl0]};
const array col1{cases[cl1]};
const array col2{cases[cl2]};
const array result{af::join(1, col0, col1, col2)};
ASSERT_ARRAYS_EQ(result(af::span, 0), col0);
ASSERT_ARRAYS_EQ(result(af::span, 1), col1);
ASSERT_ARRAYS_EQ(result(af::span, 2), col2);
}
}
}
}
#define TEST_TEMP_FORMAT(form, d) \
TEST(TEMP_FORMAT, form##_dim##d) { \
const dim4 dims(2, 2, 2, 2); \
const array a(randu(dims)); \
const array b(randu(dims)); \
\
array out = join(d, toTempFormat(form, a), toTempFormat(form, b)); \
array gold = join(d, a, b); \
EXPECT_ARRAYS_EQ(gold, out); \
}
#define TEST_TEMP_FORMATS(form) \
TEST_TEMP_FORMAT(form, 0) \
TEST_TEMP_FORMAT(form, 1) \
TEST_TEMP_FORMAT(form, 2) \
TEST_TEMP_FORMAT(form, 3)
FOREACH_TEMP_FORMAT(TEST_TEMP_FORMATS)