MPI point to point communication code error

26 views
Skip to first unread message

Ellena Mori

unread,
Feb 23, 2022, 5:11:38 PMFeb 23
to mpi4py
Hello Everyone,

I am new to this group. I got error when I try to write down a mpi point to point program. Here is my program.

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

int main(void){
  int my_rank,comm_sz, n=1024;
  double local_sum, total_sum;
  int source;
  int local_n;
  MPI_Init(NULL,NULL);
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);


  local_n=comm_sz/n;
  local_sum=sum(local_n);

  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;
    }
  }
  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 the error 

[node007:73071] *** An error occurred in MPI_Recv
[node007:73071] *** reported by process [2587557889,0]
[node007:73071] *** on communicator MPI_COMM_WORLD
[node007:73071] *** MPI_ERR_RANK: invalid rank
[node007:73071] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[node007:73071] ***    and potentially your MPI job)

Can anyone please let me know what logic error there is in my program? I try to run this program for 4 processes.
Reply all
Reply to author
Forward
0 new messages