Closed HenrikBengtsson closed 1 year ago
I've added support for availableCores(methods = "connections")
;
> cons <- replicate(n = 120L, { file(tempfile(), open = "w") }, simplify = FALSE)
> freeConnections()
[1] 5
> availableCores(methods = "connections")
connections
5
To conveniently add this method to the existing defaults, we can use:
> availableCores(constraints = "connections")
connections
5
> availableCores(constraints = "connections", which = "all")
system cgroups.cpuset nproc connections
8 8 8 5
Note that availableCores()
always returns a positive integer, so even when freeConnections() == 0
, 1L
is returned.
Background
We can only use no-more than 125 parallel PSOCK, SOCK, and MPI workers, because of the hard-coded upper limit of 125+3 open connections in R.
If we use
we risk running into:
on modern machines. A way to mitigate this problem is to use:
With the corner-case problem that we now might end up with
ncores = 0
, which gives:Idea
Maybe we could introduce a new argument
constraints
, e.g.to cover the case when we work with PSOCK, SOCK, and MPI clusters? Note that not all parallel backends rely on connections, so this should not be default. For example,
mclapply()
and callr backends do not use connections.One thing to figure out is what to do when there are zero free connections available. Currently,
availableCores()
is designed to always return at least1L
. That "contract" is easy to understand and program with. To keep this guarantee also whenfreeConnections() == 0
, we could do:and leave it to the downstream code to fail gracefully. For example,
makeClusterPSOCK()
already handles this with: