MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / oe_process

Function oe_process

sorts/odd_even_transposition_parallel.py:32–71  ·  view source on GitHub ↗
(
    position,
    value,
    l_send,
    r_send,
    lr_cv,
    rr_cv,
    result_pipe,
    multiprocessing_context,
)

Source from the content-addressed store, hash-verified

30
31
32def oe_process(
33 position,
34 value,
35 l_send,
36 r_send,
37 lr_cv,
38 rr_cv,
39 result_pipe,
40 multiprocessing_context,
41):
42 process_lock = multiprocessing_context.Lock()
43
44 # we perform n swaps since after n swaps we know we are sorted
45 # we *could* stop early if we are sorted already, but it takes as long to
46 # find out we are sorted as it does to sort the list with this algorithm
47 for i in range(10):
48 if (i + position) % 2 == 0 and r_send is not None:
49 # send your value to your right neighbor
50 with process_lock:
51 r_send[1].send(value)
52
53 # receive your right neighbor's value
54 with process_lock:
55 temp = rr_cv[0].recv()
56
57 # take the lower value since you are on the left
58 value = min(value, temp)
59 elif (i + position) % 2 != 0 and l_send is not None:
60 # send your value to your left neighbor
61 with process_lock:
62 l_send[1].send(value)
63
64 # receive your left neighbor's value
65 with process_lock:
66 temp = lr_cv[0].recv()
67
68 # take the higher value since you are on the right
69 value = max(value, temp)
70 # after all swaps are performed, send the values back to main
71 result_pipe[1].send(value)
72
73
74"""

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected