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 b1deda3

Browse filesBrowse files
committed
Test CloudFunc, CloudVar
1 parent 7b12087 commit b1deda3
Copy full SHA for b1deda3
Expand file treeCollapse file tree

22 files changed

+680
-116
lines changed
Open diff view settings
Collapse file

‎.classpath‎

Copy file name to clipboardExpand all lines: .classpath
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="lib" path="lib/Jama-1.0.3.jar"/>
5-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
65
<classpathentry kind="lib" path="lib/mtj-1.0-snapshot.jar" sourcepath="/home/yliu/workspace_java/matrix-toolkits-java/src/main"/>
76
<classpathentry kind="lib" path="lib/colt-1.2.0.jar"/>
87
<classpathentry kind="lib" path="lib/libsvm.jar"/>
98
<classpathentry kind="lib" path="lib/jzlib.jar"/>
10-
<classpathentry kind="lib" path="lib/netty-all-5.0.0.Alpha2.jar"/>
9+
<classpathentry kind="lib" path="lib/netty-all-5.0.0.Alpha2.jar" sourcepath="/home/yliu/Downloads/netty-5.0.0.Alpha2/jar/all-in-one/netty-all-5.0.0.Alpha2-sources.jar"/>
10+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-7-oracle"/>
1111
<classpathentry kind="output" path="bin"/>
1212
</classpath>
Collapse file

‎src/lambdacloud/core/CloudConfig.java‎

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

3+
import lambdacloud.net.LambdaClient;
4+
35
public class CloudConfig {
46
private static String target;
7+
private static LambdaClient client;
58

69
/**
710
* Parameter target can be
@@ -12,6 +15,12 @@ public class CloudConfig {
1215
*/
1316
public static void setTarget(String target) {
1417
CloudConfig.target = target;
18+
client = new LambdaClient();
19+
try {
20+
client.connect();
21+
} catch (Exception e) {
22+
e.printStackTrace();
23+
}
1524
}
1625

1726
public static boolean isLocal() {
@@ -34,4 +43,12 @@ public static int getPort() {
3443
return 8322;
3544

3645
}
46+
47+
public static LambdaClient getClient() {
48+
return client;
49+
}
50+
51+
public static void shutDown() {
52+
client.shutDown();
53+
}
3754
}
Collapse file

‎src/lambdacloud/core/CloudFunc.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/CloudFunc.java
+56-2Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package lambdacloud.core;
22

3+
import io.netty.channel.Channel;
4+
import lambdacloud.net.CloudFuncHandler;
5+
import lambdacloud.net.CloudResp;
36
import symjava.bytecode.BytecodeBatchFunc;
47
import symjava.bytecode.BytecodeFunc;
58
import symjava.bytecode.BytecodeVecFunc;
9+
import symjava.bytecode.IR;
610
import symjava.symbolic.Expr;
711
import symjava.symbolic.utils.JIT;
812

@@ -11,8 +15,10 @@ public class CloudFunc {
1115
BytecodeFunc func;
1216
BytecodeVecFunc vecFunc;
1317
BytecodeBatchFunc batchFunc;
18+
1419
//1=BytecodeFunc,2=BytecodeVecFunc,3=BytecodeBatchFunc
1520
int funcType = 0;
21+
IR funcIR = null;
1622

1723
public CloudFunc(String name) {
1824
this.name = name;
@@ -38,16 +44,34 @@ public CloudFunc compile(String name, Expr[] args, Expr expr) {
3844
if(CloudConfig.isLocal()) {
3945
funcType = 1;
4046
func = JIT.compile(args, expr);
47+
4148
} else {
4249
//send the exprssion to the server
50+
funcIR = JIT.getIR(args, expr);
51+
CloudFuncHandler handler = CloudConfig.getClient().getCloudFuncHandler();
52+
// handler.send(this);
53+
54+
Channel ch = CloudConfig.getClient().getChnnel();
55+
try {
56+
ch.writeAndFlush(this).sync();
57+
// Wait until the connection is closed.
58+
//ch.closeFuture().sync();
59+
60+
CloudResp resp = handler.getCloudResp();
61+
System.out.println(resp);
62+
} catch (InterruptedException e) {
63+
// TODO Auto-generated catch block
64+
e.printStackTrace();
65+
}
66+
4367
}
4468
return this;
4569
}
4670

47-
public CloudFunc compile(String name, Expr[] args, Expr[] expr) {
71+
public CloudFunc compile(String name, Expr[] args, Expr[] exprs) {
4872
if(CloudConfig.isLocal()) {
4973
funcType = 2;
50-
vecFunc = JIT.compile(args, expr);
74+
vecFunc = JIT.compile(args, exprs);
5175
} else {
5276
//send the exprssion to the server
5377
}
@@ -90,4 +114,34 @@ public void apply(CloudVar output, CloudVar ...inputs) {
90114

91115
}
92116
}
117+
public String getName() {
118+
return this.name;
119+
}
120+
121+
public IR getFuncIR() {
122+
return this.funcIR;
123+
}
124+
125+
public CloudFunc setBytecodeFunc(BytecodeFunc f) {
126+
this.func = f;
127+
return this;
128+
}
129+
public CloudFunc setBytecodeVecFunc(BytecodeVecFunc f) {
130+
this.vecFunc = f;
131+
return this;
132+
}
133+
public CloudFunc setBytecodeBatchFunc(BytecodeBatchFunc f) {
134+
this.batchFunc = f;
135+
return this;
136+
}
137+
138+
public BytecodeFunc getBytecodeFunc() {
139+
return this.func;
140+
}
141+
public BytecodeVecFunc getBytecodeVecFunc() {
142+
return this.vecFunc;
143+
}
144+
public BytecodeBatchFunc getBytecodeBatchFunc() {
145+
return this.batchFunc;
146+
}
93147
}
Collapse file

‎src/lambdacloud/core/CloudVar.java‎

Copy file name to clipboardExpand all lines: src/lambdacloud/core/CloudVar.java
+20-1Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package lambdacloud.core;
22

3+
import lambdacloud.net.CloudResp;
4+
import lambdacloud.net.CloudVarHandler;
5+
import lambdacloud.net.CloudVarResp;
6+
import lambdacloud.net.LambdaClient;
37
import symjava.bytecode.BytecodeBatchFunc;
48
import symjava.symbolic.Expr;
59
import symjava.symbolic.Symbol;
@@ -8,6 +12,7 @@
812

913
public class CloudVar extends Symbol {
1014
double[] data;
15+
boolean isOnCloud = false;
1116

1217
public CloudVar(String name) {
1318
super(name);
@@ -64,13 +69,27 @@ public CloudVar resize(int size) {
6469
}
6570

6671
public void storeToCloud() {
67-
72+
LambdaClient client = CloudConfig.getClient();
73+
//client.getCloudVarHandler().send(this);
74+
CloudVarHandler handler = client.getCloudVarHandler();
75+
try {
76+
client.getChnnel().writeAndFlush(this).sync();
77+
} catch (InterruptedException e) {
78+
// TODO Auto-generated catch block
79+
e.printStackTrace();
80+
}
81+
CloudVarResp resp = handler.getCloudResp();
82+
System.out.println(resp);
6883
}
6984

7085
public double[] fetchToLocal() {
7186
return data;
7287
}
7388

89+
public boolean isOnCloud() {
90+
return isOnCloud;
91+
}
92+
7493
public int size() {
7594
return data.length;
7695
}
Collapse file
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package lambdacloud.examples;
2+
3+
import symjava.symbolic.Expr;
4+
import lambdacloud.core.CloudConfig;
5+
import lambdacloud.core.CloudFunc;
6+
import lambdacloud.core.CloudVar;
7+
import static symjava.math.SymMath.*;
8+
import static symjava.symbolic.Symbol.*;
9+
10+
public class TestCloudFunc {
11+
public static void main(String[] args) {
12+
CloudConfig.setTarget("server1");
13+
14+
// double[] data = {1,2,3};
15+
// CloudVar var = new CloudVar("var123").init(data);
16+
// var.storeToCloud();
17+
18+
CloudFunc func = new CloudFunc("func123", new Expr[]{x,y}, sqrt(x*x+y*y)-100);
19+
System.out.println(Thread.currentThread().getName());
20+
21+
CloudFunc func2 = new CloudFunc("func456", new Expr[]{x,y}, sqrt(x*x+y*y)-100);
22+
System.out.println(Thread.currentThread().getName());
23+
24+
25+
CloudConfig.shutDown();
26+
}
27+
}
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package lambdacloud.examples;
2+
3+
import lambdacloud.core.CloudConfig;
4+
import lambdacloud.core.CloudVar;
5+
6+
public class TestCloudVar {
7+
8+
public static void main(String[] args) {
9+
CloudConfig.setTarget("local");
10+
11+
double[] data = {1,2,3};
12+
CloudVar var = new CloudVar("var123").init(data);
13+
var.storeToCloud();
14+
}
15+
16+
}
Collapse file
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package lambdacloud.net;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.handler.codec.MessageToByteEncoder;
6+
7+
import java.io.UnsupportedEncodingException;
8+
import java.nio.ByteBuffer;
9+
10+
import symjava.bytecode.IR;
11+
import lambdacloud.core.CloudFunc;
12+
13+
/**
14+
*/
15+
public class CloudFuncEncoder extends MessageToByteEncoder<CloudFunc> {
16+
17+
@Override
18+
protected void encode(ChannelHandlerContext ctx, CloudFunc func, ByteBuf out) {
19+
// Convert to a BigInteger first for easier implementation.
20+
IR funcIR = func.getFuncIR();
21+
22+
int nameLen = 0;
23+
int dataLen = funcIR.bytes.length;
24+
int packageLen = 0;
25+
byte[] nameBytes = null;
26+
byte[] allData = null;
27+
try {
28+
nameBytes = funcIR.name.getBytes("UTF-8");
29+
nameLen = nameBytes.length;
30+
packageLen = nameLen + dataLen;
31+
allData = new byte[packageLen];
32+
} catch (UnsupportedEncodingException e) {
33+
e.printStackTrace();
34+
}
35+
ByteBuffer buf = ByteBuffer.wrap(allData);
36+
buf.put(nameBytes);
37+
buf.put(funcIR.bytes);
38+
39+
// Write a message.
40+
out.writeByte((byte) 'F'); // magic number
41+
out.writeInt(funcIR.type);
42+
out.writeInt(nameLen);
43+
out.writeInt(dataLen);
44+
out.writeBytes(allData);
45+
}
46+
}
Collapse file
+103Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package lambdacloud.net;
2+
3+
import static symjava.symbolic.Symbol.x;
4+
import static symjava.symbolic.Symbol.y;
5+
import io.netty.channel.ChannelFuture;
6+
import io.netty.channel.ChannelFutureListener;
7+
import io.netty.channel.ChannelHandlerContext;
8+
import io.netty.channel.SimpleChannelInboundHandler;
9+
10+
import java.util.concurrent.BlockingQueue;
11+
import java.util.concurrent.LinkedBlockingQueue;
12+
13+
import symjava.symbolic.Expr;
14+
import lambdacloud.core.CloudFunc;
15+
import lambdacloud.core.CloudVar;
16+
17+
/**
18+
* Handler for a client-side channel. This handler maintains stateful
19+
* information which is specific to a certain channel using member variables.
20+
* Therefore, an instance of this handler can cover only one channel. You have
21+
* to create a new handler instance whenever you create a new channel and insert
22+
* this handler to avoid a race condition.
23+
*/
24+
public class CloudFuncHandler extends SimpleChannelInboundHandler<CloudFuncResp> {
25+
26+
final BlockingQueue<CloudFuncResp> answer = new LinkedBlockingQueue<CloudFuncResp>();
27+
public ChannelHandlerContext ctx;
28+
public CloudFuncResp getCloudResp() {
29+
boolean interrupted = false;
30+
try {
31+
for (;;) {
32+
try {
33+
return answer.take();
34+
} catch (InterruptedException ignore) {
35+
interrupted = true;
36+
}
37+
}
38+
} finally {
39+
if (interrupted) {
40+
Thread.currentThread().interrupt();
41+
}
42+
}
43+
}
44+
45+
// @Override
46+
// public void channelActive(ChannelHandlerContext ctx) {
47+
// this.ctx = ctx;
48+
// System.out.println("CloudFuncHandler.channelActive:");
49+
//// CloudFunc func = new CloudFunc("func123", new Expr[]{x,y}, x*y);
50+
//// //this.send(func);
51+
// System.out.println(Thread.currentThread().getName());
52+
// }
53+
54+
@Override
55+
public void messageReceived(ChannelHandlerContext ctx, final CloudFuncResp msg) {
56+
System.out.println("CloudFuncResp messageReceived:");
57+
// System.out.println(msg);
58+
ctx.channel().close().addListener(new ChannelFutureListener() {
59+
@Override
60+
public void operationComplete(ChannelFuture future) {
61+
boolean offered = answer.offer(msg);
62+
assert offered;
63+
}
64+
});
65+
}
66+
67+
@Override
68+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
69+
cause.printStackTrace();
70+
ctx.close();
71+
}
72+
73+
public boolean send(CloudFunc func) {
74+
ChannelFuture future = null;
75+
future = ctx.writeAndFlush(func);
76+
try {
77+
future.sync();
78+
} catch (InterruptedException e) {
79+
// TODO Auto-generated catch block
80+
e.printStackTrace();
81+
}
82+
if (future.isSuccess()) {
83+
return true;
84+
} else {
85+
future.cause().printStackTrace();
86+
future.channel().close();
87+
}
88+
//future.addListener(numberSender);
89+
return false;
90+
}
91+
92+
// private final ChannelFutureListener numberSender = new ChannelFutureListener() {
93+
// @Override
94+
// public void operationComplete(ChannelFuture future) throws Exception {
95+
// if (future.isSuccess()) {
96+
// sendNumbers();
97+
// } else {
98+
// future.cause().printStackTrace();
99+
// future.channel().close();
100+
// }
101+
// }
102+
// };
103+
}
Collapse file
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package lambdacloud.net;
2+
3+
public class CloudFuncResp extends CloudResp {
4+
public CloudFuncResp() {
5+
this.respType = 2;
6+
}
7+
8+
}

0 commit comments

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