+++ /dev/null
-<!--#set var="root_directory" value="../.." -->
-<!--#include virtual="$root_directory/shared/header.shtml"-->
-
-<h1>Elementary MPI — Examples</h1>
-
-<dl>
- <dt><a href="src/hello.c">hello.c</a></dt>
- <dd>simplest MPI "Hello, World!" program.</dd>
- <dt><a href="src/name.c">name.c</a></dt>
- <dd>each process is on different host - host name</dd>
- <dt><a href="src/argument_list.c">argument_list.c</a></dt>
- <dd>question of line argument list</dd>
- <dt><a href="src/different_tasks.c">different_tasks.c</a></dt>
- <dd>different tasks are performed by different processes</dd>
- <dt><a href="src/different_routines.c">different_routines.c</a></dt>
- <dd>primitive Master-Slave code</dd>
-</dl>
-
-<!--#include virtual="$root_directory/shared/footer.shtml"-->
will be launched more than once in some of the nodes chosen in a
cyclic way.</p>
+<h2 id="examples">Examples</h2>
+
+Check out the <a href="../../src/hello_MPI/">hello_MPI</a> package for
+some simple MPI examples.
+
<!--#include virtual="$root_directory/shared/footer.shtml"-->
+++ /dev/null
-\r
- /* Simplest MPI code - Hello World */\r
-\r
- /* Question of argument list */\r
-\r
-#include <stdio.h>\r
-#include <mpi.h> /* MPI header file */\r
-\r
-int main(int argc, char *argv[])\r
-{\r
-\r
- int rank, size;\r
- /* init into MPI */\r
- MPI_Init(&argc, &argv);\r
- /* my rank - my id */\r
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);\r
- /* how many processes in the virtual machine */\r
- MPI_Comm_size(MPI_COMM_WORLD, &size);\r
-\r
-\r
- /* let's sample at the argument list*/\r
- printf("I am %d out of %d --- Arguments: %d %s %s\n", rank, size,\r
- argc, argv[0], argv[argc-1] );\r
-\r
-\r
- /* out of the virtual machine */\r
- MPI_Finalize();\r
-\r
-}\r
-\r
-\r
+++ /dev/null
-\r
- /* Simplest MPI code - Hello World */\r
-\r
- /* Tasks are on different processes */\r
- /* Master - Slave subroutines */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <mpi.h> /* MPI header file */\r
-\r
-\r
- /* master routine */\r
-void master( int rank, int size ) \r
-{\r
- printf( " (master routine) I am %d out of %d -- I am the master! \n", \r
- rank, size );\r
-}\r
-\r
-\r
- /* slave routine */\r
-void slave( int rank, int size )\r
-{\r
- if ( rank == 0 ) \r
- {\r
- fprintf( stderr, "Error in the slave routine \n" );\r
- MPI_Finalize();\r
- exit (1);\r
- }\r
- else if ( rank == size-1 )\r
- printf( " (slave routine) I am %d out of %d -- I am last in this world! \n",\r
- rank, size );\r
- else\r
- printf( " (slave routine) I am %d out of %d -- I am a great worker! \n", \r
- rank, size );\r
-}\r
-\r
-\r
-int main(int argc, char *argv[])\r
-{\r
- int rank, size;\r
- /* init into MPI */\r
- MPI_Init(&argc, &argv);\r
- /* my rank - my id */\r
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);\r
- /* how many processes in the virtual machine */\r
- MPI_Comm_size(MPI_COMM_WORLD, &size);\r
-\r
- /* tasks to perform may depend on */\r
- /* which process is running */\r
- if ( rank == 0 ) \r
- master( rank, size );\r
- else \r
- slave( rank, size );\r
-\r
-\r
- /* out of the virtual machine */\r
- MPI_Finalize();\r
-}\r
+++ /dev/null
-\r
- /* Simplest MPI code - Hello World */\r
-\r
- /* Tasks are on different processes */\r
-\r
-#include <stdio.h>\r
-#include <mpi.h> /* MPI header file */\r
-\r
-int main(int argc, char *argv[])\r
-{\r
-\r
- int rank, size;\r
- /* init into MPI */\r
- MPI_Init(&argc, &argv);\r
- /* my rank - my id */\r
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);\r
- /* how many processes in the virtual machine */\r
- MPI_Comm_size(MPI_COMM_WORLD, &size);\r
-\r
-\r
- /* tasks to perform may depend on */\r
- /* which process is running */\r
- if ( rank == 0 ) \r
- printf( " I am %d out of %d -- I am the master! \n", rank, size );\r
- else if ( rank == size-1 )\r
- printf( " I am %d out of %d -- I am last in this world! \n", rank, size );\r
- else\r
- printf( " I am %d out of %d -- I am a great worker! \n", rank, size );\r
-\r
-\r
- /* out of the virtual machine */\r
- MPI_Finalize();\r
-\r
-}\r
-\r
-\r
+++ /dev/null
-\r
- /* Simplest MPI code - Hello World */\r
-\r
-#include <stdio.h>\r
-#include <mpi.h> /* MPI header file */\r
-\r
-int main(int argc, char *argv[])\r
-{\r
-\r
- int rank, size;\r
- /* init into MPI */\r
- MPI_Init(&argc, &argv);\r
- /* my rank - my id */\r
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);\r
- /* how many processes in the virtual machine */\r
- MPI_Comm_size(MPI_COMM_WORLD, &size);\r
-\r
-\r
- /* Salute the world */\r
- printf("Hello World --- I am %d out of %d.\n", rank, size);\r
-\r
-\r
- /* out of the virtual machine */\r
- MPI_Finalize();\r
-\r
-}\r
-\r
-\r
+++ /dev/null
-\r
- /* Simplest MPI code - Hello World */\r
- /* Processes are on different nodes */\r
-\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <mpi.h> /* MPI header file */\r
-\r
-int main(int argc, char *argv[])\r
-{\r
-\r
- int rank, size;\r
- char buf[100];\r
- /* init into MPI */\r
- MPI_Init(&argc, &argv);\r
- /* my rank - my id */\r
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);\r
- /* how many processes in the virtual machine */\r
- MPI_Comm_size(MPI_COMM_WORLD, &size);\r
-\r
-\r
- /* which host is this ? */\r
- strcpy(buf, "Hi from the exciting world of ");\r
- gethostname(buf + strlen(buf), 64);\r
- printf("%s --- I am %d out of %d.\n", buf, rank, size);\r
-\r
-\r
- /* out of the virtual machine */\r
- MPI_Finalize();\r
-\r
-}\r
-\r
-\r
--- /dev/null
+# Environment
+
+CC = /usr/bin/mpicc
+CFLAGS =
+LD = $(CC)
+LDFLAGS =
+RM = /bin/rm
+EXECS = hello name argument_list different_tasks different_routines
+
+all: $(EXECS)
+
+%.o : %.c
+ $(CC) -c $(CFLAGS) -o $@ $^
+
+$(EXECS) : % : %.o
+ $(LD) $(LDFLAGS) -o $@ $^
+
+clean:
+ $(RM) -f *.o $(EXECS)
+
+# Interesting Makefile sections
+# 4.12.1 Syntax of Static Pattern Rules
--- /dev/null
+hello_MPI
+=========
+
+Simple examples introducing MPI.
+
+Manifest
+--------
+
+==================== =============================================
+README this file
+Makefile automate building and cleanup
+hello.c a simple "Hello, World!" MPI program
+name.c each process is on different host - host name
+argument_list.c question of line argument list
+different_tasks.c perform different tasks on different hosts
+different_routines.c primitive Master-Slave code
+==================== =============================================
+
+Build
+-----
+
+Just run
+
+ $ make
+
+Usage
+-----
+
+MPI configuration
+~~~~~~~~~~~~~~~~~
+
+You should only have to do this once for each host, but since this is
+your first MPI program, you probably haven't done it yet. From
+
+ $ man mpd
+
+we have:
+
+ "A file named .mpd.conf file must be present in the user's home
+ directory with read and write access only for the user, and must
+ contain at least a line with MPD_SECRETWORD=<secretword>"
+
+so create the file with a random secret word:
+
+ $ touch ~/.mpd.conf
+ $ chmod 600 ~/.mpd.conf
+ $ echo "MPD_SECRETWORD=$(cat /dev/urandom | tr -d -c A-Z-a-z-0-9 | head -c 60)" ~/.mpd.conf
+
+Running MPI programs
+~~~~~~~~~~~~~~~~~~~~
+
+Create a list of possible hosts `mpd.hosts`, for example
+
+ xphy1.physics.xterm.net
+ xphy2.physics.xterm.net
+ ...
+
+Start the Message Passing Daemons with
+
+ $ mpdboot -f mpd.hosts
+
+which spawns a daemon (`mpd`) on each host. Then run your program
+(e.g. `hello`) with.
+
+ $ mpiexec -l -n 1 ./hello
+
+Note that we use ./hello instead of hello or you'll get errors like
+
+ $ mpiexec -n 1 hello
+ problem with execution of hello on xphy1.physics.xterm.net:
+ [Errno 2] No such file or directory
+
+When you're done, clean up the daemons with
+
+ $ mpdallexit
+
+As always, use `man X` to get more information about a particular
+executable.
+
+Examples for each of the executables in this package:
+
+ $ mpiexec -n 2 ./hello
+ $ mpiexec -n 2 ./name
+ $ mpiexec -n 2 ./argument_list arg-A arg-B arg-C
+ $ mpiexec -n 4 ./different_tasks
+ $ mpiexec -n 4 ./different_routines
--- /dev/null
+/* Third Simplest MPI code - You told me to... */
+
+#include <stdio.h>
+#include <mpi.h> /* MPI header file */
+
+int main(int argc, char *argv[])
+{
+ int rank, size, i;
+
+ MPI_Init(&argc, &argv); /* connect to MPI */
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* my rank (id) */
+ MPI_Comm_size(MPI_COMM_WORLD, &size); /* # of daemons in the world */
+
+ /* show the argument list*/
+ printf("I am %d out of %d --- Arguments: %d", rank, size, argc);
+ for (i=0; i < argc; i++) {
+ printf(" '%s'", argv[i]);
+ }
+ printf("\n");
+
+ MPI_Finalize(); /* disconnect from MPI */
+}
--- /dev/null
+/* Fifth Simplest MPI code - Simple Master-Slave */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <mpi.h> /* MPI header file */
+
+
+/* master routine */
+void master(int rank, int size)
+{
+ printf("(master routine) I am %d out of %d -- I am the master!\n",
+ rank, size);
+}
+
+/* slave routine */
+void slave(int rank, int size)
+{
+ if (rank == 0)
+ {
+ fprintf(stderr, "Error in the slave routine\n");
+ MPI_Finalize();
+ exit (1);
+ }
+ else if ( rank == size-1 )
+ printf("(slave routine) I am %d out of %d -- I am last in this world!\n",
+ rank, size );
+ else
+ printf("(slave routine) I am %d out of %d -- I am a great worker!\n",
+ rank, size );
+}
+
+
+int main(int argc, char *argv[])
+{
+ int rank, size;
+
+ MPI_Init(&argc, &argv); /* connect to MPI */
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* my rank (id) */
+ MPI_Comm_size(MPI_COMM_WORLD, &size); /* # of daemons in the world */
+
+ if (rank == 0)
+ master(rank, size);
+ else
+ slave(rank, size);
+
+ MPI_Finalize(); /* disconnect from MPI */
+}
--- /dev/null
+/* Fourth Simplest MPI code - Let's split up... */
+
+/* Different tasks are on different processes */
+
+#include <stdio.h>
+#include <mpi.h> /* MPI header file */
+
+int main(int argc, char *argv[])
+{
+ int rank, size;
+
+ MPI_Init(&argc, &argv); /* connect to MPI */
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* my rank (id) */
+ MPI_Comm_size(MPI_COMM_WORLD, &size); /* # of daemons in the world */
+
+ /* tasks to perform may depend on which process is running */
+ if (rank == 0)
+ printf( "I am %d out of %d -- I am the master!\n", rank, size);
+ else if (rank == size-1)
+ printf( "I am %d out of %d -- I am last in this world!\n", rank, size);
+ else
+ printf( "I am %d out of %d -- I am a great worker!\n", rank, size);
+
+ MPI_Finalize(); /* disconnect from MPI */
+}
--- /dev/null
+/* Simplest MPI code - Hello World */
+
+#include <stdio.h>
+#include <mpi.h> /* MPI header file */
+
+int main(int argc, char *argv[])
+{
+ int rank, size;
+
+ MPI_Init(&argc, &argv); /* connect to MPI */
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* my rank (id) */
+ MPI_Comm_size(MPI_COMM_WORLD, &size); /* # of daemons in the world */
+
+ /* Salute the world */
+ printf("Hello World --- I am %d out of %d.\n", rank, size);
+
+ MPI_Finalize(); /* disconnect from MPI */
+}
--- /dev/null
+/* Second Simplest MPI code - My name is... */
+
+#include <stdio.h>
+#include <string.h>
+#include <mpi.h> /* MPI header file */
+
+#define BUF 256 /* length of name buffer */
+
+int main(int argc, char *argv[])
+{
+ int rank, size;
+ char buf[BUF] = {0};
+
+ MPI_Init(&argc, &argv); /* connect to MPI */
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* my rank (id) */
+ MPI_Comm_size(MPI_COMM_WORLD, &size); /* # of daemons in the world */
+
+ /* which host is this ? */
+ strcpy(buf, "Hi from the exciting world of ");
+ gethostname(buf + strlen(buf), BUF - strlen(buf) - 1);
+ printf("%s --- I am %d out of %d.\n", buf, rank, size);
+
+}