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 98edd71

Browse filesBrowse files
committed
add CloudFunc, CloudVar, ...
1 parent 30b0b88 commit 98edd71
Copy full SHA for 98edd71

File tree

Expand file treeCollapse file tree

9 files changed

+290
-31
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

9 files changed

+290
-31
lines changed
Open diff view settings
Collapse file
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package lambdacloud.core;
2+
3+
public class CloudConfig {
4+
String config;
5+
6+
/**
7+
* Parameter config can be
8+
* 1. "local": Locally compile and run
9+
* 2. "<lambda_cloud_auth_file>.lca": Compile and run on www.lambdacloud.io
10+
*
11+
* @param target
12+
*/
13+
public CloudConfig(String config) {
14+
this.config = config;
15+
}
16+
17+
public boolean isLocal() {
18+
return config.equalsIgnoreCase("local");
19+
}
20+
21+
/**
22+
* Print the configuration of target environment
23+
* @return
24+
*/
25+
public String printTargetInfo() {
26+
return "16 CPU, 64GB RAM";
27+
}
28+
}
Collapse file

‎src/lambdacloud/core/CloudEnv.java‎

Copy file name to clipboard
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package lambdacloud.core;
2+
3+
public class CloudEnv {
4+
CloudConfig config;
5+
6+
boolean isLocal() {
7+
return true;
8+
}
9+
10+
public void setEnv(String tokenFile) {
11+
12+
}
13+
14+
public String getEnv() {
15+
return null;
16+
}
17+
18+
}
Collapse file
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package lambdacloud.core;
2+
3+
import symjava.bytecode.BytecodeBatchFunc;
4+
import symjava.bytecode.BytecodeFunc;
5+
import symjava.bytecode.BytecodeVecFunc;
6+
import symjava.symbolic.Expr;
7+
import symjava.symbolic.utils.JIT;
8+
9+
public class CloudFunc {
10+
CloudConfig config;
11+
BytecodeFunc func;
12+
BytecodeVecFunc vecFunc;
13+
BytecodeBatchFunc batchFunc;
14+
//1=BytecodeFunc,2=BytecodeVecFunc,3=BytecodeBatchFunc
15+
int funcType = 0;
16+
17+
public CloudFunc(CloudConfig config) {
18+
this.config = config;
19+
}
20+
21+
public CloudFunc compile(Expr[] args, Expr expr) {
22+
if(config.isLocal()) {
23+
funcType = 1;
24+
func = JIT.compile(args, expr);
25+
} else {
26+
//send the exprssion to the server
27+
}
28+
return this;
29+
}
30+
31+
public CloudFunc compile(Expr[] args, Expr[] expr) {
32+
if(config.isLocal()) {
33+
funcType = 2;
34+
vecFunc = JIT.compile(args, expr);
35+
} else {
36+
//send the exprssion to the server
37+
}
38+
return this;
39+
}
40+
41+
public void apply(CloudVar output, CloudVar ...inputs) {
42+
if(inputs.length == 0) {
43+
switch(funcType) {
44+
case 1:
45+
output.set(0, func.apply());
46+
break;
47+
case 2:
48+
break;
49+
case 3:
50+
break;
51+
default:
52+
throw new RuntimeException();
53+
}
54+
} else if(inputs.length == 1) {
55+
double[] data = null;
56+
double d;
57+
switch(funcType) {
58+
case 1:
59+
data = inputs[0].fetchToLocal();
60+
d = func.apply(data);
61+
output.set(0, d);
62+
break;
63+
case 2:
64+
data = inputs[0].fetchToLocal();
65+
double[] out = output.fetchToLocal();
66+
vecFunc.apply(out, 0, data);
67+
break;
68+
case 3:
69+
break;
70+
default:
71+
throw new RuntimeException();
72+
}
73+
} else {
74+
75+
}
76+
}
77+
}
Collapse file

‎src/lambdacloud/core/CloudVar.java‎

Copy file name to clipboard
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package lambdacloud.core;
2+
3+
import symjava.bytecode.BytecodeBatchFunc;
4+
import symjava.symbolic.Expr;
5+
import symjava.symbolic.utils.JIT;
6+
7+
public class CloudVar extends Expr {
8+
CloudConfig config;
9+
double[] data;
10+
11+
public CloudVar(CloudConfig config, String name) {
12+
this.label = name;
13+
this.sortKey = this.label;
14+
this.config = config;
15+
}
16+
17+
public CloudVar(CloudConfig config, Expr expr) {
18+
if(config.isLocal()) {
19+
BytecodeBatchFunc fexpr = JIT.compileBatchFunc(new Expr[0], expr);
20+
fexpr.apply(data, 0);
21+
} else {
22+
//expr contains server references
23+
}
24+
25+
}
26+
27+
public CloudVar init(double ...array) {
28+
this.data = array;
29+
return this;
30+
}
31+
32+
public void set(int index, double value) {
33+
data[index] = value;
34+
}
35+
36+
public CloudVar resize(int size) {
37+
if(this.data == null)
38+
this.data = new double[size];
39+
else {
40+
double[] newdata = new double[size];
41+
if(size > data.length) {
42+
System.arraycopy(this.data, 0, newdata, 0, this.data.length);
43+
} else {
44+
System.arraycopy(this.data, 0, newdata, 0, size);
45+
}
46+
this.data = newdata;
47+
}
48+
return this;
49+
}
50+
51+
public void storeToCloud() {
52+
53+
}
54+
55+
public double[] fetchToLocal() {
56+
return data;
57+
}
58+
59+
60+
@Override
61+
public Expr simplify() {
62+
// TODO Auto-generated method stub
63+
return null;
64+
}
65+
66+
@Override
67+
public boolean symEquals(Expr other) {
68+
// TODO Auto-generated method stub
69+
return false;
70+
}
71+
72+
@Override
73+
public Expr diff(Expr expr) {
74+
// TODO Auto-generated method stub
75+
return null;
76+
}
77+
}
Collapse file

‎src/lambdacloud/examples/Test.java‎

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package lambdacloud.examples;
2+
3+
import static symjava.symbolic.Symbol.*;
4+
5+
import lambdacloud.core.CloudConfig;
6+
import lambdacloud.core.CloudFunc;
7+
import lambdacloud.core.CloudVar;
8+
import symjava.symbolic.Expr;
9+
10+
public class Test {
11+
public static void main(String[] args) {
12+
test();
13+
}
14+
15+
public static void test() {
16+
CloudConfig config = new CloudConfig("local");
17+
18+
Expr expr = x + y;
19+
CloudFunc f = new CloudFunc(config);
20+
f.compile(new Expr[]{x, y}, expr);
21+
22+
CloudVar input = new CloudVar(config,"input").init(new double[]{1, 2});
23+
CloudVar output = new CloudVar(config,"output").resize(1);
24+
25+
long begin = System.currentTimeMillis();
26+
f.apply(output, input);
27+
long end = System.currentTimeMillis();
28+
System.out.println("Time: "+((end-begin)/1000.0));
29+
for(double d : output.fetchToLocal()) {
30+
System.out.println(d);
31+
}
32+
}
33+
}
Collapse file
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lambdacloud.examples;
2+
3+
import static symjava.symbolic.Symbol.x;
4+
import static symjava.symbolic.Symbol.y;
5+
import lambdacloud.core.CloudConfig;
6+
import lambdacloud.core.CloudFunc;
7+
import lambdacloud.core.CloudVar;
8+
import symjava.symbolic.Expr;
9+
10+
public class Test2 {
11+
12+
public static void main(String[] args) {
13+
test();
14+
}
15+
16+
public static void test() {
17+
CloudConfig config = new CloudConfig("local");
18+
19+
Expr[] exprs = new Expr[] {
20+
x + y,
21+
x - y
22+
};
23+
CloudFunc f = new CloudFunc(config);
24+
f.compile(new Expr[]{x, y}, exprs);
25+
26+
CloudVar input = new CloudVar(config,"input").init(new double[]{1, 2});
27+
CloudVar output = new CloudVar(config,"output").resize(2);
28+
29+
long begin = System.currentTimeMillis();
30+
f.apply(output, input);
31+
long end = System.currentTimeMillis();
32+
System.out.println("Time: "+((end-begin)/1000.0));
33+
for(double d : output.fetchToLocal()) {
34+
System.out.println(d);
35+
}
36+
37+
f.apply(output, new CloudVar(config, input + output));
38+
}
39+
}
Collapse file

‎src/symjava/examples/BenchmarkRosenbrock.java‎

Copy file name to clipboardExpand all lines: src/symjava/examples/BenchmarkRosenbrock.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static double test(int N) {
3030
rosen = Sum.apply(100*pow(xi-xim1*xim1,2) + pow(1-xim1,2), i, 2, N);
3131
//System.out.println("Rosenbrock function with N="+N+": "+rosen);
3232

33-
boolean debug = false;
33+
boolean debug = true;
3434
PrintWriter pw = null;
3535
String genFileName = "benchmark-rosenbrock"+N+"-manual.cpp";
3636
try {
@@ -276,7 +276,7 @@ public static void print_c_code(PrintWriter pw, SymMatrix hess) {
276276
public static void main(String[] args) {
277277
System.out.println("============Benchmark for Rosenbrock==============");
278278
System.out.println("N|Symbolic Manipulaton|Compile Gradient|Eval Gradient|Compile Hessian|Eval Hessian|Grad CheckSum|Hess CheckSum|C Code Compile");
279-
for(int N=5; N<850; N+=50)
279+
for(int N=5; N<10; N+=50)
280280
test(N);
281281
//test(5000);//Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
282282
}
Collapse file

‎src/symjava/examples/BenchmarkSqrt.java‎

Copy file name to clipboardExpand all lines: src/symjava/examples/BenchmarkSqrt.java
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import java.util.ArrayList;
77

8+
import lambdacloud.core.CloudVar;
9+
import lambdacloud.core.CloudConfig;
10+
import lambdacloud.core.CloudFunc;
811
import symjava.bytecode.BytecodeBatchFunc;
912
import symjava.bytecode.BytecodeFunc;
1013
import symjava.symbolic.Expr;
@@ -37,13 +40,17 @@ public static void test() {
3740
exprs.add(expr);
3841
}
3942

40-
ArrayList<BytecodeFunc> funcs = new ArrayList<BytecodeFunc>();
43+
final ArrayList<BytecodeFunc> funcs = new ArrayList<BytecodeFunc>();
4144
for(int i=0; i<n; i++) {
4245
Func func = new Func("func"+i, exprs.get(i));
4346
BytecodeFunc bfunc = func.toBytecodeFunc();
4447
System.out.println(bfunc.apply(0.1));
4548
funcs.add(bfunc);
4649
}
50+
51+
CloudConfig config = new CloudConfig();
52+
CloudVar output = new CloudVar(config);
53+
CloudVar input = new CloudVar(config);
4754

4855
int N=10000000;
4956
double xx = 0.1;
@@ -52,7 +59,9 @@ public static void test() {
5259
long begin = System.currentTimeMillis();
5360
for(int j=0; j<N; j++) {
5461
xx += 1e-15;
55-
out += funcs.get(i).apply(xx);
62+
input.set(0, xx);
63+
ff.apply(output, input);
64+
//out += funcs.get(i).apply(xx);
5665
}
5766
long end = System.currentTimeMillis();
5867
System.out.println("Time: "+((end-begin)/1000.0)+" expr="+exprs.get(i));

0 commit comments

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