An Introduction to Parallel Programming

Chapter03 - Home
Exercises: 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - 15 - 16 - 17 - 18 - 19 - 20 - 21 - 22 - 23 - 24 - 25 - 26 - 27 - 28 - 29 - 30 - 31 - 32 - 33 - 34 - 35 - 36 - 37

Exercise. This is the solution to exercise 3.1 in the book.

Solution. In both cases the program executes correctly. In the first case the terminating NULL character of the string is removed. Notice that in this case the reason why a correct execution occurs is because the greeting variable is initialized to zero by the compiler.Hence, when MPI_Recv is called it will only overwrite the number of characters in greeting as have been sent by MPI_Send. Thus, the NULL terminating string character is silently added and the output is correct. If we introduce the following code after line 22 of Program 3.1 in the text:

memset(greeting,0x30,MAX_STRING);

then the string will not be terminated properly anymore and the output will contain ASCII character 0x30 (”0”) for processes 1..comm _sz.

In the second case the behavior is expected to be correct. MPI_Send will send more data than needed to represent the string, so it’s wasting inter-process bandwidth unnecessarily. However, the string will be transmitted correctly (as well as additional characters that are not printed because of NULL termination of the string).