forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudFunc.java
More file actions
636 lines (558 loc) · 16.1 KB
/
CloudFunc.java
File metadata and controls
636 lines (558 loc) · 16.1 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package lambdacloud.core;
import io.netty.channel.Channel;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import lambdacloud.core.lang.LCBase;
import lambdacloud.core.lang.LCReturn;
import lambdacloud.core.utils.FuncEvalThread;
import lambdacloud.net.CloudClient;
import lambdacloud.net.CloudFuncHandler;
import lambdacloud.net.CloudQuery;
import lambdacloud.net.CloudResp;
import lambdacloud.net.CloudSDHandler;
import lambdacloud.test.CompileUtils;
import symjava.bytecode.BytecodeBatchFunc;
import symjava.bytecode.BytecodeFunc;
import symjava.bytecode.BytecodeVecFunc;
import symjava.bytecode.IR;
import symjava.symbolic.Expr;
import symjava.symbolic.TypeInfo;
import symjava.symbolic.utils.Utils;
/**
* Cloud Function (CloudFunc)
* <br>
* This class represents a function on cloud.
* A function is defined by giving a symbolic expression and arguments (optional).
*
*/
public class CloudFunc extends LCBase {
/**
* Supported types of functions
* @see BytecodeFunc
* @see BytecodeVecFunc
* @see BytecodeBatchFunc
*/
public static enum FUNC_TYPE {
SCALAR, // BytecodeFunc
VECTOR, // BytecodeVecFunc
BATCH // BytecodeBatchFunc
}
protected String name; //TODO use label?
protected BytecodeFunc func;
protected BytecodeVecFunc vecFunc;
protected BytecodeBatchFunc batchFunc;
private static AtomicInteger cfuncNameGenerator = new AtomicInteger(0);
//Info for BytecodeBatchFunc
protected int outAryLen;
protected int numArgs; //
protected boolean isOnCloud = false;
protected Class<?> clazz; //a java class of func
protected Method method;
protected CloudConfig localConfig = null;
protected boolean isAsync = false;
//1=BytecodeFunc, 2=BytecodeVecFunc, 3=BytecodeBatchFunc
protected FUNC_TYPE funcType;
protected IR funcIR = null;
//Need improving
private int device;
/**
* Construct a CloudFunc by providing an expression.
* The arguments of the function is extracted automatically.
*
* @param expr
*/
public CloudFunc(Expr expr) {
this.name = generateName();
this.compile(expr);
}
/**
* Construct a CloudFunc by providing an expression and the arguments.
*
* @param expr
* @param args
*/
public CloudFunc(Expr expr, Expr... args) {
this.name = generateName();
this.compile(expr, args);
}
/**
* Construct a CloudFunc by providing a list of expressions.
* The arguments of the function is extracted automatically.
*
* @param expr
* @param args
*/
public CloudFunc(Expr[] expr) {
this.name = generateName();
this.compile(expr);
}
/**
* Construct a CloudFunc by providing a list of expressions and the arguments.
*
* @param expr
* @param args
*/
public CloudFunc(Expr[] expr, Expr... args) {
this.name = generateName();
this.compile(expr, args);
}
/**
* What purpose by providing only a name? Get an existing function from cloud?
* @param name
*/
public CloudFunc(String name) {
this.name = name;
}
public CloudFunc(String name, Expr expr) {
this.name = name;
this.compile(expr);
}
public CloudFunc(String name, Expr expr, Expr... args) {
this.name = name;
this.compile(expr, args);
}
public CloudFunc(String name, Expr[] exprs) {
this.name = name;
this.compile(exprs);
}
public CloudFunc(String name, Expr[] exprs, Expr... args) {
this.name = name;
this.compile(exprs, args);
}
public CloudFunc(CloudConfig config, Expr expr) {
this.name = generateName();
this.localConfig = config;
this.compile(expr);
}
public CloudFunc(CloudConfig config, Expr expr, Expr... args) {
this.name = generateName();
this.localConfig = config;
this.compile(expr, args);
}
public CloudFunc(CloudConfig config, Expr[] expr) {
this.name = generateName();
this.localConfig = config;
this.compile(expr);
}
public CloudFunc(CloudConfig config, Expr[] expr, Expr... args) {
this.name = generateName();
this.localConfig = config;
this.compile(expr, args);
}
public CloudFunc(CloudConfig config, String name) {
this.name = name;
this.localConfig = config;
}
public CloudFunc(CloudConfig config, String name, Expr expr) {
this.name = name;
this.localConfig = config;
this.compile(expr);
}
public CloudFunc(CloudConfig config, String name, Expr expr, Expr... args) {
this.name = name;
this.localConfig = config;
this.compile(expr, args);
}
public CloudFunc(CloudConfig config, String name, Expr[] expr) {
this.name = name;
this.localConfig = config;
this.compile(expr);
}
public CloudFunc(CloudConfig config, String name, Expr[] expr, Expr... args) {
this.name = name;
this.localConfig = config;
this.compile(expr, args);
}
/**
* Use the given cloud configuration other than the global one
* @param conf
*/
public void setCloudConfig(CloudConfig conf) {
this.localConfig = conf;
}
/**
* Return current cloud configuration. Default is the global configuration.
* @return
*/
public CloudConfig getCloudConfig() {
if(this.localConfig != null)
return this.localConfig;
return CloudConfig.getGlobalConfig();
}
/**
* flag=true: run apply() method asynchronously
* flag=false: run apply() method synchronously
*
* @param flag
*/
public void isAsyncApply(boolean flag) {
this.isAsync = flag;
}
public void apply(CloudSD output, CloudSD ...inputs) {
CloudConfig config = getCloudConfig();
config.setCurrentClient(this.device);
//Make sure the output has the same CloudConfig as CloudFunc
output.setCloudConfig(config);
//for input see the priority of cloud config
//TODO need better implementation for the priority in CloudSD
for(int i=0; i<inputs.length; i++) {
inputs[i].setCloudConfig(config);
}
if(this.clazz != null) {
if(config.isLocalConfig()) {
try {
Object ret = method.invoke(this.clazz.newInstance(), inputs[0].getData());
output.resize(1);
output.setData(0, ((Double)ret).doubleValue());
} catch (Exception e) {
e.printStackTrace();
}
return;
}
}
if(config.isLocalConfig()) {
if(this.clazz != null) {
Double val1;
try {
val1 = (Double)method.invoke(clazz.newInstance(), inputs[0].getData());
output.resize(1);
output.setData(0, val1.doubleValue());
} catch (Exception e) {
e.printStackTrace();
}
return;
}
if(this.device >= 0) {
//Set the flag before starting evaluating thread
output.setIsReady(false);
//Start a thread to do evaluation, the result will be write back to output once
//it is done. Call the method output.fetch() will block until the data is ready
new Thread(new FuncEvalThread(this, output, inputs)).start();
return;
}
if(inputs.length == 0) {
switch(funcType) {
case SCALAR:
output.resize(1);
output.setData(0, func.apply());
break;
case VECTOR:
throw new UnsupportedOperationException();
case BATCH:
throw new UnsupportedOperationException();
default:
throw new RuntimeException();
}
} else if(inputs.length == 1) {
double[] data = null;
double d;
switch(funcType) {
case SCALAR:
//TODO Support multiple CloudSD inputs
data = inputs[0].getData();
d = func.apply(data);
output.resize(1);
output.setData(0, d);
break;
case VECTOR:
throw new UnsupportedOperationException();
case BATCH:
data = inputs[0].getData();
double[] out = output.getData();
batchFunc.apply(out, 0, data);
break;
default:
throw new RuntimeException();
}
} else {
}
} else {
//Store argument on cloud first if necessary
for(int i=0; i<inputs.length; i++) {
if(!inputs[i].isOnCloud()) {
inputs[i].push();
}
}
CloudClient client = config.getCurrentClient();
CloudSDHandler handler = client.getCloudSDHandler();
try {
CloudQuery qry = new CloudQuery();
qry.qryType = CloudQuery.CLOUD_FUNC_EVAL;
//Function name (without package name)
qry.objName = name;
//Arguments list
for(int i=0; i<inputs.length; i++)
qry.argNames.add(inputs[i].getFullName());
//The server will return a generated name for the output with empty name
if(output.isGenName())
qry.outputName = "";
else
qry.outputName = output.getName();
if(this.isAsync)
client.getChannel().writeAndFlush(qry);
else
client.getChannel().writeAndFlush(qry).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!this.isAsync) {
CloudSD rlt = handler.getCloudSD(); //blockqueue
output.setLabel(rlt.getLabel());
output.data = rlt.data;
output.isOnCloud = rlt.isOnCloud;
}
}
}
public boolean isOnCloud() {
return this.isOnCloud;
}
public String getName() {
return this.name;
}
public String getFullName() {
CloudConfig config = this.getCloudConfig();
CloudClient client = config.getClientByIndex(this.device);
if(client != null) {
return "csf://"+client.host+":"+client.port+"/"+this.name;
}
return name;
}
public IR getFuncIR() {
return this.funcIR;
}
public CloudFunc setBytecodeFunc(BytecodeFunc f) {
this.func = f;
return this;
}
public CloudFunc setBytecodeBatchFunc(BytecodeBatchFunc f) {
this.batchFunc = f;
return this;
}
public CloudFunc setBytecodeVecFunc(BytecodeVecFunc f) {
this.vecFunc = f;
return this;
}
public BytecodeFunc getBytecodeFunc() {
return this.func;
}
public BytecodeBatchFunc getBytecodeBatchFunc() {
return this.batchFunc;
}
public BytecodeVecFunc getBytecodeVecFunc() {
return this.vecFunc;
}
@Override
public Expr[] args() {
// TODO Auto-generated method stub
return null;
}
@Override
public TypeInfo getTypeInfo() {
// TODO Auto-generated method stub
return null;
}
public FUNC_TYPE getFuncType() {
return this.funcType;
}
public void setFuncType(FUNC_TYPE type) {
this.funcType = type;
}
/**
* For net work transfer of function type
* @param type
*/
public void setFuncType(int type) {
switch(type) {
case 1:
this.funcType = FUNC_TYPE.SCALAR;
break;
case 2:
this.funcType = FUNC_TYPE.VECTOR;
break;
case 3:
this.funcType = FUNC_TYPE.BATCH;
break;
default:
throw new RuntimeException("");
}
}
public int getOutAryLen() {
return this.outAryLen;
}
public void setOutAryLen(int len) {
this.outAryLen = len;
}
public int getNumArgs() {
return this.numArgs;
}
public void setNumArgs(int num) {
this.numArgs = num;
}
private String generateName() {
return "cfunc"+cfuncNameGenerator.incrementAndGet();
}
/**
* Construct a CloudFunc by providing a class which implements interface BytecodeFunc
* This is an experimental constructor.
*
* @param clazz
*/
public CloudFunc(Class<?> clazz) {
this.name = clazz.getSimpleName();
this.clazz = clazz;
if(getCloudConfig().isLocalConfig()) {
try {
method = clazz.getMethod("apply", new Class[] {double[].class});
} catch (Exception e) {
e.printStackTrace();
}
} else {
String filePath = System.getProperty("user.dir")+"/"+clazz.getName().replace(".", "/")+".class";
File f = new File(filePath);
if(!f.exists()) {
filePath = System.getProperty("user.dir")+"/bin/"+clazz.getName().replace(".", "/")+".class";
f = new File(filePath);
if(!f.exists()) {
filePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath()+
clazz.getName().replace(".", "/")+".class";
}
}
Path path = Paths.get(filePath);
try {
byte[] data = Files.readAllBytes(path);
IR ir = new IR();
ir.type = FUNC_TYPE.SCALAR;
ir.name = clazz.getName();
ir.bytes = data;
this.funcIR = ir;
CloudFuncHandler handler =getCloudConfig().getCurrentClient().getCloudFuncHandler();
Channel ch = getCloudConfig().getCurrentClient().getChannel();
try {
ch.writeAndFlush(this).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
CloudResp resp = handler.getCloudResp();
if(resp.status == 0)
this.isOnCloud = true;
else
this.isOnCloud = false;
} catch (IOException e) {
e.printStackTrace();
}
}
}
private CloudFunc compile(Expr expr) {
List<Expr> args = Utils.extractSymbols(expr);
return compile(expr, args.toArray(new Expr[0]));
}
private CloudFunc compile(Expr expr, Expr[] args) {
Expr compileExpr = expr;
this.device = 0;
if(null != expr.getDevice())
this.device = Integer.parseInt(expr.getDevice().name);
//System.out.println("CloudFunc: compile " + expr + ", send to device: " + expr.getDevice().name);
CloudConfig config = getCloudConfig();
config.setCurrentClient(config.getClientByIndex(this.device));
//TODO Do we need LCReturn? It depends on how the compile function treat the return value of an expr
if(!(expr instanceof LCBase)) {
compileExpr = new LCReturn(expr);
}
if(config.isLocalConfig()) {
//Reset flags to generate matrix, vector declaration in a new func
CompileUtils.bytecodeGenResetAll(expr);
if(expr.getType() == TYPE.MATRIX || expr.getType() == TYPE.VECTOR) {
this.vecFunc = CompileUtils.compileVecFunc(name, compileExpr, args);
this.funcType = FUNC_TYPE.VECTOR; //BytecodeVecFunc
if(expr.getType() == TYPE.VECTOR)
this.outAryLen = expr.getTypeInfo().dim[0];
else if(expr.getType() == TYPE.MATRIX)
this.outAryLen = expr.getTypeInfo().dim[0]*expr.getTypeInfo().dim[1];
this.numArgs = args.length;
} else {
funcType = FUNC_TYPE.SCALAR;
func = CompileUtils.compile(name, compileExpr, args);
//func = JIT.compile(args, expr);
}
} else {
//send the expression to the server
this.funcIR = CompileUtils.getIR(name, compileExpr, args);
this.funcType = funcIR.type;
this.outAryLen = funcIR.outAryLen;
this.numArgs = funcIR.numArgs;
//funcIR = JIT.getIR(name, args, expr);
CloudFuncHandler handler = config.getCurrentClient().getCloudFuncHandler();
Channel ch = config.getCurrentClient().getChannel();
try {
ch.writeAndFlush(this).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
CloudResp resp = handler.getCloudResp();
if(resp.status == 0)
this.isOnCloud = true;
else
this.isOnCloud = false;
}
return this;
}
private CloudFunc compile(Expr[] exprs) {
List<Expr> args = Utils.extractSymbols(exprs);
compile(args.toArray(new Expr[0]), exprs);
return this;
}
private CloudFunc compile(Expr[] exprs, Expr[] args) {
if(getCloudConfig().isLocalConfig()) {
this.funcType = FUNC_TYPE.BATCH;
this.outAryLen = -1; //Determined by input CloudSD
this.numArgs = args.length;
//both works:
//this.batchFunc = JIT.compileBatchFunc(args, exprs);
this.batchFunc = CompileUtils.compileBatchFunc(name, exprs, args);
} else {
//Need reconsider
//method 1: See CompileUtils.compileBatchFunc which follows the implementation of JIT.compileBatchFunc()
//BytecodeBatchFunc func = CompileUtils.compileBatchFunc(name, exprs, args);
//this.funcType = FUNC_TYPE.BATCH;
//this.batchFunc = func;
//method 2: use output for vecfunction (not work, return a BytecodeVecFunc)
// int dim = exprs.length;
// LCStatements lcs = new LCStatements();
// LCArray outAry = new LCDoubleArray("outAry");
// for(int i=0; i<dim; i++) {
// lcs.append( outAry[i].assign(exprs[i]) );
// }
// BytecodeVecFunc func = CompileUtils.compileVecFunc(lcs, outAry, args);
// this.batchFunc = JIT.compileBatchFunc(args, exprs);
//
//send the expression to the server
this.funcIR = CompileUtils.getIR(name, exprs, args);
this.funcType = funcIR.type;
this.outAryLen = -1;
this.numArgs = funcIR.numArgs;
CloudConfig config = this.getCloudConfig();
CloudFuncHandler handler = config.getCurrentClient().getCloudFuncHandler();
Channel ch = config.getCurrentClient().getChannel();
try {
ch.writeAndFlush(this).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
CloudResp resp = handler.getCloudResp();
if(resp.status == 0)
this.isOnCloud = true;
else
this.isOnCloud = false;
}
return this;
}
public String toString() {
return name;
}
}