Question: What is the expected speed up when the communication is minimal? #125
Replies: 3 comments
-
It depends. Ideally
On CPU with single core, yes.
If each worker runs for 10 seconds, it will still take 10 second. |
Beta Was this translation helpful? Give feedback.
-
|
I have a 12500H CPU with 4P Cores 8E Cores and Total Threads 16. Also I did not have more apps working that could push the system to its limits. function timedCalculation() {
const targetTime = 10000;
const start = Date.now();
let sum = 0;
let operations = 0;
// Busy loop until target time has passed
while (Date.now() - start < targetTime) {
++operations;
// Do some meaningless math to keep the CPU busy
sum += Math.sqrt(Math.random() * 1000);
}
return operations;
} |
Beta Was this translation helpful? Give feedback.
-
|
Can you show the full example? I'm seeing different results with following: // main.mjs
import { availableParallelism } from 'node:os'
import Tinypool from './dist/index.js'
import timedCalculation from './worker.mjs'
const WORKER_COUNT = availableParallelism() - 1
const IDS = Array.from({ length: WORKER_COUNT }).keys()
const main = timedCalculation()
const pool = new Tinypool({
filename: new URL('./worker.mjs', import.meta.url).href,
maxThreads: WORKER_COUNT,
minThreads: WORKER_COUNT,
})
const workerCounts = await Promise.all(IDS.map(() => pool.run()))
const workers = workerCounts.reduce((sum, count) => sum + count, 0)
console.log('main ', main)
console.log('workers', workers)// worker.mjs
export default function timedCalculation() {
const targetTime = 10_000
const start = Date.now()
let sum = 0
let operations = 0
// Busy loop until target time has passed
while (Date.now() - start < targetTime) {
++operations
// Do some meaningless math to keep the CPU busy
sum += Math.sqrt(Math.random() * 1000)
}
return operations
}So main did |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Suppose you have a CPU bound task: "Count how many times you can calculate the square root in 10 secs"
When I tried a serial implementation, it was faster than 2 workers, 3 workers, 4 workers of doing the same task. Did not try with more workers.
Is this expected? I supposed I would see a linear speed up since these works do not send a message until the 10secs pass so I expected almost 0 overhead.
Beta Was this translation helpful? Give feedback.
All reactions