2019-06-07 15:38:43 +00:00
|
|
|
"""
|
|
|
|
This is an implementation of odd-even transposition sort.
|
|
|
|
|
|
|
|
It works by performing a series of parallel swaps between odd and even pairs of
|
|
|
|
variables in the list.
|
|
|
|
|
|
|
|
This implementation represents each variable in the list with a process and
|
|
|
|
each process communicates with its neighboring processes in the list to perform
|
|
|
|
comparisons.
|
|
|
|
They are synchronized with locks and message passing but other forms of
|
|
|
|
synchronization could be used.
|
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2019-12-08 22:43:56 +00:00
|
|
|
from multiprocessing import Lock, Pipe, Process
|
2019-06-07 15:38:43 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# lock used to ensure that two processes do not access a pipe at the same time
|
Tests for odd_even_transposition_parallel (#10926)
* [ADD] tests for odd_even_transposition_parallel
* adding another test because build failed 6 hrs
* comment out all tests to see if it fails
* list(range(10)[::-1]) test uncommented
* [a, x, c] test uncommented
* [1.9, 42.0, 2.8] test uncommented
* [False, True, False] test uncommented
* [1, 32.0, 9] test uncommented
* [1, 32.0, 9] test uncommented
* [-442, -98, -554, 266, -491, 985, -53, -529, 82, -429] test uncommented
* test non global lock
* [DEL] Testing multiple data types. Couldn't get doctest to work
* [ADD] Comment on why non global process lock
2023-10-27 20:13:32 +00:00
|
|
|
# NOTE This breaks testing on build runner. May work better locally
|
|
|
|
# process_lock = Lock()
|
2019-06-07 15:38:43 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
The function run by the processes that sorts the list
|
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
position = the position in the list the process represents, used to know which
|
2019-06-07 15:38:43 +00:00
|
|
|
neighbor we pass our value to
|
|
|
|
value = the initial value at list[position]
|
|
|
|
LSend, RSend = the pipes we use to send to our left and right neighbors
|
|
|
|
LRcv, RRcv = the pipes we use to receive from our left and right neighbors
|
|
|
|
resultPipe = the pipe used to send results back to main
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def oe_process(position, value, l_send, r_send, lr_cv, rr_cv, result_pipe):
|
Tests for odd_even_transposition_parallel (#10926)
* [ADD] tests for odd_even_transposition_parallel
* adding another test because build failed 6 hrs
* comment out all tests to see if it fails
* list(range(10)[::-1]) test uncommented
* [a, x, c] test uncommented
* [1.9, 42.0, 2.8] test uncommented
* [False, True, False] test uncommented
* [1, 32.0, 9] test uncommented
* [1, 32.0, 9] test uncommented
* [-442, -98, -554, 266, -491, 985, -53, -529, 82, -429] test uncommented
* test non global lock
* [DEL] Testing multiple data types. Couldn't get doctest to work
* [ADD] Comment on why non global process lock
2023-10-27 20:13:32 +00:00
|
|
|
process_lock = Lock()
|
2019-06-07 15:38:43 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# we perform n swaps since after n swaps we know we are sorted
|
|
|
|
# we *could* stop early if we are sorted already, but it takes as long to
|
|
|
|
# find out we are sorted as it does to sort the list with this algorithm
|
2023-08-29 13:18:10 +00:00
|
|
|
for i in range(10):
|
2022-10-12 22:54:20 +00:00
|
|
|
if (i + position) % 2 == 0 and r_send is not None:
|
2019-10-05 05:14:13 +00:00
|
|
|
# send your value to your right neighbor
|
2022-10-12 22:54:20 +00:00
|
|
|
process_lock.acquire()
|
|
|
|
r_send[1].send(value)
|
|
|
|
process_lock.release()
|
2019-06-07 15:38:43 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# receive your right neighbor's value
|
2022-10-12 22:54:20 +00:00
|
|
|
process_lock.acquire()
|
|
|
|
temp = rr_cv[0].recv()
|
|
|
|
process_lock.release()
|
2019-06-07 15:38:43 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# take the lower value since you are on the left
|
2019-06-07 15:38:43 +00:00
|
|
|
value = min(value, temp)
|
2022-10-12 22:54:20 +00:00
|
|
|
elif (i + position) % 2 != 0 and l_send is not None:
|
2019-10-05 05:14:13 +00:00
|
|
|
# send your value to your left neighbor
|
2022-10-12 22:54:20 +00:00
|
|
|
process_lock.acquire()
|
|
|
|
l_send[1].send(value)
|
|
|
|
process_lock.release()
|
2019-06-07 15:38:43 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# receive your left neighbor's value
|
2022-10-12 22:54:20 +00:00
|
|
|
process_lock.acquire()
|
|
|
|
temp = lr_cv[0].recv()
|
|
|
|
process_lock.release()
|
2019-06-07 15:38:43 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# take the higher value since you are on the right
|
2019-06-07 15:38:43 +00:00
|
|
|
value = max(value, temp)
|
2019-10-05 05:14:13 +00:00
|
|
|
# after all swaps are performed, send the values back to main
|
2022-10-12 22:54:20 +00:00
|
|
|
result_pipe[1].send(value)
|
2019-06-07 15:38:43 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-06-07 15:38:43 +00:00
|
|
|
"""
|
|
|
|
the function which creates the processes that perform the parallel swaps
|
|
|
|
|
|
|
|
arr = the list to be sorted
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def odd_even_transposition(arr):
|
Tests for odd_even_transposition_parallel (#10926)
* [ADD] tests for odd_even_transposition_parallel
* adding another test because build failed 6 hrs
* comment out all tests to see if it fails
* list(range(10)[::-1]) test uncommented
* [a, x, c] test uncommented
* [1.9, 42.0, 2.8] test uncommented
* [False, True, False] test uncommented
* [1, 32.0, 9] test uncommented
* [1, 32.0, 9] test uncommented
* [-442, -98, -554, 266, -491, 985, -53, -529, 82, -429] test uncommented
* test non global lock
* [DEL] Testing multiple data types. Couldn't get doctest to work
* [ADD] Comment on why non global process lock
2023-10-27 20:13:32 +00:00
|
|
|
"""
|
|
|
|
>>> odd_even_transposition(list(range(10)[::-1])) == sorted(list(range(10)[::-1]))
|
|
|
|
True
|
|
|
|
>>> odd_even_transposition(["a", "x", "c"]) == sorted(["x", "a", "c"])
|
|
|
|
True
|
|
|
|
>>> odd_even_transposition([1.9, 42.0, 2.8]) == sorted([1.9, 42.0, 2.8])
|
|
|
|
True
|
|
|
|
>>> odd_even_transposition([False, True, False]) == sorted([False, False, True])
|
|
|
|
True
|
|
|
|
>>> odd_even_transposition([1, 32.0, 9]) == sorted([False, False, True])
|
|
|
|
False
|
|
|
|
>>> odd_even_transposition([1, 32.0, 9]) == sorted([1.0, 32, 9.0])
|
|
|
|
True
|
|
|
|
>>> unsorted_list = [-442, -98, -554, 266, -491, 985, -53, -529, 82, -429]
|
|
|
|
>>> odd_even_transposition(unsorted_list) == sorted(unsorted_list)
|
|
|
|
True
|
|
|
|
>>> unsorted_list = [-442, -98, -554, 266, -491, 985, -53, -529, 82, -429]
|
|
|
|
>>> odd_even_transposition(unsorted_list) == sorted(unsorted_list + [1])
|
|
|
|
False
|
|
|
|
"""
|
2022-10-12 22:54:20 +00:00
|
|
|
process_array_ = []
|
|
|
|
result_pipe = []
|
2019-10-05 05:14:13 +00:00
|
|
|
# initialize the list of pipes where the values will be retrieved
|
2019-07-05 08:34:46 +00:00
|
|
|
for _ in arr:
|
2022-10-12 22:54:20 +00:00
|
|
|
result_pipe.append(Pipe())
|
2019-10-05 05:14:13 +00:00
|
|
|
# creates the processes
|
|
|
|
# the first and last process only have one neighbor so they are made outside
|
|
|
|
# of the loop
|
2022-10-12 22:54:20 +00:00
|
|
|
temp_rs = Pipe()
|
|
|
|
temp_rr = Pipe()
|
|
|
|
process_array_.append(
|
2019-10-05 05:14:13 +00:00
|
|
|
Process(
|
2022-10-12 22:54:20 +00:00
|
|
|
target=oe_process,
|
|
|
|
args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]),
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
|
|
|
)
|
2022-10-12 22:54:20 +00:00
|
|
|
temp_lr = temp_rs
|
|
|
|
temp_ls = temp_rr
|
2019-06-07 15:38:43 +00:00
|
|
|
|
|
|
|
for i in range(1, len(arr) - 1):
|
2022-10-12 22:54:20 +00:00
|
|
|
temp_rs = Pipe()
|
|
|
|
temp_rr = Pipe()
|
|
|
|
process_array_.append(
|
2019-10-05 05:14:13 +00:00
|
|
|
Process(
|
2022-10-12 22:54:20 +00:00
|
|
|
target=oe_process,
|
|
|
|
args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]),
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
|
|
|
)
|
2022-10-12 22:54:20 +00:00
|
|
|
temp_lr = temp_rs
|
|
|
|
temp_ls = temp_rr
|
2019-06-07 15:38:43 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
process_array_.append(
|
2019-10-05 05:14:13 +00:00
|
|
|
Process(
|
2022-10-12 22:54:20 +00:00
|
|
|
target=oe_process,
|
2019-10-05 05:14:13 +00:00
|
|
|
args=(
|
|
|
|
len(arr) - 1,
|
|
|
|
arr[len(arr) - 1],
|
2022-10-12 22:54:20 +00:00
|
|
|
temp_ls,
|
2019-10-05 05:14:13 +00:00
|
|
|
None,
|
2022-10-12 22:54:20 +00:00
|
|
|
temp_lr,
|
2019-10-05 05:14:13 +00:00
|
|
|
None,
|
2022-10-12 22:54:20 +00:00
|
|
|
result_pipe[len(arr) - 1],
|
2019-10-05 05:14:13 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# start the processes
|
2022-10-12 22:54:20 +00:00
|
|
|
for p in process_array_:
|
2019-06-07 15:38:43 +00:00
|
|
|
p.start()
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# wait for the processes to end and write their values to the list
|
2023-08-29 13:18:10 +00:00
|
|
|
for p in range(len(result_pipe)):
|
2022-10-12 22:54:20 +00:00
|
|
|
arr[p] = result_pipe[p][0].recv()
|
|
|
|
process_array_[p].join()
|
2019-10-05 05:14:13 +00:00
|
|
|
return arr
|
2019-06-07 15:38:43 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# creates a reverse sorted list and sorts it
|
2019-06-07 15:38:43 +00:00
|
|
|
def main():
|
2019-12-08 22:43:56 +00:00
|
|
|
arr = list(range(10, 0, -1))
|
2019-06-07 15:38:43 +00:00
|
|
|
print("Initial List")
|
|
|
|
print(*arr)
|
2022-10-12 22:54:20 +00:00
|
|
|
arr = odd_even_transposition(arr)
|
2019-06-07 15:38:43 +00:00
|
|
|
print("Sorted List\n")
|
|
|
|
print(*arr)
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-06-07 15:38:43 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|