Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 54940f4

Browse filesBrowse files
committed
Add LambdaClout: CPU(),GPU(); Add sin(matrix)
1 parent 2756c98 commit 54940f4
Copy full SHA for 54940f4

File tree

Expand file treeCollapse file tree

9 files changed

+115
-13
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

9 files changed

+115
-13
lines changed
Open diff view settings
Collapse file
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package lambdacloud.core;
2+
3+
import lambdacloud.core.lang.LCDevice;
4+
import symjava.symbolic.Expr;
5+
6+
public class LambdaCloud {
7+
public static Expr CPU(Expr expr) {
8+
expr.runOn(new LCDevice("/cpu"));
9+
return expr;
10+
}
11+
public static Expr GPU(Expr expr) {
12+
expr.runOn(new LCDevice("/gpu"));
13+
return expr;
14+
}
15+
}
Collapse file

‎src/lambdacloud/core/graph/GraphBuilder.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/graph/GraphBuilder.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ protected static void compile(Node root) {
3232

3333
public static Node helper(Expr expr) {
3434
Expr[] args = expr.args();
35-
if(args == null || args.length == 0)
36-
return null;
3735
Node ret = new Node();
3836
ret.expr = expr.clone();
37+
if(args == null || args.length == 0)
38+
return ret;
3939

4040
Symbols ss = new Symbols("__x");
4141
int idx = 0;
Collapse file

‎src/lambdacloud/core/lang/LCReturn.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/lang/LCReturn.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public InstructionHandle bytecodeGen(String clsName, MethodGen mg,
5050
il.append(InstructionConstants.RETURN);
5151

5252
InstructionHandle startPos = arg.bytecodeGen(clsName, mg, cp, factory, il, argsMap, argsStartPos, funcRefsMap);
53-
if(arg.getType() == TYPE.VECTOR) {
53+
if(arg.getType() == TYPE.MATRIX || arg.getType() == TYPE.VECTOR) {
5454
//Copy results to outAry
5555
LocalVariableGen lg = mg.addLocalVariable("l_ret_len",
5656
Type.INT, null, null);
Collapse file

‎src/lambdacloud/examples/Example4.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/examples/Example4.java
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static symjava.math.SymMath.sqrt;
44
import static symjava.symbolic.Symbol.x;
55
import static symjava.symbolic.Symbol.y;
6+
import static lambdacloud.core.LambdaCloud.*;
67

78
import java.util.HashMap;
89
import java.util.Map;
@@ -20,6 +21,11 @@
2021
public class Example4 {
2122

2223
public static void main(String[] args) {
24+
test1();
25+
test2();
26+
}
27+
28+
public static void test1() {
2329
// TODO Auto-generated method stub
2430
//CloudConfig.setGlobalTarget("job_local.conf");
2531

@@ -47,5 +53,22 @@ public static void main(String[] args) {
4753
System.out.println(rlt);
4854

4955
}
56+
57+
public static void test2() {
58+
59+
Expr sum = CPU(x*x) + CPU(y*y);
60+
Expr expr = GPU(sqrt(sum));
61+
62+
System.out.println(expr);
63+
64+
Session sess = new Session();
65+
Map<String, Double> dict = new HashMap<String, Double>();
66+
dict.put(x.toString(), 3.0);
67+
dict.put(y.toString(), 4.0);
68+
69+
double rlt = sess.run(expr, dict);
70+
System.out.println(rlt);
71+
72+
}
5073

5174
}
Collapse file

‎src/lambdacloud/test/CompileUtils.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/test/CompileUtils.java
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public static IR getIR(String name, Expr expr, Expr ...args) {
5050
LCArray output = LCArray.getDoubleArray("output");
5151
cg = _compileVec(name, expr, output, args);
5252
ir.type = FUNC_TYPE.BATCH; //BytecodeBatchFunc
53-
ir.outAryLen = expr.getTypeInfo().dim[0];
53+
if(expr.getType() == TYPE.VECTOR)
54+
ir.outAryLen = expr.getTypeInfo().dim[0];
55+
else if(expr.getType() == TYPE.MATRIX)
56+
ir.outAryLen = expr.getTypeInfo().dim[0]*expr.getTypeInfo().dim[1];
5457
ir.numArgs = args.length;
5558

5659
//only for test purpose
Collapse file

‎src/lambdacloud/test/TestMath.java‎

Copy file name to clipboard
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lambdacloud.test;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import lambdacloud.core.CloudConfig;
7+
import lambdacloud.core.CloudSD;
8+
import lambdacloud.core.Session;
9+
import lambdacloud.core.graph.GraphBuilder;
10+
import lambdacloud.core.graph.Node;
11+
import symjava.symbolic.Matrix;
12+
import static symjava.math.SymMath.*;
13+
14+
public class TestMath {
15+
16+
public static void main(String[] args) {
17+
// TODO Auto-generated method stub
18+
Matrix A = new Matrix("A",4,4);
19+
Map<String, double[]> dict = new HashMap<String, double[]>();
20+
/*
21+
1 2 3 4
22+
1 2 1 3
23+
1 2 2 1
24+
2 3 1 4
25+
*/
26+
//matrix stored in cloumnwise
27+
dict.put(A.toString(), new double[]{1,1,1,2,2,2,2,3,3,1,2,1,4,3,1,4});
28+
29+
CloudConfig.setGlobalTarget("job_local.conf");
30+
//Node n = GraphBuilder.build(A);
31+
Node n = GraphBuilder.build(sin(A));
32+
Session sess1 = new Session();
33+
CloudSD rlt = sess1.runVec(n, dict);
34+
System.out.println("------------");
35+
for(double d : rlt.getData())
36+
System.out.println(d);
37+
}
38+
39+
}
Collapse file

‎src/lambdacloud/test/TestMatrix.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/test/TestMatrix.java
-3Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package lambdacloud.test;
22

3-
import static symjava.symbolic.Symbol.x;
4-
import static symjava.symbolic.Symbol.y;
53

64
import java.util.HashMap;
75
import java.util.Map;
@@ -13,7 +11,6 @@
1311
import lambdacloud.core.graph.Node;
1412
import lambdacloud.core.lang.LCDevice;
1513
import symjava.bytecode.BytecodeBatchFunc;
16-
import symjava.bytecode.BytecodeFunc;
1714
import symjava.matrix.SymMatrix;
1815
import symjava.matrix.SymVector;
1916
import symjava.symbolic.Concat;
Collapse file

‎src/symjava/symbolic/Sin.java‎

Copy file name to clipboardExpand all lines: src/symjava/symbolic/Sin.java
+19-6Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
99
import com.sun.org.apache.bcel.internal.generic.InstructionList;
1010
import com.sun.org.apache.bcel.internal.generic.MethodGen;
11+
import com.sun.org.apache.bcel.internal.generic.ObjectType;
1112
import com.sun.org.apache.bcel.internal.generic.Type;
1213

1314
import symjava.symbolic.arity.UnaryOp;
@@ -17,8 +18,7 @@ public class Sin extends UnaryOp {
1718

1819
public Sin(Expr arg) {
1920
super(arg);
20-
label = "sin(" + arg + ")";
21-
sortKey = label;
21+
updateLabel();
2222
}
2323

2424
@Override
@@ -59,10 +59,23 @@ public InstructionHandle bytecodeGen(String clsName, MethodGen mg,
5959
InstructionList il, Map<String, Integer> argsMap, int argsStartPos,
6060
Map<Expr, Integer> funcRefsMap) {
6161
InstructionHandle startPos = arg.bytecodeGen(clsName, mg, cp, factory, il, argsMap, argsStartPos, funcRefsMap);
62-
il.append(factory.createInvoke("java.lang.Math", "sin",
63-
Type.DOUBLE,
64-
new Type[] { Type.DOUBLE },
65-
Constants.INVOKESTATIC));
62+
if(arg.getType() == TYPE.MATRIX || arg.getType() == TYPE.VECTOR) {
63+
il.append(factory.createInvoke("symjava.symbolic.utils.BytecodeOpSupport", "sin",
64+
new ObjectType("Jama.Matrix"),
65+
new Type[] { new ObjectType("Jama.Matrix") },
66+
Constants.INVOKESTATIC));
67+
} else {
68+
il.append(factory.createInvoke("java.lang.Math", "sin",
69+
Type.DOUBLE,
70+
new Type[] { Type.DOUBLE },
71+
Constants.INVOKESTATIC));
72+
}
6673
return startPos;
6774
}
75+
76+
@Override
77+
public void updateLabel() {
78+
label = "sin(" + arg + ")";
79+
sortKey = label;
80+
}
6881
}
Collapse file

‎src/symjava/symbolic/utils/BytecodeOpSupport.java‎

Copy file name to clipboardExpand all lines: src/symjava/symbolic/utils/BytecodeOpSupport.java
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ public static Jama.Matrix concat(Jama.Matrix[] args) {
1515
return new Jama.Matrix(data, len);
1616
}
1717

18+
//-------------Math functions for Jama.Matrix----------------
19+
20+
public static Jama.Matrix sin(Jama.Matrix arg) {
21+
double[][] data = arg.getArray();
22+
for(int i=0; i<data.length; i++) {
23+
for(int j=0; j<data[0].length; j++) {
24+
data[i][j] = Math.sin(data[i][j]);
25+
}
26+
}
27+
return arg;
28+
}
29+
1830
public static void main(String[] args) {
1931
// TODO Auto-generated method stub
2032

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.