#How to dynamically distribute work across already running threads?

1 messages · Page 1 of 1 (latest)

merry glade
#

Hi,

  • I believe this is more of a multi-threading question rather than being zig specific, if it's the former please disregard
  • I've recorded this demo to explain it better
  • In the demo, you can see all threads are processing fixed amount of work (calculating pixel color) however some threads finish sooner and is there any way to redistribute the work?
  • I tried to re-produce the scenario, here's the gist
  • Output (basically there's delay for thread 2 and it's not clearly evident from o/p though, however delay can be seen in the demo)
$ zig run /tmp/main.zig
Thread: 0 working on range 1 to 3
crunching value for 1
crunching value for 2
crunching value for 3
Thread: 1 working on range 4 to 6
slow thread 1, crunching value for 4
Thread: 2 working on range 7 to 9
crunching value for 7
crunching value for 8
crunching value for 9
slow thread 1, crunching value for 5
slow thread 1, crunching value for 6

Thanks in advance.

Recorded by leelavg

Gist

zig multi-thread. GitHub Gist: instantly share code, notes, and snippets.

jovial spindle
#

a common technique is to set up a queue of tasks and have each thread continually dequeue a task and then execute it until the queue is empty

rugged hornet
#

^ on windows you can directly use IOCP as your queue for optimal work distribution but on linux you need a separate queue 😦
std.atomic.Queue comes to mind but if you have enough throughput a spin-lock while there's items in the queue should give better performance

merry glade
#

Ack, current method doesn't use mutex/queues and so asked about possible changes to current flow.

So it seems, I need to follow different flow then, will implement that, basically each thread eagerly picking work from a global queue thonkHMM

jovial spindle
#

yeah, i've written that general flow a couple times and it works pretty well