Discussion of a Parallel Bubble Sort
C1 C2 C3 C4 C5 C6 C7 C8
0 1 2 3 4 5 6 7 8
-------------------------------------
| | | | | | | | | |
| 8 | 9 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
| | | | | | | | | |
-------------------------------------
^
Scenario:
Child threads C1 through C8 are poised in the "locations"
shown. From time to time each Ci locks cells i-1 and i. Then
if the numbers in the cells are out of order, Ci swaps them.
Next Ci unlocks the cells.
The mother thread, M, decides to check to see if the list is
sorted yet. She notes that the contents of cell 0 is less than
the contents of cell 1.
Next C2 locks cells 1 and 2, swaps them, and unlocks them. Now
the array looks like this:
C1 C2 C3 C4 C5 C6 C7 C8
0 1 2 3 4 5 6 7 8
-------------------------------------
| | | | | | | | | |
| 8 | 7 | 9 | 6 | 5 | 4 | 3 | 2 | 1 |
| | | | | | | | | |
-------------------------------------
^
M, continuing now in her effort to check the array to see if it
is sorted, notes that the contents of cell 1 is less than the
contents of cell 2.
Next C3 locks cells 2 and 3, swaps them, and unlocks them. Now
the array looks like this:
C1 C2 C3 C4 C5 C6 C7 C8
0 1 2 3 4 5 6 7 8
-------------------------------------
| | | | | | | | | |
| 8 | 7 | 6 | 9 | 5 | 4 | 3 | 2 | 1 |
| | | | | | | | | |
-------------------------------------
^
M, continuing now in her effort to check the array to see if it
is sorted, notes that the contents of cell 2 is less than the
contents of cell 3.
Next C4 locks cells 3 and 4, swaps them, and unlocks them. Now
the array looks like this:
C1 C2 C3 C4 C5 C6 C7 C8
0 1 2 3 4 5 6 7 8
-------------------------------------
| | | | | | | | | |
| 8 | 7 | 6 | 5 | 9 | 4 | 3 | 2 | 1 |
| | | | | | | | | |
-------------------------------------
^
M, continuing now in her effort to check the array to see if it
is sorted, notes that the contents of cell 3 is less than the
contents of cell 4.
If things keep going as indicated, then after checking cells 7
and 8 the mother will conclude that the list is sorted, since
each pair of consecutive cells had values in the correct order.
However, at that time, the array will actually look like this:
C1 C2 C3 C4 C5 C6 C7 C8
0 1 2 3 4 5 6 7 8
-------------------------------------
| | | | | | | | | |
| 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 9 |
| | | | | | | | | |
-------------------------------------
^
Of course this is not a sorted array.
CONCLUSION: This shows that the "standard algorithm" for
determining if a list is sorted is not reliable in the current
situation. We have to think of another way for the program to
do the checking.
NOTE: This result is interesting for another reason: since the
mother is merely reading the array cells, and not writing to
any of them, it's easy to imagine that nothing can go wrong.
However in this case just the fact that other threads can write
while she reads is enough to cause errors.
One possible alternative is if the mother locks all the cells
before doing her check. This "freezes" the list. The mother
will be able to tell if this "snapshot" of the list is sorted.
If the children have been programmed so they never swap cells
that are in order, then this kind of checking done by the
mother is reliable. Of course when the mother locks all the
cells, the mother is preventing all the children from getting
any work done. Also, if mother and children compete to lock
cells, there is a possibility of deadlock unless they carry out
some form of deadlock prevention or avoidance.
It would be nice if we could figure out a way to allow the mother
to check the list without locking all the cells at once. That
way some or even most of the children could work concurrently
with the mother while she checks the list. There
is a way to do this.
There is also a fully-distributed solution -- one
that is based just on the children relaying information among
themselves.