Last active
April 10, 2022 13:22
Revisions
-
jthomas revised this gist
May 8, 2019 . 1 changed file with 0 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +0,0 @@ -
jthomas created this gist
May 8, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ { "name": "worker_threads", "version": "1.0.0", "description": "", "main": "primes-with-workers.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "James Thomas <[email protected]> (https://jamesthom.as)", "license": "MIT" } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ 'use strict'; const { Worker } = require('worker_threads'); const os = require('os') const threadCount = os.cpus().length const compute_primes = async (start, range) => { return new Promise((resolve, reject) => { let primes = [] console.log(`adding worker (${start} => ${start + range})`) const worker = new Worker('./worker.js', { workerData: { start, range }}) worker.on('error', reject) worker.on('exit', () => resolve(primes)) worker.on('message', msg => { primes = primes.concat(msg) }) }) } async function main(params) { const { min, max } = params const range = Math.ceil((max - min) / threadCount) let start = min < 2 ? 2 : min const workers = [] console.log(`Calculating primes with ${threadCount} threads...`); for (let i = 0; i < threadCount - 1; i++) { const myStart = start workers.push(compute_primes(myStart, range)) start += range } workers.push(compute_primes(start, max - start)) const primes = await Promise.all(workers) return { primes: primes.flat() } } exports.main = main This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ 'use strict'; const min = 2 function main(params) { const { start, end } = params console.log(params) const primes = [] let isPrime = true; for (let i = start; i < end; i++) { for (let j = min; j < Math.sqrt(end); j++) { if (i !== j && i%j === 0) { isPrime = false; break; } } if (isPrime) { primes.push(i); } isPrime = true; } return { primes } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ 'use strict'; const { Worker, isMainThread, parentPort, workerData } = require('worker_threads'); const min = 2 function generatePrimes(start, range) { const primes = [] let isPrime = true; let end = start + range; for (let i = start; i < end; i++) { for (let j = min; j < Math.sqrt(end); j++) { if (i !== j && i%j === 0) { isPrime = false; break; } } if (isPrime) { primes.push(i); } isPrime = true; } return primes } const primes = generatePrimes(workerData.start, workerData.range); parentPort.postMessage(primes)