forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmean.cpp
More file actions
209 lines (167 loc) · 5.52 KB
/
mean.cpp
File metadata and controls
209 lines (167 loc) · 5.52 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
/*******************************************************
* 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 <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <string>
#include <vector>
#include <testHelpers.hpp>
using std::string;
using std::vector;
using af::cdouble;
using af::cfloat;
template<typename T>
class Mean : public ::testing::Test
{
public:
virtual void SetUp() {}
};
// create a list of types to be tested
typedef ::testing::Types<cdouble, cfloat, float, double, int, uint, intl, uintl, char, uchar> TestTypes;
// register the type list
TYPED_TEST_CASE(Mean, TestTypes);
template<typename T>
struct f32HelperType {
typedef typename cond_type<is_same_type<T, double>::value,
double,
float>::type type;
};
template<typename T>
struct c32HelperType {
typedef typename cond_type<is_same_type<T, cfloat>::value,
cfloat,
typename f32HelperType<T>::type >::type type;
};
template<typename T>
struct elseType {
typedef typename cond_type< is_same_type<T, uintl>::value ||
is_same_type<T, intl>::value,
double,
T>::type type;
};
template<typename T>
struct meanOutType {
typedef typename cond_type< is_same_type<T, float>::value ||
is_same_type<T, int>::value ||
is_same_type<T, uint>::value ||
is_same_type<T, uchar>::value ||
is_same_type<T, char>::value,
float,
typename elseType<T>::type>::type type;
};
template<typename T>
void meanDimTest(string pFileName, dim_t dim)
{
typedef typename meanOutType<T>::type outType;
if (noDoubleTests<T>()) return;
if (noDoubleTests<outType>()) return;
vector<af::dim4> numDims;
vector<vector<int> > in;
vector<vector<float> > tests;
readTestsFromFile<int,float>(pFileName, numDims, in, tests);
af::dim4 dims = numDims[0];
af_array outArray = 0;
af_array inArray = 0;
vector<T> input(in[0].begin(), in[0].end());
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(input.front()),
dims.ndims(), dims.get(), (af_dtype)af::dtype_traits<T>::af_type));
ASSERT_EQ(AF_SUCCESS, af_mean(&outArray, inArray, dim));
outType *outData = new outType[dims.elements()];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
vector<outType> currGoldBar(tests[0].begin(), tests[0].end());
size_t nElems = currGoldBar.size();
for (size_t elIter=0; elIter<nElems; ++elIter) {
ASSERT_NEAR(::real(currGoldBar[elIter]), ::real(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
ASSERT_NEAR(::imag(currGoldBar[elIter]), ::imag(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
}
// cleanup
delete[] outData;
ASSERT_EQ(AF_SUCCESS, af_release_array(inArray));
ASSERT_EQ(AF_SUCCESS, af_release_array(outArray));
}
TYPED_TEST(Mean, Dim0Matrix)
{
meanDimTest<TypeParam>(string(TEST_DIR"/mean/mean_dim0_matrix.test"), 0);
}
TYPED_TEST(Mean, Dim1Cube)
{
meanDimTest<TypeParam>(string(TEST_DIR"/mean/mean_dim1_cube.test"), 1);
}
TYPED_TEST(Mean, Dim0HyperCube)
{
meanDimTest<TypeParam>(string(TEST_DIR"/mean/mean_dim0_hypercube.test"), 0);
}
TYPED_TEST(Mean, Dim2Matrix)
{
meanDimTest<TypeParam>(string(TEST_DIR"/mean/mean_dim2_matrix.test"), 2);
}
TYPED_TEST(Mean, Dim2Cube)
{
meanDimTest<TypeParam>(string(TEST_DIR"/mean/mean_dim2_cube.test"), 2);
}
TYPED_TEST(Mean, Dim2HyperCube)
{
meanDimTest<TypeParam>(string(TEST_DIR"/mean/mean_dim2_hypercube.test"), 2);
}
//////////////////////////////// CPP ////////////////////////////////////
// test mean_all interface using cpp api
#include <iostream>
template<typename T>
void testCPPMean(T const_value, af::dim4 dims)
{
typedef typename meanOutType<T>::type outType;
if (noDoubleTests<T>()) return;
if (noDoubleTests<outType>()) return;
using af::array;
using af::mean;
vector<T> hundred(dims.elements(), const_value);
outType gold = outType(0);
//for(auto i:hundred) gold += i;
for(int i = 0; i < (int)hundred.size(); i++) {
gold += hundred[i];
}
gold /= dims.elements();
array a(dims, &(hundred.front()));
outType output = mean<outType>(a);
ASSERT_NEAR(::real(output), ::real(gold), 1.0e-3);
ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-3);
}
TEST(Mean, CPP_f64)
{
testCPPMean<double>(2.1, af::dim4(10, 10, 1, 1));
}
TEST(Mean, CPP_f32)
{
testCPPMean<float>(2.1f, af::dim4(10, 5, 2, 1));
}
TEST(Mean, CPP_s32)
{
testCPPMean<int>(2, af::dim4(5, 5, 2, 2));
}
TEST(Mean, CPP_u32)
{
testCPPMean<unsigned>(2, af::dim4(100, 1, 1, 1));
}
TEST(Mean, CPP_s8)
{
testCPPMean<char>(2, af::dim4(5, 5, 2, 2));
}
TEST(Mean, CPP_u8)
{
testCPPMean<uchar>(2, af::dim4(100, 1, 1, 1));
}
TEST(Mean, CPP_cfloat)
{
testCPPMean<cfloat>(cfloat(2.1f), af::dim4(10, 5, 2, 1));
}
TEST(Mean, CPP_cdouble)
{
testCPPMean<cdouble>(cdouble(2.1), af::dim4(10, 10, 1, 1));
}