Hi,
I found a coding issue with the lab2 example in PART1 of the tutorial.
I started working my way through the ND tutorial. When I got to lab2,
something odd happened: the C++ and Fortran versions worked, but the C
version did not. With the C version, all of the processes of rank > 0
died abnormally.
I stared at the code for a while and realized that the source of the
problem was an unitialized variable, "num". Here is the fix for the C
code:
------------------------------------------------------------
$ diff -c lab2.c lab2.c-
*** lab2.c 2003-06-20 11:58:06.000000000 -0400
0 11:58:06.000000000 -0400
--- lab2.c- 2003-06-20 11:56:38.000000000 -0400
***************
*** 51,57 ****
process and then quits. By passing the 0 first, every process
gets the 0 message and can quit normally. */
! do {
MPI_Recv(&num, 1, MPI_INT, from, tag, MPI_COMM_WORLD, &status);
printf("Process %d received %d\n", rank, num);
--- 51,57 ----
process and then quits. By passing the 0 first, every process
gets the 0 message and can quit normally. */
! while (num > 0) {
MPI_Recv(&num, 1, MPI_INT, from, tag, MPI_COMM_WORLD, &status);
printf("Process %d received %d\n", rank, num);
***************
*** 63,69 ****
printf("Process %d sending %d to %d\n", rank, num, next);
MPI_Send(&num, 1, MPI_INT, next, tag, MPI_COMM_WORLD);
! } while (num > 0);
printf("Process %d exiting\n", rank);
/* The last process does one extra send to process 0, which needs to
--- 63,69 ----
printf("Process %d sending %d to %d\n", rank, num, next);
MPI_Send(&num, 1, MPI_INT, next, tag, MPI_COMM_WORLD);
! }
printf("Process %d exiting\n", rank);
/* The last process does one extra send to process 0, which needs to
--------------------------------------------
For processes of rank > 0, the "message receiving loop" must run at
least once. Changing from "while" to "do-while" satisfies this
requirement.
The fact that the code may run on some systems is an consequence of what
the compiler does with uninitialized variables. Some compilers set them
to zero, while others set them to a large positive number.
-Joseph
--
Joseph E. Sacco, Ph.D.
J.E. Sacco & Associates, Inc.
69 Aaron Way · Carlisle, MA · 01741-1324
phone: 978.371.1984 fax: 978.371.4980
|