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

How to tell if you are already within %dopar% loop? #35

Open
JSchoenbachler opened this issue May 24, 2022 · 2 comments
Open

How to tell if you are already within %dopar% loop? #35

JSchoenbachler opened this issue May 24, 2022 · 2 comments

Comments

@JSchoenbachler
Copy link

Just seeing if there is a sure fire way to determine if you are already within a foreach loop using %dopar%? As it is currently, I have a workaround function that parses sys.status() for any foreach calls that also use %dopar%-

isAlreadyInParallel = function() {
  status = sys.status()
  return(any(grepl(
    '%dopar%',
    status$sys.calls[grepl('foreach(', status$sys.calls, fixed = TRUE)],
    fixed = TRUE)))}

However, this feels pretty limited and doesn't cover the case in which someone uses a variable to hold %dopar% like below-

doFunction = if (foreach::getDoParRegistered()) `%dopar%` else `%do%`

doFunction(foreach(1:5), {...})
@HenrikBengtsson
Copy link

HenrikBengtsson commented May 24, 2022

I strongly recommend treating %dopar% as a different feature than %do%. They're very different and should not be though of as they can substitute each other. To run sequentially with %dopar%, always use registerDoSEQ(). Do not switch code to use %do%.

Unfortunately, I don't think there's a public interface to query which %dopar% adaptor is registered. [removed code snippet]. See my comment below.

Finally, (disclaimer: I'm the maintainer), if you use doFuture::registerDoFuture(), then %dopar% will process via the future framework. cf. https://doFuture.futureverse.org/. There, you control what sequential or parallel backend to use via future::plan() and that one you can query for things such number of parallel workers. Note also, that the future framework automatically protects against exploding, nested parallelization, if that's what you're after, cf. https://future.futureverse.org/#nested-futures-and-evaluation-topologies.

My $.02 (not a foreach maintainer)

@HenrikBengtsson
Copy link

Sorry, I was too quick; I forgot about getDoParName() and getDoParWorkers(), e.g.

> foreach::registerDoSEQ()
> foreach::getDoParName()
[1] "doSEQ"
> foreach::getDoParWorkers()
[1] 1

> doParallel::registerDoParallel(2)
> foreach::getDoParName()
[1] "doParallelMC"
> foreach::getDoParWorkers()
[1] 2

> doFuture::registerDoFuture()
> future::plan("sequential")
> foreach::getDoParName()
[1] "doFuture"
> foreach::getDoParWorkers()
[1] 1

> future::plan("multisession", workers = 2)
> foreach::getDoParWorkers()
[1] 2

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

2 participants