diff options
author | Leo Chan <leochanj@live.unc.edu> | 2020-10-22 01:53:21 -0400 |
---|---|---|
committer | Joshua Bakita <jbakita@cs.unc.edu> | 2020-10-22 01:56:35 -0400 |
commit | d17b33131c14864bd1eae275f49a3f148e21cf29 (patch) | |
tree | 0d8f77922e8d193cb0f6edab83018f057aad64a0 /SD-VBS/benchmarks/svm/src | |
parent | 601ed25a4c5b66cb75315832c15613a727db2c26 (diff) |
Squashed commit of the sb-vbs branch.
Includes the SD-VBS benchmarks modified to:
- Use libextra to loop as realtime jobs
- Preallocate memory before starting their main computation
- Accept input via stdin instead of via argc
Does not include the SD-VBS matlab code.
Fixes libextra execution in LITMUS^RT.
Diffstat (limited to 'SD-VBS/benchmarks/svm/src')
-rw-r--r-- | SD-VBS/benchmarks/svm/src/c/cal_learned_func.c | 42 | ||||
-rw-r--r-- | SD-VBS/benchmarks/svm/src/c/examineExample.c | 67 | ||||
-rw-r--r-- | SD-VBS/benchmarks/svm/src/c/getAlphaFromTrainSet.c | 98 | ||||
-rw-r--r-- | SD-VBS/benchmarks/svm/src/c/polynomial.c | 42 | ||||
-rw-r--r-- | SD-VBS/benchmarks/svm/src/c/script_svm.c | 171 | ||||
-rw-r--r-- | SD-VBS/benchmarks/svm/src/c/svm.h | 38 | ||||
-rw-r--r-- | SD-VBS/benchmarks/svm/src/c/takeStep.c | 188 | ||||
-rw-r--r-- | SD-VBS/benchmarks/svm/src/c/usps_read_partial.c | 85 |
8 files changed, 731 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/svm/src/c/cal_learned_func.c b/SD-VBS/benchmarks/svm/src/c/cal_learned_func.c new file mode 100644 index 0000000..79fdd68 --- /dev/null +++ b/SD-VBS/benchmarks/svm/src/c/cal_learned_func.c | |||
@@ -0,0 +1,42 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include "svm.h" | ||
6 | |||
7 | float cal_learned_func(int k, F2D* a, float* b, int N, F2D* Y, F2D* X, int dim) | ||
8 | { | ||
9 | float s, ret; | ||
10 | int i, j, m, n; | ||
11 | F2D *temp, *temp1; | ||
12 | |||
13 | s=0; | ||
14 | for(i=0; i<N; i++) | ||
15 | { | ||
16 | if( subsref(a,i,0) > 0) | ||
17 | { | ||
18 | temp = fMallocHandle(1, X->width); | ||
19 | temp1 = fMallocHandle(1, X->width); | ||
20 | |||
21 | for(m=0; m<X->width; m++) | ||
22 | { | ||
23 | asubsref(temp,m) = subsref(X,i,m); | ||
24 | asubsref(temp1,m) = subsref(X,k,m); | ||
25 | } | ||
26 | |||
27 | s += asubsref(a,i) * asubsref(Y,i) * polynomial(3, temp, temp1, dim); | ||
28 | |||
29 | free(temp); | ||
30 | free(temp1); | ||
31 | } | ||
32 | } | ||
33 | |||
34 | s = s- arrayref(b,0); | ||
35 | ret = s; | ||
36 | |||
37 | return ret; | ||
38 | } | ||
39 | |||
40 | |||
41 | |||
42 | |||
diff --git a/SD-VBS/benchmarks/svm/src/c/examineExample.c b/SD-VBS/benchmarks/svm/src/c/examineExample.c new file mode 100644 index 0000000..5335f35 --- /dev/null +++ b/SD-VBS/benchmarks/svm/src/c/examineExample.c | |||
@@ -0,0 +1,67 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include "svm.h" | ||
6 | |||
7 | int examineExample(int i, F2D* a, float* b, float C, F2D* e, F2D* X, F2D* Y, float tolerance, int N, float eps, int dim) | ||
8 | { | ||
9 | int ret, j, k, m, n; | ||
10 | float E, r1, randVal; | ||
11 | float maxDiff, temp; | ||
12 | |||
13 | if( ( asubsref(a,i) > 0) && ( asubsref(a,i) <C) ) | ||
14 | E = asubsref(e,i); | ||
15 | else | ||
16 | E = cal_learned_func(i, a, b, N, Y, X, dim) - asubsref(Y,i); | ||
17 | |||
18 | r1 = subsref(Y,i,0) * E; | ||
19 | if( ((r1 < (-1*tolerance)) && ( asubsref(a,i) < C)) || ((r1 >tolerance) && ( asubsref(a,i) > 0)) ) | ||
20 | { | ||
21 | maxDiff = 0; | ||
22 | j = i; | ||
23 | |||
24 | for(k=0; k<N; k++) | ||
25 | { | ||
26 | if( ( asubsref(a,k) > 0) && ( asubsref(a,k) < C) ) | ||
27 | { | ||
28 | temp = fabsf( E - asubsref(e,k)); | ||
29 | if (temp > maxDiff) | ||
30 | j = k; | ||
31 | } | ||
32 | } | ||
33 | |||
34 | if ( i!=j) | ||
35 | { | ||
36 | ret = takeStep(i, j, a, C, e, Y, X, eps, b, N, dim); | ||
37 | if(ret == 1) | ||
38 | return ret; | ||
39 | } | ||
40 | |||
41 | randVal = 1.0; | ||
42 | for( k= (randVal*(N-2)); k<N; k++) | ||
43 | { | ||
44 | if( ( asubsref(a,k) > 0) && ( asubsref(a,k) <C) ) | ||
45 | { | ||
46 | ret = takeStep(i, k, a, C, e, Y, X, eps, b, N, dim); | ||
47 | if (ret == 1) | ||
48 | return ret; | ||
49 | } | ||
50 | } | ||
51 | |||
52 | for(k=0; k<N; k++) | ||
53 | { | ||
54 | ret = takeStep(i, k, a, C, e, Y, X, eps, b, N, dim); | ||
55 | if(ret == 1) | ||
56 | return ret; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | |||
61 | ret = 0; | ||
62 | return ret; | ||
63 | } | ||
64 | |||
65 | |||
66 | |||
67 | |||
diff --git a/SD-VBS/benchmarks/svm/src/c/getAlphaFromTrainSet.c b/SD-VBS/benchmarks/svm/src/c/getAlphaFromTrainSet.c new file mode 100644 index 0000000..681f2c6 --- /dev/null +++ b/SD-VBS/benchmarks/svm/src/c/getAlphaFromTrainSet.c | |||
@@ -0,0 +1,98 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include "svm.h" | ||
6 | |||
7 | alphaRet* getAlphaFromTrainSet(int N, F2D* trn1, F2D* trn2, int iterations) | ||
8 | { | ||
9 | float tolerance, C, eps, *b; | ||
10 | F2D *a_result, *b_result; | ||
11 | int NumChanged, r, ExamineAll, cnt, d, dim, ret, iter, i; | ||
12 | F2D *X, *Y; | ||
13 | F2D *a, *e; | ||
14 | |||
15 | b = malloc(sizeof(float)); | ||
16 | alphaRet* alpha; | ||
17 | alpha = (alphaRet*)malloc(sizeof(alphaRet)); | ||
18 | tolerance = 0.001; | ||
19 | C = 0.05; | ||
20 | d = -1; | ||
21 | dim = 256; | ||
22 | eps = 0.001; | ||
23 | a_result = fSetArray(iterations, N, 0); | ||
24 | b_result = fSetArray(iterations, 1, 0); | ||
25 | ret = 0; | ||
26 | |||
27 | X = usps_read_partial( trn1, trn2, 0, 1, (N/iterations), iterations); | ||
28 | |||
29 | for(iter=0; iter<iterations; iter++) | ||
30 | { | ||
31 | Y = usps_read_partial( trn1, trn2, iter, 0, N/iterations, iterations); | ||
32 | |||
33 | a = fSetArray(N, 1, 0); | ||
34 | arrayref(b,0) = 0; /** check if ptr **/ | ||
35 | e = fSetArray(N, 1, 0); | ||
36 | ExamineAll = 1; | ||
37 | cnt = 0; | ||
38 | NumChanged = 0; | ||
39 | |||
40 | while(NumChanged>0 || ExamineAll == 1) | ||
41 | { | ||
42 | cnt = cnt + 1; | ||
43 | NumChanged = 0; | ||
44 | if(ExamineAll == 1) | ||
45 | { | ||
46 | for(i=0; i<N; i++) | ||
47 | { | ||
48 | ret = examineExample(i, a, b, C, e, X, Y, tolerance, N, eps, dim); | ||
49 | NumChanged = NumChanged + ret; | ||
50 | } | ||
51 | } | ||
52 | else | ||
53 | { | ||
54 | for(i=0; i<N; i++) | ||
55 | { | ||
56 | if( asubsref(a,i) > 0 && asubsref(a,i) <C ) | ||
57 | { | ||
58 | ret = examineExample(i, a, b, C, e, X, Y, tolerance, N, eps, dim); | ||
59 | NumChanged = NumChanged + ret; | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | if(ExamineAll == 1) | ||
64 | ExamineAll = 0; | ||
65 | else if(NumChanged == 0) | ||
66 | ExamineAll = 1; | ||
67 | } | ||
68 | |||
69 | for(r=0; r<N; r++) | ||
70 | subsref(a_result,iter,r) = asubsref(a,r); /** a_result has size iteration,N .. Check **/ | ||
71 | asubsref(b_result,iter) = arrayref(b,0); | ||
72 | |||
73 | fFreeHandle(Y); | ||
74 | fFreeHandle(e); | ||
75 | fFreeHandle(a); | ||
76 | } | ||
77 | |||
78 | alpha->C = C; | ||
79 | alpha->d = d; | ||
80 | alpha->dim = dim; | ||
81 | alpha->eps = eps; | ||
82 | alpha->a_result = a_result; | ||
83 | alpha->b_result = b_result; | ||
84 | alpha->a = a; | ||
85 | alpha->b = arrayref(b,0); | ||
86 | alpha->X = X; | ||
87 | alpha->tolerance = tolerance; | ||
88 | alpha->ret; | ||
89 | |||
90 | free(b); | ||
91 | |||
92 | return alpha; | ||
93 | |||
94 | } | ||
95 | |||
96 | |||
97 | |||
98 | |||
diff --git a/SD-VBS/benchmarks/svm/src/c/polynomial.c b/SD-VBS/benchmarks/svm/src/c/polynomial.c new file mode 100644 index 0000000..cce9190 --- /dev/null +++ b/SD-VBS/benchmarks/svm/src/c/polynomial.c | |||
@@ -0,0 +1,42 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include "svm.h" | ||
6 | |||
7 | float polynomial(int d, F2D* a, F2D* b, int dim) | ||
8 | { | ||
9 | float ret; | ||
10 | F2D *bt, *bt1; | ||
11 | int i,j,r,c; | ||
12 | |||
13 | r = b->height; | ||
14 | c = b->width; | ||
15 | |||
16 | bt = fMallocHandle(c, r); | ||
17 | |||
18 | for(i=0; i<r; i++) | ||
19 | { | ||
20 | for(j=0; j<c; j++) | ||
21 | { | ||
22 | subsref(bt,j,i) = subsref(b,i,j); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | bt1 = fMtimes(a, bt); | ||
27 | fFreeHandle(bt); | ||
28 | |||
29 | if(bt1->height == 1 && bt1->width ==1) | ||
30 | ret = pow(asubsref(bt1,0),d)/dim; | ||
31 | else | ||
32 | { | ||
33 | fFreeHandle(bt1); | ||
34 | return -1; | ||
35 | } | ||
36 | |||
37 | fFreeHandle(bt1); | ||
38 | return ret; | ||
39 | } | ||
40 | |||
41 | |||
42 | |||
diff --git a/SD-VBS/benchmarks/svm/src/c/script_svm.c b/SD-VBS/benchmarks/svm/src/c/script_svm.c new file mode 100644 index 0000000..62a264d --- /dev/null +++ b/SD-VBS/benchmarks/svm/src/c/script_svm.c | |||
@@ -0,0 +1,171 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include <stdio.h> | ||
6 | #include <stdlib.h> | ||
7 | #include "svm.h" | ||
8 | #include <malloc.h> | ||
9 | #include "extra.h" | ||
10 | #define SVM_MEM 1<<24 | ||
11 | int main(int argc, char* argv[]) | ||
12 | { | ||
13 | SET_UP | ||
14 | mallopt(M_TOP_PAD, SVM_MEM); | ||
15 | mallopt(M_MMAP_MAX, 0); | ||
16 | int iter, N, Ntst, i, j, k, n; | ||
17 | F2D* trn1, *tst1, *trn2, *tst2, *Yoffset; | ||
18 | alphaRet* alpha; | ||
19 | F2D *a_result, *result; | ||
20 | F2D *s; | ||
21 | F2D *b_result; | ||
22 | F2D *Xtst, *Ytst; | ||
23 | char im1[256]; | ||
24 | int dim = 256; | ||
25 | |||
26 | N = 100; | ||
27 | Ntst = 100; | ||
28 | iter = 10; | ||
29 | |||
30 | #ifdef test | ||
31 | N = 4; | ||
32 | Ntst = 4; | ||
33 | iter = 2; | ||
34 | #endif | ||
35 | |||
36 | #ifdef sim_fast | ||
37 | N = 20; | ||
38 | Ntst = 20; | ||
39 | iter = 2; | ||
40 | #endif | ||
41 | |||
42 | #ifdef sim | ||
43 | N = 16; | ||
44 | Ntst = 16; | ||
45 | iter = 8; | ||
46 | #endif | ||
47 | |||
48 | #ifdef sqcif | ||
49 | N = 60; | ||
50 | Ntst = 60; | ||
51 | iter = 6; | ||
52 | #endif | ||
53 | |||
54 | #ifdef qcif | ||
55 | N = 72; | ||
56 | Ntst = 72; | ||
57 | iter = 8; | ||
58 | #endif | ||
59 | |||
60 | #ifdef vga | ||
61 | N = 450; | ||
62 | Ntst = 450; | ||
63 | iter = 15; | ||
64 | #endif | ||
65 | |||
66 | #ifdef wuxga | ||
67 | N = 1000; | ||
68 | Ntst = 1000; | ||
69 | iter = 20; | ||
70 | #endif | ||
71 | |||
72 | |||
73 | printf("trn file 1: "); | ||
74 | scanf("%s", im1); | ||
75 | trn1 = readFile(im1); | ||
76 | |||
77 | printf("trn file 2: "); | ||
78 | scanf("%s", im1); | ||
79 | trn2 = readFile(im1); | ||
80 | |||
81 | printf("tst file 1: "); | ||
82 | scanf("%s", im1); | ||
83 | tst1 = readFile(im1); | ||
84 | |||
85 | printf("tst file 2: "); | ||
86 | scanf("%s", im1); | ||
87 | tst2 = readFile(im1); | ||
88 | |||
89 | printf("start.\n"); | ||
90 | for_each_job{ | ||
91 | alpha = getAlphaFromTrainSet(N, trn1, trn2, iter); | ||
92 | a_result = alpha->a_result; | ||
93 | b_result = alpha->b_result; | ||
94 | Yoffset = fSetArray(iter, N, 0); | ||
95 | |||
96 | Xtst = usps_read_partial(tst1, tst2, -1, 1, Ntst/iter, iter); | ||
97 | Ytst = usps_read_partial(tst1, tst2, -1, 0, Ntst/iter, iter); | ||
98 | |||
99 | for(i=0; i<iter; i++) | ||
100 | { | ||
101 | F2D *temp; | ||
102 | temp = usps_read_partial(trn1, trn2, i, 0, N/iter, iter); | ||
103 | for(j=0; j<N; j++) | ||
104 | subsref(Yoffset,i,j) = asubsref(temp,j); | ||
105 | fFreeHandle(temp); | ||
106 | } | ||
107 | |||
108 | |||
109 | result = fSetArray(Ntst,1,0); | ||
110 | for( n=0; n<Ntst; n++) | ||
111 | { | ||
112 | float maxs=0; | ||
113 | s=fSetArray(iter,1,0); | ||
114 | for( i=0; i<iter; i++) | ||
115 | { | ||
116 | for (j=0; j<N; j++) | ||
117 | { | ||
118 | if (subsref(a_result,i,j) > 0) | ||
119 | { | ||
120 | F2D *Xtemp, *XtstTemp, *X; | ||
121 | X = alpha->X; | ||
122 | Xtemp = fDeepCopyRange(X,j,1,0,X->width); | ||
123 | XtstTemp = fDeepCopyRange(Xtst, n,1,0,Xtst->width); | ||
124 | asubsref(s,i) = asubsref(s,i) + subsref(a_result,i,j) * subsref(Yoffset,i,j) * polynomial(3,Xtemp,XtstTemp, dim); | ||
125 | fFreeHandle(Xtemp); | ||
126 | fFreeHandle(XtstTemp); | ||
127 | } | ||
128 | } | ||
129 | asubsref(s,i) = asubsref(s,i) - asubsref(b_result,i); | ||
130 | if( asubsref(s,i) > maxs) | ||
131 | maxs = asubsref(s,i); | ||
132 | } | ||
133 | |||
134 | fFreeHandle(s); | ||
135 | asubsref(result,n) = maxs; | ||
136 | } | ||
137 | } | ||
138 | printf("end..\n"); | ||
139 | #ifdef CHECK | ||
140 | /** Self checking - use expected.txt from data directory **/ | ||
141 | { | ||
142 | int ret=0; | ||
143 | float tol = 0.5; | ||
144 | #ifdef GENERATE_OUTPUT | ||
145 | fWriteMatrix(result, argv[1]); | ||
146 | #endif | ||
147 | ret = fSelfCheck(result, "expected_C.txt", tol); | ||
148 | if (ret == -1) | ||
149 | printf("Error in SVM\n"); | ||
150 | } | ||
151 | /** Self checking done **/ | ||
152 | #endif | ||
153 | |||
154 | fFreeHandle(trn1); | ||
155 | fFreeHandle(tst1); | ||
156 | fFreeHandle(trn2); | ||
157 | fFreeHandle(tst2); | ||
158 | fFreeHandle(Yoffset); | ||
159 | fFreeHandle(result); | ||
160 | fFreeHandle(alpha->a_result); | ||
161 | fFreeHandle(alpha->b_result); | ||
162 | fFreeHandle(alpha->X); | ||
163 | free(alpha); | ||
164 | fFreeHandle(Xtst); | ||
165 | fFreeHandle(Ytst); | ||
166 | WRITE_TO_FILE | ||
167 | return 0; | ||
168 | } | ||
169 | |||
170 | |||
171 | |||
diff --git a/SD-VBS/benchmarks/svm/src/c/svm.h b/SD-VBS/benchmarks/svm/src/c/svm.h new file mode 100644 index 0000000..9675371 --- /dev/null +++ b/SD-VBS/benchmarks/svm/src/c/svm.h | |||
@@ -0,0 +1,38 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #ifndef _SCRIPT_SVM_ | ||
6 | #define _SCRIPT_SVM_ | ||
7 | |||
8 | #include "sdvbs_common.h" | ||
9 | |||
10 | typedef struct | ||
11 | { | ||
12 | F2D* a; | ||
13 | float b; | ||
14 | float C; | ||
15 | int d; | ||
16 | int dim; | ||
17 | F2D* e; | ||
18 | float eps; | ||
19 | F2D* a_result; | ||
20 | F2D* b_result; | ||
21 | F2D* X; | ||
22 | F2D *Y; | ||
23 | float tolerance; | ||
24 | int ret; | ||
25 | |||
26 | }alphaRet; | ||
27 | |||
28 | alphaRet* getAlphaFromTrainSet(int N, F2D* trn1, F2D* trn2, int iterations); | ||
29 | float polynomial(int d, F2D* a, F2D* b, int dim); | ||
30 | float cal_learned_func(int k, F2D* a, float* b, int N, F2D* Y, F2D* X, int dim); | ||
31 | int examineExample(int i, F2D* a, float* b, float C, F2D* e, F2D* X, F2D* Y, float tolerance, int N, float eps, int dim); | ||
32 | int takeStep(int i, int j, F2D* a, float C, F2D* e, F2D* Y, F2D* X, float eps, float* b, int N, int dim); | ||
33 | F2D* usps_read_partial(F2D* dcell1, F2D* dcell2, int idx, int opt, int dim, int iterations); | ||
34 | int script_svm(); | ||
35 | |||
36 | #endif | ||
37 | |||
38 | |||
diff --git a/SD-VBS/benchmarks/svm/src/c/takeStep.c b/SD-VBS/benchmarks/svm/src/c/takeStep.c new file mode 100644 index 0000000..ae550c2 --- /dev/null +++ b/SD-VBS/benchmarks/svm/src/c/takeStep.c | |||
@@ -0,0 +1,188 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include "svm.h" | ||
6 | |||
7 | int takeStep(int i, int j, F2D* a, float C, F2D* e, F2D* Y, F2D* X, float eps, float* b, int N, int dim) | ||
8 | { | ||
9 | int ret=1; | ||
10 | float s; | ||
11 | int m, n, k; | ||
12 | float Ei, Ej, gamma, L, H; | ||
13 | F2D *a_old; | ||
14 | float k11, k12, k22, eta; | ||
15 | F2D *temp, *temp1, *temp2; | ||
16 | float t, t1, t2; | ||
17 | float bnew, delta_b; | ||
18 | float c1, c2, Lobj, Hobj; | ||
19 | |||
20 | if(i==j) | ||
21 | return 0; | ||
22 | |||
23 | a_old = fDeepCopy(a); | ||
24 | |||
25 | if( asubsref(a_old,i)>0 && asubsref(a_old,i)<C ) | ||
26 | Ei = asubsref(e,i); | ||
27 | else | ||
28 | Ei = cal_learned_func(i, a, b, N, Y, X, dim) - asubsref(Y,i); | ||
29 | |||
30 | if( asubsref(a_old,j)>0 && asubsref(a_old,j)<C ) | ||
31 | Ej = asubsref(e,j); | ||
32 | else | ||
33 | Ej = cal_learned_func(j, a, b, N, Y, X, dim) - asubsref(Y,j); | ||
34 | |||
35 | s = asubsref(Y,i) * asubsref(Y,j); | ||
36 | |||
37 | if( asubsref(Y,i) == asubsref(Y,j) ) | ||
38 | { | ||
39 | gamma = asubsref(a_old,i) + asubsref(a_old,j); | ||
40 | if(gamma > C) | ||
41 | { | ||
42 | L = gamma - C; | ||
43 | H = C; | ||
44 | } | ||
45 | else | ||
46 | { | ||
47 | L = 0; | ||
48 | H = gamma; | ||
49 | } | ||
50 | } | ||
51 | else | ||
52 | { | ||
53 | gamma = asubsref(a_old,i) - asubsref(a_old,j); | ||
54 | if(gamma > 0) | ||
55 | { | ||
56 | L = 0; | ||
57 | H = C - gamma; | ||
58 | } | ||
59 | else | ||
60 | { | ||
61 | L = -gamma; | ||
62 | H = C; | ||
63 | } | ||
64 | } | ||
65 | |||
66 | |||
67 | if(L==H) | ||
68 | { | ||
69 | fFreeHandle(a_old); | ||
70 | return 0; | ||
71 | } | ||
72 | |||
73 | temp = fMallocHandle(1, X->width); | ||
74 | temp1 = fMallocHandle(1, X->width); | ||
75 | |||
76 | for(m=0; m<X->width; m++) | ||
77 | { | ||
78 | asubsref(temp,m) = subsref(X,i,m); | ||
79 | asubsref(temp1,m) = subsref(X,j,m); | ||
80 | } | ||
81 | |||
82 | k11 = polynomial(3, temp, temp, dim); | ||
83 | k12 = polynomial(3, temp, temp1, dim); | ||
84 | k22 = polynomial(3, temp1, temp1, dim); | ||
85 | eta = 2 * k12 - k11 - k22; | ||
86 | |||
87 | fFreeHandle(temp1); | ||
88 | fFreeHandle(temp); | ||
89 | |||
90 | if(eta<0) | ||
91 | { | ||
92 | asubsref(a,j) = asubsref(a_old,j) + asubsref(Y,j) * (Ej-Ei)/eta; | ||
93 | if( asubsref(a,j) < L) | ||
94 | asubsref(a,j) = L; | ||
95 | else if( asubsref(a,j) > H ) | ||
96 | asubsref(a,j) = H; | ||
97 | } | ||
98 | else | ||
99 | { | ||
100 | c1 = eta/2; | ||
101 | c2 = asubsref(Y,j) * (Ei-Ej) - eta * asubsref(a_old,j); | ||
102 | Lobj = c1 * L * L + c2 * L; | ||
103 | Hobj = c1 * H * H + c2 * H; | ||
104 | |||
105 | if (Lobj > (Hobj+eps)) | ||
106 | asubsref(a,j) = L; | ||
107 | else if (Lobj < (Hobj-eps)) | ||
108 | asubsref(a,j) = H; | ||
109 | else | ||
110 | asubsref(a,j) = asubsref(a_old,j); | ||
111 | } | ||
112 | |||
113 | if( fabsf( asubsref(a,j)- asubsref(a_old,j) ) < (eps* (asubsref(a,j) + asubsref(a_old,j) +eps)) ) | ||
114 | { | ||
115 | fFreeHandle(a_old); | ||
116 | return 0; | ||
117 | } | ||
118 | |||
119 | asubsref(a,i) = asubsref(a_old,i) - s * ( asubsref(a,j) - asubsref(a_old,j) ); | ||
120 | |||
121 | if( asubsref(a,i) < 0) | ||
122 | { | ||
123 | asubsref(a,j) = asubsref(a,j) + s * asubsref(a,i); | ||
124 | asubsref(a,i) = 0; | ||
125 | } | ||
126 | else if (asubsref(a,i) > C) | ||
127 | { | ||
128 | t = asubsref(a,i) - C; | ||
129 | asubsref(a,j) = asubsref(a,j) + s * t; | ||
130 | asubsref(a,i) = C; | ||
131 | } | ||
132 | |||
133 | /** Update threshold to react change in Lagrange multipliers **/ | ||
134 | |||
135 | if( asubsref(a,i) > 0 && asubsref(a,i) < C ) | ||
136 | bnew = arrayref(b,0) + Ei + asubsref(Y,i) * (asubsref(a,i) - asubsref(a_old,i)) * k11 + asubsref(Y,j) * (asubsref(a,j) - asubsref(a_old,j)) * k12; | ||
137 | else | ||
138 | { | ||
139 | if( asubsref(a,j) > 0 && asubsref(a,j) < C ) | ||
140 | bnew = arrayref(b,0) + Ej + asubsref(Y,i) * (asubsref(a,i) - asubsref(a_old,i)) * k12 + asubsref(Y,j) * (asubsref(a,j) - asubsref(a_old,j)) * k22; | ||
141 | else | ||
142 | { | ||
143 | float b1, b2; | ||
144 | b1 = arrayref(b,0) + Ei + asubsref(Y,i) * (asubsref(a,i) - asubsref(a_old,i)) * k11 + asubsref(Y,j) * (asubsref(a,j) - asubsref(a_old,j)) * k12; | ||
145 | b2 = arrayref(b,0) + Ej + asubsref(Y,i) * (asubsref(a,i) - asubsref(a_old,i)) * k12 + asubsref(Y,j) * (asubsref(a,j) - asubsref(a_old,j)) * k22; | ||
146 | bnew = (b1 + b2) / 2; | ||
147 | } | ||
148 | } | ||
149 | delta_b = bnew - arrayref(b,0); | ||
150 | arrayref(b,0) = bnew; | ||
151 | |||
152 | /** Update error cache using new Lagrange multipliers 24ai **/ | ||
153 | |||
154 | t1 = asubsref(Y,i) * (asubsref(a,i)-asubsref(a_old,i)); | ||
155 | t2 = asubsref(Y,j) * (asubsref(a,j)-asubsref(a_old,j)); | ||
156 | |||
157 | temp = fMallocHandle(1, X->width); | ||
158 | temp1 = fMallocHandle(1, X->width); | ||
159 | temp2 = fMallocHandle(1, X->width); | ||
160 | |||
161 | for (k=0; k<N; k++) | ||
162 | { | ||
163 | if (0 < asubsref(a_old,i) && asubsref(a_old,i) < C ) | ||
164 | { | ||
165 | |||
166 | for(m=0; m<X->width; m++) | ||
167 | { | ||
168 | asubsref(temp,m) = subsref(X,i,m); | ||
169 | asubsref(temp1,m) = subsref(X,k,m); | ||
170 | asubsref(temp2,m) = subsref(X,j,m); | ||
171 | } | ||
172 | |||
173 | asubsref(e,k) = asubsref(e,k)+t1 * polynomial(3, temp, temp1, dim) + t2 * polynomial(3, temp2, temp1, dim) - delta_b; | ||
174 | asubsref(e,i) = 0; | ||
175 | asubsref(e,j) = 0; | ||
176 | } | ||
177 | } | ||
178 | |||
179 | fFreeHandle(a_old); | ||
180 | fFreeHandle(temp); | ||
181 | fFreeHandle(temp1); | ||
182 | fFreeHandle(temp2); | ||
183 | ret = 1; | ||
184 | return ret; | ||
185 | } | ||
186 | |||
187 | |||
188 | |||
diff --git a/SD-VBS/benchmarks/svm/src/c/usps_read_partial.c b/SD-VBS/benchmarks/svm/src/c/usps_read_partial.c new file mode 100644 index 0000000..9cc9df0 --- /dev/null +++ b/SD-VBS/benchmarks/svm/src/c/usps_read_partial.c | |||
@@ -0,0 +1,85 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include "svm.h" | ||
6 | |||
7 | F2D* usps_read_partial(F2D* dcell1, F2D* dcell2, int idx, int opt, int dim, int iterations) | ||
8 | { | ||
9 | F2D *ret, *X, *Y; | ||
10 | F2D *ADD; | ||
11 | int i, j, k, m, n; | ||
12 | |||
13 | F2D *temp, *temp1; | ||
14 | |||
15 | if(opt == 1) | ||
16 | { | ||
17 | for(i=0; i<iterations; i++) | ||
18 | { | ||
19 | if(i==0) | ||
20 | { | ||
21 | X = fMallocHandle(dim, dcell1->width); | ||
22 | for(m=0; m<dim; m++) | ||
23 | { | ||
24 | for(n=0; n<dcell1->width; n++) | ||
25 | { | ||
26 | subsref(X,m,n) = subsref(dcell1,m,n); | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | else | ||
31 | { | ||
32 | temp = fDeepCopy(X); | ||
33 | fFreeHandle(X); | ||
34 | temp1 = fMallocHandle(dim, dcell2->width); | ||
35 | |||
36 | for(m=0; m<dim; m++) | ||
37 | { | ||
38 | for(n=0; n<dcell2->width; n++) | ||
39 | { | ||
40 | subsref(temp1,m,n) = subsref(dcell2,m,n); | ||
41 | } | ||
42 | } | ||
43 | X = ffVertcat(temp, temp1); | ||
44 | fFreeHandle(temp); | ||
45 | fFreeHandle(temp1); | ||
46 | } | ||
47 | } | ||
48 | ret = fDeepCopy(X); | ||
49 | fFreeHandle(X); | ||
50 | } | ||
51 | else | ||
52 | { | ||
53 | for(i=0; i<iterations; i++) | ||
54 | { | ||
55 | if(idx == -1) | ||
56 | ADD = fSetArray(dim, 1, i+1); | ||
57 | else | ||
58 | { | ||
59 | if( i!= idx) | ||
60 | ADD = fSetArray(dim, 1, -1); | ||
61 | else | ||
62 | ADD = fSetArray(dim, 1, 1); | ||
63 | } | ||
64 | if(i==0) | ||
65 | { | ||
66 | Y = fDeepCopy(ADD); | ||
67 | } | ||
68 | else | ||
69 | { | ||
70 | F2D* t = fDeepCopy(Y); | ||
71 | fFreeHandle(Y); | ||
72 | Y = ffVertcat(t, ADD); | ||
73 | fFreeHandle(t); | ||
74 | } | ||
75 | fFreeHandle(ADD); | ||
76 | } | ||
77 | ret = fDeepCopy(Y); | ||
78 | fFreeHandle(Y); | ||
79 | } | ||
80 | |||
81 | return ret; | ||
82 | } | ||
83 | |||
84 | |||
85 | |||