1 year ago

#219290

test-img

Encipher

MPI_Barrier throws error while run the code

I have tried to find out process running time using MPI_Barrier() function.

My code:

#include <stdio.h>
#include <math.h>
#include<mpi.h>
double sum(int n);

int main(void){

     int my_rank,comm_sz, n=13;
     double local_sum, total_sum;
     int source;
     int local_n;
     MPI_Comm comm;
     double start, finish, loc_elapsed, elapsed;

     MPI_Init(NULL,NULL);
     MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
     MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);


     local_n= n/comm_sz;

    MPI_Barrier(comm);
    start = MPI_Wtime();

    local_sum=sum(local_n);

   finish = MPI_Wtime();
   loc_elapsed = finish - start;

   MPI_Reduce(&loc_elapsed, &elapsed, 1, MPI_DOUBLE, MPI_MAX, 0, comm );



  if ( my_rank != 0) {
       MPI_Send (&local_sum , 1, MPI_DOUBLE , 0, 0, MPI_COMM_WORLD ) ;
   }

   else{
          total_sum = local_sum;
          for(source=1;source<comm_sz;source++){
          MPI_Recv (&local_sum , 1, MPI_DOUBLE , source , 0, MPI_COMM_WORLD , MPI_STATUS_IGNORE ) ;
         total_sum+=local_sum;
     }
   }
  if(my_rank==0){
       printf("Sum is=%lf",total_sum);
       printf("\n");
       printf("Elapsed time = %e\n", elapsed);
  }
  MPI_Finalize();
  return 0;

 }

 double sum(int n){
 int i;
 double cal_sum=0;
 for (i =0;i <= n;i++) {
         cal_sum = cal_sum + 4*(pow (-1, i))/((2*i)+1);
 }
 return cal_sum;
  }

I got this error while run the process

$mpirun -c 1 ./mpi_parallel_time
[node050:136537] *** An error occurred in MPI_Barrier
[node050:136537] *** reported by process [2728525825,0]
[node050:136537] *** on communicator MPI_COMM_WORLD
[node050:136537] *** MPI_ERR_COMM: invalid communicator
[node050:136537] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[node050:136537] ***    and potentially your MPI job)

Why I got this error? Am I missing any header file?

Thank you.

c

mpi

barrier

0 Answers

Your Answer

Accepted video resources