forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestThread.java
More file actions
112 lines (100 loc) · 3.62 KB
/
TestThread.java
File metadata and controls
112 lines (100 loc) · 3.62 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
package lambdacloud.test;
import static lambdacloud.core.LambdaCloud.CPU;
import static symjava.math.SymMath.sqrt;
import static symjava.symbolic.Symbol.x;
import static symjava.symbolic.Symbol.y;
import static symjava.symbolic.Symbol.z;
import java.util.HashMap;
import java.util.Map;
import lambdacloud.core.CloudConfig;
import lambdacloud.core.CloudFunc;
import lambdacloud.core.CloudSD;
import lambdacloud.core.Session;
import symjava.symbolic.Expr;
public class TestThread {
public static void main(String[] args) {
test1();
test2();
testMatrixSplit3();
}
public static void test1() {
CloudConfig config = new CloudConfig();
//make sure the expression can be run on different thread
Expr expr = CPU(x*x);
System.out.println(expr);
CloudFunc func = new CloudFunc(config, expr);
CloudSD input = new CloudSD().init(new double[]{3});
CloudSD output = new CloudSD();
func.apply(output, input);
if(output.fetch()) {
System.out.println(output);
}
}
/**
* Test multi-thread
*/
public static void test2() {
CloudConfig config = new CloudConfig();
Expr sum = CPU(x*x) + CPU(y*y) + CPU(z*z) + CPU(x+y+z);
//Expr sum = CPU(x*x) + CPU(y+z);
//Expr sum = CPU(x) + CPU(y) + CPU(z) + CPU(x);
Expr expr = CPU(sqrt(sum));
System.out.println(expr);
Session sess = new Session(config);
Map<String, Double> dict = new HashMap<String, Double>();
dict.put(x.toString(), 2.0);
dict.put(y.toString(), 3.0);
dict.put(z.toString(), 4.0);
double d = 0.0;
d = sess.runSimpleAsync(expr, dict);
System.out.println(d); //6.164414002968976
d = sess.runSimple(expr, dict);
System.out.println(d); //6.164414002968976
d = sess.runLocal(expr, dict);
System.out.println(d); //6.164414002968976
}
public static void testMatrixSplit3() {
CloudConfig config = new CloudConfig();
TestMatrix.testMatrixSplit3(config);
/**
Using 'local' config.
Test: res=[A_0_0*x_0 + A_0_1*x_1, A_1_0*x_0 + A_1_1*x_1] + y0
Generating bytecode for: symjava.bytecode.cfunc12
void apply(double[] output, int outPos, double[] A_0_1, double[] A_0_0, double[] x_0, double[] x_1) = return A_0_0*x_0 + A_0_1*x_1
Generating bytecode for: symjava.bytecode.cfunc13
void apply(double[] output, int outPos, double[] A_1_1, double[] A_1_0, double[] x_0, double[] x_1) = return A_1_0*x_0 + A_1_1*x_1
Generating bytecode for: symjava.bytecode.cfunc14
void apply(double[] output, int outPos, double[] __vec_12, double[] __vec_13, double[] y0) = return [__vec_12, __vec_13] + y0
>>Session eval: cfunc12=A_0_0*x_0 + A_0_1*x_1; args:
[ A_0_1 = [3.0, 1.0, 4.0, 3.0] (Local)
A_0_0 = [1.0, 1.0, 2.0, 2.0] (Local)
x_0 = [0.0, 1.0] (Local)
x_1 = [2.0, 1.0] (Local)
]Fetching: [csd24 = [] (Local)]
>>>Thread-11 evaluating cfunc12...; Return: csd24 = [] (Local)
Fetched: [csd24 = [12.0, 7.0] (Local)]
Return: [12.0 7.0 ]
>>Session eval: cfunc13=A_1_0*x_0 + A_1_1*x_1; args:
[ A_1_1 = [2.0, 1.0, 1.0, 4.0] (Local)
A_1_0 = [1.0, 2.0, 2.0, 3.0] (Local)
x_0 = [0.0, 1.0] (Local)
x_1 = [2.0, 1.0] (Local)
]Fetching: [csd25 = [] (Local)]
>>>Thread-12 evaluating cfunc13...; Return: csd25 = [] (Local)
Fetched: [csd25 = [7.0, 9.0] (Local)]
Return: [7.0 9.0 ]
>>Session eval: cfunc14=[__vec_12, __vec_13] + y0; args:
[ csd24 = [12.0, 7.0] (Local)
csd25 = [7.0, 9.0] (Local)
y0 = [1.0, 2.0, 3.0, 4.0] (Local)
]Fetching: [csd26 = [] (Local)]
Fetched without waiting: [csd24 = [12.0, 7.0] (Local)]
Fetched without waiting: [csd25 = [7.0, 9.0] (Local)]
>>>Thread-13 evaluating cfunc14...; Return: csd26 = [] (Local)
Fetched: [csd26 = [13.0, 9.0, 10.0, 13.0] (Local)]
Return: [13.0 9.0 10.0 13.0 ]
Fetched without waiting: [csd26 = [13.0, 9.0, 10.0, 13.0] (Local)]
Passed!
*/
}
}