My program is compiling and showing abnormal behaviour.
Used following commands for compiling
mpicc one.c -o one
mpiexec -n 2 ./one
I tried to debug it but show no compilation error. I can't understand why my program is behaving abnormally
#include<mpi.h>
#include<stdio.h>
#include<string.h>
int main(int argc,char * argv[])
{
char str[434];
int n;
int rank,size; MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank == 0)
{//Sender process
scanf("%s",&str);
MPI_Ssend(&n,1,MPI_INT,1,0,MPI_COMM_WORLD);
MPI_Ssend(&str,n,MPI_CHAR,1,0,MPI_COMM_WORLD);
printf("Sending word %s in process 0\n",str);
}
else
{//Receiver process
MPI_Recv(&n,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
MPI_Recv(&str,n,MPI_CHAR,0,0,MPI_COMM_WORLD,&status);
printf("Receiving word %s in process 1\n",str);
}
MPI_Finalize();
return 0;
}
Input:
haha
Actual result
Sending word haha in process 0
Receiving word haha in process 1
Exchange result
Sending word haha in process 0
Receiving word �������� in process 1
scanf
where you should simply pass pointer to the arraystr
. The same problems occurs later in your code. – pbnn
. And you should not use&str
which is a pointer to the array (and of typechar (*)[434]
). Use either&str[0]
or plainstr
(which decays to&str[0]
). This use of&str
is wrong everywhere you use it. – Some programmer dude