Reputation: 45
I have written a simple MPI program. I am not doing any processing in the program. It just has the MPI skeleton and lines to read a variable. However, the program gets hanged while running. It prints a few newlines and then hangs forever.
int main(int argc, char* argv[]) {
int my_rank; /* rank of process */
int p; /* number of processes */
int tag = 0; /* tag for messages */
long N;
MPI_Status status; /* return status for receive */
/* start up MPI */
MPI_Init(&argc, &argv);
/* find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* find out number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &p);
printf("\nEnter the number of random points to generate: ");
scanf("%ld", &N);
if (my_rank == 0) {
}
/* shut down MPI */
MPI_Finalize();
return 0;
}
Upvotes: 1
Views: 3790
Reputation: 1543
If you are using Eclipse for parallel programming, then (most probably) that is what causes the issue.
It is some kind of glitch in PTP. You can also experience this with printf
, it only prints after your program has finished execution or was shut down (not while the program is running).
Then try running your code from the command line, it will work.
If it still gets hanged or you are not using Eclipse for parallel programming,
then try calling fflush(stdout)
or while ((c = getchar()) != '\n' && c != EOF);
to flush everything before reading user input.
Upvotes: 2
Reputation: 309
scanf reads a function from STDIN and stores that in the variable of choice, in your case N.
When you write an MPI program, the code after mpi_init is executed by each process independently.
It will run fine till the printf statement (each process will execute the printf in parallel). Once it encounters the scanf, the program halts execution as it now expects input from STDIN, which is not provided.
Upvotes: 0