Coder Social home page Coder Social logo

austinksmith / hamsters.js Goto Github PK

View Code? Open in Web Editor NEW
566.0 28.0 33.0 42.96 MB

100% Vanilla Javascript Multithreading & Parallel Execution Library

Home Page: https://www.hamsters.io

License: Artistic License 2.0

JavaScript 100.00%
multithreading parallel-processing big-data concurrent-programming cross-platform future-proofing high-performance-computing innovation javascript-library multithreaded nodejs-server open-source optimization react-native-app scalability task-processor threadpool web-application

hamsters.js's Introduction

Hamsters.js

Author: Austin K. Smith

Website: Hamsters.io

Description: 100% Vanilla Javascript Multithreading & Parallel Execution Library

License: Artistic License 2.0

npm version downloads

Environment Support

  • All major browsers Internet Explorer 10 and greater
  • ReactNative
  • Node.js
  • Javascript shell environments
  • Inside existing worker threads (Threading inside Threads)

Examples

You can find a view examples of the library in action using the links below.

Square Root

Collatz Conjecture

Installing Hamsters

HTML

ReactNative

Node.js

Using Hamsters

Initializing

The Basics

Restructoring Standard Functions

Promises

Sorting

Persistence

Transferable Objects

Debugging

Limitations

Support Hamsters.js

Hamsters.js is a self funded project, in order to provide updates and host the project website I rely on donations. Please consider donating today so I can continue to support this project, thank you. Hamsters.js donation page

hamsters.js's People

Contributors

austinksmith avatar dependabot[bot] avatar jucrouzet avatar julienkode avatar monkeywithacupcake avatar wagenaartje avatar wolfiex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hamsters.js's Issues

[React Native] Worker is not defined on tvOS

Hello !

It is impossible to initialize my pool of threads

This is my code:

let startOptions = {
  maxThreads: 2,
  cache: false,
  debug: true,
  persistence: false,
};

hamsters.init(startOptions);
let params = {
  array: [1,2,3,4,5,6,7,8,9,10],
  animal: 'Hamster',
};
hamsters.run(params, function() {
  if(params.animal === 'Hamster') {
    rtn.data.push('Hamsters are awesome');
  }
}, function(result) {
  console.log(result);
});

What version of the library are you using?

4.1.3

Which platform are you running the library on?

tvOS

Error Message:

ExceptionsManager.js:63 Worker is not defined
handleException @ ExceptionsManager.js:63
handleError @ InitializeCore.js:125
reportFatalError @ error-guard.js:44
__guard @ MessageQueue.js:220
callFunctionReturnFlushedQueue @ MessageQueue.js:100
(anonymous) @ debuggerWorker.js:71

Uncaught ReferenceError: adTestentListener is not defined

New Issue Report

  • What version of the library are you using? 5.1

  • What operating system are you seeing the issue on? Windows 7

  • Which platform (and or) browser are you seeing the issue on? Chrome

  • What is the version of the platform (and or) browser you are seeing the issue on? Chrome 63/Angular JS 1.50

  • Example logic

I am trying to pass in Uint8Arrays and it hamsters.run and then gives me that error...

It appears I might be missing something? I have referenced the hamsters.js library properly as I set the hamsters.init and get it to run...


 hamsters.init({
                    browser: true,
                    reactNative: false,
                    node: false,
                    maxThreads: 16,
                    debug: 'verbose'
                });

and here is where I run hamsters:

var data = [];
                var myProm;
                for (var i = 0; i < 4; i++) {
                    let num = i + 1;
                    if (i === 3) {
                        user.readBinary(CVData.path + num + '.xlsb').then(function (response) {
                            data.push(new Uint8Array(response));
                        });
                    } else {
                        myProm = user.readBinary(CVData.path + num + '.xlsb').then(function (response) {
                            data.push(new Uint8Array(response));
                        });
                    }

                }
                //wait for the final promise to resolve

                myProm.then(function () {
                    var params = {
                        array: data,
                        aggregate: true
                    };

                    hamsters.run(params, function () {
                        params.array.forEach(function (item) {
                            var workbook = XLSX.read(item, { type: "array" });
                            var myJSON = XLSX.utils.sheet_to_json(workbook.Sheets.Sheet1);
                            rtn.data.push(myJSON);
                        });
                    }, function (results) {
                        console.log(results);
                    });
                });
    

Basically I want to pass in Uint8Arrays I am converting from ArrayBuffers I am getting from binary files I am loading in, use hamsters to do the processing work to turn it into a usable JSON object and then aggregate thees JSON objects into a single object with the results for each of the files. Is this possible and if so, what am I doing wrong?

[FireFox] Getting out of memory error

  • What version of the library are you using?

latest version of library

  • What operating system are you seeing the issue on?

windows 7

  • Which platform (and or) browser are you seeing the issue on?

Core JS (Vanilla)

  • What is the version of the platform (and or) browser you are seeing the issue on?

Am on Firefox browser , using core js with html5 canvas doing some heavy image processing so i mostly use hamster.js to split the image portion for processing between threads , recently i have started getting error out of memory and object could not be cloned , so i was wondering how overcome this memory issue , also any idea to allocate more memory in browser to JS so that out of memory issue not occur as i have 16GB RAM

Error Hamster #0: Line 0 in : uncaught exception: out of memory  hamsters.js:607:5
	onerror /js/vendor/hamsters/hamsters.js:607:5
uncaught exception: out of memory
Error Hamster #0: Line 0 in : DataCloneError: The object could not be cloned.  hamsters.js:607:5
	onerror /js/vendor/hamsters/hamsters.js:607:5
DataCloneError: The object could not be cloned.

Looking for feedback, what are you using Hamsters.js for?

Thank you for your interest in the library, I am opening this issue ticket to try to get a better grasp from the community for what use cases they have for the library and what features they think would be useful to add to the library.

If you are currently using the library please respond to this with the format below and let me know what you think, thank you!

What are you using the library for?

Has the library helped your use case?

How could the library be improved for your use case?

Any suggestions?

Install issues

Thank you for using Hamsters.js! In order to best assist you please follow the guidelines laid out below as they will help significantly in resolving your issues. Tickets that do not follow the provided issue template have less than satisfactory information to resolve the problem reported and will therefore not receive priority support.

Be polite and courteous

Attitude goes a long way, while we do our best to ensure the library is problem free it is a very complicated library that encapsulates many features all while trying to support as many platforms as possible. Inevitably some issues may prop up from time to time, patience, understanding, and collaboration are the best ways to resolve bugs in a timely manner.

New Issue Report

  • 5.1.1 and 5.0.0

This will help narrow down the issue you are facing to a specific release of the library, as your issue may be resolved in previous or latter releases.

  • Windows 7

Feature support can vary wildly and any issues you are facing may be exclusive to a specific operating system or even a specific version of an operating system.

  • Chrome Version 63.0.3239.84 (Official Build) (32-bit)

Some issues may be isolated to specific environments or systems, an issue you are facing in Node.js for example may not be applicable to React Native.

  • I am using angularjs 1.50 as the main framework, no node. I am attempting to follow the HTML directions but am confused because it tells me to download the latest build and then install the source directory on the server. However there are so many variations of files, I feel the instructions are not as clear as they could be for someone who isn't familiar with these processes--typically I install the build versions in most branches. Typically the only thing you need to do is reference a single library in HTML, without the need to multiple other pieces of code included. I have tried several ways of installing this but I still can't get it working even with the SRC library installed on the server.

Feature support can vary wildly and any issues you are facing may be exclusive to a specific browser or framework and specific versions of a browser or framework.

  • I don't believe the logic is the issue, I believe me not having it installed properly is the issue, can you please provide more detailed instructions as to how to install this without node via regular HTML?

Being able to reproduce an issue directly goes a very long way towards debugging issues with the library, having example logic is probably the most important step you can take towards helping us resolve your issues.

I getting error can't find variable: rtn from app.js

Thank you for using Hamsters.js! In order to best assist you please follow the guidelines laid out below as they will help significantly in resolving your issues. Tickets that do not follow the provided issue template have less than satisfactory information to resolve the problem reported and will therefore not receive priority support.

Be polite and courteous

Attitude goes a long way, while we do our best to ensure the library is problem free it is a very complicated library that encapsulates many features all while trying to support as many platforms as possible. Inevitably some issues may prop up from time to time, patience, understanding, and collaboration are the best ways to resolve bugs in a timely manner.

New Issue Report

  • What version of the library are you using?

This will help narrow down the issue you are facing to a specific release of the library, as your issue may be resolved in previous or latter releases.

  • What operating system are you seeing the issue on?

Feature support can vary wildly and any issues you are facing may be exclusive to a specific operating system or even a specific version of an operating system.

  • Which platform (and or) browser are you seeing the issue on?

Some issues may be isolated to specific environments or systems, an issue you are facing in Node.js for example may not be applicable to React Native.

  • What is the version of the platform (and or) browser you are seeing the issue on?

Feature support can vary wildly and any issues you are facing may be exclusive to a specific browser or framework and specific versions of a browser or framework.

  • Example logic

Being able to reproduce an issue directly goes a very long way towards debugging issues with the library, having example logic is probably the most important step you can take towards helping us resolve your issues.

Windows 10 | Node.js - no output when using hamsters.js + webworker-threads package

Windows: 10
Node: v10.14.2
hamsters.js: 5.1.2
webworker-threads: 0.7.17
const Worker = require('webworker-threads').Worker
const hamsters = require('hamsters.js')

hamsters.init({
  Worker: Worker
})

const params = {
  array: [1, 2, 3, 4],
  threads: 1,
  aggregate: true
}

hamsters.promise(params, () => {
  for (let i=0; i<params.array.length; i++) {
    rtn.data.push(params.array[i] * 2)
  }
}).then( (results) => {
  console.log(results)
}).catch( (error) => {
  console.error(error)
})

Console -> Hamsters.js v5.1.2 Info: Hamsters.js v5.1.2 initialized using up to 4 threads. @ 1545047704894
and freezes...
What am i doing wrong?

Getting a failure under node

Hello,

Trying the fourth test function under node.

However, I'm compiling from ES6, so I've switched the functions to arrow functions:

import * as hamsters from 'hamsters.js'

//4 threads and let's aggregate our individual thread results into one final output
const pfunc = () => {
  const params = { 'array': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] }
  hamsters.run(params, () => {
      const arr = params.array
      arr.forEach((item) => {
        rtn.data.push((item * 120) / 10)
      })
    },
    (output) => output,
    4, true)
}
const res = pfunc()
console.log("hamster res:", res)

This fails with the following error:

hamster res: undefined
/Volumes/Second/Chris/myprogram/node_modules/hamsters.js/src/es5/hamsters.js:653
      self.rtn = {
      ^

ReferenceError: self is not defined
    at null._onTimeout (/Volumes/Second/Chris/myprogram/node_modules/hamsters.js/src/es5/hamsters.js:653:7)
    at Timer.listOnTimeout (timers.js:92:15)

real    0m0.082s
user    0m0.064s
sys     0m0.014s

This is using node v4.3.2 (for compatibility reasons).

Any clues? Thanks!

4.1.2 Doesn't spawn threads in browser

For some reason when updating from 4.1.1 to 4.1.2, In 4.1.1. and running the same code with hamsters.js in node.js and in browser (Angular v4) app, both places code worked fine. However, with 4.1.2, seems browser does not spin up threads anymore.

How do i use hamster.js to calculate fibonacci?What's wrong with my code?

function test(){
var params = {'array':[0,1,2,3,4,5,6,7,8,9]};
hamsters.run(params, function() {
for(var i = 2;i<params.array.length;i++)
{
params.array[i]= params.array[i-1]+params.array[i-2];
rtn.data.push(params.array[i]);
}
}, function(output) {
console.log(output)
return output;
}, 4 , true);
}

`Worker is not defined` in Node v6

I'm trying to run the example code as such:

const hamsters = require('hamsters.js');

hamsters.init({
  maxThreads: 10,
  cache: false,
  debug: false,
  persistence: true,
});

const foo = () => {
  var params = {
    'array':[0,1,2,3,4,5,6,7,8,9]
  };
  hamsters.run(params, function() {
      var arr = params.array;
      arr.forEach(function(item) {
        rtn.data.push((item * 120)/10);
      });
  }, function(output) {
     return output;
  });
};
foo();

but got error:

node_modules/hamsters.js/src/es5/hamsters.js:124
          hamsters.wheel.hamsters.push(new Worker(hamsters.wheel.uri));
                                           ^

ReferenceError: Worker is not defined
    at spawnHamsters (node_modules/hamsters.js/src/es5/hamsters.js:124:44)

Hamsters.js Doesn't Work In A Loop With Multiple Threads?

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);
        }
    }

Help processing data

Hello, I need some help with my code.

Problem:
I adapted the code to the library's form and when I run it the problem is the conditional run first and then the threads. The correct way is when threads is finished pass the next code line which what is the conditional.

See the code in Pastebin website
Click here to view the Image

Am I doing wrong something?
Hope help me. (sorry for my english)
Ima newbie with web workers.

error can't find variable: rtn from app.js

handleHamster = () => {
var params = {
array:[0,1,2,3,4,5,6,7,8,9]
};
hamsters.run(params, function() {
var arr = params.array;
arr.forEach(function(item) {
console.warn('calllll', item)
rtn.data.push((item * 120)/10);
});
}, function(output) {
console.warn('output', output)
return output;
}, 2, false);
}
here rtn not find.

I am using version :
"hamsters.js": "^4.2.1",
"react": "16.2.0",
"react-native": "0.52.1",
"react-native-threads": "0.0.10",

Task output is leaked after run ends.

I've got a situation where I've got about 400mb of output data in Float64Arrays. This is retained and not GC's between runs.

I've put together a very simple fix for this but haven't tested with memoisation.

Documentation working example

Do you have some sort working example for this? I really do not understand the origin of your "rtn" variable and how outputs are passed back to the main thread.

how to migrate from v4 to v5

New Issue Report

  • What version of the library are you using?

am using v4.1.2 its working i was thinking to update it to v5 i have downloaded v5 and replaced v4 files with it . but am getting error

Uncaught TypeError: _hamsters2.default.init is not a function
am using webpack 4 environment

i imported it like
import hamsters from './../../../node_modules/hamsters.js/build/hamsters.js';
the i have starting params

			// init hamster
		  var startOptions = {
			maxThreads: navigator.hardwareConcurrency,
			cache: false,
			debug: true,
			persistence: false
		};  
			hamsters.init(startOptions);

so wondering what need to be change for v5 , i go through wiki its installation it shows hamsters.init

  • What operating system are you seeing the issue on?

windows 7

  • Which platform (and or) browser are you seeing the issue on?

vanilla js , webpack 4.1

  • What is the version of the platform (and or) browser you are seeing the issue on?

chrome v70

Hamsters.js v5.1.1 Error: undefined @ 1522342944715

Hi, I am trying to get this library up and running to help with some calculations I am going to be running but I keep getting an undefined error when I try and call hamsters.run.

I have an array of an array of objects, that are nested via d3 prior having it passed into hamsters, I am not sure if that is causing the issue or not, which I would hope not since this is a fairly basic use case, nothing too fancy...

So for instance :
image

and in hamsters, I get a red underline at:

image

Here is the actual code:

 this.getScores = function (data) {
                //we are going to utilize a library called hamsters for multithreading support
                let params = {
                    myParams: data,
                    threads: 4
                };

                hamsters.run(params, function () {
                    for (var i = 0; i < params.myParams; i++) {
                        //now we do the calculations in here
                        console.log(params);
                    };
                });

Any help would be appreciated, I would like to have multithreading support for this project as there are quite a lot of calculations that are going to be needed

Node compatibility

Issue: Library is not supporting as a Node module.

Its working only for html file.
I request to make one standard file/library for nodejs users.

Tests not pasing

Cloned the repo, dev.
npm install
npm run test

OS: Windows 10.

Here is the resport.
https://pastebin.com/8AL6yaHQ

Also it does not build. Havent digged why yet.
It's caused by rm and cp commands, which does not exists in windows. Most of these issues might be fixed by using a more recent webpack version and a couple of plugins to move things around and delete/clean folders.

Cheers

[React Native] Can currently only create a blob from other Blobs

Hi, I have a RN application and just want to try parallel programming using hamsters.js. First, I created a demo app to try it with a worker, namely https://github.com/fabriciovergal/react-native-workers. however I am getting a strange error,
What version of the library --> 4.2.0
What operating system are you seeing the issue on? --> Windows
Which platform (and or) browser are you seeing the issue on? React Native on andorid
What is the version of the platform (and or) browser you are seeing the issue on? --> RN 48.0
The code that I wrote simply (i saw this in how the library manages your data) :
initHamsters(){
const startOptions = {
maxThreads: 4,
cache: false,
debug: false,
persistence: false,
Worker: Worker
};
hamsters.init(startOptions);
const params = {
array: [1,2,3,4,5,6,7,8,9,10],
animal: 'Hamster',
};
hamsters.run(params, function() {
if(params.animal === 'Hamster') {
rtn.data.push('Hamsters are awesome');
}
}, function(result) {
console.log(result);
});
}
I have to say that there is not so many worker libraries availalbler for react native, and the one that I found was working, i tried it. However, it has some requirements like using two ports and having index.worker.js and I didnt provide them to run this code. the error is like this :
img_1313
and as I understand this is a react native error, which doesnt allow to use this library and Worker library.
Any ideas?

navigator.hardwareConcurrency is undefined

On my machine, when running (v4.1.1) in Edge "navigator.hardwareConcurrency" is undefined but "navigator" is defined.

During init if I don't explicitly specify "maxThreads" hamsters crashes.

I.e. We get in here and maxThreads is set to undefined:

 function setupHamstersEnvironment(onSuccess) {
....
    if(typeof navigator !== 'undefined') {
      hamsters.maxThreads = navigator.hardwareConcurrency;
    }

....
}

There should probably be an additional test, something like this:

 function setupHamstersEnvironment(onSuccess) {
....
    if(typeof navigator !== 'undefined' && typeof navigator.hardwareConcurrency !== 'undefined') {
      hamsters.maxThreads = navigator.hardwareConcurrency;
    }

....
}

Promises error

Thank you for the wonderful library.
For several days now I have been trying to try working with promises and getting an error ...
Help me please :)


Windows 10
Node v10.14.0
Hamsters 5.1.1


Code ->

const Worker = require('workerpool').Worker
const hamsters = require('hamsters.js')

hamsters.init({
  Worker: Worker
})

var params = {
  array: [1, 2, 3, 4, 5, 6, 7, 8],
  threads: 1,
  aggregate: true
}

 var method = function() {
   for(var i = 0; i < params.array.length; i++) {
    rtn.data.push(Math.sqrt(params.array[i]))
   }
 }

hamsters.promise(params, method)
.then(function(results) {
  console.log(results)
}).catch(function(error) {
  console.error(error)
})

Or ->

hamsters.promise(params, function() {
  for (let i=0; i<params.array; i++) {
    rtn.data.push(params.array[i] * 2)
  }
})
.then( (results) => {
  console.log(results)
}).catch( (error) => {
  console.error(error)
});

Error ->

ReferenceError: scaffold is not defined    
at F:\X-project-inf\node_modules\hamsters.js\build\hamsters.node.min.js:1:19131    
at new Promise (<anonymous>)    
at e.value [as promise] (F:\X-project-Inf\node_modules\hamsters.js\build\hamsters.node.min.js:1:19029)    at Object.<anonymous> (F:\X-project-Inf\src\test.js:16:10) at Module._compile (internal/modules/cjs/loader.js:688:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32)    
at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3) at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)

rtn data is pushed on different index than 0

so i have replaced v4.2 with v5.1.1 and i noticed few new things one is "data" key in rtn return object earlier it was just 0 index i fixed by adding ['data'] in code to extract the thread data which is pushed it index 0 for single thread , however the another strange thing i noticed when we create multiple threads then rtn push data on index which is same thread number , i wonder it will be good if all threads push its data index 0 is v4.2.2 was doing or any fix for it

  • What version of the library are you using?

v 5.1.1

  • What operating system are you seeing the issue on?

windows 7

  • Which platform (and or) browser are you seeing the issue on?

apache , chrome v70

  • What is the version of the platform (and or) browser you are seeing the issue on?

chrome v70

  • Example logic

image

Any explanation for the little performance gain?

I was trying to see how much performance gain I should expect from multi-thread my task. So I tried the test on the official site. But surprisingly, the result indicated that there was very little performance gain even on the 25.6 million cases.

I am aware of the Amdahls Law and Gustafson Law, but still cannot figure out a reasonable explanation. Anyone have some ideas?

Screenshot for my result FYI:
screen shot 2016-04-02 at 11 31 31 am

Can this be used to modify DOM

I have a page where I have 4 canvases displaying images and I currently use javascript timers to loop through images in a set and draw those images on the canvas. but the problem I have is that my timers are synchronous because Javascript is single threaded. so for my 4 canvases, my first canvas animates and then the second and so on. they're not animating concurrently.

I saw the examples for Hamsters.js and they're all modifying a variable but do they even modify the DOM as well?

hamsters.run does not work with not typed arrays

Hamsters.run always tries to convert a given array to a typed array... even if it is not!. The problem is, that Hamsters.run assigns dataType = "na" if no data type has been given.

However, feedHamster uses transferables when dataType is truthy (what now always is the case).

how to access outside function from hamsters.run

hi am processing canvas large data so to speedup things am trying to use hamsters for the first time

so my question is that my outside functions are not accessible within hamsters.run
example

this.testfun = function(val){
 // do something and return 
};

   hamsters.run(params, function() {
     // my code calling function  testfun  but it giver error undefined.  
    }, function(output) {
      console.log(output);
    }, hamsters.maxThreads, true);

thanks

Help me understand how to use this.

Using [email protected], I was able to run the example code. However I don't think it's making any sense to me. Also since I'm not seeing the result I'm expecting, I don't think this library is working properly.

Suppose running this code:

const foo = () => {
  var params = {
    'array':[0,1,2,3,4,5,6,7,8,9]
  };
  hamsters.run(params, function() {
      var arr = params.array;
      arr.forEach(function(item) {
        rtn.data.push((item * 120)/10);
      });
  }, function(output) {
     console.log(output);
  }, 10);
};
foo();

I was expecting the workload to be distributed among these 10 threads. But after some console logging, I see they are getting the exact same array and they are doing the exact same work, just now 10 times. How does that help? Maybe the example code just doesn't explain this but I was expecting either:

  • the thread function to be able to know its thread index so it can find the proper sub-set in the data array to work on.
  • somehow the data array is partitioned for these threads so they only see what they should do.

Could you help me understand how this library should be used? If you need an example, say we have a big array of size n (n > 10k) of positive integers and we are trying to find (x) => x * (x - 1) * (x - 2) * ... * 2 * 1 for every item in the array. Since the data items have no dependency on each other, ideally we could spawn n threads where each one would grab one number and start calculating. Could you illustrate how this could be done with this library?

Thanks a lot.

Side note: Since params and rtn are special "internal" variables, why not pass them in this way so it's easier to understand?

  hamsters.run(params, function(params, rtn) {
      var arr = params.array;
      arr.forEach(function(item) {
        rtn.data.push((item * 120)/10);
      });
  }, function(output) {
     console.log(output);
  }, 10);

TransformError couldnt find preset es2015

Hi, I have a RN application and just want to try parallel programming using hamsters.js. First, I created a demo app to try the library. however I am getting a strange error,
What version of the library --> 4.2.0
What operating system are you seeing the issue on? --> Windows

Which platform (and or) browser are you seeing the issue on? React Native on andorid

What is the version of the platform (and or) browser you are seeing the issue on? --> RN 48.0

The ERROR : TrasnformError: C//Users.. //node_modules//hamsters.js//build//hamsters.js : Couldn't find the preset 'es2015' relative to the directory C//Users//.. node_modules//hamsters.js
img_1301

Hamsters.js v5.1.1 Error: undefined @ 1529048722217 - Using Angular 5

Hello friend ๐Ÿ˜„

using your awesome library for a test drive.

##planted like this:
var hamsters = require('hamsters.js');

##used it like this:

var v = [{digit: 1, type: 'number'}, {digit: 1, type: 'number'}, {digit: 1, type: 'number'}, {digit: 1, type: 'number'}];
 var params = {
        array: v;
        threads: 4
    };

    hamsters.run(params, function() {
    console.log("I ran")
    });

and chrome console says:

Hamsters.js v5.1.1 Error: undefined @ 1529048722217

##using
Angular 5
Google Chrome Version 66.0.3359.181 (Official Build) (64-bit)
Thanks ๐Ÿ˜„

ERROR: _habitat2.default.SharedWorker is not a constructor [React Native]

New Issue Report

  • What version of the library are you using?

"hamsters.js": "^5.0.0" + "react-native-threads": "^0.0.10"

  • What operating system are you seeing the issue on?

MacOS

  • Which platform (and or) browser are you seeing the issue on?

React-Native
"react": "^16.3.0-alpha.1",
"react-native": "0.54.0",

  • Example logic

Trying to run the code described on your wiki, within a brand new sample app created with
Step 1
react-native init HamstersTest

Step 2
Babel es2015 dependencies

Step 3

import { Thread } from 'react-native-threads';
import hamsters from 'hamsters.js';
...
hamsters.init({
Worker: Thread
});
...
executeHamsters() {
var params = {
array: [1, 2, 3, 4],
threads: 4
};

hamsters.run(params, function() {
  for(var i = 0; i < params.array; i++) {
    rtn.data.push(params.array[i] * 4);
  }
}, function(results) {
  console.debug('results', results);
});

}

error _slicedToArray is not defined

thanks for the amazing lib :) i was wondering when the new version will be released as i have noticed its logger is improved with current latest version i have bit hard time with debugging as recently i have encountered error when i have started using hamster along webpacks

Error Hamster #0: Line 431 in : Uncaught ReferenceError: _slicedToArray is not defined
onerror @ hamsters.js:610

The examples in the website don't have the source code of the example

(sorry I'm not following the issue template, but the questions there didn't make sense to write down on this page)

I just started reading about the library and would like to see some example code. Now I'll dig through this repo instead.

I thought that the project examples should have the example's source code, or a link to it. It would make the website better!

Cheers!

Odd use of globals

It's slightly odd to see global scope injected scope for params and rtn variables. In my opinion, it would be more readable to see global variables removed and possibly separate the notions of hamster creation and task execution, like so:

var twoHamsters = Hamsters.spawn({
    count: 2,
    task: function (slice) {
        return slice.map(function (element) { return (element * 120) / 10; });
    },
    onSliceComplete: function (sliceResult) {
        console.log(sliceResult);
    },
    onTaskComplete: function (taskResult) {
        console.log(taskResult);
    }
});
twoHamsters.feed([1,2,3,4,5,6,7,8,9,0]);

Add support for Promises

How about adding support for Promises. Many asynchronous libraries now offer both callbacks and Promises. I would love to be able to use Hamsters.js with async/await.

Memoization could be faster / more verbose

Issue: Current hashing algorithm is slow, session storage 5mb limit introduces performance drawbacks.
Possible Solution: Use in memory caching, possibly optimize hash function

React-native android side, typeerror using hamsters and react-native-threads

Hello

I'm getting error message using hamsters and react-native-threads. I'm able to use both but still get typeError.

New Issue Report

  • What version of the library are you using?
    react-native: 0.55.2
    react-native-threads: 0.0.13
    hamsters: 5.1.1

  • What operating system are you seeing the issue on?
    Android / React-native

I'm able to call up the worker and it does it's job but I get this typeError message, so I'm not sure if I'm benefitting from using hamsters or not.

Hamsters.js v5.1.1 Info: Preparing the hamster wheels & readying hamsters @ 1542796747783
hamsters.node.min.js:1 Hamsters.js v5.1.1 Info: 16 Logical Threads Detected, Spawning 16 Hamsters @ 1542796747784
CameraScreen.js:547 TypeError: f.default.SharedWorker is not a constructor
    at e.value (hamsters.node.min.js:1)
    at e.value (hamsters.node.min.js:1)
    at e.value [as init] (hamsters.node.min.js:1)
    at CameraScreen.js:540
    at tryCallOne (core.js:37)
    at core.js:123
    at JSTimers.js:295
    at _callTimer (JSTimers.js:152)
    at _callImmediatesPass (JSTimers.js:200)
    at Object.callImmediates (JSTimers.js:464)

The code I use to call upp the hamster is:

hamsters.init({
                     maxThreads: 16,
                     Worker: this.workerThread.postMessage( workerMessage )
                   })

Is there some better way of using react-native-threads and hamsters together ?

Notifications to main thread from worker or child threads

Team,

Is there away we can send notifications (small payload, with processing info) from the worker threads back to the main thread? e.g. To possibly support providing progress data from threads that are spawned to the main thread? Maybe, allow the ability to pass in another callback for notifications.

Use dev branch version!!

A bug was discovered in transferrable object support in the most recent released version, the development branch resolves this issue but is not complete in regards to memoization yet. A new release will be out shortly that resolves all remaining discovered bugs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.