#includeOf course life cannot be so simple in the parallel programming world, so here is the first (but wrong) multiprocessor version:#include int main(int argc, char **argv) { printf("Hello world from process %d\n", myrank); return 0;}
#include <stdio.h> #include <stdlib.h> #include "mpi.h" int main(int argc, char **argv) { int myrank, p; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Comm_size(MPI_COMM_WORLD, &p); printf("Hello world from process %d\n", myrank); MPI_Finalize(); return 0;}First of all, the program has to be compiled with all the MPI libraries - a makefile will be provided for that. Secondly, it cannot be run simply by typing the executable name. For example, here is can happen when that is done:
[scrap04.cs.indiana.edu:84] ./howdy [0] Aborting program ! Could not create p4 procgroup. Possible missing file or program started without mpirun.Instead, it must be run through a script, typically named mpirun which starts up a certain number of processes, all running the same program. For example, to run 4 processes of the above program, you would execute
mpirun -np 4 ./howdyWhen the job runs, what happens is:
Typical output from the program looks like:
[scrap04.cs.indiana.edu:102] mpirun -np 4 howdy Hello world from process 0 Hello world from process 2 Hello world from process 3 Hello world from process 1Note that the output is not necessarily in rank order - but why should we think that the one with rank 1 will print before the one with rank 2? The above program is actually wrong in that there is no guarantee of what printf() does with multiple processes. Does each get connected to your terminal? Do they each get their own file descriptor? In is possible that one output line will appear in the middle of another. Following is a safer way to do the howdy program:
#include <stdio.h> #include <stdlib.h> #include "mpi.h" int main(int argc, char **argv) { int myrank; int src, dest, p; int tag = 1; char msg[256]; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Comm_size(MPI_COMM_WORLD, &p); if (myrank != 0) { sprintf(msg, "Hello world from process %d\n", myrank); dest = 0; MPI_Send(msg, strlen(msg)+1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); } else { printf("Hello world from process %d\n", myrank); for (src = 1; src < p; src++) { MPI_Recv(msg, 256, MPI_CHAR, src, tag, MPI_COMM_WORLD, &status); printf("%s", msg);} } MPI_Finalize(); return 0;}What this program does is