Skip to content
Snippets Groups Projects

P2P

Open
Jammer, Timrequested to merge
p2p into main
3 files
+ 53
35
Compare changes
  • Side-by-side
  • Inline

Files

+ 45
27
@@ -115,14 +115,15 @@ class TemplateManager:
code_string = ""
current_rank = 'all'
for inst in self._instructions:
if inst.get_rank_executing != current_rank:
if inst.get_rank_executing() != current_rank:
if current_rank != 'all':
code_string = code_string + "}\n" # end previous if
current_rank = inst.get_rank_executing()
if current_rank == 'not0':
code_string = code_string + "if (rank!=0){\n"
elif current_rank != 'all':
code_string = code_string + "if (rank==%d){\n" % current_rank
code_string = code_string + "}\n"
# end previous if
current_rank = inst.get_rank_executing()
if current_rank == 'not0':
code_string = code_string + "if (rank!=0){\n"
elif current_rank != 'all':
code_string = code_string + "if (rank==%d){\n" % current_rank
code_string += str(inst) + "\n"
# end for inst
@@ -164,7 +165,7 @@ class TemplateManager:
i.set_identifier(identifier)
if rank_to_execute is not None:
for i in inst:
i.set_ranks_executing(rank_to_execute)
i.set_rank_executing(rank_to_execute)
self._instructions.extend(inst)
elif isinstance(inst, str):
if rank_to_execute is not None:
@@ -173,11 +174,11 @@ class TemplateManager:
# use default ('all')
self._instructions.append(Instruction(inst, identifier=identifier))
else:
assert isinstance(inst, Instruction)
if identifier is not None:
inst.set_identifier(identifier)
if rank_to_execute is not None:
for i in inst:
i.set_ranks_executing(rank_to_execute)
inst.set_rank_executing(rank_to_execute)
self._instructions.append(inst)
def get_version(self) -> str:
@@ -188,7 +189,7 @@ class TemplateManager:
"""
max_v = "0.0"
for inst in self._instructions:
if isinstance(inst,MPICall):
if isinstance(inst, MPICall):
max_v = max(inst.get_version(), max_v)
return max_v
@@ -251,21 +252,25 @@ class TemplateManager:
raise ValueError("Neither Both block name nor index is given")
def __get_instruction_index(self, identifier: str) -> typing.List[int]:
def __get_instruction_index(self, ident: str | Instruction) -> typing.List[int]:
"""
internal helper function to receive the indices of instructions with ghe given identifier
internal helper function to receive the indices of instructions with ghe given identifier oris the given isntruction
"""
return [idx for inst, idx in enumerate(self._instructions) if inst.get_identifier() == identifier]
def insert_instruction(self, new_instruction: Instruction, after_instruction: str | int = None,
before_instruction: str | int = None):
if isinstance(ident, str):
return [idx for idx, inst in enumerate(self._instructions) if inst.get_identifier() == ident]
if isinstance(ident, Instruction):
return [idx for idx, inst in enumerate(self._instructions) if inst == ident]
raise ValueError("Provide string or instruction")
def insert_instruction(self, new_instruction: Instruction, after_instruction: Instruction | str | int = None,
before_instruction: Instruction | str | int = None):
"""
Inserts a new instruction into the template.
Parameters:
new_instruction (Instruction): The new instruction to insert.
after_instruction (str | int): The instruction after which to insert the new one (identifier or index).
before_instruction (str | int): The instruction before which to insert the new one (identifier or index).
after_instruction (Instruction|str | int): The instruction after which to insert the new one (identifier or index).
before_instruction (Instruction|str | int): The instruction before which to insert the new one (identifier or index).
Raises:
ValueError: if both before and after are provided
IndexError: if it finds multiple places to insert by identifier
@@ -280,7 +285,6 @@ class TemplateManager:
if isinstance(after_instruction, int):
idx_to_use = 1 + after_instruction
else:
assert isinstance(after_instruction, str)
inst_idx_list = self.__get_instruction_index(after_instruction)
if len(inst_idx_list) != 1:
raise IndexError("Did not find place to insert")
@@ -289,7 +293,6 @@ class TemplateManager:
if isinstance(before_instruction, int):
idx_to_use = before_instruction
else:
assert isinstance(before_instruction, str)
inst_idx_list = self.__get_instruction_index(before_instruction)
if len(inst_idx_list) != 1:
raise IndexError("Did not find place to insert")
@@ -297,20 +300,29 @@ class TemplateManager:
self._instructions.insert(idx_to_use, new_instruction)
def remove_instruction(self, identifier: str = None, idx: int | typing.List[int] = None):
def remove_instruction(self, identifier: str = None, idx: int | typing.List[int] = None,
instruction: Instruction | typing.List[Instruction] = None):
"""
Removes an instruction from the template.
Parameters:
identifier (str): The identifier of the instruction to remove.
idx (int | List[int]): The index or list of indices of the instruction(s) to remove.
instruction (Instruction | List[Instruction]) the list of instructions to remove
"""
# assert only one param is not None
parameters = [identifier, idx]
if parameters.count(None) != 1:
parameters = [identifier, idx, instruction]
if parameters.count(None) != 2:
raise ValueError("Only one parameter is allowed to be specified")
if instruction is not None:
if isinstance(instruction, Instruction):
instruction = [instruction]
for instruction in instruction:
self._instructions.remove(instruction)
return
idxs_to_remove = []
if idx is not None:
if isinstance(idx, int):
@@ -327,13 +339,15 @@ class TemplateManager:
self._instructions = [elem for idx, elem in enumerate(self._instructions) if idx not in idxs_to_remove]
def replace_instruction(self, new_instruction=Instruction, identifier: str = None,
def replace_instruction(self, new_instruction=Instruction, old_instruction: Instruction | List[Instruction] = None,
identifier: str = None,
idx: int | typing.List[int] = None):
"""
Replaces an instruction in the template with a new one.
Parameters:
new_instruction (Instruction | List[Instruction]): The new instruction(s) to replace with.
old_instruction (Instruction | List[Instruction]): The old instruction(s) to replace.
identifier (str): The identifier of the instruction to replace.
idx (int | List[int]): The index or list of indices of the instruction(s) to replace.
Raises
@@ -341,8 +355,8 @@ class TemplateManager:
or if both idx and identifier are provided
Notes: The instructions to be replaced must not be in contiguous order
"""
parameters = [identifier, idx]
if parameters.count(None) != 1:
parameters = [old_instruction, identifier, idx]
if parameters.count(None) != 2:
raise ValueError("Only one parameter is allowed to be specified")
new_instruction_list = []
@@ -351,6 +365,10 @@ class TemplateManager:
else:
new_instruction_list = new_instruction
if old_instruction is not None:
raise EnvironmentError("NOT IMPLEMENTED")
# TODO implement
idxs_to_replace = []
if idx is not None:
if isinstance(idx, int):
Loading