Don't underesitmate the power of Valgrind! You can simply invoke it as shown below.
mpiexec -l -n 2 valgrind spmd_sum_3 1000Valgrind also has direct hooks for MPI. See the page at:
http://valgrind.org/docs/manual/mc-manual.html#mc-manual.mpiwrap
for more details.