-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathhamming.cpp
More file actions
199 lines (158 loc) · 5.8 KB
/
hamming.cpp
File metadata and controls
199 lines (158 loc) · 5.8 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
/*******************************************************
* 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 <arrayfire.h>
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <string>
#include <vector>
using af::cdouble;
using af::cfloat;
using af::dtype_traits;
using std::endl;
using std::string;
using std::vector;
template<typename T>
class HammingMatcher8 : public ::testing::Test {
public:
virtual void SetUp() {}
};
template<typename T>
class HammingMatcher32 : public ::testing::Test {
public:
virtual void SetUp() {}
};
// create lists of types to be tested
typedef ::testing::Types<uchar, ushort> TestTypes8;
typedef ::testing::Types<uint, uintl> TestTypes32;
// register the type list
TYPED_TEST_SUITE(HammingMatcher8, TestTypes8);
TYPED_TEST_SUITE(HammingMatcher32, TestTypes32);
template<typename T>
void hammingMatcherTest(string pTestFile, int feat_dim) {
using af::dim4;
vector<dim4> numDims;
vector<vector<uint>> in32;
vector<vector<uint>> tests;
readTests<uint, uint, int>(pTestFile, numDims, in32, tests);
vector<vector<T>> in(in32.size());
for (size_t i = 0; i < in32[0].size(); i++) in[0].push_back((T)in32[0][i]);
for (size_t i = 0; i < in32[1].size(); i++) in[1].push_back((T)in32[1][i]);
dim4 qDims = numDims[0];
dim4 tDims = numDims[1];
af_array query = 0;
af_array train = 0;
af_array idx = 0;
af_array dist = 0;
ASSERT_SUCCESS(af_create_array(&query, &(in[0].front()), qDims.ndims(),
qDims.get(),
(af_dtype)dtype_traits<T>::af_type));
ASSERT_SUCCESS(af_create_array(&train, &(in[1].front()), tDims.ndims(),
tDims.get(),
(af_dtype)dtype_traits<T>::af_type));
ASSERT_SUCCESS(af_hamming_matcher(&idx, &dist, query, train, feat_dim, 1));
vector<uint> goldIdx = tests[0];
vector<uint> goldDist = tests[1];
size_t nElems = goldIdx.size();
uint *outIdx = new uint[nElems];
uint *outDist = new uint[nElems];
ASSERT_SUCCESS(af_get_data_ptr((void *)outIdx, idx));
ASSERT_SUCCESS(af_get_data_ptr((void *)outDist, dist));
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(goldDist[elIter], outDist[elIter])
<< "at: " << elIter << endl;
}
delete[] outIdx;
delete[] outDist;
ASSERT_SUCCESS(af_release_array(query));
ASSERT_SUCCESS(af_release_array(train));
ASSERT_SUCCESS(af_release_array(idx));
ASSERT_SUCCESS(af_release_array(dist));
}
TYPED_TEST(HammingMatcher8, Hamming_500_5000_Dim0) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
hammingMatcherTest<TypeParam>(
string(TEST_DIR "/hamming/hamming_500_5000_dim0_u8.test"), 0);
}
TYPED_TEST(HammingMatcher8, Hamming_500_5000_Dim1) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
hammingMatcherTest<TypeParam>(
string(TEST_DIR "/hamming/hamming_500_5000_dim1_u8.test"), 1);
}
TYPED_TEST(HammingMatcher32, Hamming_500_5000_Dim0) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
hammingMatcherTest<TypeParam>(
string(TEST_DIR "/hamming/hamming_500_5000_dim0_u32.test"), 0);
}
TYPED_TEST(HammingMatcher32, Hamming_500_5000_Dim1) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
hammingMatcherTest<TypeParam>(
string(TEST_DIR "/hamming/hamming_500_5000_dim1_u32.test"), 1);
}
///////////////////////////////////// CPP ////////////////////////////////
//
TEST(HammingMatcher, CPP) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
using af::array;
using af::dim4;
vector<dim4> numDims;
vector<vector<uint>> in;
vector<vector<uint>> tests;
readTests<uint, uint, int>(
TEST_DIR "/hamming/hamming_500_5000_dim0_u32.test", numDims, in, tests);
dim4 qDims = numDims[0];
dim4 tDims = numDims[1];
array query(qDims, &(in[0].front()));
array train(tDims, &(in[1].front()));
array idx, dist;
hammingMatcher(idx, dist, query, train, 0, 1);
vector<uint> goldIdx = tests[0];
vector<uint> goldDist = tests[1];
size_t nElems = goldIdx.size();
uint *outIdx = new uint[nElems];
uint *outDist = new uint[nElems];
idx.host(outIdx);
dist.host(outDist);
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(goldDist[elIter], outDist[elIter])
<< "at: " << elIter << endl;
}
delete[] outIdx;
delete[] outDist;
}
TEST(HammingMatcher64bit, CPP) {
UNSUPPORTED_BACKEND(AF_BACKEND_ONEAPI);
using af::array;
using af::dim4;
vector<dim4> numDims;
vector<vector<unsigned long long>> in;
vector<vector<unsigned long long>> tests;
readTests<unsigned long long, unsigned long long, int>(
TEST_DIR "/hamming/hamming_500_5000_dim0_u32.test", numDims, in, tests);
dim4 qDims = numDims[0];
dim4 tDims = numDims[1];
array query(qDims, &(in[0].front()));
array train(tDims, &(in[1].front()));
array idx, dist;
hammingMatcher(idx, dist, query, train, 0, 1);
vector<unsigned long long> goldIdx = tests[0];
vector<unsigned long long> goldDist = tests[1];
size_t nElems = goldIdx.size();
uint *outIdx = new uint[nElems];
uint *outDist = new uint[nElems];
idx.host(outIdx);
dist.host(outDist);
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(goldDist[elIter], outDist[elIter])
<< "at: " << elIter << endl;
}
delete[] outIdx;
delete[] outDist;
}