mratsim / weave

A state-of-the-art multithreading runtime: message-passing based, fast, scalable, ultra-low overhead
Other
535 stars 22 forks source link

Research on IO-bound tasks #22

Open mratsim opened 4 years ago

mratsim commented 4 years ago

Weave / Project Picasso focuses on CPU-bound tasks, i.e. those are non-blocking and you can throw more CPU at it to have your result faster.

For IO-bound tasks the idea was to defer to specialized libraries like asyncdispatch and Chronos that use OS primitives (epoll/IOCP/kqueue) to handle IO efficiently.

However even for compute bound tasks we will have to deal with IO latencies for example in a distributed system or cluster. So we need a solution to do useful work in the downtime without blocking a whole thread.

That means:

Research

Implementations

mratsim commented 4 years ago

Also Rust coupled both IO and a task parallelism in the past and decided to avoid that:

https://github.com/aturon/rfcs/blob/remove-runtime/active/0000-remove-runtime.md

https://github.com/rust-lang/rfcs/blob/master/text/0230-remove-runtime.md

mratsim commented 4 years ago

An idea on how to play well with Asyncdispatch or Chronos or any future async/await library.

They all offer a poll() function that runs their event loop.

We can add a field pollHook*: proc() {.nimcall, gcsafe.} on each worker. It would be setup by setPollingFunction(_: typedesc[Weave], poll: proc() {.nimcall, gcsafe.}) before Weave initialization (at first, can be relaxed later).

Then we modify loadBalance(), sync(), syncScope() to interleave pollHook calls before and after executing a task.

Note that loadBalance() is called in-between each parallelFor iterations:

Note that worker threads will sleep if they have no tasks, but it does not make sense for them to try to handle IO events without a task.

A potential issue is that a task can be migrated or for a parallel loop, it can even be split and then executed on 2 different threads, i.e. are the async libraries using {.threadvar.} to manage some global state? Because that will not work.

mratsim commented 4 years ago

RFC #132 and its implementation with Weave as an independent background service is probably a better path forward #136

mratsim commented 1 year ago

See https://github.com/weavers-guild/weave-io