Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
1.1 kB
2
Indexable
Never
int readA(int **A); // assume reading A and initilize it , and return size of array
int main(int argc, char *argv[]) {
 int rank, p,*A,*B,n;
 
 MPI_Init(&argc, &argv);
 MPI_Comm_size(MPI_COMM_WORLD, &p);
 MPI_Comm_rank(MPI_COMM_WORLD, &rank);

 if (rank == 0) {
 int N = readA(&A);

    n=N;
    B = (int *) Malloc(sizeof(int)*(n));
    }


 MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
 
 if (rank != 0) {
 A = (int *) Malloc(sizeof(int)*n);
 B = (int *) Malloc(sizeof(int)*(n));
 }

 MPI_Bcast(A, n, MPI_INT, 0, MPI_COMM_WORLD);
if(rank == 0){

 int Max = -10000000000;
 #pragma omp parallel for reduction(max:Max)
 for(int i = 0; i < n; i++) {
    Max = B[i] = f(A[i]);
 }
}
if(rank == 1){
int Min = 10000000000;
 #pragma omp parallel for reduction(min:Min)
 for(int i = 0; i < n; i++) {
    Min = B[i] = f(A[i]);
 }
MPI_Send(Min, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
 if (my_rank == 0)
 {
    int min=0;
    MPI_Recv(min, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("difference between max and min is %d\n", Max-min);
 }

 MPI_Finalize();
 return 0;
}
Leave a Comment