Skip to content

Commit

Permalink
Add .devcontainer
Browse files Browse the repository at this point in the history
Add num functions

Switch to building and storing image for tests

Add nvidia jetson xavier agx and apple m2
  • Loading branch information
maartenarnst committed Jul 29, 2024
1 parent 622583c commit b87795a
Show file tree
Hide file tree
Showing 13 changed files with 633 additions and 39 deletions.
12 changes: 12 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"image": "ghcr.io/uliegecsm/hwloc-xml-parser",
"extensions" : [
"eamodio.gitlens",
"mhutchie.git-graph",
"ms-azuretools.vscode-docker"
],
"runArgs": [
"--privileged",
],
"onCreateCommand": "git config --global --add safe.directory $PWD"
}
56 changes: 56 additions & 0 deletions .github/workflows/build.image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Build image

on:
push:
branches:
- main
paths:
- 'requirements/*'
- 'docker/dockerfile'
pull_request:
branches:
- main
paths:
- 'requirements/*'
- 'docker/dockerfile'

jobs:

set-vars:
uses: ./.github/workflows/set-vars.yml

build-image:
needs: [set-vars]
runs-on: [ubuntu-latest]
container:
image: docker:latest
permissions:
packages: write
steps:
- name: Checkout code.
uses: actions/checkout@v4

- name: Set up QEMU.
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx.
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry.
uses: docker/login-action@v3
with:
registry: ${{ needs.set-vars.outputs.CI_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push.
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.ref == 'refs/heads/main' }}
file: docker/dockerfile
tags: ${{ needs.set-vars.outputs.CI_IMAGE }}
cache-from: type=registry,ref=${{ needs.set-vars.outputs.CI_IMAGE }}
cache-to: type=inline
labels: "org.opencontainers.image.source=${{ github.repositoryUrl }}"
24 changes: 24 additions & 0 deletions .github/workflows/set-vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
on:
workflow_call:
outputs:
CI_IMAGE:
value: ${{ jobs.set-vars.outputs.CI_IMAGE }}
CI_REGISTRY:
value: ${{ jobs.set-vars.outputs.CI_REGISTRY }}

env:
REGISTRY: ghcr.io

jobs:

set-vars:
runs-on: [ubuntu-latest]
outputs:
CI_IMAGE : ${{ steps.common.outputs.CI_IMAGE }}
CI_REGISTRY: ${{ steps.common.outputs.CI_REGISTRY }}
steps:
- name: Export common variables.
id : common
run : |
echo "CI_IMAGE=${{ env.REGISTRY }}/${{ github.repository }}" >> $GITHUB_OUTPUT
echo "CI_REGISTRY=${{ env.REGISTRY }}" >> $GITHUB_OUTPUT
21 changes: 8 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,28 @@ on:
- main

jobs:
set-vars:
uses: ./.github/workflows/set-vars.yml

test:
needs: [set-vars]
runs-on: [ubuntu-latest]
container:
image: python:3.9
image: ${{ needs.set-vars.outputs.CI_IMAGE }}
steps:
- name: Checkout code.
uses: actions/checkout@v4

- name: Prepare.
run : |
pip install -r requirements.txt
pip install pytest
- name: Run tests.
run : |
python -m pytest tests/test_topology.py
install-as-package:
install-as-package-and-test:
needs: [set-vars]
runs-on: [ubuntu-latest]
container:
image: python:3.9
image: ${{ needs.set-vars.outputs.CI_IMAGE }}
steps:
- name: Prepare.
run : |
apt update
apt --yes --no-install-recommends install hwloc
- name: Install as package.
run : |
pip install git+https://github.com/uliegecsm/hwloc-xml-parser.git@${{ github.sha }}
Expand Down
18 changes: 18 additions & 0 deletions docker/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.9

RUN --mount=type=bind,target=/requirements,type=bind,source=requirements <<EOF

set -ex

# Install system dependencies
apt update

apt --yes --no-install-recommends install $(grep -vE "^\s*#" /requirements/requirements.system.txt | tr "\n" " ")

apt clean && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
pip install -r /requirements/requirements.python.txt
pip install pytest

EOF
58 changes: 57 additions & 1 deletion hwloc_xml_parser/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def __init__(self, element : xml.etree.ElementTree.Element, parent : 'Package')
self.hierarchical_index = f'{parent.type}:{parent.os_index}.{self.type}:{self.os_index}'
self.pus = [PU(x, parent = self) for x in element.findall(path = "object[@type='PU']")]

@typeguard.typechecked
def get_num_pus(self) -> int:
"""
Returns the number of processing units.
"""
return len(self.pus)

class Package(Object):
"""
Package. Usually equivalent to a socket.
Expand All @@ -66,6 +73,27 @@ def __init__(self, element : xml.etree.ElementTree.Element) -> None:
self.hierarchical_index = f'Package:{self.os_index}'
self.cores = [Core(x, parent = self) for x in element.findall(path = "object[@type='Core']")]

@typeguard.typechecked
def get_num_cores(self) -> int:
"""
Returns the number of cores.
"""
return len(self.cores)

@typeguard.typechecked
def get_num_pus(self) -> int:
"""
Returns the number of processing units.
"""
return sum([core.get_num_pus() for core in self.cores])

@typeguard.typechecked
def all_equal_num_pus_per_core(self) -> bool:
"""
Returns `True` if all cores have the same number of processing units.
"""
return all([core.get_num_pus() == self.cores[0].get_num_pus() for core in self.cores])

class SystemTopology:
"""
Read the system topology as reported by `hwloc`'s tool `lstopo`.
Expand Down Expand Up @@ -121,7 +149,7 @@ def _parse(self, filename : typing.Union[pathlib.Path, str]) -> None:

if not self.machine.tag == 'object':
raise ValueError(f"Expected 'object' tag, got '{self.machine.tag}'")

if not self.machine.attrib['type'] == 'Machine':
raise ValueError(f"Expected 'Machine' type, got '{self.machine.attrib['type']}'")

Expand Down Expand Up @@ -173,3 +201,31 @@ def recurse_pus(self) -> typing.Generator:
for core in self.recurse_cores():
for pu in core.pus:
yield pu

@typeguard.typechecked
def get_num_packages(self) -> int:
"""
Returns the number of packages.
"""
return len(self.packages)

@typeguard.typechecked
def get_num_cores(self) -> int:
"""
Returns the number of cores.
"""
return sum([package.get_num_cores() for package in self.packages])

@typeguard.typechecked
def get_num_pus(self) -> int:
"""
Returns the number of processing units.
"""
return sum([package.get_num_pus() for package in self.packages])

@typeguard.typechecked
def all_equal_num_pus_per_core(self) -> bool:
"""
Returns `True` if all cores have the same number of processing units.
"""
return all([package.all_equal_num_pus_per_core() for package in self.packages])
File renamed without changes.
1 change: 1 addition & 0 deletions requirements/requirements.system.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hwloc
Loading

0 comments on commit b87795a

Please sign in to comment.