Skip to content
Snippets Groups Projects
Commit 1d5a4109 authored by Amanda Ghassaei's avatar Amanda Ghassaei
Browse files

task are deallocated after they are finished

parent 82bfca72
Branches
No related tags found
No related merge requests found
...@@ -7,12 +7,16 @@ $(function(){ ...@@ -7,12 +7,16 @@ $(function(){
three = Three(); three = Three();
setupNavBar(); setupNavBar();
workers = persistentWorkers(4); workers = persistentWorkers(8);
workers.map([11,12,13,14,15, 18, 30], executable, {}); workers.map([11,12], executable, {}, incrCallback);
function executable(){ function executable(){
return arg*arg; return arg*arg;
} }
function incrCallback(result){
console.log(result);
}
}); });
...@@ -17,55 +17,82 @@ function persistentWorkers(numWorkers){ ...@@ -17,55 +17,82 @@ function persistentWorkers(numWorkers){
var workerURL = makeBlobURL(URL, myWorker); var workerURL = makeBlobURL(URL, myWorker);
for (var i=0;i<numWorkers;i++){ for (var i=0;i<numWorkers;i++){
var worker = new Worker(workerURL); var worker = new Worker(workerURL);
worker.onmessage = workerCallback; worker.onmessage = onMessage;
worker.postMessage({url: document.location.toString()}); worker.postMessage({url: document.location.toString()});
allWorkers.push(worker); allWorkers.push(worker);
} }
URL.revokeObjectURL(workerURL); URL.revokeObjectURL(workerURL);
function map(data, executable, env){ function map(data, executable, env, incrCallback){
//save args in map queue //save args in map queue
mapQueue.push({data:data, executable:executable, env:env, index:0}); mapQueue.push({data:data, executable:executable, env:env, index:0, incrCallback:incrCallback, finished:false, activeThreads:0});
for (var i=0;i<allWorkers.length;i++){ for (var i=0;i<allWorkers.length;i++){
allWorkers[i].postMessage({isWorking:true});//ask workers if they are busy allWorkers[i].postMessage({isWorking:true});//ask workers if they are busy
} }
} }
function workerCallback(e){ function onMessage(e){
if (e.data.result) console.log(e.data.result);//handle result first if (e.data.result){//handle result first
if (e.data.isWorking === false){ //get current work item off queue
//get next work item off queue
if (mapQueue.length == 0) return; if (mapQueue.length == 0) return;
var currentTask = mapQueue[0]; var currentTask = mapQueue[0];
var currentIndex = currentTask.index; currentTask.activeThreads--;//decrement active threads
currentTask.index = currentTask.index+1;
//check that the index is not out of bounds currentTask.incrCallback(e.data.result);//incremental callback
if (currentTask.data.length<=currentIndex){ if (currentTask.finished && currentTask.activeThreads == 0){
mapQueue.shift();//remove first element mapQueue.shift();//remove first element
console.log("end of task");
e.data.result = null;//remove result so it doesn't get handled twice
workerCallback(e);//try again in case there is another item in the queue
return;
} }
}
var nextTask = getNextTask(mapQueue[0], 0);
if (!nextTask) return;
var currentIndex = nextTask.index;
nextTask.index++;
//check that the index is not out of bounds
if (nextTask.data.length<=currentIndex){
nextTask.finished = true;
e.data.result = null;//remove result so it doesn't get handled twice
onMessage(e);//try again in case there is another item in the queue to start
return;
}
if (e.data.isWorking === false){
nextTask.activeThreads++;
e.target.postMessage({ e.target.postMessage({
arg:currentTask.data[currentIndex], arg:nextTask.data[currentIndex],
localEnv:currentTask.env, localEnv:nextTask.env,
executable:currentTask.executable.toString()}); executable:nextTask.executable.toString()});
}
}
function getNextTask(currentTask, index){
if (!currentTask) return null;
if (currentTask.finished){
var nextIndex = index+1;
if (mapQueue.length<=nextIndex) return null;
return getNextTask(mapQueue[nextIndex]);
} else {
return currentTask;
} }
} }
function incrementalCallback(){
}
function makeBlobURL(URL, someFunction) { function makeBlobURL(URL, someFunction) {
var blob = new Blob(["(" + someFunction.toString() + ")()"], { type: "text/javascript" }); var blob = new Blob(["(" + someFunction.toString() + ")()"], { type: "text/javascript" });
return URL.createObjectURL(blob); return URL.createObjectURL(blob);
} }
return {map:map}//return all public methods and vars return {map:map};//return all public methods and vars
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment