There are at least two important advantages to programming in a SPMD style:
We will assume in this discussion that PVM(e) is being used as the message passing library, however, the same strategy applies equally well to MPL programs.
The key to writing a manager-worker type program in SPMD style is to include a branch in your program just after a pvm_mytid() and pvm_parent() call, based on whether the particular program instance was spawned ( == has a parent). If the program detects that it has no parent, it takes the role of the manager process and does all the start-up functions, including spawning worker tasks and distributing work. It may set aside a portion of the parallel work for itself, so that it does not sit idle while the parallel calculations are carried out. When finished with its share of the calculations, it can then receive messages back from the "worker" processes, perhaps containing the results of the parallel computations.
If the program detects that it was spawned from another process, then it bypasses all the start-up functions, and goes right to work on it's share of the global problem, then possibly sends to the "manager" process results of its calculations.
Following is an example of how a Fortran MPMD style program was transformed into a SPMD program:
The MPMD style program in
is converted to the SPMD program
See the directory /usr/local/doc/Examples/spmd for the complete spmd example, including a makefile and README file with instructions for building and executing the program.