Introduction to Programming with MPI ------------------------------------ Practical Exercise 02 (Using MPI) --------------------------------- For now, omit any error handling and ignore error returns. You should use four processes ('-np 4') unless instructed otherwise. Question 1 ---------- 1.1 Starting with one the following programs (or from scratch, if you prefer): PROGRAM main USE mpi IMPLICIT NONE INTEGER :: error CALL MPI_Init(error) CALL MPI_Finalize(error) END PROGRAM main #include #include int main (int argc, char *argv[]) { MPI_Init(&argc,&argv); MPI_Finalize(); return 0; } Change the program to print out the version numbers, and to print the message to "This is process in a communicator of processes running on processor ", filling the fields in by data obtained from the information calls. Question 2 ---------- 2.1 This question is not fully portable, as it isn't possible to pass command line arguments to the MPI processes under some implementations, and the Fortran version uses either Fortran 2008 or non-standard extensions. However, it will work almost everywhere, though the exact form of the Fortran will depend on the compiler you use. Starting from the same point as in question 1, write a program to take a command name as an argument, call that command passing the process number as an argument, and print out its return code and the time that the command took (using MPI_Wtime). You should call MPI_Barrier before calling the system function to ensure that all MPI processes are properly initialised and synchronised. While this is probably unnecessary, it does avoid some potential (and very obscure) errors when using the program for real. C and C++ programmers should use the 'system' function to invoke a command. The arguments are passed to the program in the normal way, with the first one being the count (INCLUDING the program name) and the next being a vector of strings (use argument 1). int main (int argc, char *argv[]); /* argv[1] is the first command-line argument. */ int system (const char *command); /* Call the command 'command' and return its error code. */ Fortran programmers should use the following Fortran 2003 intrinsic procedures: INTEGER FUNCTION Command_argument_count () ! This returns the number of command-line arguments, NOT including ! the program name. SUBROUTINE Get_command_argument (count, argument) INTEGER, INTENT(IN) :: count CHARACTER(LEN=*), INTENT(OUT) :: argument ! This returns the Nth command-line argument in its second argument, ! counting from 1. If you have a Fortran 2008 compiler, you should also use: SUBROUTINE Execute_command_line (command, EXITSTAT=code) CHARACTER(LEN=*), INTENT(in) :: command ! Call the command 'command' and return its error code in 'code'. Otherwise, if you have gfortran, then you should use the command 'mpif90 -std=gnu ' instead of 'mpif90 ', and use: FUNCTION System (command) INTEGER :: System CHARACTER(LEN=*) :: command ! Call the command 'command' and return its error code. Test the program by calling the script 'trivial' in directory 'Programs'. The program you have written allows you to run multiple copies of a script, and for each run of the script to have a unique number as an argument. This is one way of running multiple copies of a serial program, and is all that you need for some embarrassingly parallel requirements. Look at the supplied program 'mpi_spawn.c' in directory 'Programs' to see a similar program that was written for and used in the HPCF benchmarking suite. There are similar programs available on the Web and supplied with most MPI implementations. Note that you have now written your first practically useful MPI program!