Discussion of a Parallel Bubble Sort



Problem Scenario I:

   0     1     2
-------------------
|     |     |     |
|  7  |  6  |  5  |
|     |     |     |     
-------------------
        P1    P2

 temp1  
------- 
|     | 
|  6  | P1's saved copy of contents of slot #1.
|     | 
------- 
The array now looks like this:

   0     1     2
-------------------
|     |     |     |
|  7  |  5  |  6  |
|     |     |     |     
-------------------
        P1    P2
Next P1 finishes it's swap of slot #0 and slot #1: Now the array looks like this:
	
   0     1     2
-------------------
|     |     |     |
|  6  |  7  |  6  |
|     |     |     |     
-------------------
        P1    P2
We see that two threads each tried to swap a pair of items in the array. The result was that one of the items in the array disappeared and one item was duplicated.

This shows that the results of a concurrent bubble sort will not necessarily be correct unless we synchronize the threads somehow.

However, as explained in the assignment, it is supposed to be a parallel bubble sort algorithm that can be executed by a large number of threads on a large array. So we need to look for a way of synchronizing that allows the threads to execute in parallel (simultaneously) as much as possible.




Problem Scenario II:

     C0   C1  C2  C3  C4  C5  C6 C7
  0   1   2   3   4   5   6   7   8
-------------------------------------
|   |   |   |   |   |   |   |   |   |
| 8 | 9 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
|   |   |   |   |   |   |   |   |   |
-------------------------------------
      ^
Child threads C0 through C7 are poised in the "locations" shown. From time to time each Ci locks cells i and i+1. 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 C1 locks cells 1 and 2, swaps them, and unlocks them. Now the array looks like this:

     C0   C1  C2  C3  C4  C5  C6 C7
  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 C2 locks cells 2 and 3, swaps them, and unlocks them. Now the array looks like this:

     C0   C1  C2  C3  C4  C5  C6 C7
  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 C3 locks cells 3 and 4, swaps them, and unlocks them. Now the array looks like this:

     C0   C1  C2  C3  C4  C5  C6 C7
  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:

     C0   C1  C2  C3  C4  C5  C6 C7
  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 thread is merely reading the array cells, and not writing to any of them, one might imagine that nothing can go wrong. However in this case just the fact that other threads can write while the mother thread 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 - once the array is in order and the mother has verified that, the child threads will not do anything to put the array out of order again.

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.

So as to keep out of the way of the children trying to sort the list, You can program the mother to pause a fairly long time before beginning her next check to see if the list is sorted.

Nonetheless, 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, even while she checks the list. It's possible to do this, but students should not assume the first idea they get of how to do it will be correct.

There is also a fully-distributed solution -- one that is based just on the children relaying information among themselves.