Basic Message Passing<< Process Creation And Execution | Table of Contents | MPI Function Reference >>
Your First MPI ProgramConsider the application of computing the sum of n real values between 1.0 and 1 million with equal steps between the n values. A sequential implementation appears below (:sourcefile filename=sumseq.cc lang=cpp:) If n is large, this can be time consuming. To improve the speed, we can create a parallel version which lets each process compute a subrange of the values. The subtotals computed by each process can then be added together to determine the final sum. The parallel solution requires messages between the processes. Broadcasting MessagesMPI provides a broadcast mechanism in which a process can transmit data to all processes in a given communications group using a single instruction. In our example problem, the root node must send the value of n to the compute nodes so they will know the increment value. The nodes can then determine the range for which they are responsible using their pid. The MPI routine for broadcasting messages is shown below (:source lang=cpp:)MPI_Bcast( &n, 1, MPI_INT, 0, MPI_COMM_WORLD ); where we specify the number of elements address of the first element to be broadcast. The
|