Skip to content

Use os.sched_getaffinity instead of os.cpu_count where possible #2160

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions metaflow/multicore_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ def parallel_imap_unordered(
func : Callable[[Any], Any]
Function taking a single argument and returning a result
iterable : Iterable[Any]
Iterable over arguments to pass to fun
Iterable over arguments to pass to func
max_parallel int, optional, default None
Maximum parallelism. If not specified, it uses the number of CPUs
Maximum parallelism. If not specified, it uses the number of assigned CPUs
dir : str, optional, default None
If specified, it's the directory where temporary files are created

Expand All @@ -109,7 +109,11 @@ def parallel_imap_unordered(
# Lazy import to save on startup time for metaflow as a whole
from multiprocessing import cpu_count

max_parallel = cpu_count()
max_parallel = (
len(getattr(os, "sched_getaffinity", lambda _: [])(0))
or os.cpu_count()
or 1
)

args_iter = iter(iterable)
pids = [_spawn(func, arg, dir) for arg in islice(args_iter, max_parallel)]
Expand Down Expand Up @@ -152,9 +156,9 @@ def parallel_map(
func : Callable[[Any], Any]
Function taking a single argument and returning a result
iterable : Iterable[Any]
Iterable over arguments to pass to fun
Iterable over arguments to pass to func
max_parallel int, optional, default None
Maximum parallelism. If not specified, it uses the number of CPUs
Maximum parallelism. If not specified, it uses the number of assigned CPUs
dir : str, optional, default None
If specified, it's the directory where temporary files are created

Expand Down
6 changes: 5 additions & 1 deletion metaflow/plugins/storage_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ def _bracket(min_v, v, max_v):
elif effective_cpu_limit == 0:
# There is (probably) no limit, use physical core count
processpool_max_workers = _bracket(
min_processes, os.cpu_count() or 1, max_processes
min_processes,
len(getattr(os, "sched_getaffinity", lambda _: [])(0))
or os.cpu_count()
or 1,
max_processes,
)
else:
# There is a limit, so let's bracket it within min / max
Expand Down
Loading