-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
77 lines (56 loc) · 2.6 KB
/
server.js
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
/*
* @Author: kuychaco
* @Date: 2015-07-20 15:17:09
* @Last Modified by: Katrina Uychaco
* @Last Modified time: 2015-07-24 21:58:20
*/
'use strict';
// Use socket.io to add event listeners to the server
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
// Entry point for server
var path = require('path');
var bodyParser = require('body-parser');
var port = 8000;
http.listen(port, function(){
console.log('listening on *:'+port);
});
app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// Do algorithmic processing in child processes
var childProcess = require('child_process');
io.on('connection', function(socket) {
console.log('connection open');
socket.on('train', function(data) {
var children = [];
var parameters = [{}, {}, {}, {}];
parameters[0].hiddenLayers = data.hiddenLayers1 === '[]' ? [3] : JSON.parse(data.hiddenLayers1);
parameters[1].hiddenLayers = data.hiddenLayers2 === '[]' ? [4] : JSON.parse(data.hiddenLayers2);
parameters[2].hiddenLayers = data.hiddenLayers3 === '[]' ? [5] : JSON.parse(data.hiddenLayers3);
parameters[3].hiddenLayers = data.hiddenLayers4 === '[]' ? [3,4] : JSON.parse(data.hiddenLayers4);
parameters[0].learningRate = data.learningRate1 === '' ? 0.3 : JSON.parse(data.learningRate1);
parameters[1].learningRate = data.learningRate2 === '' ? 0.3 : JSON.parse(data.learningRate2);
parameters[2].learningRate = data.learningRate3 === '' ? 0.3 : JSON.parse(data.learningRate3);
parameters[3].learningRate = data.learningRate4 === '' ? 0.3 : JSON.parse(data.learningRate4);
parameters[0].errorThresh = data.errorThresh1 === '' ? 0.005 : JSON.parse(data.errorThresh1);
parameters[1].errorThresh = data.errorThresh2 === '' ? 0.005 : JSON.parse(data.errorThresh2);
parameters[2].errorThresh = data.errorThresh3 === '' ? 0.005 : JSON.parse(data.errorThresh3);
parameters[3].errorThresh = data.errorThresh4 === '' ? 0.005 : JSON.parse(data.errorThresh4);
// Set up one child process per core
for (var i=0; i<4; i++) {
// Create child process and run code in worker.js
children[i] = childProcess.fork('./worker.js');
console.log('forked child', i, 'with PID', children[i].pid);
children[i].networkNum = i + 1;
// Send data to child process
children[i].send(parameters[i]);
children[i].on('message', function(result) {
result.networkNum = this.networkNum;
socket.emit('brain', result);
});
}
});
});