-
Notifications
You must be signed in to change notification settings - Fork 13
/
MKL-ed.cpp
302 lines (224 loc) · 9.61 KB
/
MKL-ed.cpp
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
#include <algorithm>
#include <iostream>
#include <fstream>
#include <iterator>
#include <typeinfo>
#include <numeric>
#include <vector>
#include <cmath>
#include <map>
#include <time.h>
#include "mkl_dfti.h"
#define REG(x) ((x) == 0 ? 1 : x)
//////////////////////////////////////////////////////////////////////////////
// functors
//////////////////////////////////////////////////////////////////////////////
template <class T> struct divide_by {
const T y;
divide_by(const T& y_) : y(y_) {};
T operator() (const T& x) const {return x/y;}
};
template <class T> struct multiply_by {
const T y;
multiply_by(const T& y_) : y(y_) {};
T operator() (const T& x) const {return x*y;}
};
template <class T> struct square {
T operator() (const T& x) const {return x*x;}
T operator() (const T& x, const T& y) const {return x+y*y;}
};
template <class T> struct compose_std {
T operator() (const T& x, const T& y) const {return REG(sqrt(y-x*x));}
};
//////////////////////////////////////////////////////////////////////////////
// windowed prefix sums and time series statistics
//////////////////////////////////////////////////////////////////////////////
template <class Iterator>
int windowed_prefix(Iterator ib, Iterator ie, Iterator rb, size_t w) {
// deduce floating point type from Iterator
typedef typename std::iterator_traits<Iterator>::value_type ftype;
// get length of input
size_t L = ie-ib;
// calculate out of place prefix sum
std::vector<ftype> prefix(L+1, 0);
std::partial_sum(ib, ie, prefix.begin()+1);
// calculate windowed difference
std::transform(prefix.begin()+w, prefix.end(),
prefix.begin(), rb, std::minus<ftype>());
// successful
return 0;
}
template <class Iterator, class F>
int transformed_windowed_prefix(Iterator ib, Iterator ie, Iterator rb,
size_t w, F fn) {
// deduce floating point type from Iterator
typedef typename std::iterator_traits<Iterator>::value_type ftype;
// get length of input
size_t L = ie-ib;
// calculate out of place prefix sum
std::vector<ftype> prefix(L+1, 0);
std::copy(ib, ie, prefix.begin()+1);
std::transform(prefix.begin()+1, prefix.begin()+L+1, prefix.begin()+1, fn);
std::partial_sum(prefix.begin()+1, prefix.begin()+L+1, prefix.begin()+1);
// calculate windowed difference
std::transform(prefix.begin()+w, prefix.end(),
prefix.begin(), rb, std::minus<ftype>());
// successful
return 0;
}
template <class Iterator>
int mu_sigma(Iterator ib, Iterator ie, Iterator mb, Iterator sb, size_t w) {
// deduce floating point type from Iterator
typedef typename std::iterator_traits<Iterator>::value_type ftype;
// get length of input
size_t L = ie-ib;
// get moving average
windowed_prefix(ib, ie, mb, w);
std::transform(mb, mb+L-w+1, mb, divide_by<ftype>(w));
// get first part of variance
transformed_windowed_prefix(ib, ie, sb, w, square<ftype>());
std::transform(sb, sb+L-w+1, sb, divide_by<ftype>(w));
// compose standard deviation
std::transform(mb, mb+L-w+1, sb, sb, compose_std<ftype>());
// successful
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// correlation methods
//////////////////////////////////////////////////////////////////////////////
template <class Iterator>
int correlate(Iterator qb, Iterator qe, Iterator sb, Iterator rb) {
// deduce floating point type from Iterator
typedef typename std::iterator_traits<Iterator>::value_type ftype;
if (typeid(ftype) != typeid(double) && typeid(ftype) != typeid(float)) {
std::cerr << "ERROR: series type must be float or double! (CRITICAL)"
<< std::endl;
// unsuccessful (type error)
return 1;
}
// determine the length of input arrays and allocate fourier space
const size_t L = qe-qb;
const size_t F = (L/2+1)*2;
std::vector<ftype> fftq(F, 0.0);
std::vector<ftype> ffts(F, 0.0);
// prepare forward transform
DFTI_DESCRIPTOR_HANDLE handle;
MKL_LONG status = 0;
if (typeid(ftype) == typeid(double))
status |= DftiCreateDescriptor(&handle, DFTI_DOUBLE, DFTI_REAL, 1, L);
else
status |= DftiCreateDescriptor(&handle, DFTI_SINGLE, DFTI_REAL, 1, L);
// transform query and subject
status |= DftiSetValue(handle, DFTI_PLACEMENT, DFTI_NOT_INPLACE);
status |= DftiCommitDescriptor(handle);
status |= DftiComputeForward(handle, &qb[0], &fftq[0]);
status |= DftiComputeForward(handle, &sb[0], &ffts[0]);
// multipy fourier transforms
for (Iterator pq = fftq.begin(), ps = ffts.begin();
pq < fftq.end(); pq +=2, ps+=2){
// get values and multiply conjugated
const ftype a = *pq, b = *(pq+1), c = *ps, d = *(ps+1);
const ftype real = a*c + b*d, imag = a*d - b*c;
// write down result
*(pq) = real/L; *(pq+1) = imag/L;
}
// write result and free handle
status |= DftiComputeBackward(handle, &fftq[0], &rb[0]);
status |= DftiFreeDescriptor(&handle);
// status
if (status != 0)
std::cerr << "ERROR: error during fourier transform! (CRITICAL)"
<< std::endl;
return static_cast<int>(status);
}
//////////////////////////////////////////////////////////////////////////////
// matching methods
//////////////////////////////////////////////////////////////////////////////
template <class Iterator>
int znorm_local_ed(Iterator qb, Iterator qe,
Iterator sb, Iterator se, Iterator rb) {
// deduce floating point type from Iterator
typedef typename std::iterator_traits<Iterator>::value_type ftype;
if (typeid(ftype) != typeid(double) && typeid(ftype) != typeid(float)) {
std::cerr << "ERROR: series type must be float or double! (CRITICAL)"
<< std::endl;
// unsuccessful (type error)
return 1;
}
if (typeid(ftype) == typeid(float))
std::cerr << "WARNING: single precision may be insufficient "
<< "for long queries or subject time series!" << std::endl;
// sizes of query and subject
size_t M = qe-qb, N = se-sb;
if (M > N) {
std::cerr << "ERROR: query longer than subject! (CRITICAL)"
<< std::endl;
// unsuccessful (length error)
return 2;
}
// remember status during computation
int status = 0;
// calculate statistics of query
const ftype avgQ = std::accumulate(qb, qe, static_cast<ftype>(0))/M;
const ftype stdQ = sqrt(std::accumulate(qb, qe, static_cast<ftype>(0),
square<ftype>())/M-avgQ*avgQ);
// calculate statistics of subject
std::vector<ftype> avgS(N-M+1);
std::vector<ftype> stdS(N-M+1);
status |= mu_sigma(sb, se, avgS.begin(), stdS.begin(), M);
// calculate correlation terms between query and subject
std::vector<ftype> corr(N, 0);
std::copy(qb, qe, corr.begin());
status |= correlate(corr.begin(), corr.end(), sb, corr.begin());
// calculate the final result: (corr-M*muQ*muS)/(M*stdQ*stdS)
std::transform(avgS.begin(), avgS.end(), avgS.begin(),
multiply_by<ftype>(avgQ*M));
std::transform(stdS.begin(), stdS.end(), stdS.begin(),
multiply_by<ftype>(stdQ*M));
std::transform(avgS.begin(), avgS.end(), corr.begin(), corr.begin(),
std::minus<ftype>());
std::transform(corr.begin(), corr.end()-M+1, stdS.begin(), corr.begin(),
std::divides<ftype>());
// copy result
std::copy(corr.begin(), corr.end()-M+1, rb);
// status
return status;
}
int main(int argc, char* argv[]) {
if (argc != 5){
std::cout << "call" << argv[0]
<< " query.bin subject.bin M N" << std::endl;
return 1;
}
int M = atoi(argv[3]);
int N = atoi(argv[4]);
std::cout << "\n= info =====================================" << std::endl;
std::cout << "|Query| = " << M << "\t"
<< "|Subject| = " << N << "\t" << std::endl;
std::vector<double> query(M);
std::vector<double> subject(N);
std::vector<double> result (N);
std::cout << "\n= loading data =============================" << std::endl;
// read query from file
std::ifstream qfile(argv[1], std::ios::binary|std::ios::in);
qfile.read((char *) &query[0], sizeof(double)*M);
// read subject from file
std::ifstream sfile(argv[2], std::ios::binary|std::ios::in);
sfile.read((char *) &subject[0], sizeof(double)*N);
double t1 = clock();
znorm_local_ed(query.begin(), query.end(),
subject.begin(), subject.end(), result.begin());
double bsf = 100000;
int bsf_index= - 1;
for (int i = 0; i < N-M+1; ++i)
if(bsf > result[i]) {
bsf = result[i];
bsf_index=i;
}
double t2 = clock();
std::cout << "\n= result ===================================" << std::endl;
std::cout << "distance: " << sqrt(2*M*(1+bsf)) << std::endl;
std::cout << "location: " << bsf_index << std::endl;
std::cout << "Miliseconds to find best match: " << (t2-t1)/CLOCKS_PER_SEC*1000
<< std::endl;
}