-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Fiber::ExecutionContext::Parallel#resize
#15956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Fiber::ExecutionContext::Parallel#resize
#15956
Conversation
Whenever the scheduler tries to reschedule or its main loop iterates, it checks if it has been told to shutdown now, and if so, drains its runnables queue back into the global queue, and terminates.
Relies on copy-on-write for the schedulers array: we dup and replace the array instead of mutating it, to modify #steal to read the reference once, then always refer to the same array (never mutated) so we can safely steal without acquiring the mutex.
71efcdb to
1a56f90
Compare
Fiber::ExecutionContext::Parallel#resize
|
~~EDIT: I believe the cooperative shutdown would use I dropped the enum and use a boolean that is enough for now. We'll see when we implement context shutdown if an enum would make sense. I also fixed an issue (must reset |
|
I'd use |
We must update the spinning threads counter before waking up a thread, because the thread's scheduler will declare itself as spinning right after unpark and expects the counter to already have been incremented. We must also stop spinning before shutting down because the scheduler might have been spinning.
|
|
Could we get a test for |
…16135) The `Concurrent` implementation mostly duplicates the `Parallel` one, and the parallel schedulers now take a few shortcuts when parallelism is set to 1 (skip stealing, quick evloop check) and `Concurrent` doesn't exhibit any noticeable performance improvement over `Parallel`. Having a single implementation instead of two, almost identical, will ease maintenance. I kept the type and didn't deprecate it yet. Its only value is that you can't mistakenly resize the context, and an exception is raised when you try to (related to #15956). Co-authored-by: Johannes Müller <[email protected]>
The concurrent context doesn't exhibit any performance improvement over the parallel context (see #16135). So we may as well use a parallel context as default context, and allow resizing it for more parallelism (see #15956). The default configuration is without parallelism (MT:1), thus the execution context remains concurrent only, which is backward compatible (nothing changes). Resizing the context to enable parallelism will be at the discretion of the application: it can keep using the `CRYSTAL_WORKERS` environment variable, and/or fallback to the number of CPUs or use a hardcoded value, and/or provide a command line argument, or anything else. Co-authored-by: Johannes Müller <[email protected]>
Allows to dynamically resize a Parallel execution context to a new maximum parallelism.
Parallelism can grow: merely adds more schedulers, which may eventually start more threads.
Parallelism can shrink: immediately removes any overflow schedulers and tell them to shutdown. The actual shutdown is cooperative, a scheduler won't notice the shutdown until it's current fiber is rescheduled.
Follow up to #15936 and #15946.