forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudSD.java
More file actions
463 lines (412 loc) · 11 KB
/
CloudSD.java
File metadata and controls
463 lines (412 loc) · 11 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
package lambdacloud.core;
import java.util.concurrent.atomic.AtomicInteger;
import io.netty.channel.Channel;
import lambdacloud.core.lang.LCAssign;
import lambdacloud.net.CloudQuery;
import lambdacloud.net.CloudSDHandler;
import lambdacloud.net.CloudSDRespHandler;
import lambdacloud.net.CloudSDResp;
import lambdacloud.net.CloudClient;
import symjava.bytecode.BytecodeVecFunc;
import symjava.symbolic.Expr;
import symjava.symbolic.Symbol;
import symjava.symbolic.utils.JIT;
import symjava.symbolic.utils.Utils;
/**
* Cloud Shared Data (CloudSD)
* <br>
* An instance of CloudSD represents a shared data set on the cloud server.
* The data set can be created on local machine and stored to the cloud side.
* A data set on the cloud server can be download to local machine by
* providing its name.
* <br>
*
* priority:
* 1. csd://ip_address/var_name
* 2. useCloudConfig
* 3. globalCloudConfig
*
*
* <br>
* Example:
* <p><blockquote><pre>
* CloudSD data = new CloudSD("myvar").init(new double[]{1, 2, 3, 4, 5});
* data.push(); // Push data onto cloud
* if(data.fetch()) {
* for(double d : data.getData()) {
* System.out.println(d);
* }
* }
* </pre></blockquote>
*
*/
public class CloudSD extends Symbol {
protected double[] data = new double[0];
protected boolean isOnCloud = false;
protected CloudConfig localConfig = null;
protected boolean isReady = true;//Indicate if the result of a function is under evaluating
protected boolean isAsync = false; //Test purpose: isAsync==true when isReady==false
private static AtomicInteger cdsVarNameGenerator = new AtomicInteger(0);
protected boolean isGenName = false; //A flag to indicate if the name is a generated name
/**
* Construct an instance with a generated name
*/
public CloudSD() {
super(generateName());
this.isGenName = true;
}
/**
* Construct an instance with a given name
*
* @param name
*/
public CloudSD(String name) {
super(name);
}
/**
* Construct a CloudSD object with a generated name based on a given expression.
* This constructor is used to create a new instance of CloudSD from other CloudSD.
* <br>
* An example application is in the iterating algorithms as below:
* for(int i=0; i<maxIter; i++) {
* func.apply(output, input);
* Expr update = input + 1.0*output;
* input = update; //Cast update to type CloudSD
* }
*
* @param expr
*/
public CloudSD(Expr expr) {
super(generateName());
this.isGenName = true;
this.compile(this.label, expr);
}
public CloudSD(String name, Expr expr) {
super(name);
this.compile(name, expr);
}
public CloudSD(CloudConfig config) {
super(generateName());
this.isGenName = true;
this.localConfig = config;
}
public CloudSD(CloudConfig config, String name) {
super(name);
this.localConfig = config;
}
public CloudSD(CloudConfig config, Expr expr) {
super(generateName());
this.isGenName = true;
this.localConfig = config;
this.compile(this.label, expr);
}
public CloudSD(CloudConfig config, String name, Expr expr) {
super(name);
this.localConfig = config;
this.compile(name, expr);
}
/**
* Initialize the cloud variable with the given array.
* The new cloud variable simply wrap the array; that is,
* it is backed by the given array. Any modifications to the
* cloud variable will cause the array to be modified and vice versa.
* @param array
* @return
*/
public CloudSD init(double ...array) {
this.data = array;
return this;
}
private static String generateName() {
return "csd"+cdsVarNameGenerator.incrementAndGet();
}
/**
* 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();
}
/**
* Set the value of the backed array at index
* @param index
* @param value
*/
public void setData(int index, double value) {
data[index] = value;
}
/**
* Get the value of the backed array at index. If the data on cloud need to be returned
* call fetch() first.
*
* @param index
* @return
*/
public double getData(int index) {
return data[index];
}
/**
* Return the backed array (local data). If the data on cloud need to be returned
* call fetch() first.
*
* @return
*/
public double[] getData() {
return data;
}
/**
* Resize the backed array. Old data will be copied to the new backed array
* if the new size is larger than the old size otherwise the data that beyond
* the new size will be discarded.
* @param size
* @return
*/
public CloudSD resize(int size) {
if(this.data == null)
this.data = new double[size];
else {
double[] newdata = new double[size];
if(size > data.length) {
System.arraycopy(this.data, 0, newdata, 0, this.data.length);
} else {
System.arraycopy(this.data, 0, newdata, 0, size);
}
this.data = newdata;
}
return this;
}
/**
* Return the length of the backed array
* @return
*/
public int size() {
return data.length;
}
/**
* Return the length of the backed array
* @return
*/
public int length() {
return data.length;
}
/**
* Return the name of the cloud variable. The name is the identifier
* of the cloud variable on the cloud server. Any local instance of
* CloudSD has the same name will be assumed to be the same variable
* on the cloud side.
* @return
*/
public String getName() {
String[] arr = this.label.split("/");
if(arr.length > 1)
return arr[arr.length-1];
else
return this.label;
}
public String getFullName() {
return this.label;
}
private String[] parseName(String name) {
if(name.startsWith("csd://")) {
String host_ip = this.label.substring(6);
host_ip = host_ip.substring(0, host_ip.indexOf('/'));
String[] arr = host_ip.split(":");
if(arr.length == 2) return arr;
}
return null;
}
/**
* Store the local variable to the cloud.
* priority:
* 1. csd://ip_address/var_name
* 2. useCloudConfig
* 3. globalCloudConfig
* TODO change name to store()
*/
public boolean push() {
String[] host_ip = parseName(this.getFullName());
if(host_ip != null) {
CloudClient c = new CloudClient(host_ip[0], Integer.valueOf(host_ip[1]));
try {
c.connect();
} catch (Exception e) {
e.printStackTrace();
}
return push(c);
}
CloudClient client = getCloudConfig().getCurrentClient();
if(!getCloudConfig().isLocalConfig()) {
return push(client);
} else {
this.isOnCloud = false;
}
return this.isOnCloud;
}
private boolean push(CloudClient client) {
CloudSDRespHandler handler = client.getCloudSDRespHandler();
try {
System.err.println("Pushing data: "+this.toString()+" to "+client.toString());
client.getChannel().writeAndFlush(this).sync();
CloudSDResp resp = handler.getCloudResp();
if(resp.status == 0)
this.isOnCloud = true;
else
this.isOnCloud = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
return this.isOnCloud;
}
/**
* Fetch a cloud variable to local. The name of the variable
* on the cloud must be specified. Return true if success.
* Call getData() to access the data in the cloud variable
* TODO change name to fetch()
* @return
*/
public boolean fetch() {
String[] host_ip = parseName(this.getFullName());
if(host_ip != null && host_ip.length == 2) {
CloudClient c = new CloudClient(host_ip[0], Integer.valueOf(host_ip[1]));
try {
c.connect();
} catch (Exception e) {
e.printStackTrace();
}
System.err.println("Fetching data "+this.getFullName());
return fetch(c);
}
if(getCloudConfig().isLocalConfig()) {
synchronized(this) {
while(!isReady) { //Return immediately if it is ready
//Block until it is ready
try {
System.out.println("Fetching: "+this.toString());
wait();
System.out.println("Fetched: "+this.toString());
return true;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if(isAsync) //only for test purpose
System.out.println("Fetched without waiting: "+this.toString());
return true;
} else {
CloudClient client = getCloudConfig().getCurrentClient();
return fetch(client);
}
}
private boolean fetch(CloudClient client) {
Channel ch = client.getChannel();
CloudQuery qry = new CloudQuery();
qry.objName = this.getFullName();
qry.qryType = CloudQuery.CLOUD_SD;
try {
ch.writeAndFlush(qry).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
CloudSDHandler h = client.getCloudSDHandler();
//while(true) {
CloudSD var = h.getCloudSD();
this.data = var.data;
this.isOnCloud = var.isOnCloud();
if(this.data.length > 0)
return this.isOnCloud;
return false;
//}
}
public boolean isOnCloud() {
return isOnCloud;
}
public void setOnCloudFlag(boolean flag) {
this.isOnCloud = flag;
}
@Override
public Expr simplify() {
return null;
}
@Override
public boolean symEquals(Expr other) {
return false;
}
@Override
public Expr diff(Expr expr) {
return null;
}
public static CloudSD valueOf(Expr expr) {
return new CloudSD(expr);
}
public Expr assign(Expr expr) {
return new LCAssign(this, expr);
}
public Expr assign(double val) {
return new LCAssign(this, Expr.valueOf(val));
}
public Expr assign(int val) {
return new LCAssign(this, Expr.valueOf(val));
}
public String toString() {
if(isOnCloud)
return this.getFullName()+" = "+printData() + " (On Cloud)";
else
return this.getFullName()+" = "+printData();
}
private String printData() {
if(data == null || data.length == 0)
return "[]";
int max = 10;
if(data.length < max) max = data.length;
StringBuilder sb = new StringBuilder();
sb.append("[").append(data[0]);
for(int i=1; i<max; i++) {
sb.append(", ").append(data[i]);
}
if(data.length > max)
sb.append(", ...]; len="+data.length);
else
sb.append("]");
return sb.toString();
}
public void setIsReady(boolean flag) {
isReady = flag;
if(!isReady) isAsync = true;
}
/**
* Return true if the name is a generated name other than
* a user specified name.
* <BR>
* The generated name of the return value of a function will
* always be replaced by the generated name from a cloud server
*
* @return
*/
public boolean isGenName() {
return this.isGenName;
}
private CloudSD compile(String name, Expr expr) {
if(getCloudConfig().isLocalConfig()) {
CloudSD[] args = Utils.extractCloudSDs(expr).toArray(new CloudSD[0]);
BytecodeVecFunc fexpr = JIT.compileVecFunc(args, expr);
data = new double[args[0].size()];
fexpr.apply(data, 0, Utils.getDataFromCloudSDs(args));
} else {
//expr contains server references
//TODO Is it possible to lazy eval the expr at cloud side?
// that is to say this CloudSD is the return value of a CloudFunc
// which constructed from the given expr?
}
return this;
}
}