-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcOpenMP.c
344 lines (279 loc) · 6.2 KB
/
funcOpenMP.c
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <sys/time.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <omp.h>
/* I/O routines */
FILE *open_traindata(char *trainfile)
{
FILE *fp;
fp = fopen(trainfile, "r");
if (fp == NULL) {
printf("traindata; File %s not available\n", trainfile);
exit(1);
}
return fp;
}
FILE *open_querydata(char *queryfile)
{
FILE *fp;
fp = fopen(queryfile, "r");
if (fp == NULL) {
printf("querydata: File %s not available\n", queryfile);
exit(1);
}
return fp;
}
double read_nextnum(FILE *fp)
{
double val;
int c = fscanf(fp, "%lf", &val);
if (c <= 0) {
fprintf(stderr, "fscanf returned %d\n", c);
exit(1);
}
return val;
}
/* Timer */
double gettime()
{
struct timeval tv;
gettimeofday(&tv, 0);
return (double) (tv.tv_sec+tv.tv_usec/1000000.0);
}
/* Function to approximate */
double fitfun(double *x, int n)
{
double f = 0.0;
int i;
#if 1
for(i=0; i<n; i++) /* circle */
f += x[i]*x[i];
#endif
#if 0
for(i=0; i<n-1; i++) { /* himmelblau */
f = f + pow((x[i]*x[i]+x[i+1]-11.0),2) + pow((x[i]+x[i+1]*x[i+1]-7.0),2);
}
#endif
#if 0
for (i=0; i<n-1; i++) /* rosenbrock */
f = f + 100.0*pow((x[i+1]-x[i]*x[i]),2) + pow((x[i]-1.0),2);
#endif
#if 0
for (i=0; i<n; i++) /* rastrigin */
f = f + pow(x[i],2) + 10.0 - 10.0*cos(2*M_PI*x[i]);
#endif
return f;
}
/* random number generator */
#define SEED_RAND() srand48(1)
#define URAND() drand48()
#ifndef LB
#define LB -1.0
#endif
#ifndef UB
#define UB 1.0
#endif
double get_rand(int k)
{
return (UB-LB)*URAND()+LB;
}
/* utils */
double compute_min(double *v, int n)
{
int i;
double vmin = v[0];
for (i = 1; i < n; i++)
if (v[i] < vmin) vmin = v[i];
return vmin;
}
double compute_max(double *v, int n)
{
int i;
double vmax = v[0];
for (i = 1; i < n; i++)
if (v[i] > vmax) vmax = v[i];
return vmax;
}
double compute_sum(double *v, int n)
{
int i;
double s = 0;
for (i = 0; i < n; i++) s += v[i];
return s;
}
double compute_sum_pow(double *v, int n, int p)
{
int i;
double s = 0;
for (i = 0; i < n; i++) s += pow(v[i], p);
return s;
}
double compute_mean(double *v, int n)
{
int i;
double s = 0;
for (i = 0; i < n; i++) s += v[i];
return s/n;
}
double compute_std(double *v, int n, double mean)
{
int i;
double s = 0;
for (i = 0; i < n; i++) s += pow(v[i]-mean,2);
return sqrt(s/(n-1));
}
double compute_var(double *v, int n, double mean)
{
int i;
double s = 0;
for (i = 0; i < n; i++) s += pow(v[i]-mean,2);
return s/n;
}
double compute_dist(double *v, double *w, int n)
{
int i;
double s = 0.0;
for (i = 0; i < n; i++) {
s+= pow(v[i]-w[i],2);
}
return sqrt(s);
}
double compute_max_pos(double *v, int n, int *pos)
{
int i, p = 0;
double vmax = v[0];
for (i = 1; i < n; i++)
if (v[i] > vmax) {
vmax = v[i];
p = i;
}
*pos = p;
return vmax;
}
double compute_min_pos(double *v, int n, int *pos)
{
int i, p = 0;
double vmin = v[0];
for (i = 1; i < n; i++)
if (v[i] < vmin) {
vmin = v[i];
p = i;
}
*pos = p;
return vmin;
}
double compute_root(double dist, int norm)
{
if (dist == 0) return 0;
switch (norm) {
case 2:
return sqrt(dist);
case 1:
case 0:
return dist;
default:
return pow(dist, 1 / (double) norm);
}
}
double compute_distance(double *pat1, double *pat2, int lpat, int norm)
{
register int i;
double dist = 0.0;
for (i = 0; i < lpat; i++) {
double diff = 0.0;
diff = pat1[i] - pat2[i];
switch (norm) {
double adiff;
case 2:
dist += diff * diff;
break;
case 1:
dist += fabs(diff);
break;
case 0:
if ((adiff = fabs(diff)) > dist)
dist = adiff;
break;
default:
dist += pow(fabs(diff), (double) norm);
break;
}
}
return dist; // compute_root(dist);
}
//ftiaksame quicksort
void quicksort(double* arr, int* indices, int left, int right) {
if (left >= right)
return;
// Choose the pivot element (e.g., the middle element)
int pivotIndex = (left + right) / 2;
double pivotValue = arr[pivotIndex];
// Partition the array around the pivot
int i = left;
int j = right;
while (i <= j) {
while (arr[i] < pivotValue)
i++;
while (arr[j] > pivotValue)
j--;
if (i <= j) {
// Swap elements at indices i and j
double tempValue = arr[i];
arr[i] = arr[j];
arr[j] = tempValue;
// Swap corresponding indices
int tempIndex = indices[i];
indices[i] = indices[j];
indices[j] = tempIndex;
i++;
j--;
}
}
// Recursively sort the sub-arrays
quicksort(arr, indices, left, j);
quicksort(arr, indices, i, right);
}
void compute_knn_brute_force(double **xdata, double *q, int npat, int lpat, int knn, int *nn_x, double *nn_d)
{
int i, max_i = 0;
double max_d = 0, new_d = 0;
// Initialize pairs of index and distance
#pragma omp parallel for shared(nn_x, nn_d) private(i)
for (i = 0; i < knn; i++) {
nn_x[i] = -1;
nn_d[i] = 1e99 - i;
}
max_d = compute_max_pos(nn_d, knn, &max_i);
#pragma omp parallel for shared(xdata, q, nn_x, nn_d, max_d) private(i, new_d)
for (i = 0; i < npat; i++) {
new_d = compute_dist(q, xdata[i], lpat); // euclidean
if (new_d < max_d) { // add point to the list of knns, replace element max_i
nn_x[max_i] = i;
nn_d[max_i] = new_d;
max_d = compute_max_pos(nn_d, knn, &max_i);
}
}
// Sort the knn list
//valame quicksort
quicksort(nn_d, nn_x, 0, knn - 1);
}
/* compute an approximation based on the values of the neighbors */
double predict_value(int dim, int knn, double *xdata, double *ydata, double *point, double *dist)
{
int i;
double sum_v = 0.0;
double sum_weight = 0.0;
double weight;
// Inverse distance weighted average
for (i = 0; i < knn; i++) {
weight = 1.0 / dist[i]; // Calculate the weight as the inverse of the distance
sum_v += weight * ydata[i]; // Weighted sum of function values
sum_weight += weight; // Sum of weights
}
if (sum_weight != 0.0) {
return sum_v / sum_weight; // Weighted average
} else {
return 0.0; // Handle the case when all distances are zero (to avoid division by zero)
}
}