summaryrefslogtreecommitdiff
path: root/doc/html/mpi/getting_started.html
blob: e3d9e8c5fe39349e3f64010030b28867e17152fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Getting started</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../mpi.html" title="Chapter&#160;26.&#160;Boost.MPI">
<link rel="prev" href="../mpi.html" title="Chapter&#160;26.&#160;Boost.MPI">
<link rel="next" href="tutorial.html" title="Tutorial">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../mpi.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../mpi.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tutorial.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="mpi.getting_started"></a>Getting started</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="getting_started.html#mpi.mpi_impl">MPI Implementation</a></span></dt>
<dt><span class="section"><a href="getting_started.html#mpi.config">Configure and Build</a></span></dt>
<dt><span class="section"><a href="getting_started.html#mpi.using">Using Boost.MPI</a></span></dt>
</dl></div>
<p>
      Getting started with Boost.MPI requires a working MPI implementation, a recent
      version of Boost, and some configuration information.
    </p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="mpi.mpi_impl"></a>MPI Implementation</h3></div></div></div>
<p>
        To get started with Boost.MPI, you will first need a working MPI implementation.
        There are many conforming <a href="http://www-unix.mcs.anl.gov/mpi/implementations.html" target="_top">MPI
        implementations</a> available. Boost.MPI should work with any of the
        implementations, although it has only been tested extensively with:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <a href="http://www.open-mpi.org" target="_top">Open MPI</a>
          </li>
<li class="listitem">
            <a href="http://www-unix.mcs.anl.gov/mpi/mpich/" target="_top">MPICH2</a>
          </li>
<li class="listitem">
            <a href="https://software.intel.com/en-us/intel-mpi-library" target="_top">Intel
            MPI</a>
          </li>
</ul></div>
<p>
        You can test your implementation using the following simple program, which
        passes a message from one processor to another. Each processor prints a message
        to standard output.
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">mpi</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">argv</span><span class="special">[])</span>
<span class="special">{</span>
  <span class="identifier">MPI_Init</span><span class="special">(&amp;</span><span class="identifier">argc</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">argv</span><span class="special">);</span>

  <span class="keyword">int</span> <span class="identifier">rank</span><span class="special">;</span>
  <span class="identifier">MPI_Comm_rank</span><span class="special">(</span><span class="identifier">MPI_COMM_WORLD</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">rank</span><span class="special">);</span>
  <span class="keyword">if</span> <span class="special">(</span><span class="identifier">rank</span> <span class="special">==</span> <span class="number">0</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">int</span> <span class="identifier">value</span> <span class="special">=</span> <span class="number">17</span><span class="special">;</span>
    <span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">MPI_Send</span><span class="special">(&amp;</span><span class="identifier">value</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">MPI_INT</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">MPI_COMM_WORLD</span><span class="special">);</span>
    <span class="keyword">if</span> <span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">MPI_SUCCESS</span><span class="special">)</span>
      <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Rank 0 OK!"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
  <span class="special">}</span> <span class="keyword">else</span> <span class="keyword">if</span> <span class="special">(</span><span class="identifier">rank</span> <span class="special">==</span> <span class="number">1</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">int</span> <span class="identifier">value</span><span class="special">;</span>
    <span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">MPI_Recv</span><span class="special">(&amp;</span><span class="identifier">value</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">MPI_INT</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">MPI_COMM_WORLD</span><span class="special">,</span>
			  <span class="identifier">MPI_STATUS_IGNORE</span><span class="special">);</span>
    <span class="keyword">if</span> <span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">MPI_SUCCESS</span> <span class="special">&amp;&amp;</span> <span class="identifier">value</span> <span class="special">==</span> <span class="number">17</span><span class="special">)</span>
      <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Rank 1 OK!"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
  <span class="special">}</span>
  <span class="identifier">MPI_Finalize</span><span class="special">();</span>
  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
        You should compile and run this program on two processors. To do this, consult
        the documentation for your MPI implementation. With <a href="http://www.open-mpi.org" target="_top">OpenMPI</a>,
        for instance, you compile with the <code class="computeroutput"><span class="identifier">mpiCC</span></code>
        or <code class="computeroutput"><span class="identifier">mpic</span><span class="special">++</span></code>
        compiler, boot the LAM/MPI daemon, and run your program via <code class="computeroutput"><span class="identifier">mpirun</span></code>. For instance, if your program is
        called <code class="computeroutput"><span class="identifier">mpi</span><span class="special">-</span><span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span></code>,
        use the following commands:
      </p>
<pre class="programlisting">mpiCC -o mpi-test mpi-test.cpp
lamboot
mpirun -np 2 ./mpi-test
lamhalt
</pre>
<p>
        When you run this program, you will see both <code class="computeroutput"><span class="identifier">Rank</span>
        <span class="number">0</span> <span class="identifier">OK</span><span class="special">!</span></code> and <code class="computeroutput"><span class="identifier">Rank</span>
        <span class="number">1</span> <span class="identifier">OK</span><span class="special">!</span></code> printed to the screen. However, they may
        be printed in any order and may even overlap each other. The following output
        is perfectly legitimate for this MPI program:
      </p>
<pre class="programlisting">Rank Rank 1 OK!
0 OK!
</pre>
<p>
        If your output looks something like the above, your MPI implementation appears
        to be working with a C++ compiler and we're ready to move on.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="mpi.config"></a>Configure and Build</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="getting_started.html#mpi.bjam">Build Environment</a></span></dt>
<dt><span class="section"><a href="getting_started.html#mpi.bootstraping">Bootstrap</a></span></dt>
<dt><span class="section"><a href="getting_started.html#mpi.mpi_setup">Setting up your MPI Implementation</a></span></dt>
<dt><span class="section"><a href="getting_started.html#mpi.installation">Build and Install</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="mpi.bjam"></a>Build Environment</h4></div></div></div>
<p>
          As the rest of Boost, Boost.MPI uses version 2 of the <a href="http://www.boost.org/doc/html/bbv2.html" target="_top">Boost.Build</a>
          system for configuring and building the library binary.
        </p>
<p>
          Please refer to the general Boost installation instructions for <a href="http://www.boost.org/doc/libs/release/more/getting_started/unix-variants.html#prepare-to-use-a-boost-library-binary" target="_top">Unix
          Variant</a> (including Unix, Linux and MacOS) or <a href="http://www.boost.org/doc/libs/1_58_0/more/getting_started/windows.html#prepare-to-use-a-boost-library-binary" target="_top">Windows</a>.
          The simplified build instructions should apply on most platforms with a
          few specific modifications described below.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="mpi.bootstraping"></a>Bootstrap</h4></div></div></div>
<p>
          As described in the boost installation instructions, go to to root of your
          Boost source distribution and run the <code class="computeroutput"><span class="identifier">bootstrap</span></code>
          script (<code class="computeroutput"><span class="special">./</span><span class="identifier">bootstrap</span><span class="special">.</span><span class="identifier">sh</span></code> for
          unix variants or <code class="computeroutput"><span class="identifier">bootstrap</span><span class="special">.</span><span class="identifier">bat</span></code>
          for Windows). That will generate a 'project-config.jam` file in the root
          directory. Use your favourite text editor and add the following line:
        </p>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">mpi</span> <span class="special">;</span>
</pre>
<p>
          Alternatively, you can provided explicitly the list of Boost libraries
          you want to build. Please refer to the <code class="computeroutput"><span class="special">--</span><span class="identifier">help</span></code> option<code class="computeroutput"> <span class="identifier">of</span>
          <span class="identifier">the</span> </code>bootstrap` script.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="mpi.mpi_setup"></a>Setting up your MPI Implementation</h4></div></div></div>
<p>
          First, you need to scan the <code class="literal">include/boost/mpi/config.hpp</code>
          file and check if some settings needs to be modified for your MPI implementation
          or preferences.
        </p>
<p>
          In particular, the <code class="computeroutput"><a class="link" href="../BOOST_MPI_HOMOGENEOUS.html" title="Macro BOOST_MPI_HOMOGENEOUS">BOOST_MPI_HOMOGENEOUS</a></code>
          macro, that you will need to comment out if you plan to run on an heterogeneous
          set of machines. See the <a class="link" href="tutorial.html#mpi.homogeneous_machines" title="Homogeneous Machines">optimization</a>
          notes below.
        </p>
<p>
          Most MPI implementations requires specific compilation and link options.
          In order to mask theses options to the user, most MPI implementations provides
          wrappers which silently pass those options to the compiler.
        </p>
<p>
          Depending on your MPI implementation, some work might be needed to tell
          Boost which specific MPI option to use. This is done through the <code class="computeroutput"><span class="keyword">using</span> <span class="identifier">mpi</span> <span class="special">;</span></code> directive of the <code class="computeroutput"><span class="identifier">project</span><span class="special">-</span><span class="identifier">config</span><span class="special">.</span><span class="identifier">jam</span></code>
          file.
        </p>
<p>
          The general form is the following (do not forget to leave spaces around
          <span class="bold"><strong>:</strong></span> and before <span class="bold"><strong>;</strong></span>):
        </p>
<pre class="programlisting">using mpi
   : [&lt;MPI compiler wrapper&gt;]
   : [&lt;compilation and link options&gt;]
   : [&lt;mpi runner&gt;] ;
</pre>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
              <span class="bold"><strong>If you're lucky</strong></span>
            </li></ul></div>
<p>
          For those who uses <a href="http://www-unix.mcs.anl.gov/mpi/mpich/" target="_top">MPICH2</a>,
          <a href="http://www.open-mpi.org" target="_top">OpenMPI</a> or some of their derivatives,
          configuration can be almost automatic. In fact, if your <code class="computeroutput"><span class="identifier">mpicxx</span></code>
          command is in your path, you just need to use:
        </p>
<pre class="programlisting">using mpi ;
</pre>
<p>
          The directive will find the wrapper and deduce the options to use.
        </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
              <span class="bold"><strong>If your wrapper is not in your path</strong></span>
            </li></ul></div>
<p>
          ...or if it does not have a usual wrapper name, you will need to tell the
          build system where to find it:
        </p>
<pre class="programlisting">using mpi : /opt/mpi/bullxmpi/1.2.8.3/bin/mpicc ;
</pre>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
              <span class="bold"><strong>If your wrapper is really eccentric</strong></span>
            </li></ul></div>
<p>
          or does not exist at all (it happens), you need to provide the compilation
          and build options to the build environment using <code class="computeroutput"><span class="identifier">jam</span></code>
          directives. For example, the following could be used for a specific Intel
          MPI implementation:
        </p>
<pre class="programlisting">using mpi : mpiicc :
      &lt;library-path&gt;/softs/intel/impi/5.0.1.035/intel64/lib
      &lt;library-path&gt;/softs/intel/impi/5.0.1.035/intel64/lib/release_mt
      &lt;include&gt;/softs/intel/impi/5.0.1.035/intel64/include
      &lt;find-shared-library&gt;mpifort
      &lt;find-shared-library&gt;mpi_mt
      &lt;find-shared-library&gt;mpigi
      &lt;find-shared-library&gt;dl
      &lt;find-shared-library&gt;rt ;
</pre>
<p>
          To do that, you need to guess the libraries and include directories associated
          with your environment. You can refer to the your specific MPI environment
          documentation. Most of the time though, your wrapper have an option that
          provide that information, it usually starts with <code class="computeroutput"><span class="special">--</span><span class="identifier">show</span></code>:
        </p>
<pre class="programlisting">$ mpiicc -show
icc -I/softs/.../include ... -L/softs/.../lib ... -Xlinker -rpath -Xlinker /softs/.../lib .... -lmpi -ldl -lrt -lpthread
$
</pre>
<pre class="programlisting">$ mpicc --showme
icc -I/opt/.../include -pthread -L/opt/.../lib -lmpi -ldl -lm -lnuma -Wl,--export-dynamic -lrt -lnsl -lutil -lm -ldl
$ mpicc --showme:compile
-I/opt/mpi/bullxmpi/1.2.8.3/include -pthread
$ mpicc --showme:link
-pthread -L/opt/.../lib -lmpi -ldl -lm -lnuma -Wl,--export-dynamic -lrt -lnsl -lutil -lm -ldl
$
</pre>
<p>
          To see the results of MPI auto-detection, pass <code class="computeroutput"><span class="special">--</span><span class="identifier">debug</span><span class="special">-</span><span class="identifier">configuration</span></code> on the bjam command line.
        </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
              <span class="bold"><strong>If you want to run the regression tests</strong></span>
            </li></ul></div>
<p>
          ...Which is a good thing.
        </p>
<p>
          The (optional) third argument configures Boost.MPI for running regression
          tests. These parameters specify the executable used to launch jobs (the
          default is "mpirun") followed by any necessary arguments to this
          to run tests and tell the program to expect the number of processors to
          follow (default: "-np"). With the default parameters, for instance,
          the test harness will execute, e.g.,
        </p>
<pre class="programlisting">mpirun -np 4 all_gather_test
</pre>
<p>
          Some implementations provides alternative launcher that can be more convenient.
          For example, Intel's MPI provides the <code class="computeroutput"><span class="identifier">mpiexec</span><span class="special">.</span><span class="identifier">hydra</span></code>:
        </p>
<pre class="programlisting">$mpiexec.hydra -np 4 all_gather_test
</pre>
<p>
          which does not requires any daemon to be running (as opposed to their
          <code class="computeroutput"><span class="identifier">mpirun</span></code> command). Such a
          launcher need to be specified though:
        </p>
<pre class="programlisting">using mpi : mpiicc :
      .....
 : mpiexec.hydra -n  ;
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="mpi.installation"></a>Build and Install</h4></div></div></div>
<p>
          To build the whole Boost distribution:
        </p>
<pre class="programlisting">$cd &lt;boost distribution&gt;
$./b2 install
</pre>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../doc/src/images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top">
<p>
            Or, if you have a multi-cpu machine (say 24):
          </p>
<pre class="programlisting">$cd &lt;boost distribution&gt;
$./b2 -j24 install
</pre>
</td></tr>
</table></div>
<p>
          Installation of Boost.MPI can be performed in the build step by specifying
          <code class="computeroutput"><span class="identifier">install</span></code> on the command
          line and (optionally) providing an installation location, e.g.,
        </p>
<pre class="programlisting">$./b2 install
</pre>
<p>
          This command will install libraries into a default system location. To
          change the path where libraries will be installed, add the option <code class="computeroutput"><span class="special">--</span><span class="identifier">prefix</span><span class="special">=</span><span class="identifier">PATH</span></code>.
        </p>
<p>
          Then, you can run the regression tests with:
        </p>
<pre class="programlisting">$cd &lt;boost distribution/lib/mpi/test
$..<span class="emphasis"><em>..</em></span>../b2
</pre>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="mpi.using"></a>Using Boost.MPI</h3></div></div></div>
<p>
        To build applications based on Boost.MPI, compile and link them as you normally
        would for MPI programs, but remember to link against the <code class="computeroutput"><span class="identifier">boost_mpi</span></code>
        and <code class="computeroutput"><span class="identifier">boost_serialization</span></code> libraries,
        e.g.,
      </p>
<pre class="programlisting">mpic++ -I/path/to/boost/mpi my_application.cpp -Llibdir \
  -lboost_mpi-gcc-mt-1_35 -lboost_serialization-gcc-d-1_35.a
</pre>
<p>
        If you plan to use the <a class="link" href="tutorial.html#mpi.python" title="Python Bindings">Python bindings</a>
        for Boost.MPI in conjunction with the C++ Boost.MPI, you will also need to
        link against the boost_mpi_python library, e.g., by adding <code class="computeroutput"><span class="special">-</span><span class="identifier">lboost_mpi_python</span><span class="special">-</span><span class="identifier">gcc</span><span class="special">-</span><span class="identifier">mt</span><span class="special">-</span><span class="number">1</span><span class="identifier">_35</span></code> to your link command. This step will
        only be necessary if you intend to <a class="link" href="tutorial.html#mpi.python_user_data" title="Transmitting User-Defined Data">register
        C++ types</a> or use the <a class="link" href="tutorial.html#mpi.python_skeleton_content" title="Skeleton/Content Mechanism">skeleton/content
        mechanism</a> from within Python.
      </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2005-2007 Douglas Gregor,
      Matthias Troyer, Trustees of Indiana University<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
        http://www.boost.org/LICENSE_1_0.txt </a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../mpi.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../mpi.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tutorial.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>