File: java.rst

package info (click to toggle)
openmpi 5.0.8-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,692 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (350 lines) | stat: -rw-r--r-- 12,481 bytes parent folder | download | duplicates (10)
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
.. _open-mpi-java-label:

Open MPI Java bindings
======================

Open MPI |ompi_ver| provides support for Java-based MPI applications.

.. warning:: The Open MPI Java bindings are provided on a
   "provisional" basis |mdash| i.e., they are not part of the current or
   proposed MPI standards.  Thus, inclusion of Java support is not
   required by the standard.  Continued inclusion of the Java bindings
   is contingent upon active user interest and continued developer
   support.

The rest of this document provides step-by-step instructions on
building OMPI with Java bindings, and compiling and running Java-based
MPI applications. Also, part of the functionality is explained with
examples. Further details about the design, implementation and usage
of Java bindings in Open MPI can be found in its canonical reference
paper [#ompijava]_. The bindings follow a JNI approach, that is, we do
not provide a pure Java implementation of MPI primitives, but a thin
layer on top of the C implementation. This is the same approach as in
mpiJava [#mpijava]_; in fact, mpiJava was taken as a starting point
for Open MPI Java bindings, but they were later totally rewritten.

Building the Java bindings
--------------------------

Java support requires that Open MPI be built at least with shared
libraries (i.e., ``--enable-shared``).  Note that this is the default
for Open MPI, so you don't have to explicitly add the option. The Java
bindings will build only if ``--enable-mpi-java`` is specified, and a
JDK is found in a typical system default location.

If the JDK is not in a place where we automatically find it, you can
specify the location. For example, this is required on the Mac
platform as the JDK headers are located in a non-typical location. Two
options are available for this purpose:

#. ``--with-jdk-bindir=<foo>``: the location of ``javac`` and ``javah``
#. ``--with-jdk-headers=<bar>``: the directory containing ``jni.h``

Some example configurations are provided in Open MPI configuration
platform files under ``contrib/platform/hadoop``. These examples can
provide a starting point for your own custom configuration.

In summary, therefore, you can configure the system using the
following Java-related options::

  $ ./configure --with-platform=contrib/platform/hadoop/<your-platform> ...

or::

  $ ./configure --enable-mpi-java --with-jdk-bindir=<foo> --with-jdk-headers=<bar> ...

or simply::

  $ ./configure --enable-mpi-java ...

if JDK is in a "standard" place that ``configure`` can automatically
find.

Building Java MPI applications
------------------------------

The ``mpijavac`` wrapper compiler is available for compiling
Java-based MPI applications. It ensures that all required Open MPI
libraries and classpaths are defined.  For example:

.. code-block::

   $ mpijavac Hello.java

You can use the ``--showme`` option to see the full command line of
the Java compiler that is invoked:

.. code-block::

   $ mpijavac Hello.java --showme
   /usr/bin/javac -cp /opt/openmpi/lib/mpi.jar Hello.java

Note that if you are specifying a ``-cp`` argument on the command line
to pass your application-specific classpaths, Open MPI will *extend*
that argument to include the ``mpi.jar``:

.. code-block::

   $ mpijavac -cp /path/to/my/app.jar Hello.java --showme
   /usr/bin/javac -cp /path/to/my/app.jar:/opt/openmpi/lib/mpi.jar Hello.java

Similarly, if you have a ``CLASSPATH`` environment variable defined,
``mpijavac`` will convert that into a ``-cp`` argument and extend it
to include the ``mpi.jar``:

.. code-block::

   $ export CLASSPATH=/path/to/my/app.jar
   $ mpijavac Hello.java --showme
   /usr/bin/javac -cp /path/to/my/app.jar:/opt/openmpi/lib/mpi.jar Hello.java


Running Java MPI applications
-----------------------------

Once your application has been compiled, you can run it with the
standard ``mpirun`` command line::

  $ mpirun <options> java <your-java-options> <my-app>

``mpirun`` will detect the ``java`` token and ensure that the required
MPI libraries and class paths are defined to support execution. You
therefore do **not** need to specify the Java library path to the MPI
installation, nor the MPI classpath. Any classpath definitions
required for your application should be specified either on the
command line or via the ``CLASSPATH`` environment variable. Note that
the local directory will be added to the classpath if nothing is
specified.

.. note:: The ``java`` executable, all required libraries, and your
          application classes must be available on all nodes.

Basic usage of the Java bindings
--------------------------------

There is an MPI package that contains all classes of the MPI Java
bindings: ``Comm``, ``Datatype``, ``Request``, etc. These classes have a
direct correspondence with handle types defined by the MPI standard. MPI
primitives are just methods included in these classes. The convention
used for naming Java methods and classes is the usual camel-case
convention, e.g., the equivalent of ``MPI_File_set_info(fh,info)`` is
``fh.setInfo(info)``, where ``fh`` is an object of the class ``File``.

Apart from classes, the MPI package contains predefined public
attributes under a convenience class ``MPI``. Examples are the
predefined communicator ``MPI.COMM_WORLD`` and predefined datatypes such
as ``MPI.DOUBLE``. Also, MPI initialization and finalization are methods
of the ``MPI`` class and must be invoked by all MPI Java
applications. The following example illustrates these concepts:

.. code-block:: java

   import mpi.*;

   class ComputePi {

      public static void main(String args[]) throws MPIException {

          MPI.Init(args);

          int rank = MPI.COMM_WORLD.getRank(),
              size = MPI.COMM_WORLD.getSize(),
              nint = 100; // Intervals.
          double h = 1.0/(double)nint, sum = 0.0;

          for (int i=rank+1; i<=nint; i+=size) {
              double x = h * ((double)i - 0.5);
              sum += (4.0 / (1.0 + x * x));
          }

          double sBuf[] = { h * sum },
                 rBuf[] = new double[1];

          MPI.COMM_WORLD.reduce(sBuf, rBuf, 1, MPI.DOUBLE, MPI.SUM, 0);

          if (rank == 0) {
              System.out.println("PI: " + rBuf[0]);
          }
          MPI.Finalize();
      }
   }

Exception handling
------------------

The Java bindings in Open MPI support exception handling. By default,
errors are fatal, but this behavior can be changed. The Java API will
throw exceptions if the ``MPI.ERRORS_RETURN`` error handler is set:

.. code-block:: java

   MPI.COMM_WORLD.setErrhandler(MPI.ERRORS_RETURN);

If you add this statement to your program, it will show the line
where it breaks, instead of just crashing in case of an error.
Error-handling code can be separated from main application code by
means of try-catch blocks, for instance:

.. code-block:: java

   try
   {
       File file = new File(MPI.COMM_SELF, "filename", MPI.MODE_RDONLY);
   }
   catch(MPIException ex)
   {
       System.err.println("Error Message: "+ ex.getMessage());
       System.err.println("  Error Class: "+ ex.getErrorClass());
       ex.printStackTrace();
       System.exit(-1);
   }

How to specify buffers
----------------------

In MPI primitives that require a buffer (either send or receive), the
Java API admits a Java array. Since Java arrays can be relocated by
the Java runtime environment, the MPI Java bindings need to make a
copy of the contents of the array to a temporary buffer, then pass the
pointer to this buffer to the underlying C implementation. From the
practical point of view, this implies an overhead associated to all
buffers that are represented by Java arrays. The overhead is small for
small buffers but increases for large arrays.

There is a pool of temporary buffers with a default capacity of 64K.
If a temporary buffer of 64K or less is needed, then the buffer will
be obtained from the pool. But if the buffer is larger, then it will
be necessary to allocate the buffer and free it later.

The default capacity of pool buffers can be modified with an Open MPI
MCA parameter::

  $ mpirun --mca ompi_mpi_java_eager SIZE ...

The value of ``SIZE`` can be:

* ``N``: An integer number of bytes
* ``Nk``: An integer number (suffixed with ``k``) of kilobytes
* ``Nm``: An integer number (suffixed with ``m``) of megabytes

An alternative is to use "direct buffers" provided by standard classes
available in the Java SDK such as ``ByteBuffer``. For convenience,
Open MPI provides a few static methods ``new[Type]Buffer`` in the
``MPI`` class to create direct buffers for a number of basic
datatypes. Elements of the direct buffer can be accessed with methods
``put()`` and ``get()``, and the number of elements in the buffer can
be obtained with the method ``capacity()``. This example illustrates
its use:

.. code-block:: java

   int myself = MPI.COMM_WORLD.getRank();
   int tasks  = MPI.COMM_WORLD.getSize();

   IntBuffer in  = MPI.newIntBuffer(MAXLEN * tasks),
             out = MPI.newIntBuffer(MAXLEN);

   for (int i = 0; i < MAXLEN; i++)
       out.put(i, myself);      // fill the buffer with the rank

   Request request = MPI.COMM_WORLD.iAllGather(
                     out, MAXLEN, MPI.INT, in, MAXLEN, MPI.INT);
   request.waitFor();
   request.free();

   for (int i = 0; i < tasks; i++) {
       for (int k = 0; k < MAXLEN; k++) {
           if (in.get(k + i * MAXLEN) != i)
               throw new AssertionError("Unexpected value");
       }
   }

Direct buffers are available for: ``BYTE``, ``CHAR``, ``SHORT``,
``INT``, ``LONG``, ``FLOAT``, and ``DOUBLE``.

.. note:: There is no direct buffer for booleans.

Direct buffers are not a replacement for arrays, because they have
higher allocation and deallocation costs than arrays. In some cases
arrays will be a better choice. You can easily convert a buffer into
an array and vice versa.

.. important:: All non-blocking methods *must* use direct buffers.
               Only blocking methods can choose between arrays and
               direct buffers.

The above example also illustrates that it is necessary to call the
``free()`` method on objects whose class implements the ``Freeable``
interface. Otherwise, a memory leak will occur.

Specifying offsets in buffers
-----------------------------

In a C program, it is common to specify an offset in a array with
``&array[i]`` or ``array+i`` to send data starting from a given
position in the array. The equivalent form in the Java bindings is to
``slice()`` the buffer to start at an offset. Making a ``slice()`` on
a buffer is only necessary, when the offset is not zero. Slices work
for both arrays and direct buffers.

.. code-block:: java

   import static mpi.MPI.slice;
   // ...
   int numbers[] = new int[SIZE];
   // ...
   MPI.COMM_WORLD.send(slice(numbers, offset), count, MPI.INT, 1, 0);


Supported APIs
--------------

Complete MPI-3.1 coverage is provided in the Open MPI Java bindings,
with a few exceptions:

* The bindings for the ``MPI_Neighbor_alltoallw`` and
  ``MPI_Ineighbor_alltoallw`` functions are not implemented.

* Also excluded are functions that incorporate the concepts of
  explicit virtual memory addressing, such as
  ``MPI_Win_shared_query``.


Known issues
------------

There exist issues with the Omnipath (PSM2) interconnect involving
Java. The problems definitely exist in PSM2 v10.2; we have not tested
previous versions.

As of November 2016, there is not yet a PSM2 release that completely
fixes the issue.

The following ``mpirun`` command options will disable PSM2::

   shell$ mpirun ... --mca mtl ^psm2 java ...your-java-options... your-app-class


Questions?  Problems?
---------------------

The Java API documentation is generated at build time in
``$prefix/share/doc/openmpi/javadoc``.

Additionally, `this Cisco blog post
<https://blogs.cisco.com/performance/java-bindings-for-open-mpi>`_ has
quite a bit of information about the Open MPI Java bindings.

If you have any problems, or find any bugs, please feel free to report
them to `Open MPI user's mailing list
<https://www.open-mpi.org/community/lists/ompi.php>`_.

.. rubric:: Footnotes

.. [#ompijava] O. Vega-Gisbert, J. E. Roman, and J. M. Squyres. "Design
   and implementation of Java bindings in Open MPI". Parallel Comput.
   59: 1-20 (2016).

.. [#mpijava] M. Baker et al. "mpiJava: An object-oriented Java
   interface to MPI". In Parallel and Distributed Processing, LNCS
   vol. 1586, pp. 748-762, Springer (1999).