SLIDE 27 Parallel algorithms based on the naive refinement III
Algorithm 3: GcdOfAllPairsInner(A, B, G)
Input: 1-D arrays of positive integers A = l1, l2, . . . , lk, B = m1, m2, . . . , mr and a 2-D array G = [Gi,j | 1 ≤ i ≤ k, 1 ≤ j ≤ r]. Output: All possible pairs of gcd(li, mj) for 1 ≤ i ≤ k, 1 ≤ j ≤ r with gcd(li, mj) stored in Gi,j. comment C is a global variable equal to a base-case threshold, say 16. Assume C ≥ 2. if k ≤ C and r ≤ C then
1:
for (i, j) ∈ {1, . . . , k} × {1, . . . , r} do
2:
Gi,j ← gcd(Ai, Bj); [Place of INEFFICIENCY for cache complexity]
3:
else if k > C and r ≤ C then
4:
Divide A into two halves A1 = l1, . . . , lk/2, A2 = lk/2+1, . . . , lk ;
5:
GcdOfAllPairsInner(A1, B, G);
6:
GcdOfAllPairsInner(A2, B, G);
7:
else if k ≤ C and r > C then
8:
Divide B into two halves B1 = m1, . . . , mr/2, B2 = mr/2+1, . . . , mr ;
9:
GcdOfAllPairsInner(A, B1, G);
10:
GcdOfAllPairsInner(A, B2, G);
11:
else
12:
Divide A and B arrays into two halves
13:
A1 = l1, . . . , lk/2, A2 = lk/2+1, . . . , lk; B1 = m1, . . . , mr/2, B2 = mr/2+1, . . . , mr ; spawn GcdOfAllPairsInner(A1, B1, G);
14:
spawn GcdOfAllPairsInner(A2, B2, G);
15:
spawn GcdOfAllPairsInner(A1, B2, G);
16:
spawn GcdOfAllPairsInner(A2, B1, G);
17:
27 / 65