Skip to content
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

Expand ranges when reading thread_siblings_list #849

Merged
merged 4 commits into from
Oct 4, 2023
Merged
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
25 changes: 22 additions & 3 deletions legate/util/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import platform
import sys
from functools import cached_property
from itertools import chain

from .fs import get_legate_paths, get_legion_paths
from .types import CPUInfo, GPUInfo, LegatePaths, LegionPaths
Expand Down Expand Up @@ -98,9 +99,7 @@ def cpus(self) -> tuple[CPUInfo, ...]:
line = open(
f"/sys/devices/system/cpu/cpu{i}/topology/thread_siblings_list" # noqa E501
).read()
sibling_sets.add(
tuple(sorted(int(x) for x in line.strip().split(",")))
)
sibling_sets.add(extract_values(line.strip()))
return tuple(
CPUInfo(siblings) for siblings in sorted(sibling_sets)
)
Expand Down Expand Up @@ -139,3 +138,23 @@ def gpus(self) -> tuple[GPUInfo, ...]:
results.append(GPUInfo(i, info.total))

return tuple(results)


def expand_range(value: str) -> tuple[int, ...]:
if value == "":
return tuple()
if "-" not in value:
return tuple((int(value),))
start, stop = value.split("-")

return tuple(range(int(start), int(stop) + 1))


def extract_values(line: str) -> tuple[int, ...]:
return tuple(
sorted(
chain.from_iterable(
expand_range(r) for r in line.strip().split(",")
)
)
)
74 changes: 74 additions & 0 deletions tests/unit/legate/util/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,77 @@ def test_gpus_osx(self) -> None:
msg = "GPU execution is not available on OSX."
with pytest.raises(RuntimeError, match=msg):
s.gpus


class Test_expand_range:
def test_errors(self) -> None:
with pytest.raises(ValueError):
m.expand_range("foo")

def test_empty(self) -> None:
assert m.expand_range("") == ()

@pytest.mark.parametrize("val", ("0", "1", "12", "100"))
def test_single_number(self, val: str) -> None:
assert m.expand_range(val) == (int(val),)

@pytest.mark.parametrize("val", ("0-10", "1-2", "12-25"))
def test_range(self, val: str) -> None:
start, stop = val.split("-")
assert m.expand_range(val) == tuple(range(int(start), int(stop) + 1))


class Test_extract_values:
def test_errors(self) -> None:
with pytest.raises(ValueError):
m.extract_values("foo")

def test_empty(self) -> None:
assert m.extract_values("") == ()

@pytest.mark.parametrize("val", ("0", "1,2", "3,5,7"))
def test_individual(self, val: str) -> None:
expected = {
"0": (0,),
"1,2": (1, 2),
"3,5,7": (3, 5, 7),
}
assert m.extract_values(val) == expected[val]

@pytest.mark.parametrize("val", ("2,1", "8,5,3,2", "1,3,2,5,4,7,6"))
def test_individual_ordered(self, val: str) -> None:
expected = {
"2,1": (1, 2),
"8,5,3,2": (2, 3, 5, 8),
"1,3,2,5,4,7,6": (1, 2, 3, 4, 5, 6, 7),
}
assert m.extract_values(val) == expected[val]

@pytest.mark.parametrize("val", ("0-2", "0-2,4-5", "0-1,3-5,8-11"))
def test_range(self, val: str) -> None:
expected = {
"0-2": (0, 1, 2),
"0-2,4-5": (0, 1, 2, 4, 5),
"0-1,3-5,8-11": (0, 1, 3, 4, 5, 8, 9, 10, 11),
}
assert m.extract_values(val) == expected[val]

@pytest.mark.parametrize("val", ("2-3,0-1", "0-1,4-5,2-3"))
def test_range_ordered(self, val: str) -> None:
expected = {
"2-3,0-1": (0, 1, 2, 3),
"0-1,4-5,2-3": (0, 1, 2, 3, 4, 5),
}
assert m.extract_values(val) == expected[val]

@pytest.mark.parametrize(
"val", ("0,1-2", "1-2,0", "0,1-2,3,4-5,6", "5-6,4,1-3,0")
)
def test_mixed(self, val: str) -> None:
expected = {
"0,1-2": (0, 1, 2),
"1-2,0": (0, 1, 2),
"0,1-2,3,4-5,6": (0, 1, 2, 3, 4, 5, 6),
"5-6,4,1-3,0": (0, 1, 2, 3, 4, 5, 6),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW just FYI it's common to move values + expected values together at the same time through parameters. The danger here is that the keys in the expected dict become out of sync with the val parameter values since they are duplicated. I think this is fine in this small case, but if you are interested there are more examples, etc on the pytest site

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created PR #851 because I really like the non-duplicating version.

assert m.extract_values(val) == expected[val]