Skip to content

Commit

Permalink
Test windows cpu count
Browse files Browse the repository at this point in the history
  • Loading branch information
adamltyson committed Oct 13, 2023
1 parent 9bac2b7 commit cc7bd1b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 7 additions & 2 deletions brainglobe_utils/general/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ def get_num_processes(
else:
n_processes = n_cpu_cores

if platform.system() == "Windows":
n_max_processes = min(n_max_processes, MAX_PROCESSES_WINDOWS)
n_max_processes = limit_cpus_windows(n_max_processes)

if n_max_processes is not None:
if n_max_processes < n_processes:
Expand All @@ -172,6 +171,12 @@ def get_num_processes(
return int(n_processes)


def limit_cpus_windows(n_max_processes):
if platform.system() == "Windows":
n_max_processes = min(n_max_processes, MAX_PROCESSES_WINDOWS)
return n_max_processes


def get_cores_available():
try:
os.environ["SLURM_JOB_ID"]
Expand Down
29 changes: 29 additions & 0 deletions tests/tests/test_unit/test_general/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
from pathlib import Path
from random import shuffle
from unittest.mock import patch

import pytest

Expand Down Expand Up @@ -90,6 +91,7 @@ def test_get_num_processes():
assert os.cpu_count() == system.get_num_processes(min_free_cpu_cores=0)


## orig
def test_max_processes():
max_proc = 5
correct_n = min(os.cpu_count(), max_proc)
Expand All @@ -98,6 +100,33 @@ def test_max_processes():
)


def test_max_processes_windows_low():
cpu_count = 10
with patch(
"brainglobe_utils.general.system.platform.system",
return_value="Windows",
):
with patch(
"brainglobe_utils.general.system.psutil.cpu_count",
return_value=cpu_count,
):
assert system.limit_cpus_windows(cpu_count) == cpu_count


def test_max_processes_windows_high():
cpu_count = 128
with patch(
"brainglobe_utils.general.system.platform.system",
return_value="Windows",
):
with patch(
"brainglobe_utils.general.system.psutil.cpu_count",
return_value=cpu_count,
):
# 61 is max on Windows
assert system.limit_cpus_windows(cpu_count) == 61


class Paths:
def __init__(self, directory):
self.one = directory / "one.aaa"
Expand Down

0 comments on commit cc7bd1b

Please sign in to comment.