Skip to content

Commit

Permalink
Fix OSX compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bouweandela committed Jan 17, 2025
1 parent 0dce90c commit 469ad46
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
13 changes: 11 additions & 2 deletions esmvalcore/_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,15 @@ def __repr__(self):
return string


def available_cpu_count() -> int:
"""Return the number of available CPU cores."""
if hasattr(os, "sched_getaffinity"):
return len(os.sched_getaffinity(0))
if count := os.cpu_count():
return count
return 1


class TaskSet(set):
"""Container for tasks."""

Expand Down Expand Up @@ -867,7 +876,7 @@ def _get_dask_config(self, max_parallel_tasks: int) -> dict:
# No need to do anything when we are not running PreprocessingTasks.
return {}

n_available_cpu_cores = len(os.sched_getaffinity(0))
n_available_cpu_cores = available_cpu_count()
n_threaded_dask_schedulers = min(n_preproc_tasks, max_parallel_tasks)
n_workers = max(
1, round(n_available_cpu_cores / n_threaded_dask_schedulers)
Expand All @@ -890,7 +899,7 @@ def _run_parallel(self, scheduler_address, max_parallel_tasks):
n_running = 0

if max_parallel_tasks is None:
max_parallel_tasks = len(os.sched_getaffinity(0))
max_parallel_tasks = available_cpu_count()
max_parallel_tasks = min(max_parallel_tasks, n_tasks)
logger.info(
"Running %s tasks using %s processes", n_tasks, max_parallel_tasks
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/task/test_available_cpu_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest

from esmvalcore import _task


def test_available_cpu_count_linux(mocker):
mocker.patch.object(_task.os, "sched_getaffinity", return_value={0, 1})
result = _task.available_cpu_count()
assert result == 2
_task.os.sched_getaffinity.assert_called_once_with(0)


@pytest.mark.parametrize(
"cpu_count,expected",
[
(None, 1),
(2, 2),
],
)
def test_available_cpu_count_osx(monkeypatch, mocker, cpu_count, expected):
monkeypatch.delattr(_task.os, "sched_getaffinity")
mocker.patch.object(_task.os, "cpu_count", return_value=cpu_count)
result = _task.available_cpu_count()
assert result == expected
_task.os.cpu_count.assert_called_once_with()

0 comments on commit 469ad46

Please sign in to comment.