forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagonal.cpp
More file actions
157 lines (127 loc) · 4.61 KB
/
diagonal.cpp
File metadata and controls
157 lines (127 loc) · 4.61 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
/*******************************************************
* 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 <half.hpp>
#include <testHelpers.hpp>
#include <cmath>
#include <vector>
using af::array;
using af::constant;
using af::deviceGC;
using af::diag;
using af::dim4;
using af::exception;
using af::max;
using af::seq;
using af::span;
using af::sum;
using std::abs;
using std::vector;
template<typename T>
class Diagonal : public ::testing::Test {};
typedef ::testing::Types<float, double, int, uint, char, unsigned char,
half_float::half>
TestTypes;
TYPED_TEST_CASE(Diagonal, TestTypes);
TYPED_TEST(Diagonal, Create) {
SUPPORTED_TYPE_CHECK(TypeParam);
try {
static const int size = 1000;
vector<TypeParam> input(size * size);
for (int i = 0; i < size; i++) { input[i] = i; }
for (int jj = 10; jj < size; jj += 100) {
array data(jj, &input.front(), afHost);
array out = diag(data, 0, false);
vector<TypeParam> h_out(out.elements());
out.host(&h_out.front());
for (int i = 0; i < (int)out.dims(0); i++) {
for (int j = 0; j < (int)out.dims(1); j++) {
if (i == j)
ASSERT_EQ(input[i], h_out[i * out.dims(0) + j]);
else
ASSERT_EQ(TypeParam(0), h_out[i * out.dims(0) + j]);
}
}
}
} catch (const exception& ex) { FAIL() << ex.what(); }
}
TYPED_TEST(Diagonal, DISABLED_CreateLargeDim) {
SUPPORTED_TYPE_CHECK(TypeParam);
try {
deviceGC();
{
static const size_t largeDim = 65535 + 1;
array diagvals = constant(1, largeDim);
array out = diag(diagvals, 0, false);
ASSERT_EQ(largeDim, sum<float>(out));
}
} catch (const exception& ex) { FAIL() << ex.what(); }
}
TYPED_TEST(Diagonal, Extract) {
SUPPORTED_TYPE_CHECK(TypeParam);
try {
static const int size = 1000;
vector<TypeParam> input(size * size);
for (int i = 0; i < size * size; i++) { input[i] = i; }
for (int jj = 10; jj < size; jj += 100) {
array data(jj, jj, &input.front(), afHost);
array out = diag(data, 0);
vector<TypeParam> h_out(out.elements());
out.host(&h_out.front());
for (int i = 0; i < (int)out.dims(0); i++) {
ASSERT_EQ(input[i * data.dims(0) + i], h_out[i]);
}
}
} catch (const exception& ex) { FAIL() << ex.what(); }
}
TYPED_TEST(Diagonal, ExtractLargeDim) {
SUPPORTED_TYPE_CHECK(TypeParam);
try {
static const size_t n = 10;
static const size_t largeDim = 65535 + 1;
array largedata = constant(1, n, n, largeDim);
array out = diag(largedata, 0);
ASSERT_EQ(n * largeDim, sum<float>(out));
largedata = constant(1, n, n, 1, largeDim);
array out1 = diag(largedata, 0);
ASSERT_EQ(n * largeDim, sum<float>(out1));
} catch (const exception& ex) { FAIL() << ex.what(); }
}
TYPED_TEST(Diagonal, ExtractRect) {
SUPPORTED_TYPE_CHECK(TypeParam);
try {
static const int size0 = 1000, size1 = 900;
vector<TypeParam> input(size0 * size1);
for (int i = 0; i < size0 * size1; i++) { input[i] = i; }
for (int jj = 10; jj < size0; jj += 100) {
for (int kk = 10; kk < size1; kk += 90) {
array data(jj, kk, &input.front(), afHost);
array out = diag(data, 0);
vector<TypeParam> h_out(out.elements());
out.host(&h_out.front());
ASSERT_EQ(out.dims(0), std::min(jj, kk));
for (int i = 0; i < (int)out.dims(0); i++) {
ASSERT_EQ(input[i * data.dims(0) + i], h_out[i]);
}
}
}
} catch (const exception& ex) { FAIL() << ex.what(); }
}
TEST(Diagonal, ExtractGFOR) {
dim4 dims = dim4(100, 100, 3);
array A = round(100 * randu(dims));
array B = constant(0, 100, 1, 3);
gfor(seq ii, 3) { B(span, span, ii) = diag(A(span, span, ii)); }
for (int ii = 0; ii < 3; ii++) {
array c_ii = diag(A(span, span, ii));
array b_ii = B(span, span, ii);
ASSERT_EQ(max<double>(abs(c_ii - b_ii)) < 1E-5, true);
}
}