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
|
/*******************************************************
* 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 <ctime>
#include <iostream>
#include <algorithm>
#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, short, ushort> 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, short> ::value ||
is_same_type<T, ushort> ::value ||
is_same_type<T, char> ::value,
float,
typename elseType<T>::type>::type type;
};
template<typename T>
void meanDimTest(string pFileName, dim_t dim, bool isWeighted=false)
{
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);
if (!isWeighted) {
af::dim4 dims = numDims[0];
vector<T> input(in[0].begin(), in[0].end());
af::array inArray(dims, &(input.front()));
af::array outArray = af::mean(inArray, dim);
outType *outData = new outType[dims.elements()];
outArray.host((void*)outData);
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;
} else {
af::dim4 dims = numDims[0];
af::dim4 wdims = numDims[1];
vector<T> input(in[0].begin(), in[0].end());
vector<float> weights(in[1].begin(), in[1].end());
af::array inArray(dims, &(input.front()));
af::array wtsArray(wdims, &(weights.front()));
af::array outArray = af::mean(inArray, wtsArray, dim);
outType *outData = new outType[dims.elements()];
outArray.host((void*)outData);
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;
}
}
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);
}
TYPED_TEST(Mean, Wtd_Dim0Matrix)
{
meanDimTest<TypeParam>(string(TEST_DIR "/mean/wtd_mean_dim0_mat.test"), 0, true);
}
TYPED_TEST(Mean, Wtd_Dim1Matrix)
{
meanDimTest<TypeParam>(string(TEST_DIR "/mean/wtd_mean_dim1_mat.test"), 1, true);
}
template<typename T>
void meanAllTest(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 = gold + hundred[i];
}
gold = 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(MeanAll, f64)
{
meanAllTest<double>(2.1, af::dim4(10, 10, 1, 1));
}
TEST(MeanAll, f32)
{
meanAllTest<float>(2.1f, af::dim4(10, 5, 2, 1));
}
TEST(MeanAll, s32)
{
meanAllTest<int>(2, af::dim4(5, 5, 2, 2));
}
TEST(MeanAll, u32)
{
meanAllTest<unsigned>(2, af::dim4(100, 1, 1, 1));
}
TEST(MeanAll, s8)
{
meanAllTest<char>(2, af::dim4(5, 5, 2, 2));
}
TEST(MeanAll, u8)
{
meanAllTest<uchar>(2, af::dim4(100, 1, 1, 1));
}
TEST(MeanAll, c32)
{
meanAllTest<cfloat>(cfloat(2.1f), af::dim4(10, 5, 2, 1));
}
TEST(MeanAll, s16)
{
meanAllTest<short>(2, af::dim4(5, 5, 2, 2));
}
TEST(MeanAll, u16)
{
meanAllTest<ushort>(2, af::dim4(100, 1, 1, 1));
}
TEST(MeanAll, c64)
{
meanAllTest<cdouble>(cdouble(2.1), af::dim4(10, 10, 1, 1));
}
template<typename T>
T random() { return T(std::rand()%10); }
template<> cfloat random<cfloat>() { return cfloat(float(std::rand()%10), float(std::rand()%10)); }
template<> cdouble random<cdouble>() { return cdouble(double(std::rand()%10), double(std::rand()%10)); }
template<typename T>
class WeightedMean : public ::testing::Test
{
public:
virtual void SetUp() {}
};
// register the type list
TYPED_TEST_CASE(WeightedMean, TestTypes);
template<typename T, typename wtsType>
void weightedMeanAllTest(af::dim4 dims)
{
typedef typename meanOutType<T>::type outType;
if (noDoubleTests<T>()) return;
if (noDoubleTests<outType>()) return;
if (noDoubleTests<wtsType>()) return;
using af::array;
using af::mean;
std::srand(std::time(0));
vector<T> data(dims.elements());
vector<wtsType> wts(dims.elements());
std::generate(data.begin(), data.end(), random<T>);
std::generate(wts.begin(), wts.end(), random<wtsType>);
outType wtdSum = outType(0);
wtsType wtsSum = wtsType(0);
for(int i = 0; i < (int)data.size(); i++) {
wtdSum = wtdSum + data[i]*wts[i];
wtsSum = wtsSum + wts[i];
}
outType gold = wtdSum / wtsSum;
array a(dims, &(data.front()));
array w(dims, &(wts.front()));
outType output = mean<outType>(a, w);
ASSERT_NEAR(::real(output), ::real(gold), 1.0e-2);
ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-2);
}
TYPED_TEST(WeightedMean, Basic)
{
weightedMeanAllTest<TypeParam, float>(af::dim4(32, 30, 33, 17));
}
|