You are on page 1of 2

Classwork 03: Work Partition

Team 2

1. Write a parallel program that uses a divide-and-conquer approach to find


the first zero in a list of integers stored in an array. Use 16 processes and
256 numbers. Assume distributed memory

def firstZero(arr, low, high):


if (high >= low):

mid = low + int((high - low) / 2)


if (( mid == 0 or arr[mid-1] == 1)
and arr[mid] == 0):
return mid

if (arr[mid] == 1):
return firstZero(arr, (mid + 1), high)

else:
return firstZero(arr, low, (mid - 1))

return -1

def countZeroes(arr, n):


first = firstZero(arr, 0, n - 1)

if (first == -1):
return 0
return (n - first)
2. Write parallel programs to compute the summation of n integers. Assume
that n is a power of 2. Partition the n integers into n/2 pairs. Use n/2
processes to add together each pair of integers resulting in n/2 integers.
Repeat the method on the n/2 integers to obtain n/4 integers and continue
until the final result is obtained. (This is a binary tree algorithm). Assume
shared memory.

// S1 is the original list of integers


// Px is the number of process number, where x is the id
// P0
divide(s1, s1, s2); // Divide s1 into s1 and s2 equally
send(s2, P4);
divide(s1, s1, s2); // Divide s1 further into s1 and s2 ( n/4)
send(s2, P2);
divide(s1, s1, 2); // Divide further (n/8)
send(s2, P1);

path_sum = *s1;
recv(&part_sum1, P1);
part_sum = part_sum + part_sum1;
recv(&part_sum1, P2);
part_sum = part_sum + part_sum1;
recv(&part_sum1, P4);
part_sum = part_sum + part_sum1;

You might also like