From: W. Trevor King Date: Wed, 15 Sep 2010 03:43:47 +0000 (-0400) Subject: Cleaned up src/hello_MPI/. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4abc7544177552c453a6a7385225b855584542bc;p=parallel_computing.git Cleaned up src/hello_MPI/. --- diff --git a/content/Elementary_MPI/examples/index.shtml b/content/Elementary_MPI/examples/index.shtml deleted file mode 100644 index 03a8ef3..0000000 --- a/content/Elementary_MPI/examples/index.shtml +++ /dev/null @@ -1,19 +0,0 @@ - - - -

Elementary MPI — Examples

- -
-
hello.c
-
simplest MPI "Hello, World!" program.
-
name.c
-
each process is on different host - host name
-
argument_list.c
-
question of line argument list
-
different_tasks.c
-
different tasks are performed by different processes
-
different_routines.c
-
primitive Master-Slave code
-
- - diff --git a/content/Elementary_MPI/index.shtml b/content/Elementary_MPI/index.shtml index 6dc4f26..02848bc 100644 --- a/content/Elementary_MPI/index.shtml +++ b/content/Elementary_MPI/index.shtml @@ -136,4 +136,9 @@ number of nodes included in the communication ring, the executable will be launched more than once in some of the nodes chosen in a cyclic way.

+

Examples

+ +Check out the hello_MPI package for +some simple MPI examples. + diff --git a/src/MPI_hello/argument_list.c b/src/MPI_hello/argument_list.c deleted file mode 100644 index 7ce5310..0000000 --- a/src/MPI_hello/argument_list.c +++ /dev/null @@ -1,31 +0,0 @@ - - /* Simplest MPI code - Hello World */ - - /* Question of argument list */ - -#include -#include /* MPI header file */ - -int main(int argc, char *argv[]) -{ - - int rank, size; - /* init into MPI */ - MPI_Init(&argc, &argv); - /* my rank - my id */ - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - /* how many processes in the virtual machine */ - MPI_Comm_size(MPI_COMM_WORLD, &size); - - - /* let's sample at the argument list*/ - printf("I am %d out of %d --- Arguments: %d %s %s\n", rank, size, - argc, argv[0], argv[argc-1] ); - - - /* out of the virtual machine */ - MPI_Finalize(); - -} - - diff --git a/src/MPI_hello/different_routines.c b/src/MPI_hello/different_routines.c deleted file mode 100644 index 82d3914..0000000 --- a/src/MPI_hello/different_routines.c +++ /dev/null @@ -1,58 +0,0 @@ - - /* Simplest MPI code - Hello World */ - - /* Tasks are on different processes */ - /* Master - Slave subroutines */ - -#include -#include -#include /* 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; - /* init into MPI */ - MPI_Init(&argc, &argv); - /* my rank - my id */ - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - /* how many processes in the virtual machine */ - MPI_Comm_size(MPI_COMM_WORLD, &size); - - /* tasks to perform may depend on */ - /* which process is running */ - if ( rank == 0 ) - master( rank, size ); - else - slave( rank, size ); - - - /* out of the virtual machine */ - MPI_Finalize(); -} diff --git a/src/MPI_hello/different_tasks.c b/src/MPI_hello/different_tasks.c deleted file mode 100644 index 6d31a49..0000000 --- a/src/MPI_hello/different_tasks.c +++ /dev/null @@ -1,36 +0,0 @@ - - /* Simplest MPI code - Hello World */ - - /* Tasks are on different processes */ - -#include -#include /* MPI header file */ - -int main(int argc, char *argv[]) -{ - - int rank, size; - /* init into MPI */ - MPI_Init(&argc, &argv); - /* my rank - my id */ - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - /* how many processes in the virtual machine */ - MPI_Comm_size(MPI_COMM_WORLD, &size); - - - /* 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 ); - - - /* out of the virtual machine */ - MPI_Finalize(); - -} - - diff --git a/src/MPI_hello/hello.c b/src/MPI_hello/hello.c deleted file mode 100644 index cd5632f..0000000 --- a/src/MPI_hello/hello.c +++ /dev/null @@ -1,28 +0,0 @@ - - /* Simplest MPI code - Hello World */ - -#include -#include /* MPI header file */ - -int main(int argc, char *argv[]) -{ - - int rank, size; - /* init into MPI */ - MPI_Init(&argc, &argv); - /* my rank - my id */ - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - /* how many processes in the virtual machine */ - MPI_Comm_size(MPI_COMM_WORLD, &size); - - - /* Salute the world */ - printf("Hello World --- I am %d out of %d.\n", rank, size); - - - /* out of the virtual machine */ - MPI_Finalize(); - -} - - diff --git a/src/MPI_hello/name.c b/src/MPI_hello/name.c deleted file mode 100644 index 3dfa9d9..0000000 --- a/src/MPI_hello/name.c +++ /dev/null @@ -1,33 +0,0 @@ - - /* Simplest MPI code - Hello World */ - /* Processes are on different nodes */ - -#include -#include -#include /* MPI header file */ - -int main(int argc, char *argv[]) -{ - - int rank, size; - char buf[100]; - /* init into MPI */ - MPI_Init(&argc, &argv); - /* my rank - my id */ - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - /* how many processes in the virtual machine */ - MPI_Comm_size(MPI_COMM_WORLD, &size); - - - /* which host is this ? */ - strcpy(buf, "Hi from the exciting world of "); - gethostname(buf + strlen(buf), 64); - printf("%s --- I am %d out of %d.\n", buf, rank, size); - - - /* out of the virtual machine */ - MPI_Finalize(); - -} - - diff --git a/src/MPI_hello/.htaccess b/src/hello_MPI/.htaccess similarity index 100% rename from src/MPI_hello/.htaccess rename to src/hello_MPI/.htaccess diff --git a/src/MPI_hello/.make_tar b/src/hello_MPI/.make_tar similarity index 100% rename from src/MPI_hello/.make_tar rename to src/hello_MPI/.make_tar diff --git a/src/hello_MPI/Makefile b/src/hello_MPI/Makefile new file mode 100644 index 0000000..a6377ae --- /dev/null +++ b/src/hello_MPI/Makefile @@ -0,0 +1,22 @@ +# 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 diff --git a/src/hello_MPI/README b/src/hello_MPI/README new file mode 100644 index 0000000..e7ddd46 --- /dev/null +++ b/src/hello_MPI/README @@ -0,0 +1,86 @@ +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=" + +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 diff --git a/src/hello_MPI/argument_list.c b/src/hello_MPI/argument_list.c new file mode 100644 index 0000000..bc91d3e --- /dev/null +++ b/src/hello_MPI/argument_list.c @@ -0,0 +1,22 @@ +/* Third Simplest MPI code - You told me to... */ + +#include +#include /* 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 */ +} diff --git a/src/hello_MPI/different_routines.c b/src/hello_MPI/different_routines.c new file mode 100644 index 0000000..bf9486d --- /dev/null +++ b/src/hello_MPI/different_routines.c @@ -0,0 +1,47 @@ +/* Fifth Simplest MPI code - Simple Master-Slave */ + +#include +#include +#include /* 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 */ +} diff --git a/src/hello_MPI/different_tasks.c b/src/hello_MPI/different_tasks.c new file mode 100644 index 0000000..874cccf --- /dev/null +++ b/src/hello_MPI/different_tasks.c @@ -0,0 +1,25 @@ +/* Fourth Simplest MPI code - Let's split up... */ + +/* Different tasks are on different processes */ + +#include +#include /* 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 */ +} diff --git a/src/hello_MPI/hello.c b/src/hello_MPI/hello.c new file mode 100644 index 0000000..bcfb656 --- /dev/null +++ b/src/hello_MPI/hello.c @@ -0,0 +1,18 @@ +/* Simplest MPI code - Hello World */ + +#include +#include /* 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 */ +} diff --git a/src/hello_MPI/name.c b/src/hello_MPI/name.c new file mode 100644 index 0000000..389a014 --- /dev/null +++ b/src/hello_MPI/name.c @@ -0,0 +1,23 @@ +/* Second Simplest MPI code - My name is... */ + +#include +#include +#include /* 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); + +}