-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoop.py
144 lines (118 loc) · 4.94 KB
/
oop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from mpi4py import MPI
class Node:
def __init__(self, machine_id, parent_id, operation_name, mod):
self.machine_id = machine_id
self.parent_id = parent_id
self.initial_operation = operation_name
self.operations = self.get_operations()
self.mod = mod
self.children_product = {} #concatenate the results of the children products
def get_operations(self):
if self.machine_id % 2 == 0:
return ["enhance", "split", "chop"]
else:
return ["trim", "reverse"]
def send_to_parent(self, result):
if self.parent_id != 1:
self.comm.send((self.parent_id, result, self.machine_id), dest=self.parent_id, tag=self.parent_id)
def receive_from_children(self):
status = MPI.Status()
result_of_child, child_id = self.comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
parent_id = status.Get_tag()
return parent_id, child_id, result_of_child
class MPIProgram:
def __init__(self):
# MPI initialization
self.comm = MPI.COMM_WORLD
self.rank = self.comm.Get_rank()
self.size = self.comm.Get_size()
self.MASTER = 0
self.wear_opname = ["enhance", "reverse", "chop", "trim", "split"]
def read_input_file(self, filename):
with open(filename, 'r') as file:
lines = [line.strip() for line in file.readlines()]
return lines
def calculate_string(self, product, operation, mod):
if operation == "enhance":
product = product[0] + product + product[-1]
elif operation == "reverse":
product = product[::-1]
elif operation == "chop":
if (len(product) > 1):
product = product[:-1]
elif operation == "trim":
if (len(product) > 2):
product = product[1:-1]
elif operation == "split":
length = len(product)
if length % 2 == 0:
split_point = length // 2
else:
split_point = (length + 1) // 2
product = product[:split_point]
else:
print("Invalid operation")
return product
def master_process(self):
num_children = {}
leaf_nodes = []
# Read and process the input file
input_lines = read_input_file("input.txt")
# Extract relevant information from input_lines
num_machines = int(input_lines[0])
num_cycles = int(input_lines[1])
wear_factors = list(map(int, input_lines[2].split()))
maintenance_threshold = int(input_lines[3])
# Process child-parent relationships to identify leaf nodes
child_parent_relationships = [list(map(str, line.split())) for line in input_lines[4:num_machines+3]]
parent_set = set()
# Instead use OOP to store information about each node
node_info = {}
num_children = {i: 0 for i in range(1, num_machines + 1)}
for child, parent, operation_name in child_parent_relationships:
parent=int(parent)
child=int(child)
parent_set.add(parent)
operations=[]
print("this is child",child,"this is parent",parent)
if (child % 2 == 0) :
operations =["enhance","split","chop"]
mod=3
else :
operations = ["trim","reverse"]
mod=2
print(operations)
current_op_index=operations.index(operation_name)
#ADD NODE_INFO THE CURRENT OPERATION INDEX
node_info[child]["current_op_number"]=current_op_index
num_children[parent] += 1 #index i holds the number of children of node i
#create a node object
node_info[child] = Node(child, parent, operation_name, mod)
#find initial opration index in the operations
#node_info[parent]["children_product"]=[]
# Determine leaf nodes (machines without parents)
leaf_nodes = sorted(set(range(2, num_machines + 1)) - parent_set) #2den itibaren numaralandırılmış olduğunu varsayıyorum
print("leaf nodes",leaf_nodes)
# Extract initial product names
num_leaf_machines = len(leaf_nodes)
products = input_lines[num_machines+3:num_machines+3+num_leaf_machines] # Assuming line number is the same as num_leaf_machines
print("products",products)
# Instead make each node send its information to the corresponding worker process
i=1
for leaf_id, product in zip(leaf_nodes, products):
#create initial nodes to send to workers
i+=1
# Receive the final result from the root node (ID 1)
final_result = self.comm.recv(source=1, tag=1)
print("Final Result:", final_result)
def worker_process(self):
# (Same as your worker process code)
pass
def run(self):
if self.rank == self.MASTER:
self.master_process()
else:
self.worker_process()
if __name__ == "__main__":
mpi_program = MPIProgram()
mpi_program.run()