-
Notifications
You must be signed in to change notification settings - Fork 1
/
myknnOpenMP.c
140 lines (104 loc) · 2.94 KB
/
myknnOpenMP.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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#ifndef PROBDIM
#define PROBDIM 2
#endif
#include "func.c"
static double **xdata;
static double ydata[TRAINELEMS];
#define MAX_NNB 256
double find_knn_value(double *p, int n, int knn)
{
int nn_x[MAX_NNB];
double nn_d[MAX_NNB];
compute_knn_brute_force(xdata, p, TRAINELEMS, PROBDIM, knn, nn_x, nn_d); // brute-force /linear search
int dim = PROBDIM;
int nd = knn; // number of points
double xd[MAX_NNB*PROBDIM]; // points
double fd[MAX_NNB]; // function values
for (int i = 0; i < knn; i++) {
fd[i] = ydata[nn_x[i]];
}
for (int i = 0; i < knn; i++) {
for (int j = 0; j < PROBDIM; j++) {
xd[i*dim+j] = xdata[nn_x[i]][j];
}
}
double fi;
fi = predict_value(dim, nd, xd, fd, p, nn_d);
return fi;
}
int main(int argc, char *argv[])
{
double x[PROBDIM];
if (argc != 3)
{
printf("usage: %s <trainfile> <queryfile>\n", argv[0]);
exit(1);
}
char *trainfile = argv[1];
char *queryfile = argv[2];
double *xmem = (double *)malloc(TRAINELEMS*PROBDIM*sizeof(double));
xdata = (double **)malloc(TRAINELEMS*sizeof(double *));
#pragma omp parallel
for (int i = 0; i < TRAINELEMS; i++) {
xdata[i] = xmem + i*PROBDIM; //&mem[i*PROBDIM];
}
FILE *fpin = open_traindata(trainfile);
#pragma omp single nowait
for (int i=0;i<TRAINELEMS;i++) {
for (int k = 0; k < PROBDIM; k++)
xdata[i][k] = read_nextnum(fpin);
#if defined(SURROGATES)
ydata[i] = read_nextnum(fpin);
#else
ydata[i] = 0;
#endif
}
fclose(fpin);
fpin = open_querydata(queryfile);
//FILE *fpout = fopen("output.knn.txt","w");
double *y = malloc(QUERYELEMS*sizeof(double));
double t0, t1, t_first = 0.0, t_sum = 0.0;
double sse = 0.0;
double err, err_sum = 0.0;
for (int i=0;i<QUERYELEMS;i++) { /* requests */
for (int k = 0; k < PROBDIM; k++)
x[k] = read_nextnum(fpin);
#if defined(SURROGATES)
y[i] = read_nextnum(fpin);
#else
y[i] = 0.0;
#endif
t0 = gettime();
double yp = find_knn_value(x, PROBDIM, NNBS);
t1 = gettime();
t_sum += (t1-t0);
if (i == 0) t_first = (t1-t0);
sse += (y[i]-yp)*(y[i]-yp);
//for (k = 0; k < PROBDIM; k++)
// fprintf(fpout,"%.5f ", x[k]);
err = 100.0*fabs((yp-y[i])/y[i]);
//fprintf(fpout,"%.5f %.5f %.2f\n", y[i], yp, err);
err_sum += err;
}
fclose(fpin);
//fclose(fpout);
double mse = sse/QUERYELEMS;
double ymean = compute_mean(y, QUERYELEMS);
double var = compute_var(y, QUERYELEMS, ymean);
double r2 = 1-(mse/var);
printf("Results for %d query points\n", QUERYELEMS);
printf("APE = %.2f %%\n", err_sum/QUERYELEMS);
printf("MSE = %.6f\n", mse);
printf("R2 = 1 - (MSE/Var) = %.6lf\n", r2);
t_sum = t_sum*1000.0; // convert to ms
t_first = t_first*1000.0; // convert to ms
printf("Total time = %lf ms\n", t_sum);
printf("Time for 1st query = %lf ms\n", t_first);
printf("Time for 2..N queries = %lf ms\n", t_sum-t_first);
printf("Average time/query = %lf ms\n", (t_sum-t_first)/(QUERYELEMS-1));
return 0;
}