forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.cpp
More file actions
166 lines (135 loc) · 4.79 KB
/
scan.cpp
File metadata and controls
166 lines (135 loc) · 4.79 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
/*******************************************************
* 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 <af/array.h>
#include <vector>
#include <iostream>
#include <string>
#include <testHelpers.hpp>
#include <af/device.h>
using std::vector;
using std::string;
using std::cout;
using std::endl;
using af::cfloat;
using af::cdouble;
typedef af_err (*scanFunc)(af_array *, const af_array, const int);
template<typename Ti, typename To, scanFunc af_scan>
void scanTest(string pTestFile, int off = 0, bool isSubRef=false, const vector<af_seq> seqv=vector<af_seq>())
{
if (noDoubleTests<Ti>()) return;
vector<af::dim4> numDims;
vector<vector<int> > data;
vector<vector<int> > tests;
readTests<int,int,int> (pTestFile,numDims,data,tests);
af::dim4 dims = numDims[0];
vector<Ti> in(data[0].begin(), data[0].end());
af_array inArray = 0;
af_array outArray = 0;
af_array tempArray = 0;
// Get input array
if (isSubRef) {
ASSERT_EQ(AF_SUCCESS, af_create_array(&tempArray, &in.front(), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<Ti>::af_type));
ASSERT_EQ(AF_SUCCESS, af_index(&inArray, tempArray, seqv.size(), &seqv.front()));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(tempArray));
} else {
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &in.front(), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<Ti>::af_type));
}
// Compare result
for (int d = 0; d < (int)tests.size(); ++d) {
vector<To> currGoldBar(tests[d].begin(), tests[d].end());
// Run sum
ASSERT_EQ(AF_SUCCESS, af_scan(&outArray, inArray, d + off));
// Get result
To *outData;
outData = new To[dims.elements()];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
size_t nElems = currGoldBar.size();
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter
<< " for dim " << d +off
<< std::endl;
}
// Delete
delete[] outData;
ASSERT_EQ(AF_SUCCESS, af_destroy_array(outArray));
}
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
}
vector<af_seq> init_subs()
{
vector<af_seq> subs;
subs.push_back(af_make_seq(2, 6, 1));
subs.push_back(af_make_seq(1, 5, 1));
subs.push_back(af_make_seq(1, 3, 1));
subs.push_back(af_make_seq(1, 2, 1));
return subs;
}
#define SCAN_TESTS(FN, TAG, Ti, To) \
TEST(Scan,Test_##FN##_##TAG) \
{ \
scanTest<Ti, To, af_##FN>( \
string(TEST_DIR"/scan/"#FN".test") \
); \
} \
SCAN_TESTS(accum, float , float , float );
SCAN_TESTS(accum, double , double , double );
SCAN_TESTS(accum, int , int , int );
SCAN_TESTS(accum, cfloat , cfloat , cfloat );
SCAN_TESTS(accum, cdouble , cdouble, cdouble);
SCAN_TESTS(accum, unsigned, unsigned , unsigned );
SCAN_TESTS(accum, uchar , unsigned char, unsigned);
TEST(Scan,Test_Scan_Big0)
{
scanTest<int, int, af_accum>(
string(TEST_DIR"/scan/big0.test"),
0
);
}
TEST(Scan,Test_Scan_Big1)
{
scanTest<int, int, af_accum>(
string(TEST_DIR"/scan/big1.test"),
1
);
}
///////////////////////////////// CPP ////////////////////////////////////
//
TEST(Scan, CPP)
{
vector<af::dim4> numDims;
vector<vector<int> > data;
vector<vector<int> > tests;
readTests<int,int,int> (string(TEST_DIR"/scan/accum.test"),numDims,data,tests);
af::dim4 dims = numDims[0];
vector<float> in(data[0].begin(), data[0].end());
if (noDoubleTests<float>()) return;
af::array input(dims, &(in.front()));
// Compare result
for (int d = 0; d < (int)tests.size(); ++d) {
vector<float> currGoldBar(tests[d].begin(), tests[d].end());
// Run sum
af::array output = af::accum(input, d);
// Get result
float *outData;
outData = new float[dims.elements()];
output.host((void*)outData);
size_t nElems = currGoldBar.size();
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter
<< " for dim " << d
<< std::endl;
}
// Delete
delete[] outData;
}
}