Skip to content
Snippets Groups Projects
Commit b3054538 authored by Jammer, Tim's avatar Jammer, Tim
Browse files

implemented List cases for managing list of Instructions (see !7)

parent 54801c07
No related branches found
No related tags found
1 merge request!9Infrastructure: Type Hints, Instruction class and lists of instructions
...@@ -33,30 +33,36 @@ class InstructionBlock: ...@@ -33,30 +33,36 @@ class InstructionBlock:
assert not isinstance(name, int) assert not isinstance(name, int)
self.name = name self.name = name
# TODO test if op also can be a list of operations
def register_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all'): def register_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all'):
""" """
Registers an operation based on rank. Registers an operation based on rank.
Parameters: Parameters:
- op: The operation to register. - op: The operation (or list of Operations) to register.
- kind: Rank to execute the operation ('all', 'not0', or integer). - kind: Rank to execute the operation ('all', 'not0', or integer).
- all: all Ranks execute this operation - all: all Ranks execute this operation
- not0: all Ranks but the Root (rank 0) execute - not0: all Ranks but the Root (rank 0) execute
- Or the integer of the rank that should execute - Or the integer of the rank that should execute
""" """
if kind == 'all': if kind == 'all':
if isinstance(op, list):
self.operations['all'].extend(op)
else:
self.operations['all'].append(op) self.operations['all'].append(op)
elif kind == 'not0': elif kind == 'not0':
if isinstance(op, list):
self.operations['not0'].extend(op)
else:
self.operations['not0'].append(op) self.operations['not0'].append(op)
else: else:
as_int = int(kind) # will Raise ValueError if not integer as_int = int(kind) # will Raise ValueError if not integer
if as_int not in self.operations: if as_int not in self.operations:
self.operations[as_int] = [] self.operations[as_int] = []
if isinstance(op, list):
self.operations[as_int].extend(op)
else:
self.operations[as_int].append(op) self.operations[as_int].append(op)
# TODO implement list part
def get_version(self) -> str: def get_version(self) -> str:
""" """
Retrieves the minimum required MPI version. Retrieves the minimum required MPI version.
...@@ -111,55 +117,73 @@ class InstructionBlock: ...@@ -111,55 +117,73 @@ class InstructionBlock:
except (KeyError, IndexError) as e: except (KeyError, IndexError) as e:
return False return False
def get_operation(self, kind: int | str = 'all', index: int = 0) -> Instruction | typing.List[Instruction]: def get_operation(self, kind: int | str = 'all', index: str | int = 0) -> Instruction | typing.List[Instruction]:
""" """
Retrieve the operation registered. will Raise IndexError if not present Retrieve the operation registered. will Raise IndexError if not present
Parameters: Parameters:
- kind ('all','not0' or integer): which ranks should execute the operation - kind ('all','not0' or integer): which ranks should execute the operation
- index (int): the index of the operation within the given kind - index ('all' or int): the index of the operation within the given kind; 'all' means that the list of all operations for the kind is returned
Returns: Returns:
str: The operation specified by kind and index str: The operation specified by kind and index
""" """
return self.operations[kind][index] if index == 'all':
return self.operations[kind]
# todo implement list case else:
as_int = int(index) # will Raise ValueError if not integer
return self.operations[kind][as_int]
def replace_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all', index: int = 0): def replace_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all',
index: str | int = 0):
""" """
Replace the operation registered. will Raise IndexError if not present Replace the operation registered. will Raise IndexError if not present
Parameters: Parameters:
- op (str or MPICall) the new operation - op the new operation or list of operations
- kind ('all','not0' or integer): which ranks should execute the operation - kind ('all','not0' or integer): which ranks should execute the operation
- index (int): the index of the operation within the given kind - index ('all' or int): the index of the operation within the given kind; 'all' means all operations will be replaced with the given list
""" Notes : if one wants to replace all operations one needs to provide a list
if len(self.operations[kind]) < index: if one only wants to replace one operation: no list of operations is allowed
"""
if index == 'all':
if not isinstance(op, list):
raise ValueError('Provide List for replacement')
self.operations[kind] = op
else:
as_int = int(index) # will Raise ValueError if not integer
if not isinstance(op, Instruction):
raise ValueError('Provide Instruction')
if len(self.operations[kind]) < as_int:
raise IndexError("Operation Not Found") raise IndexError("Operation Not Found")
self.operations[kind][index] = op self.operations[kind][as_int] = op
# todo implement list caee
def insert_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all', def insert_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all',
before_index: int = 0): before_index: int = 0):
""" """
Inserts an operation before the specified one. will Raise IndexError if not present Inserts an operation before the specified one. will Raise IndexError if not present
Parameters: Parameters:
- op (str or MPICall) the new operation - op the new operation or list of operations
- kind ('all','not0' or integer): which ranks should execute the operation - kind ('all','not0' or integer): which ranks should execute the operation
- index (int): the index of the operation within the given kind - index (int): the index of the operation within the given kind
""" """
as_int = int(before_index) # will Raise ValueError if not integer
if len(self.operations[kind]) < before_index: if len(self.operations[kind]) < before_index:
raise IndexError("Operation Not Found") raise IndexError("Operation Not Found")
if isinstance(op, list):
self.operations[kind] = (
self.operations[kind][0:before_index - 1] + op + self.operations[kind][before_index:])
else:
self.operations[kind].insert(before_index, op) self.operations[kind].insert(before_index, op)
# todo implement list caee def remove_operation(self, kind: str | int = 'all', index: str | int = 0):
def remove_operation(self, kind: str | int = 'all', index: int = 0):
""" """
Removes the operation registered. will Raise IndexError if not present Removes the operation registered. will Raise IndexError if not present
Parameters: Parameters:
- kind ('all','not0' or integer): which ranks should execute the operation - kind ('all','not0' or integer): which ranks should execute the operation
- index (int): the index of the operation within the given kind - index ('all' or int): the index of the operation within the given kind
""" """
if index == 'all':
self.operations[kind] = []
else:
as_int = int(index) # will Raise ValueError if not integer
if len(self.operations[kind]) < index: if len(self.operations[kind]) < index:
raise IndexError("Operation Not Found") raise IndexError("Operation Not Found")
del self.operations[kind][index] del self.operations[kind][index]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment