Hi,
I am new to MPI programming and still trying out very basic codes.
I wanted to make core 0 to receive simple values sent by other workers. But it gives me a MPI error at run time. when executing MPI_Send command.
Here is my code,
#include <stdio.h>
#include <mpi.h>
#include<iostream>
using namespace std;
int main(int argc, char *argv[]) {
int numprocs, rank, namelen, ierr;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv); // What ever I write in between will be parallelize.,....................
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Current Worker index
MPI_Get_processor_name(processor_name, &namelen);
// Send rank to process 0
double val = -1.0*rank;
MPI_Send(&val, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
printf("ProcID %d sent value %f to ProcID %d",rank,val,0);
if(rank == 0){
int i;
double val, sum = 0;
MPI_Status status;
int cp = 0;
for (i = 0 ; i!=numprocs ; ++i){
ierr = MPI_Recv(&val, 1, MPI_DOUBLE, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
if(ierr == MPI_SUCCESS){
printf("ProcID %d Receives value %f from ProcID %d\n",rank,val);
sum += 1;
cp++;
}
else MPI_Abort(MPI_COMM_WORLD,1);
}
printf("the Total is %f\n",sum);
}
MPI_Finalize();//.......................................................................
}
When I change MPI_Send line to,
if(rank != 0) {
double val = -1.0*rank;
MPI_Send(&val, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
printf("ProcID %d sent value %f to ProcID %d",rank,val,0);
}
I don't get any thing at all. It doesn't do anything.
I am running this on my laptop with a core i7 processor.
Any help would be appreciated?