forked from hunter-packages/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_index.cpp
More file actions
173 lines (139 loc) · 5.47 KB
/
sort_index.cpp
File metadata and controls
173 lines (139 loc) · 5.47 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
/*******************************************************
* 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/defines.h>
#include <af/traits.hpp>
#include <vector>
#include <iostream>
#include <complex>
#include <string>
#include <testHelpers.hpp>
using std::vector;
using std::string;
using std::cout;
using std::endl;
using af::cfloat;
using af::cdouble;
template<typename T>
class SortIndex : 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, uint, int, uchar, short, ushort, intl, uintl> TestTypes;
// register the type list
TYPED_TEST_CASE(SortIndex, TestTypes);
template<typename T>
void sortTest(string pTestFile, const bool dir, const unsigned resultIdx0, const unsigned resultIdx1, bool isSubRef = false, const vector<af_seq> * seqv = NULL)
{
if (noDoubleTests<T>()) return;
vector<af::dim4> numDims;
vector<vector<T> > in;
vector<vector<float> > tests;
readTests<T, float, int>(pTestFile,numDims,in,tests);
af::dim4 idims = numDims[0];
af_array inArray = 0;
af_array tempArray = 0;
af_array sxArray = 0;
af_array ixArray = 0;
if (isSubRef) {
ASSERT_EQ(AF_SUCCESS, af_create_array(&tempArray, &(in[0].front()), idims.ndims(), idims.get(), (af_dtype) af::dtype_traits<T>::af_type));
ASSERT_EQ(AF_SUCCESS, af_index(&inArray, tempArray, seqv->size(), &seqv->front()));
} else {
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in[0].front()), idims.ndims(), idims.get(), (af_dtype) af::dtype_traits<T>::af_type));
}
ASSERT_EQ(AF_SUCCESS, af_sort_index(&sxArray, &ixArray, inArray, 0, dir));
size_t nElems = tests[resultIdx0].size();
// Get result
T* sxData = new T[tests[resultIdx0].size()];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)sxData, sxArray));
// Compare result
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(tests[resultIdx0][elIter], sxData[elIter]) << "at: " << elIter << std::endl;
}
// Get result
unsigned* ixData = new unsigned[tests[resultIdx1].size()];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)ixData, ixArray));
#ifndef AF_OPENCL
// Compare result
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(tests[resultIdx1][elIter], ixData[elIter]) << "at: " << elIter << std::endl;
}
#endif
// Delete
delete[] sxData;
delete[] ixData;
if(inArray != 0) af_release_array(inArray);
if(sxArray != 0) af_release_array(sxArray);
if(ixArray != 0) af_release_array(ixArray);
if(tempArray != 0) af_release_array(tempArray);
}
#define SORT_INIT(desc, file, dir, resultIdx0, resultIdx1) \
TYPED_TEST(SortIndex, desc) \
{ \
sortTest<TypeParam>(string(TEST_DIR"/sort/"#file".test"), dir, resultIdx0, resultIdx1); \
}
SORT_INIT(Sort0True, sort, true, 0, 1);
SORT_INIT(Sort0False, sort,false, 2, 3);
SORT_INIT(Sort2d0False, basic_2d, true, 0, 1);
SORT_INIT(Sort10x10True, sort_10x10, true, 0, 1);
SORT_INIT(Sort10x10False, sort_10x10, false, 2, 3);
SORT_INIT(Sort1000True, sort_1000, true, 0, 1);
SORT_INIT(SortMedTrue, sort_med1, true, 0, 1);
SORT_INIT(Sort1000False, sort_1000, false, 2, 3);
SORT_INIT(SortMedFalse, sort_med1, false, 2, 3);
// Takes too much time in current implementation. Enable when everything is parallel
//SORT_INIT(SortMed5True, sort_med, true, 0, 1);
//SORT_INIT(SortMed5False, sort_med, false, 2, 3);
//SORT_INIT(SortLargeTrue, sort_large, true, 0, 1);
//SORT_INIT(SortLargeFalse, sort_large, false, 2, 3);
;
//////////////////////////////////// CPP /////////////////////////////////
//
TEST(SortIndex, CPP)
{
if (noDoubleTests<float>()) return;
const bool dir = true;
const unsigned resultIdx0 = 0;
const unsigned resultIdx1 = 1;
vector<af::dim4> numDims;
vector<vector<float> > in;
vector<vector<float> > tests;
readTests<float, float, int>(string(TEST_DIR"/sort/sort_10x10.test"),numDims,in,tests);
af::dim4 idims = numDims[0];
af::array input(idims, &(in[0].front()));
af::array outValues, outIndices;
af::sort(outValues, outIndices, input, 0, dir);
size_t nElems = tests[resultIdx0].size();
// Get result
float* sxData = new float[tests[resultIdx0].size()];
outValues.host((void*)sxData);
// Compare result
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(tests[resultIdx0][elIter], sxData[elIter]) << "at: " << elIter << std::endl;
}
// Get result
unsigned* ixData = new unsigned[tests[resultIdx1].size()];
outIndices.host((void*)ixData);
// Compare result
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(tests[resultIdx1][elIter], ixData[elIter]) << "at: " << elIter << std::endl;
}
// Delete
delete[] sxData;
delete[] ixData;
}