Untitled
unknown
c_cpp
2 years ago
1.7 kB
9
Indexable
Given array A of N integers and function f(x). Write an effective parallel program that
calculates and displays members of an array B of N integer
B[i] = max (f(A[i]), f(A[N -1 - i])) (*)
Requirements:
1. You have 2 computers with 4 cores each.
2. The array A is initially known to one of process only. No need for initialization of
this array. The same process has to display the array B calculated according to its
definition (*).
3. The function int f(int x) takes significant time to execute and is known, no need to
implement it.
4. You may assume that N / 8 is even number.
#define WORLD MPI_COMM_WORLD
#define IGNOR MPI_STATUS_IGNORE
int main(int argc,char **argv){
MPI_Init(&argc, &argv);
int rank, size, *A,*B,N;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(size != 2){
printf("2 procs only");
MPI_Abort(WORLD,EXIT_FAILURE);
}
if(rank == 0 ){
N= readA(&A);
MPI_SEND(&N,1,MPI_INT,1,0,WORLD);
}
else{
MPI_Recv(&N,1,MPI_INT,0,0,WORLD,IGNOR);
A = (int*)malloc(N*sizeof(int));
}
B = (int*)malloc(N/2*sizeof(int));
int start = rank * N/4;
int end = start + N/4;
# pragma omp parallel for num_threads(4)
for(int i = start;i<end;i++){
B[i]= f(A[i]);
B[N-1-i] = f(A[N-1-i]);
if( B[i]<B[N-1-i])
B[i] = B[N-1-i];
else
B[N-1-i] = B[i];
}
if(rank == 0){
MPI_Recv(B+N/4,N/4,MPI_INT,1,0,WORLD,IGNOR);
# pragma omp parallel for
for(int i = 0;i<N/2;i++){
printf("B[%d] = B[%d] = %d\n",i,N-1-i,B[i]);
}
else {
MPI_Send(B+N/4,N/4,MPI_INT,0,0,WORLD);
}
MPI_Finalize();
return EXIT_SUCCESS ;
Editor is loading...
Leave a Comment