In a loop Hamsters seems to take a vector/array and split it across the threads available but does not aggregate the results to one final output - the output is per thread ONLY. For instance, if you multiply an array like [1,2,3,4,5] with the scalar 2, the array is split into two threads (assuming a duo-core machine) with data [1,2, 3] and [4,5] respectively, which ends up with two separate arrays... i.e. [2,4,6] and [8,10] AND NOT [2,4,6,8,10] as I expected. See code below.
How can we AGGREGATE MULTIPLE threads output BACK INTO ONE array? BUT more peculiarly, ONLY the first (or one) thread actually returns the expected result, the other thread(s) do not seem to always return correct results - unless ONLY if threads is set to 1, which defeats the value of using multiple cores/parallelism. It doesn't matter even if I use tools (hamsters.tools.loop) the results returned are wrong. Is this a bug?
var matrix = [
[1,2,3,4,5],
[1,2,3,4,5]
];
var i;
for (i = 0; i < matrix.length; i += 1) {
try {
var j, dotprod = 0, matrix_row = matrix[i];
for (j = 0; j < matrix_row.length; j += 1) {
dotprod += (matrix_row[j] * 2);
}
console.log("Vanilla-output:" + dotprod);
var params = {'array': matrix_row};
hamsters.run(params, function () {
var arr = params.array;
var dotprod = [];
arr.forEach(function (item) {
dotprod.push(item * item);
});
console.log("each-thread:" + dotprod);
rtn.data = dotprod;
}, function (results) {
var output = 0;
console.log("Hamsters-aggregation:" + results);
results.forEach(function (item) {
output += item;
});
console.log("Hamsters-summation:" + output);
}, hamsters.maxThreads, true, 'Float32');
} catch (err) {
console.log(err);
}
}