Skip to content
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

Scopes and blocking operations #835

Open
pkolaczk opened this issue Mar 22, 2021 · 5 comments
Open

Scopes and blocking operations #835

pkolaczk opened this issue Mar 22, 2021 · 5 comments

Comments

@pkolaczk
Copy link

I've run into a problem with scopes. It looks like scope switches to one of the thread-pool threads for its body. So if the body does any blocking operation, it is possible to run into a deadlock.

Example 1: I'd like to spawn a bunch of tasks on the thread-pool inside the scope so they can access the outer variables. Because processing is much slower than producing, this quickly pushes thousands of tasks into rayon queue and memory consumption is high. So I wanted to limit the number of tasks produced to a number low enough that memory is not a problem, but also high enough to allow parallel processing. Unfortunately any attempt to block the producer loop would cause a deadlock if rayon pool is sized to 1 thread.

Example 2: Spawning multiple tasks that send the results to the channel and receiving the results serially. I can't put the receiver loop inside the scope, because it can block and again - deadlock is possible if the pool is sized to only 1 thread.

Is there any way to get around this limitation?
Perfectly if scope did not switch to a thread pool thread, but ran the body on the parent thread would solve this.

@nikomatsakis
Copy link
Member

We've talked about having having a scope that always runs on the parent thread, but we never added it. I'm trying to remember the details why we did not, @cuviper may recall if there are particular reasons.

@cuviper
Copy link
Member

cuviper commented Mar 22, 2021

See #562 to allow working without Send, which would also have the effect you want here, since that would guarantee we're not moving execution to the thread pool. There's a prototype implementation, but I haven't yet reviewed that further.

@lilydjwg
Copy link

I suddenly find my program stuck because the machine has only one CPU core. Setting RAYON_NUM_THREADS to larger than one works but it's unexpected anyway.

@cuviper
Copy link
Member

cuviper commented May 19, 2021

Perfectly if scope did not switch to a thread pool thread, but ran the body on the parent thread would solve this.

Rayon 1.5.1 now has in_place_scope which works this way -- can you try it?

lilydjwg added a commit to lilydjwg/swapview that referenced this issue May 20, 2021
@lilydjwg
Copy link

in_place_scope works in my case, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants