forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestLCBuilder.java
More file actions
180 lines (143 loc) · 5.35 KB
/
TestLCBuilder.java
File metadata and controls
180 lines (143 loc) · 5.35 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
package lambdacloud.test;
import static symjava.math.SymMath.log;
import static symjava.math.SymMath.random;
import static symjava.math.SymMath.sin;
import static symjava.math.SymMath.sqrt;
import static symjava.symbolic.Symbol.x;
import static symjava.symbolic.Symbol.y;
import lambdacloud.core.CloudFunc;
import lambdacloud.core.CloudSD;
import lambdacloud.core.lang.LCBuilder;
import lambdacloud.core.lang.LCIf;
import lambdacloud.core.lang.LCInt;
import lambdacloud.core.lang.LCLoop;
import lambdacloud.core.lang.LCVar;
import symjava.relational.Ge;
import symjava.relational.Le;
import symjava.relational.Lt;
import symjava.symbolic.Expr;
public class TestLCBuilder {
public static void testOverWriteArgs() {
LCBuilder task = new LCBuilder("local");
LCVar x = task.declareDouble("x");
LCVar y = task.declareDouble("y");
task.append(x.assign(sqrt(x*x+y*y))); //x=x+y
task.Return(-1); //return 0
System.out.println(task);
CloudSD input = new CloudSD("input").init(new double[]{3, 4});
CloudSD output = new CloudSD("output").resize(1);
task.build(new LCVar[]{x, y}).apply(output, input);
if( output.fetch() )
System.out.println("output="+output.getData(0)); //-1
if( input.fetch() ) {
System.out.println("input=");
for(double d: input.getData())
System.out.println(d); //5.0
}
}
public static void testReturn() {
LCBuilder task = new LCBuilder("job_aws.conf");
LCVar x = task.declareDouble("x");
LCVar y = task.declareDouble("y");
task.Return(sqrt(x*x+y*y)); //return sqrt(x*x+y*y)
System.out.println(task);
CloudSD input = new CloudSD("input").init(new double[]{3, 4});
CloudSD output = new CloudSD("output").resize(1);
task.build(new LCVar[]{x, y}).apply(output, input);
if( output.fetch() )
System.out.println(output.getData(0)); //5.0
}
public static void testLoopAsignReturn() {
LCBuilder cloudTask = new LCBuilder("job_aws.conf");
LCVar i = cloudTask.declareInt("i"); //int i;
LCVar sum = cloudTask.declareDouble("sum");//double sum;
//for(i=0; i<10; i++) {
cloudTask.For(i.assign(0), Lt.apply(i, 10), i.assign(i+1))
.appendBody(sum.assign(sum+i)); //sum = sum + i
//} //end for
cloudTask.Return(sum); //return sum;
System.out.println(cloudTask);
//input is not needed here
CloudSD input = new CloudSD("input").init(new double[]{1});
CloudSD output = new CloudSD("output").resize(1);
cloudTask.build().apply(output, input);
if( output.fetch() )
System.out.println(output.getData(0)); //45.0
}
public static void test() {
// LC cloudTask = new LC("local");
// CloudSD x = cloudTask.declareDouble("x");
// CloudSD y = cloudTask.declareDouble("y");
// CSD output1 = cloudTask.declareCSD("output1");
// CSD output2 = cloudTask.declareCSD("output2");
//
// cloudTask.append(x.assign(3));
// cloudTask.append(y.assign(4));
// //assign is not supported for CSD?
// cloudTask.append(output1.assign(sqrt(x*x+y*y)));
//
// CloudSD i = cloudTask.declareInt("i");
// CloudSD sum = cloudTask.declareDouble("sum");
//
// //for(i=0; i<10; i++) {
// CloudLoop loop = cloudTask.forLoop(i.assign(0), Lt.apply(i, 10), i.assign(i+1));
// loop.appendBody(sum.assign(sum+i)); //sum = sum + i
// //} end for
//
// //assign is not supported for CSD?
// cloudTask.append(output2.assign(sum)); //output = sum
//
// cloudTask.compile(null).apply();
//
//// output1.fetchToLocal();
//// System.out.println(output1.get(0)); //=5
////
//// output2.fetchToLocal();
//// System.out.println(output2.get(0)); //=55
}
public static void main(String[] args) {
testOverWriteArgs();
testReturn();
testLoopAsignReturn();
//MonteCarloTest1();
//MonteCarloTest2();
}
public static void MonteCarloTest1() {
LCBuilder task = new LCBuilder("local");
LCVar x = task.declareDouble("x");
LCVar y = task.declareDouble("y");
LCVar a = task.declareDouble("a");
LCVar b = task.declareDouble("b");
LCVar c = task.declareDouble("c");
LCVar d = task.declareDouble("d");
Expr eq = (x-0.5)*(x-0.5) + (y-0.5)*(y-0.5);
Expr domain = ( Ge.apply(eq, a*a) & Le.apply(eq, b*b) ) | ( Ge.apply(eq, c*c) & Le.apply(eq, d*d) );
//Expr domain = Ge.apply(eq, a*a) & Le.apply(eq, b*b);
//CloudFunc func = new CloudFunc("MTest1",new LCVar[]{x,y,a,b,c,d}, domain);
task.Return(domain);
task.build(new LCVar[]{x,y,a,b,c,d});
}
public static void MonteCarloTest2() {
LCBuilder task = new LCBuilder("local");
LCVar x = task.declareDouble("x");
LCVar y = task.declareDouble("y");
LCVar a = task.declareDouble("a");
LCVar b = task.declareDouble("b");
LCVar c = task.declareDouble("c");
LCVar d = task.declareDouble("d");
LCVar sum = task.declareDouble("sum");
LCVar counter = task.declareInt("counter");
LCVar ret = task.declareDouble("ret");
task.append(counter.assign(0)); // All the integers are initialized as 0s
Expr eq = (x-0.5)*(x-0.5) + (y-0.5)*(y-0.5);
Expr domain = Ge.apply(1, a) & Ge.apply(1, b);
LCIf ifBranch = task.If(domain);
ifBranch.appendTrue( sum.assign(sum + sin(sqrt(log(x+y+1)))) );// sum = sum + sin(sqrt(log(x+y+1))))
//ifBranch.appendTrue( sum.assign(sum + x) );
ifBranch.appendTrue( counter.assign(counter+1) );// counter = counter + 1
// } //end if
//CloudFunc func = new CloudFunc("MTest1",new LCVar[]{x,y,a,b,c,d}, domain);
task.Return(ret);
task.build(new LCVar[]{x,y,a,b,c,d});
}
}