Correct typo "nning" -> "running" in content/message_passing/index.shtml.
[parallel_computing.git] / content / Elementary_MPI / index.shtml
1 <!--#set var="root_directory" value="../.." --><!--#include virtual="$root_directory/shared/header.shtml"-->
2
3 <h1>Elementary MPI</h1>
4
5 <!--TableOfContents:Begin-->
6 <!--TableOfContents:End-->
7
8 <h2 id="what">What is MPI?</h2>
9
10 <p>MPI consists of a library of routines with Fortran, C, and C++
11 bindings to facilitate the coding of parallel codes in a distributed
12 memory MIMD environment. In addition it provides a run-time
13 environment to build and execute the parallel codes. It comes with
14 implementations on most popular platforms, including
15 the <em>beowulf</em> type systems.</p>
16
17 <p>MPICH is a full implementation of MPI in a platform independent
18 way. The MPICH implementation of MPI is what we will use in this
19 course.</p>
20
21 <p>MPI maintains internal data-structures related to the
22 administrations of the parallel tasks and to allow internal
23 communications between the tasks and the environment. The latter are
24 referred to as <em>handles</em>.</p>
25
26 <p>The C MPI routines return an <code>int</code> and the Fortran
27 routines return an integer <code>ierror</code> in the calls which
28 contain the error status of the calls. Error detection and debugging
29 of parallel codes are difficult in general, and MPI can only attempt
30 to detect errors.</p>
31
32 <h2 id="Binding">Binding to Fortran and C</h2>
33
34 <p>All MPI routines are labeled MPI_Name, with the first letter of the
35 name capitalized.</p>
36
37 <p>The C calls syntax is</p>
38 <pre>
39 int MPI_Name( &lt;argument list&gt; )
40 </pre>
41 <p>with an error code returned as an <code>int</code> upon execution.
42 The header file is <code>mpi.h</code>. The MPI provided constants are
43 all capitalized, i.e., <code>MPI_COMM_WORLD</code>.</p>
44
45 <p>Fortran calls follow the syntax</p>
46 <pre>
47 call MPI_Name ( &lt;argument list&gt;, ierror )
48 </pre>
49 <p>with the error code being an integer type. The header file is
50 <code>mpif.h</code>. The MPI provided constants are all captialized,
51 i.e., <code>MPI_COMM_WORLD</code>.  However, note that Fortran is case
52 insensitive, and that consequently the use of capital letters is for
53 clarity only.</p>
54
55 <h2 id="init">MPI Initialization</h2>
56
57 <pre>
58 int MPI_Init( int *argc, char ***argv)
59 </pre>
60
61 <p>Initializes the executables on the nodes to the MPI handling
62 daemons. It creates a communicator <code>MPI_COMM_WORLD</code> which
63 encompasses all the initialized processes on the various nodes and
64 give a unique identification to each process, the rank number. The
65 rank and size of the communicator can be obtained via MPI query
66 routines.</p>
67
68 <h2 id="who">Who Am I?</h2>
69
70 <pre>
71 int MPI_Comm_size( MPI_Comm comm, int *size)
72 int MPI_Comm_rank( MPI_Comm comm, int *rank)
73 </pre>
74
75 <p>Returns respectively the size of the specified communicator and the
76 rank (process number) of the current process within the specified
77 communicator. The rank number goes between 0
78 and <code>size-1</code>.</p>
79
80 <h2 id="final">Finalizing an MPI code</h2>
81
82 <pre>
83 int MPI_Finalize()
84 </pre>
85
86 <p>Must be the last call to MPI in any MPI code. No other call to MPI
87 routines can be executed after it. Note that a new communicator can
88 not be invoked after <code>MPI_finalize()</code>.</p>
89
90 <h2 id="CL">Compiling &amp; Linking an MPI code</h2>
91
92 <pre>
93 mpicc &lt;name.c&gt; -o &lt;name&gt;
94 </pre>
95
96 <p>Compiles a C code <code>name.c</code>, produces the object under
97 <code>name.o</code>, and links it with the MPI
98 library. The <code>-o</code> option is to specify the executable name,
99 the default being <code>a.out</code>. This command calls the default C
100 compiler, usualy the GNU gcc compiler on linux based
101 computers.</p>
102
103 <pre>
104 mpif77 &lt;name.f&gt; -o &lt;name&gt;
105 </pre>
106
107 <p>Compiles a Fortran code <code>name.f</code>, produces the object
108 under <code>name.o</code>, and links it with the MPI
109 library. The <code>-o</code> option is to specify the executable name,
110 the default being <code>a.out</code>. This command calls the default
111 Fortran compiler, usualy the GNU f77 compiler on linux based
112 computers.</p>
113
114 <h2 id="running">Running an MPI code</h2>
115
116 <p>MPI2 provides the <code>mpd</code> subset of commands to build a
117 virtual parallel machine, a communication ring. These were described
118 in the previous section on <a href="../MPI2/#ring">Communication
119 Ring</a> under MPI2.</p>
120
121 <p>Once a communication ring has been established, the command
122 <code>mpiexec</code> launches an executable on a specified number of
123 nodes.</p>
124
125 <pre>
126 mpiexec -np &lt;number-of-processes&gt; &lt;executable&gt;
127 </pre>
128 <p>will start the executable code <code>executable</code> on
129 <code>&lt;number-of-processes&gt;</code> nodes.</p>
130
131 <p>MPI2 will choose the nodes automatically, starting from the current
132 one (launching node) and selecting from the list of available
133 processors in the communication
134 ring. If <code>&lt;number-of-processes&gt;</code> is larger than the
135 number of nodes included in the communication ring, the executable
136 will be launched more than once in some of the nodes chosen in a
137 cyclic way.</p>
138
139 <h2 id="examples">Examples</h2>
140
141 Check out the <a href="../../src/hello_MPI/">hello_MPI</a> package for
142 some simple MPI examples.
143
144 <!--#include virtual="$root_directory/shared/footer.shtml"-->