-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathgradient.cpp
More file actions
180 lines (145 loc) · 5.03 KB
/
gradient.cpp
File metadata and controls
180 lines (145 loc) · 5.03 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
/*******************************************************
* 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/traits.hpp>
#include <complex>
#include <iostream>
#include <string>
#include <vector>
using af::cdouble;
using af::cfloat;
using af::dim4;
using af::dtype_traits;
using std::endl;
using std::string;
using std::vector;
template<typename T>
class Grad : 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> TestTypes;
// register the type list
TYPED_TEST_SUITE(Grad, TestTypes);
template<typename T>
void gradTest(string pTestFile, const unsigned resultIdx0,
const unsigned resultIdx1, 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, float>(pTestFile, numDims, in, tests);
dim4 idims = numDims[0];
af_array inArray = 0;
af_array tempArray = 0;
af_array g0Array = 0;
af_array g1Array = 0;
if (isSubRef) {
ASSERT_SUCCESS(af_create_array(&tempArray, &(in[0].front()),
idims.ndims(), idims.get(),
(af_dtype)dtype_traits<T>::af_type));
ASSERT_SUCCESS(
af_index(&inArray, tempArray, seqv->size(), &seqv->front()));
} else {
ASSERT_SUCCESS(af_create_array(&inArray, &(in[0].front()),
idims.ndims(), idims.get(),
(af_dtype)dtype_traits<T>::af_type));
}
ASSERT_SUCCESS(af_gradient(&g0Array, &g1Array, inArray));
size_t nElems = tests[resultIdx0].size();
// Get result
T* grad0Data = new T[tests[resultIdx0].size()];
ASSERT_SUCCESS(af_get_data_ptr((void*)grad0Data, g0Array));
// Compare result
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(tests[resultIdx0][elIter], grad0Data[elIter])
<< "at: " << elIter << endl;
}
// Get result
T* grad1Data = new T[tests[resultIdx1].size()];
ASSERT_SUCCESS(af_get_data_ptr((void*)grad1Data, g1Array));
// Compare result
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(tests[resultIdx1][elIter], grad1Data[elIter])
<< "at: " << elIter << endl;
}
// Delete
delete[] grad0Data;
delete[] grad1Data;
if (inArray != 0) af_release_array(inArray);
if (g0Array != 0) af_release_array(g0Array);
if (g1Array != 0) af_release_array(g1Array);
if (tempArray != 0) af_release_array(tempArray);
}
#define GRAD_INIT(desc, file, resultIdx0, resultIdx1) \
TYPED_TEST(Grad, desc) { \
gradTest<TypeParam>(string(TEST_DIR "/grad/" #file ".test"), \
resultIdx0, resultIdx1); \
}
GRAD_INIT(Grad0, grad, 0, 1);
GRAD_INIT(Grad1, grad2D, 0, 1);
GRAD_INIT(Grad2, grad3D, 0, 1);
/////////////////////////////////////// CPP
//////////////////////////////////////////////
//
using af::array;
TEST(Grad, CPP) {
const unsigned resultIdx0 = 0;
const unsigned resultIdx1 = 1;
vector<dim4> numDims;
vector<vector<float>> in;
vector<vector<float>> tests;
readTests<float, float, float>(string(TEST_DIR "/grad/grad3D.test"),
numDims, in, tests);
dim4 idims = numDims[0];
array input(idims, &(in[0].front()));
array g0, g1;
grad(g0, g1, input);
size_t nElems = tests[resultIdx0].size();
// Get result
float* grad0Data = new float[tests[resultIdx0].size()];
g0.host((void*)grad0Data);
// Compare result
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(tests[resultIdx0][elIter], grad0Data[elIter])
<< "at: " << elIter << endl;
}
// Get result
float* grad1Data = new float[tests[resultIdx1].size()];
g1.host((void*)grad1Data);
// Compare result
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(tests[resultIdx1][elIter], grad1Data[elIter])
<< "at: " << elIter << endl;
}
// Delete
delete[] grad0Data;
delete[] grad1Data;
}
TEST(Grad, MaxDim) {
using af::constant;
using af::sum;
const size_t largeDim = 65535 * 8 + 1;
array input = constant(1, 2, largeDim);
array g0, g1;
grad(g0, g1, input);
ASSERT_EQ(0.f, sum<float>(g0));
ASSERT_EQ(0.f, sum<float>(g1));
}