How to tell if you are already within %dopar% loop?
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), {...})
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)
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