-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
64 lines (50 loc) · 1.46 KB
/
main.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
const napa = require('napajs');
const NUMBER_OF_WORKERS = 4;
const zone = napa.zone.create('zone', { workers: NUMBER_OF_WORKERS} );
let size;
if (process.argv[2]) {
size = parseInt(process.argv[2]);
if (size !== size) {
console.log('size should be an integer');
process.exit(1);
}
} else {
console.log('size should be provided');
process.exit(1);
}
const matrix = [];
const vector = [];
for (let i = 0; i < size; i++) {
matrix.push([]);
for (let j = 0; j < size; j++) {
matrix[i].push(getRandomInt(0,100));
}
}
for (let i = 0; i < size; i++) {
vector.push([getRandomInt(0,100)]);
}
console.log('Matrix:');
console.log(matrix);
console.log('Vector:');
console.log(vector);
function multiply (row, vector) {
let result = 0;
for (let i = 0; i < row.length; i++) {
result += row[i] * vector[i][0];
}
for (const currentTime = new Date().getTime() + 5; new Date().getTime() < currentTime;); //looping to simulate some work
return result;
}
var promises = [];
const start = Date.now();
for(var i = 0; i < size; i++)
promises[i] = zone.execute(multiply, [matrix[i], vector]);
Promise.all(promises).then((results) => {
const end = Date.now();
console.log('Result:');
console.log(results.map(result => [parseInt(result._payload)]));
console.log('Runtime: ' + (end - start) + 'ms');
});
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}