This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRESTmultiply.js
255 lines (221 loc) · 7.13 KB
/
RESTmultiply.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
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
var USE_CLUSTER = false;
var USE_WEBCL = true;
var WEBPORT = 3000;
// Proto libraries
require("./string.prototype.js");
// Library from NPM
var i_express = require('express'),
i_gzippo = require('gzippo'),
i_cluster = require('cluster'),
i_os = require('os'),
// Custom libraries
csr_matrix = require("./csrStuff").csr_matrix,
g_webcl = USE_WEBCL ? require("./SpMSpM-Multiply.js") : null,
g_apikey = require("./apikey"),
g_utils = require("./utils"),
// Log shortcut
log = require("./logging.js").log;
var numCPUs = i_os.cpus().length;
var CLUSTER_CHILDS = 1 * numCPUs;
var loadLateWebCL = function() {
if (USE_WEBCL === true) {
g_webcl = require("./SpMSpM-Multiply.js");
}
};
process.argv.forEach(function(val, index, array) {
if ( val === "--no-webcl" ) {
USE_WEBCL = false;
loadLateWebCL();
} else if ( val === "--cluster" ) {
USE_CLUSTER = true;
} else if ( val.startsWith("--port=") ) {
var newPort = val.slice("--port=".length);
if ( g_utils.isUnsignedInteger(newPort) && ( newPort < 65535 ) ) {
WEBPORT = newPort;
}
} else if ( val.startsWith("--key=") ) {
var newKey = val.slice("--key=".length);
g_apikey.putKey(String(newKey));
}
});
var startClusterServer = function(isCluster) {
if ( isCluster === true ) {
if ( i_cluster.isMaster ) {
log.info("Starting a cluster of services with " + CLUSTER_CHILDS + " child(s).");
for ( var i = 0; i < CLUSTER_CHILDS; ++i ) {
i_cluster.fork();
}
/*
i_cluster.on('disconnect', function(worker) {
log.error('server child disconnect!');
i_cluster.fork();
});
*/
} else {
childServer(isCluster);
}
} else {
childServer(isCluster);
}
};
var childServer = function(isCluster) {
// App and server placeholder
var CURRENT_SERVER = null;
var app = i_express();
// Startup function
var startServer = function(worker) {
CURRENT_SERVER = app.listen(WEBPORT);
log.info("Starting 'Matrix Multiply REST service'.");
log.info("Listening on port: " + WEBPORT + ".");
log.info("WebCl enabled: " + USE_WEBCL + ".");
};
// Function type check
var isFunction = function(functionToCheck) {
var getType = {};
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
};
//
var callNodeGC = function() {
if ( (typeof gc !== 'undefined') && isFunction(gc) ) {
gc();
}
};
if (isCluster === false) {
// Catch SIGINT
process.on('SIGINT', function() {
log.warn('Caught SIGINT. Trying to close gracefully.');
// Kill server if online
if (CURRENT_SERVER !== null) {
CURRENT_SERVER.close();
}
// Add here WebCl.releaseAll()
// If the GC is exposed call it
callNodeGC();
// Exit process
process.exit();
});
}
// Redifine app.listen to disable NAGLE
app.listen = function(){
var server = require('http').createServer(this);
server.on("connection", function (socket) { socket.setNoDelay(true); });
return server.listen.apply(server, arguments);
};
app.configure(function(){
// Title
app.set('title', 'Multiply service');
// REST errors
app.set('format_error', 'Wrong URI format.');
app.set('format_error_input', 'Wrong parameters format.');
// Request parser
app.use(i_gzippo.compress());
app.use(i_express.bodyParser());
app.use(app.router);
});
var makeOk = function(s) {
return {'STATUS' : '1'};
};
var callReturnFunction = function(res) {
return function(error, JSONdata) {
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
if (error !== null) {
res.send(500, 'Something is broken!\n\nDetails: ' + error + '\n\n');
} else {
log.info("Sending results through net (async).");
if ( JSONdata !== null ) {
res.json( JSONdata );
} else {
res.json( makeOk() );
}
}
};
};
app.get('/', function(req, res) {
var dataOut = 'Service UP.';
res.send(dataOut);
});
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,POST");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.all('/service/:key/*', function(req, res, next) {
if ( g_apikey.isValidKey( req.params.key ) ) {
log.info("Key " + req.params.key + " is valid.");
next();
} else {
res.send(401);
}
});
app.get('/service/:key/networktest', function(req,res){
log.info("GET /service/:key/networktest");
// A network performance test
callReturnFunction(res)( null, {"array": require("./matrixGenerator.js").generateBinaryMatrix(2000,500) } );
});
app.post('/service/:key/multiply', function(req,res){
log.info("POST /service/:key/multiply");
f_multiply_matrices_response(req,res,false);
});
app.post('/service/:key/multiplyCOO', function(req,res){
log.info("POST /service/:key/multiplyCOO");
f_multiply_matrices_response(req,res,true);
});
var f_multiply_matrices_response = function(req,res,fromCOO) {
if ( req.hasOwnProperty("body") ) {
if ( req.body.hasOwnProperty("matrixa") && req.body.hasOwnProperty("matrixb") ) {
var matrixA = req.body.matrixa;
var matrixB = req.body.matrixb;
try {
var resultMat = f_multiply_matrices( JSON.parse(matrixA), JSON.parse(matrixB), fromCOO );
callReturnFunction(res)( null, resultMat );
} catch(err) {
log.info("Wrong POST request: Error while multipling: " + err);
callReturnFunction(res)( err );
}
} else {
log.info("Wrong POST request: no matrixa/matrixb");
callReturnFunction(res)( app.get('format_error_input') );
}
} else {
log.info("Wrong POST request: no body");
callReturnFunction(res)( app.get('format_error') );
}
};
var f_multiply_matrices = function(matrixA, matrixB, fromCOO) {
// Import the matrices
log.silly("Importing first matrix");
var csrA, csrB;
if (matrixA.hasOwnProperty("DATA")) {
csrA = new csr_matrix({"numrows": matrixA.ROWCOUNT, "numcols": matrixA.COLCOUNT,
"rowptr": matrixA.ROW, "colindices": matrixA.COL, "data": matrixA.DATA});
} else {
csrA = new csr_matrix({"numrows": matrixA.ROWCOUNT, "numcols": matrixA.COLCOUNT,
"rowptr": matrixA.ROW, "colindices": matrixA.COL});
}
log.silly("Imported first matrix: " + csrA.getRowCount() + "x" + csrA.getColCount() );
log.silly("Importing second matrix");
if (matrixB.hasOwnProperty("DATA")) {
csrB = new csr_matrix({"numrows": matrixB.ROWCOUNT, "numcols": matrixB.COLCOUNT,
"rowptr": matrixB.ROW, "colindices": matrixB.COL, "data": matrixB.DATA});
} else {
csrB = new csr_matrix({"numrows": matrixB.ROWCOUNT, "numcols": matrixB.COLCOUNT,
"rowptr": matrixB.ROW, "colindices": matrixB.COL});
}
log.silly("Imported second matrix: " + csrB.getRowCount() + "x" + csrB.getColCount() );
log.silly("Calculating multiplication");
var mulStep = null;
if (USE_WEBCL) {
mulStep = g_webcl.multiplyMatrix(csrA, csrB, fromCOO);
callNodeGC();
} else {
mulStep = csrA.multiply( csrB );
}
log.info("Calculated multiplication. Result matrix: " + mulStep.getRowCount() + "x" + mulStep.getColCount() );
return mulStep.toJSON();
};
// Start the server
startServer();
};
// Start cluster
startClusterServer(USE_CLUSTER);