mednet.utils.resources

Tools for interacting with the running computer or GPU.

Module Attributes

GB

The number of bytes in a gigabyte.

Functions

aggregate(data[, start, end])

Aggregate monitored data to average/max each entry.

cpu_constants()

Return static CPU information about the current system.

cuda_constants()

Return GPU (static) information using nvidia-smi.

mps_constants()

Return GPU (static) information using /usr/bin/powermetrics.

Classes

ResourceMonitor(interval, device_type, main_pid)

An external, non-blocking CPU/GPU resource monitor.

mednet.utils.resources.GB = 1073741824.0

The number of bytes in a gigabyte.

mednet.utils.resources.cuda_constants()[source]

Return GPU (static) information using nvidia-smi.

Check _run_nvidia_smi() for operational details if necessary.

Returns:

If nvidia-smi is not available, returns None, otherwise, we return a dictionary containing the following nvidia-smi query information, in this order:

  • gpu_name, as gpu_name (str)

  • driver_version, as gpu_driver_version (str)

  • memory.total, as gpu_memory_total (transformed to gigabytes, float)

Return type:

dict[str, str | int | float]

mednet.utils.resources.mps_constants()[source]

Return GPU (static) information using /usr/bin/powermetrics.

Returns:

If nvidia-smi is not available, returns None, otherwise, we return a dictionary containing the following nvidia-smi query information, in this order:

  • gpu_name, as gpu_name (str)

  • driver_version, as gpu_driver_version (str)

  • memory.total, as gpu_memory_total (transformed to gigabytes, float)

Return type:

dict[str, str | int | float]

mednet.utils.resources.cpu_constants()[source]

Return static CPU information about the current system.

Returns:

A dictionary containing these entries:

  1. cpu_memory_total (float): total memory available, in gigabytes

  2. cpu_count (int): number of logical CPUs available.

Return type:

dict[str, int | float]

class mednet.utils.resources.ResourceMonitor(interval, device_type, main_pid)[source]

Bases: object

An external, non-blocking CPU/GPU resource monitor.

Parameters:
  • interval (int | float) – Number of seconds to wait between each measurement (maybe a floating point number as accepted by time.sleep()).

  • device_type (Literal['cpu', 'cuda', 'mps']) – String representation of one of the supported pytorch device types triggering the correct readout of resource usage.

  • main_pid (int) – The main process identifier to monitor.

start()[source]

Start the monitoring process.

Return type:

None

stop()[source]

Stop the monitoring process.

Return type:

None

clear()[source]

Prepare for an acquisition cycle by clearing logged events.

Return type:

None

checkpoint()[source]

Force the monitoring process to yield data and clear the internal accumulator.

Returns:

A list of hardware counters that were monitored during the start/end interval.

Return type:

list[dict[str, int | float]]

mednet.utils.resources.aggregate(data, start=0, end=None)[source]

Aggregate monitored data to average/max each entry.

Parameters:
  • data (list[dict[str, int | float]]) – Input data collected by the resource monitor, in the format of a list.

  • start (int | None) – Start index to aggregate data from at the data list.

  • end (int | None) – End index to aggregate data to at the data list.

Returns:

A collapsed representation of the input list with the common keys found in all dictionaries and averages (or maximum values) of measures found across similar keys.

Return type:

dict[str, int | float]