File: gaussiankernel.cpp

package info (click to toggle)
arrayfire 3.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 109,016 kB
  • sloc: cpp: 127,909; lisp: 6,878; python: 3,923; ansic: 1,051; sh: 347; makefile: 338; xml: 175
file content (159 lines) | stat: -rw-r--r-- 4,230 bytes parent folder | download
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
/*******************************************************
 * Copyright (c) 2015, 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;

template<typename T>
class GaussianKernel : public ::testing::Test
{
    public:
        virtual void SetUp() {}
};

// create a list of types to be tested
typedef ::testing::Types<float> TestTypes;

// register the type list
TYPED_TEST_CASE(GaussianKernel, TestTypes);

template<typename T>
void gaussianKernelTest(string pFileName, double sigma)
{
    if (noDoubleTests<T>()) return;

    vector<af::dim4>     numDims;
    vector<vector<int> > in;
    vector<vector<T> >   tests;

    readTestsFromFile<int,T>(pFileName, numDims, in, tests);

    af_array outArray  = 0;

    vector<int> input(in[0].begin(), in[0].end());

    ASSERT_EQ(AF_SUCCESS, af_gaussian_kernel(&outArray, input[0], input[1], sigma, sigma));

    dim_t outElems = 0;
    ASSERT_EQ(AF_SUCCESS, af_get_elements(&outElems, outArray));
    T *outData = new T[outElems];

    ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));

    vector<T> currGoldBar(tests[0].begin(), tests[0].end());
    size_t nElems = currGoldBar.size();

    ASSERT_EQ(outElems, (dim_t)nElems);

    for (size_t elIter=0; elIter<nElems; ++elIter) {
        ASSERT_NEAR(currGoldBar[elIter], outData[elIter], 1.0e-3)<< "at: " << elIter<< std::endl;
    }

    delete[] outData;
    ASSERT_EQ(AF_SUCCESS, af_release_array(outArray));
}

TYPED_TEST(GaussianKernel, Small1D)
{
    gaussianKernelTest<TypeParam>(string(TEST_DIR"/gaussian/gauss1_7.test"), 0.0);
}

TYPED_TEST(GaussianKernel, Large1D)
{
    gaussianKernelTest<TypeParam>(string(TEST_DIR"/gaussian/gauss1_15.test"), 0.0);
}

TYPED_TEST(GaussianKernel, Small1DWithSigma)
{
    gaussianKernelTest<TypeParam>(string(TEST_DIR"/gaussian/gauss1_7_sigma1.test"), 1.0);
}

TYPED_TEST(GaussianKernel, SmallSmall2D)
{
    gaussianKernelTest<TypeParam>(string(TEST_DIR"/gaussian/gauss2_7x7.test"), 0.0);
}

TYPED_TEST(GaussianKernel, LargeSmall2D)
{
    gaussianKernelTest<TypeParam>(string(TEST_DIR"/gaussian/gauss2_15x7.test"), 0.0);
}

TYPED_TEST(GaussianKernel, LargeLarge2D)
{
    gaussianKernelTest<TypeParam>(string(TEST_DIR"/gaussian/gauss2_15x15.test"), 0.0);
}

TYPED_TEST(GaussianKernel, SmallSmall2DWithSigma)
{
    gaussianKernelTest<TypeParam>(string(TEST_DIR"/gaussian/gauss2_7x7_sigma1.test"), 1.0);
}

//////////////////////////////// CPP ////////////////////////////////////
// test mean_all interface using cpp api

#include <iostream>

void gaussianKernelTestCPP(string pFileName, double sigma)
{
    using af::array;
    using af::gaussianKernel;

    vector<af::dim4>       numDims;
    vector<vector<int> >   in;
    vector<vector<float> > tests;

    readTestsFromFile<int,float>(pFileName, numDims, in, tests);

    vector<int> input(in[0].begin(), in[0].end());

    array out = gaussianKernel(input[0], input[1], sigma, sigma);

    dim_t outElems = out.elements();
    float *outData = new float[outElems];
    out.host(outData);

    vector<float> currGoldBar(tests[0].begin(), tests[0].end());
    size_t nElems = currGoldBar.size();

    ASSERT_EQ(outElems, (dim_t)nElems);

    for (size_t elIter=0; elIter<nElems; ++elIter) {
        ASSERT_NEAR(currGoldBar[elIter], outData[elIter], 1.0e-3)<< "at: " << elIter<< std::endl;
    }

    delete[] outData;
}

TEST(GaussianKernel, Small1D_CPP)
{
    gaussianKernelTestCPP(string(TEST_DIR"/gaussian/gauss1_7.test"), 0.0);
}

TEST(GaussianKernel, Small1DWithSigma_CPP)
{
    gaussianKernelTestCPP(string(TEST_DIR"/gaussian/gauss1_7_sigma1.test"), 1.0);
}

TEST(GaussianKernel, SmallSmall2D_CPP)
{
    gaussianKernelTestCPP(string(TEST_DIR"/gaussian/gauss2_7x7.test"), 0.0);
}

TEST(GaussianKernel, SmallSmall2DWithSigma_CPP)
{
    gaussianKernelTestCPP(string(TEST_DIR"/gaussian/gauss2_7x7_sigma1.test"), 1.0);
}