HenrikBengtsson / doFuture

:rocket: R package: doFuture - Use Foreach to Parallelize via Future Framework
https://doFuture.futureverse.org
84 stars 6 forks source link

If changing number of future workers, then `registerDoFuture()` needs to be recalled #9

Closed HenrikBengtsson closed 7 years ago

HenrikBengtsson commented 7 years ago

If changing number of future workers, then registerDoFuture() needs to be recalled in order to update foreach about it. This is what registerDoFuture() does:

registerDoFuture <- function() {
  info <- function(data, item) {
    switch(item,
      name = "doFuture",
      version = packageVersion("doFuture"),
      workers = nbrOfWorkers()
    )
  }

  setDoPar(doFuture, data=NULL, info=info)
}

That setDoPar(..., info) is what updates foreach.

The question is, can we automate / hide this? For instance, can it be check when our foreach-adapter doFuture() is called? Or is that too late?

HenrikBengtsson commented 7 years ago

Ah... false alarm; info is not a list but a function that foreach uses to query the resource each time. Nice. Proof that it works as expected:

> library("doFuture")

> registerDoFuture()
> nbrOfWorkers()
[1] 1
> getDoParWorkers()
[1] 1

> plan(multiprocess, workers = 2L)
> nbrOfWorkers()
[1] 2
> getDoParWorkers()
[1] 2

> library("future.BatchJobs")
> plan(batchjobs_local)
> nbrOfWorkers()
[1] 1
> getDoParWorkers()
[1] 1

> plan(batchjobs_torque)
> nbrOfWorkers()
[1] Inf
> getDoParWorkers()
[1] Inf