def divide_resources(a: Resources, b: Resources, membw_read_write_ratio: Optional[float] = None) -> Resources: """if ratio is provided then Flattens rt.MEMBW_READ and rt.MEMBW_WRITE to rt.MEMBW_FLAT.""" assert set(a.keys()) == set(b.keys()), \ 'the same dimensions must be provided for both resources' # must flatten membw_read_write if ResourceType.MEMBW_READ in a.keys() and membw_read_write_ratio is not None: a = flat_membw_read_write(a, membw_read_write_ratio) b = flat_membw_read_write(b, membw_read_write_ratio) dimensions = set(a.keys()) c = {} for dimension in dimensions: c[dimension] = float(a[dimension]) / float(b[dimension]) return c
def calculate_read_write_ratio(capacity: Resources) -> Optional[float]: dimensions = capacity.keys() if ResourceType.MEMBW_READ in dimensions: assert ResourceType.MEMBW_WRITE in dimensions return float(capacity[ResourceType.MEMBW_READ]) / float(capacity[ResourceType.MEMBW_WRITE]) else: return None
def flat_membw_read_write(a: Resources, membw_read_write_ratio: Optional[float]) -> Resources: """Return resources with replaced memorybandwidth writes and reads """ """with counted flat value.""" dimensions = a.keys() b = a.copy() if ResourceType.MEMBW_READ in dimensions: assert ResourceType.MEMBW_WRITE in dimensions assert type(membw_read_write_ratio) == float del b[ResourceType.MEMBW_READ] del b[ResourceType.MEMBW_WRITE] b[ResourceType.MEMBW_FLAT] = \ a[ResourceType.MEMBW_READ] + membw_read_write_ratio * a[ResourceType.MEMBW_WRITE] return b
def subtract_resources(a: Resources, b: Resources, membw_read_write_ratio: Optional[float] = None) -> Resources: _check_keys(a, b) dimensions = set(a.keys()) c = a.copy() for dimension in dimensions: if dimension not in ( ResourceType.MEMBW_READ, ResourceType.MEMBW_WRITE) or membw_read_write_ratio is None: c[dimension] = a[dimension] - b[dimension] if ResourceType.MEMBW_READ in dimensions and membw_read_write_ratio is not None: assert ResourceType.MEMBW_WRITE in dimensions assert type(membw_read_write_ratio) == float read, write = ResourceType.MEMBW_READ, ResourceType.MEMBW_WRITE c[read] = a[read] - (b[read] + b[write] * membw_read_write_ratio) c[write] = a[write] - (b[write] + b[read] / membw_read_write_ratio) return c
def sum_resources(a: Resources, b: Resources) -> Resources: _check_keys(a, b) c = {} for resource in a.keys(): c[resource] = a[resource] + b[resource] return c
def _check_keys(a, b: Resources): if not set(a.keys()) == set(b.keys()): raise ValueError( 'the same dimensions must be provided for both resources %r vs %r', a.keys(), b.keys())