-
Notifications
You must be signed in to change notification settings - Fork 0
/
odd_even_transposition_parallel.py
94 lines (77 loc) · 2.23 KB
/
odd_even_transposition_parallel.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
from multiprocessing import Lock, Pipe, Process
process_lock = Lock()
def oe_process(position, value, l_send, r_send, lr_cv, rr_cv, result_pipe):
global process_lock
for i in range(10):
if (i + position) % 2 == 0 and r_send is not None:
process_lock.acquire()
r_send[1].send(value)
process_lock.release()
process_lock.acquire()
temp = rr_cv[0].recv()
process_lock.release()
value = min(value, temp)
elif (i + position) % 2 != 0 and l_send is not None:
process_lock.acquire()
l_send[1].send(value)
process_lock.release()
process_lock.acquire()
temp = lr_cv[0].recv()
process_lock.release()
value = max(value, temp)
result_pipe[1].send(value)
def odd_even_transposition(arr):
process_array_ = []
result_pipe = []
for _ in arr:
result_pipe.append(Pipe())
temp_rs = Pipe()
temp_rr = Pipe()
process_array_.append(
Process(
target=oe_process,
args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]),
)
)
temp_lr = temp_rs
temp_ls = temp_rr
for i in range(1, len(arr) - 1):
temp_rs = Pipe()
temp_rr = Pipe()
process_array_.append(
Process(
target=oe_process,
args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]),
)
)
temp_lr = temp_rs
temp_ls = temp_rr
process_array_.append(
Process(
target=oe_process,
args=(
len(arr) - 1,
arr[len(arr) - 1],
temp_ls,
None,
temp_lr,
None,
result_pipe[len(arr) - 1],
),
)
)
for p in process_array_:
p.start()
for p in range(len(result_pipe)):
arr[p] = result_pipe[p][0].recv()
process_array_[p].join()
return arr
def main():
arr = list(range(10, 0, -1))
print("Initial List")
print(*arr)
arr = odd_even_transposition(arr)
print("Sorted List\n")
print(*arr)
if __name__ == "__main__":
main()