Write a parallel program with MPI functions to do the following:

profilekpecet
project_a.docx

Project 8

Due Sunday 4/5/2015 by 11:59 PM

No delay is accepted

Do not delete anything from this file. Just write your answer under the word: Answer.

Write a parallel program with MPI functions to do the following:

1) Define an array of size 1000.

2) Generate a random integer in the range of: 0 t0 999 for: n .

3) Generate n random integers in the range of: 0 to 1000 and save them in array.

4) For m (its value comes from the input) processes divide that array to (m-1) segments. All the segments except the last one must be equal (Note that the last segment may get bigger than other segments)

5) Each of process p1, p2, p3,…, pm-1 obtains the biggest number from its segment and send it to the process with rank = 0.

6) Process 0 receives a number from each process: p1, p2, p3,…, pm-1, finds the biggest number and print it.

Note: If m -1 is bigger n terminate the execution of the program with an appropriate message the shows the value of m-1 and n .

The following is a serial program. It may help.

//File name: a.c

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

#define atmost 1000

int find(int* a, int from, int to){

int i, b;

printf("%d----%d\n", from,to);

b = a[from];

for(i = from + 1; i <= to; i++)

if(a[i] > b)

b = a[i];

return b;

}

int main(){

int i, n, a[atmost], comm_sz, biggest, h, b;

printf("How many segments? ");

scanf("%d", &comm_sz);

srand((unsigned)time(NULL));

n = rand()%atmost;

printf("n = %d\n", n);

for(i = 0; i < n; i++){

a[i] = rand() % 10001;

printf("a[%d] = %d, ", i, a[i]);

}

printf("\n");

h = n/(comm_sz-1);

biggest = -1;

printf("h = %d\n", h);

for(i = 0; i < comm_sz - 1; i++){

if(i == comm_sz - 2)

b = find(a, i * h, n-1);

else

b = find(a, i * h, (i+1) * h - 1);

if(b > biggest)

biggest = b;

}

printf("The biggest is: %d\n", biggest);

return 0;

}

Note: Since I need to test your project use static array (i.e. Do not use the built-in function: malloc). Name your array: a. Only use one array. Do not sort.

Answer