-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
348 lines (197 loc) · 6.8 KB
/
index.html
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
<!DOCTYPE html PUBLIC>
<html>
<head>
<!-- FROM -->
<!-- http://www.nihilogic.dk/labs/javascript_canvas_fractals/ -->
</head>
<body>
<canvas id="drawarea" ></canvas>
<table id="output" style="width:500px;">
</table>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="julia.js" ></script>
<script type="text/javascript" src="underscore.min.js" ></script>
<script type="text/javascript" src="parallel.min.js" ></script>
<script type="text/javascript" src="dat.gui.min.js"></script>
<script src="socket.io.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var Options = function() {
this.ctx = {}; //Context storage
this.type = ""; //Type of render (ie local or distributed)
this.expecting = 0;
this.size = 400;
this.showRender = true;
this.clientNumber = 0;
this.isAvailable = true;
this.distributedCores = 0;
this.distributedID = "";
this.computeDistributed = function(){
window.distributed();
};
this.parallelCores = 4;
this.computeLocal = function(){
window.local();
};
};
/* ----------------------------
GUI using Dat.Gui
----------------------------*/
window.options = new Options();
var gui = new dat.GUI();
var f1 = gui.addFolder('Options');
f1.add(options, 'size',0,2000).step(2).listen();
f1.add(options, 'showRender');
var f2 = gui.addFolder('Parallel');
f2.add(options, 'parallelCores',1,20).step(1);
f2.add(options, 'computeLocal');
var f3 = gui.addFolder('Distributed');
availability = f3.add(options,'isAvailable');
f3.add(options, 'distributedID').listen();
f3.add(options, 'computeDistributed');
f1.open();
f2.open();
f3.open();
/* ----------------------------
Initialise Canvas
----------------------------*/
window.canvasInit = function(size){
var canvas = document.getElementById("drawarea");
var ctx = canvas.getContext("2d");
canvas.width = canvas.height = size;
canvas.style.width = canvas.style.height+ "px";
//Reset the context, to check that image data is correct
ctx.fillStyle = "#000000";
ctx.fillRect(0,0,canvas.width,canvas.height);
options.ctx = ctx;
$("#output").html("");
return ctx;
};
/* ----------------------------
Compute/Render
----------------------------*/
window.render = function(result){
//Get the current image data
current = options.ctx.getImageData(0,0,options.size,options.size);
//Merge into correct object
for(at in result.data){
current.data[at] = result.data[at];
}
if (options.showRender) {
options.ctx.putImageData(current,0,0);
}
console.log(options.type, "Client:", result.client, "Core:",result.core,"Start:", result.start, "End:",result.end, "Time:", result.time);
/* $("#output").append("<tr><td>" + options.type + "</td><td>" + result.client + "</td><td>" + result.core + "</td><td>" + result.start+ "</td><td>" + result.end+ "</td><td>" + result.time+ "</td></tr>"); */
//Need to send results back to the socket.io server
if (options.type == "distributed") {
//Now we need to register (Todo, can we combine with connect above?)
socket.emit('response', result, function (data) {});
}
};
/* ----------------------------
Computes a set area
----------------------------*/
window.compute = function(start, end, callback, client){
console.log("ID",options.distributedID,"client",client);
if (options.type == "local"){
cores = options.parallelCores;
} else {
cores = options.distributedCores;
}
for(var j = 0; j < options.parallelCores; j++){
//Parameters to pass
pass = {
start: start,
end: end,
size: options.size,
clientNumber: client,
client: options.distributedID, //Distributed ID
core: j, //What core number we are,
offsetStart: (client * 4) + j,
gap: cores
};
//Spawn a worker, fetch data on compleation
Parallel.spawn(julia, pass).fetch(function (result) {
//Render what is required
callback(result);
});
}
};
/* --------------------------------------------------
Render by Local means (ParallelCores)
-----------------------------------------------------*/
window.local = function() {
options.type = "local";
//Set the current context
options.ctx = window.canvasInit(options.size);
//Compute from the beginning to the end
window.compute(0, options.size * options.size, window.render,0);
};
var server = 'http://parrallel-juila-sets.192.168.1.3.xip.io:8080';
var socket = io.connect(server);
socket.on('connect', function (data) {
//Now we need to register (Todo, can we combine with connect above?)
socket.emit('register', true, function (data) {
options.distributedID = data;
});
});
socket.on("connections", function(data){
//Update the distributed cores amount to let the user know whats going on
options.distributedCores = (data.total * 4);
});
//If the availability changes
availability.onChange(function(value){
//Now we need to register (Todo, can we combine with connect above?)
socket.emit('register', value, function (data) {
options.distributedID = data;
});
});
/* ----------------------------
Now been asked to compute, lets go
----------------------------*/
socket.on("compute", function(data){
console.log(data);
//Update the context so that we know what we are working in
window.canvasInit(options.size);
//If a distributed ID has been set
if (options.distributedID.length){
options.type = "distributed";
//Update the sizing options
options.size = data.size;
//Compute from the beginning to the end
window.compute(data.start, data.end, window.render, data.client);
}
});
socket.on("results", function(result){
//Get the current image data
/*current = options.ctx.getImageData(0,0,options.size,options.size);
//Merge into correct object
for(at in result.data){
current.data[at] = result.data[at];
}
if (options.showRender) {
options.ctx.putImageData(result,0,0);
} */
options.expecting--;
console.log("results!", options.expecting, result.client + "-" + result.core);
$("#output").append("<tr><td>remote</td><td>" + result.client + "</td><td>" + result.core + "</td><td>" + result.offset+ "</td><td>" + result.gap + "</td><td>" + result.time+ "</td></tr>");
if (!options.expecting){
ended = new Date().getTime();
total = ended - started;
$("#output").append("<tr><td>completed</td><td>" + total + "</td><td></td><td></td><td></td><td></td></tr>");
}
});
window.distributed = function(){
started = new Date().getTime();
options.expecting = prompt("Expected Cores",window.options.distributedCores);
//Firstly, lets send out options to the socket.io server
imgOpts = {
size:options.size,
};
//Emit to the server
socket.emit('request', imgOpts);
};
});
</script>
</body>
</html>