Test 3

CPSC 362
Test #3
Name: _____________________________
April 26, 2012
Closed books, closed notes, 75 minutes, 110 points, weights in parentheses. Please use a
pencil.
1. (10) Consider Figure 11.2. What causes the drop in megaflop performance as matrix
size increases. Be precise.
2. (20) Consider Cannon’s algorithm. Figure 11.10 depicts communication and
computation patterns for one of the processors. Show the equivalent submatrices of
A and B for the processor in row 3 and column 3 (box in bold). Put your answers in
the space provided below. Your answer should look like Figure 11.10 when complete.
3. (10, 2 points each) Consider Figure 11.7. Then answer the following questions.
a. What communication network would be ideal for this algorithm? Explain briefly.
b. In each iteration what is being passed from processor to processor? Be specific.
c. (True or False) Each processor holds the entire matrix A. If false, explain why.
d. (True or False) The values of C move from processor to processor, following B. If
true, explain what happens to values of C. If false, explain why.
e. (True or False) At the end, each processor holds the values of B it started with. If
false, explain where the values of B that originate in one processor end up.
4. (20) Consider the following system of equations. Eliminate variable x1 from the 2nd,
3rd, and 4th equations. Show the resulting system of equations.
2x1
4x1
−2x1
4x1
+
x2
+ 5x2
+ 5x2
+ 11x2
− x3 + 2x4
− 3x3 + 6x4
− 2x3 + 6x4
− 4x3 + 8x4
=
=
=
=
5
9
4
2
5. (20) Consider the quicksort code provided on a separate sheet. Then answer the
questions below.
a. (6) If the subarray being considered is bounded by indices (left=5) and (right=15),
is quicksort_r called? Explain.
b. (2) At the end of the function median, in what array location is the smallest of the
three values stored (left, mid, right, other: specify)
c. (2) At the end of median, in what array location is the pivot element stored (left,
mid, right, other: specify)
d. After quicksort_r completes (line 138), the almost sorted array is divided into
segments (see below):
A
B
C
D
E
F
|_____|___|________| … |_____|_________|_____|
(5) (True or False) If the array is being sorted in increasing order, all elements in
segment A are less than elements of B which are all less than elements of C, etc.
If false, explain where different values are, or are the locations of values
unpredictable?
(5) ______ What is the maximum length of any of the segments?
6. (20) Consider Figure 14.3 depicting hyperquicksort on a two-dimensional hypercube.
Then answer the questions below.
a. (2) ______ In a 128-processor hypercube, how many broadcast steps occur?
b. (4) _______________ In the 4th broadcast step of the algorithm, assuming a 512processor hypercube, which processors broadcast their median values (list their
processor ids)?
c. (2) ______ In the 6th iteration of the algorithm on a 256-processor hypercube,
with which processor does processor 3 swap values?
d. (2) (True or False) At the end of this algorithm, the values in processor 0 will be
less than or equal to the values in processor 1 which will be less than or equal to
the values in processor 2, etc
e. (10) Given a 16-processor hypercube, explain fully and precisely what happens
during the third iteration. Which processors preform a broadcast? Which
processors receive the values (specify source processor id and destination
processor ids)? Which processors swap and merge?
7. Bonus (10) Quinn describes two iterative methods for solving sparse linear systems of
equations. (a) Name and clearly define the first iterative method. (b) Name and
clearly define the second iterative methods. (c) Clearly explain the difference between
the two methods. Which method converges faster? Put your answer on the back of
this sheet.
QuickSort
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. #define CUTOFF 10
5. #define length(x) ( sizeof x / sizeof *x )
6.
7. struct pivots {
8. int left, right;
9. };
10.
11.
void swap ( int *a, int *b );
12.
void insertion_sort ( int list[], int left, int right );
13.
int median ( int list[], int left, int right );
14.
struct pivots partition ( int list[], int left, int right );
15.
void quicksort_r ( int list[], int left, int right );
16.
void quicksort ( int list[], int left, int right );
17.
18.
int main ( void )
19.
{
20.
int list[1000];
21.
int i;
22.
23.
for ( i = 0; i < length ( list ); i++ )
24.
list[i] = (int)( (double)rand() / RAND_MAX * 100 );
25.
26.
for ( i = 0; i < length ( list ); i++ )
27.
printf ( "%-4d", list[i] );
28.
printf ( "\n\n" );
29.
30.
quicksort ( list, 0, length ( list ) );
31.
32.
for ( i = 0; i < length ( list ); i++ )
33.
printf ( "%-4d", list[i] );
34.
printf ( "\n" );
35.
36.
return 0;
37.
}
38.
39.
void swap ( int *a, int *b )
40.
{
41.
int save = *a;
42.
*a = *b;
43.
*b = save;
44.
}
45.
46.
void insertion_sort ( int list[], int left, int right )
47.
{
48.
int i, j;
49.
50.
for ( i = left + 1; i < right; i++ ) {
51.
int save = list[i];
52.
53.
for ( j = i; j > 0 && list[j - 1] > save; j-- )
54.
list[j] = list[j - 1];
55.
56.
list[j] = save;
57.
}
58.
}
59.
60.
int median ( int list[], int left, int right )
61.
{
62.
/* Find the median of three values in list, use it as the pivot */
63.
int mid = ( left + right ) / 2;
64.
65.
if ( list[left] > list[mid] )
66.
swap ( &list[left], &list[mid] );
67.
68.
if ( list[left] > list[right] )
69.
swap ( &list[left], &list[right] );
70.
71.
if ( list[mid] > list[right] )
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
swap ( &list[mid], &list[right] );
swap ( &list[mid], &list[right - 1] );
return list[right - 1];
}
struct pivots partition ( int list[], int left, int right )
{
int k;
int i = left, j = right - 1;
int m = left, n = right - 1;
int pivot = median ( list, left, right );
struct pivots ret;
/* Three way partition <,==,> */
for ( ; ; ) {
while ( list[++i] < pivot )
;
while ( list[--j] > pivot ) {
if ( j == left )
break;
}
if ( i >= j )
break;
swap ( &list[i], &list[j] );
if ( list[i] == pivot )
swap ( &list[++m], &list[i] );
if ( list[j] == pivot )
swap ( &list[--n], &list[j] );
}
swap ( &list[i], &list[right - 1] );
j = i - 1;
i = i + 1;
for ( k = left; k < m; k++, j-- )
swap ( &list[k], &list[j] );
for ( k = right - 1; k > n; k--, i++ )
swap ( &list[k], &list[i] );
ret.left = i;
ret.right = j;
return ret;
}
void quicksort_r ( int list[], int left, int right )
{
/* Terminate on small subfiles */
if ( left + CUTOFF <= right ) {
struct pivots pivot = partition ( list, left, right );
quicksort_r ( list, left, pivot.right );
quicksort_r ( list, pivot.left, right );
}
}
void quicksort ( int list[], int left, int right )
{
quicksort_r ( list, left, right - 1 );
/* Insertion sort on almost sorted list */
insertion_sort ( list, left, right );
}