Skip to content

Commit

Permalink
Add factorization utility (#7048)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbourbeau authored Sep 23, 2022
1 parent 66585d5 commit 17c70e9
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions distributed/deploy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
import math

from dask.system import CPU_COUNT
from dask.utils import factors


def _factors(n: int) -> set[int]:
"""Return the factors of an integer
https://stackoverflow.com/a/6800214/616616
"""
seq = ([i, n // i] for i in range(1, int(pow(n, 0.5) + 1)) if n % i == 0)
return {j for item in seq for j in item}


def nprocesses_nthreads(n=CPU_COUNT):
Expand All @@ -29,6 +36,6 @@ def nprocesses_nthreads(n=CPU_COUNT):
if n <= 4:
processes = n
else:
processes = min(f for f in factors(n) if f >= math.sqrt(n))
processes = min(f for f in _factors(n) if f >= math.sqrt(n))
threads = n // processes
return (processes, threads)

0 comments on commit 17c70e9

Please sign in to comment.